00001 <?php 00002 00003 /* 00004 * $Id: TableInfo.php,v 1.16 2005/10/17 19:05:10 dlawson_mi 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 protected $name; 00033 protected $columns = array(); 00034 protected $foreignKeys = array(); 00035 protected $indexes = array(); 00036 protected $primaryKey; 00037 00038 protected $pkLoaded = false; 00039 protected $fksLoaded = false; 00040 protected $indexesLoaded = false; 00041 protected $colsLoaded = false; 00042 protected $vendorLoaded = false; 00043 00048 protected $vendorSpecificInfo = array(); 00049 00054 protected $conn; 00055 00060 protected $database; 00061 00063 protected $dblink; 00064 00066 protected $dbname; 00067 00073 function __construct(DatabaseInfo $database, $name) { 00074 $this->database = $database; 00075 $this->name = $name; 00076 $this->conn = $database->getConnection(); // shortcut because all drivers need this for the queries 00077 $this->dblink = $this->conn->getResource(); 00078 $this->dbname = $database->getName(); 00079 } 00080 00087 function __sleep() 00088 { 00089 return array('name', 'columns', 'foreignKeys', 'indexes', 'primaryKey'); 00090 } 00091 00096 function __wakeup() 00097 { 00098 // restore chaining 00099 foreach($this->columns as $col) { 00100 $col->table = $this; 00101 } 00102 } 00103 00108 abstract protected function initColumns(); 00109 00114 abstract protected function initPrimaryKey(); 00115 00120 abstract protected function initForeignKeys(); 00121 00126 abstract protected function initIndexes(); 00127 00132 //it must be asbtract and be implemented in every vendor specific driver, 00133 //however since it's an experimental stuff it has an empty body in order 00134 //not to break BC 00135 /*abstract*/ protected function initVendorSpecificInfo(){} 00136 00142 public function getPrimaryKey() 00143 { 00144 if(!$this->pkLoaded) $this->initPrimaryKey(); 00145 return $this->primaryKey; 00146 } 00147 00154 public function getColumn($name) 00155 { 00156 if(!$this->colsLoaded) $this->initColumns(); 00157 if (!isset($this->columns[$name])) { 00158 throw new SQLException("Table `".$this->name."` has no column `".$name."`"); 00159 } 00160 return $this->columns[$name]; 00161 } 00162 00168 public function hasColumn($name) 00169 { 00170 if(!$this->colsLoaded) $this->initColumns(); 00171 return isset($this->columns[$name]); 00172 } 00173 00178 public function getColumns() 00179 { 00180 if(!$this->colsLoaded) $this->initColumns(); 00181 return array_values($this->columns); // re-key numerically 00182 } 00183 00190 public function getForeignKey($name) 00191 { 00192 if(!$this->fksLoaded) $this->initForeignKeys(); 00193 if (!isset($this->foreignKeys[$name])) { 00194 throw new SQLException("Table `".$this->name."` has no foreign key `".$name."`"); 00195 } 00196 return $this->foreignKeys[$name]; 00197 } 00198 00203 public function getForeignKeys() 00204 { 00205 if(!$this->fksLoaded) $this->initForeignKeys(); 00206 return array_values($this->foreignKeys); 00207 } 00208 00215 public function getIndex($name) 00216 { 00217 if(!$this->indexesLoaded) $this->initIndexes(); 00218 if (!isset($this->indexes[$name])) { 00219 throw new SQLException("Table `".$this->name."` has no index `".$name."`"); 00220 } 00221 return $this->indexes[$name]; 00222 } 00223 00228 public function getIndexes() 00229 { 00230 if(!$this->indexesLoaded) $this->initIndexes(); 00231 return array_values($this->indexes); 00232 } 00233 00238 public function getIndices() 00239 { 00240 return $this->getIndexes(); 00241 } 00242 00247 public function getName() 00248 { 00249 return $this->name; 00250 } 00251 00255 public function toString() 00256 { 00257 return $this->name; 00258 } 00259 00261 public function foreignKeysLoaded() 00262 { 00263 return $this->fksLoaded; 00264 } 00265 00267 public function primaryKeyLoaded() 00268 { 00269 return $this->pkLoaded; 00270 } 00271 00273 public function columnsLoaded() 00274 { 00275 return $this->colsLoaded; 00276 } 00277 00279 public function indexesLoaded() 00280 { 00281 return $this->indexesLoaded; 00282 } 00283 00288 public function getVendorSpecificInfo() 00289 { 00290 if(!$this->vendorLoaded) $this->initVendorSpecificInfo(); 00291 return $this->vendorSpecificInfo; 00292 } 00293 00295 public function addColumn(ColumnInfo $column) 00296 { 00297 $this->columns[$column->getName()] = $column; 00298 } 00299 00301 public function getDatabase() 00302 { 00303 return $this->database; 00304 } 00305 }