From ab347586ef289e960ab7cfad32574e526cdcce0b Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 23 Aug 2011 12:29:29 -0400 Subject: Got PDO working --- application/config/database.php | 2 +- system/database/drivers/pdo/pdo_driver.php | 29 ++++---- system/database/drivers/pdo/pdo_result.php | 106 +++++++---------------------- 3 files changed, 43 insertions(+), 94 deletions(-) diff --git a/application/config/database.php b/application/config/database.php index b4b34bf66..5a84a471a 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -17,7 +17,7 @@ | ['password'] The password used to connect to the database | ['database'] The name of the database you want to connect to | ['dbdriver'] The database type. ie: mysql. Currently supported: - mysql, mysqli, postgre, odbc, mssql, sqlite, oci8 + mysql, mysqli, pdo, postgre, odbc, mssql, sqlite, oci8 | ['dbprefix'] You can add an optional prefix, which will be added | to the table name when using the Active Record class | ['pconnect'] TRUE/FALSE - Whether to use a persistent connection diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index 000ac083b..3adc5f5ef 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -51,6 +51,9 @@ class CI_DB_pdo_driver extends CI_DB { function CI_DB_pdo_driver($params) { parent::CI_DB($params); + + $this->hostname = $this->hostname . ";dbname=".$this->database; + $this->trans_enabled = FALSE; $this->_random_keyword = ' RND('.time().')'; // database specific random keyword } @@ -63,7 +66,8 @@ class CI_DB_pdo_driver extends CI_DB { */ function db_connect() { - return new PDO($this->hostname, $this->username, $this->password, array( + return new PDO($this->hostname,$this->username,$this->password, array( + PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT )); } @@ -77,7 +81,9 @@ class CI_DB_pdo_driver extends CI_DB { */ function db_pconnect() { - return new PDO($this->hostname, $this->username, $this->password, array( + return new PDO($this->hostname,$this->username,$this->password, array( + PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT, + PDO::ATTR_PERSISTENT => true )); } @@ -152,7 +158,7 @@ class CI_DB_pdo_driver extends CI_DB { function _execute($sql) { $sql = $this->_prep_query($sql); - return @pdo_exec($this->conn_id, $sql); + return $this->conn_id->query($sql); } // -------------------------------------------------------------------- @@ -197,7 +203,7 @@ class CI_DB_pdo_driver extends CI_DB { // even if the queries produce a successful result. $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; - return pdo_autocommit($this->conn_id, FALSE); + return $this->conn_id->beginTransaction(); } // -------------------------------------------------------------------- @@ -221,8 +227,7 @@ class CI_DB_pdo_driver extends CI_DB { return TRUE; } - $ret = pdo_commit($this->conn_id); - pdo_autocommit($this->conn_id, TRUE); + $ret = $this->conn->commit(); return $ret; } @@ -247,8 +252,7 @@ class CI_DB_pdo_driver extends CI_DB { return TRUE; } - $ret = pdo_rollback($this->conn_id); - pdo_autocommit($this->conn_id, TRUE); + $ret = $this->conn_id->rollBack(); return $ret; } @@ -311,7 +315,7 @@ class CI_DB_pdo_driver extends CI_DB { */ function insert_id() { - return @pdo_insert_id($this->conn_id); + return $this->conn_id->lastInsertId(); } // -------------------------------------------------------------------- @@ -411,7 +415,8 @@ class CI_DB_pdo_driver extends CI_DB { */ function _error_message() { - return pdo_errormsg($this->conn_id); + $error_array = $this->conn_id->errorInfo(); + return $error_array[2]; } // -------------------------------------------------------------------- @@ -424,7 +429,7 @@ class CI_DB_pdo_driver extends CI_DB { */ function _error_number() { - return pdo_error($this->conn_id); + return $this->conn_id->errorCode(); } // -------------------------------------------------------------------- @@ -488,7 +493,7 @@ class CI_DB_pdo_driver extends CI_DB { $tables = array($tables); } - return '('.implode(', ', $tables).')'; + return (count($tables) == 1) ? $tables[0] : '('.implode(', ', $tables).')'; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/pdo_result.php b/system/database/drivers/pdo/pdo_result.php index 161a77bf8..c38658626 100644 --- a/system/database/drivers/pdo/pdo_result.php +++ b/system/database/drivers/pdo/pdo_result.php @@ -34,7 +34,7 @@ class CI_DB_pdo_result extends CI_DB_result { */ function num_rows() { - return @pdo_num_rows($this->result_id); + return $this->result_id->rowCount(); } // -------------------------------------------------------------------- @@ -47,7 +47,7 @@ class CI_DB_pdo_result extends CI_DB_result { */ function num_fields() { - return @pdo_num_fields($this->result_id); + return $this->result_id->columnCount(); } // -------------------------------------------------------------------- @@ -62,13 +62,11 @@ class CI_DB_pdo_result extends CI_DB_result { */ function list_fields() { - $field_names = array(); - for ($i = 0; $i < $this->num_fields(); $i++) + if ($this->db->db_debug) { - $field_names[] = pdo_field_name($this->result_id, $i); + return $this->db->display_error('db_unsuported_feature'); } - - return $field_names; + return FALSE; } // -------------------------------------------------------------------- @@ -83,20 +81,25 @@ class CI_DB_pdo_result extends CI_DB_result { */ function field_data() { - $retval = array(); - for ($i = 0; $i < $this->num_fields(); $i++) + $data = array(); + + try { - $F = new stdClass(); - $F->name = pdo_field_name($this->result_id, $i); - $F->type = pdo_field_type($this->result_id, $i); - $F->max_length = pdo_field_len($this->result_id, $i); - $F->primary_key = 0; - $F->default = ''; - - $retval[] = $F; + for($i = 0; $i < $this->num_fields(); $i++) + { + $data[] = $this->result_id->getColumnMeta($i); + } + + return $data; + } + catch (Exception $e) + { + if ($this->db->db_debug) + { + return $this->db->display_error('db_unsuported_feature'); + } + return FALSE; } - - return $retval; } // -------------------------------------------------------------------- @@ -144,14 +147,7 @@ class CI_DB_pdo_result extends CI_DB_result { */ function _fetch_assoc() { - if (function_exists('pdo_fetch_object')) - { - return pdo_fetch_array($this->result_id); - } - else - { - return $this->_pdo_fetch_array($this->result_id); - } + return $this->result_id->fetch(PDO::FETCH_ASSOC); } // -------------------------------------------------------------------- @@ -165,60 +161,8 @@ class CI_DB_pdo_result extends CI_DB_result { * @return object */ function _fetch_object() - { - if (function_exists('pdo_fetch_object')) - { - return pdo_fetch_object($this->result_id); - } - else - { - return $this->_pdo_fetch_object($this->result_id); - } - } - - - /** - * Result - object - * - * subsititutes the pdo_fetch_object function when - * not available (pdo_fetch_object requires unixPDO) - * - * @access private - * @return object - */ - function _pdo_fetch_object(& $pdo_result) { - $rs = array(); - $rs_obj = FALSE; - if (pdo_fetch_into($pdo_result, $rs)) { - foreach ($rs as $k=>$v) { - $field_name= pdo_field_name($pdo_result, $k+1); - $rs_obj->$field_name = $v; - } - } - return $rs_obj; - } - - - /** - * Result - array - * - * subsititutes the pdo_fetch_array function when - * not available (pdo_fetch_array requires unixPDO) - * - * @access private - * @return array - */ - function _pdo_fetch_array(& $pdo_result) { - $rs = array(); - $rs_assoc = FALSE; - if (pdo_fetch_into($pdo_result, $rs)) { - $rs_assoc=array(); - foreach ($rs as $k=>$v) { - $field_name= pdo_field_name($pdo_result, $k+1); - $rs_assoc[$field_name] = $v; - } - } - return $rs_assoc; + { + return $this->result_id->fetchObject(); } } -- cgit v1.2.3-24-g4f1b