MSSQLTableInfo.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MSSQLTableInfo.php,v 1.11 2004/03/05 15:46:11 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/CreoleTypes.php'; 00023 require_once 'creole/metadata/TableInfo.php'; 00024 00032 class MSSQLTableInfo extends TableInfo { 00033 00038 protected function initColumns() 00039 { 00040 include_once 'creole/metadata/ColumnInfo.php'; 00041 include_once 'creole/drivers/mssql/MSSQLTypes.php'; 00042 00043 if (!@mssql_select_db($this->dbname, $this->dblink)) { 00044 throw new SQLException('No database selected'); 00045 } 00046 00047 $res = mssql_query("sp_columns ".$this->name, $this->dblink); 00048 if (!$res) { 00049 throw new SQLException('Could not get column names', mssql_get_last_message()); 00050 } 00051 00052 while ($row = mssql_fetch_array($res)) { 00053 $name = $row['COLUMN_NAME']; 00054 $type = $row['TYPE_NAME']; 00055 $length = $row['LENGTH']; 00056 $is_nullable = $row['NULLABLE']; 00057 $default = $row['COLUMN_DEF']; 00058 $precision = $row['PRECISION']; 00059 $this->columns[$name] = new ColumnInfo($this, $name, MSSQLTypes::getType($type), $type, $length, $precision, $is_nullable, $default); 00060 } 00061 00062 $this->colsLoaded = true; 00063 } 00064 00069 protected function initIndexes() 00070 { 00071 // columns have to be loaded first 00072 if (!$this->colsLoaded) $this->initColumns(); 00073 include_once 'creole/metadata/IndexInfo.php'; 00074 00075 if (!@mssql_select_db($this->dbname, $this->dblink)) { 00076 throw new SQLException('No database selected'); 00077 } 00078 00079 $res = mssql_query("sp_indexes_rowset ".$this->name, $this->dblink); 00080 00081 while ($row = mssql_fetch_array($res)) { 00082 $name = $row['INDEX_NAME']; 00083 // All primary keys are indexes (right...?) 00084 if (!isset($this->indexes[$name])) { 00085 $this->indexes[$name] = new IndexInfo($name); 00086 } 00087 $this->indexes[$name]->addColumn($this->columns[ $row['COLUMN_NAME'] ]); 00088 } 00089 00090 $this->indexesLoaded = true; 00091 } 00092 00097 protected function initForeignKeys() 00098 { 00099 // columns have to be loaded first 00100 if (!$this->colsLoaded) $this->initColumns(); 00101 include_once 'creole/metadata/ForeignKeyInfo.php'; 00102 00103 if (!@mssql_select_db($this->dbname, $this->dblink)) { 00104 throw new SQLException('No database selected'); 00105 } 00106 00107 $res = mssql_query("SELECT ccu1.TABLE_NAME, ccu1.COLUMN_NAME, ccu2.TABLE_NAME AS FK_TABLE_NAME, ccu2.COLUMN_NAME AS FK_COLUMN_NAME 00108 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu1 INNER JOIN 00109 INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc1 ON tc1.CONSTRAINT_NAME = ccu1.CONSTRAINT_NAME AND 00110 CONSTRAINT_TYPE = 'Foreign Key' INNER JOIN 00111 INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON rc1.CONSTRAINT_NAME = tc1.CONSTRAINT_NAME INNER JOIN 00112 INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu2 ON ccu2.CONSTRAINT_NAME = rc1.UNIQUE_CONSTRAINT_NAME 00113 WHERE (ccu1.table_name = '".$this->name."')", $this->dblink); 00114 00115 while($row = mssql_fetch_array($res)) { 00116 $name = $row['COLUMN_NAME']; 00117 $ftbl = $row['FK_TABLE_NAME']; 00118 $fcol = $row['FK_COLUMN_NAME']; 00119 00120 if (!isset($this->foreignKeys[$name])) { 00121 $this->foreignKeys[$name] = new ForeignKeyInfo($name); 00122 00123 if (($foreignTable = $this->database->getTable($ftbl)) === null) { 00124 $foreignTable = new TableInfo($ltbl); 00125 $this->database->addTable($foreignTable); 00126 } 00127 00128 if (($foreignCol = $foreignTable->getColumn($name)) === null) { 00129 $foreignCol = new ColumnInfo($foreignTable, $name); 00130 $foreignTable->addColumn($foreignCol); 00131 } 00132 00133 $this->foreignKeys[$name]->addReference($this->columns[$name], $foreignCol); 00134 } 00135 } 00136 00137 $this->fksLoaded = true; 00138 } 00139 00144 protected function initPrimaryKey() 00145 { 00146 // columns have to be loaded first 00147 if (!$this->colsLoaded) $this->initColumns(); 00148 include_once 'creole/metadata/PrimaryKeyInfo.php'; 00149 00150 if (!@mssql_select_db($this->dbname, $this->dblink)) { 00151 throw new SQLException('No database selected'); 00152 } 00153 00154 $res = mssql_query("SELECT COLUMN_NAME 00155 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 00156 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ON 00157 INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME = INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.constraint_name 00158 WHERE (INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'PRIMARY KEY') AND 00159 (INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME = '".$this->name."')", $this->dblink); 00160 00161 // Loop through the returned results, grouping the same key_name together. 00162 // name of the primary key will be the first column name in the key. 00163 while($row = mssql_fetch_row($res)) { 00164 $name = $row[0]; 00165 if (!isset($this->primaryKey)) { 00166 $this->primaryKey = new PrimaryKeyInfo($name); 00167 } 00168 $this->primaryKey->addColumn($this->columns[ $name ]); 00169 } 00170 00171 $this->pkLoaded = true; 00172 } 00173 00174 }

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS