Blame Identity/Webenv/phpBB/3.0.4/includes/db/db_tools.php

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