-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbschema.php
More file actions
513 lines (468 loc) · 12.2 KB
/
Copy pathdbschema.php
File metadata and controls
513 lines (468 loc) · 12.2 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/* Copyright 2010-2013 Mo McRoberts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once(PLATFORM_LIB . 'uri.php');
abstract class DBIndex
{
const PRIMARY = 1;
const UNIQUE = 2;
const INDEX = 3;
}
abstract class DBType
{
const CHAR = 1;
const INT = 2;
const VARCHAR = 3;
const DATE = 4;
const DATETIME = 5;
const ENUM = 6;
const SET = 7;
const SERIAL = 8;
const BOOL = 9;
const BOOLEAN = 9;
const UUID = 10;
const TEXT = 11;
const BLOB = 12;
const BINARY = 13;
const VARBINARY = 14;
const TIME = 15;
const DECIMAL = 16;
}
abstract class DBCol
{
const NULLS = 0;
const NOT_NULL = 1;
const UNSIGNED = 2;
const BIG = 4; /* Use largest available field width (e.g., BIGINT vs INT, LONGTEXT vs TEXT) */
}
interface IDBSchema
{
public function moduleVersion($moduleId);
public function setModuleVersion($moduleId, $newVersion, $comment = null);
public function dropTable($name);
public function renameTable($oldName, $newName);
public function table($name);
public function tableWithOptions($name, $options);
}
abstract class DBSchema implements IDBSchema
{
public $db; /* Associated database connection */
protected $tableClass; /* Name of descendant of DBTable */
public static function schemaForConnection($connection)
{
return URI::handlerForScheme($connection->dbms, 'DBSchema', false, $connection);
}
public function __construct($connection)
{
$this->db = $connection;
}
/* Drop a table */
public function dropTable($name)
{
if(!($t = $this->tableWithOptions($name, DBTable::DROP)))
{
return false;
}
return $t->apply();
}
/* Rename a table */
public function renameTable($oldName, $newName)
{
return $this->db->exec('ALTER TABLE {' . $oldName . '} RENAME TO {' . $newName . '}');
}
/* Return an existing table */
public function table($name)
{
return $this->tableWithOptions($name, DBTable::EXISTING);
}
/* Return an existing table or create a new one */
public function tableWithOptions($name, $options)
{
$c = $this->tableClass;
if(!strlen($c))
{
trigger_error('Fatal error: DBSchema: {$this->tableClass} is undefined or empty', E_USER_ERROR);
exit(1);
}
return new $c($this, $name, $options);
}
/* Update the stored version number of the specified module */
public function setModuleVersion($moduleId, $newVersion, $comment = null)
{
$this->db->exec('UPDATE {_version} SET "version" = ?, "comment" = ?, "updated" = ' . $this->db->now() . ' WHERE "ident" = ?', $newVersion, $comment, $moduleId);
}
}
abstract class DBTable
{
/* Flags for DBSchema::tableWithOptions */
const EXISTING = 0; /* Don't create the table: retrieve existing spec */
const CREATE_NEVER = 0;
const CREATE_IF_NEEDED = 1; /* Create it if it doesn't exist; no-op otherwise */
const CREATE_ALWAYS = 2; /* Always create, dropping if necessary */
const DROP = 3; /* Drop the table */
public $schema;
public $name;
public $options;
protected $exists;
protected $columns = array();
protected $indices = array();
protected $changes = array();
protected $nativeCreateOptions = array();
public function __construct($schema, $name, $options)
{
$this->schema = $schema;
$this->name = $name;
$this->options = $options;
if($options == self::EXISTING || $options == self::CREATE_IF_NEEDED)
{
$this->retrieve();
}
}
public function apply()
{
if($this->options == self::DROP)
{
return $this->dropTable();
}
if($this->options == self::EXISTING)
{
if(count($this->changes))
{
return $this->applyChanges();
}
return true;
}
return $this->createTable();
}
public function column($name)
{
if(isset($this->columns[$name])) return $this->columns[$name];
return null;
}
public function columns()
{
return $this->columns;
}
public function columnWithSpec($name, $type, $sizeValues, $flags = null, $defaultValue = null, $comment = null)
{
if($flags == null) $flags = DBCol::NULLS;
$info = array(
'name' => $name,
'type' => $type,
'sizeValues' => $sizeValues,
'flags' => $flags,
'default' => $defaultValue,
'comment' => $comment,
);
switch($type)
{
case DBType::CHAR:
if(empty($info['sizeValues'])) $info['sizeValues'] = 1;
if(!is_int($info['sizeValues']))
{
trigger_error('DBTable::columnWithSpec: the size of a CHAR column must be an integer or null', E_USER_NOTICE);
return false;
}
break;
case DBType::VARCHAR:
if(!is_int($info['sizeValues']))
{
trigger_error('DBTable::columnWithSpec: the size of a VARCHAR column must be an integer', E_USER_NOTICE);
return false;
}
break;
case DBType::ENUM:
case DBType::SET:
if(!is_array($info['sizeValues']) || !count($info['sizeValues']))
{
trigger_error('DBTable::columnWithSpec: an array of values must be specified for ' . ($type == DBType::ENUM ? 'an ENUM' : 'a SET') . ' column', E_USER_NOTICE);
return false;
}
break;
case DBType::DATE:
case DBType::DATETIME:
case DBType::TIME:
case DBType::UUID:
case DBType::BOOL:
case DBType::SERIAL:
case DBType::INT:
case DBType::TEXT:
$info['sizeValues'] = null;
break;
case DBType::DECIMAL:
if(!is_array($info['sizeValues']) || count($info['sizeValues']) != 2)
{
trigger_error('DBTable::columnWithSpec: a two-entry array must be specified as the size for a DECIMAL column', E_USER_NOTICE);
return false;
}
break;
default:
trigger_error('DBTable: Unsupported column type ' . $info['type'], E_USER_NOTICE);
return false;
}
if(!$this->nativeColumnSpec($info))
{
return false;
}
if($this->options == self::EXISTING)
{
if(isset($this->columns[$name]))
{
$this->changes[] = $this->replaceColumn($info);
}
else
{
$this->changes[] = $this->addColumn($info);
}
}
$this->columns[$name] = $info;
}
public function indexWithSpec($name, $type, $column /* ... */)
{
$columns = func_get_args();
array_shift($columns);
array_shift($columns);
switch($type)
{
case DBIndex::PRIMARY:
$spec = 'PRIMARY KEY';
$name = 'PRIMARY';
break;
case DBIndex::UNIQUE:
$spec = 'UNIQUE KEY';
break;
case DBIndex::INDEX:
$spec = 'INDEX';
break;
default:
trigger_error('DBTable: Unsupported index type ' . $type, E_USER_NOTICE);
return false;
}
if(!strlen($name))
{
$name = implode('_', $columns);
}
$info = array(
'name' => $name,
'type' => $type,
'columns' => $columns,
);
if(!($this->nativeIndexSpec($info)))
{
return false;
}
if($this->options == self::EXISTING)
{
if(isset($this->indices[$info['name']]))
{
$this->dropIndex($info['name']);
}
$this->changes[] = $this->addIndex($info);
}
$this->indices[$info['name']] = $info;
return true;
}
public function indices()
{
return $this->indices;
}
/* Drop an index from a table; can be overridden by descendants */
public function dropIndex($name)
{
if(!strlen($name)) $name = 'PRIMARY';
if(isset($this->indices[$name]))
{
unset($this->indices[$name]);
if($this->options == self::EXISTING)
{
if($name === null || !strcasecmp($name, 'PRIMARY'))
{
$this->changes[] = 'ALTER TABLE {' . $this->name . '} DROP PRIMARY KEY';
}
else
{
$this->changes[] = 'ALTER TABLE {' . $this->name . '} DROP INDEX "' . $name . '"';
}
}
}
}
/* Convenience function: create the primary key from a single field */
public function primaryKey($name)
{
return $this->indexWithSpec(null, DBIndex::PRIMARY, $name);
}
/* Convenience function: create an index from a single field */
public function index($name)
{
return $this->indexWithSpec($name, DBIndex::INDEX, $name);
}
/* Convenience function: create an unique key from a single field */
public function uniqueKey($name)
{
return $this->indexWithSpec($name, DBIndex::UNIQUE, $name);
}
/* Drop a column from a table */
public function dropColumn($name)
{
if(isset($this->columns[$name]))
{
unset($this->indices[$name]);
if($this->options == self::EXISTING)
{
$this->changes[] = 'ALTER TABLE {' . $this->name . '} DROP COLUMN "' . $name . '"';
}
}
}
/* Query the database schema and populate $this->columns and
* $this->indices with the properties of the existing table.
*/
abstract protected function retrieve();
/* Set $info['spec'] to be a native column specification string
* for the given specification parameters, suitable for use in
* CREATE TABLE and ALTER TABLE statements.
*
* e.g., "foo" VARCHAR(32) NOT NULL DEFAULT 'Bar' COMMENT 'A dummy column'
*/
abstract protected function nativeColumnSpec(&$info);
abstract protected function nativeIndexSpec(&$info);
/* Return a string containing an ALTER TABLE statement for replacing
* an existing column with a new specification.
*
* If the previous column name differs from the new one, then
* $info['previous'] will be set to the old name.
*/
protected function replaceColumn($info)
{
if(isset($info['previous']))
{
return 'ALTER TABLE {' . $this->name . '} CHANGE COLUMN "' . $info['previous'] . '" ' . $info['spec'];
}
return 'ALTER TABLE {' . $this->name . '} MODIFY COLUMN ' . $info['spec'];
}
/* Return a string containing an ALTER TABLE statement for adding
* a new column.
*/
protected function addColumn($info)
{
return 'ALTER TABLE {' . $this->name . '} ADD COLUMN ' . $info['spec'];
}
/* Return a string containing an ALTER TABLE statement for adding
* a new index.
*/
protected function addIndex($info)
{
return 'ALTER TABLE {' . $this->name . '} ADD ' . $info['spec'];
}
/* Execute a DROP TABLE statement */
protected function dropTable()
{
$drop = 'DROP TABLE IF EXISTS {' . $this->name . '}';
return $this->schema->db->exec($drop);
}
/* Execute a CREATE TABLE statement based on the content of
* $this->columns and $this->indicies.
*/
protected function createTable()
{
if(!count($this->columns))
{
trigger_error('DBTable: Cannot create a table with no defined columns', E_USER_NOTICE);
return false;
}
$drop = 'DROP TABLE IF EXISTS {' . $this->name . '}';
$cl = array();
if($this->options == self::CREATE_IF_NEEDED)
{
if($this->exists)
{
return true;
}
$create = 'CREATE TABLE IF NOT EXISTS {' . $this->name . '} (';
}
else
{
$create = 'CREATE TABLE {' . $this->name . '} (';
}
foreach($this->columns as $col)
{
$cl[] = $col['spec'];
}
foreach($this->indices as $index)
{
if($index['type'] == DBIndex::PRIMARY)
{
$cl[] = $index['spec'];
}
}
$create .= "\n " . implode(",\n ", $cl) . "\n) " . $this->nativeCreateOptions();
do
{
$this->schema->db->begin();
if($this->options == self::CREATE_ALWAYS)
{
$this->schema->db->exec($drop);
}
$this->schema->db->exec($create);
}
while(!$this->schema->db->commit());
foreach($this->indices as $index)
{
if($index['type'] == DBIndex::PRIMARY)
{
continue;
}
do
{
$this->schema->db->begin();
$this->schema->db->exec($index['fullspec']);
}
while(!$this->schema->db->commit());
}
return true;
}
/* Execute a sequence of ALTER TABLE statements contained within
* $this->changes
*/
protected function applyChanges()
{
do
{
$this->schema->db->begin();
foreach($this->changes as $change)
{
if(defined('DBSCHEMA_DEBUG') && DBSCHEMA_DEBUG)
{
echo "[$change]\n";
}
$this->schema->db->exec($change);
}
}
while(!$this->schema->db->commit());
return true;
}
public function setNativeCreateOption($key, $value)
{
$this->nativeCreateOptions[$key] = $value;
}
protected function nativeCreateOptions()
{
$list = array();
foreach($this->nativeCreateOptions as $name => $value)
{
$list[] = $name . ' ' . $value;
}
return implode(' ', $list);
}
}