Skip to content

Commit

Permalink
IDibiDriver: escape() & unescape() replaced with escape*() & unescape…
Browse files Browse the repository at this point in the history
…Binary() (BC break!)
  • Loading branch information
dg committed Oct 8, 2015
1 parent d62fc3a commit 3490fef
Show file tree
Hide file tree
Showing 20 changed files with 587 additions and 390 deletions.
2 changes: 1 addition & 1 deletion src/Dibi/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
76 changes: 46 additions & 30 deletions src/Dibi/Drivers/FirebirdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
}


Expand All @@ -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);
}


Expand Down
83 changes: 50 additions & 33 deletions src/Dibi/Drivers/MsSql2005Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}


Expand Down
8 changes: 4 additions & 4 deletions src/Dibi/Drivers/MsSql2005Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)) {
Expand All @@ -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'];
Expand Down
84 changes: 50 additions & 34 deletions src/Dibi/Drivers/MsSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]'.");
}
}
Expand Down Expand Up @@ -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'");
}


Expand All @@ -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);
}


Expand Down
Loading

0 comments on commit 3490fef

Please sign in to comment.