DatabaseInfo.php

Go to the documentation of this file.
00001 <?php 00002 00003 /* 00004 * $Id: DatabaseInfo.php,v 1.4 2004/04/27 19:05:15 micha 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 00031 class DatabaseInfo 00032 { 00033 var $tables = array(); 00034 00035 var $sequences = array(); 00036 00038 var $tablesLoaded = false; 00039 00041 var $seqsLoaded = false; 00042 00047 var $conn; 00048 00050 var $dbname; 00051 00056 var $dblink; 00057 00061 function DatabaseInfo(/*Connection*/ &$conn) 00062 { 00063 if (! is_a($conn, 'Connection')) { 00064 trigger_error( 00065 "DatabaseInfo::DatabaseInfo(): parameter 1 not of type 'Connection' !", 00066 E_USER_ERROR 00067 ); 00068 } 00069 00070 $this->conn =& $conn; 00071 $this->dblink =& $conn->getResource(); 00072 $dsn = $conn->getDSN(); 00073 $this->dbname = $dsn['database']; 00074 } 00075 00080 function getName() 00081 { 00082 return $this->dbname; 00083 } 00084 00091 function __sleep() 00092 { 00093 return array('tables','conn'); 00094 } 00095 00100 function __wakeup() 00101 { 00102 // Re-init vars from serialized connection 00103 $this->dbname =& $conn->database; 00104 $this->dblink =& $conn->connection; 00105 00106 // restore chaining 00107 for($i=0, $j=count($this->tables); $i < $j; $i++) { 00108 $tbl =& $this->tables[$i]; 00109 $tbl->database =& $this; 00110 $tbl->dbname = $this->dbname; 00111 $tbl->dblink = $this->dblink; 00112 $tbl->schema = $this->schema; 00113 } 00114 } 00115 00120 function & getConnection() 00121 { 00122 return $this->conn; 00123 } 00124 00130 function & getTable($name) 00131 { 00132 if(!$this->tablesLoaded) { 00133 if (($e = $this->initTables()) !== true) { 00134 return $e; 00135 } 00136 } 00137 00138 if (!isset($this->tables[strtoupper($name)])) { 00139 return new SQLException(CREOLE_ERROR_NOSUCHTABLE, "Database `".$this->name."` has no table `".$name."`"); 00140 } 00141 00142 return $this->tables[ strtoupper($name) ]; 00143 } 00144 00149 function & getTables() 00150 { 00151 if(!$this->tablesLoaded) { 00152 if (($e = $this->initTables()) !== true) { 00153 return $e; 00154 } 00155 } 00156 00157 return array_values($this->tables); //re-key [numerically] 00158 } 00159 00165 function addTable(/*TableInfo*/ &$table) 00166 { 00167 if (! is_a($table, 'TableInfo')) { 00168 trigger_error( 00169 "DatabaseInfo::addTable(): parameter 1 not of type 'TableInfo' !", 00170 E_USER_ERROR 00171 ); 00172 } 00173 $this->tables[strtoupper($table->getName())] =& $table; 00174 } 00175 00180 function initTables() 00181 { 00182 trigger_error ( 00183 "DatabaseInfo::initTables(): abstract function has to be reimplemented !", 00184 E_USER_ERROR 00185 ); 00186 } 00187 00188 // FIXME 00189 // Figure out sequences. What are they exactly? Simply columns? 00190 // Should this logic really be at the db level (yes & no, i think). Maybe 00191 // also a Column::isSequence() method ? PosgreSQL supports sequences obviously, 00192 // but currently this part of dbinfo classes is not being used. 00193 00198 function initSequences() 00199 { 00200 trigger_error ( 00201 "DatabaseInfo::initSequences(): abstract function has to be reimplemented !", 00202 E_USER_ERROR 00203 ); 00204 } 00205 00210 function isSequence($key) 00211 { 00212 if(!$this->seqsLoaded) { 00213 if (($e = $this->initSequences()) !== true) { 00214 return $e; 00215 } 00216 } 00217 00218 return isset($this->sequences[ strtoupper($key) ]); 00219 } 00220 00225 function getSequences() 00226 { 00227 if(!$this->seqsLoaded) { 00228 if (($e = $this->initSequences()) !== true) { 00229 return $e; 00230 } 00231 } 00232 00233 return array_values($this->sequences); //re-key [numerically] 00234 } 00235 00236 } 00237

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS