1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 14:16:39 +02:00

- Object renamed to DibiObject

- DibiTranslator: improved %and and %or handling
- DibiTable::findAll allows to add conditions
This commit is contained in:
David Grudl
2008-09-05 05:35:15 +00:00
parent f935968aa7
commit e5af8a8c67
19 changed files with 73 additions and 51 deletions

View File

@@ -56,16 +56,13 @@ if (!class_exists('FileNotFoundException', FALSE)) {
class FileNotFoundException extends IOException {}
}
if (!class_exists(/*Nette::*/'Object', FALSE)) {
require_once dirname(__FILE__) . '/Nette/Object.php';
}
if (!interface_exists(/*Nette::*/'IDebuggable', FALSE)) {
require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
}
// dibi libraries
require_once dirname(__FILE__) . '/libs/interfaces.php';
require_once dirname(__FILE__) . '/libs/DibiObject.php';
require_once dirname(__FILE__) . '/libs/DibiException.php';
require_once dirname(__FILE__) . '/libs/DibiConnection.php';
require_once dirname(__FILE__) . '/libs/DibiResult.php';

View File

@@ -34,7 +34,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiMsSqlDriver extends /*Nette::*/Object implements IDibiDriver
class DibiMsSqlDriver extends DibiObject implements IDibiDriver
{
/**

View File

@@ -40,7 +40,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
class DibiMySqlDriver extends DibiObject implements IDibiDriver
{
/**

View File

@@ -40,7 +40,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
class DibiMySqliDriver extends DibiObject implements IDibiDriver
{
/**

View File

@@ -33,7 +33,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
class DibiOdbcDriver extends DibiObject implements IDibiDriver
{
/**

View File

@@ -33,7 +33,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver
class DibiOracleDriver extends DibiObject implements IDibiDriver
{
/**

View File

@@ -34,7 +34,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
class DibiPdoDriver extends DibiObject implements IDibiDriver
{
/**

View File

@@ -34,7 +34,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
class DibiPostgreDriver extends DibiObject implements IDibiDriver
{
/**

View File

@@ -34,7 +34,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiSqliteDriver extends /*Nette::*/Object implements IDibiDriver
class DibiSqliteDriver extends DibiObject implements IDibiDriver
{
/**

View File

@@ -27,7 +27,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiConnection extends /*Nette::*/Object
class DibiConnection extends DibiObject
{
/**
* Current connection configuration.

View File

@@ -27,7 +27,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiDataSource extends /*Nette::*/Object implements IDataSource
class DibiDataSource extends DibiObject implements IDataSource
{
/** @var DibiConnection */
private $connection;

View File

@@ -27,7 +27,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiFluent extends /*Nette::*/Object
class DibiFluent extends DibiObject
{
/** @var array */
public static $masks = array(

View File

@@ -27,7 +27,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
final class DibiLogger extends /*Nette::*/Object
final class DibiLogger extends DibiObject
{
/** @var string Name of the file where SQL errors should be logged */
private $file;

View File

@@ -1,27 +1,29 @@
<?php
/**
* Nette Framework
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2004, 2008 David Grudl (http://davidgrudl.com)
* Copyright (c) 2005, 2008 David Grudl (http://davidgrudl.com)
*
* This source file is subject to the "Nette license" that is bundled
* 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://nettephp.com
* For more information please see http://dibiphp.com
*
* @copyright Copyright (c) 2004, 2008 David Grudl
* @license http://nettephp.com/license Nette license
* @link http://nettephp.com
* @category Nette
* @package Nette
* @copyright Copyright (c) 2005, 2008 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com
* @package dibi
* @version $Id$
*/
/*namespace Nette;*/
/**
* Nette::Object is the ultimate ancestor of all instantiable classes.
* DibiObject is the ultimate ancestor of all instantiable classes.
*
* DibiObject is copy of Nette::Object from Nette Framework (http://nettephp.com).
*
* It defines some handful methods and enhances object core of PHP:
* - access to undeclared members throws exceptions
@@ -57,10 +59,10 @@
* </code>
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2008 David Grudl
* @package Nette
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
abstract class Object
abstract class DibiObject
{
/** @var array (method => array(type => callback)) */
private static $extMethods;

View File

@@ -41,7 +41,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiResult extends /*Nette::*/Object implements IDataSource
class DibiResult extends DibiObject implements IDataSource
{
/**
* IDibiDriver.

View File

@@ -26,7 +26,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
abstract class DibiTable extends /*Nette::*/Object
abstract class DibiTable extends DibiObject
{
/** @var string primary key mask */
public static $primaryMask = 'id';
@@ -204,22 +204,23 @@ abstract class DibiTable extends /*Nette::*/Object
/**
* Selects all rows.
* @param array conditions
* @param string column to order by
* @return DibiResult
*/
public function findAll($order = NULL)
public function findAll($conditions = NULL, $order = NULL)
{
if ($order === NULL) {
return $this->complete($this->connection->query(
'SELECT * FROM %n', $this->name
));
$order = func_get_args();
if (is_array($conditions)) {
array_shift($order);
} else {
$order = func_get_args();
return $this->complete($this->connection->query(
'SELECT * FROM %n', $this->name,
'ORDER BY %n', $order
));
$conditions = NULL;
}
return $this->complete($this->connection->query(
'SELECT * FROM %n', $this->name,
'%ex', $conditions ? array('WHERE %and', $conditions) : NULL,
'%ex', $order ? array('ORDER BY %n', $order) : NULL
));
}

View File

@@ -27,7 +27,7 @@
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
final class DibiTranslator extends /*Nette::*/Object
final class DibiTranslator extends DibiObject
{
/** @var string */
public $sql;
@@ -201,16 +201,37 @@ final class DibiTranslator extends /*Nette::*/Object
$separator = ', ';
switch ($modifier) {
case 'and':
case 'or':
case 'or': // key=val AND key IS NULL AND ...
$separator = ' ' . strtoupper($modifier) . ' ';
if (!is_string(key($value))) {
if (empty($value)) {
return '1';
} elseif (!is_string(key($value))) {
foreach ($value as $v) {
$vx[] = $this->formatValue($v, 'sql');
}
return implode($separator, $vx);
} else {
foreach ($value as $k => $v) {
$pair = explode('%', $k, 2); // split into identifier & modifier
$k = $this->delimite($pair[0]);
if (isset($pair[1])) {
$pair = explode(' ', $pair[1], 2); // split into modifier & operator
$op = isset($pair[1]) ? $pair[1] : '=';
$v = $this->formatValue($v, $pair[0]);
} else {
$op = '=';
$v = $this->formatValue($v, FALSE);
}
if ($v === 'NULL') {
$op = 'IS';
}
$vx[] = $k . ' ' . $op . ' ' . $v;
}
}
// break intentionally omitted
case 'a': // SET key=val, key=val, ...
return implode($separator, $vx);
case 'a': // key=val, key=val, ...
foreach ($value as $k => $v) {
$pair = explode('%', $k, 2); // split into identifier & modifier
$vx[] = $this->delimite($pair[0]) . '='
@@ -219,7 +240,7 @@ final class DibiTranslator extends /*Nette::*/Object
return implode($separator, $vx);
case 'l': // LIST (val, val, ...)
case 'l': // (val, val, ...)
foreach ($value as $k => $v) {
$pair = explode('%', $k, 2); // split into identifier & modifier
$vx[] = $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE);
@@ -235,7 +256,7 @@ final class DibiTranslator extends /*Nette::*/Object
}
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
default:
default: // value, value, value - all with the same modifier
foreach ($value as $v) {
$vx[] = $this->formatValue($v, $modifier);
}

View File

@@ -24,7 +24,7 @@
* Default implemenation of IDibiVariable.
* @package dibi
*/
class DibiVariable extends /*Nette::*/Object implements IDibiVariable
class DibiVariable extends DibiObject implements IDibiVariable
{
/** @var mixed */
public $value;

View File

@@ -49,6 +49,7 @@ $products->findAll()->dump();
// select all, order by title, product_id
$products->findAll('title', $products->primary)->dump();
$products->findAll(array('title' => 'Chair'), 'title')->dump();
// fetches single row with id 3