1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-06-18 16:11:06 +02:00

- better PostgreSQL support for ALTER TABLE

- stress tested sql_list_index(), found some bugs in it :/


git-svn-id: file:///svn/phpbb/trunk@7855 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
David M
2007-07-09 22:39:50 +00:00
parent c0ea9006c5
commit 84a16e2ac6

View File

@ -937,6 +937,8 @@ function prepare_column_data($dbms, $column_data)
$sql = ''; $sql = '';
$return_array = array();
switch ($dbms) switch ($dbms)
{ {
case 'firebird': case 'firebird':
@ -1011,14 +1013,23 @@ function prepare_column_data($dbms, $column_data)
break; break;
case 'postgres': case 'postgres':
$return_array['column_type'] = $column_type;
$return_array['null'] = 'NOT NULL';
if (!is_null($column_data[1]))
{
$return_array['default'] = $column_data[1];
}
$sql .= " {$column_type} "; $sql .= " {$column_type} ";
$sql .= 'NOT NULL'; $sql .= 'NOT NULL ';
$sql .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}' " : ''; $sql .= (!is_null($column_data[1])) ? "DEFAULT '{$column_data[1]}' " : '';
// Unsigned? Then add a CHECK contraint // Unsigned? Then add a CHECK contraint
if (in_array($orig_column_type, $unsigned_types)) if (in_array($orig_column_type, $unsigned_types))
{ {
$return_array['constraint'] = "CHECK ({$column_name} >= 0)";
$sql .= " CHECK ({$column_name} >= 0)"; $sql .= " CHECK ({$column_name} >= 0)";
} }
break; break;
@ -1040,9 +1051,9 @@ function prepare_column_data($dbms, $column_data)
break; break;
} }
return array( $return_array['column_type_sql'] = $sql;
'column_type_sql' => $sql,
); return $return_array;
} }
/** /**
@ -1476,6 +1487,16 @@ function sql_list_index($dbms, $table_name)
continue; continue;
} }
switch ($dbms)
{
case 'firebird':
case 'oracle':
case 'postgres':
case 'sqlite':
$row[$col] = substr($row[$col], strlen($table_name) + 1);
break;
}
$index_array[] = $row[$col]; $index_array[] = $row[$col];
} }
$db->sql_freeresult($result); $db->sql_freeresult($result);
@ -1519,16 +1540,32 @@ function sql_column_change($dbms, $table_name, $column_name, $column_data)
break; break;
case 'postgres': case 'postgres':
$default_pos = strpos($column_data['column_type_sql'], ' DEFAULT'); $sql = 'ALTER TABLE ' . $table_name . ' ';
if ($default_pos === false) $sql_array = array();
$sql_array[] = 'ALTER COLUMN ' . $column_name . ' TYPE ' . $column_data['column_type'];
if ($column_data['null'] == 'NOT NULL')
{ {
$sql = 'ALTER TABLE ' . $table_name . ' ALTER COLUMN ' . $column_name . ' TYPE ' . $column_data['column_type_sql']; $sql_array[] = 'ALTER COLUMN ' . $column_name . ' SET NOT NULL';
} }
else else
{ {
$sql = 'ALTER TABLE ' . $table_name . ' ALTER COLUMN ' . $column_name . ' TYPE ' . substr($column_data['column_type_sql'], 0, $default_pos) . ', ALTER COLUMN ' . $column_name . ' SET ' . substr($column_data['column_type_sql'], $default_pos + 1); $sql_array[] = 'ALTER COLUMN ' . $column_name . ' DROP NOT NULL';
} }
if (isset($column_data['default']))
{
$sql_array[] = 'ALTER COLUMN ' . $column_name . " SET DEFAULT '" . $column_data['default'] . "'";
}
if (isset($column_data['constraint']))
{
$sql_array[] = 'ALTER COLUMN ' . $column_name . " ADD '" . $column_data['constraint'] . "'";
}
$sql .= implode(', ', $sql_array);
_sql($sql, $errored, $error_ary); _sql($sql, $errored, $error_ary);
break; break;