DatabaseInfo.php

Go to the documentation of this file.
00001 <?php 00002 00003 /* 00004 * $Id: DatabaseInfo.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 DatabaseInfo { 00031 00032 protected $tables = array(); 00033 00034 protected $sequences = array(); 00035 00037 protected $tablesLoaded = false; 00038 00040 protected $seqsLoaded = false; 00041 00046 protected $conn; 00047 00049 protected $dbname; 00050 00055 protected $dblink; 00056 00060 public function __construct(Connection $conn) 00061 { 00062 $this->conn = $conn; 00063 $this->dblink = $conn->getResource(); 00064 $dsn = $conn->getDSN(); 00065 $this->dbname = $dsn['database']; 00066 } 00067 00072 public function getName() 00073 { 00074 return $this->dbname; 00075 } 00076 00083 function __sleep() 00084 { 00085 return array('tables','conn'); 00086 } 00087 00092 function __wakeup() 00093 { 00094 // Re-init vars from serialized connection 00095 $this->dbname = $conn->database; 00096 $this->dblink = $conn->connection; 00097 00098 // restore chaining 00099 foreach($this->tables as $tbl) { 00100 $tbl->database = $this; 00101 $tbl->dbname = $this->dbname; 00102 $tbl->dblink = $this->dblink; 00103 $tbl->schema = $this->schema; 00104 } 00105 } 00106 00111 public function getConnection() 00112 { 00113 return $this->conn; 00114 } 00115 00122 public function getTable($name) 00123 { 00124 if(!$this->tablesLoaded) $this->initTables(); 00125 if (!isset($this->tables[strtoupper($name)])) { 00126 throw new SQLException("Database `".$this->name."` has no table `".$name."`"); 00127 } 00128 return $this->tables[ strtoupper($name) ]; 00129 } 00130 00135 public function getTables() 00136 { 00137 if(!$this->tablesLoaded) $this->initTables(); 00138 return array_values($this->tables); //re-key [numerically] 00139 } 00140 00146 public function addTable(TableInfo $table) 00147 { 00148 $this->tables[strtoupper($table->getName())] = $table; 00149 } 00150 00155 abstract protected function initTables(); 00156 00157 // FIXME 00158 // Figure out sequences. What are they exactly? Simply columns? 00159 // Should this logic really be at the db level (yes & no, i think). Maybe 00160 // also a Column::isSequence() method ? PosgreSQL supports sequences obviously, 00161 // but currently this part of dbinfo classes is not being used. 00162 00167 abstract protected function initSequences(); 00168 00173 public function isSequence($key) 00174 { 00175 if(!$this->seqsLoaded) $this->initSequences(); 00176 return isset($this->sequences[ strtoupper($key) ]); 00177 } 00178 00183 public function getSequences() 00184 { 00185 if(!$this->seqsLoaded) $this->initSequences(); 00186 return array_values($this->sequences); //re-key [numerically] 00187 } 00188 00189 } 00190

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS