Zend_Db_Adapter_Pdo_Mssql::describeTable PHP Method

describeTable() public method

The return value is an associative array keyed by the column name, as returned by the RDBMS. The value of each array element is an associative array with the following keys: SCHEMA_NAME => string; name of database or schema TABLE_NAME => string; COLUMN_NAME => string; column name COLUMN_POSITION => number; ordinal position of column in table DATA_TYPE => string; SQL datatype name of column DEFAULT => string; default expression of column, null if none NULLABLE => boolean; true if column can have nulls LENGTH => number; length of CHAR/VARCHAR SCALE => number; scale of NUMERIC/DECIMAL PRECISION => number; precision of NUMERIC/DECIMAL UNSIGNED => boolean; unsigned property of an integer type PRIMARY => boolean; true if column is part of the primary key PRIMARY_POSITION => integer; position of column in primary key PRIMARY_AUTO => integer; position of auto-generated column in primary key
public describeTable ( string $tableName, string $schemaName = null ) : array
$tableName string
$schemaName string OPTIONAL
return array
    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;
    }