From 2a4f5ec456c5dda3db5d32000840b227bac58c64 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 18 Jun 2008 23:34:35 +0000 Subject: [PATCH] improved DibiPdoDriver identifier escaping --- dibi/drivers/pdo.php | 23 ++++++++++++++++++++++- dibi/drivers/postgre.php | 9 ++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index e95f75c6..4399744f 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -235,7 +235,28 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver return $this->connection->quote($value, PDO::PARAM_LOB); case dibi::IDENTIFIER: - return $value; // quoting is not supported by PDO + switch ($this->connection->getAttribute(PDO::ATTR_DRIVER_NAME)) { + case 'mysql': + return '`' . str_replace('.', '`.`', $value) . '`'; + + case 'pgsql': + $a = strrpos($value, '.'); + if ($a === FALSE) { + return '"' . str_replace('"', '""', $value) . '"'; + } else { + return substr($value, 0, $a) . '."' . str_replace('"', '""', substr($value, $a + 1)) . '"'; + } + + case 'sqlite': + case 'sqlite2': + case 'odbc': + case 'oci': // TODO: not tested + case 'mssql': + return '[' . str_replace('.', '].[', $value) . ']'; + + default: + return $value; + } case dibi::FIELD_BOOL: return $this->connection->quote($value, PDO::PARAM_BOOL); diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index c6d955be..606ac43c 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -250,9 +250,12 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver case dibi::IDENTIFIER: $a = strrpos($value, '.'); - if ($a === FALSE) return '"' . str_replace('"', '""', $value) . '"'; - // table.col delimite as table."col" - return substr($value, 0, $a) . '."' . str_replace('"', '""', substr($value, $a + 1)) . '"'; + if ($a === FALSE) { + return '"' . str_replace('"', '""', $value) . '"'; + } else { + // table.col delimite as table."col" + return substr($value, 0, $a) . '."' . str_replace('"', '""', substr($value, $a + 1)) . '"'; + } case dibi::FIELD_BOOL: return $value ? 'TRUE' : 'FALSE';