/
Sqlite.php
242 lines (212 loc) · 6.61 KB
/
Sqlite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
*
* This file is part of the Aura Project for PHP.
*
* @package Aura.Sql
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Sql\Adapter;
/**
*
* SQLite adapter.
*
* @package Aura.Sql
*
*/
class Sqlite extends AbstractAdapter
{
/**
*
* The string used for SQLite autoincrement data types.
*
* This is different for versions 2 and 3 of SQLite.
*
* @var string
*
*/
protected $autoinc_string = 'INTEGER PRIMARY KEY AUTOINCREMENT';
/**
*
* The PDO DSN for the connection, typically a file path.
*
* @var string
*
*/
protected $dsn = null;
/**
*
* The PDO type prefix.
*
* @var string
*
*/
protected $dsn_prefix = 'sqlite';
/**
*
* The quote character before an entity name (table, index, etc).
*
* @var string
*
*/
protected $quote_name_prefix = '"';
/**
*
* The quote character after an entity name (table, index, etc).
*
* @var string
*
*/
protected $quote_name_suffix = '"';
/**
*
* Returns a list of tables in the database.
*
* @param string $schema Optionally, pass a schema name to get the list
* of tables in this schema.
*
* @return array The list of tables in the database.
*
*/
public function fetchTableList($schema = null)
{
if ($schema) {
$cmd = "
SELECT name FROM {$schema}.sqlite_master WHERE type = 'table'
ORDER BY name
";
} else {
$cmd = "
SELECT name FROM sqlite_master WHERE type = 'table'
UNION ALL
SELECT name FROM sqlite_temp_master WHERE type = 'table'
ORDER BY name
";
}
return $this->fetchCol($cmd);
}
/**
*
* Describes the columns in a table.
*
* @param string $spec Return the columns in this table. This may be just
* a `table` name, or a `schema.table` name.
*
* @return array An associative array where the key is the column name
* and the value is a Column object.
*
*/
public function fetchTableCols($spec)
{
list($schema, $table) = $this->splitName($spec);
// strip non-word characters to try and prevent SQL injections
$table = preg_replace('/[^\w]/', '', $table);
// is there a schema?
if ($schema) {
// sanitize and add a dot
$schema = preg_replace('/[^\w]/', '', $schema) . '.';
}
// where the description will be stored
$cols = array();
// get the CREATE TABLE sql; need this for finding autoincrement cols
$cmd = "
SELECT sql FROM {$schema}sqlite_master
WHERE type = 'table' AND name = :table
";
$create_table = $this->fetchValue($cmd, array('table' => $table));
// get the column descriptions
$table = $this->quoteName($table);
$raw_cols = $this->fetchAll("PRAGMA {$schema}TABLE_INFO($table)");
// loop through the result rows; each describes a column.
foreach ($raw_cols as $val) {
$name = $val['name'];
list($type, $size, $scale) = $this->getTypeSizeScope($val['type']);
// find autoincrement column in CREATE TABLE sql.
$autoinc_find = str_replace(' ', '\s+', $this->autoinc_string);
$find = "(\"$name\"|\'$name\'|`$name`|\[$name\]|\\b$name)"
. "\s+$autoinc_find";
$autoinc = preg_match(
"/$find/Ui",
$create_table,
$matches
);
$default = null;
if ($val['dflt_value'] && $val['dflt_value'] != 'NULL') {
$default = trim($val['dflt_value'], "'");
}
$cols[$name] = array(
'name' => $name,
'type' => $type,
'size' => ($size ? (int) $size : null),
'scale' => ($scale ? (int) $scale : null),
'default' => $default,
'notnull' => (bool) ($val['notnull']),
'primary' => (bool) ($val['pk'] == 1),
'autoinc' => (bool) $autoinc,
);
}
// For defaults using keywords, SQLite always reports the keyword
// *value*, not the keyword itself (e.g., '2007-03-07' instead of
// 'CURRENT_DATE').
//
// The allowed keywords are CURRENT_DATE, CURRENT_TIME, and
// CURRENT_TIMESTAMP.
//
// <http://www.sqlite.org/lang_createtable.html>
//
// Check the table-creation SQL for the default value to see if it's
// a keyword and report 'null' in those cases.
// get the list of column names
$names = array_keys($cols);
// how many are there?
$last = count($names) - 1;
// loop through each column and find out if its default is a keyword
foreach ($names as $curr => $name) {
// if there is a default value ...
if ($cols[$name]['default']) {
// look for :curr_col :curr_type . DEFAULT CURRENT_(*)
$find = $cols[$name]['name'] . '\s+'
. $cols[$name]['type']
. '.*\s+DEFAULT\s+CURRENT_';
// if not at the end, don't look further than the next coldef
if ($curr < $last) {
$next = $names[$curr + 1];
$find .= '.*' . $cols[$next]['name'] . '\s+'
. $cols[$next]['type'];
}
// is the default a keyword?
preg_match("/$find/ims", $create_table, $matches);
if (! empty($matches)) {
$cols[$name]['default'] = null;
}
}
// convert to a column object
$cols[$name] = $this->column_factory->newInstance(
$cols[$name]['name'],
$cols[$name]['type'],
$cols[$name]['size'],
$cols[$name]['scale'],
$cols[$name]['notnull'],
$cols[$name]['default'],
$cols[$name]['autoinc'],
$cols[$name]['primary']
);
}
// done!
return $cols;
}
/**
*
* Returns the last ID inserted on the connection.
*
* @return mixed
*
*/
public function lastInsertId()
{
$pdo = $this->getPdo();
return $pdo->lastInsertId();
}
}