1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 13:47:33 +02:00

- added DibiFluent

- bugfix in dibi::dump()
This commit is contained in:
David Grudl
2008-05-21 11:20:46 +00:00
parent 69876a70b7
commit 3728b16a21
7 changed files with 477 additions and 8 deletions

View File

@@ -52,6 +52,7 @@ require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
require_once dirname(__FILE__) . '/libs/DibiVariable.php';
require_once dirname(__FILE__) . '/libs/DibiTable.php';
require_once dirname(__FILE__) . '/libs/DibiDataSource.php';
require_once dirname(__FILE__) . '/libs/DibiFluent.php';
@@ -424,11 +425,76 @@ class dibi
*/
protected static function __callStatic($name, $args)
{
//if ($name = 'select', 'update', ...') {
// return self::command()->$name($args);
//}
return call_user_func_array(array(self::getConnection(), $name), $args);
}
/********************* fluent SQL builders ****************d*g**/
/**
* @return DibiFluent
*/
public static function command()
{
return new DibiFluent(self::getConnection());
}
/**
* @param string column name
* @return DibiFluent
*/
public static function select($args)
{
$args = func_get_args();
return self::command()->__call('select', $args);
}
/**
* @param string table
* @param array
* @return DibiFluent
*/
public static function update($table, array $args)
{
return self::command()->update('%n', $table)->set($args);
}
/**
* @param string table
* @param array
* @return DibiFluent
*/
public static function insert($table, array $args)
{
return self::command()->insert()
->into('%n', $table, '(%n)', array_keys($args))->values('%l', array_values($args));
}
/**
* @param string table
* @return DibiFluent
*/
public static function delete($table)
{
return self::command()->delete()->from('%n', $table);
}
/********************* data types ****************d*g**/
@@ -593,8 +659,8 @@ class dibi
} else {
if ($sql === NULL) $sql = self::$sql;
static $keywords1 = 'SELECT|UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|TRUE|FALSE';
static $keywords1 = 'SELECT|UPDATE|INSERT(?:\s+INTO)|REPLACE(?:\s+INTO)|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN';
// insert new lines
$sql = ' ' . $sql;
@@ -647,7 +713,8 @@ class dibi
{
return array(
'dibi version: ' . dibi::VERSION,
'Number or queries: ' . dibi::$numOfQueries . (dibi::$totalTime === NULL ? '' : ' (elapsed time: ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)'),
'Number or queries: ' . dibi::$numOfQueries
. (dibi::$totalTime === NULL ? '' : ' (elapsed time: ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)'),
);
}

View File

@@ -272,7 +272,7 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
// see http://dev.mysql.com/doc/refman/5.0/en/select.html
$sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit)
. ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
. ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
}

View File

@@ -252,7 +252,7 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
// see http://dev.mysql.com/doc/refman/5.0/en/select.html
$sql .= ' LIMIT ' . ($limit < 0 ? '18446744073709551615' : (int) $limit)
. ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
. ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
}

312
dibi/libs/DibiFluent.php Normal file
View File

