forked from GNUsocial/gnu-social
[DATABASE] Always quote identifiers
The code used to operate under the assumption that MariaDB doesn't support quoting identifiers. Not only is that not exactly true, but MariaDB has reserved keywords that cannot be used as table or column names unquoted.
This commit is contained in:
@@ -17,11 +17,11 @@
|
||||
/**
|
||||
* Database schema for PostgreSQL
|
||||
*
|
||||
* @category Database
|
||||
* @package GNUsocial
|
||||
* @author Evan Prodromou <evan@status.net>
|
||||
* @author Brenda Wallace <shiny@cpan.org>
|
||||
* @author Brion Vibber <brion@status.net>
|
||||
* @category Database
|
||||
* @package GNUsocial
|
||||
* @author Evan Prodromou <evan@status.net>
|
||||
* @author Brenda Wallace <shiny@cpan.org>
|
||||
* @author Brion Vibber <brion@status.net>
|
||||
* @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
|
||||
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||
*/
|
||||
@@ -52,7 +52,6 @@ class PgsqlSchema extends Schema
|
||||
* @return array tabledef for that table.
|
||||
* @throws SchemaTableMissingException
|
||||
*/
|
||||
|
||||
public function getTableDef($table)
|
||||
{
|
||||
$def = [];
|
||||
@@ -68,7 +67,6 @@ class PgsqlSchema extends Schema
|
||||
$orderedFields = [];
|
||||
|
||||
foreach ($columns as $row) {
|
||||
|
||||
$name = $row['column_name'];
|
||||
$orderedFields[$row['ordinal_position']] = $name;
|
||||
|
||||
@@ -146,7 +144,7 @@ class PgsqlSchema extends Schema
|
||||
// name hack -- is this reliable?
|
||||
if ($keyName == "{$table}_pkey") {
|
||||
$def['primary key'] = $cols;
|
||||
} else if (preg_match("/^{$table}_(.*)_fkey$/", $keyName, $matches)) {
|
||||
} elseif (preg_match("/^{$table}_(.*)_fkey$/", $keyName, $matches)) {
|
||||
$fkey = $this->getForeignKeyInfo($table, $keyName);
|
||||
$colMap = array_combine($cols, $fkey['col_names']);
|
||||
$def['foreign keys'][$keyName] = [$fkey['table_name'], $colMap];
|
||||
@@ -166,7 +164,7 @@ class PgsqlSchema extends Schema
|
||||
* @return array of arrays
|
||||
* @throws PEAR_Exception
|
||||
*/
|
||||
function fetchMetaInfo($table, $infoTable, $orderBy = null)
|
||||
public function fetchMetaInfo($table, $infoTable, $orderBy = null)
|
||||
{
|
||||
$query = "SELECT * FROM information_schema.%s " .
|
||||
"WHERE table_name='%s'";
|
||||
@@ -183,7 +181,7 @@ class PgsqlSchema extends Schema
|
||||
* @return array of arrays
|
||||
* @throws PEAR_Exception
|
||||
*/
|
||||
function getIndexInfo($table)
|
||||
public function getIndexInfo($table)
|
||||
{
|
||||
$query = 'SELECT ' .
|
||||
'(SELECT relname FROM pg_class WHERE oid=indexrelid) AS key_name, ' .
|
||||
@@ -202,7 +200,7 @@ class PgsqlSchema extends Schema
|
||||
* @return array array of rows with keys: fkey_name, table_name, table_id, col_names (array of strings)
|
||||
* @throws PEAR_Exception
|
||||
*/
|
||||
function getForeignKeyInfo($table, $constraint_name)
|
||||
public function getForeignKeyInfo($table, $constraint_name)
|
||||
{
|
||||
// In a sane world, it'd be easier to query the column names directly.
|
||||
// But it's pretty hard to work with arrays such as col_indexes in direct SQL here.
|
||||
@@ -234,7 +232,7 @@ class PgsqlSchema extends Schema
|
||||
* @return array of strings
|
||||
* @throws PEAR_Exception
|
||||
*/
|
||||
function getTableColumnNames($table_id, $col_indexes)
|
||||
public function getTableColumnNames($table_id, $col_indexes)
|
||||
{
|
||||
$indexes = array_map('intval', explode(' ', $col_indexes));
|
||||
$query = 'SELECT attnum AS col_index, attname AS col_name ' .
|
||||
@@ -284,7 +282,7 @@ class PgsqlSchema extends Schema
|
||||
* @return string correct SQL for that column
|
||||
*/
|
||||
|
||||
function columnSql(array $cd)
|
||||
public function columnSql(array $cd)
|
||||
{
|
||||
$line = [];
|
||||
$line[] = parent::columnSql($cd);
|
||||
@@ -311,7 +309,7 @@ class PgsqlSchema extends Schema
|
||||
* @param array $old previous column definition as found in DB
|
||||
* @param array $cd current column definition
|
||||
*/
|
||||
function appendAlterModifyColumn(array &$phrase, $columnName, array $old, array $cd)
|
||||
public function appendAlterModifyColumn(array &$phrase, $columnName, array $old, array $cd)
|
||||
{
|
||||
$prefix = 'ALTER COLUMN ' . $this->quoteIdentifier($columnName) . ' ';
|
||||
|
||||
@@ -323,13 +321,13 @@ class PgsqlSchema extends Schema
|
||||
|
||||
if (!empty($old['not null']) && empty($cd['not null'])) {
|
||||
$phrase[] = $prefix . 'DROP NOT NULL';
|
||||
} else if (empty($old['not null']) && !empty($cd['not null'])) {
|
||||
} elseif (empty($old['not null']) && !empty($cd['not null'])) {
|
||||
$phrase[] = $prefix . 'SET NOT NULL';
|
||||
}
|
||||
|
||||
if (isset($old['default']) && !isset($cd['default'])) {
|
||||
$phrase[] = $prefix . 'DROP DEFAULT';
|
||||
} else if (!isset($old['default']) && isset($cd['default'])) {
|
||||
} elseif (!isset($old['default']) && isset($cd['default'])) {
|
||||
$phrase[] = $prefix . 'SET DEFAULT ' . $this->quoteDefaultValue($cd);
|
||||
}
|
||||
}
|
||||
@@ -342,23 +340,12 @@ class PgsqlSchema extends Schema
|
||||
* @param string $table
|
||||
* @param string $name
|
||||
*/
|
||||
function appendDropIndex(array &$statements, $table, $name)
|
||||
public function appendDropIndex(array &$statements, $table, $name)
|
||||
{
|
||||
$statements[] = "DROP INDEX $name";
|
||||
}
|
||||
|
||||
/**
|
||||
* Quote a db/table/column identifier if necessary.
|
||||
*
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
function quoteIdentifier($name)
|
||||
{
|
||||
return $this->conn->quoteIdentifier($name);
|
||||
}
|
||||
|
||||
function mapType($column)
|
||||
public function mapType($column)
|
||||
{
|
||||
$map = [
|
||||
'serial' => 'bigserial', // FIXME: creates the wrong name for the sequence for some internal sequence-lookup function, so better fix this to do the real 'create sequence' dance.
|
||||
@@ -377,7 +364,7 @@ class PgsqlSchema extends Schema
|
||||
$size = $column['size'];
|
||||
if ($size == 'small') {
|
||||
return 'int2';
|
||||
} else if ($size == 'big') {
|
||||
} elseif ($size == 'big') {
|
||||
return 'int8';
|
||||
}
|
||||
}
|
||||
@@ -388,7 +375,7 @@ class PgsqlSchema extends Schema
|
||||
}
|
||||
|
||||
// @fixme need name... :P
|
||||
function typeAndSize($column)
|
||||
public function typeAndSize($column)
|
||||
{
|
||||
if ($column['type'] == 'enum') {
|
||||
$vals = array_map([$this, 'quote'], $column['enum']);
|
||||
@@ -408,7 +395,7 @@ class PgsqlSchema extends Schema
|
||||
* @param array $tableDef
|
||||
* @return array
|
||||
*/
|
||||
function filterDef(array $tableDef)
|
||||
public function filterDef(array $tableDef)
|
||||
{
|
||||
foreach ($tableDef['fields'] as $name => &$col) {
|
||||
// No convenient support for field descriptions
|
||||
@@ -443,7 +430,7 @@ class PgsqlSchema extends Schema
|
||||
* @param array $def
|
||||
* @return array
|
||||
*/
|
||||
function filterKeyDef(array $def)
|
||||
public function filterKeyDef(array $def)
|
||||
{
|
||||
// PostgreSQL doesn't like prefix lengths specified on keys...?
|
||||
foreach ($def as $i => $item) {
|
||||
|
Reference in New Issue
Block a user