forked from socialskeptic/sainsburys
/
db.php
405 lines (376 loc) · 10.6 KB
/
db.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* Database object creation helper methods.
*
* @package Fuel\Database
* @author Kohana Team
* @copyright (c) 2009 Kohana Team
* @license http://kohanaphp.com/license
*/
namespace Fuel\Core;
class DB
{
// Query types
const SELECT = 1;
const INSERT = 2;
const UPDATE = 3;
const DELETE = 4;
public static $query_count = 0;
/**
* Create a new [Database_Query] of the given type.
*
* // Create a new SELECT query
* $query = DB::query('SELECT * FROM users');
*
* // Create a new DELETE query
* $query = DB::query('DELETE FROM users WHERE id = 5');
*
* Specifying the type changes the returned result. When using
* `DB::SELECT`, a [Database_Query_Result] will be returned.
* `DB::INSERT` queries will return the insert id and number of rows.
* For all other queries, the number of affected rows is returned.
*
* @param integer type: DB::SELECT, DB::UPDATE, etc
* @param string SQL statement
* @return Database_Query
*/
public static function query($sql, $type = null)
{
return new \Database_Query($sql, $type);
}
/*
* Returns the last query
*
* @return string the last query
*/
public static function last_query($db = null)
{
return \Database_Connection::instance($db)->last_query;
}
/*
* Returns the DB drivers error info
*
* @return mixed the DB drivers error info
*/
public static function error_info($db = null)
{
return \Database_Connection::instance($db)->error_info();
}
/**
* Create a new [Database_Query_Builder_Select]. Each argument will be
* treated as a column. To generate a `foo AS bar` alias, use an array.
*
* // SELECT id, username
* $query = DB::select('id', 'username');
*
* // SELECT id AS user_id
* $query = DB::select(array('id', 'user_id'));
*
* @param mixed column name or array($column, $alias) or object
* @param ...
* @return Database_Query_Builder_Select
*/
public static function select($columns = NULL)
{
return new \Database_Query_Builder_Select(func_get_args());
}
/**
* Create a new [Database_Query_Builder_Select] from an array of columns.
*
* // SELECT id, username
* $query = DB::select_array(array('id', 'username'));
*
* @param array columns to select
* @return Database_Query_Builder_Select
*/
public static function select_array(array $columns = NULL)
{
return new \Database_Query_Builder_Select($columns);
}
/**
* Create a new [Database_Query_Builder_Insert].
*
* // INSERT INTO users (id, username)
* $query = DB::insert('users', array('id', 'username'));
*
* @param string table to insert into
* @param array list of column names or array($column, $alias) or object
* @return Database_Query_Builder_Insert
*/
public static function insert($table = NULL, array $columns = NULL)
{
return new \Database_Query_Builder_Insert($table, $columns);
}
/**
* Create a new [Database_Query_Builder_Update].
*
* // UPDATE users
* $query = DB::update('users');
*
* @param string table to update
* @return Database_Query_Builder_Update
*/
public static function update($table = NULL)
{
return new \Database_Query_Builder_Update($table);
}
/**
* Create a new [Database_Query_Builder_Delete].
*
* // DELETE FROM users
* $query = DB::delete('users');
*
* @param string table to delete from
* @return Database_Query_Builder_Delete
*/
public static function delete($table = NULL)
{
return new \Database_Query_Builder_Delete($table);
}
/**
* Create a new [Database_Expression] which is not escaped. An expression
* is the only way to use SQL functions within query builders.
*
* $expression = DB::expr('COUNT(users.id)');
*
* @param string expression
* @return Database_Expression
*/
public static function expr($string)
{
return new \Database_Expression($string);
}
/**
* Create a new [Database_Expression] containing a quoted identifier. An expression
* is the only way to use SQL functions within query builders.
*
* $expression = DB::identifier('users.id'); // returns `users`.`id` for MySQL
*
* @param string $string the string to quote
* @param string $db the database connection to use
* @return Database_Expression
*/
public static function identifier($string, $db = null)
{
return new \Database_Expression(static::quote_identifier($string, $db));
}
/**
* Quote a value for an SQL query.
*
* @param string $string the string to quote
* @param string $db the database connection to use
* @return string the quoted value
*/
public static function quote($string, $db = null)
{
if (is_array($string))
{
foreach ($string as $k => $s)
{
$string[$k] = static::quote($s, $db);
}
return $string;
}
return \Database_Connection::instance($db)->quote($string);
}
/**
* Quotes an identifier so it is ready to use in a query.
*
* @param string $string the string to quote
* @param string $db the database connection to use
* @return string the quoted identifier
*/
public static function quote_identifier($string, $db = null)
{
if (is_array($string))
{
foreach ($string as $k => $s)
{
$string[$k] = static::quote_identifier($s, $db);
}
return $string;
}
return \Database_Connection::instance($db)->quote_identifier($string);
}
/**
* Quote a database table name and adds the table prefix if needed.
*
* @param string $string the string to quote
* @param string $db the database connection to use
* @return string the quoted identifier
*/
public static function quote_table($string, $db = null)
{
if (is_array($string))
{
foreach ($string as $k => $s)
{
$string[$k] = static::quote_table($s, $db);
}
return $string;
}
return \Database_Connection::instance($db)->quote_table($string);
}
/**
* Escapes a string to be ready for use in a sql query
*
* @param string $string the string to escape
* @param string $db the database connection to use
* @return string the escaped string
*/
public static function escape($string, $db = null)
{
return \Database_Connection::instance($db)->escape($string);
}
/**
* If a table name is given it will return the table name with the configured
* prefix. If not, then just the prefix is returned
*
* @param string $table the table name to prefix
* @param string $db the database connection to use
* @return string the prefixed table name or the prefix
*/
public static function table_prefix($table = null, $db = null)
{
return \Database_Connection::instance($db)->table_prefix($table);
}
/**
* Lists all of the columns in a table. Optionally, a LIKE string can be
* used to search for specific fields.
*
* // Get all columns from the "users" table
* $columns = DB::list_columns('users');
*
* // Get all name-related columns
* $columns = DB::list_columns('users', '%name%');
*
* @param string table to get columns from
* @param string column to search for
* @param string the database connection to use
* @return array
*/
public static function list_columns($table = null, $like = null, $db = null)
{
return \Database_Connection::instance($db)->list_columns($table, $like);
}
/**
* If a table name is given it will return the table name with the configured
* prefix. If not, then just the prefix is returned
*
* @param string $table the table name to prefix
* @param string $db the database connection to use
* @return string the prefixed table name or the prefix
*/
public static function list_tables($like = null, $db = null)
{
return \Database_Connection::instance($db)->list_tables($like);
}
/**
* Returns a normalized array describing the SQL data type
*
* DB::datatype('char');
*
* @param string SQL data type
* @param string db connection
* @return array
*/
public static function datatype($type, $db = null)
{
return \Database_Connection::instance($db)->datatype($type);
}
/**
* Count the number of records in a table.
*
* // Get the total number of records in the "users" table
* $count = DB::count_records('users');
*
* @param mixed table name string or array(query, alias)
* @param string db connection
* @return integer
*/
public static function count_records($table, $db = null)
{
return \Database_Connection::instance($db)->count_records($table);
}
/**
* Count the number of records in the last query, without LIMIT or OFFSET applied.
*
* // Get the total number of records that match the last query
* $count = $db->count_last_query();
*
* @param string db connection
* @return integer
*/
public static function count_last_query($db = null)
{
return \Database_Connection::instance($db)->count_last_query();
}
/**
* Set the connection character set. This is called automatically by [static::connect].
*
* DB::set_charset('utf8');
*
* @throws Database_Exception
* @param string character set name
* @param string db connection
* @return void
*/
public static function set_charset($charset, $db = null)
{
\Database_Connection::instance($db)->set_charset($charset);
}
/**
* Checks whether a connection is in transaction.
*
* DB::in_transaction();
*
* @param string db connection
* @return bool
*/
public static function in_transaction($db = null)
{
return \Database_Connection::instance($db)->in_transaction();
}
/**
* Begins a transaction on instance
*
* DB::start_transaction();
*
* @param string db connection
* @return bool
*/
public static function start_transaction($db = null)
{
return \Database_Connection::instance($db)->start_transaction();
}
/**
* Commits all pending transactional queries
*
* DB::commit_transaction();
*
* @param string db connection
* @return bool
*/
public static function commit_transaction($db = null)
{
return \Database_Connection::instance($db)->commit_transaction();
}
/**
* Rollsback pending transactional queries
* Rollback to the current level uses SAVEPOINT,
* it does not work if current RDBMS does not support them.
* In this case system rollsback all queries and closes the transaction
*
* DB::rollback_transaction();
*
* @param string $db connection
* @param bool $rollback_all:
* true - rollback everything and close transaction;
* false - rollback only current level
* @return bool
*/
public static function rollback_transaction($db = null, $rollback_all = true)
{
return \Database_Connection::instance($db)->rollback_transaction($rollback_all);
}
}