| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2257 |
lars |
9 |
* Copyright (c) 2014 - 2018, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
12 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
13 |
* in the Software without restriction, including without limitation the rights
|
|
|
14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
15 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
16 |
* furnished to do so, subject to the following conditions:
|
|
|
17 |
*
|
|
|
18 |
* The above copyright notice and this permission notice shall be included in
|
|
|
19 |
* all copies or substantial portions of the Software.
|
|
|
20 |
*
|
|
|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
27 |
* THE SOFTWARE.
|
|
|
28 |
*
|
|
|
29 |
* @package CodeIgniter
|
|
|
30 |
* @author EllisLab Dev Team
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2257 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
33 |
* @license http://opensource.org/licenses/MIT MIT License
|
|
|
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 3.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* PDO SQLite Database Adapter Class
|
|
|
42 |
*
|
|
|
43 |
* Note: _DB is an extender class that the app controller
|
|
|
44 |
* creates dynamically based on whether the query builder
|
|
|
45 |
* class is being used or not.
|
|
|
46 |
*
|
|
|
47 |
* @package CodeIgniter
|
|
|
48 |
* @subpackage Drivers
|
|
|
49 |
* @category Database
|
|
|
50 |
* @author EllisLab Dev Team
|
|
|
51 |
* @link https://codeigniter.com/user_guide/database/
|
|
|
52 |
*/
|
|
|
53 |
class CI_DB_pdo_sqlite_driver extends CI_DB_pdo_driver {
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Sub-driver
|
|
|
57 |
*
|
|
|
58 |
* @var string
|
|
|
59 |
*/
|
|
|
60 |
public $subdriver = 'sqlite';
|
|
|
61 |
|
|
|
62 |
// --------------------------------------------------------------------
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* ORDER BY random keyword
|
|
|
66 |
*
|
|
|
67 |
* @var array
|
|
|
68 |
*/
|
| 2049 |
lars |
69 |
protected $_random_keyword = array('RANDOM()', 'RANDOM()');
|
| 68 |
lars |
70 |
|
|
|
71 |
// --------------------------------------------------------------------
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Class constructor
|
|
|
75 |
*
|
|
|
76 |
* Builds the DSN if not already set.
|
|
|
77 |
*
|
|
|
78 |
* @param array $params
|
|
|
79 |
* @return void
|
|
|
80 |
*/
|
|
|
81 |
public function __construct($params)
|
|
|
82 |
{
|
|
|
83 |
parent::__construct($params);
|
|
|
84 |
|
|
|
85 |
if (empty($this->dsn))
|
|
|
86 |
{
|
|
|
87 |
$this->dsn = 'sqlite:';
|
|
|
88 |
|
|
|
89 |
if (empty($this->database) && empty($this->hostname))
|
|
|
90 |
{
|
|
|
91 |
$this->database = ':memory:';
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$this->database = empty($this->database) ? $this->hostname : $this->database;
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// --------------------------------------------------------------------
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Show table query
|
|
|
102 |
*
|
|
|
103 |
* Generates a platform-specific query string so that the table names can be fetched
|
|
|
104 |
*
|
|
|
105 |
* @param bool $prefix_limit
|
|
|
106 |
* @return string
|
|
|
107 |
*/
|
|
|
108 |
protected function _list_tables($prefix_limit = FALSE)
|
|
|
109 |
{
|
|
|
110 |
$sql = 'SELECT "NAME" FROM "SQLITE_MASTER" WHERE "TYPE" = \'table\'';
|
|
|
111 |
|
|
|
112 |
if ($prefix_limit === TRUE && $this->dbprefix !== '')
|
|
|
113 |
{
|
|
|
114 |
return $sql.' AND "NAME" LIKE \''.$this->escape_like_str($this->dbprefix)."%' "
|
|
|
115 |
.sprintf($this->_like_escape_str, $this->_like_escape_chr);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
return $sql;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// --------------------------------------------------------------------
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Fetch Field Names
|
|
|
125 |
*
|
|
|
126 |
* @param string $table Table name
|
|
|
127 |
* @return array
|
|
|
128 |
*/
|
|
|
129 |
public function list_fields($table)
|
|
|
130 |
{
|
|
|
131 |
// Is there a cached result?
|
|
|
132 |
if (isset($this->data_cache['field_names'][$table]))
|
|
|
133 |
{
|
|
|
134 |
return $this->data_cache['field_names'][$table];
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
if (($result = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
|
|
|
138 |
{
|
|
|
139 |
return FALSE;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
$this->data_cache['field_names'][$table] = array();
|
|
|
143 |
foreach ($result->result_array() as $row)
|
|
|
144 |
{
|
|
|
145 |
$this->data_cache['field_names'][$table][] = $row['name'];
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
return $this->data_cache['field_names'][$table];
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
// --------------------------------------------------------------------
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Returns an object with field data
|
|
|
155 |
*
|
|
|
156 |
* @param string $table
|
|
|
157 |
* @return array
|
|
|
158 |
*/
|
|
|
159 |
public function field_data($table)
|
|
|
160 |
{
|
|
|
161 |
if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE)
|
|
|
162 |
{
|
|
|
163 |
return FALSE;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
$query = $query->result_array();
|
|
|
167 |
if (empty($query))
|
|
|
168 |
{
|
|
|
169 |
return FALSE;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
$retval = array();
|
|
|
173 |
for ($i = 0, $c = count($query); $i < $c; $i++)
|
|
|
174 |
{
|
|
|
175 |
$retval[$i] = new stdClass();
|
|
|
176 |
$retval[$i]->name = $query[$i]['name'];
|
|
|
177 |
$retval[$i]->type = $query[$i]['type'];
|
|
|
178 |
$retval[$i]->max_length = NULL;
|
|
|
179 |
$retval[$i]->default = $query[$i]['dflt_value'];
|
|
|
180 |
$retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
return $retval;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
// --------------------------------------------------------------------
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* Replace statement
|
|
|
190 |
*
|
|
|
191 |
* @param string $table Table name
|
|
|
192 |
* @param array $keys INSERT keys
|
|
|
193 |
* @param array $values INSERT values
|
|
|
194 |
* @return string
|
|
|
195 |
*/
|
|
|
196 |
protected function _replace($table, $keys, $values)
|
|
|
197 |
{
|
|
|
198 |
return 'INSERT OR '.parent::_replace($table, $keys, $values);
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
// --------------------------------------------------------------------
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Truncate statement
|
|
|
205 |
*
|
|
|
206 |
* Generates a platform-specific truncate string from the supplied data
|
|
|
207 |
*
|
|
|
208 |
* If the database does not support the TRUNCATE statement,
|
|
|
209 |
* then this method maps to 'DELETE FROM table'
|
|
|
210 |
*
|
|
|
211 |
* @param string $table
|
|
|
212 |
* @return string
|
|
|
213 |
*/
|
|
|
214 |
protected function _truncate($table)
|
|
|
215 |
{
|
|
|
216 |
return 'DELETE FROM '.$table;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
}
|