* @package Mammut\DB\Adapter\DB2 */ class Table extends \Mammut\DB\Table { private $info = NULL; // cache public function __construct(DB2 &$db, $name) { parent::__construct($db, $name); } public function getInfo() { if(is_object($this->info)) return $this->info; $name = $this->getName(); $info = new TableInfo($name); $param = array(); $charset = NULL; $collate = NULL; $sql = "SELECT ci.table_catalog db_name, ci.table_schema schema_name, ci.table_name table_name, ci.column_name column_name, CASE WHEN pt.typtype='e' THEN 'enum' ELSE ci.data_type END AS data_type, ci.character_maximum_length size, ci.column_default default_value, ci.is_nullable nullable, pd.description description, pt.typname type_name, pt.oid type_oid FROM information_schema.columns ci LEFT JOIN pg_class pc ON pc.relname = ci.table_name LEFT JOIN pg_attribute pa ON pa.attname = ci.column_name AND pa.attrelid = pc.oid LEFT JOIN pg_description pd ON pd.objoid = pc.oid AND pd.objsubid = pa.attnum LEFT JOIN pg_type pt ON data_type='USER-DEFINED' AND pt.typname=ci.udt_name WHERE ci.table_catalog=current_database() AND ci.table_schema='" . $this->db->getSchema() . "' AND ci.table_name='{$name}'"; $result = $this->db->query($sql); while($row = $result->fetchArray()) { $name = $row['column_name']; $type = $row['data_type']; $size = -1; switch(strtolower($type)) { case 'boolean': $type = ColumnInfo::TYPE_BOOLEAN; break; case 'character': $type = ColumnInfo::TYPE_CHAR; $size = $row['size']; break; case 'character varying': $type = ColumnInfo::TYPE_VCHAR; $size = $row['size']; break; case 'smallint': if($row['description'] == 'tiny') $type = ColumnInfo::TYPE_TINY; else $type = ColumnInfo::TYPE_SHORT; break; case 'integer': $type = ColumnInfo::TYPE_INT; break; case 'bigint': $type = ColumnInfo::TYPE_LONG; break; case 'date': $type = ColumnInfo::TYPE_DATE; break; case 'timestamp without time zone': $type = ColumnInfo::TYPE_DATETIME; break; case 'bytea': $type = ColumnInfo::TYPE_BLOB; break; case 'enum': $type = ColumnInfo::TYPE_ENUM; $evalues = $this->db->getArrayList("SELECT enumlabel FROM pg_catalog.pg_enum WHERE enumtypid=" . $row['type_oid']); $values = array(); foreach($evalues as $e) array_push($values, $e['enumlabel']); $param[] = 'values(' . implode(',', $values) . ')'; unset($evalues, $e, $values); break; } $cinfo = new ColumnInfo($name, $type, $size, $param, $charset, $collate); $info->addColumn($cinfo); } $this->info = $info; return $info; } public function addColumn(ColumnInfo $info, $after = NULL) { $colSQL = DB2Util::fieldDefSQL($this->db, $info); $sql = "ALTER TABLE " . $this->db->escapeTableName($this->getName()) . " ADD COLUMN " . $colSQL; if(is_int($after)) { if($after < 0) $sql .= ' FIRST'; else { $tinfo = $this->getInfo(); $colName = $tinfo->getColumn($after)->getName(); $sql .= ' AFTER ' . $this->db->escapeColumnName($colName); unset($colName, $tinfo); } } $this->info = NULL; $this->db->query($sql); } public function alterColumn($column, ColumnInfo $newInfo) { } public function isIndexDefined($name) { throw new ImplementationException('not implemented'); } }