TableInfo.php

Go to the documentation of this file.
00001 <?php 00002 00003 /* 00004 * $Id: TableInfo.php,v 1.11 2004/03/20 04:16:50 hlellelid Exp $ 00005 * 00006 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00007 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00008 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00009 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00010 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00011 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00012 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00013 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00014 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00015 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00016 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * This software consists of voluntary contributions made by many individuals 00019 * and is licensed under the LGPL. For more information please see 00020 * <http://creole.phpdb.org>. 00021 */ 00022 00030 abstract class TableInfo { 00031 00032 // FIXME 00033 // - Currently all member attributes are public. This should be fixed 00034 // when PHP's magic __sleep() and __wakeup() functions & serialization support 00035 // handles protected/private members. (if ever) 00036 00037 protected $name; 00038 protected $columns = array(); 00039 protected $foreignKeys = array(); 00040 protected $indexes = array(); 00041 protected $primaryKey; 00042 00043 protected $pkLoaded = false; 00044 protected $fksLoaded = false; 00045 protected $indexesLoaded = false; 00046 protected $colsLoaded = false; 00047 00052 protected $conn; 00053 00058 protected $database; 00059 00061 protected $dblink; 00062 00064 protected $dbname; 00065 00071 function __construct(DatabaseInfo $database, $name) { 00072 $this->database = $database; 00073 $this->name = $name; 00074 $this->conn = $database->getConnection(); // shortcut because all drivers need this for the queries 00075 $this->dblink = $this->conn->getResource(); 00076 $this->dbname = $database->getName(); 00077 } 00078 00085 function __sleep() 00086 { 00087 return array('name', 'columns', 'foreignKeys', 'indexes', 'primaryKey'); 00088 } 00089 00094 function __wakeup() 00095 { 00096 // restore chaining 00097 foreach($this->columns as $col) { 00098 $col->table = $this; 00099 } 00100 } 00101 00106 abstract protected function initColumns(); 00107 00112 abstract protected function initPrimaryKey(); 00113 00118 abstract protected function initForeignKeys(); 00119 00124 abstract protected function initIndexes(); 00125 00126 00132 public function getPrimaryKey() 00133 { 00134 if(!$this->pkLoaded) $this->initPrimaryKey(); 00135 return $this->primaryKey; 00136 } 00137 00144 public function getColumn($name) 00145 { 00146 if(!$this->colsLoaded) $this->initColumns(); 00147 if (!isset($this->columns[$name])) { 00148 throw new SQLException("Table `".$this->name."` has no column `".$name."`"); 00149 } 00150 return $this->columns[$name]; 00151 } 00152 00157 public function getColumns() 00158 { 00159 if(!$this->colsLoaded) $this->initColumns(); 00160 return array_values($this->columns); // re-key numerically 00161 } 00162 00169 public function getForeignKey($name) 00170 { 00171 if(!$this->fksLoaded) $this->initForeignKeys(); 00172 if (!isset($this->foreignKeys[$name])) { 00173 throw new SQLException("Table `".$this->name."` has no foreign key `".$name."`"); 00174 } 00175 return $this->foreignKeys[$name]; 00176 } 00177 00182 public function getForeignKeys() 00183 { 00184 if(!$this->fksLoaded) $this->initForeignKeys(); 00185 return array_values($this->foreignKeys); 00186 } 00187 00194 public function getIndex($name) 00195 { 00196 if(!$this->indexesLoaded) $this->initIndexes(); 00197 if (!isset($this->indexes[$name])) { 00198 throw new SQLException("Table `".$this->name."` has no index `".$name."`"); 00199 } 00200 return @$this->indexes[$name]; 00201 } 00202 00207 public function getIndexes() 00208 { 00209 if(!$this->indexesLoaded) $this->initIndexes(); 00210 return array_values($this->indexes); 00211 } 00212 00217 public function getIndices() 00218 { 00219 return $this->getIndexes(); 00220 } 00221 00226 public function getName() 00227 { 00228 return $this->name; 00229 } 00230 00234 public function toString() 00235 { 00236 return $this->name; 00237 } 00238 00240 public function foreignKeysLoaded() 00241 { 00242 return $this->fksLoaded; 00243 } 00244 00246 public function primaryKeyLoaded() 00247 { 00248 return $this->pkLoaded; 00249 } 00250 00252 public function columnsLoaded() 00253 { 00254 return $this->colsLoaded; 00255 } 00256 00258 public function indexesLoaded() 00259 { 00260 return $this->indexesLoaded; 00261 } 00262 00264 public function addColumn(ColumnInfo $column) 00265 { 00266 $this->columns[$column->getName()] = $column; 00267 } 00268 00270 public function getDatabase() 00271 { 00272 return $this->database; 00273 } 00274 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS