| [ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: Database.php 315 2005-12-24 20:48:31Z hans $ 5 * 6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 17 * 18 * This software consists of voluntary contributions made by many individuals 19 * and is licensed under the LGPL. For more information please see 20 * <http://propel.phpdb.org>. 21 */ 22 23 require_once 'propel/engine/database/model/XMLElement.php'; 24 include_once 'propel/engine/database/model/IDMethod.php'; 25 include_once 'propel/engine/database/model/NameGenerator.php'; 26 include_once 'propel/engine/database/model/Table.php'; 27 28 /** 29 * A class for holding application data structures. 30 * 31 * @author Hans Lellelid <hans@xmpl.org> (Propel) 32 * @author Leon Messerschmidt <leon@opticode.co.za> (Torque) 33 * @author John McNally<jmcnally@collab.net> (Torque) 34 * @author Martin Poeschl<mpoeschl@marmot.at> (Torque) 35 * @author Daniel Rall<dlr@collab.net> (Torque) 36 * @author Byron Foster <byron_foster@yahoo.com> (Torque) 37 * @version $Revision: 315 $ 38 * @package propel.engine.database.model 39 */ 40 class Database extends XMLElement { 41 42 private $platform; 43 private $tableList = array(); 44 private $curColumn; 45 private $name; 46 private $pkg; 47 private $baseClass; 48 private $basePeer; 49 private $defaultIdMethod; 50 private $defaultPhpType; 51 private $defaultPhpNamingMethod; 52 private $defaultTranslateMethod; 53 private $dbParent; 54 private $tablesByName = array(); 55 private $tablesByPhpName = array(); 56 private $heavyIndexing; 57 58 private $domainMap = array(); 59 60 /** 61 * Sets up the Database object based on the attributes that were passed to loadFromXML(). 62 * @see parent::loadFromXML() 63 */ 64 protected function setupObject() 65 { 66 $this->name = $this->getAttribute("name"); 67 $this->pkg = $this->getAttribute("package"); 68 $this->baseClass = $this->getAttribute("baseClass"); 69 $this->basePeer = $this->getAttribute("basePeer"); 70 $this->defaultPhpType = $this->getAttribute("defaultPhpType"); 71 $this->defaultIdMethod = $this->getAttribute("defaultIdMethod"); 72 $this->defaultPhpNamingMethod = $this->getAttribute("defaultPhpNamingMethod", NameGenerator::CONV_METHOD_UNDERSCORE); 73 $this->defaultTranslateMethod = $this->getAttribute("defaultTranslateMethod", Validator::TRANSLATE_NONE); 74 $this->heavyIndexing = $this->booleanValue($this->getAttribute("heavyIndexing")); 75 } 76 77 /** 78 * Returns the Platform implementation for this database. 79 * 80 * @return Platform a Platform implementation 81 */ 82 public function getPlatform() 83 { 84 return $this->platform; 85 } 86 87 /** 88 * Sets the Platform implementation for this database. 89 * 90 * @param Platform $platform A Platform implementation 91 */ 92 public function setPlatform($platform) 93 { 94 $this->platform = $platform; 95 } 96 97 /** 98 * Get the name of the Database 99 */ 100 public function getName() 101 { 102 return $this->name; 103 } 104 105 /** 106 * Set the name of the Database 107 */ 108 public function setName($name) 109 { 110 $this->name = $name; 111 } 112 113 /** 114 * Get the value of package. 115 * @return value of package. 116 */ 117 public function getPackage() 118 { 119 return $this->pkg; 120 } 121 122 /** 123 * Set the value of package. 124 * @param v Value to assign to package. 125 */ 126 public function setPackage($v) 127 { 128 $this->pkg = $v; 129 } 130 131 /** 132 * Get the value of baseClass. 133 * @return value of baseClass. 134 */ 135 public function getBaseClass() 136 { 137 return $this->baseClass; 138 } 139 140 /** 141 * Set the value of baseClass. 142 * @param v Value to assign to baseClass. 143 */ 144 public function setBaseClass($v) 145 { 146 $this->baseClass = $v; 147 } 148 149 /** 150 * Get the value of basePeer. 151 * @return value of basePeer. 152 */ 153 public function getBasePeer() 154 { 155 return $this->basePeer; 156 } 157 158 /** 159 * Set the value of basePeer. 160 * @param v Value to assign to basePeer. 161 */ 162 public function setBasePeer($v) 163 { 164 $this->basePeer = $v; 165 } 166 167 /** 168 * Get the value of defaultIdMethod. 169 * @return value of defaultIdMethod. 170 */ 171 public function getDefaultIdMethod() 172 { 173 return $this->defaultIdMethod; 174 } 175 176 /** 177 * Set the value of defaultIdMethod. 178 * @param v Value to assign to defaultIdMethod. 179 */ 180 public function setDefaultIdMethod($v) 181 { 182 $this->defaultIdMethod = $v; 183 } 184 185 /** 186 * Get the value of defaultPHPNamingMethod which specifies the 187 * method for converting schema names for table and column to PHP names. 188 * @return string The default naming conversion used by this database. 189 */ 190 public function getDefaultPhpNamingMethod() 191 { 192 return $this->defaultPhpNamingMethod; 193 } 194 195 /** 196 * Set the value of defaultPHPNamingMethod. 197 * @param string $v The default naming conversion for this database to use. 198 */ 199 public function setDefaultPhpNamingMethod($v) 200 { 201 $this->defaultPhpNamingMethod = $v; 202 } 203 204 /** 205 * Get the value of defaultTranslateMethod which specifies the 206 * method for translate validator error messages. 207 * @return string The default translate method. 208 */ 209 public function getDefaultTranslateMethod() 210 { 211 return $this->defaultTranslateMethod; 212 } 213 214 /** 215 * Set the value of defaultTranslateMethod. 216 * @param string $v The default translate method to use. 217 */ 218 public function setDefaultTranslateMethod($v) 219 { 220 $this->defaultTranslateMethod = $v; 221 } 222 223 /** 224 * Get the value of heavyIndexing. 225 * @return boolean Value of heavyIndexing. 226 */ 227 public function isHeavyIndexing() 228 { 229 return $this->heavyIndexing; 230 } 231 232 /** 233 * Set the value of heavyIndexing. 234 * @param boolean $v Value to assign to heavyIndexing. 235 */ 236 public function setHeavyIndexing($v) 237 { 238 $this->heavyIndexing = (boolean) $v; 239 } 240 241 /** 242 * Return an array of all tables 243 */ 244 public function getTables() 245 { 246 return $this->tableList; 247 } 248 249 /** 250 * Return the table with the specified name. 251 * @param string $name The name of the table (e.g. 'my_table') 252 * @return Table a Table object or null if it doesn't exist 253 */ 254 public function getTable($name) 255 { 256 if (isset($this->tablesByName[$name])) { 257 return $this->tablesByName[$name]; 258 } 259 return null; // just to be explicit 260 } 261 262 /** 263 * Return the table with the specified phpName. 264 * @param string $phpName the PHP Name of the table (e.g. 'MyTable') 265 * @return Table a Table object or null if it doesn't exist 266 */ 267 public function getTableByPhpName($phpName) 268 { 269 if (isset($this->tablesByPhpName[$phpName])) { 270 return $this->tablesByPhpName[$phpName]; 271 } 272 return null; // just to be explicit 273 } 274 275 /** 276 * An utility method to add a new table from an xml attribute. 277 */ 278 public function addTable($data) 279 { 280 if ($data instanceof Table) { 281 $tbl = $data; // alias 282 $tbl->setDatabase($this); 283 $this->tableList[] = $tbl; 284 $this->tablesByName[ $tbl->getName() ] = $tbl; 285 $this->tablesByPhpName[ $tbl->getPhpName() ] = $tbl; 286 if ($tbl->getPackage() === null) { 287 $tbl->setPackage($this->getPackage()); 288 } 289 return $tbl; 290 } else { 291 $tbl = new Table(); 292 $tbl->setDatabase($this); 293 $tbl->loadFromXML($data); 294 return $this->addTable($tbl); // call self w/ different param 295 } 296 } 297 298 /** 299 * Set the parent of the database 300 */ 301 public function setAppData(AppData $parent) 302 { 303 $this->dbParent = $parent; 304 } 305 306 /** 307 * Get the parent of the table 308 */ 309 public function getAppData() 310 { 311 return $this->dbParent; 312 } 313 314 /** 315 * Adds Domain object from <domain> tag. 316 * @param mixed XML attributes (array) or Domain object. 317 */ 318 public function addDomain($data) { 319 320 if ($data instanceof Domain) { 321 $domain = $data; // alias 322 $domain->setDatabase($this); 323 $this->domainMap[ $domain->getName() ] = $domain; 324 return $domain; 325 } else { 326 $domain = new Table(); 327 $domain->setDatabase($this); 328 $domain->loadFromXML($data); 329 return $this->addDomain($domain); // call self w/ different param 330 } 331 } 332 333 /** 334 * Get already configured Domain object by name. 335 * @return Domain 336 */ 337 public function getDomain($domainName) { 338 if (!isset($this->domainMap[$domainName])) { 339 return null; 340 } 341 return $this->domainMap[$domainName]; 342 } 343 344 public function doFinalInitialization() 345 { 346 $tables = $this->getTables(); 347 348 for($i=0,$size=count($tables); $i < $size; $i++) { 349 $currTable = $tables[$i]; 350 351 // check schema integrity 352 // if idMethod="autoincrement", make sure a column is 353 // specified as autoIncrement="true" 354 // FIXME: Handle idMethod="native" via DB adapter. 355 /* 356 357 --- REMOVING THIS BECAUSE IT'S ANNOYING 358 359 if ($currTable->getIdMethod() == IDMethod::NATIVE ) { 360 $columns = $currTable->getColumns(); 361 $foundOne = false; 362 for ($j=0, $cLen=count($columns); $j < $cLen && !$foundOne; $j++) { 363 $foundOne = $columns[$j]->isAutoIncrement(); 364 } 365 366 if (!$foundOne) { 367 $errorMessage = "Table '" . $currTable->getName() 368 . "' is set to use native id generation, but it does not " 369 . "have a column which declared as the one to " 370 . "auto increment (i.e. autoIncrement=\"true\")"; 371 372 throw new BuildException($errorMessage); 373 } 374 } 375 */ 376 377 $currTable->doFinalInitialization(); 378 379 // setup reverse fk relations 380 $fks = $currTable->getForeignKeys(); 381 for ($j=0, $fksLen=count($fks); $j < $fksLen; $j++) { 382 $currFK = $fks[$j]; 383 $foreignTable = $this->getTable($currFK->getForeignTableName()); 384 if ($foreignTable === null) { 385 throw new BuildException("ERROR!! Attempt to set foreign" 386 . " key to nonexistent table, " 387 . $currFK->getForeignTableName() . "!"); 388 } 389 390 $referrers = $foreignTable->getReferrers(); 391 if ($referrers === null || ! in_array($currFK,$referrers) ) { 392 $foreignTable->addReferrer($currFK); 393 } 394 395 // local column references 396 $localColumnNames = $currFK->getLocalColumns(); 397 398 for($k=0,$lcnLen=count($localColumnNames); $k < $lcnLen; $k++) { 399 400 $local = $currTable->getColumn($localColumnNames[$k]); 401 402 // give notice of a schema inconsistency. 403 // note we do not prevent the npe as there is nothing 404 // that we can do, if it is to occur. 405 if ($local === null) { 406 throw new BuildException("ERROR!! Attempt to define foreign" 407 . " key with nonexistent column, " 408 . $localColumnNames[$k] . ", in table, " 409 . $currTable->getName() . "!"); 410 } 411 412 //check for foreign pk's 413 if ($local->isPrimaryKey()) { 414 $currTable->setContainsForeignPK(true); 415 } 416 417 } // for each local col name 418 419 // foreign column references 420 $foreignColumnNames = $currFK->getForeignColumns(); 421 for($k=0,$fcnLen=count($localColumnNames); $k < $fcnLen; $k++) { 422 $foreign = $foreignTable->getColumn($foreignColumnNames[$k]); 423 // if the foreign column does not exist, we may have an 424 // external reference or a misspelling 425 if ($foreign === null) { 426 throw new BuildException("ERROR!! Attempt to set foreign" 427 . " key to nonexistent column, " 428 . $foreignColumnNames[$k] . ", in table, " 429 . $foreignTable->getName() . "!"); 430 } else { 431 $foreign->addReferrer($currFK); 432 } 433 } // for each foreign col ref 434 } 435 } 436 } 437 438 /** 439 * Creats a string representation of this Database. 440 * The representation is given in xml format. 441 */ 442 public function toString() 443 { 444 $result = "<database name=\"" . $this->getName() . '"' 445 . " package=\"" . $this->getPackage() . '"' 446 . " defaultIdMethod=\"" . $this->getDefaultIdMethod() 447 . '"' 448 . " baseClass=\"" . $this->getBaseClass() . '"' 449 . " basePeer=\"" . $this->getBasePeer() . '"' 450 . ">\n"; 451 452 for ($i=0, $size=count($this->tableList); $i < $size; $i++) { 453 $result .= $this->tableList[$i]->toString(); 454 } 455 456 $result .= "</database>"; 457 458 return $result; 459 } 460 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |