123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class CI_DB_sqlite3_driver extends CI_DB {
-
- public $dbdriver = 'sqlite3';
-
-
- protected $_random_keyword = array('RANDOM()', 'RANDOM()');
-
-
- public function db_connect($persistent = FALSE)
- {
- if ($persistent)
- {
- log_message('debug', 'SQLite3 doesn\'t support persistent connections');
- }
- try
- {
- return ( ! $this->password)
- ? new SQLite3($this->database)
- : new SQLite3($this->database, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $this->password);
- }
- catch (Exception $e)
- {
- return FALSE;
- }
- }
-
-
- public function version()
- {
- if (isset($this->data_cache['version']))
- {
- return $this->data_cache['version'];
- }
- $version = SQLite3::version();
- return $this->data_cache['version'] = $version['versionString'];
- }
-
-
- protected function _execute($sql)
- {
- return $this->is_write_type($sql)
- ? $this->conn_id->exec($sql)
- : $this->conn_id->query($sql);
- }
-
-
- protected function _trans_begin()
- {
- return $this->conn_id->exec('BEGIN TRANSACTION');
- }
-
-
- protected function _trans_commit()
- {
- return $this->conn_id->exec('END TRANSACTION');
- }
-
-
- protected function _trans_rollback()
- {
- return $this->conn_id->exec('ROLLBACK');
- }
-
-
- protected function _escape_str($str)
- {
- return $this->conn_id->escapeString($str);
- }
-
-
- public function affected_rows()
- {
- return $this->conn_id->changes();
- }
-
-
- public function insert_id()
- {
- return $this->conn_id->lastInsertRowID();
- }
-
-
- protected function _list_tables($prefix_limit = FALSE)
- {
- return 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\''
- .(($prefix_limit !== FALSE && $this->dbprefix != '')
- ? ' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix).'%\' '.sprintf($this->_like_escape_str, $this->_like_escape_chr)
- : '');
- }
-
-
- public function list_fields($table)
- {
- if (($result = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
- {
- return FALSE;
- }
- $fields = array();
- foreach ($result->result_array() as $row)
- {
- $fields[] = $row['name'];
- }
- return $fields;
- }
-
-
- public function field_data($table)
- {
- if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
- {
- return FALSE;
- }
- $query = $query->result_array();
- if (empty($query))
- {
- return FALSE;
- }
- $retval = array();
- for ($i = 0, $c = count($query); $i < $c; $i++)
- {
- $retval[$i] = new stdClass();
- $retval[$i]->name = $query[$i]['name'];
- $retval[$i]->type = $query[$i]['type'];
- $retval[$i]->max_length = NULL;
- $retval[$i]->default = $query[$i]['dflt_value'];
- $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
- }
- return $retval;
- }
-
-
- public function error()
- {
- return array('code' => $this->conn_id->lastErrorCode(), 'message' => $this->conn_id->lastErrorMsg());
- }
-
-
- protected function _replace($table, $keys, $values)
- {
- return 'INSERT OR '.parent::_replace($table, $keys, $values);
- }
-
-
- protected function _truncate($table)
- {
- return 'DELETE FROM '.$table;
- }
-
-
- protected function _close()
- {
- $this->conn_id->close();
- }
- }
|