aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/db/tools
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/phpbb/db/tools')
-rw-r--r--phpBB/phpbb/db/tools/mssql.php46
-rw-r--r--phpBB/phpbb/db/tools/postgres.php2
-rw-r--r--phpBB/phpbb/db/tools/tools.php57
3 files changed, 85 insertions, 20 deletions
diff --git a/phpBB/phpbb/db/tools/mssql.php b/phpBB/phpbb/db/tools/mssql.php
index a132832005..d31aa2ba0b 100644
--- a/phpBB/phpbb/db/tools/mssql.php
+++ b/phpBB/phpbb/db/tools/mssql.php
@@ -477,7 +477,7 @@ class mssql extends tools
{
$statements = array();
- $statements[] = 'DROP INDEX ' . $table_name . '.' . $index_name;
+ $statements[] = 'DROP INDEX [' . $table_name . '].[' . $index_name . ']';
return $this->_sql_run_sql($statements);
}
@@ -524,7 +524,10 @@ class mssql extends tools
{
$statements = array();
- $this->check_index_name_length($table_name, $index_name);
+ if ($this->is_sql_server_2000())
+ {
+ $this->check_index_name_length($table_name, $index_name);
+ }
$statements[] = 'CREATE UNIQUE INDEX [' . $index_name . '] ON [' . $table_name . ']([' . implode('], [', $column) . '])';
@@ -538,7 +541,10 @@ class mssql extends tools
{
$statements = array();
- $this->check_index_name_length($table_name, $index_name);
+ if ($this->is_sql_server_2000())
+ {
+ $this->check_index_name_length($table_name, $index_name);
+ }
// remove index length
$column = preg_replace('#:.*$#', '', $column);
@@ -601,7 +607,7 @@ class mssql extends tools
// Change the column
$statements[] = 'ALTER TABLE [' . $table_name . '] ALTER COLUMN [' . $column_name . '] ' . $column_data['column_type_sql'];
- if (!empty($column_data['default']))
+ if (!empty($column_data['default']) && !$this->mssql_is_column_identity($table_name, $column_name))
{
// Add new default value constraint
$statements[] = 'ALTER TABLE [' . $table_name . '] ADD CONSTRAINT [DF_' . $table_name . '_' . $column_name . '_1] ' . $column_data['default'] . ' FOR [' . $column_name . ']';
@@ -679,6 +685,37 @@ class mssql extends tools
}
/**
+ * Checks to see if column is an identity column
+ *
+ * Identity columns cannot have defaults set for them.
+ *
+ * @param string $table_name
+ * @param string $column_name
+ * @return bool true if identity, false if not
+ */
+ protected function mssql_is_column_identity($table_name, $column_name)
+ {
+ if ($this->mssql_is_sql_server_2000())
+ {
+ // http://msdn.microsoft.com/en-us/library/aa175912%28v=sql.80%29.aspx
+ // Deprecated in SQL Server 2005
+ $sql = "SELECT COLUMNPROPERTY(object_id('{$table_name}'), '{$column_name}', 'IsIdentity') AS is_identity";
+ }
+ else
+ {
+ $sql = "SELECT is_identity FROM sys.columns
+ WHERE object_id = object_id('{$table_name}')
+ AND name = '{$column_name}'";
+ }
+
+ $result = $this->db->sql_query($sql);
+ $is_identity = $this->db->sql_fetchfield('is_identity');
+ $this->db->sql_freeresult($result);
+
+ return (bool) $is_identity;
+ }
+
+ /**
* Get a list with existing indexes for the column
*
* @param string $table_name
@@ -717,6 +754,7 @@ class mssql extends tools
AND cols.object_id = ix.object_id
WHERE ix.object_id = object_id('{$table_name}')
AND cols.name = '{$column_name}'
+ AND ix.is_primary_key = 0
AND ix.is_unique = " . ($unique ? '1' : '0');
}
diff --git a/phpBB/phpbb/db/tools/postgres.php b/phpBB/phpbb/db/tools/postgres.php
index e2a4e668a6..077d6e06f9 100644
--- a/phpBB/phpbb/db/tools/postgres.php
+++ b/phpBB/phpbb/db/tools/postgres.php
@@ -448,7 +448,7 @@ class postgres extends tools
// We don't even care about storing the results. We already know the answer if we get rows back.
if ($this->db->sql_fetchrow($result))
{
- $statements[] = "DROP SEQUENCE {$table_name}_seq;\n";
+ $statements[] = "DROP SEQUENCE IF EXISTS {$table_name}_seq;\n";
}
$this->db->sql_freeresult($result);
diff --git a/phpBB/phpbb/db/tools/tools.php b/phpBB/phpbb/db/tools/tools.php
index 9273d69cd6..2f891e43d5 100644
--- a/phpBB/phpbb/db/tools/tools.php
+++ b/phpBB/phpbb/db/tools/tools.php
@@ -941,16 +941,19 @@ class tools implements tools_interface
continue;
}
- // These DBMS prefix index name with the table name
switch ($this->sql_layer)
{
+ // These DBMS prefix index name with the table name
case 'oracle':
case 'sqlite3':
- $row[$col] = substr($row[$col], strlen($table_name) + 1);
+ $new_index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name, false);
+ break;
+ default:
+ $new_index_name = $this->check_index_name_length($table_name, $index_name, false);
break;
}
- if (strtolower($row[$col]) == strtolower($index_name))
+ if (strtolower($row[$col]) == strtolower($new_index_name))
{
$this->db->sql_freeresult($result);
return true;
@@ -1359,12 +1362,14 @@ class tools implements tools_interface
{
case 'mysql_40':
case 'mysql_41':
+ $index_name = $this->check_index_name_length($table_name, $index_name, false);
$statements[] = 'DROP INDEX ' . $index_name . ' ON ' . $table_name;
break;
case 'oracle':
case 'sqlite3':
- $statements[] = 'DROP INDEX ' . $table_name . '_' . $index_name;
+ $index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name, false);
+ $statements[] = 'DROP INDEX ' . $index_name;
break;
}
@@ -1487,17 +1492,17 @@ class tools implements tools_interface
{
$statements = array();
- $this->check_index_name_length($table_name, $index_name);
-
switch ($this->sql_layer)
{
case 'oracle':
case 'sqlite3':
- $statements[] = 'CREATE UNIQUE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
+ $index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name);
+ $statements[] = 'CREATE UNIQUE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
break;
case 'mysql_40':
case 'mysql_41':
+ $index_name = $this->check_index_name_length($table_name, $index_name);
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD UNIQUE INDEX ' . $index_name . '(' . implode(', ', $column) . ')';
break;
}
@@ -1512,8 +1517,6 @@ class tools implements tools_interface
{
$statements = array();
- $this->check_index_name_length($table_name, $index_name);
-
// remove index length unless MySQL4
if ('mysql_40' != $this->sql_layer)
{
@@ -1524,7 +1527,8 @@ class tools implements tools_interface
{
case 'oracle':
case 'sqlite3':
- $statements[] = 'CREATE INDEX ' . $table_name . '_' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
+ $index_name = $this->check_index_name_length($table_name, $table_name . '_' . $index_name);
+ $statements[] = 'CREATE INDEX ' . $index_name . ' ON ' . $table_name . '(' . implode(', ', $column) . ')';
break;
case 'mysql_40':
@@ -1539,6 +1543,7 @@ class tools implements tools_interface
}
// no break
case 'mysql_41':
+ $index_name = $this->check_index_name_length($table_name, $index_name);
$statements[] = 'ALTER TABLE ' . $table_name . ' ADD INDEX ' . $index_name . ' (' . implode(', ', $column) . ')';
break;
}
@@ -1551,15 +1556,37 @@ class tools implements tools_interface
*
* @param string $table_name
* @param string $index_name
+ * @param bool $throw_error
+ * @return string The index name, shortened if too long
*/
- protected function check_index_name_length($table_name, $index_name)
+ protected function check_index_name_length($table_name, $index_name, $throw_error = true)
{
- $table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config)
- if (strlen($table_name . $index_name) - strlen($table_prefix) > 24)
+ if (strlen($index_name) > 30)
{
- $max_length = strlen($table_prefix) + 24;
- trigger_error("Index name '{$table_name}_$index_name' on table '$table_name' is too long. The maximum is $max_length characters.", E_USER_ERROR);
+ // Try removing the table prefix if it's at the beginning
+ $table_prefix = substr(CONFIG_TABLE, 0, -6); // strlen(config)
+ if (strpos($index_name, $table_prefix) === 0)
+ {
+ $index_name = substr($index_name, strlen($table_prefix));
+ return $this->check_index_name_length($table_name, $index_name, $throw_error);
+ }
+
+ // Try removing the remaining suffix part of table name then
+ $table_suffix = substr($table_name, strlen($table_prefix));
+ if (strpos($index_name, $table_suffix) === 0)
+ {
+ // Remove the suffix and underscore separator between table_name and index_name
+ $index_name = substr($index_name, strlen($table_suffix) + 1);
+ return $this->check_index_name_length($table_name, $index_name, $throw_error);
+ }
+
+ if ($throw_error)
+ {
+ trigger_error("Index name '$index_name' on table '$table_name' is too long. The maximum is 30 characters.", E_USER_ERROR);
+ }
}
+
+ return $index_name;
}
/**