1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-04 21:28:02 +02:00

- DibiFluent implements Countable, IteratorAggregate

- DibiDataSource is deprecated
- DibiTranslator - fixed DateTime class support
This commit is contained in:
David Grudl
2009-02-05 01:26:08 +00:00
parent a5a1da19a7
commit bf6dc1cbd1
8 changed files with 136 additions and 74 deletions

View File

@@ -67,6 +67,7 @@ require_once dirname(__FILE__) . '/libs/DibiException.php';
require_once dirname(__FILE__) . '/libs/DibiConnection.php'; require_once dirname(__FILE__) . '/libs/DibiConnection.php';
require_once dirname(__FILE__) . '/libs/DibiResult.php'; require_once dirname(__FILE__) . '/libs/DibiResult.php';
require_once dirname(__FILE__) . '/libs/DibiResultIterator.php'; require_once dirname(__FILE__) . '/libs/DibiResultIterator.php';
require_once dirname(__FILE__) . '/libs/DibiRow.php';
require_once dirname(__FILE__) . '/libs/DibiTranslator.php'; require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
require_once dirname(__FILE__) . '/libs/DibiVariable.php'; require_once dirname(__FILE__) . '/libs/DibiVariable.php';
require_once dirname(__FILE__) . '/libs/DibiTableX.php'; require_once dirname(__FILE__) . '/libs/DibiTableX.php';
@@ -116,10 +117,13 @@ class dibi
const REVISION = '$WCREV$ released on $WCDATE$'; const REVISION = '$WCREV$ released on $WCDATE$';
/**#@-*/ /**#@-*/
/** /**#@+
* Configuration options * Configuration options
*/ */
const RESULT_WITH_TABLES = 'resultWithTables'; // for MySQL const RESULT_WITH_TABLES = 'resultWithTables'; // for MySQL
const ROW_CLASS = 'rowClass';
const ASC = 'ASC', DESC = 'DESC';
/**#@-*/
/** @var DibiConnection[] Connection registry storage for DibiConnection objects */ /** @var DibiConnection[] Connection registry storage for DibiConnection objects */
private static $registry = array(); private static $registry = array();

View File

@@ -20,12 +20,26 @@
/**
* 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
{ {

View File

@@ -27,18 +27,18 @@
* @copyright Copyright (c) 2005, 2009 David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl
* @package dibi * @package dibi
*/ */
class DibiFluent extends DibiObject class DibiFluent extends DibiObject implements Countable, IteratorAggregate
{ {
/** @var array */ /** @var array */
public static $masks = array( public static $masks = array(
'SELECT' => array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY', 'SELECT' => array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY',
'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET', '%end'), 'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET'),
'UPDATE' => array('UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT', '%end'), 'UPDATE' => array('UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT'),
'INSERT' => array('INSERT', 'INTO', 'VALUES', 'SELECT', '%end'), 'INSERT' => array('INSERT', 'INTO', 'VALUES', 'SELECT'),
'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT', '%end'), 'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT'),
); );
/** @var array */ /** @var array default modifiers for arrays */
public static $modifiers = array( public static $modifiers = array(
'SELECT' => '%n', 'SELECT' => '%n',
'FROM' => '%n', 'FROM' => '%n',
@@ -51,7 +51,7 @@ class DibiFluent extends DibiObject
'GROUP BY' => '%by', 'GROUP BY' => '%by',
); );
/** @var array */ /** @var array clauses separators */
public static $separators = array( public static $separators = array(
'SELECT' => ',', 'SELECT' => ',',
'FROM' => FALSE, 'FROM' => FALSE,
@@ -258,9 +258,10 @@ class DibiFluent extends DibiObject
public function fetch() public function fetch()
{ {
if ($this->command === 'SELECT') { if ($this->command === 'SELECT') {
$this->clauses['LIMIT'] = array(1); return $this->connection->query($this->_export(NULL, array('%lmt', 1)))->fetch();
} else {
return $this->connection->query($this->_export())->fetch();
} }
return $this->execute()->fetch();
} }
@@ -272,9 +273,10 @@ class DibiFluent extends DibiObject
public function fetchSingle() public function fetchSingle()
{ {
if ($this->command === 'SELECT') { if ($this->command === 'SELECT') {
$this->clauses['LIMIT'] = array(1); return $this->connection->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle();
} else {
return $this->connection->query($this->_export())->fetchSingle();
} }
return $this->execute()->fetchSingle();
} }
@@ -287,7 +289,7 @@ class DibiFluent extends DibiObject
*/ */
public function fetchAll($offset = NULL, $limit = NULL) public function fetchAll($offset = NULL, $limit = NULL)
{ {
return $this->execute()->fetchAll($offset, $limit); return $this->connection->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->fetchAll();
} }
@@ -302,7 +304,7 @@ class DibiFluent extends DibiObject
*/ */
public function fetchAssoc($assoc) public function fetchAssoc($assoc)
{ {
return $this->execute()->fetchAssoc($assoc); return $this->connection->query($this->_export())->fetchAssoc($assoc);
} }
@@ -316,7 +318,32 @@ class DibiFluent extends DibiObject
*/ */
public function fetchPairs($key = NULL, $value = NULL) public function fetchPairs($key = NULL, $value = NULL)
{ {
return $this->execute()->fetchPairs($key, $value); return $this->connection->query($this->_export())->fetchPairs($key, $value);
}
/**
* Required by the IteratorAggregate interface.
* @param int offset
* @param int limit
* @return DibiResultIterator
*/
public function getIterator($offset = NULL, $limit = NULL)
{
return $this->connection->query($this->_export(NULL, array('%ofs %lmt', $offset, $limit)))->getIterator();
}
/**
* @return int
*/
public function count()
{
return (int) $this->connection->query(
'SELECT COUNT(*) FROM (%ex', $this->_export(), ') AS [data]'
)->fetchSingle();
} }
@@ -338,7 +365,7 @@ class DibiFluent extends DibiObject
* @param string clause name * @param string clause name
* @return array * @return array
*/ */
protected function _export($clause = NULL) protected function _export($clause = NULL, $args = array())
{ {
if ($clause === NULL) { if ($clause === NULL) {
$data = $this->clauses; $data = $this->clauses;
@@ -352,18 +379,16 @@ class DibiFluent extends DibiObject
} }
} }
$args = array();
foreach ($data as $clause => $statement) { foreach ($data as $clause => $statement) {
if ($statement !== NULL) { if ($statement !== NULL) {
if ($clause[0] !== '%') { $args[] = $clause;
$args[] = $clause; if ($clause === $this->command) {
if ($clause === $this->command) { $args[] = implode(' ', array_keys($this->flags));
$args[] = implode(' ', array_keys($this->flags));
}
} }
array_splice($args, count($args), 0, $statement); array_splice($args, count($args), 0, $statement);
} }
} }
return $args; return $args;
} }
@@ -395,6 +420,17 @@ class DibiFluent extends DibiObject
return $this->connection->sql($this->_export()); return $this->connection->sql($this->_export());
} }
/**
* Returns the dibi connection.
* @return DibiConnection
*/
final public function getConnection()
{
return $this->connection;
}
} }

View File

@@ -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 IDataSource class DibiResult extends DibiObject implements Countable, IteratorAggregate
{ {
/** @var array IDibiDriver */ /** @var array IDibiDriver */
private $driver; private $driver;
@@ -639,37 +639,3 @@ class DibiResult extends DibiObject implements IDataSource
} }
} }
/**
* dibi result-set row
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2009 David Grudl
* @package dibi
*/
class DibiRow extends ArrayObject
{
/**
* @param array
*/
public function __construct($arr)
{
parent::__construct($arr, 2);
}
/**
* PHP < 5.3 workaround
* @return void
*/
public function __wakeup()
{
$this->setFlags(2);
}
}

52
dibi/libs/DibiRow.php Normal file
View File

@@ -0,0 +1,52 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2005, 2009 David Grudl (http://davidgrudl.com)
*
* This source file is subject to the "dibi license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://dibiphp.com
*
* @copyright Copyright (c) 2005, 2009 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
* @package dibi
* @version $Id$
*/
/**
* dibi result-set row
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2009 David Grudl
* @package dibi
*/
class DibiRow extends ArrayObject
{
/**
* @param array
*/
public function __construct($arr)
{
parent::__construct($arr, 2);
}
/**
* PHP < 5.3 workaround
* @return void
*/
public function __wakeup()
{
$this->setFlags(2);
}
}

View File

@@ -25,6 +25,7 @@
* @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
*/ */
abstract class DibiTableX extends DibiObject abstract class DibiTableX extends DibiObject
{ {

View File

@@ -296,9 +296,11 @@ final class DibiTranslator extends DibiObject
if ($value instanceof IDibiVariable) { if ($value instanceof IDibiVariable) {
return $value->toSql($this, $modifier); return $value->toSql($this, $modifier);
}
if (!is_scalar($value)) { // array is already processed } elseif ($value instanceof DateTime) {
$value = $value->format('U');
} elseif (!is_scalar($value)) { // array is already processed
$this->hasError = TRUE; $this->hasError = TRUE;
return '**Unexpected type ' . gettype($value) . '**'; return '**Unexpected type ' . gettype($value) . '**';
} }
@@ -329,7 +331,7 @@ final class DibiTranslator extends DibiObject
case 'd': // date case 'd': // date
case 't': // datetime case 't': // datetime
$value = is_numeric($value) ? (int) $value : ($value instanceof DateTime ? $value->format('U') : strtotime($value)); $value = is_numeric($value) ? (int) $value : strtotime($value);
return $this->driver->escape($value, $modifier); return $this->driver->escape($value, $modifier);
case 'by': case 'by':
@@ -355,6 +357,7 @@ 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) . '**';

View File

@@ -39,20 +39,6 @@ interface IDibiVariable
/**
* Provides an interface between a dataset and data-aware components.
* @package dibi
*/
interface IDataSource extends Countable, IteratorAggregate
{
//function IteratorAggregate::getIterator();
//function Countable::count();
}
/** /**
* Defines method that must profiler implement. * Defines method that must profiler implement.
* @package dibi * @package dibi