Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame^] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * SQLite Database Adapter Class |
| 31 | * |
| 32 | * Note: _DB is an extender class that the app controller |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 33 | * creates dynamically based on whether the query builder |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 34 | * class is being used or not. |
| 35 | * |
| 36 | * @package CodeIgniter |
| 37 | * @subpackage Drivers |
| 38 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 39 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | * @link http://codeigniter.com/user_guide/database/ |
| 41 | */ |
| 42 | class CI_DB_sqlite_driver extends CI_DB { |
| 43 | |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 44 | public $dbdriver = 'sqlite'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 45 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | // The character used to escape with - not needed for SQLite |
Andrey Andreev | d33e24c | 2012-02-12 03:23:07 +0200 | [diff] [blame] | 47 | protected $_escape_char = '"'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 49 | protected $_random_keyword = ' Random()'; // database specific random keyword |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Non-persistent database connection |
| 53 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 55 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 56 | public function db_connect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | { |
| 58 | if ( ! $conn_id = @sqlite_open($this->database, FILE_WRITE_MODE, $error)) |
| 59 | { |
| 60 | log_message('error', $error); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | if ($this->db_debug) |
| 63 | { |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 64 | $this->display_error($error, '', TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 66 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | return FALSE; |
| 68 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | return $conn_id; |
| 71 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 72 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | // -------------------------------------------------------------------- |
| 74 | |
| 75 | /** |
| 76 | * Persistent database connection |
| 77 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 80 | public function db_pconnect() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | { |
| 82 | if ( ! $conn_id = @sqlite_popen($this->database, FILE_WRITE_MODE, $error)) |
| 83 | { |
| 84 | log_message('error', $error); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | if ($this->db_debug) |
| 87 | { |
Derek Allard | fac8fbc | 2010-02-05 16:14:49 +0000 | [diff] [blame] | 88 | $this->display_error($error, '', TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | return FALSE; |
| 92 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 93 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | return $conn_id; |
| 95 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 96 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | // -------------------------------------------------------------------- |
| 98 | |
| 99 | /** |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 100 | * Database version number |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | * @return string |
| 103 | */ |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 104 | public function version() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | { |
Andrey Andreev | 08856b8 | 2012-03-03 03:19:28 +0200 | [diff] [blame] | 106 | return isset($this->data_cache['version']) |
| 107 | ? $this->data_cache['version'] |
| 108 | : $this->data_cache['version'] = sqlite_libversion(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 110 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 111 | // -------------------------------------------------------------------- |
| 112 | |
| 113 | /** |
| 114 | * Execute the query |
| 115 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | * @param string an SQL query |
| 117 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 118 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 119 | protected function _execute($sql) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | { |
Andrey Andreev | a0c2785 | 2012-03-02 13:49:28 +0200 | [diff] [blame] | 121 | return $this->is_write_type($sql) |
| 122 | ? @sqlite_exec($this->conn_id, $sql) |
| 123 | : @sqlite_query($this->conn_id, $sql); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | // -------------------------------------------------------------------- |
| 127 | |
| 128 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | * Begin Transaction |
| 130 | * |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 131 | * @param bool $test_mode = FALSE |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | * @return bool |
| 133 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 134 | public function trans_begin($test_mode = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 137 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | { |
| 139 | return TRUE; |
| 140 | } |
| 141 | |
| 142 | // Reset the transaction failure flag. |
| 143 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 144 | // even if the queries produce a successful result. |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 145 | $this->_trans_failure = ($test_mode === TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | |
| 147 | $this->simple_query('BEGIN TRANSACTION'); |
| 148 | return TRUE; |
| 149 | } |
| 150 | |
| 151 | // -------------------------------------------------------------------- |
| 152 | |
| 153 | /** |
| 154 | * Commit Transaction |
| 155 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | * @return bool |
| 157 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 158 | public function trans_commit() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 160 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 161 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | { |
| 163 | return TRUE; |
| 164 | } |
| 165 | |
| 166 | $this->simple_query('COMMIT'); |
| 167 | return TRUE; |
| 168 | } |
| 169 | |
| 170 | // -------------------------------------------------------------------- |
| 171 | |
| 172 | /** |
| 173 | * Rollback Transaction |
| 174 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | * @return bool |
| 176 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 177 | public function trans_rollback() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | // When transactions are nested we only begin/commit/rollback the outermost ones |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 180 | if ( ! $this->trans_enabled OR $this->_trans_depth > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | { |
| 182 | return TRUE; |
| 183 | } |
| 184 | |
| 185 | $this->simple_query('ROLLBACK'); |
| 186 | return TRUE; |
| 187 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | // -------------------------------------------------------------------- |
| 190 | |
| 191 | /** |
| 192 | * Escape String |
| 193 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 195 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | * @return string |
| 197 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 198 | public function escape_str($str, $like = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 200 | if (is_array($str)) |
| 201 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 202 | foreach ($str as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 204 | $str[$key] = $this->escape_str($val, $like); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return $str; |
| 208 | } |
| 209 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 210 | $str = sqlite_escape_string($str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 212 | // escape LIKE condition wildcards |
| 213 | if ($like === TRUE) |
| 214 | { |
Andrey Andreev | 94708bd | 2012-03-26 12:09:28 +0300 | [diff] [blame] | 215 | return str_replace(array($this->_like_escape_chr, '%', '_'), |
Andrey Andreev | 830f5af | 2012-03-26 12:11:38 +0300 | [diff] [blame] | 216 | array($this->_like_escape_chr.$this->_like_escape_chr, $this->_like_escape_chr.'%', $this->_like_escape_chr.'_'), |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 217 | $str); |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 218 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 219 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 220 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 222 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | // -------------------------------------------------------------------- |
| 224 | |
| 225 | /** |
| 226 | * Affected Rows |
| 227 | * |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 228 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 230 | public function affected_rows() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | { |
| 232 | return sqlite_changes($this->conn_id); |
| 233 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | // -------------------------------------------------------------------- |
| 236 | |
| 237 | /** |
| 238 | * Insert ID |
| 239 | * |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 240 | * @return int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 242 | public function insert_id() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | { |
| 244 | return @sqlite_last_insert_rowid($this->conn_id); |
| 245 | } |
| 246 | |
| 247 | // -------------------------------------------------------------------- |
| 248 | |
| 249 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 250 | * List table query |
| 251 | * |
| 252 | * Generates a platform-specific query string so that the table names can be fetched |
| 253 | * |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 254 | * @param bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | * @return string |
| 256 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 257 | protected function _list_tables($prefix_limit = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | { |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 259 | $sql = "SELECT name FROM sqlite_master WHERE type='table'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 261 | if ($prefix_limit !== FALSE && $this->dbprefix != '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | { |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 263 | return $sql." AND 'name' LIKE '".$this->escape_like_str($this->dbprefix)."%' ".sprintf($this->_like_escape_str, $this->_like_escape_chr); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | } |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 265 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | return $sql; |
| 267 | } |
| 268 | |
| 269 | // -------------------------------------------------------------------- |
| 270 | |
| 271 | /** |
| 272 | * Show column query |
| 273 | * |
| 274 | * Generates a platform-specific query string so that the column names can be fetched |
| 275 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | * @param string the table name |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 277 | * @return bool |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 278 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 279 | protected function _list_columns($table = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | { |
| 281 | // Not supported |
| 282 | return FALSE; |
| 283 | } |
| 284 | |
| 285 | // -------------------------------------------------------------------- |
| 286 | |
| 287 | /** |
| 288 | * Field data query |
| 289 | * |
| 290 | * Generates a platform-specific query so that the column data can be retrieved |
| 291 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | * @param string the table name |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 293 | * @return string |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 295 | protected function _field_data($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | { |
Andrey Andreev | 9411557 | 2012-06-07 21:34:56 +0300 | [diff] [blame] | 297 | return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // -------------------------------------------------------------------- |
| 301 | |
| 302 | /** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 303 | * Error |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 305 | * Returns an array containing code and message of the last |
| 306 | * database error that has occured. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | * |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 308 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | */ |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 310 | public function error() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | { |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 312 | $error = array('code' => sqlite_last_error($this->conn_id)); |
| 313 | $error['message'] = sqlite_error_string($error['code']); |
| 314 | return $error; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | // -------------------------------------------------------------------- |
| 318 | |
| 319 | /** |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 320 | * Replace statement |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | * |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 322 | * Generates a platform-specific replace string from the supplied data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | * @param string the table name |
| 325 | * @param array the insert keys |
| 326 | * @param array the insert values |
| 327 | * @return string |
| 328 | */ |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 329 | protected function _replace($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | { |
Andrey Andreev | 17ceeae | 2012-04-05 15:38:30 +0300 | [diff] [blame] | 331 | return 'INSERT OR '.parent::_replace($table, $keys, $values); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | // -------------------------------------------------------------------- |
| 335 | |
| 336 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 337 | * Truncate statement |
| 338 | * |
| 339 | * Generates a platform-specific truncate string from the supplied data |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 340 | * |
| 341 | * If the database does not support the truncate() command, |
| 342 | * then this function maps to 'DELETE FROM table' |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | * @param string the table name |
| 345 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 346 | */ |
Andrey Andreev | d903878 | 2012-01-26 12:38:49 +0200 | [diff] [blame] | 347 | protected function _truncate($table) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | { |
Andrey Andreev | a6fe36e | 2012-04-05 16:00:32 +0300 | [diff] [blame] | 349 | return 'DELETE FROM '.$table; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 351 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | // -------------------------------------------------------------------- |
| 353 | |
| 354 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | * Close DB Connection |
| 356 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | * @return void |
| 358 | */ |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 359 | protected function _close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | { |
Andrey Andreev | 79922c0 | 2012-05-23 12:27:17 +0300 | [diff] [blame] | 361 | @sqlite_close($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | /* End of file sqlite_driver.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 367 | /* Location: ./system/database/drivers/sqlite/sqlite_driver.php */ |