mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 13:17:58 +02:00
- DibiConnection & DibiTranslator refactoring
- DibiException accepts SQL parameter - undeprecated IDataSource
This commit is contained in:
@@ -242,12 +242,8 @@ class DibiConnection extends DibiObject
|
|||||||
{
|
{
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$trans = new DibiTranslator($this->driver);
|
$translator = new DibiTranslator($this->driver);
|
||||||
if ($trans->translate($args)) {
|
return $this->nativeQuery($translator->translate($args));
|
||||||
return $this->nativeQuery($trans->sql);
|
|
||||||
} else {
|
|
||||||
throw new DibiException('SQL translate error: ' . $trans->sql);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -262,12 +258,8 @@ class DibiConnection extends DibiObject
|
|||||||
{
|
{
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$trans = new DibiTranslator($this->driver);
|
$translator = new DibiTranslator($this->driver);
|
||||||
if ($trans->translate($args)) {
|
return $translator->translate($args);
|
||||||
return $trans->sql;
|
|
||||||
} else {
|
|
||||||
throw new DibiException('SQL translate error: ' . $trans->sql);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -281,10 +273,31 @@ class DibiConnection extends DibiObject
|
|||||||
{
|
{
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$this->connect();
|
$this->connect();
|
||||||
$trans = new DibiTranslator($this->driver);
|
try {
|
||||||
$ok = $trans->translate($args);
|
$translator = new DibiTranslator($this->driver);
|
||||||
dibi::dump($trans->sql);
|
dibi::dump($translator->translate($args));
|
||||||
return $ok;
|
return TRUE;
|
||||||
|
|
||||||
|
} catch (DibiException $e) {
|
||||||
|
dibi::dump($e->getSql());
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates (translates) and returns SQL query as DibiDataSource.
|
||||||
|
* @param array|mixed one or more arguments
|
||||||
|
* @return DibiDataSource
|
||||||
|
* @throws DibiException
|
||||||
|
*/
|
||||||
|
final public function dataSource($args)
|
||||||
|
{
|
||||||
|
$args = func_get_args();
|
||||||
|
$this->connect();
|
||||||
|
$translator = new DibiTranslator($this->driver);
|
||||||
|
return new DibiDataSource($translator->translate($args), $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -20,26 +20,12 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides an interface between a dataset and data-aware components.
|
|
||||||
* @package dibi
|
|
||||||
* @deprecated
|
|
||||||
*/
|
|
||||||
interface IDataSource extends Countable, IteratorAggregate
|
|
||||||
{
|
|
||||||
//function IteratorAggregate::getIterator();
|
|
||||||
//function Countable::count();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default implementation of IDataSource for dibi.
|
* Default implementation of IDataSource for dibi.
|
||||||
*
|
*
|
||||||
* @author David Grudl
|
* @author David Grudl
|
||||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||||
* @package dibi
|
* @package dibi
|
||||||
* @deprecated
|
|
||||||
*/
|
*/
|
||||||
class DibiDataSource extends DibiObject implements IDataSource
|
class DibiDataSource extends DibiObject implements IDataSource
|
||||||
{
|
{
|
||||||
|
@@ -27,31 +27,14 @@
|
|||||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||||
* @package dibi
|
* @package dibi
|
||||||
*/
|
*/
|
||||||
class DibiException extends Exception
|
class DibiException extends Exception implements /*Nette\*/IDebuggable
|
||||||
{
|
{
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* database server exception.
|
|
||||||
*
|
|
||||||
* @author David Grudl
|
|
||||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
|
||||||
* @package dibi
|
|
||||||
*/
|
|
||||||
class DibiDriverException extends DibiException implements /*Nette\*/IDebuggable
|
|
||||||
{
|
|
||||||
/** @var string */
|
|
||||||
private static $errorMsg;
|
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $sql;
|
private $sql;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct an dibi driver exception.
|
* Construct an dibi exception.
|
||||||
* @param string Message describing the exception
|
* @param string Message describing the exception
|
||||||
* @param int Some code
|
* @param int Some code
|
||||||
* @param string SQL command
|
* @param string SQL command
|
||||||
@@ -104,12 +87,30 @@ class DibiDriverException extends DibiException implements /*Nette\*/IDebuggable
|
|||||||
return $panels;
|
return $panels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* database server exception.
|
||||||
|
*
|
||||||
|
* @author David Grudl
|
||||||
|
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||||
|
* @package dibi
|
||||||
|
*/
|
||||||
|
class DibiDriverException extends DibiException
|
||||||
|
{
|
||||||
|
|
||||||
/********************* error catching ****************d*g**/
|
/********************* error catching ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
private static $errorMsg;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts catching potential errors/warnings
|
* Starts catching potential errors/warnings
|
||||||
* @return void
|
* @return void
|
||||||
|
@@ -27,7 +27,7 @@
|
|||||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||||
* @package dibi
|
* @package dibi
|
||||||
*/
|
*/
|
||||||
class DibiFluent extends DibiObject implements Countable, IteratorAggregate
|
class DibiFluent extends DibiObject implements IDataSource
|
||||||
{
|
{
|
||||||
/** @var array */
|
/** @var array */
|
||||||
public static $masks = array(
|
public static $masks = array(
|
||||||
@@ -133,7 +133,7 @@ class DibiFluent extends DibiObject implements Countable, IteratorAggregate
|
|||||||
} elseif (is_string(key($arg))) { // associative array
|
} elseif (is_string(key($arg))) { // associative array
|
||||||
$args = array('%a', $arg);
|
$args = array('%a', $arg);
|
||||||
}
|
}
|
||||||
}
|
} // case $arg === FALSE is handled below
|
||||||
}
|
}
|
||||||
|
|
||||||
if (array_key_exists($clause, $this->clauses)) {
|
if (array_key_exists($clause, $this->clauses)) {
|
||||||
@@ -413,6 +413,16 @@ class DibiFluent extends DibiObject implements Countable, IteratorAggregate
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return DibiDataSource
|
||||||
|
*/
|
||||||
|
public function toDataSource()
|
||||||
|
{
|
||||||
|
return new DibiDataSource($this->connection->sql($this->_export()), $this->connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns SQL query.
|
* Returns SQL query.
|
||||||
* @return string
|
* @return string
|
||||||
|
@@ -40,7 +40,7 @@
|
|||||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||||
* @package dibi
|
* @package dibi
|
||||||
*/
|
*/
|
||||||
class DibiResult extends DibiObject implements Countable, IteratorAggregate
|
class DibiResult extends DibiObject implements IDataSource
|
||||||
{
|
{
|
||||||
/** @var array IDibiDriver */
|
/** @var array IDibiDriver */
|
||||||
private $driver;
|
private $driver;
|
||||||
|
@@ -29,9 +29,6 @@
|
|||||||
*/
|
*/
|
||||||
final class DibiTranslator extends DibiObject
|
final class DibiTranslator extends DibiObject
|
||||||
{
|
{
|
||||||
/** @var string */
|
|
||||||
public $sql;
|
|
||||||
|
|
||||||
/** @var IDibiDriver */
|
/** @var IDibiDriver */
|
||||||
private $driver;
|
private $driver;
|
||||||
|
|
||||||
@@ -81,7 +78,8 @@ final class DibiTranslator extends DibiObject
|
|||||||
/**
|
/**
|
||||||
* Generates SQL.
|
* Generates SQL.
|
||||||
* @param array
|
* @param array
|
||||||
* @return bool
|
* @return string
|
||||||
|
* @throws DibiException
|
||||||
*/
|
*/
|
||||||
public function translate(array $args)
|
public function translate(array $args)
|
||||||
{
|
{
|
||||||
@@ -174,13 +172,16 @@ final class DibiTranslator extends DibiObject
|
|||||||
|
|
||||||
$sql = implode(' ', $sql);
|
$sql = implode(' ', $sql);
|
||||||
|
|
||||||
|
if ($this->hasError) {
|
||||||
|
throw new DibiException('SQL translate error', 0, $sql);
|
||||||
|
}
|
||||||
|
|
||||||
// apply limit
|
// apply limit
|
||||||
if ($this->limit > -1 || $this->offset > 0) {
|
if ($this->limit > -1 || $this->offset > 0) {
|
||||||
$this->driver->applyLimit($sql, $this->limit, $this->offset);
|
$this->driver->applyLimit($sql, $this->limit, $this->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->sql = $sql;
|
return $sql;
|
||||||
return !$this->hasError;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -197,15 +198,14 @@ final class DibiTranslator extends DibiObject
|
|||||||
if (is_array($value) || $value instanceof ArrayObject) {
|
if (is_array($value) || $value instanceof ArrayObject) {
|
||||||
|
|
||||||
$vx = $kx = array();
|
$vx = $kx = array();
|
||||||
$operator = ', ';
|
|
||||||
switch ($modifier) {
|
switch ($modifier) {
|
||||||
case 'and':
|
case 'and':
|
||||||
case 'or': // key=val AND key IS NULL AND ...
|
case 'or': // key=val AND key IS NULL AND ...
|
||||||
$operator = ' ' . strtoupper($modifier) . ' ';
|
|
||||||
if (empty($value)) {
|
if (empty($value)) {
|
||||||
return '1';
|
return '1';
|
||||||
|
}
|
||||||
|
|
||||||
} else foreach ($value as $k => $v) {
|
foreach ($value as $k => $v) {
|
||||||
if (is_string($k)) {
|
if (is_string($k)) {
|
||||||
$pair = explode('%', $k, 2); // split into identifier & modifier
|
$pair = explode('%', $k, 2); // split into identifier & modifier
|
||||||
$k = $this->delimite($pair[0]) . ' ';
|
$k = $this->delimite($pair[0]) . ' ';
|
||||||
@@ -225,12 +225,12 @@ final class DibiTranslator extends DibiObject
|
|||||||
$vx[] = $this->formatValue($v, 'sql');
|
$vx[] = $this->formatValue($v, 'sql');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return implode($operator, $vx);
|
return implode(' ' . strtoupper($modifier) . ' ', $vx);
|
||||||
|
|
||||||
case 'n': // identifier names
|
case 'n': // identifier names
|
||||||
foreach ($value as $k => $v) {
|
foreach ($value as $k => $v) {
|
||||||
if (is_string($k)) {
|
if (is_string($k)) {
|
||||||
$vx[] = $this->delimite($k) . ' AS ' . $v;
|
$vx[] = $this->delimite($k) . (empty($v) ? '' : ' AS ' . $v);
|
||||||
} else {
|
} else {
|
||||||
$vx[] = $this->delimite($v);
|
$vx[] = $this->delimite($v);
|
||||||
}
|
}
|
||||||
@@ -244,7 +244,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
$vx[] = $this->delimite($pair[0]) . '='
|
$vx[] = $this->delimite($pair[0]) . '='
|
||||||
. $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE);
|
. $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE);
|
||||||
}
|
}
|
||||||
return implode($operator, $vx);
|
return implode(', ', $vx);
|
||||||
|
|
||||||
|
|
||||||
case 'l': // (val, val, ...)
|
case 'l': // (val, val, ...)
|
||||||
@@ -276,8 +276,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
|
|
||||||
case 'sql':
|
case 'sql':
|
||||||
$translator = new self($this->driver);
|
$translator = new self($this->driver);
|
||||||
$translator->translate($value);
|
return $translator->translate($value);
|
||||||
return $translator->sql;
|
|
||||||
|
|
||||||
default: // value, value, value - all with the same modifier
|
default: // value, value, value - all with the same modifier
|
||||||
foreach ($value as $v) {
|
foreach ($value as $v) {
|
||||||
@@ -357,7 +356,6 @@ final class DibiTranslator extends DibiObject
|
|||||||
case 'a':
|
case 'a':
|
||||||
case 'l':
|
case 'l':
|
||||||
case 'v':
|
case 'v':
|
||||||
case 'by':
|
|
||||||
$this->hasError = TRUE;
|
$this->hasError = TRUE;
|
||||||
return '**Unexpected type ' . gettype($value) . '**';
|
return '**Unexpected type ' . gettype($value) . '**';
|
||||||
|
|
||||||
|
@@ -39,6 +39,20 @@ interface IDibiVariable
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides an interface between a dataset and data-aware components.
|
||||||
|
* @package dibi
|
||||||
|
*/
|
||||||
|
interface IDataSource extends Countable, IteratorAggregate
|
||||||
|
{
|
||||||
|
//function IteratorAggregate::getIterator($offset = NULL, $limit = NULL);
|
||||||
|
//function Countable::count();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines method that must profiler implement.
|
* Defines method that must profiler implement.
|
||||||
* @package dibi
|
* @package dibi
|
||||||
|
Reference in New Issue
Block a user