00001 <?php
00013 class DB_PDO extends DB_Base
00014 {
00015 function DB_PDO($conn, $persistent) {
00016 $this->safesql = new SafeSQL_ANSI;
00017 $this->type = 'pdo';
00018
00019 $att = array(PDO::ATTR_PERSISTENT => $persistent);
00020 try {
00021 $this->conn = new PDO($conn['dsn'], $conn['user'], $conn['pass'], $att);
00022 } catch(PDOException $e) {
00023 $this->_catch("Unable to connect to database: ".$e->getMessage());
00024 return false;
00025 }
00026
00027 if(!$this->conn) {
00028 $this->_catch("Unable to connect to database!");
00029 return false;
00030 }
00031
00032 $this->driver = $this->conn->getAttribute(PDO::ATTR_DRIVER_NAME);
00033 if($this->driver == 'mysql' && DB_MYSQL_ANSI_MODE !== false) {
00034
00035
00036 $this->run_query("SET SESSION sql_mode='ANSI'");
00037 }
00038
00039 return true;
00040 }
00041
00042 function _catch($msg="") {
00043 if($this->conn) {
00044 $this->error = $this->conn->errorInfo();
00045 if(!$this->error) return true;
00046 $this->error = $this->error[2];
00047 $this->error($msg."<br>{$this->query}\n {$this->error}");
00048 } else {
00049 $this->error($msg);
00050 }
00051 }
00052
00053 function get_table_defn($table) {
00054 if($this->driver == 'mysql') {
00055 return $this->get_all_pair("EXPLAIN \"$table\"");
00056 }
00057
00058 $defn = array();
00059 $q = $this->run_query("SELECT * FROM \"$table\" LIMIT 1");
00060 for($i = 0; $i < $this->num_fields($q); $i++) {
00061 $col = $q->getColumnMeta($i);
00062 $defn[$col['name']] = $col['native_type'];
00063 }
00064 return $defn;
00065 }
00066
00067 function run_query($sql) {
00068 return $this->conn->query($sql);
00069 }
00070
00071 function fetch_row(&$q) {
00072 return $q->fetch(PDO::FETCH_NUM);
00073 }
00074
00075 function fetch_array(&$q) {
00076 return $q->fetch(PDO::FETCH_ASSOC);
00077 }
00078
00079 function fetch_field(&$q, $num) {
00080 return $q->fetchColumn($num);
00081 }
00082
00083 function num_fields(&$q) {
00084 return $q->columnCount();
00085 }
00086
00087 function num_rows(&$q) {
00088 return $q->rowCount();
00089 }
00090
00091 function get_insert_id() {
00092 return $this->conn->lastInsertId();
00093 }
00094
00095 function free_result($q) {
00096 return true;
00097 }
00098 }
00099
00100 ?>