/
Database.php
669 lines (586 loc) · 18 KB
/
Database.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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Generator\Model;
use DOMNode;
use DOMDocument;
use Propel\Generator\Exception\EngineException;
/**
* A class for holding application data structures.
*
* @author Hans Lellelid <hans@xmpl.org> (Propel)
* @author Leon Messerschmidt <leon@opticode.co.za> (Torque)
* @author John McNally<jmcnally@collab.net> (Torque)
* @author Martin Poeschl<mpoeschl@marmot.at> (Torque)
* @author Daniel Rall<dlr@collab.net> (Torque)
* @author Byron Foster <byron_foster@yahoo.com> (Torque)
*/
class Database extends ScopedElement
{
/**
* The default string format for objects based on this database
* (e.g. 'XML', 'YAML', 'CSV', 'JSON')
*
* @var string
*/
protected $defaultStringFormat;
/**
* List of behaviors registered for this table
*
* @var array
*/
protected $behaviors = array();
private $platform;
private $tableList = array();
private $name;
private $baseClass;
private $basePeer;
private $defaultIdMethod;
private $defaultPhpNamingMethod;
private $defaultTranslateMethod;
private $parentSchema;
private $tablesByName = array();
private $tablesByLowercaseName = array();
private $tablesByPhpName = array();
private $heavyIndexing;
protected $tablePrefix = '';
private $domainMap = array();
/**
* Constructs a new Database object.
*
* @param string $name
*/
public function __construct($name = null)
{
$this->name = $name;
}
/**
* Sets up the Database object based on the attributes that were passed to loadFromXML().
* @see parent::loadFromXML()
*/
protected function setupObject()
{
parent::setupObject();
$this->name = $this->getAttribute("name");
$this->baseClass = $this->getAttribute("baseClass");
$this->basePeer = $this->getAttribute("basePeer");
$this->defaultIdMethod = $this->getAttribute("defaultIdMethod", IdMethod::NATIVE);
$this->defaultPhpNamingMethod = $this->getAttribute("defaultPhpNamingMethod", NameGenerator::CONV_METHOD_UNDERSCORE);
$this->defaultTranslateMethod = $this->getAttribute("defaultTranslateMethod", Validator::TRANSLATE_NONE);
$this->heavyIndexing = $this->booleanValue($this->getAttribute("heavyIndexing"));
$this->tablePrefix = $this->getAttribute('tablePrefix', $this->getBuildProperty('tablePrefix'));
$this->defaultStringFormat = $this->getAttribute('defaultStringFormat', 'YAML');
}
/**
* Returns the PlatformInterface implementation for this database.
*
* @return PlatformInterface a Platform implementation
*/
public function getPlatform()
{
return $this->platform;
}
/**
* Sets the PlatformInterface implementation for this database.
*
* @param PlatformInterface $platform A Platform implementation
*/
public function setPlatform($platform)
{
$this->platform = $platform;
}
/**
* Get the name of the Database
*/
public function getName()
{
return $this->name;
}
/**
* Set the name of the Database
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get the value of baseClass.
* @return value of baseClass.
*/
public function getBaseClass()
{
return $this->baseClass;
}
/**
* Set the value of baseClass.
* @param v Value to assign to baseClass.
*/
public function setBaseClass($v)
{
$this->baseClass = $v;
}
/**
* Get the value of basePeer.
* @return value of basePeer.
*/
public function getBasePeer()
{
return $this->basePeer;
}
/**
* Set the value of basePeer.
* @param v Value to assign to basePeer.
*/
public function setBasePeer($v)
{
$this->basePeer = $v;
}
/**
* Get the value of defaultIdMethod.
* @return value of defaultIdMethod.
*/
public function getDefaultIdMethod()
{
return $this->defaultIdMethod;
}
/**
* Set the value of defaultIdMethod.
* @param v Value to assign to defaultIdMethod.
*/
public function setDefaultIdMethod($v)
{
$this->defaultIdMethod = $v;
}
/**
* Get the value of defaultPHPNamingMethod which specifies the
* method for converting schema names for table and column to PHP names.
* @return string The default naming conversion used by this database.
*/
public function getDefaultPhpNamingMethod()
{
return $this->defaultPhpNamingMethod;
}
/**
* Set the value of defaultPHPNamingMethod.
* @param string $v The default naming conversion for this database to use.
*/
public function setDefaultPhpNamingMethod($v)
{
$this->defaultPhpNamingMethod = $v;
}
/**
* Get the value of defaultTranslateMethod which specifies the
* method for translate validator error messages.
* @return string The default translate method.
*/
public function getDefaultTranslateMethod()
{
return $this->defaultTranslateMethod;
}
/**
* Set the default string format for ActiveRecord objects in this Db.
*
* @param string $defaultStringFormat Any of 'XML', 'YAML', 'JSON', or 'CSV'
*/
public function setDefaultStringFormat($defaultStringFormat)
{
$this->defaultStringFormat = $defaultStringFormat;
}
/**
* Get the default string format for ActiveRecord objects in this Db.
*
* @return string The default string format
*/
public function getDefaultStringFormat()
{
return $this->defaultStringFormat;
}
/**
* Set the value of defaultTranslateMethod.
* @param string $v The default translate method to use.
*/
public function setDefaultTranslateMethod($v)
{
$this->defaultTranslateMethod = $v;
}
/**
* Get the value of heavyIndexing.
*
* This is a synonym for getHeavyIndexing().
*
* @return boolean Value of heavyIndexing.
* @see getHeavyIndexing()
*/
public function isHeavyIndexing()
{
return $this->getHeavyIndexing();
}
/**
* Get the value of heavyIndexing.
*
* @return boolean Value of heavyIndexing.
*/
public function getHeavyIndexing()
{
return $this->heavyIndexing;
}
/**
* Set the value of heavyIndexing.
* @param boolean $v Value to assign to heavyIndexing.
*/
public function setHeavyIndexing($v)
{
$this->heavyIndexing = (boolean) $v;
}
/**
* Return the list of all tables
* @return array
*/
public function getTables()
{
return $this->tableList;
}
/**
* Return the number of tables in the database
* @return integer
*/
public function countTables()
{
$count = 0;
foreach ($this->tableList as $table) {
if (!$table->isReadOnly()) {
$count++;
}
}
return $count;
}
/**
* Return the list of all tables that have a SQL representation
* @return array
*/
public function getTablesForSql()
{
$tables = array();
foreach ($this->tableList as $table) {
if (!$table->isSkipSql()) {
$tables[] = $table;
}
}
return $tables;
}
/**
* Check whether the database has a table.
* @param string $name the name of the table (e.g. 'my_table')
* @param boolean $caseInsensitive Whether the check is case insensitive. False by default.
*
* @return boolean
*/
public function hasTable($name, $caseInsensitive = false)
{
if ($caseInsensitive) {
return array_key_exists(strtolower($name), $this->tablesByLowercaseName);
} else {
return array_key_exists($name, $this->tablesByName);
}
}
/**
* Return the table with the specified name.
* @param string $name The name of the table (e.g. 'my_table')
* @param boolean $caseInsensitive Whether the check is case insensitive. False by default.
*
* @return Table a Table object or null if it doesn't exist
*/
public function getTable($name, $caseInsensitive = false)
{
if ($this->hasTable($name, $caseInsensitive)) {
if ($caseInsensitive) {
return $this->tablesByLowercaseName[strtolower($name)];
} else {
return $this->tablesByName[$name];
}
}
return null; // just to be explicit
}
/**
* Check whether the database has a table.
* @param string $phpName the PHP Name of the table (e.g. 'MyTable')
* @return boolean
*/
public function hasTableByPhpName($phpName)
{
return array_key_exists($phpName, $this->tablesByPhpName);
}
/**
* Return the table with the specified phpName.
* @param string $phpName the PHP Name of the table (e.g. 'MyTable')
* @return Table a Table object or null if it doesn't exist
*/
public function getTableByPhpName($phpName)
{
if (isset($this->tablesByPhpName[$phpName])) {
return $this->tablesByPhpName[$phpName];
}
return null; // just to be explicit
}
/**
* An utility method to add a new table from an xml attribute.
*/
public function addTable($data)
{
if ($data instanceof Table) {
$tbl = $data; // alias
if (isset($this->tablesByName[$tbl->getName()])) {
throw new EngineException(sprintf('Table "%s" declared twice', $tbl->getName()));
}
$tbl->setDatabase($this);
if ($tbl->getSchema() === null) {
$tbl->setSchema($this->getSchema());
}
$this->tableList[] = $tbl;
$this->tablesByName[$tbl->getName()] = $tbl;
$this->tablesByLowercaseName[strtolower($tbl->getName())] = $tbl;
$this->tablesByPhpName[$tbl->getPhpName()] = $tbl;
if (strpos($tbl->getNamespace(), '\\') === 0) {
$tbl->setNamespace(substr($tbl->getNamespace(), 1));
} elseif ($namespace = $this->getNamespace()) {
if ($tbl->getNamespace() === null) {
$tbl->setNamespace($namespace);
} else {
$tbl->setNamespace($namespace . '\\' . $tbl->getNamespace());
}
}
if ($tbl->getPackage() === null) {
$tbl->setPackage($this->getPackage());
}
return $tbl;
} else {
$tbl = new Table();
$tbl->setDatabase($this);
$tbl->setSchema($this->getSchema());
$tbl->loadFromXML($data);
return $this->addTable($tbl); // call self w/ different param
}
}
/**
* Sets the parent schema
*
* @param Schema $parent The parent schema
*/
public function setParentSchema(Schema $parent)
{
$this->parentSchema = $parent;
}
/**
* Returns the parent schema
*
* @return Schema
*/
public function getParentSchema()
{
return $this->parentSchema;
}
/**
* Adds Domain object from <domain> tag.
* @param mixed XML attributes (array) or Domain object.
*/
public function addDomain($data)
{
if ($data instanceof Domain) {
$domain = $data; // alias
$domain->setDatabase($this);
$this->domainMap[$domain->getName()] = $domain;
return $domain;
}
$domain = new Domain();
$domain->setDatabase($this);
$domain->loadFromXML($data);
return $this->addDomain($domain); // call self w/ different param
}
/**
* Get already configured Domain object by name.
* @return Domain
*/
public function getDomain($domainName)
{
if (isset($this->domainMap[$domainName])) {
return $this->domainMap[$domainName];
}
return null; // just to be explicit
}
public function getGeneratorConfig()
{
if ($schema = $this->getMappingSchema()) {
return $schema->getGeneratorConfig();
}
return null;
}
public function getBuildProperty($key)
{
if ($config = $this->getGeneratorConfig()) {
return $config->getBuildProperty($key);
}
return '';
}
/**
* Adds a new Behavior to the database
* @return Behavior A behavior instance
*/
public function addBehavior($bdata)
{
if ($bdata instanceof Behavior) {
$behavior = $bdata;
$behavior->setDatabase($this);
$this->behaviors[$behavior->getName()] = $behavior;
return $behavior;
} else {
$class = $this->getConfiguredBehavior($bdata['name']);
$behavior = new $class();
$behavior->loadFromXML($bdata);
return $this->addBehavior($behavior);
}
}
/**
* Get the database behaviors
* @return Array of Behavior objects
*/
public function getBehaviors()
{
return $this->behaviors;
}
/**
* check if the database has a behavior by name
*
* @param string $name the behavior name
* @return boolean True if the behavior exists
*/
public function hasBehavior($name)
{
return array_key_exists($name, $this->behaviors);
}
/**
* Get one database behavior by name
* @param string $name the behavior name
* @return Behavior a behavior object
*/
public function getBehavior($name)
{
return $this->behaviors[$name];
}
/**
* Get the table prefix for this database
*
* @return string the table prefix
*/
public function getTablePrefix()
{
return $this->tablePrefix;
}
/**
* Get the next behavior on all tables, ordered by behavior priority,
* and skipping the ones that were already executed,
*
* @return Behavior
*/
public function getNextTableBehavior()
{
// order the behaviors according to Behavior::$tableModificationOrder
$behaviors = array();
foreach ($this->getTables() as $table) {
foreach ($table->getBehaviors() as $behavior) {
if (!$behavior->isTableModified()) {
$behaviors[$behavior->getTableModificationOrder()][] = $behavior;
}
}
}
ksort($behaviors);
foreach ($behaviors as $behaviorList) {
foreach ($behaviorList as $behavior) {
return $behavior;
}
}
}
public function doFinalInitialization()
{
// add the referrers for the foreign keys
$this->setupTableReferrers();
// add default behaviors to database
if ($defaultBehaviors = $this->getBuildProperty('behaviorDefault')) {
// add generic behaviors from build.properties
$defaultBehaviors = explode(',', $defaultBehaviors);
foreach ($defaultBehaviors as $behavior) {
$this->addBehavior(array('name' => trim($behavior)));
}
}
// execute database behaviors
foreach ($this->getBehaviors() as $behavior) {
$behavior->modifyDatabase();
}
// execute table behaviors (may add new tables and new behaviors)
while ($behavior = $this->getNextTableBehavior()) {
$behavior->getTableModifier()->modifyTable();
$behavior->setTableModified(true);
}
// do naming and heavy indexing
foreach ($this->getTables() as $table) {
$table->doFinalInitialization();
// setup referrers again, since final initialization may have added columns
$table->setupReferrers(true);
}
}
/**
* Can be called several times
*/
protected function setupTableReferrers()
{
foreach ($this->getTables() as $table) {
$table->doNaming();
$table->setupReferrers();
}
}
/**
* @see XmlElement::appendXml(DOMNode)
*/
public function appendXml(DOMNode $node)
{
$doc = ($node instanceof DOMDocument) ? $node : $node->ownerDocument;
$dbNode = $node->appendChild($doc->createElement('database'));
$dbNode->setAttribute('name', $this->name);
if ($this->pkg) {
$dbNode->setAttribute('package', $this->pkg);
}
if ($this->defaultIdMethod) {
$dbNode->setAttribute('defaultIdMethod', $this->defaultIdMethod);
}
if ($this->baseClass) {
$dbNode->setAttribute('baseClass', $this->baseClass);
}
if ($this->basePeer) {
$dbNode->setAttribute('basePeer', $this->basePeer);
}
if ($this->defaultPhpNamingMethod) {
$dbNode->setAttribute('defaultPhpNamingMethod', $this->defaultPhpNamingMethod);
}
if ($this->defaultTranslateMethod) {
$dbNode->setAttribute('defaultTranslateMethod', $this->defaultTranslateMethod);
}
/*
FIXME - Before we can add support for domains in the schema, we need
to have a method of the Column that indicates whether the column was mapped
to a SPECIFIC domain (since Column->getDomain() will always return a Domain object)
foreach ($this->domainMap as $domain) {
$domain->appendXml($dbNode);
}
*/
foreach ($this->vendorInfos as $vi) {
$vi->appendXml($dbNode);
}
foreach ($this->tableList as $table) {
$table->appendXml($dbNode);
}
}
}