[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/propel-generator/classes/propel/engine/database/model/ -> Table.php (source)

   1  <?php
   2  
   3  /*
   4   *  $Id: Table.php 351 2006-03-15 18:42:34Z 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/EngineException.php';
  25  include_once 'propel/engine/database/model/IDMethod.php';
  26  include_once 'propel/engine/database/model/NameFactory.php';
  27  include_once 'propel/engine/database/model/Column.php';
  28  include_once 'propel/engine/database/model/Unique.php';
  29  include_once 'propel/engine/database/model/ForeignKey.php';
  30  include_once 'propel/engine/database/model/IdMethodParameter.php';
  31  include_once 'propel/engine/database/model/Validator.php';
  32  
  33  /**
  34   * Data about a table used in an application.
  35   *
  36   * @author Hans Lellelid <hans@xmpl.org> (Propel)
  37   * @author Leon Messerschmidt <leon@opticode.co.za> (Torque)
  38   * @author Jason van Zyl <jvanzyl@apache.org> (Torque)
  39   * @author Martin Poeschl <mpoeschl@marmot.at> (Torque)
  40   * @author John McNally <jmcnally@collab.net> (Torque)
  41   * @author Daniel Rall <dlr@collab.net> (Torque)
  42   * @author Byron Foster <byron_foster@yahoo.com> (Torque)
  43   * @version $Revision: 351 $
  44   * @package propel.engine.database.model
  45   */
  46  class Table extends XMLElement implements IDMethod {
  47  
  48      /** enables debug output */
  49      const DEBUG = false;
  50  
  51      //private attributes;
  52      private $columnList;
  53      private $validatorList;
  54      private $foreignKeys;
  55      private $indices;
  56      private $unices;
  57      private $idMethodParameters;
  58      private $name;
  59      private $description;
  60      private $phpName;
  61      private $idMethod;
  62      private $phpNamingMethod;
  63      private $tableParent;
  64      private $referrers = array();
  65      private $foreignTableNames;
  66      private $containsForeignPK;
  67      private $inheritanceColumn;
  68      private $skipSql;
  69      private $readOnly;
  70      private $abstractValue;
  71      private $alias;
  72      private $enterface;
  73      private $pkg;
  74      private $baseClass;
  75      private $basePeer;
  76      private $columnsByName;
  77      private $columnsByPhpName;
  78      private $needsTransactionInPostgres;//maybe this can be retrieved from vendorSpecificInfo?
  79      private $heavyIndexing;
  80      private $forReferenceOnly;
  81      private $isTree;
  82  
  83      /**
  84       * Constructs a table object with a name
  85       *
  86       * @param string $name table name
  87       */
  88      public function __construct($name = null)
  89      {
  90          $this->name = $name;
  91          $this->columnList = array();
  92          $this->validatorList = array();
  93          $this->foreignKeys = array();
  94          $this->indices = array();
  95          $this->unices = array();
  96          $this->columnsByName = array();
  97          $this->columnsByPhpName = array();
  98          $this->vendorSpecificInfo = array();
  99      }
 100  
 101     /**
 102       * Sets up the Rule object based on the attributes that were passed to loadFromXML().
 103       * @see parent::loadFromXML()
 104       */
 105      public function setupObject()
 106      {
 107          $this->name = $this->getAttribute("name");
 108          $this->phpName = $this->getAttribute("phpName");
 109          $this->idMethod = $this->getAttribute("idMethod", $this->getDatabase()->getDefaultIdMethod());
 110  
 111          // retrieves the method for converting from specified name to a PHP name.
 112          $this->phpNamingMethod = $this->getAttribute("phpNamingMethod", $this->getDatabase()->getDefaultPhpNamingMethod());
 113  
 114          $this->skipSql = $this->booleanValue($this->getAttribute("skipSql"));
 115          $this->readOnly = $this->booleanValue($this->getAttribute("readOnly"));
 116  
 117          $this->pkg = $this->getAttribute("package");
 118          $this->abstractValue = $this->booleanValue($this->getAttribute("abstract"));
 119          $this->baseClass = $this->getAttribute("baseClass");
 120          $this->basePeer = $this->getAttribute("basePeer");
 121          $this->alias = $this->getAttribute("alias");
 122  
 123          $this->heavyIndexing = ( $this->booleanValue($this->getAttribute("heavyIndexing"))
 124                  || ("false" !== $this->getAttribute("heavyIndexing")
 125                          && $this->getDatabase()->isHeavyIndexing() ) );
 126          $this->description = $this->getAttribute("description");
 127          $this->enterface = $this->getAttribute("interface"); // sic ('interface' is reserved word)
 128          $this->isTree = $this->booleanValue($this->getAttribute("isTree"));
 129      }
 130  
 131      /**
 132       * <p>A hook for the SAX XML parser to call when this table has
 133       * been fully loaded from the XML, and all nested elements have
 134       * been processed.</p>
 135       *
 136       * <p>Performs heavy indexing and naming of elements which weren't
 137       * provided with a name.</p>
 138       */
 139      public function doFinalInitialization()
 140      {
 141          // Heavy indexing must wait until after all columns composing
 142          // a table's primary key have been parsed.
 143          if ($this->heavyIndexing) {
 144              $this->doHeavyIndexing();
 145          }
 146  
 147          // Name any indices which are missing a name using the
 148          // appropriate algorithm.
 149          $this->doNaming();
 150  
 151          // if idMethod is "native" and in fact there are no autoIncrement
 152          // columns in the table, then change it to "none"
 153          if ($this->getIdMethod() === IDMethod::NATIVE) {
 154              $anyAutoInc = false;
 155              foreach($this->getColumns() as $col) {
 156                  if ($col->isAutoIncrement()) {
 157                      $anyAutoInc = true;
 158                      break;
 159                  }
 160              }
 161              if (!$anyAutoInc) {
 162                  $this->setIdMethod(IDMethod::NO_ID_METHOD);
 163              }
 164          }
 165      }
 166  
 167      /**
 168       * <p>Adds extra indices for multi-part primary key columns.</p>
 169       *
 170       * <p>For databases like MySQL, values in a where clause much
 171       * match key part order from the left to right.  So, in the key
 172       * definition <code>PRIMARY KEY (FOO_ID, BAR_ID)</code>,
 173       * <code>FOO_ID</code> <i>must</i> be the first element used in
 174       * the <code>where</code> clause of the SQL query used against
 175       * this table for the primary key index to be used.  This feature
 176       * could cause problems under MySQL with heavily indexed tables,
 177       * as MySQL currently only supports 16 indices per table (i.e. it
 178       * might cause too many indices to be created).</p>
 179       *
 180       * <p>See <a href="http://www.mysql.com/doc/E/X/EXPLAIN.html">the
 181       * manual</a> for a better description of why heavy indexing is
 182       * useful for quickly searchable database tables.</p>
 183       */
 184      private function doHeavyIndexing()
 185      {
 186          if (self::DEBUG) {
 187              print("doHeavyIndex() called on table " . $this->name."\n");
 188          }
 189  
 190          $pk = $this->getPrimaryKey();
 191          $size = count($pk);
 192  
 193          try {
 194              // We start at an offset of 1 because the entire column
 195              // list is generally implicitly indexed by the fact that
 196              // it's a primary key.
 197              for ($i=1; $i < $size; $i++) {
 198                  $this->addIndex(new Index($this, array_slice($pk, $i, $size)));
 199              }
 200          } catch (EngineException $e) {
 201              print $e->getMessage() . "\n";
 202              print $e->getTraceAsString();
 203          }
 204      }
 205  
 206      /**
 207       * Names composing objects which haven't yet been named.  This
 208       * currently consists of foreign-key and index entities.
 209       */
 210      private function doNaming() {
 211  
 212          // Assure names are unique across all databases.
 213          try {
 214              for ($i=0, $size = count($this->foreignKeys); $i < $size; $i++) {
 215                  $fk = $this->foreignKeys[$i];
 216                  $name = $fk->getName();
 217                  if (empty($name)) {
 218                      $name = $this->acquireConstraintName("FK", $i + 1);
 219                      $fk->setName($name);
 220                  }
 221              }
 222  
 223              for ($i = 0, $size = count($this->indices); $i < $size; $i++) {
 224                  $index = $this->indices[$i];
 225                  $name = $index->getName();
 226                  if (empty($name)) {
 227                      $name = $this->acquireConstraintName("I", $i + 1);
 228                      $index->setName($name);
 229                  }
 230              }
 231  
 232              for ($i = 0, $size = count($this->unices); $i < $size; $i++) {
 233                  $index = $this->unices[$i];
 234                  $name = $index->getName();
 235                  if (empty($name)) {
 236                      $name = $this->acquireConstraintName("U", $i + 1);
 237                      $index->setName($name);
 238                  }
 239              }
 240  
 241              // NOTE: Most RDBMSes can apparently name unique column
 242              // constraints/indices themselves (using MySQL and Oracle
 243              // as test cases), so we'll assume that we needn't add an
 244              // entry to the system name list for these.
 245          } catch (EngineException $nameAlreadyInUse) {
 246              print $nameAlreadyInUse->getMessage() . "\n";
 247              print $nameAlreadyInUse->getTraceAsString();
 248          }
 249      }
 250  
 251      /**
 252       * Macro to a constraint name.
 253       *
 254       * @param nameType constraint type
 255       * @param nbr unique number for this constraint type
 256       * @return unique name for constraint
 257       * @throws EngineException
 258       */
 259      private function acquireConstraintName($nameType, $nbr)
 260      {
 261          $inputs = array();
 262          $inputs[] = $this->getDatabase();
 263          $inputs[] = $this->getName();
 264          $inputs[] = $nameType;
 265          $inputs[] = $nbr;
 266          return NameFactory::generateName(NameFactory::CONSTRAINT_GENERATOR, $inputs);
 267      }
 268  
 269      /**
 270       * Gets the value of base class for classes produced from this table.
 271       *
 272       * @return The base class for classes produced from this table.
 273       */
 274      public function getBaseClass()
 275      {
 276          if ($this->isAlias() && $this->baseClass === null) {
 277              return $this->alias;
 278          } elseif ($this->baseClass === null) {
 279              return $this->getDatabase()->getBaseClass();
 280          } else {
 281              return $this->baseClass;
 282          }
 283      }
 284  
 285      /**
 286       * Set the value of baseClass.
 287       * @param v  Value to assign to baseClass.
 288       */
 289      public function setBaseClass($v)
 290      {
 291          $this->baseClass = $v;
 292      }
 293  
 294      /**
 295       * Get the value of basePeer.
 296       * @return value of basePeer.
 297       */
 298      public function getBasePeer()
 299      {
 300          if ($this->isAlias() && $this->basePeer === null) {
 301              return $this->alias . "Peer";
 302          } elseif ($this->basePeer === null) {
 303              return $this->getDatabase()->getBasePeer();
 304          } else {
 305              return $this->basePeer;
 306          }
 307      }
 308  
 309      /**
 310       * Set the value of basePeer.
 311       * @param v  Value to assign to basePeer.
 312       */
 313      public function setBasePeer($v)
 314      {
 315          $this->basePeer = $v;
 316      }
 317  
 318      /**
 319       * A utility function to create a new column from attrib and add it to this
 320       * table.
 321       *
 322       * @param $coldata xml attributes or Column class for the column to add
 323       * @return the added column
 324       */
 325      public function addColumn($data)
 326      {
 327          if ($data instanceof Column) {
 328              $col = $data; // alias
 329              $col->setTable($this);
 330              if ($col->isInheritance()) {
 331                  $this->inheritanceColumn = $col;
 332              }
 333              $this->columnList[] = $col;
 334              $this->columnsByName[$col->getName()] = $col;
 335              $this->columnsByPhpName[$col->getPhpName()] = $col;
 336              $col->setPosition(count($this->columnList));
 337              $this->needsTransactionInPostgres |= $col->requiresTransactionInPostgres();
 338              return $col;
 339          } else {
 340              $col = new Column();
 341              $col->setTable($this);
 342              $col->loadFromXML($data);
 343              return $this->addColumn($col); // call self w/ different param
 344          }
 345      }
 346  
 347     /**
 348      * Add a validator to this table.
 349      *
 350      * Supports two signatures:
 351      * - addValidator(Validator $validator)
 352      * - addValidator(array $attribs)
 353      *
 354      * @param mixed $data Validator object or XML attribs (array) from <validator /> element.
 355      * @return Validator The added Validator.
 356      * @throws EngineException
 357      */
 358     public function addValidator($data)
 359     {
 360       if ($data instanceof Validator)
 361       {
 362        $validator = $data;
 363        $col = $this->getColumn($validator->getColumnName());
 364        if($col == null) {
 365          throw new EngineException("Failed adding validator to table '" . $this->getName() .
 366            "': column '" . $validator->getColumnName() . "' does not exist !");
 367        }
 368        $validator->setColumn($col);
 369        $validator->setTable($this);
 370        $this->validatorList[] = $validator;
 371        return $validator;
 372       }
 373       else
 374       {
 375        $validator = new Validator();
 376        $validator->setTable($this);
 377        $validator->loadFromXML($data);
 378        return $this->addValidator($validator);
 379       }
 380     }
 381  
 382      /**
 383       * A utility function to create a new foreign key
 384       * from attrib and add it to this table.
 385       */
 386      public function addForeignKey($fkdata)
 387      {
 388          if ($fkdata instanceof ForeignKey) {
 389              $fk = $fkdata;
 390              $fk->setTable($this);
 391              $this->foreignKeys[] = $fk;
 392  
 393              if ($this->foreignTableNames === null) {
 394                  $this->foreignTableNames = array();
 395              }
 396              if (!in_array($fk->getForeignTableName(), $this->foreignTableNames)) {
 397                  $this->foreignTableNames[] = $fk->getForeignTableName();
 398              }
 399              return $fk;
 400          } else {
 401              $fk = new ForeignKey();
 402              $fk->loadFromXML($fkdata);
 403              return $this->addForeignKey($fk);
 404          }
 405      }
 406  
 407      /**
 408       * Gets the column that subclasses of the class representing this
 409       * table can be produced from.
 410       * @return string
 411       */
 412      public function getChildrenColumn()
 413      {
 414          return $this->inheritanceColumn;
 415      }
 416  
 417      /**
 418       * Get the subclasses that can be created from this table.
 419       * @return array string[] Class names
 420       */
 421      public function getChildrenNames()
 422      {
 423          if ($this->inheritanceColumn === null
 424                  || !$this->inheritanceColumn->isEnumeratedClasses()) {
 425              return null;
 426          }
 427          $children = $this->inheritanceColumn->getChildren();
 428          $names = array();
 429          for ($i = 0, $size=count($children); $i < $size; $i++) {
 430              $names[] = get_class($children[$i]);
 431          }
 432          return $names;
 433      }
 434  
 435      /**
 436       * Adds the foreign key from another table that refers to this table.
 437       */
 438      public function addReferrer(ForeignKey $fk)
 439      {
 440          if ($this->referrers === null) {
 441              $this->referrers = array();
 442          }
 443          $this->referrers[] = $fk;
 444      }
 445  
 446      /**
 447       * Get list of references to this table.
 448       */
 449      public function getReferrers()
 450      {
 451          return $this->referrers;
 452      }
 453  
 454      /**
 455       * Set whether this table contains a foreign PK
 456       */
 457      public function setContainsForeignPK($b)
 458      {
 459          $this->containsForeignPK = (boolean) $b;
 460      }
 461  
 462      /**
 463       * Determine if this table contains a foreign PK
 464       */
 465      public function getContainsForeignPK()
 466      {
 467          return $this->containsForeignPK;
 468      }
 469  
 470      /**
 471       * A list of tables referenced by foreign keys in this table
 472       */
 473      public function getForeignTableNames()
 474      {
 475          if ($this->foreignTableNames === null) {
 476              $this->foreignTableNames = array();
 477          }
 478          return $this->foreignTableNames;
 479      }
 480  
 481      /**
 482       * Return true if the column requires a transaction in Postgres
 483       */
 484      public function requiresTransactionInPostgres()
 485      {
 486          return $this->needsTransactionInPostgres;
 487      }
 488  
 489      /**
 490       * A utility function to create a new id method parameter
 491       * from attrib or object and add it to this table.
 492       */
 493      public function addIdMethodParameter($impdata)
 494      {
 495          if ($impdata instanceof IdMethodParameter) {
 496              $imp = $impdata;
 497              $imp->setTable($this);
 498              if ($this->idMethodParameters === null) {
 499                  $this->idMethodParameters = array();
 500              }
 501              $this->idMethodParameters[] = $imp;
 502              return $imp;
 503          } else {
 504              $imp = new IdMethodParameter();
 505              $imp->loadFromXML($impdata);
 506              return $this->addIdMethodParameter($imp); // call self w/ diff param
 507          }
 508      }
 509  
 510      /**
 511       * Adds a new index to the index list and set the
 512       * parent table of the column to the current table
 513       */
 514      public function addIndex($idxdata)
 515      {
 516          if ($idxdata instanceof Index) {
 517              $index = $idxdata;
 518              $index->setTable($this);
 519              $index->getName(); // we call this method so that the name is created now if it doesn't already exist.
 520              $this->indices[] = $index;
 521              return $index;
 522          } else {
 523              $index = new Index($this);
 524              $index->loadFromXML($idxdata);
 525              return $this->addIndex($index);
 526          }
 527      }
 528  
 529      /**
 530       * Adds a new Unique to the Unique list and set the
 531       * parent table of the column to the current table
 532       */
 533      public function addUnique($unqdata)
 534      {
 535          if ($unqdata instanceof Unique) {
 536              $unique = $unqdata;
 537              $unique->setTable($this);
 538              $unique->getName(); // we call this method so that the name is created now if it doesn't already exist.
 539              $this->unices[] = $unique;
 540              return $unique;
 541          } else {
 542              $unique = new Unique($this);
 543              $unique->loadFromXML($unqdata);
 544              return $this->addUnique($unique);
 545          }
 546      }
 547  
 548      /**
 549       * Get the name of the Table
 550       */
 551      public function getName()
 552      {
 553          return $this->name;
 554      }
 555  
 556      /**
 557       * Set the name of the Table
 558       */
 559      public function setName($newName)
 560      {
 561          $this->name = $newName;
 562      }
 563  
 564      /**
 565       * Get the description for the Table
 566       */
 567      public function getDescription()
 568      {
 569          return $this->description;
 570      }
 571  
 572      /**
 573       * Set the description for the Table
 574       *
 575       * @param newDescription description for the Table
 576       */
 577      public function setDescription($newDescription)
 578      {
 579          $this->description = $newDescription;
 580      }
 581  
 582      /**
 583       * Get name to use in PHP sources
 584       * @return string
 585       */
 586      public function getPhpName()
 587      {
 588          if ($this->phpName === null) {
 589              $inputs = array();
 590              $inputs[] = $this->name;
 591              $inputs[] = $this->phpNamingMethod;
 592              try {
 593                  $this->phpName = NameFactory::generateName(NameFactory::PHP_GENERATOR, $inputs);
 594              } catch (EngineException $e) {
 595                  print $e->getMessage() . "\n";
 596                  print $e->getTraceAsString();
 597              }
 598          }
 599          return $this->phpName;
 600      }
 601  
 602      /**
 603       * Set name to use in PHP sources
 604       * @param string $phpName
 605       */
 606      public function setPhpName($phpName)
 607      {
 608          $this->phpName = $phpName;
 609      }
 610  
 611      /**
 612       * Get the method for generating pk's
 613       * [HL] changing behavior so that Database default
 614       *        method is returned if no method has been specified
 615       *        for the table.
 616       * @return string
 617       */
 618      public function getIdMethod()
 619      {
 620          if ($this->idMethod === null) {
 621              return IDMethod::NO_ID_METHOD;
 622          } else {
 623              return $this->idMethod;
 624          }
 625      }
 626  
 627      /**
 628       * Set the method for generating pk's
 629       */
 630      public function setIdMethod($idMethod)
 631      {
 632          $this->idMethod = $idMethod;
 633      }
 634  
 635      /**
 636       * Skip generating sql for this table (in the event it should
 637       * not be created from scratch).
 638       * @return boolean Value of skipSql.
 639       */
 640      public function isSkipSql()
 641      {
 642          return ($this->skipSql || $this->isAlias() || $this->isForReferenceOnly());
 643      }
 644  
 645      /**
 646       * Is table read-only, in which case only accessors (and relationship setters)
 647       * will be created.
 648       * @return boolan Value of readOnly.
 649       */
 650      public function isReadOnly()
 651      {
 652          return $this->readOnly;
 653      }
 654  
 655      /**
 656       * Set whether this table should have its creation sql generated.
 657       * @param boolean $v Value to assign to skipSql.
 658       */
 659      public function setSkipSql($v)
 660      {
 661          $this->skipSql = $v;
 662      }
 663  
 664      /**
 665       * PhpName of om object this entry references.
 666       * @return value of external.
 667       */
 668      public function getAlias()
 669      {
 670          return $this->alias;
 671      }
 672  
 673      /**
 674       * Is this table specified in the schema or is there just
 675       * a foreign key reference to it.
 676       * @return value of external.
 677       */
 678      public function isAlias()
 679      {
 680          return ($this->alias !== null);
 681      }
 682  
 683      /**
 684       * Set whether this table specified in the schema or is there just
 685       * a foreign key reference to it.
 686       * @param v  Value to assign to alias.
 687       */
 688      public function setAlias($v)
 689      {
 690          $this->alias = $v;
 691      }
 692  
 693  
 694      /**
 695       * Interface which objects for this table will implement
 696       * @return value of interface.
 697       */
 698      public function getInterface()
 699      {
 700          return $this->enterface;
 701      }
 702  
 703      /**
 704       * Interface which objects for this table will implement
 705       * @param v  Value to assign to interface.
 706       */
 707      public function setInterface($v)
 708      {
 709          $this->enterface = $v;
 710      }
 711  
 712      /**
 713       * When a table is abstract, it marks the business object class that is
 714       * generated as being abstract. If you have a table called "FOO", then the
 715       * Foo BO will be <code>public abstract class Foo</code>
 716       * This helps support class hierarchies
 717       *
 718       * @return value of abstractValue.
 719       */
 720      public function isAbstract()
 721      {
 722          return $this->abstractValue;
 723      }
 724  
 725      /**
 726       * When a table is abstract, it marks the business object
 727       * class that is generated as being abstract. If you have a
 728       * table called "FOO", then the Foo BO will be
 729       * <code>public abstract class Foo</code>
 730       * This helps support class hierarchies
 731       *
 732       * @param v  Value to assign to abstractValue.
 733       */
 734      public function setAbstract($v)
 735      {
 736          $this->abstractValue = (boolean) $v;
 737      }
 738  
 739      /**
 740       * Get the value of package.
 741       * @return value of package.
 742       */
 743      public function getPackage()
 744      {
 745          return $this->pkg;
 746      }
 747  
 748      /**
 749       * Set the value of package.
 750       * @param v  Value to assign to package.
 751       */
 752      public function setPackage($v)
 753      {
 754          $this->pkg = $v;
 755      }
 756  
 757      /**
 758       * Returns an Array containing all the columns in the table
 759       */
 760      public function getColumns()
 761      {
 762          return $this->columnList;
 763      }
 764  
 765      /**
 766       * Utility method to get the number of columns in this table
 767       */
 768      public function getNumColumns()
 769      {
 770          return count($this->columnList);
 771      }
 772  
 773      /**
 774       * Utility method to get the number of columns in this table
 775       */
 776      public function getNumLazyLoadColumns()
 777      {
 778          $count = 0;
 779          foreach($this->columnList as $col) {
 780              if ($col->isLazyLoad()) {
 781                  $count++;
 782              }
 783          }
 784          return $count;
 785      }
 786  
 787      /**
 788       * Returns an Array containing all the validators in the table
 789       */
 790      public function getValidators()
 791      {
 792        return $this->validatorList;
 793      }
 794  
 795      /**
 796       * Returns an Array containing all the FKs in the table
 797       */
 798      public function getForeignKeys()
 799      {
 800          return $this->foreignKeys;
 801      }
 802  
 803      /**
 804       * Returns a Collection of parameters relevant for the chosen
 805       * id generation method.
 806       */
 807      public function getIdMethodParameters()
 808      {
 809          return $this->idMethodParameters;
 810      }
 811  
 812      /**
 813       * A name to use for creating a sequence if one is not specified.
 814       */
 815      public function getSequenceName()
 816      {
 817          static $longNamesMap = array();
 818          $result = null;
 819          if ($this->getIdMethod() == self::NATIVE) {
 820              $idMethodParams = $this->getIdMethodParameters();
 821              if ($idMethodParams === null) {
 822                  $maxIdentifierLength = $this->getDatabase()->getPlatform()->getMaxColumnNameLength();
 823                  if(strlen($this->getName() . "_SEQ") > $maxIdentifierLength)
 824                  {
 825                    if(!isset($longNamesMap[$this->getName()]))
 826                    {
 827                      $longNamesMap[$this->getName()] = strval(count($longNamesMap) + 1);
 828                    }
 829                    $result = substr($this->getName(), 0, $maxIdentifierLength - strlen("_SEQ_" . $longNamesMap[$this->getName()])) . "_SEQ_" . $longNamesMap[$this->getName()];
 830                  }
 831                  else
 832                  {
 833                    $result = $this->getName() . "_SEQ";
 834                  }
 835              } else {
 836                  $result = $idMethodParams[0]->getValue();
 837              }
 838          }
 839          return $result;
 840      }
 841  
 842      /**
 843       * Returns an Array containing all the FKs in the table
 844       */
 845      public function getIndices()
 846      {
 847          return $this->indices;
 848      }
 849  
 850      /**
 851       * Returns an Array containing all the UKs in the table
 852       */
 853      public function getUnices()
 854      {
 855          return $this->unices;
 856      }
 857  
 858      /**
 859       * Returns a specified column.
 860       * @return Return a Column object or null if it does not exist.
 861       */
 862      public function getColumn($name)
 863      {
 864          return @$this->columnsByName[$name];
 865      }
 866  
 867      /**
 868       * Returns a specified column.
 869       * @return Return a Column object or null if it does not exist.
 870       */
 871      public function getColumnByPhpName($phpName)
 872      {
 873          return @$this->columnsByPhpName[$phpName];
 874      }
 875  
 876      /**
 877       * Return the first foreign key that includes col in it's list
 878       * of local columns.  Eg. Foreign key (a,b,c) refrences tbl(x,y,z)
 879       * will be returned of col is either a,b or c.
 880       * @param string $col
 881       * @return Return a Column object or null if it does not exist.
 882       */
 883      public function getForeignKey($col)
 884      {
 885          $firstFK = null;
 886          for($i=0,$size=count($this->foreignKeys); $i < $size; $i++) {
 887              $key = $this->foreignKeys[$i];
 888              if (in_array($col, $key->getLocalColumns())) {
 889                  if ($firstFK === null) {
 890                      $firstFK = $key;
 891                  } else {
 892                      throw new EngineException($col . " has ben declared as a foreign key multiple times.  This is not"
 893                                         . " being handled properly. (Try moving foreign key declarations to the foreign table.)");
 894                  }
 895              }
 896          }
 897          return $firstFK;
 898      }
 899  
 900      /**
 901       * Returns true if the table contains a specified column
 902       * @param mixed $col Column or column name.
 903       */
 904      public function containsColumn($col)
 905      {
 906          if ($col instanceof Column) {
 907              return in_array($col, $this->columnList);
 908          } else {
 909              return ($this->getColumn($col) !== null);
 910          }
 911      }
 912  
 913      /**
 914       * Set the parent of the table
 915       *
 916       * @param parent the parant database
 917       */
 918      public function setDatabase($parent)
 919      {
 920          $this->tableParent = $parent;
 921      }
 922  
 923      /**
 924       * Get the parent of the table
 925       *
 926       * @return the parant database
 927       */
 928      public function getDatabase()
 929      {
 930          return $this->tableParent;
 931      }
 932  
 933      /**
 934       * Flag to determine if code/sql gets created for this table.
 935       * Table will be skipped, if return true.
 936       * @return value of forReferenceOnly.
 937       */
 938      public function isForReferenceOnly()
 939      {
 940          return $this->forReferenceOnly;
 941      }
 942  
 943      /**
 944       * Flag to determine if code/sql gets created for this table.
 945       * Table will be skipped, if set to true.
 946       * @param v  Value to assign to forReferenceOnly.
 947       */
 948      public function setForReferenceOnly($v)
 949      {
 950          $this->forReferenceOnly = (boolean) $v;
 951      }
 952  
 953     /**
 954       * Flag to determine if tree node class should be generated for this table.
 955       * @return valur of isTree
 956      */
 957     public function isTree()
 958     {
 959          return $this->isTree;
 960     }
 961  
 962      /**
 963       * Flag to determine if tree node class should be generated for this table.
 964       * @param v  Value to assign to isTree.
 965       */
 966      public function setIsTree($v)
 967      {
 968          $this->isTree = (boolean) $v;
 969      }
 970  
 971      /**
 972       * Returns a XML representation of this table.
 973       *
 974       * @return XML representation of this table
 975       */
 976      public function toString() {
 977  
 978          $result = "<table name=\"" . $this->name . "\"";
 979  
 980          if ($this->phpName !== null) {
 981              $result .= " phpName=\""
 982                    . $this->phpName
 983                    . '"';
 984          }
 985  
 986          if ($this->idMethod !== null) {
 987              $result .= " idMethod=\""
 988                    . $this->idMethod
 989                    . '"';
 990          }
 991  
 992          if ($this->skipSql) {
 993              $result .= " skipSql=\""
 994                    . ($this->skipSql ? "true" : "false")
 995                    . '"';
 996          }
 997  
 998          if ($this->readOnly) {
 999              $result .= " readOnly=\""
1000                    . ($this->readOnly ? "true" : "false")
1001                    . '"';
1002          }
1003  
1004          if ($this->isTree) {
1005              $result .= " isTree=\""
1006                    . ($this->isTree ? "true" : "false")
1007                    . '"';
1008          }
1009  
1010          if ($this->forReferenceOnly) {
1011              $result .= " forReferenceOnly=\""
1012                    . ($this->forReferenceOnly ? "true" : "false")
1013                    . '"';
1014          }
1015  
1016          if ($this->abstractValue) {
1017              $result .= " abstract=\""
1018                    . ($this->abstractValue ? "true" : "false")
1019                    . '"';
1020          }
1021  
1022          if ($this->enterface !== null) {
1023              $result .= " interface=\""
1024                    . $this->enterface
1025                    . '"';
1026          }
1027  
1028          if ($this->description !== null) {
1029              $result .= " description=\""
1030                    . $this->description
1031                    . '"';
1032          }
1033  
1034          if ($this->baseClass !== null) {
1035              $result .= " baseClass=\""
1036                    . $this->baseClass
1037                    . '"';
1038          }
1039  
1040          if ($this->basePeer !== null) {
1041              $result .= " basePeer=\""
1042                    . $this->basePeer
1043                    . '"';
1044          }
1045  
1046          $result .= ">\n";
1047  
1048          if ($this->columnList !== null) {
1049              for($i=0,$_i=count($this->columnList); $i < $_i; $i++) {
1050                  $result .= $this->columnList[$i]->toString();
1051              }
1052          }
1053  
1054          if ($this->validatorList !== null) {
1055              for($i=0,$_i=count($this->validatorList); $i < $_i; $i++) {
1056                  $result .= $this->validatorList[$i]->toString();
1057              }
1058          }
1059  
1060          if ($this->foreignKeys !== null) {
1061              for($i=0,$_i=count($this->foreignKeys); $i < $_i; $i++) {
1062                  $result .= $this->foreignKeys[$i]->toString();
1063              }
1064          }
1065  
1066          if ($this->idMethodParameters !== null) {
1067              for($i=0,$_i=count($this->idMethodParameters); $i < $_i; $i++) {
1068                  $result .= $this->idMethodParameters[$i]->toString();
1069              }
1070          }
1071  
1072          $result .= "</table>\n";
1073  
1074          return $result;
1075      }
1076  
1077      /**
1078       * Returns the collection of Columns which make up the single primary
1079       * key for this table.
1080       *
1081       * @return array A list of the primary key parts.
1082       */
1083      public function getPrimaryKey()
1084      {
1085          $pk = array();
1086          for($i=0,$_i=count($this->columnList); $i < $_i; $i++) {
1087              $col = $this->columnList[$i];
1088              if ($col->isPrimaryKey()) {
1089                  $pk[] = $col;
1090              }
1091          }
1092          return $pk;
1093      }
1094  
1095      /**
1096       * Determine whether this table has a primary key.
1097       *
1098       * @return boolean Whether this table has any primary key parts.
1099       */
1100      public function hasPrimaryKey()
1101      {
1102          return (count($this->getPrimaryKey()) > 0);
1103      }
1104  
1105      /**
1106       * Determine whether this table has any auto-increment primary key(s).
1107       *
1108       * @return boolean Whether this table has a non-"none" id method and has a primary key column that is auto-increment.
1109       */
1110  	public function hasAutoIncrementPrimaryKey()
1111      {
1112          if ($this->getIdMethod() != IDMethod::NO_ID_METHOD) {
1113              $pks =$this->getPrimaryKey();
1114              foreach ($pks as $pk) {
1115                  if ($pk->isAutoIncrement()) {
1116                      return true;
1117                  }
1118              }
1119          }
1120          return false;
1121      }
1122  
1123      /**
1124       * Returns all parts of the primary key, separated by commas.
1125       *
1126       * @return A CSV list of primary key parts.
1127       * @deprecated Use the DDLBuilder->getColumnList() with the #getPrimaryKey() method.
1128       */
1129      public function printPrimaryKey()
1130      {
1131          return $this->printList($this->columnList);
1132      }
1133  
1134      /**
1135       * Returns the elements of the list, separated by commas.
1136       * @param array $list
1137       * @return A CSV list.
1138       * @deprecated Use the DDLBuilder->getColumnList() with the #getPrimaryKey() method.
1139       */
1140      private function printList($list){
1141          $result = "";
1142          $comma = 0;
1143          for($i=0,$_i=count($list); $i < $_i; $i++) {
1144              $col = $list[$i];
1145              if ($col->isPrimaryKey()) {
1146                  $result .= ($comma++ ? ',' : '') . $this->getDatabase()->getPlatform()->quoteIdentifier($col->getName());
1147              }
1148          }
1149          return $result;
1150      }
1151  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7