@@ -0,0 +1,312 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2005, 2008 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, 2008 David Grudl
* @license http://dibiphp.com/license dibi license
* @link http://dibiphp.com/
* @package dibi
*/
/**
* dibi SQL builder via fluent interfaces. EXPERIMENTAL!
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
* @version $Revision$ $Date$
*/
final class DibiFluent extends /*Nette::*/Object
{
/** @var array */
public static $masks = array(
'SELECT' => array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'GROUP BY',
'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET', '%end'),
'UPDATE' => array('UPDATE', 'SET', 'WHERE', 'ORDER BY', 'LIMIT', '%end'),
'INSERT' => array('INSERT', 'INTO', 'VALUES', 'SELECT', '%end'),
'DELETE' => array('DELETE', 'FROM', 'USING', 'WHERE', 'ORDER BY', 'LIMIT', '%end'),
);
/** @var array */
public static $separators = array(
'SELECT' => ',',
'FROM' => FALSE,
'WHERE' => 'AND',
'GROUP BY' => ',',
'HAVING' => 'AND',
'ORDER BY' => ',',
'LIMIT' => FALSE,
'OFFSET' => FALSE,
'SET' => ',',
'VALUES' => ',',
'INTO' => FALSE,
);
/** @var DibiConnection */
private $connection;
/** @var string */
private $command;
/** @var array */
private $clauses = array();
/** @var array */
private $flags = array();
/** @var array */
private $cursor;
/**
* @param DibiConnection
*/
public function __construct(DibiConnection $connection)
{
$this->connection = $connection;
}
/**
* Appends new argument to the clause.
* @param string clause name
* @param array arguments
* @return DibiFluent provides a fluent interface
*/
public function __call($clause, $args)
{
$clause = self::_clause($clause);
// lazy initialization
if ($this->command === NULL) {
if (isset(self::$masks[$clause])) {
$this->clauses = array_fill_keys(self::$masks[$clause], NULL);
}
$this->cursor = & $this->clauses[$clause];
$this->cursor = array();
$this->command = $clause;
}
// special types or argument
if (count($args) === 1) {
$arg = $args[0];
if ($arg === TRUE) {
$args = array();
} elseif (is_string($arg) && preg_match('#^[a-z][a-z0-9_.]*$#i', $arg)) {
$args = array('%n', $arg);
}
}
if (array_key_exists($clause, $this->clauses)) {
// append to clause
$this->cursor = & $this->clauses[$clause];
if ($args === array(FALSE)) {
$this->cursor = NULL;
return $this;
}
if (isset(self::$separators[$clause])) {
$sep = self::$separators[$clause];
if ($sep === FALSE) {
$this->cursor = array();
} elseif (!empty($this->cursor)) {
$this->cursor[] = $sep;
}
}
} else {
// append to currect flow
if ($args === array(FALSE)) {
return $this;
}
$this->cursor[] = $clause;
}
if ($this->cursor === NULL) {
$this->cursor = array();
}
array_splice($this->cursor, count($this->cursor), 0, $args);
return $this;
}
/**
* Switch to a clause.
* @param string clause name
* @return DibiFluent provides a fluent interface
*/
public function clause($clause, $remove = FALSE)
{
$this->cursor = & $this->clauses[self::_clause($clause)];
if ($remove) {
$this->cursor = NULL;
} elseif ($this->cursor === NULL) {
$this->cursor = array();
}
return $this;
}
/**
* Change a SQL flag.
* @param string flag name
* @param bool value
* @return DibiFluent provides a fluent interface
*/
public function setFlag($flag, $value = TRUE)
{
$flag = strtoupper($flag);
if ($value) {
$this->flags[$flag] = TRUE;
} else {
unset($this->flags[$flag]);
}
return $this;
}
/**
* Is a flag set?
* @param string flag name
* @return bool
*/
final public function getFlag($flag, $value = TRUE)
{
return isset($this->flags[strtoupper($flag)]);
}
/**
* Returns SQL command.
* @return string
*/
final public function getCommand()
{
return $this->command;
}
/**
* Generates and executes SQL query.
* @return DibiResult|NULL result set object (if any)
* @throws DibiException
*/
public function execute()
{
return $this->connection->query($this->_export());
}
/**
* Generates and prints SQL query or it's part.
* @param string clause name
* @return bool
*/
public function test($clause = NULL)
{
return $this->connection->test($this->_export($clause));
}
/**
* Generates parameters for DibiTranslator.
* @param string clause name
* @return array
*/
protected function _export($clause = NULL)
{
if ($clause === NULL) {
$data = $this->clauses;
} else {
$clause = self::_clause($clause);
if (array_key_exists($clause, $this->clauses)) {
$data = array($clause => $this->clauses[$clause]);
} else {
return array();
}
}
$args = array();
foreach ($data as $clause => $statement) {
if ($statement !== NULL) {
if ($clause[0] !== '%') {
$args[] = $clause;
if ($clause === $this->command) {
$args[] = implode(' ', array_keys($this->flags));
}
}
array_splice($args, count($args), 0, $statement);
}
}
return $args;
}
/**
* Format camelCase clause name to UPPER CASE.
* @param string
* @return string
*/
private static function _clause($s)
{
if ($s === 'order' || $s === 'group') {
$s .= 'By';
trigger_error("Did you mean '$s'?", E_USER_NOTICE);
}
return strtoupper(preg_replace('#[A-Z]#', ' $0', $s));
}
/**
* Returns (highlighted) SQL query.
* @return string
*/
final public function __toString()
{
ob_start();
$this->test();
return ob_get_clean();
}
}
// PHP < 5.2 compatibility
if (!function_exists('array_fill_keys')) {
function array_fill_keys($keys, $value)
{
return array_combine($keys, array_fill(0, count($keys), $value));
}
}

View File

@@ -495,13 +495,36 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
final public function setType($col, $type = NULL, $format = NULL)
/**
* Define column type.
* @param string column
* @param string type (use constant Dibi::FIELD_*)
* @param string optional format
* @return void
*/
final public function setType($col, $type, $format = NULL)
{
$this->xlat[$col] = array($type, $format);
}
/**
* Define multiple columns types (for internal usage).
* @param array
* @return void
*/
final public function setTypes(array $types)
{
$this->xlat = $types;
}
/**
* Returns column type.
* @return array ($type, $format)
*/
final public function getType($col)
{
return isset($this->xlat[$col]) ? $this->xlat[$col] : NULL;
@@ -509,6 +532,10 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
/**
* Converts value to specified type and format
* @return array ($type, $format)
*/
final public function convert($value, $type, $format = NULL)
{
if ($value === NULL || $value === FALSE) {

View File

@@ -130,7 +130,7 @@ final class DibiTranslator extends /*Nette::*/Object
%([a-zA-Z]{1,4})(?![a-zA-Z])|## 8) modifier
)/xs',
*/ // note: this can change $this->args & $this->cursor & ...
. preg_replace_callback('/(?=`|\[|\'|"|%)(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|%([a-zA-Z]{1,4})(?![a-zA-Z]))/s',
. preg_replace_callback('/(?=`|\[|\'|"|%)(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|%([a-zA-Z]{1,4})(?![a-zA-Z]))/s',
array($this, 'cb'),
substr($arg, $toSkip)
);
@@ -219,7 +219,7 @@ final class DibiTranslator extends /*Nette::*/Object
return implode($separator, $vx);
case 'l': // LIST val, val, ...
case 'l': // LIST (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);
@@ -301,7 +301,7 @@ final class DibiTranslator extends /*Nette::*/Object
return $value;
} else {
return substr($value, 0, $toSkip)
. preg_replace_callback('/(?=`|\[|\'|")(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"(\'|"))/s',
. preg_replace_callback('/(?=`|\[|\'|")(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"(\'|"))/s',
array($this, 'cb'),
substr($value, $toSkip)
);

63
examples/fluent.test.php Normal file
View File

@@ -0,0 +1,63 @@
<h1>dibi dump example</h1>
<?php
require_once '../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite',
'database' => 'sample.sdb',
));
$id = 10;
$record = array(
'title' => 'Drtička na trávu',
'price' => 318,
'active' => TRUE,
);
// SELECT ...
dibi::select('product_id')->as('id')
->select('title')
->from('products')
->innerJoin('orders')->using('(product_id)')
->innerJoin('customers USING (customer_id)')
->orderBy('title')
->test();
echo "\n";
// INSERT ...
dibi::insert('products', $record)
->setFlag('IGNORE')
->test();
echo "\n";
// UPDATE ...
dibi::update('products', $record)
->where('product_id = %d', $id)
->test();
echo "\n";
// DELETE ...
dibi::delete('products')
->where('product_id = %d', $id)
->test();
echo "\n";
// custom commands
dibi::command()
->update('products')
->where('product_id = %d', $id)
->set($record)
->test();
echo "\n";
dibi::command()
->truncate('products')
->test();