mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-26 05:08:52 +00:00
- use proper OOP for db_tools
- made it all static. If one of you disagree, you are free to change it. I just couldn't think of a reason of having it otherwise... git-svn-id: file:///svn/phpbb/trunk@8319 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
parent
35f59ceb9a
commit
cbb4db46b2
2 changed files with 233 additions and 234 deletions
|
@ -25,12 +25,7 @@ if (!defined('IN_PHPBB'))
|
||||||
*/
|
*/
|
||||||
class phpbb_db_tools
|
class phpbb_db_tools
|
||||||
{
|
{
|
||||||
/**
|
public static $dbms_type_map = array(
|
||||||
* Current sql layer
|
|
||||||
*/
|
|
||||||
var $sql_layer = '';
|
|
||||||
|
|
||||||
var $dbms_type_map = array(
|
|
||||||
'mysql' => array(
|
'mysql' => array(
|
||||||
'INT:' => 'int(%d)',
|
'INT:' => 'int(%d)',
|
||||||
'BINT' => 'bigint(20)',
|
'BINT' => 'bigint(20)',
|
||||||
|
@ -240,22 +235,13 @@ class phpbb_db_tools
|
||||||
);
|
);
|
||||||
|
|
||||||
// A list of types being unsigned for better reference in some db's
|
// A list of types being unsigned for better reference in some db's
|
||||||
var $unsigned_types = array('UINT', 'UINT:', 'USINT', 'BOOL', 'TIMESTAMP');
|
public static $unsigned_types = array('UINT', 'UINT:', 'USINT', 'BOOL', 'TIMESTAMP');
|
||||||
var $supported_dbms = array('firebird', 'mssql', 'mysql', 'oracle', 'postgres', 'sqlite');
|
public static $supported_dbms = array('firebird', 'mssql', 'mysql', 'oracle', 'postgres', 'sqlite');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set this to true if you only want to return the 'to-be-executed' SQL statement(s) (as an array).
|
* Set this to true if you only want to return the 'to-be-executed' SQL statement(s) (as an array).
|
||||||
*/
|
*/
|
||||||
var $return_statements = false;
|
public static $return_statements = false;
|
||||||
|
|
||||||
/**
|
|
||||||
*/
|
|
||||||
function __construct(&$db)
|
|
||||||
{
|
|
||||||
$this->db = $db;
|
|
||||||
|
|
||||||
$this->sql_layer = $this->db->dbms_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle passed database update array.
|
* Handle passed database update array.
|
||||||
|
@ -275,9 +261,9 @@ class phpbb_db_tools
|
||||||
* {KEY/INDEX NAME} => array({COLUMN NAMES}),
|
* {KEY/INDEX NAME} => array({COLUMN NAMES}),
|
||||||
* )
|
* )
|
||||||
*
|
*
|
||||||
* For more information have a look at /develop/create_schema_files.php (only available through CVS)
|
* For more information have a look at /install/schemas/schema_data.php (only available through CVS)
|
||||||
*/
|
*/
|
||||||
function perform_schema_changes($schema_changes)
|
public static function perform_schema_changes($schema_changes)
|
||||||
{
|
{
|
||||||
if (empty($schema_changes))
|
if (empty($schema_changes))
|
||||||
{
|
{
|
||||||
|
@ -293,9 +279,9 @@ class phpbb_db_tools
|
||||||
{
|
{
|
||||||
foreach ($columns as $column_name => $column_data)
|
foreach ($columns as $column_name => $column_data)
|
||||||
{
|
{
|
||||||
$result = $this->sql_column_change($table, $column_name, $column_data);
|
$result = self::sql_column_change($table, $column_name, $column_data);
|
||||||
|
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
$statements = array_merge($statements, $result);
|
$statements = array_merge($statements, $result);
|
||||||
}
|
}
|
||||||
|
@ -311,11 +297,11 @@ class phpbb_db_tools
|
||||||
foreach ($columns as $column_name => $column_data)
|
foreach ($columns as $column_name => $column_data)
|
||||||
{
|
{
|
||||||
// Only add the column if it does not exist yet
|
// Only add the column if it does not exist yet
|
||||||
if (!$this->sql_column_exists($table, $column_name))
|
if (!self::sql_column_exists($table, $column_name))
|
||||||
{
|
{
|
||||||
$result = $this->sql_column_add($table, $column_name, $column_data);
|
$result = self::sql_column_add($table, $column_name, $column_data);
|
||||||
|
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
$statements = array_merge($statements, $result);
|
$statements = array_merge($statements, $result);
|
||||||
}
|
}
|
||||||
|
@ -331,9 +317,9 @@ class phpbb_db_tools
|
||||||
{
|
{
|
||||||
foreach ($indexes as $index_name)
|
foreach ($indexes as $index_name)
|
||||||
{
|
{
|
||||||
$result = $this->sql_index_drop($table, $index_name);
|
$result = self::sql_index_drop($table, $index_name);
|
||||||
|
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
$statements = array_merge($statements, $result);
|
$statements = array_merge($statements, $result);
|
||||||
}
|
}
|
||||||
|
@ -348,9 +334,9 @@ class phpbb_db_tools
|
||||||
{
|
{
|
||||||
foreach ($columns as $column)
|
foreach ($columns as $column)
|
||||||
{
|
{
|
||||||
$result = $this->sql_column_remove($table, $column);
|
$result = self::sql_column_remove($table, $column);
|
||||||
|
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
$statements = array_merge($statements, $result);
|
$statements = array_merge($statements, $result);
|
||||||
}
|
}
|
||||||
|
@ -363,9 +349,9 @@ class phpbb_db_tools
|
||||||
{
|
{
|
||||||
foreach ($schema_changes['add_primary_keys'] as $table => $columns)
|
foreach ($schema_changes['add_primary_keys'] as $table => $columns)
|
||||||
{
|
{
|
||||||
$result = $this->sql_create_primary_key($table, $columns);
|
$result = self::sql_create_primary_key($table, $columns);
|
||||||
|
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
$statements = array_merge($statements, $result);
|
$statements = array_merge($statements, $result);
|
||||||
}
|
}
|
||||||
|
@ -379,9 +365,9 @@ class phpbb_db_tools
|
||||||
{
|
{
|
||||||
foreach ($index_array as $index_name => $column)
|
foreach ($index_array as $index_name => $column)
|
||||||
{
|
{
|
||||||
$result = $this->sql_create_unique_index($table, $index_name, $column);
|
$result = self::sql_create_unique_index($table, $index_name, $column);
|
||||||
|
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
$statements = array_merge($statements, $result);
|
$statements = array_merge($statements, $result);
|
||||||
}
|
}
|
||||||
|
@ -396,9 +382,9 @@ class phpbb_db_tools
|
||||||
{
|
{
|
||||||
foreach ($index_array as $index_name => $column)
|
foreach ($index_array as $index_name => $column)
|
||||||
{
|
{
|
||||||
$result = $this->sql_create_index($table, $index_name, $column);
|
$result = self::sql_create_unique_index($table, $index_name, $column);
|
||||||
|
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
$statements = array_merge($statements, $result);
|
$statements = array_merge($statements, $result);
|
||||||
}
|
}
|
||||||
|
@ -406,7 +392,7 @@ class phpbb_db_tools
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
return $statements;
|
return $statements;
|
||||||
}
|
}
|
||||||
|
@ -416,25 +402,27 @@ class phpbb_db_tools
|
||||||
* Check if a specified column exist
|
* Check if a specified column exist
|
||||||
* @return bool True if column exists, else false
|
* @return bool True if column exists, else false
|
||||||
*/
|
*/
|
||||||
function sql_column_exists($table, $column_name)
|
public static function sql_column_exists($table, $column_name)
|
||||||
{
|
{
|
||||||
switch ($this->sql_layer)
|
global $db;
|
||||||
|
|
||||||
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
|
|
||||||
$sql = "SHOW COLUMNS FROM $table";
|
$sql = "SHOW COLUMNS FROM $table";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
// lower case just in case
|
// lower case just in case
|
||||||
if (strtolower($row['Field']) == $column_name)
|
if (strtolower($row['Field']) == $column_name)
|
||||||
{
|
{
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -446,17 +434,17 @@ class phpbb_db_tools
|
||||||
WHERE c.relname = '{$table}'
|
WHERE c.relname = '{$table}'
|
||||||
AND a.attnum > 0
|
AND a.attnum > 0
|
||||||
AND a.attrelid = c.oid";
|
AND a.attrelid = c.oid";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
// lower case just in case
|
// lower case just in case
|
||||||
if (strtolower($row['attname']) == $column_name)
|
if (strtolower($row['attname']) == $column_name)
|
||||||
{
|
{
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
@ -468,17 +456,17 @@ class phpbb_db_tools
|
||||||
FROM syscolumns c
|
FROM syscolumns c
|
||||||
LEFT JOIN sysobjects o ON c.id = o.id
|
LEFT JOIN sysobjects o ON c.id = o.id
|
||||||
WHERE o.name = '{$table}'";
|
WHERE o.name = '{$table}'";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
// lower case just in case
|
// lower case just in case
|
||||||
if (strtolower($row['name']) == $column_name)
|
if (strtolower($row['name']) == $column_name)
|
||||||
{
|
{
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -486,17 +474,17 @@ class phpbb_db_tools
|
||||||
$sql = "SELECT column_name
|
$sql = "SELECT column_name
|
||||||
FROM user_tab_columns
|
FROM user_tab_columns
|
||||||
WHERE table_name = '{$table}'";
|
WHERE table_name = '{$table}'";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
// lower case just in case
|
// lower case just in case
|
||||||
if (strtolower($row['column_name']) == $column_name)
|
if (strtolower($row['column_name']) == $column_name)
|
||||||
{
|
{
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -504,17 +492,17 @@ class phpbb_db_tools
|
||||||
$sql = "SELECT RDB\$FIELD_NAME as FNAME
|
$sql = "SELECT RDB\$FIELD_NAME as FNAME
|
||||||
FROM RDB\$RELATION_FIELDS
|
FROM RDB\$RELATION_FIELDS
|
||||||
WHERE RDB\$RELATION_NAME = '{$table}'";
|
WHERE RDB\$RELATION_NAME = '{$table}'";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
// lower case just in case
|
// lower case just in case
|
||||||
if (strtolower($row['fname']) == $column_name)
|
if (strtolower($row['fname']) == $column_name)
|
||||||
{
|
{
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -522,17 +510,17 @@ class phpbb_db_tools
|
||||||
$sql = "SELECT colname
|
$sql = "SELECT colname
|
||||||
FROM syscat.columns
|
FROM syscat.columns
|
||||||
WHERE tabname = '$table'";
|
WHERE tabname = '$table'";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
// lower case just in case
|
// lower case just in case
|
||||||
if (strtolower($row['colname']) == $column_name)
|
if (strtolower($row['colname']) == $column_name)
|
||||||
{
|
{
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -542,15 +530,15 @@ class phpbb_db_tools
|
||||||
FROM sqlite_master
|
FROM sqlite_master
|
||||||
WHERE type = 'table'
|
WHERE type = 'table'
|
||||||
AND name = '{$table}'";
|
AND name = '{$table}'";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
if (!$result)
|
if (!$result)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$row = $this->db->sql_fetchrow($result);
|
$row = $db->sql_fetchrow($result);
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
preg_match('#\((.*)\)#s', $row['sql'], $matches);
|
||||||
|
|
||||||
|
@ -579,27 +567,29 @@ class phpbb_db_tools
|
||||||
* Private method for performing sql statements (either execute them or return them)
|
* Private method for performing sql statements (either execute them or return them)
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function _sql_run_sql($statements)
|
private static function _sql_run_sql($statements)
|
||||||
{
|
{
|
||||||
if ($this->return_statements)
|
if (self::$return_statements)
|
||||||
{
|
{
|
||||||
return $statements;
|
return $statements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
global $db;
|
||||||
|
|
||||||
// We could add error handling here...
|
// We could add error handling here...
|
||||||
foreach ($statements as $sql)
|
foreach ($statements as $sql)
|
||||||
{
|
{
|
||||||
if ($sql === 'begin')
|
if ($sql === 'begin')
|
||||||
{
|
{
|
||||||
$this->db->sql_transaction('begin');
|
$db->sql_transaction('begin');
|
||||||
}
|
}
|
||||||
else if ($sql === 'commit')
|
else if ($sql === 'commit')
|
||||||
{
|
{
|
||||||
$this->db->sql_transaction('commit');
|
$db->sql_transaction('commit');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$this->db->sql_query($sql);
|
$db->sql_query($sql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -610,16 +600,18 @@ class phpbb_db_tools
|
||||||
* Function to prepare some column information for better usage
|
* Function to prepare some column information for better usage
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
function sql_prepare_column_data($table_name, $column_name, $column_data)
|
private static function sql_prepare_column_data($table_name, $column_name, $column_data)
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
// Get type
|
// Get type
|
||||||
if (strpos($column_data[0], ':') !== false)
|
if (strpos($column_data[0], ':') !== false)
|
||||||
{
|
{
|
||||||
list($orig_column_type, $column_length) = explode(':', $column_data[0]);
|
list($orig_column_type, $column_length) = explode(':', $column_data[0]);
|
||||||
|
|
||||||
if (!is_array($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']))
|
if (!is_array(self::$dbms_type_map[$db->dbms_type][$orig_column_type . ':']))
|
||||||
{
|
{
|
||||||
$column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'], $column_length);
|
$column_type = sprintf(self::$dbms_type_map[$db->dbms_type][$orig_column_type . ':'], $column_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
$orig_column_type .= ':';
|
$orig_column_type .= ':';
|
||||||
|
@ -627,20 +619,20 @@ class phpbb_db_tools
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$orig_column_type = $column_data[0];
|
$orig_column_type = $column_data[0];
|
||||||
$column_type = $this->dbms_type_map[$this->sql_layer][$column_data[0]];
|
$column_type = self::$dbms_type_map[$db->dbms_type][$column_data[0]];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjust default value if db-dependant specified
|
// Adjust default value if db-dependant specified
|
||||||
if (is_array($column_data[1]))
|
if (is_array($column_data[1]))
|
||||||
{
|
{
|
||||||
$column_data[1] = (isset($column_data[1][$this->sql_layer])) ? $column_data[1][$this->sql_layer] : $column_data[1]['default'];
|
$column_data[1] = (isset($column_data[1][$db->dbms_type])) ? $column_data[1][$db->dbms_type] : $column_data[1]['default'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql = '';
|
$sql = '';
|
||||||
|
|
||||||
$return_array = array();
|
$return_array = array();
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
$sql .= " {$column_type} ";
|
$sql .= " {$column_type} ";
|
||||||
|
@ -758,7 +750,7 @@ class phpbb_db_tools
|
||||||
$sql .= "DEFAULT {$default_val}";
|
$sql .= "DEFAULT {$default_val}";
|
||||||
|
|
||||||
// Unsigned? Then add a CHECK contraint
|
// Unsigned? Then add a CHECK contraint
|
||||||
if (in_array($orig_column_type, $this->unsigned_types))
|
if (in_array($orig_column_type, self::$unsigned_types))
|
||||||
{
|
{
|
||||||
$return_array['constraint'] = "CHECK ({$column_name} >= 0)";
|
$return_array['constraint'] = "CHECK ({$column_name} >= 0)";
|
||||||
$sql .= " CHECK ({$column_name} >= 0)";
|
$sql .= " CHECK ({$column_name} >= 0)";
|
||||||
|
@ -807,8 +799,10 @@ class phpbb_db_tools
|
||||||
return $return_array;
|
return $return_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sql_create_table($table_name, $table_data)
|
public static function sql_create_table($table_name, $table_data)
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
// holds the DDL for a column
|
// holds the DDL for a column
|
||||||
$columns = array();
|
$columns = array();
|
||||||
|
|
||||||
|
@ -826,7 +820,7 @@ class phpbb_db_tools
|
||||||
foreach ($table_data['COLUMNS'] as $column_name => $column_data)
|
foreach ($table_data['COLUMNS'] as $column_name => $column_data)
|
||||||
{
|
{
|
||||||
// here lies an array, filled with information compiled on the column's data
|
// here lies an array, filled with information compiled on the column's data
|
||||||
$prepared_column = $this->sql_prepare_column_data($table_name, $column_name, $column_data);
|
$prepared_column = self::sql_prepare_column_data($table_name, $column_name, $column_data);
|
||||||
|
|
||||||
// here we add the definition of the new column to the list of columns
|
// here we add the definition of the new column to the list of columns
|
||||||
$columns[] = "\t {$column_name} " . $prepared_column['column_type_sql'];
|
$columns[] = "\t {$column_name} " . $prepared_column['column_type_sql'];
|
||||||
|
@ -854,7 +848,7 @@ class phpbb_db_tools
|
||||||
// this makes up all the columns in the create table statement
|
// this makes up all the columns in the create table statement
|
||||||
$table_sql .= implode(",\n", $columns);
|
$table_sql .= implode(",\n", $columns);
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
$table_sql .= "\n);";
|
$table_sql .= "\n);";
|
||||||
|
@ -879,7 +873,7 @@ class phpbb_db_tools
|
||||||
$table_data['PRIMARY_KEY'] = array($table_data['PRIMARY_KEY']);
|
$table_data['PRIMARY_KEY'] = array($table_data['PRIMARY_KEY']);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
case 'postgres':
|
case 'postgres':
|
||||||
|
@ -890,7 +884,7 @@ class phpbb_db_tools
|
||||||
|
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
case 'mssql':
|
case 'mssql':
|
||||||
$primary_key_stmts = $this->sql_create_primary_key($table_name, $table_data['PRIMARY_KEY']);
|
$primary_key_stmts = self::sql_create_primary_key($table_name, $table_data['PRIMARY_KEY']);
|
||||||
foreach ($primary_key_stmts as $pk_stmt)
|
foreach ($primary_key_stmts as $pk_stmt)
|
||||||
{
|
{
|
||||||
$statements[] = $pk_stmt;
|
$statements[] = $pk_stmt;
|
||||||
|
@ -906,7 +900,7 @@ class phpbb_db_tools
|
||||||
|
|
||||||
|
|
||||||
// close the table
|
// close the table
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'mysql':
|
case 'mysql':
|
||||||
// make sure the table is in UTF-8 mode
|
// make sure the table is in UTF-8 mode
|
||||||
|
@ -975,7 +969,7 @@ class phpbb_db_tools
|
||||||
$key_data[1] = array($key_data[1]);
|
$key_data[1] = array($key_data[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$key_stmts = ($key_data[0] == 'UNIQUE') ? $this->sql_create_unique_index($table_name, $key_name, $key_data[1]) : $this->sql_create_index($table_name, $key_name, $key_data[1]);
|
$key_stmts = ($key_data[0] == 'UNIQUE') ? self::sql_create_unique_index($table_name, $key_name, $key_data[1]) : self::sql_create_index($table_name, $key_name, $key_data[1]);
|
||||||
foreach ($key_stmts as $key_stmt)
|
foreach ($key_stmts as $key_stmt)
|
||||||
{
|
{
|
||||||
$statements[] = $key_stmt;
|
$statements[] = $key_stmt;
|
||||||
|
@ -983,18 +977,18 @@ class phpbb_db_tools
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_sql_run_sql($statements);
|
return self::_sql_run_sql($statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add new column
|
* Add new column
|
||||||
*/
|
*/
|
||||||
function sql_column_add($table_name, $column_name, $column_data)
|
public static function sql_column_add($table_name, $column_name, $column_data)
|
||||||
{
|
{
|
||||||
$column_data = $this->sql_prepare_column_data($table_name, $column_name, $column_data);
|
$column_data = self::sql_prepare_column_data($table_name, $column_name, $column_data);
|
||||||
$statements = array();
|
$statements = array();
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
$statements[] = 'ALTER TABLE "' . $table_name . '" ADD "' . $column_name . '" ' . $column_data['column_type_sql'];
|
$statements[] = 'ALTER TABLE "' . $table_name . '" ADD "' . $column_name . '" ' . $column_data['column_type_sql'];
|
||||||
|
@ -1021,22 +1015,22 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
if (version_compare(sqlite_libversion(), '3.0') == -1)
|
global $db;
|
||||||
{
|
|
||||||
$sql = "SELECT sql
|
$sql = "SELECT sql
|
||||||
FROM sqlite_master
|
FROM sqlite_master
|
||||||
WHERE type = 'table'
|
WHERE type = 'table'
|
||||||
AND name = '{$table_name}'
|
AND name = '{$table_name}'
|
||||||
ORDER BY type DESC, name;";
|
ORDER BY type DESC, name;";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
if (!$result)
|
if (!$result)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$row = $this->db->sql_fetchrow($result);
|
$row = $db->sql_fetchrow($result);
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
$statements[] = 'begin';
|
$statements[] = 'begin';
|
||||||
|
|
||||||
|
@ -1071,25 +1065,22 @@ class phpbb_db_tools
|
||||||
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
||||||
|
|
||||||
$statements[] = 'commit';
|
$statements[] = 'commit';
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD ' . $column_name . ' [' . $column_data['column_type_sql'] . ']';
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_sql_run_sql($statements);
|
return self::_sql_run_sql($statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drop column
|
* Drop column
|
||||||
*/
|
*/
|
||||||
function sql_column_remove($table_name, $column_name)
|
public static function sql_column_remove($table_name, $column_name)
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
$statements = array();
|
$statements = array();
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
$statements[] = 'ALTER TABLE "' . $table_name . '" DROP "' . $column_name . '"';
|
$statements[] = 'ALTER TABLE "' . $table_name . '" DROP "' . $column_name . '"';
|
||||||
|
@ -1116,22 +1107,22 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
if (version_compare(sqlite_libversion(), '3.0') == -1)
|
global $db;
|
||||||
{
|
|
||||||
$sql = "SELECT sql
|
$sql = "SELECT sql
|
||||||
FROM sqlite_master
|
FROM sqlite_master
|
||||||
WHERE type = 'table'
|
WHERE type = 'table'
|
||||||
AND name = '{$table_name}'
|
AND name = '{$table_name}'
|
||||||
ORDER BY type DESC, name;";
|
ORDER BY type DESC, name;";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
if (!$result)
|
if (!$result)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$row = $this->db->sql_fetchrow($result);
|
$row = $db->sql_fetchrow($result);
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
$statements[] = 'begin';
|
$statements[] = 'begin';
|
||||||
|
|
||||||
|
@ -1166,25 +1157,22 @@ class phpbb_db_tools
|
||||||
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
$statements[] = 'DROP TABLE ' . $table_name . '_temp';
|
||||||
|
|
||||||
$statements[] = 'commit';
|
$statements[] = 'commit';
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$statements[] = 'ALTER TABLE ' . $table_name . ' DROP COLUMN ' . $column_name;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_sql_run_sql($statements);
|
return self::_sql_run_sql($statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drop Index
|
* Drop Index
|
||||||
*/
|
*/
|
||||||
function sql_index_drop($table_name, $index_name)
|
public static function sql_index_drop($table_name, $index_name)
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
$statements = array();
|
$statements = array();
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'mssql':
|
case 'mssql':
|
||||||
$statements[] = 'DROP INDEX ' . $table_name . '.' . $index_name;
|
$statements[] = 'DROP INDEX ' . $table_name . '.' . $index_name;
|
||||||
|
@ -1203,17 +1191,19 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_sql_run_sql($statements);
|
return self::_sql_run_sql($statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add primary key
|
* Add primary key
|
||||||
*/
|
*/
|
||||||
function sql_create_primary_key($table_name, $column)
|
public static function sql_create_primary_key($table_name, $column)
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
$statements = array();
|
$statements = array();
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
case 'postgres':
|
case 'postgres':
|
||||||
|
@ -1236,20 +1226,22 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
|
global $db;
|
||||||
|
|
||||||
$sql = "SELECT sql
|
$sql = "SELECT sql
|
||||||
FROM sqlite_master
|
FROM sqlite_master
|
||||||
WHERE type = 'table'
|
WHERE type = 'table'
|
||||||
AND name = '{$table_name}'
|
AND name = '{$table_name}'
|
||||||
ORDER BY type DESC, name;";
|
ORDER BY type DESC, name;";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
if (!$result)
|
if (!$result)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$row = $this->db->sql_fetchrow($result);
|
$row = $db->sql_fetchrow($result);
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
$statements[] = 'begin';
|
$statements[] = 'begin';
|
||||||
|
|
||||||
|
@ -1285,17 +1277,19 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_sql_run_sql($statements);
|
return self::_sql_run_sql($statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add unique index
|
* Add unique index
|
||||||
*/
|
*/
|
||||||
function sql_create_unique_index($table_name, $index_name, $column)
|
public static function sql_create_unique_index($table_name, $index_name, $column)
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
$statements = array();
|
$statements = array();
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
case 'postgres':
|
case 'postgres':
|
||||||
|
@ -1314,17 +1308,19 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_sql_run_sql($statements);
|
return self::_sql_run_sql($statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add index
|
* Add index
|
||||||
*/
|
*/
|
||||||
function sql_create_index($table_name, $index_name, $column)
|
public static function sql_create_index($table_name, $index_name, $column)
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
$statements = array();
|
$statements = array();
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
case 'postgres':
|
case 'postgres':
|
||||||
|
@ -1343,7 +1339,7 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_sql_run_sql($statements);
|
return self::_sql_run_sql($statements);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1352,26 +1348,28 @@ class phpbb_db_tools
|
||||||
* * UNIQUE indices
|
* * UNIQUE indices
|
||||||
* * PRIMARY keys
|
* * PRIMARY keys
|
||||||
*/
|
*/
|
||||||
function sql_list_index($table_name)
|
public static function sql_list_index($table_name)
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
$index_array = array();
|
$index_array = array();
|
||||||
|
|
||||||
if ($this->sql_layer == 'mssql')
|
if ($db->dbms_type == 'mssql')
|
||||||
{
|
{
|
||||||
$sql = "EXEC sp_statistics '$table_name'";
|
$sql = "EXEC sp_statistics '$table_name'";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
if ($row['TYPE'] == 3)
|
if ($row['TYPE'] == 3)
|
||||||
{
|
{
|
||||||
$index_array[] = $row['INDEX_NAME'];
|
$index_array[] = $row['INDEX_NAME'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
$sql = "SELECT LOWER(RDB\$INDEX_NAME) as index_name
|
$sql = "SELECT LOWER(RDB\$INDEX_NAME) as index_name
|
||||||
|
@ -1419,15 +1417,15 @@ class phpbb_db_tools
|
||||||
$col = 'name';
|
$col = 'name';
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
if ($this->sql_layer == 'mysql' && !$row['Non_unique'])
|
if ($db->dbms_type == 'mysql' && !$row['Non_unique'])
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
case 'oracle':
|
case 'oracle':
|
||||||
|
@ -1440,7 +1438,7 @@ class phpbb_db_tools
|
||||||
|
|
||||||
$index_array[] = $row[$col];
|
$index_array[] = $row[$col];
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_map('strtolower', $index_array);
|
return array_map('strtolower', $index_array);
|
||||||
|
@ -1449,12 +1447,12 @@ class phpbb_db_tools
|
||||||
/**
|
/**
|
||||||
* Change column type (not name!)
|
* Change column type (not name!)
|
||||||
*/
|
*/
|
||||||
function sql_column_change($table_name, $column_name, $column_data)
|
public static function sql_column_change($table_name, $column_name, $column_data)
|
||||||
{
|
{
|
||||||
$column_data = $this->sql_prepare_column_data($table_name, $column_name, $column_data);
|
$column_data = self::sql_prepare_column_data($table_name, $column_name, $column_data);
|
||||||
$statements = array();
|
$statements = array();
|
||||||
|
|
||||||
switch ($this->sql_layer)
|
switch ($db->dbms_type)
|
||||||
{
|
{
|
||||||
case 'firebird':
|
case 'firebird':
|
||||||
// Change type...
|
// Change type...
|
||||||
|
@ -1503,6 +1501,8 @@ class phpbb_db_tools
|
||||||
// we don't want to double up on constraints if we change different number data types
|
// we don't want to double up on constraints if we change different number data types
|
||||||
if (isset($column_data['constraint']))
|
if (isset($column_data['constraint']))
|
||||||
{
|
{
|
||||||
|
global $db;
|
||||||
|
|
||||||
$constraint_sql = "SELECT consrc as constraint_data
|
$constraint_sql = "SELECT consrc as constraint_data
|
||||||
FROM pg_constraint, pg_class bc
|
FROM pg_constraint, pg_class bc
|
||||||
WHERE conrelid = bc.oid
|
WHERE conrelid = bc.oid
|
||||||
|
@ -1518,8 +1518,8 @@ class phpbb_db_tools
|
||||||
|
|
||||||
$constraint_exists = false;
|
$constraint_exists = false;
|
||||||
|
|
||||||
$result = $this->db->sql_query($constraint_sql);
|
$result = $db->sql_query($constraint_sql);
|
||||||
while ($row = $this->db->sql_fetchrow($result))
|
while ($row = $db->sql_fetchrow($result))
|
||||||
{
|
{
|
||||||
if (trim($row['constraint_data']) == trim($column_data['constraint']))
|
if (trim($row['constraint_data']) == trim($column_data['constraint']))
|
||||||
{
|
{
|
||||||
|
@ -1527,7 +1527,7 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
if (!$constraint_exists)
|
if (!$constraint_exists)
|
||||||
{
|
{
|
||||||
|
@ -1541,21 +1541,22 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'sqlite':
|
case 'sqlite':
|
||||||
|
global $db;
|
||||||
|
|
||||||
$sql = "SELECT sql
|
$sql = "SELECT sql
|
||||||
FROM sqlite_master
|
FROM sqlite_master
|
||||||
WHERE type = 'table'
|
WHERE type = 'table'
|
||||||
AND name = '{$table_name}'
|
AND name = '{$table_name}'
|
||||||
ORDER BY type DESC, name;";
|
ORDER BY type DESC, name;";
|
||||||
$result = $this->db->sql_query($sql);
|
$result = $db->sql_query($sql);
|
||||||
|
|
||||||
if (!$result)
|
if (!$result)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$row = $this->db->sql_fetchrow($result);
|
$row = $db->sql_fetchrow($result);
|
||||||
$this->db->sql_freeresult($result);
|
$db->sql_freeresult($result);
|
||||||
|
|
||||||
$statements[] = 'begin';
|
$statements[] = 'begin';
|
||||||
|
|
||||||
|
@ -1592,7 +1593,7 @@ class phpbb_db_tools
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_sql_run_sql($statements);
|
return self::_sql_run_sql($statements);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1170,17 +1170,15 @@ class install_install extends module
|
||||||
include($phpbb_root_path . 'includes/db/db_tools.php');
|
include($phpbb_root_path . 'includes/db/db_tools.php');
|
||||||
include($phpbb_root_path . 'install/schemas/schema_data.php');
|
include($phpbb_root_path . 'install/schemas/schema_data.php');
|
||||||
|
|
||||||
$tools = new phpbb_db_tools($db);
|
|
||||||
|
|
||||||
// we must do this so that we can handle the errors
|
// we must do this so that we can handle the errors
|
||||||
$tools->return_statements = true;
|
phpbb_db_tools::$return_statements = true;
|
||||||
|
|
||||||
foreach ($schema_data as $table_name => $table_data)
|
foreach ($schema_data as $table_name => $table_data)
|
||||||
{
|
{
|
||||||
// Change prefix
|
// Change prefix
|
||||||
$table_name = preg_replace('#phpbb_#i', $data['table_prefix'], $table_name);
|
$table_name = preg_replace('#phpbb_#i', $data['table_prefix'], $table_name);
|
||||||
|
|
||||||
$statements = $tools->sql_create_table($table_name, $table_data);
|
$statements = phpbb_db_tools::sql_create_table($table_name, $table_data);
|
||||||
|
|
||||||
foreach ($statements as $sql)
|
foreach ($statements as $sql)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue