|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package dbal
|
|
|
4c79b5 |
* @version $Id: db_tools.php 8814 2008-09-04 12:01:47Z acydburn $
|
|
|
4c79b5 |
* @copyright (c) 2007 phpBB Group
|
|
|
4c79b5 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @ignore
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
if (!defined('IN_PHPBB'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
exit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Database Tools for handling cross-db actions such as altering columns, etc.
|
|
|
4c79b5 |
* Currently not supported is returning SQL for creating tables.
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package dbal
|
|
|
4c79b5 |
* @note currently not used within phpBB3, but may be utilized later.
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class phpbb_db_tools
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Current sql layer
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
var $sql_layer = '';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
var $dbms_type_map = array(
|
|
|
4c79b5 |
'mysql_41' => array(
|
|
|
4c79b5 |
'INT:' => 'int(%d)',
|
|
|
4c79b5 |
'BINT' => 'bigint(20)',
|
|
|
4c79b5 |
'UINT' => 'mediumint(8) UNSIGNED',
|
|
|
4c79b5 |
'UINT:' => 'int(%d) UNSIGNED',
|
|
|
4c79b5 |
'TINT:' => 'tinyint(%d)',
|
|
|
4c79b5 |
'USINT' => 'smallint(4) UNSIGNED',
|
|
|
4c79b5 |
'BOOL' => 'tinyint(1) UNSIGNED',
|
|
|
4c79b5 |
'VCHAR' => 'varchar(255)',
|
|
|
4c79b5 |
'VCHAR:' => 'varchar(%d)',
|
|
|
4c79b5 |
'CHAR:' => 'char(%d)',
|
|
|
4c79b5 |
'XSTEXT' => 'text',
|
|
|
4c79b5 |
'XSTEXT_UNI'=> 'varchar(100)',
|
|
|
4c79b5 |
'STEXT' => 'text',
|
|
|
4c79b5 |
'STEXT_UNI' => 'varchar(255)',
|
|
|
4c79b5 |
'TEXT' => 'text',
|
|
|
4c79b5 |
'TEXT_UNI' => 'text',
|
|
|
4c79b5 |
'MTEXT' => 'mediumtext',
|
|
|
4c79b5 |
'MTEXT_UNI' => 'mediumtext',
|
|
|
4c79b5 |
'TIMESTAMP' => 'int(11) UNSIGNED',
|
|
|
4c79b5 |
'DECIMAL' => 'decimal(5,2)',
|
|
|
4c79b5 |
'DECIMAL:' => 'decimal(%d,2)',
|
|
|
4c79b5 |
'PDECIMAL' => 'decimal(6,3)',
|
|
|
4c79b5 |
'PDECIMAL:' => 'decimal(%d,3)',
|
|
|
4c79b5 |
'VCHAR_UNI' => 'varchar(255)',
|
|
|
4c79b5 |
'VCHAR_UNI:'=> 'varchar(%d)',
|
|
|
4c79b5 |
'VCHAR_CI' => 'varchar(255)',
|
|
|
4c79b5 |
'VARBINARY' => 'varbinary(255)',
|
|
|
4c79b5 |
),
|
|
|
4c79b5 |
|
|
|
4c79b5 |
'mysql_40' => array(
|
|
|
4c79b5 |
'INT:' => 'int(%d)',
|
|
|
4c79b5 |
'BINT' => 'bigint(20)',
|
|
|
4c79b5 |
'UINT' => 'mediumint(8) UNSIGNED',
|
|
|
4c79b5 |
'UINT:' => 'int(%d) UNSIGNED',
|
|
|
4c79b5 |
'TINT:' => 'tinyint(%d)',
|
|
|
4c79b5 |
'USINT' => 'smallint(4) UNSIGNED',
|
|
|
4c79b5 |
'BOOL' => 'tinyint(1) UNSIGNED',
|
|
|
4c79b5 |
'VCHAR' => 'varbinary(255)',
|
|
|
4c79b5 |
'VCHAR:' => 'varbinary(%d)',
|
|
|
4c79b5 |
'CHAR:' => 'binary(%d)',
|
|
|
4c79b5 |
'XSTEXT' => 'blob',
|
|
|
4c79b5 |
'XSTEXT_UNI'=> 'blob',
|
|
|
4c79b5 |
'STEXT' => 'blob',
|
|
|
4c79b5 |
'STEXT_UNI' => 'blob',
|
|
|
4c79b5 |
'TEXT' => 'blob',
|
|
|
4c79b5 |
'TEXT_UNI' => 'blob',
|
|
|
4c79b5 |
'MTEXT' => 'mediumblob',
|
|
|
4c79b5 |
'MTEXT_UNI' => 'mediumblob',
|
|
|
4c79b5 |
'TIMESTAMP' => 'int(11) UNSIGNED',
|
|
|
4c79b5 |
'DECIMAL' => 'decimal(5,2)',
|
|
|
4c79b5 |
'DECIMAL:' => 'decimal(%d,2)',
|
|
|
4c79b5 |
'PDECIMAL' => 'decimal(6,3)',
|
|
|
4c79b5 |
'PDECIMAL:' => 'decimal(%d,3)',
|
|
|
4c79b5 |
'VCHAR_UNI' => 'blob',
|
|
|
4c79b5 |
'VCHAR_UNI:'=> array('varbinary(%d)', 'limit' => array('mult', 3, 255, 'blob')),
|
|
|
4c79b5 |
'VCHAR_CI' => 'blob',
|
|
|
4c79b5 |
'VARBINARY' => 'varbinary(255)',
|
|
|
4c79b5 |
),
|
|
|
4c79b5 |
|
|
|
4c79b5 |
'firebird' => array(
|
|
|
4c79b5 |
'INT:' => 'INTEGER',
|
|
|
4c79b5 |
'BINT' => 'DOUBLE PRECISION',
|
|
|
4c79b5 |
'UINT' => 'INTEGER',
|
|
|
4c79b5 |
'UINT:' => 'INTEGER',
|
|
|
4c79b5 |
'TINT:' => 'INTEGER',
|
|
|
4c79b5 |
'USINT' => 'INTEGER',
|
|
|
4c79b5 |
'BOOL' => 'INTEGER',
|
|
|
4c79b5 |
'VCHAR' => 'VARCHAR(255) CHARACTER SET NONE',
|
|
|
4c79b5 |
'VCHAR:' => 'VARCHAR(%d) CHARACTER SET NONE',
|
|
|
4c79b5 |
'CHAR:' => 'CHAR(%d) CHARACTER SET NONE',
|
|
|
4c79b5 |
'XSTEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
|
|
4c79b5 |
'STEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
|
|
4c79b5 |
'TEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
|
|
4c79b5 |
'MTEXT' => 'BLOB SUB_TYPE TEXT CHARACTER SET NONE',
|
|
|
4c79b5 |
'XSTEXT_UNI'=> 'VARCHAR(100) CHARACTER SET UTF8',
|
|
|
4c79b5 |
'STEXT_UNI' => 'VARCHAR(255) CHARACTER SET UTF8',
|
|
|
4c79b5 |
'TEXT_UNI' => 'BLOB SUB_TYPE TEXT CHARACTER SET UTF8',
|
|
|
4c79b5 |
'MTEXT_UNI' => 'BLOB SUB_TYPE TEXT CHARACTER SET UTF8',
|
|
|
4c79b5 |
'TIMESTAMP' => 'INTEGER',
|
|
|
4c79b5 |
'DECIMAL' => 'DOUBLE PRECISION',
|
|
|
4c79b5 |
'DECIMAL:' => 'DOUBLE PRECISION',
|
|
|
4c79b5 |
'PDECIMAL' => 'DOUBLE PRECISION',
|
|
|
4c79b5 |
'PDECIMAL:' => 'DOUBLE PRECISION',
|
|
|
4c79b5 |
'VCHAR_UNI' => 'VARCHAR(255) CHARACTER SET UTF8',
|
|
|
4c79b5 |
'VCHAR_UNI:'=> 'VARCHAR(%d) CHARACTER SET UTF8',
|
|
|
4c79b5 |
'VCHAR_CI' => 'VARCHAR(255) CHARACTER SET UTF8',
|
|
|
4c79b5 |
'VARBINARY' => 'CHAR(255) CHARACTER SET NONE',
|
|
|
4c79b5 |
),
|
|
|
4c79b5 |
|
|
|
4c79b5 |
'mssql' => array(
|
|
|
4c79b5 |
'INT:' => '[int]',
|
|
|
4c79b5 |
'BINT' => '[float]',
|
|
|
4c79b5 |
'UINT' => '[int]',
|
|
|
4c79b5 |
'UINT:' => '[int]',
|
|
|
4c79b5 |
'TINT:' => '[int]',
|
|
|
4c79b5 |
'USINT' => '[int]',
|
|
|
4c79b5 |
'BOOL' => '[int]',
|
|
|
4c79b5 |
'VCHAR' => '[varchar] (255)',
|
|
|
4c79b5 |
'VCHAR:' => '[varchar] (%d)',
|
|
|
4c79b5 |
'CHAR:' => '[char] (%d)',
|
|
|
4c79b5 |
'XSTEXT' => '[varchar] (1000)',
|
|
|
4c79b5 |
'STEXT' => '[varchar] (3000)',
|
|
|
4c79b5 |
'TEXT' => '[varchar] (8000)',
|
|
|
4c79b5 |
'MTEXT' => '[text]',
|
|
|
4c79b5 |
'XSTEXT_UNI'=> '[varchar] (100)',
|
|
|
4c79b5 |
'STEXT_UNI' => '[varchar] (255)',
|
|
|
4c79b5 |
'TEXT_UNI' => '[varchar] (4000)',
|
|
|
4c79b5 |
'MTEXT_UNI' => '[text]',
|
|
|
4c79b5 |
'TIMESTAMP' => '[int]',
|
|
|
4c79b5 |
'DECIMAL' => '[float]',
|
|
|
4c79b5 |
'DECIMAL:' => '[float]',
|
|
|
4c79b5 |
'PDECIMAL' => '[float]',
|
|
|
4c79b5 |
'PDECIMAL:' => '[float]',
|
|
|
4c79b5 |
'VCHAR_UNI' => '[varchar] (255)',
|
|
|
4c79b5 |
'VCHAR_UNI:'=> '[varchar] (%d)',
|
|
|
4c79b5 |
'VCHAR_CI' => '[varchar] (255)',
|
|
|
4c79b5 |
'VARBINARY' => '[varchar] (255)',
|
|
|
4c79b5 |
),
|
|
|
4c79b5 |
|
|
|
4c79b5 |
'oracle' => array(
|
|
|
4c79b5 |
'INT:' => 'number(%d)',
|
|
|
4c79b5 |
'BINT' => 'number(20)',
|
|
|
4c79b5 |
'UINT' => 'number(8)',
|
|
|
4c79b5 |
'UINT:' => 'number(%d)',
|
|
|
4c79b5 |
'TINT:' => 'number(%d)',
|
|
|
4c79b5 |
'USINT' => 'number(4)',
|
|
|
4c79b5 |
'BOOL' => 'number(1)',
|
|
|
4c79b5 |
'VCHAR' => 'varchar2(255)',
|
|
|
4c79b5 |
'VCHAR:' => 'varchar2(%d)',
|
|
|
4c79b5 |
'CHAR:' => 'char(%d)',
|
|
|
4c79b5 |
'XSTEXT' => 'varchar2(1000)',
|
|
|
4c79b5 |
'STEXT' => 'varchar2(3000)',
|
|
|
4c79b5 |
'TEXT' => 'clob',
|
|
|
4c79b5 |
'MTEXT' => 'clob',
|
|
|
4c79b5 |
'XSTEXT_UNI'=> 'varchar2(300)',
|
|
|
4c79b5 |
'STEXT_UNI' => 'varchar2(765)',
|
|
|
4c79b5 |
'TEXT_UNI' => 'clob',
|
|
|
4c79b5 |
'MTEXT_UNI' => 'clob',
|
|
|
4c79b5 |
'TIMESTAMP' => 'number(11)',
|
|
|
4c79b5 |
'DECIMAL' => 'number(5, 2)',
|
|
|
4c79b5 |
'DECIMAL:' => 'number(%d, 2)',
|
|
|
4c79b5 |
'PDECIMAL' => 'number(6, 3)',
|
|
|
4c79b5 |
'PDECIMAL:' => 'number(%d, 3)',
|
|
|
4c79b5 |
'VCHAR_UNI' => 'varchar2(765)',
|
|
|
4c79b5 |
'VCHAR_UNI:'=> array('varchar2(%d)', 'limit' => array('mult', 3, 765, 'clob')),
|
|
|
4c79b5 |
'VCHAR_CI' => 'varchar2(255)',
|
|
|
4c79b5 |
'VARBINARY' => 'raw(255)',
|
|
|
4c79b5 |
),
|
|
|
4c79b5 |
|
|
|
4c79b5 |
'sqlite' => array(
|
|
|
4c79b5 |
'INT:' => 'int(%d)',
|
|
|
4c79b5 |
'BINT' => 'bigint(20)',
|
|
|
4c79b5 |
'UINT' => 'INTEGER UNSIGNED', //'mediumint(8) UNSIGNED',
|
|
|
4c79b5 |
'UINT:' => 'INTEGER UNSIGNED', // 'int(%d) UNSIGNED',
|
|
|
4c79b5 |
'TINT:' => 'tinyint(%d)',
|
|
|
4c79b5 |
'USINT' => 'INTEGER UNSIGNED', //'mediumint(4) UNSIGNED',
|
|
|
4c79b5 |
'BOOL' => 'INTEGER UNSIGNED', //'tinyint(1) UNSIGNED',
|
|
|
4c79b5 |
'VCHAR' => 'varchar(255)',
|
|
|
4c79b5 |
'VCHAR:' => 'varchar(%d)',
|
|
|
4c79b5 |
'CHAR:' => 'char(%d)',
|
|
|
4c79b5 |
'XSTEXT' => 'text(65535)',
|
|
|
4c79b5 |
'STEXT' => 'text(65535)',
|
|
|
4c79b5 |
'TEXT' => 'text(65535)',
|
|
|
4c79b5 |
'MTEXT' => 'mediumtext(16777215)',
|
|
|
4c79b5 |
'XSTEXT_UNI'=> 'text(65535)',
|
|
|
4c79b5 |
'STEXT_UNI' => 'text(65535)',
|
|
|
4c79b5 |
'TEXT_UNI' => 'text(65535)',
|
|
|
4c79b5 |
'MTEXT_UNI' => 'mediumtext(16777215)',
|
|
|
4c79b5 |
'TIMESTAMP' => 'INTEGER UNSIGNED', //'int(11) UNSIGNED',
|
|
|
4c79b5 |
'DECIMAL' => 'decimal(5,2)',
|
|
|
4c79b5 |
'DECIMAL:' => 'decimal(%d,2)',
|
|
|
4c79b5 |
'PDECIMAL' => 'decimal(6,3)',
|
|
|
4c79b5 |
'PDECIMAL:' => 'decimal(%d,3)',
|
|
|
4c79b5 |
'VCHAR_UNI' => 'varchar(255)',
|
|
|
4c79b5 |
'VCHAR_UNI:'=> 'varchar(%d)',
|
|
|
4c79b5 |
'VCHAR_CI' => 'varchar(255)',
|
|
|
4c79b5 |
'VARBINARY' => 'blob',
|
|
|
4c79b5 |
),
|
|
|
4c79b5 |
|
|
|
4c79b5 |
'postgres' => array(
|
|
|
4c79b5 |
'INT:' => 'INT4',
|
|
|
4c79b5 |
'BINT' => 'INT8',
|
|
|
4c79b5 |
'UINT' => 'INT4', // unsigned
|
|
|
4c79b5 |
'UINT:' => 'INT4', // unsigned
|
|
|
4c79b5 |
'USINT' => 'INT2', // unsigned
|
|
|
4c79b5 |
'BOOL' => 'INT2', // unsigned
|
|
|
4c79b5 |
'TINT:' => 'INT2',
|
|
|
4c79b5 |
'VCHAR' => 'varchar(255)',
|
|
|
4c79b5 |
'VCHAR:' => 'varchar(%d)',
|
|
|
4c79b5 |
'CHAR:' => 'char(%d)',
|
|
|
4c79b5 |
'XSTEXT' => 'varchar(1000)',
|
|
|
4c79b5 |
'STEXT' => 'varchar(3000)',
|
|
|
4c79b5 |
'TEXT' => 'varchar(8000)',
|
|
|
4c79b5 |
'MTEXT' => 'TEXT',
|
|
|
4c79b5 |
'XSTEXT_UNI'=> 'varchar(100)',
|
|
|
4c79b5 |
'STEXT_UNI' => 'varchar(255)',
|
|
|
4c79b5 |
'TEXT_UNI' => 'varchar(4000)',
|
|
|
4c79b5 |
'MTEXT_UNI' => 'TEXT',
|
|
|
4c79b5 |
'TIMESTAMP' => 'INT4', // unsigned
|
|
|
4c79b5 |
'DECIMAL' => 'decimal(5,2)',
|
|
|
4c79b5 |
'DECIMAL:' => 'decimal(%d,2)',
|
|
|
4c79b5 |
'PDECIMAL' => 'decimal(6,3)',
|
|
|
4c79b5 |
'PDECIMAL:' => 'decimal(%d,3)',
|
|
|
4c79b5 |
'VCHAR_UNI' => 'varchar(255)',
|
|
|
4c79b5 |
'VCHAR_UNI:'=> 'varchar(%d)',
|
|
|
4c79b5 |
'VCHAR_CI' => 'varchar_ci',
|
|
|
4c79b5 |
'VARBINARY' => 'bytea',
|
|
|
4c79b5 |
),
|
|
|
4c79b5 |
);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// A list of types being unsigned for better reference in some db's
|
|
|
4c79b5 |
var $unsigned_types = array('UINT', 'UINT:', 'USINT', 'BOOL', 'TIMESTAMP');
|
|
|
4c79b5 |
var $supported_dbms = array('firebird', 'mssql', 'mysql_40', 'mysql_41', 'oracle', 'postgres', 'sqlite');
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Set this to true if you only want to return the 'to-be-executed' SQL statement(s) (as an array).
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
var $return_statements = false;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function phpbb_db_tools(&$db)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db = $db;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Determine mapping database type
|
|
|
4c79b5 |
switch ($this->db->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'mysql':
|
|
|
4c79b5 |
$this->sql_layer = 'mysql_40';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql4':
|
|
|
4c79b5 |
if (version_compare($this->db->sql_server_info(true), '4.1.3', '>='))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_layer = 'mysql_41';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_layer = 'mysql_40';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysqli':
|
|
|
4c79b5 |
$this->sql_layer = 'mysql_41';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
case 'mssql_odbc':
|
|
|
4c79b5 |
$this->sql_layer = 'mssql';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
default:
|
|
|
4c79b5 |
$this->sql_layer = $this->db->sql_layer;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Handle passed database update array.
|
|
|
4c79b5 |
* Expected structure...
|
|
|
4c79b5 |
* Key being one of the following
|
|
|
4c79b5 |
* change_columns: Column changes (only type, not name)
|
|
|
4c79b5 |
* add_columns: Add columns to a table
|
|
|
4c79b5 |
* drop_keys: Dropping keys
|
|
|
4c79b5 |
* drop_columns: Removing/Dropping columns
|
|
|
4c79b5 |
* add_primary_keys: adding primary keys
|
|
|
4c79b5 |
* add_unique_index: adding an unique index
|
|
|
4c79b5 |
* add_index: adding an index
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* The values are in this format:
|
|
|
4c79b5 |
* {TABLE NAME} => array(
|
|
|
4c79b5 |
* {COLUMN NAME} => array({COLUMN TYPE}, {DEFAULT VALUE}, {OPTIONAL VARIABLES}),
|
|
|
4c79b5 |
* {KEY/INDEX NAME} => array({COLUMN NAMES}),
|
|
|
4c79b5 |
* )
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* For more information have a look at /develop/create_schema_files.php (only available through CVS)
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function perform_schema_changes($schema_changes)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (empty($schema_changes))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Change columns?
|
|
|
4c79b5 |
if (!empty($schema_changes['change_columns']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($schema_changes['change_columns'] as $table => $columns)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($columns as $column_name => $column_data)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result = $this->sql_column_change($table, $column_name, $column_data);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array_merge($statements, $result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Add columns?
|
|
|
4c79b5 |
if (!empty($schema_changes['add_columns']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($schema_changes['add_columns'] as $table => $columns)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($columns as $column_name => $column_data)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// Only add the column if it does not exist yet
|
|
|
4c79b5 |
if (!$this->sql_column_exists($table, $column_name))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result = $this->sql_column_add($table, $column_name, $column_data);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array_merge($statements, $result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Remove keys?
|
|
|
4c79b5 |
if (!empty($schema_changes['drop_keys']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($schema_changes['drop_keys'] as $table => $indexes)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($indexes as $index_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result = $this->sql_index_drop($table, $index_name);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array_merge($statements, $result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Drop columns?
|
|
|
4c79b5 |
if (!empty($schema_changes['drop_columns']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($schema_changes['drop_columns'] as $table => $columns)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($columns as $column)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result = $this->sql_column_remove($table, $column);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array_merge($statements, $result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Add primary keys?
|
|
|
4c79b5 |
if (!empty($schema_changes['add_primary_keys']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($schema_changes['add_primary_keys'] as $table => $columns)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result = $this->sql_create_primary_key($table, $columns);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array_merge($statements, $result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Add unqiue indexes?
|
|
|
4c79b5 |
if (!empty($schema_changes['add_unique_index']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($schema_changes['add_unique_index'] as $table => $index_array)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($index_array as $index_name => $column)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result = $this->sql_create_unique_index($table, $index_name, $column);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array_merge($statements, $result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Add indexes?
|
|
|
4c79b5 |
if (!empty($schema_changes['add_index']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($schema_changes['add_index'] as $table => $index_array)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
foreach ($index_array as $index_name => $column)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result = $this->sql_create_index($table, $index_name, $column);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array_merge($statements, $result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $statements;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Check if a specified column exist
|
|
|
4c79b5 |
* @return bool True if column exists, else false
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_column_exists($table, $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql = "SHOW COLUMNS FROM $table";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
while ($row = $this->db->sql_fetchrow($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// lower case just in case
|
|
|
4c79b5 |
if (strtolower($row['Field']) == $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// PostgreSQL has a way of doing this in a much simpler way but would
|
|
|
4c79b5 |
// not allow us to support all versions of PostgreSQL
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
$sql = "SELECT a.attname
|
|
|
4c79b5 |
FROM pg_class c, pg_attribute a
|
|
|
4c79b5 |
WHERE c.relname = '{$table}'
|
|
|
4c79b5 |
AND a.attnum > 0
|
|
|
4c79b5 |
AND a.attrelid = c.oid";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
while ($row = $this->db->sql_fetchrow($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// lower case just in case
|
|
|
4c79b5 |
if (strtolower($row['attname']) == $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// same deal with PostgreSQL, we must perform more complex operations than
|
|
|
4c79b5 |
// we technically could
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$sql = "SELECT c.name
|
|
|
4c79b5 |
FROM syscolumns c
|
|
|
4c79b5 |
LEFT JOIN sysobjects o ON c.id = o.id
|
|
|
4c79b5 |
WHERE o.name = '{$table}'";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
while ($row = $this->db->sql_fetchrow($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// lower case just in case
|
|
|
4c79b5 |
if (strtolower($row['name']) == $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
$sql = "SELECT column_name
|
|
|
4c79b5 |
FROM user_tab_columns
|
|
|
4c79b5 |
WHERE table_name = '{$table}'";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
while ($row = $this->db->sql_fetchrow($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// lower case just in case
|
|
|
4c79b5 |
if (strtolower($row['column_name']) == $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
$sql = "SELECT RDB\$FIELD_NAME as FNAME
|
|
|
4c79b5 |
FROM RDB\$RELATION_FIELDS
|
|
|
4c79b5 |
WHERE RDB\$RELATION_NAME = '{$table}'";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
while ($row = $this->db->sql_fetchrow($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// lower case just in case
|
|
|
4c79b5 |
if (strtolower($row['fname']) == $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// ugh, SQLite
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
$sql = "SELECT sql
|
|
|
4c79b5 |
FROM sqlite_master
|
|
|
4c79b5 |
WHERE type = 'table'
|
|
|
4c79b5 |
AND name = '{$table}'";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$row = $this->db->sql_fetchrow($result);
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$cols = trim($matches[1]);
|
|
|
4c79b5 |
$col_array = preg_split('/,(?![\s\w]+\))/m', $cols);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($col_array as $declaration)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
|
4c79b5 |
if ($entities[0] == 'PRIMARY')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
continue;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (strtolower($entities[0]) == $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Private method for performing sql statements (either execute them or return them)
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_run_sql($statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($this->return_statements)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $statements;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// We could add error handling here...
|
|
|
4c79b5 |
foreach ($statements as $sql)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($sql === 'begin')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db->sql_transaction('begin');
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if ($sql === 'commit')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db->sql_transaction('commit');
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->db->sql_query($sql);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Function to prepare some column information for better usage
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_prepare_column_data($table_name, $column_name, $column_data)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// Get type
|
|
|
4c79b5 |
if (strpos($column_data[0], ':') !== false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
list($orig_column_type, $column_length) = explode(':', $column_data[0]);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!is_array($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'], $column_length);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (isset($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'][0])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'div':
|
|
|
4c79b5 |
$column_length /= $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'][1];
|
|
|
4c79b5 |
$column_length = ceil($column_length);
|
|
|
4c79b5 |
$column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'][0], $column_length);
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][0])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'mult':
|
|
|
4c79b5 |
$column_length *= $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][1];
|
|
|
4c79b5 |
if ($column_length > $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][2])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$column_type = $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][3];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'][0], $column_length);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$orig_column_type .= ':';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$orig_column_type = $column_data[0];
|
|
|
4c79b5 |
$column_type = $this->dbms_type_map[$this->sql_layer][$column_data[0]];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Adjust default value if db-dependant specified
|
|
|
4c79b5 |
if (is_array($column_data[1]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$column_data[1] = (isset($column_data[1][$this->sql_layer])) ? $column_data[1][$this->sql_layer] : $column_data[1]['default'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql = '';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$return_array = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
$sql .= " {$column_type} ";
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!is_null($column_data[1]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql .= 'DEFAULT ' . ((is_numeric($column_data[1])) ? $column_data[1] : "'{$column_data[1]}'") . ' ';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql .= 'NOT NULL';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// This is a UNICODE column and thus should be given it's fair share
|
|
|
4c79b5 |
if (preg_match('/^X?STEXT_UNI|VCHAR_(CI|UNI:?)/', $column_data[0]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql .= ' COLLATE UNICODE';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$sql .= " {$column_type} ";
|
|
|
4c79b5 |
$sql_default = " {$column_type} ";
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// For adding columns we need the default definition
|
|
|
4c79b5 |
if (!is_null($column_data[1]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// For hexadecimal values do not use single quotes
|
|
|
4c79b5 |
if (strpos($column_data[1], '0x') === 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql_default .= 'DEFAULT (' . $column_data[1] . ') ';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql_default .= 'DEFAULT (' . ((is_numeric($column_data[1])) ? $column_data[1] : "'{$column_data[1]}'") . ') ';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql .= 'NOT NULL';
|
|
|
4c79b5 |
$sql_default .= 'NOT NULL';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$return_array['column_type_sql_default'] = $sql_default;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$sql .= " {$column_type} ";
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// For hexadecimal values do not use single quotes
|
|
|
4c79b5 |
if (!is_null($column_data[1]) && substr($column_type, -4) !== 'text' && substr($column_type, -4) !== 'blob')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql .= (strpos($column_data[1], '0x') === 0) ? "DEFAULT {$column_data[1]} " : "DEFAULT '{$column_data[1]}' ";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$sql .= 'NOT NULL';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($column_data[2]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($column_data[2] == 'auto_increment')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql .= ' auto_increment';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if ($this->sql_layer === 'mysql_41' && $column_data[2] == 'true_sort')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql .= ' COLLATE utf8_unicode_ci';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
$sql .= " {$column_type} ";
|
|
|
4c79b5 |
$sql .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}' " : '';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// In Oracle empty strings ('') are treated as NULL.
|
|
|
4c79b5 |
// Therefore in oracle we allow NULL's for all DEFAULT '' entries
|
|
|
4c79b5 |
// Oracle does not like setting NOT NULL on a column that is already NOT NULL (this happens only on number fields)
|
|
|
4c79b5 |
if (preg_match('/number/i', $column_type))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql .= ($column_data[1] === '') ? '' : 'NOT NULL';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
$return_array['column_type'] = $column_type;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql .= " {$column_type} ";
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$default_val = "nextval('{$table_name}_seq')";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (!is_null($column_data[1]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$default_val = "'" . $column_data[1] . "'";
|
|
|
4c79b5 |
$return_array['null'] = 'NOT NULL';
|
|
|
4c79b5 |
$sql .= 'NOT NULL ';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$return_array['default'] = $default_val;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql .= "DEFAULT {$default_val}";
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Unsigned? Then add a CHECK contraint
|
|
|
4c79b5 |
if (in_array($orig_column_type, $this->unsigned_types))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$return_array['constraint'] = "CHECK ({$column_name} >= 0)";
|
|
|
4c79b5 |
$sql .= " CHECK ({$column_name} >= 0)";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
if (isset($column_data[2]) && $column_data[2] == 'auto_increment')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql .= ' INTEGER PRIMARY KEY';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql .= ' ' . $column_type;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql .= ' NOT NULL ';
|
|
|
4c79b5 |
$sql .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}'" : '';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$return_array['column_type_sql'] = $sql;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $return_array;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Add new column
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_column_add($table_name, $column_name, $column_data)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$column_data = $this->sql_prepare_column_data($table_name, $column_name, $column_data);
|
|
|
4c79b5 |
$statements = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE "' . $table_name . '" ADD "' . $column_name . '" ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE [' . $table_name . '] ADD [' . $column_name . '] ' . $column_data['column_type_sql_default'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE `' . $table_name . '` ADD COLUMN `' . $column_name . '` ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD ' . $column_name . ' ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD COLUMN "' . $column_name . '" ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
if (version_compare(sqlite_libversion(), '3.0') == -1)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql = "SELECT sql
|
|
|
4c79b5 |
FROM sqlite_master
|
|
|
4c79b5 |
WHERE type = 'table'
|
|
|
4c79b5 |
AND name = '{$table_name}'
|
|
|
4c79b5 |
ORDER BY type DESC, name;";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$row = $this->db->sql_fetchrow($result);
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = 'begin';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Create a backup table and populate it, destroy the existing one
|
|
|
4c79b5 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
|
4c79b5 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
|
4c79b5 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$new_table_cols = trim($matches[1]);
|
|
|
4c79b5 |
$old_table_cols = preg_split('/,(?![\s\w]+\))/m', $new_table_cols);
|
|
|
4c79b5 |
$column_list = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($old_table_cols as $declaration)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
|
4c79b5 |
if ($entities[0] == 'PRIMARY')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
continue;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$column_list[] = $entities[0];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$columns = implode(',', $column_list);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$new_table_cols = $column_name . ' ' . $column_data['column_type_sql'] . ',' . $new_table_cols;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// create a new table and fill it up. destroy the temp one
|
|
|
4c79b5 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . $new_table_cols . ');';
|
|
|
4c79b5 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
|
4c79b5 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = 'commit';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD ' . $column_name . ' [' . $column_data['column_type_sql'] . ']';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_sql_run_sql($statements);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Drop column
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_column_remove($table_name, $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE "' . $table_name . '" DROP "' . $column_name . '"';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE [' . $table_name . '] DROP COLUMN [' . $column_name . ']';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE `' . $table_name . '` DROP COLUMN `' . $column_name . '`';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' DROP ' . $column_name;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' DROP COLUMN "' . $column_name . '"';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
if (version_compare(sqlite_libversion(), '3.0') == -1)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql = "SELECT sql
|
|
|
4c79b5 |
FROM sqlite_master
|
|
|
4c79b5 |
WHERE type = 'table'
|
|
|
4c79b5 |
AND name = '{$table_name}'
|
|
|
4c79b5 |
ORDER BY type DESC, name;";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$row = $this->db->sql_fetchrow($result);
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = 'begin';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Create a backup table and populate it, destroy the existing one
|
|
|
4c79b5 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
|
4c79b5 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
|
4c79b5 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$new_table_cols = trim($matches[1]);
|
|
|
4c79b5 |
$old_table_cols = preg_split('/,(?![\s\w]+\))/m', $new_table_cols);
|
|
|
4c79b5 |
$column_list = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($old_table_cols as $declaration)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
|
4c79b5 |
if ($entities[0] == 'PRIMARY' || $entities[0] === $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
continue;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$column_list[] = $entities[0];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$columns = implode(',', $column_list);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$new_table_cols = $new_table_cols = preg_replace('/' . $column_name . '[^,]+(?:,|$)/m', '', $new_table_cols);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// create a new table and fill it up. destroy the temp one
|
|
|
4c79b5 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . $new_table_cols . ');';
|
|
|
4c79b5 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
|
4c79b5 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = 'commit';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' DROP COLUMN ' . $column_name;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_sql_run_sql($statements);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Drop Index
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_index_drop($table_name, $index_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$statements[] = 'DROP INDEX ' . $table_name . '.' . $index_name;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$statements[] = 'DROP INDEX ' . $index_name . ' ON ' . $table_name;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
$statements[] = 'DROP INDEX ' . $table_name . '_' . $index_name;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_sql_run_sql($statements);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Add primary key
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_create_primary_key($table_name, $column)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD PRIMARY KEY (' . implode(', ', $column) . ')';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$sql = "ALTER TABLE [{$table_name}] WITH NOCHECK ADD ";
|
|
|
4c79b5 |
$sql .= "CONSTRAINT [PK_{$table_name}] PRIMARY KEY CLUSTERED (";
|
|
|
4c79b5 |
$sql .= '[' . implode("],\n\t\t[", $column) . ']';
|
|
|
4c79b5 |
$sql .= ') ON [PRIMARY]';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = $sql;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD PRIMARY KEY (' . implode(', ', $column) . ')';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . 'add CONSTRAINT pk_' . $table_name . ' PRIMARY KEY (' . implode(', ', $column) . ')';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
$sql = "SELECT sql
|
|
|
4c79b5 |
FROM sqlite_master
|
|
|
4c79b5 |
WHERE type = 'table'
|
|
|
4c79b5 |
AND name = '{$table_name}'
|
|
|
4c79b5 |
ORDER BY type DESC, name;";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$row = $this->db->sql_fetchrow($result);
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = 'begin';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Create a backup table and populate it, destroy the existing one
|
|
|
4c79b5 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
|
4c79b5 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
|
4c79b5 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$new_table_cols = trim($matches[1]);
|
|
|
4c79b5 |
$old_table_cols = preg_split('/,(?![\s\w]+\))/m', $new_table_cols);
|
|
|
4c79b5 |
$column_list = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($old_table_cols as $declaration)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
|
4c79b5 |
if ($entities[0] == 'PRIMARY')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
continue;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$column_list[] = $entities[0];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$columns = implode(',', $column_list);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// create a new table and fill it up. destroy the temp one
|
|
|
4c79b5 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . $new_table_cols . ', PRIMARY KEY (' . implode(', ', $column) . '));';
|
|
|
4c79b5 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
|
4c79b5 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = 'commit';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_sql_run_sql($statements);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Add unique index
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_create_unique_index($table_name, $index_name, $column)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
$statements[] = 'CREATE UNIQUE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$statements[] = 'CREATE UNIQUE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$statements[] = 'CREATE UNIQUE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ') ON [PRIMARY]';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_sql_run_sql($statements);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Add index
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_create_index($table_name, $index_name, $column)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$statements = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
$statements[] = 'CREATE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$statements[] = 'CREATE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$statements[] = 'CREATE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ') ON [PRIMARY]';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_sql_run_sql($statements);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* List all of the indices that belong to a table,
|
|
|
4c79b5 |
* does not count:
|
|
|
4c79b5 |
* * UNIQUE indices
|
|
|
4c79b5 |
* * PRIMARY keys
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_list_index($table_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$index_array = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->sql_layer == 'mssql')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql = "EXEC sp_statistics '$table_name'";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
while ($row = $this->db->sql_fetchrow($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($row['TYPE'] == 3)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$index_array[] = $row['INDEX_NAME'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
$sql = "SELECT LOWER(RDB\$INDEX_NAME) as index_name
|
|
|
4c79b5 |
FROM RDB\$INDICES
|
|
|
4c79b5 |
WHERE RDB\$RELATION_NAME = " . strtoupper($table_name) . "
|
|
|
4c79b5 |
AND RDB\$UNIQUE_FLAG IS NULL
|
|
|
4c79b5 |
AND RDB\$FOREIGN_KEY IS NULL";
|
|
|
4c79b5 |
$col = 'index_name';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
$sql = "SELECT ic.relname as index_name
|
|
|
4c79b5 |
FROM pg_class bc, pg_class ic, pg_index i
|
|
|
4c79b5 |
WHERE (bc.oid = i.indrelid)
|
|
|
4c79b5 |
AND (ic.oid = i.indexrelid)
|
|
|
4c79b5 |
AND (bc.relname = '" . $table_name . "')
|
|
|
4c79b5 |
AND (i.indisunique != 't')
|
|
|
4c79b5 |
AND (i.indisprimary != 't')";
|
|
|
4c79b5 |
$col = 'index_name';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$sql = 'SHOW KEYS
|
|
|
4c79b5 |
FROM ' . $table_name;
|
|
|
4c79b5 |
$col = 'Key_name';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
$sql = "SELECT index_name
|
|
|
4c79b5 |
FROM user_indexes
|
|
|
4c79b5 |
WHERE table_name = '" . $table_name . "'
|
|
|
4c79b5 |
AND generated = 'N'";
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
$sql = "PRAGMA index_info('" . $table_name . "');";
|
|
|
4c79b5 |
$col = 'name';
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
while ($row = $this->db->sql_fetchrow($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (($this->sql_layer == 'mysql_40' || $this->sql_layer == 'mysql_41') && !$row['Non_unique'])
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
continue;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
$row[$col] = substr($row[$col], strlen($table_name) + 1);
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$index_array[] = $row[$col];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return array_map('strtolower', $index_array);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Change column type (not name!)
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_column_change($table_name, $column_name, $column_data)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$column_data = $this->sql_prepare_column_data($table_name, $column_name, $column_data);
|
|
|
4c79b5 |
$statements = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch ($this->sql_layer)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'firebird':
|
|
|
4c79b5 |
// Change type...
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE "' . $table_name . '" ALTER COLUMN "' . $column_name . '" TYPE ' . ' ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mssql':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE [' . $table_name . '] ALTER COLUMN [' . $column_name . '] ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'mysql_40':
|
|
|
4c79b5 |
case 'mysql_41':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE `' . $table_name . '` CHANGE `' . $column_name . '` `' . $column_name . '` ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'oracle':
|
|
|
4c79b5 |
$statements[] = 'ALTER TABLE ' . $table_name . ' MODIFY ' . $column_name . ' ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'postgres':
|
|
|
4c79b5 |
$sql = 'ALTER TABLE ' . $table_name . ' ';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql_array = array();
|
|
|
4c79b5 |
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' TYPE ' . $column_data['column_type'];
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($column_data['null']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($column_data['null'] == 'NOT NULL')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' SET NOT NULL';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if ($column_data['null'] == 'NULL')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' DROP NOT NULL';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($column_data['default']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' SET DEFAULT ' . $column_data['default'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// we don't want to double up on constraints if we change different number data types
|
|
|
4c79b5 |
if (isset($column_data['constraint']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$constraint_sql = "SELECT consrc as constraint_data
|
|
|
4c79b5 |
FROM pg_constraint, pg_class bc
|
|
|
4c79b5 |
WHERE conrelid = bc.oid
|
|
|
4c79b5 |
AND bc.relname = '{$table_name}'
|
|
|
4c79b5 |
AND NOT EXISTS (
|
|
|
4c79b5 |
SELECT *
|
|
|
4c79b5 |
FROM pg_constraint as c, pg_inherits as i
|
|
|
4c79b5 |
WHERE i.inhrelid = pg_constraint.conrelid
|
|
|
4c79b5 |
AND c.conname = pg_constraint.conname
|
|
|
4c79b5 |
AND c.consrc = pg_constraint.consrc
|
|
|
4c79b5 |
AND c.conrelid = i.inhparent
|
|
|
4c79b5 |
)";
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$constraint_exists = false;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result = $this->db->sql_query($constraint_sql);
|
|
|
4c79b5 |
while ($row = $this->db->sql_fetchrow($result))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (trim($row['constraint_data']) == trim($column_data['constraint']))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$constraint_exists = true;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$constraint_exists)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sql_array[] = 'ADD ' . $column_data['constraint'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql .= implode(', ', $sql_array);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = $sql;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'sqlite':
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$sql = "SELECT sql
|
|
|
4c79b5 |
FROM sqlite_master
|
|
|
4c79b5 |
WHERE type = 'table'
|
|
|
4c79b5 |
AND name = '{$table_name}'
|
|
|
4c79b5 |
ORDER BY type DESC, name;";
|
|
|
4c79b5 |
$result = $this->db->sql_query($sql);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$row = $this->db->sql_fetchrow($result);
|
|
|
4c79b5 |
$this->db->sql_freeresult($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = 'begin';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Create a temp table and populate it, destroy the existing one
|
|
|
4c79b5 |
$statements[] = preg_replace('#CREATE\s+TABLE\s+"?' . $table_name . '"?#i', 'CREATE TEMPORARY TABLE ' . $table_name . '_temp', $row['sql']);
|
|
|
4c79b5 |
$statements[] = 'INSERT INTO ' . $table_name . '_temp SELECT * FROM ' . $table_name;
|
|
|
4c79b5 |
$statements[] = 'DROP TABLE ' . $table_name;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$new_table_cols = trim($matches[1]);
|
|
|
4c79b5 |
$old_table_cols = preg_split('/,(?![\s\w]+\))/m', $new_table_cols);
|
|
|
4c79b5 |
$column_list = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($old_table_cols as $key => $declaration)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$entities = preg_split('#\s+#', trim($declaration));
|
|
|
4c79b5 |
$column_list[] = $entities[0];
|
|
|
4c79b5 |
if ($entities[0] == $column_name)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$old_table_cols[$key] = $column_name . ' ' . $column_data['column_type_sql'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$columns = implode(',', $column_list);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// create a new table and fill it up. destroy the temp one
|
|
|
4c79b5 |
$statements[] = 'CREATE TABLE ' . $table_name . ' (' . implode(',', $old_table_cols) . ');';
|
|
|
4c79b5 |
$statements[] = 'INSERT INTO ' . $table_name . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . $table_name . '_temp;';
|
|
|
4c79b5 |
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$statements[] = 'commit';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->_sql_run_sql($statements);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
?>
|