MSSQLTableInfo.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MSSQLTableInfo.php,v 1.1 2004/05/02 21:45:52 micha 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 00033 class MSSQLTableInfo extends TableInfo 00034 { 00041 function initColumns() 00042 { 00043 include_once 'creole/metadata/ColumnInfo.php'; 00044 include_once 'creole/drivers/mssql/MSSQLTypes.php'; 00045 00046 if (!@mssql_select_db($this->dbname, $this->dblink)) { 00047 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00048 } 00049 00050 $res = mssql_query("sp_columns ".$this->name, $this->dblink); 00051 if (!$res) { 00052 return new SQLException(CREOLE_ERROR, 'Could not get column names', mssql_get_last_message()); 00053 } 00054 00055 while ($row = mssql_fetch_array($res)) { 00056 $name = $row['COLUMN_NAME']; 00057 $type = $row['TYPE_NAME']; 00058 $length = $row['LENGTH']; 00059 $is_nullable = $row['NULLABLE']; 00060 $default = $row['COLUMN_DEF']; 00061 $precision = $row['PRECISION']; 00062 $this->columns[$name] =& new ColumnInfo($this, $name, MSSQLTypes::getType($type), $type, $length, $precision, $is_nullable, $default); 00063 } 00064 00065 $this->colsLoaded = true; 00066 return true; 00067 } 00068 00075 function initIndexes() 00076 { 00077 // columns have to be loaded first 00078 if (!$this->colsLoaded) $this->initColumns(); 00079 include_once 'creole/metadata/IndexInfo.php'; 00080 00081 if (!@mssql_select_db($this->dbname, $this->dblink)) { 00082 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00083 } 00084 00085 $res = mssql_query("sp_indexes_rowset ".$this->name, $this->dblink); 00086 00087 while ($row = mssql_fetch_array($res)) { 00088 $name = $row['INDEX_NAME']; 00089 // All primary keys are indexes (right...?) 00090 if (!isset($this->indexes[$name])) { 00091 $this->indexes[$name] =& new IndexInfo($name); 00092 } 00093 $this->indexes[$name]->addColumn($this->columns[ $row['COLUMN_NAME'] ]); 00094 } 00095 00096 $this->indexesLoaded = true; 00097 return true; 00098 } 00099 00106 function initForeignKeys() 00107 { 00108 // columns have to be loaded first 00109 if (!$this->colsLoaded) { 00110 if (($e = $this->initColumns()) !== true) { 00111 return $e; 00112 } 00113 } 00114 00115 include_once 'creole/metadata/ForeignKeyInfo.php'; 00116 00117 if (!@mssql_select_db($this->dbname, $this->dblink)) { 00118 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00119 } 00120 00121 $res = mssql_query("SELECT ccu1.TABLE_NAME, ccu1.COLUMN_NAME, ccu2.TABLE_NAME AS FK_TABLE_NAME, ccu2.COLUMN_NAME AS FK_COLUMN_NAME 00122 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu1 INNER JOIN 00123 INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc1 ON tc1.CONSTRAINT_NAME = ccu1.CONSTRAINT_NAME AND 00124 CONSTRAINT_TYPE = 'Foreign Key' INNER JOIN 00125 INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON rc1.CONSTRAINT_NAME = tc1.CONSTRAINT_NAME INNER JOIN 00126 INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu2 ON ccu2.CONSTRAINT_NAME = rc1.UNIQUE_CONSTRAINT_NAME 00127 WHERE (ccu1.table_name = '".$this->name."')", $this->dblink); 00128 00129 while($row = mssql_fetch_array($res)) 00130 { 00131 $name = $row['COLUMN_NAME']; 00132 $ftbl = $row['FK_TABLE_NAME']; 00133 $fcol = $row['FK_COLUMN_NAME']; 00134 00135 if (!isset($this->foreignKeys[$name])) 00136 { 00137 $this->foreignKeys[$name] =& new ForeignKeyInfo($name); 00138 00139 /* TODO: when does getTable() return null ? */ 00140 if (($foreignTable =& $this->database->getTable($ftbl)) === null) { 00141 $foreignTable =& new TableInfo($ltbl); 00142 $this->database->addTable($foreignTable); 00143 } 00144 00145 if (Creole::isError($foreignTable)) { return $foreignTable; } 00146 00147 /* TODO: when does getColumn() return null ? */ 00148 if (($foreignCol = $foreignTable->getColumn($name)) === null) { 00149 $foreignCol = new ColumnInfo($foreignTable, $name); 00150 $foreignTable->addColumn($foreignCol); 00151 } 00152 00153 if (Creole::isError($foreignCol)) { return $foreignCol; } 00154 00155 $this->foreignKeys[$name]->addReference($this->columns[$name], $foreignCol); 00156 } 00157 } 00158 00159 $this->fksLoaded = true; 00160 return true; 00161 } 00162 00169 function initPrimaryKey() 00170 { 00171 // columns have to be loaded first 00172 if (!$this->colsLoaded) { 00173 if (($e = $this->initColumns()) !== true) { 00174 return $e; 00175 } 00176 } 00177 00178 include_once 'creole/metadata/PrimaryKeyInfo.php'; 00179 00180 if (!@mssql_select_db($this->dbname, $this->dblink)) { 00181 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected'); 00182 } 00183 00184 $res = mssql_query("SELECT COLUMN_NAME 00185 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS 00186 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ON 00187 INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME = INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.constraint_name 00188 WHERE (INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'PRIMARY KEY') AND 00189 (INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME = '".$this->name."')", $this->dblink); 00190 00191 // Loop through the returned results, grouping the same key_name together. 00192 // name of the primary key will be the first column name in the key. 00193 while($row = mssql_fetch_row($res)) { 00194 $name = $row[0]; 00195 if (!isset($this->primaryKey)) { 00196 $this->primaryKey =& new PrimaryKeyInfo($name); 00197 } 00198 $this->primaryKey->addColumn($this->columns[ $name ]); 00199 } 00200 00201 $this->pkLoaded = true; 00202 return true; 00203 } 00204 00205 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS