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();
|
||||
$this->connect();
|
||||
$trans = new DibiTranslator($this->driver);
|
||||
if ($trans->translate($args)) {
|
||||
return $this->nativeQuery($trans->sql);
|
||||
} else {
|
||||
throw new DibiException('SQL translate error: ' . $trans->sql);
|
||||
}
|
||||
$translator = new DibiTranslator($this->driver);
|
||||
return $this->nativeQuery($translator->translate($args));
|
||||
}
|
||||
|
||||
|
||||
@@ -262,12 +258,8 @@ class DibiConnection extends DibiObject
|
||||
{
|
||||
$args = func_get_args();
|
||||
$this->connect();
|
||||
$trans = new DibiTranslator($this->driver);
|
||||
if ($trans->translate($args)) {
|
||||
return $trans->sql;
|
||||
} else {
|
||||
throw new DibiException('SQL translate error: ' . $trans->sql);
|
||||
}
|
||||
$translator = new DibiTranslator($this->driver);
|
||||
return $translator->translate($args);
|
||||
}
|
||||
|
||||
|
||||
@@ -281,10 +273,31 @@ class DibiConnection extends DibiObject
|
||||
{
|
||||
$args = func_get_args();
|
||||
$this->connect();
|
||||
$trans = new DibiTranslator($this->driver);
|
||||
$ok = $trans->translate($args);
|
||||
dibi::dump($trans->sql);
|
||||
return $ok;
|
||||
try {
|
||||
$translator = new DibiTranslator($this->driver);
|
||||
dibi::dump($translator->translate($args));
|
||||
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.
|
||||
*
|
||||
* @author David Grudl
|
||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||
* @package dibi
|
||||
* @deprecated
|
||||
*/
|
||||
class DibiDataSource extends DibiObject implements IDataSource
|
||||
{
|
||||
|
@@ -27,31 +27,14 @@
|
||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||
* @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 */
|
||||
private $sql;
|
||||
|
||||
|
||||
/**
|
||||
* Construct an dibi driver exception.
|
||||
* Construct an dibi exception.
|
||||
* @param string Message describing the exception
|
||||
* @param int Some code
|
||||
* @param string SQL command
|
||||
@@ -104,12 +87,30 @@ class DibiDriverException extends DibiException implements /*Nette\*/IDebuggable
|
||||
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**/
|
||||
|
||||
|
||||
|
||||
/** @var string */
|
||||
private static $errorMsg;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Starts catching potential errors/warnings
|
||||
* @return void
|
||||
|
@@ -27,7 +27,7 @@
|
||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||
* @package dibi
|
||||
*/
|
||||
class DibiFluent extends DibiObject implements Countable, IteratorAggregate
|
||||
class DibiFluent extends DibiObject implements IDataSource
|
||||
{
|
||||
/** @var array */
|
||||
public static $masks = array(
|
||||
@@ -133,7 +133,7 @@ class DibiFluent extends DibiObject implements Countable, IteratorAggregate
|
||||
} elseif (is_string(key($arg))) { // associative array
|
||||
$args = array('%a', $arg);
|
||||
}
|
||||
}
|
||||
} // case $arg === FALSE is handled below
|
||||
}
|
||||
|
||||
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.
|
||||
* @return string
|
||||
|
@@ -40,7 +40,7 @@
|
||||
* @copyright Copyright (c) 2005, 2009 David Grudl
|
||||
* @package dibi
|
||||
*/
|
||||
class DibiResult extends DibiObject implements Countable, IteratorAggregate
|
||||
class DibiResult extends DibiObject implements IDataSource
|
||||
{
|
||||
/** @var array IDibiDriver */
|
||||
private $driver;
|
||||
|
@@ -29,9 +29,6 @@
|
||||
*/
|
||||
final class DibiTranslator extends DibiObject
|
||||
{
|
||||
/** @var string */
|
||||
public $sql;
|
||||
|
||||
/** @var IDibiDriver */
|
||||
private $driver;
|
||||
|
||||
@@ -81,7 +78,8 @@ final class DibiTranslator extends DibiObject
|
||||
/**
|
||||
* Generates SQL.
|
||||
* @param array
|
||||
* @return bool
|
||||
* @return string
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function translate(array $args)
|
||||
{
|
||||
@@ -174,13 +172,16 @@ final class DibiTranslator extends DibiObject
|
||||
|
||||
$sql = implode(' ', $sql);
|
||||
|
||||
if ($this->hasError) {
|
||||
throw new DibiException('SQL translate error', 0, $sql);
|
||||
}
|
||||
|
||||
// apply limit
|
||||
if ($this->limit > -1 || $this->offset > 0) {
|
||||
$this->driver->applyLimit($sql, $this->limit, $this->offset);
|
||||
}
|
||||
|
||||
$this->sql = $sql;
|
||||
return !$this->hasError;
|
||||
return $sql;
|
||||
}
|
||||
|
||||
|
||||
@@ -197,15 +198,14 @@ final class DibiTranslator extends DibiObject
|
||||
if (is_array($value) || $value instanceof ArrayObject) {
|
||||
|
||||
$vx = $kx = array();
|
||||
$operator = ', ';
|
||||
switch ($modifier) {
|
||||
case 'and':
|
||||
case 'or': // key=val AND key IS NULL AND ...
|
||||
$operator = ' ' . strtoupper($modifier) . ' ';
|
||||
if (empty($value)) {
|
||||
return '1';
|
||||
}
|
||||
|
||||
} else foreach ($value as $k => $v) {
|
||||
foreach ($value as $k => $v) {
|
||||
if (is_string($k)) {
|
||||
$pair = explode('%', $k, 2); // split into identifier & modifier
|
||||
$k = $this->delimite($pair[0]) . ' ';
|
||||
@@ -225,12 +225,12 @@ final class DibiTranslator extends DibiObject
|
||||
$vx[] = $this->formatValue($v, 'sql');
|
||||
}
|
||||
}
|
||||
return implode($operator, $vx);
|
||||
return implode(' ' . strtoupper($modifier) . ' ', $vx);
|
||||
|
||||
case 'n': // identifier names
|
||||
foreach ($value as $k => $v) {
|
||||
if (is_string($k)) {
|
||||
$vx[] = $this->delimite($k) . ' AS ' . $v;
|
||||
$vx[] = $this->delimite($k) . (empty($v) ? '' : ' AS ' . $v);
|
||||
} else {
|
||||
$vx[] = $this->delimite($v);
|
||||
}
|
||||
@@ -244,7 +244,7 @@ final class DibiTranslator extends DibiObject
|
||||
$vx[] = $this->delimite($pair[0]) . '='
|
||||
. $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE);
|
||||
}
|
||||
return implode($operator, $vx);
|
||||
return implode(', ', $vx);
|
||||
|
||||
|
||||
case 'l': // (val, val, ...)
|
||||
@@ -276,8 +276,7 @@ final class DibiTranslator extends DibiObject
|
||||
|
||||
case 'sql':
|
||||
$translator = new self($this->driver);
|
||||
$translator->translate($value);
|
||||
return $translator->sql;
|
||||
return $translator->translate($value);
|
||||
|
||||
default: // value, value, value - all with the same modifier
|
||||
foreach ($value as $v) {
|
||||
@@ -357,7 +356,6 @@ final class DibiTranslator extends DibiObject
|
||||
case 'a':
|
||||
case 'l':
|
||||
case 'v':
|
||||
case 'by':
|
||||
$this->hasError = TRUE;
|
||||
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.
|
||||
* @package dibi
|
||||
|
Reference in New Issue
Block a user