mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-05 17:51:40 +09:00
Merge #901 DB 컬럼 정보를 불러오는 기능 + DB 컬럼 속성을 변경하는 기능 by kijin
* pr/901: Implement modifyColumn() for Cubrid Implement modifyColumn() for MS SQL Implement modifyColumn() for MySQL Implement getColumnInfo() for Cubrid Implement getColumnInfo() for MS SQL Initial implementation of getColumnInfo() for MySQL
This commit is contained in:
commit
3d70d5197e
3 changed files with 313 additions and 1 deletions
|
|
@ -539,6 +539,68 @@ class DBCubrid extends DB
|
|||
$this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify a column (only supported in CUBRID 8.4 and above, otherwise returns false)
|
||||
* @param string $table_name table name
|
||||
* @param string $column_name column name
|
||||
* @param string $type column type, default value is 'number'
|
||||
* @param int $size column size
|
||||
* @param string|int $default default value
|
||||
* @param boolean $notnull not null status, default value is false
|
||||
* @return bool
|
||||
*/
|
||||
function modifyColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = FALSE)
|
||||
{
|
||||
if(!version_compare(__CUBRID_VERSION__, '8.4.0', '>='))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$type = strtoupper($this->column_type[$type]);
|
||||
if($type == 'INTEGER')
|
||||
{
|
||||
$size = '';
|
||||
}
|
||||
|
||||
$query = sprintf("alter class \"%s%s\" modify \"%s\" ", $this->prefix, $table_name, $column_name);
|
||||
|
||||
if($type == 'char' || $type == 'varchar')
|
||||
{
|
||||
if($size)
|
||||
{
|
||||
$size = $size * 3;
|
||||
}
|
||||
}
|
||||
|
||||
if($size)
|
||||
{
|
||||
$query .= sprintf("%s(%s) ", $type, $size);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query .= sprintf("%s ", $type);
|
||||
}
|
||||
|
||||
if($default)
|
||||
{
|
||||
if($type == 'INTEGER' || $type == 'BIGINT' || $type == 'INT')
|
||||
{
|
||||
$query .= sprintf("default %d ", $default);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query .= sprintf("default '%s' ", $default);
|
||||
}
|
||||
}
|
||||
|
||||
if($notnull)
|
||||
{
|
||||
$query .= "not null ";
|
||||
}
|
||||
|
||||
return $this->_query($query) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check column exist status of the table
|
||||
* @param string $table_name table name
|
||||
|
|
@ -567,6 +629,62 @@ class DBCubrid extends DB
|
|||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 * from \"db_attribute\" where " . "\"attr_name\" ='%s' and \"class_name\" = '%s%s'", $column_name, $this->prefix, $table_name);
|
||||
$result = $this->_query($query);
|
||||
if($this->isError())
|
||||
{
|
||||
return;
|
||||
}
|
||||
$output = $this->_fetch($result);
|
||||
if($output)
|
||||
{
|
||||
$dbtype = strtolower($output->data_type);
|
||||
if($dbtype === 'string') $dbtype = 'character varying';
|
||||
$size = ($output->prec > 0) ? $output->prec : 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))
|
||||
{
|
||||
if($size % 3 == 0) $size = intval($size / 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
$xetype = $dbtype;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$xetype = $dbtype;
|
||||
$size = null;
|
||||
}
|
||||
return (object)array(
|
||||
'name' => $output->attr_name,
|
||||
'dbtype' => $dbtype,
|
||||
'xetype' => $xetype,
|
||||
'size' => $size,
|
||||
'default_value' => $output->default_value,
|
||||
'notnull' => !$output->is_nullable,
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an index to the table
|
||||
* $target_columns = array(col1, col2)
|
||||
|
|
|
|||
|
|
@ -427,6 +427,46 @@ class DBMssql extends DB
|
|||
$this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify a column
|
||||
* @param string $table_name table name
|
||||
* @param string $column_name column name
|
||||
* @param string $type column type, default value is 'number'
|
||||
* @param int $size column size
|
||||
* @param string|int $default default value
|
||||
* @param boolean $notnull not null status, default value is false
|
||||
* @return bool
|
||||
*/
|
||||
function modifyColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false)
|
||||
{
|
||||
$type = $this->column_type[$type];
|
||||
if(strtoupper($type) == 'INTEGER')
|
||||
{
|
||||
$size = '';
|
||||
}
|
||||
|
||||
$query = sprintf("alter table %s%s alter column %s ", $this->prefix, $table_name, $column_name);
|
||||
if($size)
|
||||
{
|
||||
$query .= sprintf(" %s(%s) ", $type, $size);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query .= sprintf(" %s ", $type);
|
||||
}
|
||||
|
||||
if($default)
|
||||
{
|
||||
$query .= sprintf(" default '%s' ", $default);
|
||||
}
|
||||
if($notnull)
|
||||
{
|
||||
$query .= " not null ";
|
||||
}
|
||||
|
||||
return $this->_query($query) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check column exist status of the table
|
||||
* @param string $table_name table name
|
||||
|
|
@ -448,7 +488,67 @@ 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)
|
||||
|
|
|
|||
|
|
@ -340,6 +340,45 @@ class DBMysql extends DB
|
|||
$this->_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify a column
|
||||
* @param string $table_name table name
|
||||
* @param string $column_name column name
|
||||
* @param string $type column type, default value is 'number'
|
||||
* @param int $size column size
|
||||
* @param string|int $default default value
|
||||
* @param boolean $notnull not null status, default value is false
|
||||
* @return bool
|
||||
*/
|
||||
function modifyColumn($table_name, $column_name, $type = 'number', $size = '', $default = '', $notnull = false)
|
||||
{
|
||||
$type = $this->column_type[$type];
|
||||
if(strtoupper($type) == 'INTEGER')
|
||||
{
|
||||
$size = '';
|
||||
}
|
||||
|
||||
$query = sprintf("alter table `%s%s` modify `%s` ", $this->prefix, $table_name, $column_name);
|
||||
if($size)
|
||||
{
|
||||
$query .= sprintf(" %s(%s) ", $type, $size);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query .= sprintf(" %s ", $type);
|
||||
}
|
||||
if($default)
|
||||
{
|
||||
$query .= sprintf(" default '%s' ", $default);
|
||||
}
|
||||
if($notnull)
|
||||
{
|
||||
$query .= " not null ";
|
||||
}
|
||||
|
||||
return $this->_query($query) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check column exist status of the table
|
||||
* @param string $table_name table name
|
||||
|
|
@ -370,6 +409,61 @@ class DBMysql extends DB
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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("show fields from `%s%s` where `Field` = '%s'", $this->prefix, $table_name, $column_name);
|
||||
$result = $this->_query($query);
|
||||
if($this->isError())
|
||||
{
|
||||
return;
|
||||
}
|
||||
$output = $this->_fetch($result);
|
||||
if($output)
|
||||
{
|
||||
$dbtype = $output->{'Type'};
|
||||
if($xetype = array_search($dbtype, $this->column_type))
|
||||
{
|
||||
$size = null;
|
||||
}
|
||||
elseif(strpos($dbtype, '(') !== false)
|
||||
{
|
||||
list($dbtype, $size) = explode('(', $dbtype, 2);
|
||||
$size = intval(rtrim($size, ')'));
|
||||
if($xetype = array_search($dbtype, $this->column_type))
|
||||
{
|
||||
// no-op
|
||||
}
|
||||
else
|
||||
{
|
||||
$xetype = $dbtype;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$xetype = $dbtype;
|
||||
$size = null;
|
||||
}
|
||||
return (object)array(
|
||||
'name' => $output->{'Field'},
|
||||
'dbtype' => $dbtype,
|
||||
'xetype' => $xetype,
|
||||
'size' => $size,
|
||||
'default_value' => $output->{'Default'},
|
||||
'notnull' => strncmp($output->{'Null'}, 'NO', 2) == 0 ? true : false,
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an index to the table
|
||||
* $target_columns = array(col1, col2)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue