Permalink
Browse files

IDibiDriver: escape() & unescape() replaced with escape*() & unescape…

…Binary() (BC break!)
  • Loading branch information...
1 parent d62fc3a commit 3490fef8e491fab97908ee35b2c00224becef0e2 @dg committed Oct 7, 2015
@@ -51,7 +51,7 @@ class DibiDataSource extends DibiObject implements IDataSource
public function __construct($sql, DibiConnection $connection)
{
if (strpbrk($sql, " \t\r\n") === FALSE) {
- $this->sql = $connection->getDriver()->escape($sql, dibi::IDENTIFIER); // table name
+ $this->sql = $connection->getDriver()->escapeIdentifier($sql); // table name
} else {
$this->sql = '(' . $sql . ') t'; // SQL command
}
@@ -256,34 +256,48 @@ public function createResultDriver($resource)
/**
* Encodes data for use in a SQL statement.
- * @param mixed value
- * @param string type (dibi::TEXT, dibi::BOOL, ...)
- * @return string encoded value
- * @throws InvalidArgumentException
+ * @param string
+ * @return string
*/
- public function escape($value, $type)
+ public function escapeText($value)
+ {
+ return "'" . str_replace("'", "''", $value) . "'";
+ }
+
+
+ public function escapeBinary($value)
+ {
+ return "'" . str_replace("'", "''", $value) . "'";
+ }
+
+
+ public function escapeIdentifier($value)
+ {
+ return $value;
+ }
+
+
+ public function escapeBool($value)
{
- switch ($type) {
- case dibi::TEXT:
- case dibi::BINARY:
- return "'" . str_replace("'", "''", $value) . "'";
+ return $value ? 1 : 0;
+ }
- case dibi::IDENTIFIER:
- return $value;
- case dibi::BOOL:
- return $value ? 1 : 0;
+ public function escapeDate($value)
+ {
+ if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
+ $value = new DibiDateTime($value);
+ }
+ return $value->format("'Y-m-d'");
+ }
- case dibi::DATE:
- case dibi::DATETIME:
- if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
- $value = new DibiDateTime($value);
- }
- return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
- default:
- throw new InvalidArgumentException('Unsupported type.');
+ public function escapeDateTime($value)
+ {
+ if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
+ $value = new DibiDateTime($value);
}
+ return $value->format("'Y-m-d H:i:s'");
}
@@ -301,17 +315,19 @@ public function escapeLike($value, $pos)
/**
* Decodes data from result set.
- * @param string value
- * @param string type (dibi::BINARY)
- * @return string decoded value
- * @throws InvalidArgumentException
+ * @param string
+ * @return string
*/
- public function unescape($value, $type)
+ public function unescapeBinary($value)
{
- if ($type === dibi::BINARY) {
- return $value;
- }
- throw new InvalidArgumentException('Unsupported type.');
+ return $value;
+ }
+
+
+ /** @deprecated */
+ public function escape($value, $type)
+ {
+ return DibiHelpers::escape($this, $value, $type);
}
@@ -213,37 +213,52 @@ public function createResultDriver($resource)
/**
* Encodes data for use in a SQL statement.
* @param mixed value
- * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value
- * @throws InvalidArgumentException
*/
- public function escape($value, $type)
+ public function escapeText($value)
+ {
+ return "'" . str_replace("'", "''", $value) . "'";
+ }
+
+
+ public function escapeBinary($value)
+ {
+ return "'" . str_replace("'", "''", $value) . "'";
+ }
+
+
+ public function escapeIdentifier($value)
+ {
+ // @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
+ return '[' . str_replace(']', ']]', $value) . ']';
+ }
+
+
+ public function escapeBool($value)
{
- switch ($type) {
- case dibi::TEXT:
- case dibi::BINARY:
- return "'" . str_replace("'", "''", $value) . "'";
-
- case dibi::IDENTIFIER:
- // @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
- return '[' . str_replace(']', ']]', $value) . ']';
-
- case dibi::BOOL:
- return $value ? 1 : 0;
-
- case dibi::DATE:
- case dibi::DATETIME:
- if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
- $value = new DibiDateTime($value);
- }
- return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
-
- default:
- throw new InvalidArgumentException('Unsupported type.');
+ return $value ? 1 : 0;
+ }
+
+
+ public function escapeDate($value)
+ {
+ if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
+ $value = new DibiDateTime($value);
+ }
+ return $value->format("'Y-m-d'");
+ }
+
+
+ public function escapeDateTime($value)
+ {
+ if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
+ $value = new DibiDateTime($value);
}
+ return $value->format("'Y-m-d H:i:s'");
}
+
/**
* Encodes string for use in a LIKE statement.
* @param string
@@ -259,17 +274,19 @@ public function escapeLike($value, $pos)
/**
* Decodes data from result set.
- * @param string value
- * @param string type (dibi::BINARY)
- * @return string decoded value
- * @throws InvalidArgumentException
+ * @param string
+ * @return string
*/
- public function unescape($value, $type)
+ public function unescapeBinary($value)
{
- if ($type === dibi::BINARY) {
- return $value;
- }
- throw new InvalidArgumentException('Unsupported type.');
+ return $value;
+ }
+
+
+ /** @deprecated */
+ public function escape($value, $type)
+ {
+ return DibiHelpers::escape($this, $value, $type);
}
@@ -53,7 +53,7 @@ public function getColumns($table)
SELECT c.name as COLUMN_NAME, c.is_identity AS AUTO_INCREMENT
FROM sys.columns c
INNER JOIN sys.tables t ON c.object_id = t.object_id
- WHERE t.name = {$this->driver->escape($table, dibi::TEXT)}
+ WHERE t.name = {$this->driver->escapeText($table)}
");
$autoIncrements = array();
@@ -74,7 +74,7 @@ public function getColumns($table)
And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
And CCU.COLUMN_NAME = C.COLUMN_NAME
) As Z
- WHERE C.TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}
+ WHERE C.TABLE_NAME = {$this->driver->escapeText($table)}
");
$columns = array();
while ($row = $res->fetch(TRUE)) {
@@ -101,13 +101,13 @@ public function getColumns($table)
*/
public function getIndexes($table)
{
- $keyUsagesRes = $this->driver->query("SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}");
+ $keyUsagesRes = $this->driver->query("SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = {$this->driver->escapeText($table)}");
$keyUsages = array();
while ($row = $keyUsagesRes->fetch(TRUE)) {
$keyUsages[$row['CONSTRAINT_NAME']][(int) $row['ORDINAL_POSITION'] - 1] = $row['COLUMN_NAME'];
}
- $res = $this->driver->query("SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = {$this->driver->escape($table, dibi::TEXT)}");
+ $res = $this->driver->query("SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = {$this->driver->escapeText($table)}");
$indexes = array();
while ($row = $res->fetch(TRUE)) {
$indexes[$row['CONSTRAINT_NAME']]['name'] = $row['CONSTRAINT_NAME'];
@@ -63,7 +63,7 @@ public function connect(array & $config)
throw new DibiDriverException("Can't connect to DB.");
}
- if (isset($config['database']) && !@mssql_select_db($this->escape($config['database'], dibi::IDENTIFIER), $this->connection)) { // intentionally @
+ if (isset($config['database']) && !@mssql_select_db($this->escapeIdentifier($config['database']), $this->connection)) { // intentionally @
throw new DibiDriverException("Can't select DB '$config[database]'.");
}
}
@@ -198,34 +198,48 @@ public function createResultDriver($resource)
/**
* Encodes data for use in a SQL statement.
* @param mixed value
- * @param string type (dibi::TEXT, dibi::BOOL, ...)
* @return string encoded value
- * @throws InvalidArgumentException
*/
- public function escape($value, $type)
+ public function escapeText($value)
+ {
+ return "'" . str_replace("'", "''", $value) . "'";
+ }
+
+
+ public function escapeBinary($value)
+ {
+ return "'" . str_replace("'", "''", $value) . "'";
+ }
+
+
+ public function escapeIdentifier($value)
+ {
+ // @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
+ return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']';
+ }
+
+
+ public function escapeBool($value)
+ {
+ return $value ? 1 : 0;
+ }
+
+
+ public function escapeDate($value)
+ {
+ if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
+ $value = new DibiDateTime($value);
+ }
+ return $value->format("'Y-m-d'");
+ }
+
+
+ public function escapeDateTime($value)
{
- switch ($type) {
- case dibi::TEXT:
- case dibi::BINARY:
- return "'" . str_replace("'", "''", $value) . "'";
-
- case dibi::IDENTIFIER:
- // @see https://msdn.microsoft.com/en-us/library/ms176027.aspx
- return '[' . str_replace(array('[', ']'), array('[[', ']]'), $value) . ']';
-
- case dibi::BOOL:
- return $value ? 1 : 0;
-
- case dibi::DATE:
- case dibi::DATETIME:
- if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
- $value = new DibiDateTime($value);
- }
- return $value->format($type === dibi::DATETIME ? "'Y-m-d H:i:s'" : "'Y-m-d'");
-
- default:
- throw new InvalidArgumentException('Unsupported type.');
+ if (!$value instanceof DateTime && !$value instanceof DateTimeInterface) {
+ $value = new DibiDateTime($value);
}
+ return $value->format("'Y-m-d H:i:s'");
}
@@ -244,17 +258,19 @@ public function escapeLike($value, $pos)
/**
* Decodes data from result set.
- * @param string value
- * @param string type (dibi::BINARY)
- * @return string decoded value
- * @throws InvalidArgumentException
+ * @param string
+ * @return string
*/
- public function unescape($value, $type)
+ public function unescapeBinary($value)
{
- if ($type === dibi::BINARY) {
- return $value;
- }
- throw new InvalidArgumentException('Unsupported type.');
+ return $value;
+ }
+
+
+ /** @deprecated */
+ public function escape($value, $type)
+ {
+ return DibiHelpers::escape($this, $value, $type);
}
Oops, something went wrong.

0 comments on commit 3490fef

Please sign in to comment.