[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/vendor/creole/metadata/ -> TableInfo.php (source)

   1  <?php
   2  
   3  /*
   4   *  $Id: TableInfo.php,v 1.16 2005/10/17 19:05:10 dlawson_mi Exp $
   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://creole.phpdb.org>.
  21   */
  22  
  23  /**
  24   * Represents a table.
  25   *
  26   * @author    Hans Lellelid <hans@xmpl.org>
  27   * @version   $Revision: 1.16 $
  28   * @package   creole.metadata
  29   */
  30  abstract class TableInfo {
  31  
  32      protected $name;
  33      protected $columns = array();
  34      protected $foreignKeys = array();
  35      protected $indexes = array();
  36      protected $primaryKey;
  37  
  38      protected $pkLoaded = false;
  39      protected $fksLoaded = false;
  40      protected $indexesLoaded = false;
  41      protected $colsLoaded = false;
  42      protected $vendorLoaded = false;
  43  
  44      /**
  45       * Additional and optional vendor specific information.
  46       * @var vendorSpecificInfo
  47       */
  48      protected $vendorSpecificInfo = array();
  49  
  50      /**
  51       * Database Connection.
  52       * @var Connection
  53       */
  54      protected $conn;
  55  
  56      /**
  57       * The parent DatabaseInfo object.
  58       * @var DatabaseInfo
  59       */
  60      protected $database;
  61  
  62      /** Shortcut to db resource link id (needed by drivers for queries). */
  63      protected $dblink;
  64  
  65      /** Shortcut to db name (needed by many drivers for queries). */
  66      protected $dbname;
  67  
  68      /**
  69       * @param string $table The table name.
  70       * @param string $database The database name.
  71       * @param resource $dblink The db connection resource.
  72       */
  73      function __construct(DatabaseInfo $database, $name) {
  74          $this->database = $database;
  75          $this->name = $name;
  76          $this->conn = $database->getConnection(); // shortcut because all drivers need this for the queries
  77          $this->dblink = $this->conn->getResource();
  78          $this->dbname = $database->getName();
  79      }
  80  
  81      /**
  82       * This "magic" method is invoked upon serialize().
  83       * Because the Info class hierarchy is recursive, we must handle
  84       * the serialization and unserialization of this object.
  85       * @return array The class variables that should be serialized (all must be public!).
  86       */
  87      function __sleep()
  88      {
  89          return array('name', 'columns', 'foreignKeys', 'indexes', 'primaryKey');
  90      }
  91  
  92      /**
  93       * This "magic" method is invoked upon unserialize().
  94       * This method re-hydrates the object and restores the recursive hierarchy.
  95       */
  96      function __wakeup()
  97      {
  98          // restore chaining
  99          foreach($this->columns as $col) {
 100              $col->table = $this;
 101          }
 102      }
 103  
 104      /**
 105       * Loads the columns.
 106       * @return void
 107       */
 108      abstract protected function initColumns();
 109  
 110      /**
 111       * Loads the primary key information for this table.
 112       * @return void
 113       */
 114      abstract protected function initPrimaryKey();
 115  
 116      /**
 117       * Loads the foreign keys for this table.
 118       * @return void
 119       */
 120      abstract protected function initForeignKeys();
 121  
 122      /**
 123       * Loads the indexes information for this table.
 124       * @return void
 125       */
 126      abstract protected function initIndexes();
 127  
 128      /**
 129       * Loads the vendor specific information for this table.
 130       * @return void
 131       */
 132      //it must be asbtract and be implemented in every vendor specific driver,
 133      //however since it's an experimental stuff it has an empty body in order
 134      //not to break BC
 135      /*abstract*/ protected function initVendorSpecificInfo(){}
 136  
 137      /**
 138       * Get parimary key in this table.
 139       * @throws Exception - if foreign keys are unsupported by DB.
 140       * @return array ForeignKeyInfo[]
 141       */
 142      public function getPrimaryKey()
 143      {
 144          if(!$this->pkLoaded) $this->initPrimaryKey();
 145          return $this->primaryKey;
 146      }
 147  
 148      /**
 149       * Get the ColumnInfo object for specified column.
 150       * @param string $name The column name.
 151       * @return ColumnInfo
 152       * @throws SQLException - if column does not exist for this table.
 153       */
 154      public function getColumn($name)
 155      {
 156          if(!$this->colsLoaded) $this->initColumns();
 157          if (!isset($this->columns[$name])) {
 158              throw new SQLException("Table `".$this->name."` has no column `".$name."`");
 159          }
 160          return $this->columns[$name];
 161      }
 162  
 163      /**
 164       * Return whether table contains specified column.
 165       * @param string $name The column name.
 166       * @return boolean
 167       */
 168      public function hasColumn($name)
 169      {
 170          if(!$this->colsLoaded) $this->initColumns();
 171          return isset($this->columns[$name]);
 172      }
 173  
 174      /**
 175       * Get array of columns for this table.
 176       * @return array ColumnInfo[]
 177       */
 178      public function getColumns()
 179      {
 180          if(!$this->colsLoaded) $this->initColumns();
 181          return array_values($this->columns); // re-key numerically
 182      }
 183  
 184      /**
 185       * Get specified fk for this table.
 186       * @param string $name The foreign key name to retrieve.
 187       * @return ForeignKeyInfo
 188       * @throws SQLException - if fkey does not exist for this table.
 189       */
 190      public function getForeignKey($name)
 191      {
 192          if(!$this->fksLoaded) $this->initForeignKeys();
 193          if (!isset($this->foreignKeys[$name])) {
 194              throw new SQLException("Table `".$this->name."` has no foreign key `".$name."`");
 195          }
 196          return $this->foreignKeys[$name];
 197      }
 198  
 199      /**
 200       * Get all foreign keys.
 201       * @return array ForeignKeyInfo[]
 202       */
 203      public function getForeignKeys()
 204      {
 205          if(!$this->fksLoaded) $this->initForeignKeys();
 206          return array_values($this->foreignKeys);
 207      }
 208  
 209      /**
 210       * Gets the IndexInfo object for a specified index.
 211       * @param string $name The index name to retrieve.
 212       * @return IndexInfo
 213       * @throws SQLException - if index does not exist for this table.
 214       */
 215      public function getIndex($name)
 216      {
 217          if(!$this->indexesLoaded) $this->initIndexes();
 218          if (!isset($this->indexes[$name])) {
 219              throw new SQLException("Table `".$this->name."` has no index `".$name."`");
 220          }
 221          return $this->indexes[$name];
 222      }
 223  
 224      /**
 225       * Get array of IndexInfo objects for this table.
 226       * @return array IndexInfo[]
 227       */
 228      public function getIndexes()
 229      {
 230          if(!$this->indexesLoaded) $this->initIndexes();
 231          return array_values($this->indexes);
 232      }
 233  
 234      /**
 235       * Alias for getIndexes() method.
 236       * @return array
 237       */
 238      public function getIndices()
 239      {
 240          return $this->getIndexes();
 241      }
 242  
 243      /**
 244       * Get table name.
 245       * @return string
 246       */
 247      public function getName()
 248      {
 249          return $this->name;
 250      }
 251  
 252      /**
 253       * @return string
 254       */
 255      public function toString()
 256      {
 257          return $this->name;
 258      }
 259  
 260      /** Have foreign keys been loaded? */
 261      public function foreignKeysLoaded()
 262      {
 263          return $this->fksLoaded;
 264      }
 265  
 266      /** Has primary key info been loaded? */
 267      public function primaryKeyLoaded()
 268      {
 269          return $this->pkLoaded;
 270      }
 271  
 272      /** Have columns been loaded? */
 273      public function columnsLoaded()
 274      {
 275          return $this->colsLoaded;
 276      }
 277  
 278      /** Has index information been loaded? */
 279      public function indexesLoaded()
 280      {
 281          return $this->indexesLoaded;
 282      }
 283  
 284      /**
 285       * Get vendor specific optional information for this table.
 286       * @return array vendorSpecificInfo[]
 287       */
 288      public function getVendorSpecificInfo()
 289      {
 290          if(!$this->vendorLoaded) $this->initVendorSpecificInfo();
 291          return $this->vendorSpecificInfo;
 292      }
 293  
 294      /** Adds a column to this table. */
 295      public function addColumn(ColumnInfo $column)
 296      {
 297          $this->columns[$column->getName()] = $column;
 298      }
 299  
 300      /** Get the parent DatabaseInfo object. */
 301      public function getDatabase()
 302      {
 303          return $this->database;
 304      }
 305  }


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