Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix MariaDB reserved keywords issue #1106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Resources/contao/drivers/DC_Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3143,12 +3143,12 @@ protected function save($varValue)
{
if ($GLOBALS['TL_DCA'][$this->strTable]['list']['sorting']['mode'] == 4)
{
$this->Database->prepare("UPDATE " . $this->strTable . " SET " . $this->strField . "='' WHERE pid=?")
$this->Database->prepare("UPDATE " . $this->strTable . " SET " . \Database::escapeColumnName($this->strField) . "='' WHERE pid=?")
->execute($this->activeRecord->pid);
}
else
{
$this->Database->execute("UPDATE " . $this->strTable . " SET " . $this->strField . "=''");
$this->Database->execute("UPDATE " . $this->strTable . " SET " . \Database::escapeColumnName($this->strField) . "=''");
}
}

Expand All @@ -3161,7 +3161,7 @@ protected function save($varValue)
$arrValues = $this->values;
array_unshift($arrValues, $varValue);

$objUpdateStmt = $this->Database->prepare("UPDATE " . $this->strTable . " SET " . $this->strField . "=? WHERE " . implode(' AND ', $this->procedure))
$objUpdateStmt = $this->Database->prepare("UPDATE " . $this->strTable . " SET " . \Database::escapeColumnName($this->strField) . "=? WHERE " . implode(' AND ', $this->procedure))
->execute($arrValues);

if ($objUpdateStmt->affectedRows)
Expand Down
30 changes: 30 additions & 0 deletions src/Resources/contao/library/Contao/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,36 @@ public function getUuid()
}


/**
* Escape the column name if it a reserved word
*
* @param string $name
*
* @return string
*/
public static function escapeColumnName($name)
{
if (in_array($name, ['rows'], true)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use the doctrine platform method here to quote everything we need (plus force rows)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't in_array($name, ['rows'], true) the same as 'rows' === $name? Or did you just want to avoid the Yoda condition. 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He just prepared for more in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ausi, what about this? (should work in 3.5 too):

// I'd name it "quote", not "escape"...
public static function quoteColumnName($name)
{
    if (method_exists('Contao\System', 'getContainer')) {
        $connection = Contao\System::getContainer()->get('connection');
        if ($connection instanceof Doctrine\DBAL\Connection) {
            $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name);
        }
    }

    // Force quoting "rows" for older Contao versions
    // (which will be ignored if already done by the platform in newer Contao versions)
    if (in_array($name, ['rows'], true)) {
        $name = '`'.$name.'`';
    }

    return $name;
}

$name = '`'.$name.'`';
}

return $name;
}


/**
* Escape a list of column names
*
* @param string[] $names
*
* @return string[]
*/
public static function escapeColumnNames(array $names)
{
return array_map([static::class, 'escapeColumnName'], $names);
}


/**
* Execute a query and do not cache the result
*
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/contao/library/Contao/Database/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function set($arrParams)
if (strncasecmp($this->strQuery, 'INSERT', 6) === 0)
{
$strQuery = sprintf('(%s) VALUES (%s)',
implode(', ', array_keys($arrParams)),
implode(', ', \Database::escapeColumnNames(array_keys($arrParams))),
str_replace('%', '%%', implode(', ', array_values($arrParams))));
}

Expand All @@ -193,7 +193,7 @@ public function set($arrParams)

foreach ($arrParams as $k=>$v)
{
$arrSet[] = $k . '=' . $v;
$arrSet[] = \Database::escapeColumnName($k) . '=' . $v;
}

$strQuery = 'SET ' . str_replace('%', '%%', implode(', ', $arrSet));
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/contao/library/Contao/Model/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function find(array $arrOptions)
// Where condition
if ($arrOptions['column'] !== null)
{
$strQuery .= " WHERE " . (is_array($arrOptions['column']) ? implode(" AND ", $arrOptions['column']) : $arrOptions['table'] . '.' . $arrOptions['column'] . "=?");
$strQuery .= " WHERE " . (is_array($arrOptions['column']) ? implode(" AND ", $arrOptions['column']) : $arrOptions['table'] . '.' . \Database::escapeColumnName($arrOptions['column']) . "=?");
}

// Group by
Expand Down