00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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->conn->getResource())) {
00044 throw new SQLException('No database selected');
00045 }
00046
00047 $res = mssql_query("sp_columns ".$this->name, $this->conn->getResource());
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 $scale = $row['SCALE'];
00060 $identity = false;
00061 if (strtolower($type) == "int identity") {
00062 $identity = true;
00063 }
00064 $this->columns[$name] = new ColumnInfo($this, $name, MSSQLTypes::getType($type), $type, $length, $precision, $scale, $is_nullable, $default, $identity);
00065 }
00066
00067 $this->colsLoaded = true;
00068 }
00069
00074 protected function initIndexes()
00075 {
00076
00077 if (!$this->colsLoaded) $this->initColumns();
00078 include_once 'creole/metadata/IndexInfo.php';
00079
00080 if (!@mssql_select_db($this->dbname, $this->conn->getResource())) {
00081 throw new SQLException('No database selected');
00082 }
00083
00084 $res = mssql_query("sp_indexes_rowset ".$this->name, $this->conn->getResource());
00085
00086 while ($row = mssql_fetch_array($res)) {
00087 $name = $row['INDEX_NAME'];
00088
00089 if (!isset($this->indexes[$name])) {
00090 $this->indexes[$name] = new IndexInfo($name);
00091 }
00092 $this->indexes[$name]->addColumn($this->columns[ $row['COLUMN_NAME'] ]);
00093 }
00094
00095 $this->indexesLoaded = true;
00096 }
00097
00102 protected function initForeignKeys()
00103 {
00104
00105 if (!$this->colsLoaded) $this->initColumns();
00106 include_once 'creole/metadata/ForeignKeyInfo.php';
00107
00108 if (!@mssql_select_db($this->dbname, $this->conn->getResource())) {
00109 throw new SQLException('No database selected');
00110 }
00111
00112 $res = mssql_query("SELECT ccu1.TABLE_NAME, ccu1.COLUMN_NAME, ccu2.TABLE_NAME AS FK_TABLE_NAME, ccu2.COLUMN_NAME AS FK_COLUMN_NAME
00113 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu1 INNER JOIN
00114 INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc1 ON tc1.CONSTRAINT_NAME = ccu1.CONSTRAINT_NAME AND
00115 CONSTRAINT_TYPE = 'Foreign Key' INNER JOIN
00116 INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON rc1.CONSTRAINT_NAME = tc1.CONSTRAINT_NAME INNER JOIN
00117 INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu2 ON ccu2.CONSTRAINT_NAME = rc1.UNIQUE_CONSTRAINT_NAME
00118 WHERE (ccu1.table_name = '".$this->name."')", $this->conn->getResource());
00119
00120 while($row = mssql_fetch_array($res)) {
00121 $name = $row['COLUMN_NAME'];
00122 $ftbl = $row['FK_TABLE_NAME'];
00123 $fcol = $row['FK_COLUMN_NAME'];
00124
00125 if (!isset($this->foreignKeys[$name])) {
00126 $this->foreignKeys[$name] = new ForeignKeyInfo($name);
00127
00128 if ($this->database->hasTable($ftbl)) {
00129 $foreignTable = $this->database->getTable($ftbl);
00130 } else {
00131 $foreignTable = new TableInfo($ltbl);
00132 $this->database->addTable($foreignTable);
00133 }
00134
00135 if ($foreignTable->hasColumn($fcol)) {
00136 $foreignCol = $foreignTable->getColumn($fcol);
00137 } else {
00138 $foreignCol = new ColumnInfo($foreignTable, $fcol);
00139 $foreignTable->addColumn($foreignCol);
00140 }
00141
00142 $this->foreignKeys[$name]->addReference($this->columns[$name], $foreignCol);
00143 }
00144 }
00145
00146 $this->fksLoaded = true;
00147 }
00148
00153 protected function initPrimaryKey()
00154 {
00155
00156 if (!$this->colsLoaded) $this->initColumns();
00157 include_once 'creole/metadata/PrimaryKeyInfo.php';
00158
00159 if (!@mssql_select_db($this->dbname, $this->conn->getResource())) {
00160 throw new SQLException('No database selected');
00161 }
00162
00163 $res = mssql_query("SELECT COLUMN_NAME
00164 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
00165 INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ON
00166 INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME = INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE.constraint_name
00167 WHERE (INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'PRIMARY KEY') AND
00168 (INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME = '".$this->name."')", $this->conn->getResource());
00169
00170
00171
00172 while($row = mssql_fetch_row($res)) {
00173 $name = $row[0];
00174 if (!isset($this->primaryKey)) {
00175 $this->primaryKey = new PrimaryKeyInfo($name);
00176 }
00177 $this->primaryKey->addColumn($this->columns[ $name ]);
00178 }
00179
00180 $this->pkLoaded = true;
00181 }
00182
00183 }