1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Global database interface, complete with static methods. |
4
|
|
|
* Use this class for interacting with the database. |
5
|
|
|
* |
6
|
|
|
* @package framework |
7
|
|
|
* @subpackage model |
8
|
|
|
*/ |
9
|
|
|
class DB { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This constant was added in SilverStripe 2.4 to indicate that SQL-queries |
13
|
|
|
* should now use ANSI-compatible syntax. The most notable affect of this |
14
|
|
|
* change is that table and field names should be escaped with double quotes |
15
|
|
|
* and not backticks |
16
|
|
|
*/ |
17
|
|
|
const USE_ANSI_SQL = true; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The global database connection. |
22
|
|
|
* @var SS_Database |
23
|
|
|
*/ |
24
|
|
|
private static $connections = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The last SQL query run. |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public static $lastQuery; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Internal flag to keep track of when db connection was attempted. |
34
|
|
|
*/ |
35
|
|
|
private static $connection_attempted = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set the global database connection. |
39
|
|
|
* Pass an object that's a subclass of SS_Database. This object will be used when {@link DB::query()} |
40
|
|
|
* is called. |
41
|
|
|
* |
42
|
|
|
* @param $connection The connecton object to set as the connection. |
43
|
|
|
* @param $name The name to give to this connection. If you omit this argument, the connection |
44
|
|
|
* will be the default one used by the ORM. However, you can store other named connections to |
45
|
|
|
* be accessed through DB::get_conn($name). This is useful when you have an application that |
46
|
|
|
* needs to connect to more than one database. |
47
|
|
|
*/ |
48
|
|
|
public static function set_conn(SS_Database $connection, $name = 'default') { |
49
|
|
|
self::$connections[$name] = $connection; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @deprecated since version 4.0 Use DB::set_conn instead |
54
|
|
|
*/ |
55
|
|
|
public static function setConn(SS_Database $connection, $name = 'default') { |
56
|
|
|
Deprecation::notice('4.0', 'Use DB::set_conn instead'); |
57
|
|
|
self::set_conn($connection, $name); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the global database connection. |
62
|
|
|
* |
63
|
|
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
64
|
|
|
* the default connection is returned. |
65
|
|
|
* @return SS_Database |
66
|
|
|
*/ |
67
|
|
|
public static function get_conn($name = 'default') { |
68
|
|
|
if(isset(self::$connections[$name])) { |
69
|
|
|
return self::$connections[$name]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @deprecated since version 4.0 Use DB::get_conn instead |
75
|
|
|
*/ |
76
|
|
|
public static function getConn($name = 'default') { |
77
|
|
|
Deprecation::notice('4.0', 'Use DB::get_conn instead'); |
78
|
|
|
return self::get_conn($name); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Retrieves the schema manager for the current database |
83
|
|
|
* |
84
|
|
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
85
|
|
|
* the default connection is returned. |
86
|
|
|
* @return DBSchemaManager |
87
|
|
|
*/ |
88
|
|
|
public static function get_schema($name = 'default') { |
89
|
|
|
$connection = self::get_conn($name); |
90
|
|
|
if($connection) return $connection->getSchemaManager(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Builds a sql query with the specified connection |
95
|
|
|
* |
96
|
|
|
* @param SQLExpression $expression The expression object to build from |
97
|
|
|
* @param array $parameters Out parameter for the resulting query parameters |
98
|
|
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
99
|
|
|
* the default connection is returned. |
100
|
|
|
* @return string The resulting SQL as a string |
101
|
|
|
*/ |
102
|
|
|
public static function build_sql(SQLExpression $expression, &$parameters, $name = 'default') { |
103
|
|
|
$connection = self::get_conn($name); |
104
|
|
|
if($connection) { |
105
|
|
|
return $connection->getQueryBuilder()->buildSQL($expression, $parameters); |
106
|
|
|
} else { |
107
|
|
|
$parameters = array(); |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieves the connector object for the current database |
114
|
|
|
* |
115
|
|
|
* @param string $name An optional name given to a connection in the DB::setConn() call. If omitted, |
116
|
|
|
* the default connection is returned. |
117
|
|
|
* @return DBConnector |
118
|
|
|
*/ |
119
|
|
|
public static function get_connector($name = 'default') { |
120
|
|
|
$connection = self::get_conn($name); |
121
|
|
|
if($connection) return $connection->getConnector(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set an alternative database in a browser cookie, |
126
|
|
|
* with the cookie lifetime set to the browser session. |
127
|
|
|
* This is useful for integration testing on temporary databases. |
128
|
|
|
* |
129
|
|
|
* There is a strict naming convention for temporary databases to avoid abuse: |
130
|
|
|
* <prefix> (default: 'ss_') + tmpdb + <7 digits> |
131
|
|
|
* As an additional security measure, temporary databases will |
132
|
|
|
* be ignored in "live" mode. |
133
|
|
|
* |
134
|
|
|
* Note that the database will be set on the next request. |
135
|
|
|
* Set it to null to revert to the main database. |
136
|
|
|
*/ |
137
|
|
|
public static function set_alternative_database_name($name = null) { |
138
|
|
|
// Skip if CLI |
139
|
|
|
if(Director::is_cli()) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
if($name) { |
143
|
|
|
if(!self::valid_alternative_database_name($name)) { |
144
|
|
|
throw new InvalidArgumentException(sprintf( |
145
|
|
|
'Invalid alternative database name: "%s"', |
146
|
|
|
$name |
147
|
|
|
)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$key = Config::inst()->get('Security', 'token'); |
151
|
|
|
if(!$key) { |
152
|
|
|
throw new LogicException('"Security.token" not found, run "sake dev/generatesecuretoken"'); |
153
|
|
|
} |
154
|
|
|
if(!function_exists('mcrypt_encrypt')) { |
155
|
|
|
throw new LogicException('DB::set_alternative_database_name() requires the mcrypt PHP extension'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$key = md5($key); // Ensure key is correct length for chosen cypher |
159
|
|
|
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CFB); |
160
|
|
|
$iv = mcrypt_create_iv($ivSize); |
161
|
|
|
$encrypted = mcrypt_encrypt( |
162
|
|
|
MCRYPT_RIJNDAEL_256, $key, $name, MCRYPT_MODE_CFB, $iv |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
// Set to browser session lifetime, and restricted to HTTP access only |
166
|
|
|
Cookie::set("alternativeDatabaseName", base64_encode($encrypted), 0, null, null, false, true); |
167
|
|
|
Cookie::set("alternativeDatabaseNameIv", base64_encode($iv), 0, null, null, false, true); |
168
|
|
|
} else { |
169
|
|
|
Cookie::force_expiry("alternativeDatabaseName", null, null, false, true); |
170
|
|
|
Cookie::force_expiry("alternativeDatabaseNameIv", null, null, false, true); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the name of the database in use |
176
|
|
|
*/ |
177
|
|
|
public static function get_alternative_database_name() { |
178
|
|
|
$name = Cookie::get("alternativeDatabaseName"); |
179
|
|
|
$iv = Cookie::get("alternativeDatabaseNameIv"); |
180
|
|
|
|
181
|
|
|
if($name) { |
|
|
|
|
182
|
|
|
$key = Config::inst()->get('Security', 'token'); |
183
|
|
|
if(!$key) { |
184
|
|
|
throw new LogicException('"Security.token" not found, run "sake dev/generatesecuretoken"'); |
185
|
|
|
} |
186
|
|
|
if(!function_exists('mcrypt_encrypt')) { |
187
|
|
|
throw new LogicException('DB::set_alternative_database_name() requires the mcrypt PHP extension'); |
188
|
|
|
} |
189
|
|
|
$key = md5($key); // Ensure key is correct length for chosen cypher |
190
|
|
|
$decrypted = mcrypt_decrypt( |
191
|
|
|
MCRYPT_RIJNDAEL_256, $key, base64_decode($name), MCRYPT_MODE_CFB, base64_decode($iv) |
192
|
|
|
); |
193
|
|
|
return (self::valid_alternative_database_name($decrypted)) ? $decrypted : false; |
194
|
|
|
} else { |
195
|
|
|
return false; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Determines if the name is valid, as a security |
201
|
|
|
* measure against setting arbitrary databases. |
202
|
|
|
* |
203
|
|
|
* @param String $name |
204
|
|
|
* @return Boolean |
205
|
|
|
*/ |
206
|
|
|
public static function valid_alternative_database_name($name) { |
207
|
|
|
if(Director::isLive()) return false; |
208
|
|
|
|
209
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_'; |
210
|
|
|
$pattern = strtolower(sprintf('/^%stmpdb\d{7}$/', $prefix)); |
211
|
|
|
return (bool)preg_match($pattern, $name); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Connect to a database. |
216
|
|
|
* |
217
|
|
|
* Given the database configuration, this method will create the correct |
218
|
|
|
* subclass of {@link SS_Database}. |
219
|
|
|
* |
220
|
|
|
* @param array $database A map of options. The 'type' is the name of the subclass of SS_Database to use. For the |
|
|
|
|
221
|
|
|
* rest of the options, see the specific class. |
222
|
|
|
* @param string $name identifier for the connection |
|
|
|
|
223
|
|
|
* |
224
|
|
|
* @return SS_Database |
225
|
|
|
*/ |
226
|
|
|
public static function connect($databaseConfig, $label = 'default') { |
227
|
|
|
|
228
|
|
|
// This is used by the "testsession" module to test up a test session using an alternative name |
229
|
|
|
if($name = self::get_alternative_database_name()) { |
230
|
|
|
$databaseConfig['database'] = $name; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
if(!isset($databaseConfig['type']) || empty($databaseConfig['type'])) { |
234
|
|
|
user_error("DB::connect: Not passed a valid database config", E_USER_ERROR); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
self::$connection_attempted = true; |
238
|
|
|
|
239
|
|
|
$dbClass = $databaseConfig['type']; |
240
|
|
|
|
241
|
|
|
// Using Injector->create allows us to use registered configurations |
242
|
|
|
// which may or may not map to explicit objects |
243
|
|
|
$conn = Injector::inst()->create($dbClass); |
244
|
|
|
$conn->connect($databaseConfig); |
245
|
|
|
|
246
|
|
|
self::set_conn($conn, $label); |
247
|
|
|
|
248
|
|
|
return $conn; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Returns true if a database connection has been attempted. |
253
|
|
|
* In particular, it lets the caller know if we're still so early in the execution pipeline that |
254
|
|
|
* we haven't even tried to connect to the database yet. |
255
|
|
|
*/ |
256
|
|
|
public static function connection_attempted() { |
257
|
|
|
return self::$connection_attempted; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @deprecated since version 4.0 DB::getConnect was never implemented and is obsolete |
262
|
|
|
*/ |
263
|
|
|
public static function getConnect($parameters) { |
|
|
|
|
264
|
|
|
Deprecation::notice('4.0', 'DB::getConnect was never implemented and is obsolete'); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Execute the given SQL query. |
269
|
|
|
* @param string $sql The SQL query to execute |
270
|
|
|
* @param int $errorLevel The level of error reporting to enable for the query |
271
|
|
|
* @return SS_Query |
272
|
|
|
*/ |
273
|
|
|
public static function query($sql, $errorLevel = E_USER_ERROR) { |
274
|
|
|
self::$lastQuery = $sql; |
275
|
|
|
|
276
|
|
|
return self::get_conn()->query($sql, $errorLevel); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Helper function for generating a list of parameter placeholders for the |
281
|
|
|
* given argument(s) |
282
|
|
|
* |
283
|
|
|
* @param array|integer $input An array of items needing placeholders, or a |
284
|
|
|
* number to specify the number of placeholders |
285
|
|
|
* @param string $join The string to join each placeholder together with |
286
|
|
|
* @return string|null Either a list of placeholders, or null |
287
|
|
|
*/ |
288
|
|
|
public static function placeholders($input, $join = ', ') { |
289
|
|
|
if(is_array($input)) { |
290
|
|
|
$number = count($input); |
291
|
|
|
} elseif(is_numeric($input)) { |
292
|
|
|
$number = intval($input); |
293
|
|
|
} else { |
294
|
|
|
return null; |
295
|
|
|
} |
296
|
|
|
if($number === 0) return null; |
297
|
|
|
return implode($join, array_fill(0, $number, '?')); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param string $sql The parameterised query |
302
|
|
|
* @param array $parameters The parameters to inject into the query |
303
|
|
|
* |
304
|
|
|
* @return string |
305
|
|
|
*/ |
306
|
|
|
public static function inline_parameters($sql, $parameters) { |
307
|
|
|
$segments = preg_split('/\?/', $sql); |
308
|
|
|
$joined = ''; |
309
|
|
|
$inString = false; |
310
|
|
|
$numSegments = count($segments); |
311
|
|
|
for($i = 0; $i < $numSegments; $i++) { |
312
|
|
|
$input = $segments[$i]; |
313
|
|
|
// Append next segment |
314
|
|
|
$joined .= $segments[$i]; |
315
|
|
|
// Don't add placeholder after last segment |
316
|
|
|
if($i === $numSegments - 1) { |
317
|
|
|
break; |
318
|
|
|
} |
319
|
|
|
// check string escape on previous fragment |
320
|
|
|
// Remove escaped backslashes, count them! |
321
|
|
|
$input = preg_replace('/\\\\\\\\/', '', $input); |
322
|
|
|
// Count quotes |
323
|
|
|
$totalQuotes = substr_count($input, "'"); // Includes double quote escaped quotes |
324
|
|
|
$escapedQuotes = substr_count($input, "\\'"); |
325
|
|
|
if((($totalQuotes - $escapedQuotes) % 2) !== 0) { |
326
|
|
|
$inString = !$inString; |
327
|
|
|
} |
328
|
|
|
// Append placeholder replacement |
329
|
|
|
if($inString) { |
330
|
|
|
// Literal question mark |
331
|
|
|
$joined .= '?'; |
332
|
|
|
continue; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// Encode and insert next parameter |
336
|
|
|
$next = array_shift($parameters); |
337
|
|
|
if(is_array($next) && isset($next['value'])) { |
338
|
|
|
$next = $next['value']; |
339
|
|
|
} |
340
|
|
|
if (is_bool($next)) { |
341
|
|
|
$value = $next ? '1' : '0'; |
342
|
|
|
} |
343
|
|
|
elseif (is_int($next)) { |
344
|
|
|
$value = $next; |
345
|
|
|
} |
346
|
|
|
else { |
347
|
|
|
$value = DB::is_active() ? Convert::raw2sql($next, true) : $next; |
|
|
|
|
348
|
|
|
} |
349
|
|
|
$joined .= $value; |
350
|
|
|
} |
351
|
|
|
return $joined; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Execute the given SQL parameterised query with the specified arguments |
356
|
|
|
* |
357
|
|
|
* @param string $sql The SQL query to execute. The ? character will denote parameters. |
358
|
|
|
* @param array $parameters An ordered list of arguments. |
359
|
|
|
* @param int $errorLevel The level of error reporting to enable for the query |
360
|
|
|
* @return SS_Query |
361
|
|
|
*/ |
362
|
|
|
public static function prepared_query($sql, $parameters, $errorLevel = E_USER_ERROR) { |
363
|
|
|
self::$lastQuery = $sql; |
364
|
|
|
|
365
|
|
|
return self::get_conn()->preparedQuery($sql, $parameters, $errorLevel); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Execute a complex manipulation on the database. |
370
|
|
|
* A manipulation is an array of insert / or update sequences. The keys of the array are table names, |
371
|
|
|
* and the values are map containing 'command' and 'fields'. Command should be 'insert' or 'update', |
372
|
|
|
* and fields should be a map of field names to field values, including quotes. The field value can |
373
|
|
|
* also be a SQL function or similar. |
374
|
|
|
* |
375
|
|
|
* Example: |
376
|
|
|
* <code> |
377
|
|
|
* array( |
378
|
|
|
* // Command: insert |
379
|
|
|
* "table name" => array( |
380
|
|
|
* "command" => "insert", |
381
|
|
|
* "fields" => array( |
382
|
|
|
* "ClassName" => "'MyClass'", // if you're setting a literal, you need to escape and provide quotes |
383
|
|
|
* "Created" => "now()", // alternatively, you can call DB functions |
384
|
|
|
* "ID" => 234, |
385
|
|
|
* ), |
386
|
|
|
* "id" => 234 // an alternative to providing ID in the fields list |
387
|
|
|
* ), |
388
|
|
|
* |
389
|
|
|
* // Command: update |
390
|
|
|
* "other table" => array( |
391
|
|
|
* "command" => "update", |
392
|
|
|
* "fields" => array( |
393
|
|
|
* "ClassName" => "'MyClass'", |
394
|
|
|
* "LastEdited" => "now()", |
395
|
|
|
* ), |
396
|
|
|
* "where" => "ID = 234", |
397
|
|
|
* "id" => 234 // an alternative to providing a where clause |
398
|
|
|
* ), |
399
|
|
|
* ) |
400
|
|
|
* </code> |
401
|
|
|
* |
402
|
|
|
* You'll note that only one command on a given table can be called. |
403
|
|
|
* That's a limitation of the system that's due to it being written for {@link DataObject::write()}, |
404
|
|
|
* which needs to do a single write on a number of different tables. |
405
|
|
|
* |
406
|
|
|
* @todo Update this to support paramaterised queries |
407
|
|
|
* |
408
|
|
|
* @param array $manipulation |
409
|
|
|
*/ |
410
|
|
|
public static function manipulate($manipulation) { |
411
|
|
|
self::$lastQuery = $manipulation; |
|
|
|
|
412
|
|
|
return self::get_conn()->manipulate($manipulation); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Get the autogenerated ID from the previous INSERT query. |
417
|
|
|
* @return int |
418
|
|
|
*/ |
419
|
|
|
public static function get_generated_id($table) { |
420
|
|
|
return self::get_conn()->getGeneratedID($table); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @deprecated since version 4.0 Use DB::get_generated_id instead |
425
|
|
|
*/ |
426
|
|
|
public static function getGeneratedID($table) { |
427
|
|
|
Deprecation::notice('4.0', 'Use DB::get_generated_id instead'); |
428
|
|
|
return self::get_generated_id($table); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Check if the connection to the database is active. |
433
|
|
|
* |
434
|
|
|
* @return boolean |
435
|
|
|
*/ |
436
|
|
|
public static function is_active() { |
437
|
|
|
return ($conn = self::get_conn()) && $conn->isActive(); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* @deprecated since version 4.0 Use DB::is_active instead |
442
|
|
|
*/ |
443
|
|
|
public static function isActive() { |
444
|
|
|
Deprecation::notice('4.0', 'Use DB::is_active instead'); |
445
|
|
|
return self::is_active(); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Create the database and connect to it. This can be called if the |
450
|
|
|
* initial database connection is not successful because the database |
451
|
|
|
* does not exist. |
452
|
|
|
* |
453
|
|
|
* @param string $database Name of database to create |
454
|
|
|
* @return boolean Returns true if successful |
455
|
|
|
*/ |
456
|
|
|
public static function create_database($database) { |
457
|
|
|
return self::get_conn()->selectDatabase($database, true); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* @deprecated since version 4.0 Use DB::create_database instead |
462
|
|
|
*/ |
463
|
|
|
public static function createDatabase($connect, $username, $password, $database) { |
464
|
|
|
Deprecation::notice('4.0', 'Use DB::create_database instead'); |
465
|
|
|
return self::create_database($database); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Create a new table. |
470
|
|
|
* @param string $tableName The name of the table |
|
|
|
|
471
|
|
|
* @param array$fields A map of field names to field types |
472
|
|
|
* @param array $indexes A map of indexes |
473
|
|
|
* @param array $options An map of additional options. The available keys are as follows: |
474
|
|
|
* - 'MSSQLDatabase'/'MySQLDatabase'/'PostgreSQLDatabase' - database-specific options such as "engine" |
475
|
|
|
* for MySQL. |
476
|
|
|
* - 'temporary' - If true, then a temporary table will be created |
477
|
|
|
* @return string The table name generated. This may be different from the table name, for example with |
478
|
|
|
* temporary tables. |
479
|
|
|
*/ |
480
|
|
|
public static function create_table($table, $fields = null, $indexes = null, $options = null, |
481
|
|
|
$advancedOptions = null |
482
|
|
|
) { |
483
|
|
|
return self::get_schema()->createTable($table, $fields, $indexes, $options, $advancedOptions); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* @deprecated since version 4.0 Use DB::create_table instead |
488
|
|
|
*/ |
489
|
|
|
public static function createTable($table, $fields = null, $indexes = null, $options = null) { |
490
|
|
|
Deprecation::notice('4.0', 'Use DB::create_table instead'); |
491
|
|
|
return self::create_table($table, $fields, $indexes, $options); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Create a new field on a table. |
496
|
|
|
* @param string $table Name of the table. |
497
|
|
|
* @param string $field Name of the field to add. |
498
|
|
|
* @param string $spec The field specification, eg 'INTEGER NOT NULL' |
499
|
|
|
*/ |
500
|
|
|
public static function create_field($table, $field, $spec) { |
501
|
|
|
return self::get_schema()->createField($table, $field, $spec); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* @deprecated since version 4.0 Use DB::create_field instead |
506
|
|
|
*/ |
507
|
|
|
public static function createField($table, $field, $spec) { |
508
|
|
|
Deprecation::notice('4.0', 'Use DB::create_field instead'); |
509
|
|
|
return self::create_field($table, $field, $spec); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Generate the following table in the database, modifying whatever already exists |
514
|
|
|
* as necessary. |
515
|
|
|
* |
516
|
|
|
* @param string $table The name of the table |
517
|
|
|
* @param string $fieldSchema A list of the fields to create, in the same form as DataObject::$db |
518
|
|
|
* @param string $indexSchema A list of indexes to create. The keys of the array are the names of the index. |
519
|
|
|
* The values of the array can be one of: |
520
|
|
|
* - true: Create a single column index on the field named the same as the index. |
521
|
|
|
* - array('fields' => array('A','B','C'), 'type' => 'index/unique/fulltext'): This gives you full |
522
|
|
|
* control over the index. |
523
|
|
|
* @param boolean $hasAutoIncPK A flag indicating that the primary key on this table is an autoincrement type |
524
|
|
|
* @param string $options SQL statement to append to the CREATE TABLE call. |
525
|
|
|
* @param array $extensions List of extensions |
526
|
|
|
*/ |
527
|
|
|
public static function require_table($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK = true, |
528
|
|
|
$options = null, $extensions = null |
529
|
|
|
) { |
530
|
|
|
return self::get_schema()->requireTable($table, $fieldSchema, $indexSchema, $hasAutoIncPK, $options, |
|
|
|
|
531
|
|
|
$extensions); |
|
|
|
|
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* @deprecated since version 4.0 Use DB::require_table instead |
536
|
|
|
*/ |
537
|
|
|
public static function requireTable($table, $fieldSchema = null, $indexSchema = null, $hasAutoIncPK = true, |
538
|
|
|
$options = null, $extensions = null |
539
|
|
|
) { |
540
|
|
|
Deprecation::notice('4.0', 'Use DB::require_table instead'); |
541
|
|
|
return self::require_table($table, $fieldSchema, $indexSchema, $hasAutoIncPK, $options, $extensions); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Generate the given field on the table, modifying whatever already exists as necessary. |
546
|
|
|
* |
547
|
|
|
* @param string $table The table name. |
548
|
|
|
* @param string $field The field name. |
549
|
|
|
* @param string $spec The field specification. |
550
|
|
|
*/ |
551
|
|
|
public static function require_field($table, $field, $spec) { |
552
|
|
|
return self::get_schema()->requireField($table, $field, $spec); |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* @deprecated since version 4.0 Use DB::require_field instead |
557
|
|
|
*/ |
558
|
|
|
public static function requireField($table, $field, $spec) { |
559
|
|
|
Deprecation::notice('4.0', 'Use DB::require_field instead'); |
560
|
|
|
return self::require_field($table, $field, $spec); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Generate the given index in the database, modifying whatever already exists as necessary. |
565
|
|
|
* |
566
|
|
|
* @param string $table The table name. |
567
|
|
|
* @param string $index The index name. |
568
|
|
|
* @param string|boolean $spec The specification of the index. See requireTable() for more information. |
569
|
|
|
*/ |
570
|
|
|
public static function require_index($table, $index, $spec) { |
571
|
|
|
self::get_schema()->requireIndex($table, $index, $spec); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* @deprecated since version 4.0 Use DB::require_index instead |
576
|
|
|
*/ |
577
|
|
|
public static function requireIndex($table, $index, $spec) { |
578
|
|
|
Deprecation::notice('4.0', 'Use DB::require_index instead'); |
579
|
|
|
self::require_index($table, $index, $spec); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* If the given table exists, move it out of the way by renaming it to _obsolete_(tablename). |
584
|
|
|
* |
585
|
|
|
* @param string $table The table name. |
586
|
|
|
*/ |
587
|
|
|
public static function dont_require_table($table) { |
588
|
|
|
self::get_schema()->dontRequireTable($table); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* @deprecated since version 4.0 Use DB::dont_require_table instead |
593
|
|
|
*/ |
594
|
|
|
public static function dontRequireTable($table) { |
595
|
|
|
Deprecation::notice('4.0', 'Use DB::dont_require_table instead'); |
596
|
|
|
self::dont_require_table($table); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
/** |
600
|
|
|
* See {@link SS_Database->dontRequireField()}. |
601
|
|
|
* |
602
|
|
|
* @param string $table The table name. |
603
|
|
|
* @param string $fieldName The field name not to require |
604
|
|
|
*/ |
605
|
|
|
public static function dont_require_field($table, $fieldName) { |
606
|
|
|
self::get_schema()->dontRequireField($table, $fieldName); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* @deprecated since version 4.0 Use DB::dont_require_field instead |
611
|
|
|
*/ |
612
|
|
|
public static function dontRequireField($table, $fieldName) { |
613
|
|
|
Deprecation::notice('4.0', 'Use DB::dont_require_field instead'); |
614
|
|
|
self::dont_require_field($table, $fieldName); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Checks a table's integrity and repairs it if necessary. |
619
|
|
|
* |
620
|
|
|
* @param string $tableName The name of the table. |
|
|
|
|
621
|
|
|
* @return boolean Return true if the table has integrity after the method is complete. |
622
|
|
|
*/ |
623
|
|
|
public static function check_and_repair_table($table) { |
624
|
|
|
return self::get_schema()->checkAndRepairTable($table); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* @deprecated since version 4.0 Use DB::check_and_repair_table instead |
629
|
|
|
*/ |
630
|
|
|
public static function checkAndRepairTable($table) { |
631
|
|
|
Deprecation::notice('4.0', 'Use DB::check_and_repair_table instead'); |
632
|
|
|
self::check_and_repair_table($table); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* Return the number of rows affected by the previous operation. |
637
|
|
|
* |
638
|
|
|
* @return integer The number of affected rows |
639
|
|
|
*/ |
640
|
|
|
public static function affected_rows() { |
641
|
|
|
return self::get_conn()->affectedRows(); |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* @deprecated since version 4.0 Use DB::affected_rows instead |
646
|
|
|
*/ |
647
|
|
|
public static function affectedRows() { |
648
|
|
|
Deprecation::notice('4.0', 'Use DB::affected_rows instead'); |
649
|
|
|
return self::affected_rows(); |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Returns a list of all tables in the database. |
654
|
|
|
* The table names will be in lower case. |
655
|
|
|
* |
656
|
|
|
* @return array The list of tables |
657
|
|
|
*/ |
658
|
|
|
public static function table_list() { |
659
|
|
|
return self::get_schema()->tableList(); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
/** |
663
|
|
|
* @deprecated since version 4.0 Use DB::table_list instead |
664
|
|
|
*/ |
665
|
|
|
public static function tableList() { |
666
|
|
|
Deprecation::notice('4.0', 'Use DB::table_list instead'); |
667
|
|
|
return self::table_list(); |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* Get a list of all the fields for the given table. |
672
|
|
|
* Returns a map of field name => field spec. |
673
|
|
|
* |
674
|
|
|
* @param string $table The table name. |
675
|
|
|
* @return array The list of fields |
676
|
|
|
*/ |
677
|
|
|
public static function field_list($table) { |
678
|
|
|
return self::get_schema()->fieldList($table); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* @deprecated since version 4.0 Use DB::field_list instead |
683
|
|
|
*/ |
684
|
|
|
public static function fieldList($table) { |
685
|
|
|
Deprecation::notice('4.0', 'Use DB::field_list instead'); |
686
|
|
|
return self::field_list($table); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* Enable supression of database messages. |
691
|
|
|
*/ |
692
|
|
|
public static function quiet() { |
693
|
|
|
self::get_schema()->quiet(); |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* Show a message about database alteration |
698
|
|
|
* |
699
|
|
|
* @param string $message to display |
700
|
|
|
* @param string $type one of [created|changed|repaired|obsolete|deleted|error] |
701
|
|
|
*/ |
702
|
|
|
public static function alteration_message($message, $type = "") { |
703
|
|
|
self::get_schema()->alterationMessage($message, $type); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
} |
707
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: