public function describeTable($tableName, $schemaName = null)
{
if ($schemaName != null) {
if (strpos($schemaName, '.') !== false) {
$result = explode('.', $schemaName);
$schemaName = $result[1];
}
}
/**
* Discover metadata information about this table.
*/
$sql = "exec sp_columns @table_name = " . $this->quoteIdentifier($tableName, true);
if ($schemaName != null) {
$sql .= ", @table_owner = " . $this->quoteIdentifier($schemaName, true);
}
$stmt = $this->query($sql);
$result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
$table_name = 2;
$column_name = 3;
$type_name = 5;
$precision = 6;
$length = 7;
$scale = 8;
$nullable = 10;
$column_def = 12;
$column_position = 16;
/**
* Discover primary key column(s) for this table.
*/
$sql = "exec sp_pkeys @table_name = " . $this->quoteIdentifier($tableName, true);
if ($schemaName != null) {
$sql .= ", @table_owner = " . $this->quoteIdentifier($schemaName, true);
}
$stmt = $this->query($sql);
$primaryKeysResult = $stmt->fetchAll(Zend_Db::FETCH_NUM);
$primaryKeyColumn = array();
$pkey_column_name = 3;
$pkey_key_seq = 4;
foreach ($primaryKeysResult as $pkeysRow) {
$primaryKeyColumn[$pkeysRow[$pkey_column_name]] = $pkeysRow[$pkey_key_seq];
}
$desc = array();
$p = 1;
foreach ($result as $key => $row) {
$identity = false;
$words = explode(' ', $row[$type_name], 2);
if (isset($words[0])) {
$type = $words[0];
if (isset($words[1])) {
$identity = (bool) preg_match('/identity/', $words[1]);
}
}
$isPrimary = array_key_exists($row[$column_name], $primaryKeyColumn);
if ($isPrimary) {
$primaryPosition = $primaryKeyColumn[$row[$column_name]];
} else {
$primaryPosition = null;
}
$desc[$this->foldCase($row[$column_name])] = array('SCHEMA_NAME' => null, 'TABLE_NAME' => $this->foldCase($row[$table_name]), 'COLUMN_NAME' => $this->foldCase($row[$column_name]), 'COLUMN_POSITION' => (int) $row[$column_position], 'DATA_TYPE' => $type, 'DEFAULT' => $row[$column_def], 'NULLABLE' => (bool) $row[$nullable], 'LENGTH' => $row[$length], 'SCALE' => $row[$scale], 'PRECISION' => $row[$precision], 'UNSIGNED' => null, 'PRIMARY' => $isPrimary, 'PRIMARY_POSITION' => $primaryPosition, 'IDENTITY' => $identity);
}
return $desc;
}