Implement getColumnInfo() for MS SQL

This commit is contained in:
Kijin Sung 2014-08-10 22:22:29 +09:00
parent 3335942980
commit 4ac32e78aa

View file

@ -449,6 +449,66 @@ class DBMssql extends DB
return true;
}
/**
* Get information about a column
* @param string $table_name table name
* @param string $column_name column name
* @return object
*/
function getColumnInfo($table_name, $column_name)
{
$query = sprintf("select syscolumns.name as name, systypes.name as type_name, syscolumns.length as length, " .
"syscolumns.isnullable as isnullable, syscomments.text as default_value from syscolumns " .
"inner join sysobjects on sysobjects.id = syscolumns.id " .
"inner join systypes on systypes.xtype = syscolumns.xtype " .
"left join syscomments on syscolumns.cdefault = syscomments.id " .
"where sysobjects.name = '%s%s' and syscolumns.name = '%s'", $this->prefix, $table_name, $column_name);
$result = $this->_query($query);
if($this->isError())
{
return;
}
$output = $this->_fetch($result);
if($output)
{
$dbtype = $output->type_name;
$size = ($output->length > 0) ? $output->length : null;
if($xetype = array_search("$dbtype($size)", $this->column_type))
{
$dbtype = "$dbtype($size)";
$size = null;
}
elseif($size !== null)
{
if($xetype = array_search($dbtype, $this->column_type))
{
// no-op
}
else
{
$xetype = $dbtype;
}
}
else
{
$xetype = $dbtype;
$size = null;
}
return (object)array(
'name' => $output->name,
'dbtype' => $dbtype,
'xetype' => $xetype,
'size' => $size,
'default_value' => $output->default_value,
'notnull' => !$output->isnullable,
);
}
else
{
return false;
}
}
/**
* Add an index to the table
* $target_columns = array(col1, col2)