OCI8TableInfo.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: OCI8TableInfo.php,v 1.8 2004/03/20 04:16:50 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00022 require_once 'creole/metadata/TableInfo.php'; 00023 00032 class OCI8TableInfo extends TableInfo { 00033 00034 private $schema; 00035 00036 public function __construct(OCI8DatabaseInfo $database, $name) 00037 { 00038 $this->schema = $database->getSchema(); 00039 parent::__construct($database, $name); 00040 } 00041 00043 protected function initColumns() 00044 { 00045 00046 include_once 'creole/metadata/ColumnInfo.php'; 00047 include_once 'creole/drivers/oracle/OCI8Types.php'; 00048 00049 00050 // To get all of the attributes we need, we'll actually do 00051 // two separate queries. The first gets names and default values 00052 // the second will fill in some more details. 00053 00054 $sql = "SELECT column_name, 00055 data_type, 00056 data_precision, 00057 data_length, 00058 data_default, 00059 nullable 00060 FROM all_tab_columns 00061 WHERE table_name = upper('{$this->name}') 00062 AND OWNER = '{$this->schema}'"; 00063 00064 $statement = @OCIParse($this->dblink,$sql); 00065 $success = @OCIExecute($statement,OCI_DEFAULT); 00066 if (!$success) { 00067 throw new SQLException("Could Not Get Columns"); 00068 } 00069 00070 $row = array(); 00071 while (OCIFetchInto($statement, $row, OCI_ASSOC)) { 00072 $row = array_change_key_case($row,CASE_LOWER); 00073 // FIXME -- add support for precision/scale from query above (replace 'null' param w/ precision value). 00074 $this->columns[$row['column_name']] = new ColumnInfo($this, $row['column_name'], OCI8Types::getType($row['data_type']), $row['data_type'], $row['data_length'], null, $row['nullable'], $row['data_default']); 00075 } 00076 00077 $this->colsLoaded = true; 00078 } 00079 00081 protected function initPrimaryKey() 00082 { 00083 include_once 'creole/metadata/PrimaryKeyInfo.php'; 00084 00085 // columns have to be loaded first 00086 if (!$this->colsLoaded) $this->initColumns(); 00087 00088 00089 // Primary Keys Query 00090 $sql = "SELECT a.owner, a.table_name, 00091 a.constraint_name, a.column_name 00092 FROM all_cons_columns a, all_constraints b 00093 WHERE b.constraint_type = 'P' 00094 AND a.constraint_name = b.constraint_name 00095 AND b.table_name = '{$this->name}' 00096 AND b.owner = '{$this->schema}' 00097 "; 00098 00099 00100 $statement = @OCIParse($this->dblink,$sql); 00101 $success = @OCIExecute($statement,OCI_DEFAULT); 00102 if (!$success) { 00103 throw new SQLException("Could Not Get Primary Keys"); 00104 } 00105 00106 $row = array(); 00107 while (OCIFetchInto($statement, $row, OCI_ASSOC)) { 00108 $row = array_change_key_case($row,CASE_LOWER); 00109 00110 $name = $row['column_name']; 00111 00112 if (!isset($this->primaryKey)) { 00113 $this->primaryKey = new PrimaryKeyInfo($name); 00114 } 00115 00116 $this->primaryKey->addColumn($this->columns[$name]); 00117 } 00118 00119 $this->pkLoaded = true; 00120 } 00121 00123 protected function initIndexes() { 00124 00125 include_once 'creole/metadata/IndexInfo.php'; 00126 00127 // columns have to be loaded first 00128 if (!$this->colsLoaded) $this->initColumns(); 00129 00130 // Indexes 00131 $sql = "SELECT 00132 index_name, 00133 table_name, 00134 index_type, 00135 uniqueness 00136 FROM all_indexes 00137 WHERE table_owner = '{$this->schema}' 00138 AND table_name = '{$this->name}' 00139 AND index_name NOT IN (SELECT 00140 constraint_name 00141 FROM all_constraints 00142 WHERE constraint_type = 'P')"; 00143 00144 $statement = @OCIParse($this->dblink,$sql); 00145 $success = @OCIExecute($statement,OCI_DEFAULT); 00146 if (!$success) { 00147 throw new SQLException("Could Not Get Primary Keys"); 00148 } 00149 00150 00151 // Loop through the returned results, grouping the same key_name together 00152 // adding each column for that key. 00153 00154 $row = array(); 00155 while (OCIFetchInto($statement, $row, OCI_ASSOC)) { 00156 $row = array_change_key_case($row,CASE_LOWER); 00157 00158 $name = $row['index_name']; 00159 00160 if (!isset($this->indexes[$name])) { 00161 $this->indexes[$name] = new IndexInfo($name); 00162 } 00163 00164 $this->indexes[$name]->addColumn($this->columns[ $name ]); 00165 } 00166 00167 00168 $this->indexesLoaded = true; 00169 } 00170 00172 protected function initForeignKeys() { 00173 00174 include_once 'creole/metadata/ForeignKeyInfo.php'; 00175 00176 // columns have to be loaded first 00177 if (!$this->colsLoaded) $this->initColumns(); 00178 00179 // Foreign keys 00180 $sql = "SELECT a.owner AS local_owner, 00181 a.table_name AS local_table, 00182 c.column_name AS local_column, 00183 a.constraint_name AS foreign_key_name, 00184 b.owner AS foreign_owner, 00185 b.table_name AS foreign_table, 00186 d.column_name AS foreign_column, 00187 b.constraint_name AS foreign_constraint_name 00188 FROM all_constraints a, all_constraints b, 00189 all_cons_columns c, all_cons_columns d 00190 WHERE a.r_constraint_name = b.constraint_name 00191 AND c.constraint_name = a.constraint_name 00192 AND d.constraint_name = b.constraint_name 00193 AND a.r_owner = b.owner 00194 AND a.constraint_type='R' 00195 AND a.table_name = '{$this->name}' 00196 AND a.owner = '{$this->schema}' "; 00197 00198 00199 $statement = @OCIParse($this->dblink,$sql); 00200 $success = @OCIExecute($statement,OCI_DEFAULT); 00201 if (!$success) { 00202 throw new SQLException("Could Not Get Primary Keys"); 00203 } 00204 00205 00206 00207 // Loop through the returned results, grouping the same key_name together 00208 // adding each column for that key. 00209 00210 $row = array(); 00211 while (OCIFetchInto($statement, $row, OCI_ASSOC)) { 00212 $row = array_change_key_case($row,CASE_LOWER); 00213 00214 $name = $row['foreign_key_name']; 00215 00216 $foreignTable = $this->database->getTable($row['foreign_table']); 00217 $foreignColumn = $foreignTable->getColumn($row['foreign_column']); 00218 00219 00220 $localTable = $this->database->getTable($row['local_table']); 00221 $localColumn = $localTable->getColumn($row['local_column']); 00222 00223 00224 if (!isset($this->foreignKeys[$name])) { 00225 $this->foreignKeys[$name] = new ForeignKeyInfo($name); 00226 } 00227 $this->foreignKeys[$name]->addReference($localColumn,$foreignColumn); 00228 } 00229 00230 $this->fksLoaded = true; 00231 } 00232 00233 }

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS