/** * Returns the column descriptions for a table. * * 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 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 * IDENTITY => integer; true if column is auto-generated with unique values * * @todo Discover integer unsigned property. * * @param string $tableName * @param string $schemaName OPTIONAL * @return array */ public function describeTable($tableName, $schemaName = null) { $desc = parent::describeTable($tableName, $schemaName); $tableName = strtolower($tableName); $schemaName = strtolower($schemaName); $sql = "select distinct table_name\n from information_schema.key_column_usage\n where referenced_table_name = ? "; $bind = array(); $bind[] = $tableName; if ($schemaName != null) { $bind[] = $schemaName; $sql .= " and constraint_schema = ? "; } $dependentTables = $this->fetchAll($sql, $bind); $sql = "select column_name,\n referenced_table_name as 'table_name_reference',\n referenced_table_schema as 'schema_name_reference',\n referenced_column_name as 'column_name_reference'\n from information_schema.key_column_usage\n where referenced_table_name is not null\n and table_name = ?"; $bind = array(); $bind[] = $tableName; /* if ($schemaName != null) { $bind[]= $schemaName; $sql.= " table_schema = ? "; } */ $referenceMap = $this->fetchAll($sql, $bind); $desc[$this->foldCase('reference_map')] = $referenceMap; $desc[$this->foldCase('dependent_tables')] = $dependentTables; return $desc; }
/** * Get Existing Columns * * @return array */ protected function _getExistingColumns() { return $this->_db->describeTable($this->_model->table()); }