123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class CI_DB_pdo_driver extends CI_DB {
-
- public $dbdriver = 'pdo';
-
- public $options = array();
-
-
- public function __construct($params)
- {
- parent::__construct($params);
- if (preg_match('/([^:]+):/', $this->dsn, $match) && count($match) === 2)
- {
-
-
- $this->subdriver = $match[1];
- return;
- }
-
- elseif (preg_match('/([^:]+):/', $this->hostname, $match) && count($match) === 2)
- {
- $this->dsn = $this->hostname;
- $this->hostname = NULL;
- $this->subdriver = $match[1];
- return;
- }
- elseif (in_array($this->subdriver, array('mssql', 'sybase'), TRUE))
- {
- $this->subdriver = 'dblib';
- }
- elseif ($this->subdriver === '4D')
- {
- $this->subdriver = '4d';
- }
- elseif ( ! in_array($this->subdriver, array('4d', 'cubrid', 'dblib', 'firebird', 'ibm', 'informix', 'mysql', 'oci', 'odbc', 'pgsql', 'sqlite', 'sqlsrv'), TRUE))
- {
- log_message('error', 'PDO: Invalid or non-existent subdriver');
- if ($this->db_debug)
- {
- show_error('Invalid or non-existent PDO subdriver');
- }
- }
- $this->dsn = NULL;
- }
-
-
- public function db_connect($persistent = FALSE)
- {
- if ($persistent === TRUE)
- {
- $this->options[PDO::ATTR_PERSISTENT] = TRUE;
- }
- try
- {
- return new PDO($this->dsn, $this->username, $this->password, $this->options);
- }
- catch (PDOException $e)
- {
- if ($this->db_debug && empty($this->failover))
- {
- $this->display_error($e->getMessage(), '', TRUE);
- }
- return FALSE;
- }
- }
-
-
- public function version()
- {
- if (isset($this->data_cache['version']))
- {
- return $this->data_cache['version'];
- }
-
- try
- {
- return $this->data_cache['version'] = $this->conn_id->getAttribute(PDO::ATTR_SERVER_VERSION);
- }
- catch (PDOException $e)
- {
- return parent::version();
- }
- }
-
-
- protected function _execute($sql)
- {
- return $this->conn_id->query($sql);
- }
-
-
- protected function _trans_begin()
- {
- return $this->conn_id->beginTransaction();
- }
-
-
- protected function _trans_commit()
- {
- return $this->conn_id->commit();
- }
-
-
- protected function _trans_rollback()
- {
- return $this->conn_id->rollBack();
- }
-
-
- protected function _escape_str($str)
- {
-
- $str = $this->conn_id->quote($str);
-
- return ($str[0] === "'")
- ? substr($str, 1, -1)
- : $str;
- }
-
-
- public function affected_rows()
- {
- return is_object($this->result_id) ? $this->result_id->rowCount() : 0;
- }
-
-
- public function insert_id($name = NULL)
- {
- return $this->conn_id->lastInsertId($name);
- }
-
-
- protected function _field_data($table)
- {
- return 'SELECT TOP 1 * FROM '.$this->protect_identifiers($table);
- }
-
-
- public function error()
- {
- $error = array('code' => '00000', 'message' => '');
- $pdo_error = $this->conn_id->errorInfo();
- if (empty($pdo_error[0]))
- {
- return $error;
- }
- $error['code'] = isset($pdo_error[1]) ? $pdo_error[0].'/'.$pdo_error[1] : $pdo_error[0];
- if (isset($pdo_error[2]))
- {
- $error['message'] = $pdo_error[2];
- }
- return $error;
- }
-
-
- protected function _update_batch($table, $values, $index)
- {
- $ids = array();
- foreach ($values as $key => $val)
- {
- $ids[] = $val[$index];
- foreach (array_keys($val) as $field)
- {
- if ($field !== $index)
- {
- $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
- }
- }
- }
- $cases = '';
- foreach ($final as $k => $v)
- {
- $cases .= $k.' = CASE '."\n";
- foreach ($v as $row)
- {
- $cases .= $row."\n";
- }
- $cases .= 'ELSE '.$k.' END, ';
- }
- $this->where($index.' IN('.implode(',', $ids).')', NULL, FALSE);
- return 'UPDATE '.$table.' SET '.substr($cases, 0, -2).$this->_compile_wh('qb_where');
- }
-
-
- protected function _truncate($table)
- {
- return 'TRUNCATE TABLE '.$table;
- }
- }
|