mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 05:37:39 +02:00
- DibiDriver::format splitted into escape() & unescape()
- added DibiConnection::unescape - DibiPostgreDriver support escaping & unescaping BYTEA type
This commit is contained in:
2
TODO.txt
2
TODO.txt
@@ -1,3 +1 @@
|
|||||||
- sjednotit DibiVariable, modifier a type u IDibiDriver::format()
|
|
||||||
- odstranit podporu pro modifik<69>tory v kl<6B><6C><EFBFBD>ch pole?
|
|
||||||
- event, log, profiler
|
- event, log, profiler
|
||||||
|
@@ -76,16 +76,14 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
const
|
const
|
||||||
FIELD_TEXT = 's', // as 'string'
|
FIELD_TEXT = 's', // as 'string'
|
||||||
FIELD_BINARY = 'S',
|
FIELD_BINARY = 'bin',
|
||||||
FIELD_BOOL = 'b',
|
FIELD_BOOL = 'b',
|
||||||
FIELD_INTEGER = 'i',
|
FIELD_INTEGER = 'i',
|
||||||
FIELD_FLOAT = 'f',
|
FIELD_FLOAT = 'f',
|
||||||
FIELD_DATE = 'd',
|
FIELD_DATE = 'd',
|
||||||
FIELD_DATETIME = 't',
|
FIELD_DATETIME = 't',
|
||||||
FIELD_UNKNOWN = '?',
|
|
||||||
|
|
||||||
// special
|
// special
|
||||||
FIELD_COUNTER = 'C', // counter or autoincrement, is integer
|
|
||||||
IDENTIFIER = 'n';
|
IDENTIFIER = 'n';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -186,21 +186,50 @@ class DibiMsSqlDriver extends /*Nette::*/Object implements IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function format($value, $type)
|
public function escape($value, $type)
|
||||||
{
|
{
|
||||||
if ($type === dibi::FIELD_TEXT) return "'" . str_replace("'", "''", $value) . "'";
|
switch ($type) {
|
||||||
if ($type === dibi::IDENTIFIER) return '[' . str_replace('.', '].[', $value) . ']';
|
case dibi::FIELD_TEXT:
|
||||||
if ($type === dibi::FIELD_BOOL) return $value ? -1 : 0;
|
case dibi::FIELD_BINARY:
|
||||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
return "'" . str_replace("'", "''", $value) . "'";
|
||||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
|
||||||
throw new InvalidArgumentException('Unsupported formatting type.');
|
case dibi::IDENTIFIER:
|
||||||
|
return '[' . str_replace('.', '].[', $value) . ']';
|
||||||
|
|
||||||
|
case dibi::FIELD_BOOL:
|
||||||
|
return $value ? -1 : 0;
|
||||||
|
|
||||||
|
case dibi::FIELD_DATE:
|
||||||
|
return date("'Y-m-d'", $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
return date("'Y-m-d H:i:s'", $value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -239,21 +239,50 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function format($value, $type)
|
public function escape($value, $type)
|
||||||
{
|
{
|
||||||
if ($type === dibi::FIELD_TEXT) return "'" . mysql_real_escape_string($value, $this->connection) . "'";
|
switch ($type) {
|
||||||
if ($type === dibi::IDENTIFIER) return '`' . str_replace('.', '`.`', $value) . '`';
|
case dibi::FIELD_TEXT:
|
||||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
case dibi::FIELD_BINARY:
|
||||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
return "'" . mysql_real_escape_string($value, $this->connection) . "'";
|
||||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
|
||||||
throw new InvalidArgumentException('Unsupported formatting type.');
|
case dibi::IDENTIFIER:
|
||||||
|
return '`' . str_replace('.', '`.`', $value) . '`';
|
||||||
|
|
||||||
|
case dibi::FIELD_BOOL:
|
||||||
|
return $value ? 1 : 0;
|
||||||
|
|
||||||
|
case dibi::FIELD_DATE:
|
||||||
|
return date("'Y-m-d'", $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
return date("'Y-m-d H:i:s'", $value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -219,21 +219,50 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function format($value, $type)
|
public function escape($value, $type)
|
||||||
{
|
{
|
||||||
if ($type === dibi::FIELD_TEXT) return "'" . mysqli_real_escape_string($this->connection, $value) . "'";
|
switch ($type) {
|
||||||
if ($type === dibi::IDENTIFIER) return '`' . str_replace('.', '`.`', $value) . '`';
|
case dibi::FIELD_TEXT:
|
||||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
case dibi::FIELD_BINARY:
|
||||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
return "'" . mysqli_real_escape_string($this->connection, $value) . "'";
|
||||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
|
||||||
throw new InvalidArgumentException('Unsupported formatting type.');
|
case dibi::IDENTIFIER:
|
||||||
|
return '`' . str_replace('.', '`.`', $value) . '`';
|
||||||
|
|
||||||
|
case dibi::FIELD_BOOL:
|
||||||
|
return $value ? 1 : 0;
|
||||||
|
|
||||||
|
case dibi::FIELD_DATE:
|
||||||
|
return date("'Y-m-d'", $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
return date("'Y-m-d H:i:s'", $value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -200,21 +200,50 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function format($value, $type)
|
public function escape($value, $type)
|
||||||
{
|
{
|
||||||
if ($type === dibi::FIELD_TEXT) return "'" . str_replace("'", "''", $value) . "'";
|
switch ($type) {
|
||||||
if ($type === dibi::IDENTIFIER) return '[' . str_replace('.', '].[', $value) . ']';
|
case dibi::FIELD_TEXT:
|
||||||
if ($type === dibi::FIELD_BOOL) return $value ? -1 : 0;
|
case dibi::FIELD_BINARY:
|
||||||
if ($type === dibi::FIELD_DATE) return date("#m/d/Y#", $value);
|
return "'" . str_replace("'", "''", $value) . "'";
|
||||||
if ($type === dibi::FIELD_DATETIME) return date("#m/d/Y H:i:s#", $value);
|
|
||||||
throw new InvalidArgumentException('Unsupported formatting type.');
|
case dibi::IDENTIFIER:
|
||||||
|
return '[' . str_replace('.', '].[', $value) . ']';
|
||||||
|
|
||||||
|
case dibi::FIELD_BOOL:
|
||||||
|
return $value ? -1 : 0;
|
||||||
|
|
||||||
|
case dibi::FIELD_DATE:
|
||||||
|
return date("#m/d/Y#", $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
return date("#m/d/Y H:i:s#", $value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -197,21 +197,50 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function format($value, $type)
|
public function escape($value, $type)
|
||||||
{
|
{
|
||||||
if ($type === dibi::FIELD_TEXT) return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
|
switch ($type) {
|
||||||
if ($type === dibi::IDENTIFIER) return '[' . str_replace('.', '].[', $value) . ']'; // TODO: not tested
|
case dibi::FIELD_TEXT:
|
||||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
case dibi::FIELD_BINARY:
|
||||||
if ($type === dibi::FIELD_DATE) return date("U", $value);
|
return "'" . str_replace("'", "''", $value) . "'"; // TODO: not tested
|
||||||
if ($type === dibi::FIELD_DATETIME) return date("U", $value);
|
|
||||||
throw new InvalidArgumentException('Unsupported formatting type.');
|
case dibi::IDENTIFIER:
|
||||||
|
return '[' . str_replace('.', '].[', $value) . ']'; // TODO: not tested
|
||||||
|
|
||||||
|
case dibi::FIELD_BOOL:
|
||||||
|
return $value ? 1 : 0;
|
||||||
|
|
||||||
|
case dibi::FIELD_DATE:
|
||||||
|
return date("U", $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
return date("U", $value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -218,21 +218,52 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function format($value, $type)
|
public function escape($value, $type)
|
||||||
{
|
{
|
||||||
if ($type === dibi::FIELD_TEXT) return $this->connection->quote($value);
|
switch ($type) {
|
||||||
if ($type === dibi::IDENTIFIER) return $value; // quoting is not supported by PDO
|
case dibi::FIELD_TEXT:
|
||||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
return $this->connection->quote($value, PDO::PARAM_STR);
|
||||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
|
||||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
case dibi::FIELD_BINARY:
|
||||||
throw new InvalidArgumentException('Unsupported formatting type.');
|
return $this->connection->quote($value, PDO::PARAM_LOB);
|
||||||
|
|
||||||
|
case dibi::IDENTIFIER:
|
||||||
|
return $value; // quoting is not supported by PDO
|
||||||
|
|
||||||
|
case dibi::FIELD_BOOL:
|
||||||
|
return $this->connection->quote($value, PDO::PARAM_BOOL);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATE:
|
||||||
|
return date("'Y-m-d'", $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
return date("'Y-m-d H:i:s'", $value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -224,31 +224,69 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function format($value, $type)
|
public function escape($value, $type)
|
||||||
{
|
{
|
||||||
if ($type === dibi::FIELD_TEXT) {
|
switch ($type) {
|
||||||
if ($this->escMethod) return "'" . pg_escape_string($this->connection, $value) . "'";
|
case dibi::FIELD_TEXT:
|
||||||
return "'" . pg_escape_string($value) . "'";
|
if ($this->escMethod) {
|
||||||
}
|
return "'" . pg_escape_string($this->connection, $value) . "'";
|
||||||
|
} else {
|
||||||
|
return "'" . pg_escape_string($value) . "'";
|
||||||
|
}
|
||||||
|
|
||||||
if ($type === dibi::IDENTIFIER) {
|
case dibi::FIELD_BINARY:
|
||||||
|
if ($this->escMethod) {
|
||||||
|
return "'" . pg_escape_bytea($this->connection, $value) . "'";
|
||||||
|
} else {
|
||||||
|
return "'" . pg_escape_bytea($value) . "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
case dibi::IDENTIFIER:
|
||||||
$a = strrpos($value, '.');
|
$a = strrpos($value, '.');
|
||||||
if ($a === FALSE) return '"' . str_replace('"', '""', $value) . '"';
|
if ($a === FALSE) return '"' . str_replace('"', '""', $value) . '"';
|
||||||
// table.col delimite as table."col"
|
// table.col delimite as table."col"
|
||||||
return substr($value, 0, $a) . '."' . str_replace('"', '""', substr($value, $a + 1)) . '"';
|
return substr($value, 0, $a) . '."' . str_replace('"', '""', substr($value, $a + 1)) . '"';
|
||||||
}
|
|
||||||
|
|
||||||
if ($type === dibi::FIELD_BOOL) return $value ? 'TRUE' : 'FALSE';
|
case dibi::FIELD_BOOL:
|
||||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
return $value ? 'TRUE' : 'FALSE';
|
||||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
|
||||||
throw new InvalidArgumentException('Unsupported formatting type.');
|
case dibi::FIELD_DATE:
|
||||||
|
return date("'Y-m-d'", $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
return date("'Y-m-d H:i:s'", $value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type)
|
||||||
|
{
|
||||||
|
switch ($type) {
|
||||||
|
case dibi::FIELD_BINARY:
|
||||||
|
return pg_unescape_bytea($value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -203,21 +203,50 @@ class DibiSqliteDriver extends /*Nette::*/Object implements IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function format($value, $type)
|
public function escape($value, $type)
|
||||||
{
|
{
|
||||||
if ($type === dibi::FIELD_TEXT) return "'" . sqlite_escape_string($value) . "'";
|
switch ($type) {
|
||||||
if ($type === dibi::IDENTIFIER) return '[' . str_replace('.', '].[', $value) . ']';
|
case dibi::FIELD_TEXT:
|
||||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
case dibi::FIELD_BINARY:
|
||||||
if ($type === dibi::FIELD_DATE) return date($this->fmtDate, $value);
|
return "'" . sqlite_escape_string($value) . "'";
|
||||||
if ($type === dibi::FIELD_DATETIME) return date($this->fmtDateTime, $value);
|
|
||||||
throw new InvalidArgumentException('Unsupported formatting type.');
|
case dibi::IDENTIFIER:
|
||||||
|
return '[' . str_replace('.', '].[', $value) . ']';
|
||||||
|
|
||||||
|
case dibi::FIELD_BOOL:
|
||||||
|
return $value ? 1 : 0;
|
||||||
|
|
||||||
|
case dibi::FIELD_DATE:
|
||||||
|
return date($this->fmtDate, $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
return date($this->fmtDateTime, $value);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type)
|
||||||
|
{
|
||||||
|
throw new InvalidArgumentException('Unsupported type.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -372,15 +372,30 @@ class DibiConnection extends /*Nette::*/Object
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escapes the string.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string unescaped string
|
* @param string unescaped string
|
||||||
* @return string escaped and optionally quoted string
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
|
* @return string escaped and quoted string
|
||||||
*/
|
*/
|
||||||
public function escape($value)
|
public function escape($value, $type = dibi::FIELD_TEXT)
|
||||||
{
|
{
|
||||||
$this->connect(); // MySQL & PDO require connection
|
$this->connect(); // MySQL & PDO require connection
|
||||||
return $this->driver->format($value, dibi::FIELD_TEXT);
|
return $this->driver->escape($value, $type);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
*/
|
||||||
|
public function unescape($value, $type = dibi::FIELD_BINARY)
|
||||||
|
{
|
||||||
|
return $this->driver->unescape($value, $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -393,7 +408,7 @@ class DibiConnection extends /*Nette::*/Object
|
|||||||
*/
|
*/
|
||||||
public function delimite($value)
|
public function delimite($value)
|
||||||
{
|
{
|
||||||
return $this->driver->format($value, dibi::IDENTIFIER);
|
return $this->driver->escape($value, dibi::IDENTIFIER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -81,16 +81,6 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static $types = array(
|
|
||||||
dibi::FIELD_TEXT => 'string',
|
|
||||||
dibi::FIELD_BINARY => 'string',
|
|
||||||
dibi::FIELD_INTEGER => 'int',
|
|
||||||
dibi::FIELD_FLOAT => 'float',
|
|
||||||
dibi::FIELD_COUNTER => 'int',
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param IDibiDriver
|
* @param IDibiDriver
|
||||||
* @param array
|
* @param array
|
||||||
@@ -542,20 +532,30 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset(self::$types[$type])) {
|
switch ($type) {
|
||||||
settype($value, self::$types[$type]);
|
case dibi::FIELD_TEXT:
|
||||||
|
return (string) $value;
|
||||||
|
|
||||||
|
case dibi::FIELD_BINARY:
|
||||||
|
return $this->getDriver()->unescape($value, $type);
|
||||||
|
|
||||||
|
case dibi::FIELD_INTEGER:
|
||||||
|
return (int) $value;
|
||||||
|
|
||||||
|
case dibi::FIELD_FLOAT:
|
||||||
|
return (float) $value;
|
||||||
|
|
||||||
|
case dibi::FIELD_DATE:
|
||||||
|
case dibi::FIELD_DATETIME:
|
||||||
|
$value = strtotime($value);
|
||||||
|
return $format === NULL ? $value : date($format, $value);
|
||||||
|
|
||||||
|
case dibi::FIELD_BOOL:
|
||||||
|
return ((bool) $value) && $value !== 'f' && $value !== 'F';
|
||||||
|
|
||||||
|
default:
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($type === dibi::FIELD_DATE || $type === dibi::FIELD_DATETIME) {
|
|
||||||
return $format === NULL ? strtotime($value) : date($format, strtotime($value));
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($type === dibi::FIELD_BOOL) {
|
|
||||||
return ((bool) $value) && $value !== 'f' && $value !== 'F';
|
|
||||||
}
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -261,13 +261,12 @@ final class DibiTranslator extends /*Nette::*/Object
|
|||||||
|
|
||||||
switch ($modifier) {
|
switch ($modifier) {
|
||||||
case 's': // string
|
case 's': // string
|
||||||
return $this->driver->format($value, dibi::FIELD_TEXT);
|
case 'bin':// binary
|
||||||
|
case 'b': // boolean
|
||||||
|
return $this->driver->escape($value, $modifier);
|
||||||
|
|
||||||
case 'sn': // string or NULL
|
case 'sn': // string or NULL
|
||||||
return $value == '' ? 'NULL' : $this->driver->format($value, dibi::FIELD_TEXT); // notice two equal signs
|
return $value == '' ? 'NULL' : $this->driver->escape($value, dibi::FIELD_TEXT); // notice two equal signs
|
||||||
|
|
||||||
case 'b': // boolean
|
|
||||||
return $this->driver->format($value, dibi::FIELD_BOOL);
|
|
||||||
|
|
||||||
case 'i': // signed int
|
case 'i': // signed int
|
||||||
case 'u': // unsigned int, ignored
|
case 'u': // unsigned int, ignored
|
||||||
@@ -285,10 +284,8 @@ final class DibiTranslator extends /*Nette::*/Object
|
|||||||
return (string) ($value + 0);
|
return (string) ($value + 0);
|
||||||
|
|
||||||
case 'd': // date
|
case 'd': // date
|
||||||
return $this->driver->format(is_string($value) ? strtotime($value) : $value, dibi::FIELD_DATE);
|
|
||||||
|
|
||||||
case 't': // datetime
|
case 't': // datetime
|
||||||
return $this->driver->format(is_string($value) ? strtotime($value) : $value, dibi::FIELD_DATETIME);
|
return $this->driver->escape(is_string($value) ? strtotime($value) : $value, $modifier);
|
||||||
|
|
||||||
case 'n': // identifier name
|
case 'n': // identifier name
|
||||||
return $this->delimite($value);
|
return $this->delimite($value);
|
||||||
@@ -307,7 +304,10 @@ final class DibiTranslator extends /*Nette::*/Object
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 'and':
|
||||||
|
case 'or':
|
||||||
case 'a':
|
case 'a':
|
||||||
|
case 'l':
|
||||||
case 'v':
|
case 'v':
|
||||||
$this->hasError = TRUE;
|
$this->hasError = TRUE;
|
||||||
return '**Unexpected type ' . gettype($value) . '**';
|
return '**Unexpected type ' . gettype($value) . '**';
|
||||||
@@ -321,13 +321,13 @@ final class DibiTranslator extends /*Nette::*/Object
|
|||||||
|
|
||||||
// without modifier procession
|
// without modifier procession
|
||||||
if (is_string($value))
|
if (is_string($value))
|
||||||
return $this->driver->format($value, dibi::FIELD_TEXT);
|
return $this->driver->escape($value, dibi::FIELD_TEXT);
|
||||||
|
|
||||||
if (is_int($value) || is_float($value))
|
if (is_int($value) || is_float($value))
|
||||||
return (string) $value; // something like -9E-005 is accepted by SQL
|
return (string) $value; // something like -9E-005 is accepted by SQL
|
||||||
|
|
||||||
if (is_bool($value))
|
if (is_bool($value))
|
||||||
return $this->driver->format($value, dibi::FIELD_BOOL);
|
return $this->driver->escape($value, dibi::FIELD_BOOL);
|
||||||
|
|
||||||
if ($value === NULL)
|
if ($value === NULL)
|
||||||
return 'NULL';
|
return 'NULL';
|
||||||
@@ -427,10 +427,10 @@ final class DibiTranslator extends /*Nette::*/Object
|
|||||||
return $this->delimite($matches[2]);
|
return $this->delimite($matches[2]);
|
||||||
|
|
||||||
if ($matches[3]) // SQL strings: '...'
|
if ($matches[3]) // SQL strings: '...'
|
||||||
return $this->driver->format( str_replace("''", "'", $matches[4]), dibi::FIELD_TEXT);
|
return $this->driver->escape( str_replace("''", "'", $matches[4]), dibi::FIELD_TEXT);
|
||||||
|
|
||||||
if ($matches[5]) // SQL strings: "..."
|
if ($matches[5]) // SQL strings: "..."
|
||||||
return $this->driver->format( str_replace('""', '"', $matches[6]), dibi::FIELD_TEXT);
|
return $this->driver->escape( str_replace('""', '"', $matches[6]), dibi::FIELD_TEXT);
|
||||||
|
|
||||||
if ($matches[7]) { // string quote
|
if ($matches[7]) { // string quote
|
||||||
$this->hasError = TRUE;
|
$this->hasError = TRUE;
|
||||||
@@ -453,7 +453,7 @@ final class DibiTranslator extends /*Nette::*/Object
|
|||||||
if (strpos($value, ':') !== FALSE) {
|
if (strpos($value, ':') !== FALSE) {
|
||||||
$value = strtr($value, dibi::getSubst());
|
$value = strtr($value, dibi::getSubst());
|
||||||
}
|
}
|
||||||
return $this->driver->format($value, dibi::IDENTIFIER);
|
return $this->driver->escape($value, dibi::IDENTIFIER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -142,13 +142,27 @@ interface IDibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format to SQL command.
|
* Encodes data for use in an SQL statement.
|
||||||
*
|
*
|
||||||
* @param string value
|
* @param string value
|
||||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, ...)
|
||||||
* @return string formatted value
|
* @return string encoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
function format($value, $type);
|
function escape($value, $type);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes data from resultset.
|
||||||
|
*
|
||||||
|
* @param string value
|
||||||
|
* @param string type (dibi::FIELD_BINARY)
|
||||||
|
* @return string decoded value
|
||||||
|
* @throws InvalidArgumentException
|
||||||
|
*/
|
||||||
|
function unescape($value, $type);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user