* @package Mammut\DB\Adapter\MSSQL */ class LinkedSrvTable extends Table { private $info = NULL; // cache public function __construct(MSSQL &$dbbase, $server, $db, $table, $prefix = false) { } /** * * @param $db DB * instance * @param $tableName string * name * @return TableInfo info object */ public function getInfo() { if(is_object($this->info)) return $this->info; $table = $this->getName(); $info = new TableInfo($table); $result = $this->db->query("SELECT * FROM information_schema.columns WHERE table_name = '{$table}'"); while($row = $result->fetchArray()) { $name = $row['COLUMN_NAME']; $type = ''; $size = -1; $param = array(); $charset = NULL; $collate = NULL; if(preg_match('#^([a-zA-Z]+)#', $row['DATA_TYPE'], $match)) { if($row['IS_NULLABLE'] == 'YES') $param[] = 'null'; switch(strtolower(trim($match[1]))) { case 'tinyint': $type = ColumnInfo::TYPE_TINY; break; case 'smallint': $type = ColumnInfo::TYPE_SHORT; break; case 'int': $type = ColumnInfo::TYPE_INT; break; case 'bigint': $type = ColumnInfo::TYPE_LONG; break; case 'float': $type = ColumnInfo::TYPE_FLOAT; break; case 'char': $type = ColumnInfo::TYPE_CHAR; $size = (int) $row['CHARACTER_MAXIMUM_LENGTH']; break; case 'varchar': $type = ColumnInfo::TYPE_VCHAR; $size = (int) $row['CHARACTER_MAXIMUM_LENGTH']; break; case 'date': $type = ColumnInfo::TYPE_DATE; break; case 'datetime': $type = ColumnInfo::TYPE_DATETIME; break; case 'bit': $type = ColumnInfo::TYPE_BOOLEAN; break; case 'text': $type = ColumnInfo::TYPE_TEXT; break; case 'varbinary': $type = ColumnInfo::TYPE_BINARY; break; case 'image': $type = ColumnInfo::TYPE_BLOB; break; } } $addinfo = $this->db->getArrayList("SELECT objtype, objname, name, CAST(value AS nvarchar(255)) AS value FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', '{$table}', 'column', '{$name}')"); foreach($addinfo as $add) { if($add['name'] == 'MFType') { if(preg_match('!enum\((.*)\)!', $add['value'], $match)) { $type = ColumnInfo::TYPE_ENUM; $param[] = 'values(' . $match[1] . ')'; $size = -1; } } } $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 = MSSQLUtil::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) { throw new ImplementationException('not implemented'); } public function removeColumn($column) { $this->info = NULL; if($column instanceof ColumnInfo) $column = $column->getName(); $this->db->query("ALTER TABLE " . $this->db->escapeTableName($this->getName()) . " DROP COLUMN " . $column); } }