mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 13:17:58 +02:00
- added fluent SQL builders support in DibiConnection and DibiTable
This commit is contained in:
@@ -460,7 +460,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function command()
|
public static function command()
|
||||||
{
|
{
|
||||||
return new DibiFluent(self::getConnection());
|
return self::getConnection()->command();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -472,7 +472,7 @@ class dibi
|
|||||||
public static function select($args)
|
public static function select($args)
|
||||||
{
|
{
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
return self::command()->__call('select', $args);
|
return self::getConnection()->command()->__call('select', $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -484,7 +484,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function update($table, array $args)
|
public static function update($table, array $args)
|
||||||
{
|
{
|
||||||
return self::command()->update('%n', $table)->set($args);
|
return self::getConnection()->update($table, $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -496,8 +496,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function insert($table, array $args)
|
public static function insert($table, array $args)
|
||||||
{
|
{
|
||||||
return self::command()->insert()
|
return self::getConnection()->insert($table, $args);
|
||||||
->into('%n', $table, '(%n)', array_keys($args))->values('%l', array_values($args));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -508,7 +507,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function delete($table)
|
public static function delete($table)
|
||||||
{
|
{
|
||||||
return self::command()->delete()->from('%n', $table);
|
return self::getConnection()->delete($table);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -131,7 +131,7 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
|
|||||||
if (!$ok) {
|
if (!$ok) {
|
||||||
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
|
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($config['database'])) {
|
if (isset($config['database'])) {
|
||||||
|
@@ -124,14 +124,14 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
|
|||||||
$ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); // intentionally @
|
$ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); // intentionally @
|
||||||
if (!$ok) {
|
if (!$ok) {
|
||||||
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
|
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($config['sqlmode'])) {
|
if (isset($config['sqlmode'])) {
|
||||||
if (!@mysqli_query($this->connection, "SET sql_mode='$config[sqlmode]'")) { // intentionally @
|
if (!@mysqli_query($this->connection, "SET sql_mode='$config[sqlmode]'")) { // intentionally @
|
||||||
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
|
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->buffered = empty($config['unbuffered']);
|
$this->buffered = empty($config['unbuffered']);
|
||||||
|
@@ -456,6 +456,72 @@ class DibiConnection extends DibiObject
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********************* fluent SQL builders ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return DibiFluent
|
||||||
|
*/
|
||||||
|
public function command()
|
||||||
|
{
|
||||||
|
return new DibiFluent($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string column name
|
||||||
|
* @return DibiFluent
|
||||||
|
*/
|
||||||
|
public function select($args)
|
||||||
|
{
|
||||||
|
$args = func_get_args();
|
||||||
|
return $this->command()->__call('select', $args);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string table
|
||||||
|
* @param array
|
||||||
|
* @return DibiFluent
|
||||||
|
*/
|
||||||
|
public function update($table, array $args)
|
||||||
|
{
|
||||||
|
return $this->command()->update('%n', $table)->set($args);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string table
|
||||||
|
* @param array
|
||||||
|
* @return DibiFluent
|
||||||
|
*/
|
||||||
|
public function insert($table, array $args)
|
||||||
|
{
|
||||||
|
return $this->command()->insert()
|
||||||
|
->into('%n', $table, '(%n)', array_keys($args))->values('%l', array_values($args));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string table
|
||||||
|
* @return DibiFluent
|
||||||
|
*/
|
||||||
|
public function delete($table)
|
||||||
|
{
|
||||||
|
return $this->command()->delete()->from('%n', $table);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********************* misc ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Import SQL dump from file - extreme fast!
|
* Import SQL dump from file - extreme fast!
|
||||||
*
|
*
|
||||||
|
@@ -236,7 +236,7 @@ class DibiFluent extends DibiObject
|
|||||||
if ($this->command === 'SELECT') {
|
if ($this->command === 'SELECT') {
|
||||||
$this->clauses['LIMIT'] = array(1);
|
$this->clauses['LIMIT'] = array(1);
|
||||||
}
|
}
|
||||||
return $this->connection->query($this->_export())->fetch();
|
return $this->execute()->fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -250,7 +250,7 @@ class DibiFluent extends DibiObject
|
|||||||
if ($this->command === 'SELECT') {
|
if ($this->command === 'SELECT') {
|
||||||
$this->clauses['LIMIT'] = array(1);
|
$this->clauses['LIMIT'] = array(1);
|
||||||
}
|
}
|
||||||
return $this->connection->query($this->_export())->fetchSingle();
|
return $this->execute()->fetchSingle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -264,7 +264,7 @@ class DibiFluent extends DibiObject
|
|||||||
*/
|
*/
|
||||||
public function fetchAll($offset = NULL, $limit = NULL, $simplify = TRUE)
|
public function fetchAll($offset = NULL, $limit = NULL, $simplify = TRUE)
|
||||||
{
|
{
|
||||||
return $this->connection->query($this->_export())->fetchAll($offset, $limit, $simplify);
|
return $this->execute()->fetchAll($offset, $limit, $simplify);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -279,7 +279,7 @@ class DibiFluent extends DibiObject
|
|||||||
*/
|
*/
|
||||||
public function fetchAssoc($assoc)
|
public function fetchAssoc($assoc)
|
||||||
{
|
{
|
||||||
return $this->connection->query($this->_export())->fetchAssoc($assoc);
|
return $this->execute()->fetchAssoc($assoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -293,7 +293,7 @@ class DibiFluent extends DibiObject
|
|||||||
*/
|
*/
|
||||||
public function fetchPairs($key = NULL, $value = NULL)
|
public function fetchPairs($key = NULL, $value = NULL)
|
||||||
{
|
{
|
||||||
return $this->connection->query($this->_export())->fetchPairs($key, $value);
|
return $this->execute()->fetchPairs($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -419,7 +419,7 @@ class DibiResult extends DibiObject implements IDataSource
|
|||||||
|
|
||||||
if ($x === NULL) { // build leaf
|
if ($x === NULL) { // build leaf
|
||||||
if ($leaf === '=') {
|
if ($leaf === '=') {
|
||||||
$x = $row;
|
$x = $row;
|
||||||
} elseif ($leaf === TRUE || $leaf === '@') {
|
} elseif ($leaf === TRUE || $leaf === '@') {
|
||||||
$x = (object) $row;
|
$x = (object) $row;
|
||||||
} else {
|
} else {
|
||||||
|
@@ -132,6 +132,10 @@ abstract class DibiTable extends DibiObject
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********************* basic commands ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts row into a table.
|
* Inserts row into a table.
|
||||||
* @param array|object
|
* @param array|object
|
||||||
@@ -297,4 +301,31 @@ abstract class DibiTable extends DibiObject
|
|||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********************* fluent SQL builders ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates fluent SQL builder.
|
||||||
|
* @return DibiFluent
|
||||||
|
*/
|
||||||
|
public function command()
|
||||||
|
{
|
||||||
|
return new DibiFluent($this->connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string column name
|
||||||
|
* @return DibiFluent
|
||||||
|
*/
|
||||||
|
public function select($args)
|
||||||
|
{
|
||||||
|
$args = func_get_args();
|
||||||
|
return $this->command()->__call('select', $args)->from($this->name);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -214,7 +214,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
} else {
|
} else {
|
||||||
foreach ($value as $k => $v) {
|
foreach ($value as $k => $v) {
|
||||||
$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]);
|
||||||
if (isset($pair[1])) {
|
if (isset($pair[1])) {
|
||||||
$pair = explode(' ', $pair[1], 2); // split into modifier & operator
|
$pair = explode(' ', $pair[1], 2); // split into modifier & operator
|
||||||
$op = isset($pair[1]) ? $pair[1] : '=';
|
$op = isset($pair[1]) ? $pair[1] : '=';
|
||||||
|
@@ -87,3 +87,7 @@ var_dump($id); // generated id
|
|||||||
$key = '3 OR 1=1';
|
$key = '3 OR 1=1';
|
||||||
$products->delete($key);
|
$products->delete($key);
|
||||||
// --> DELETE FROM [products] WHERE [product_id] IN ( 3 )
|
// --> DELETE FROM [products] WHERE [product_id] IN ( 3 )
|
||||||
|
|
||||||
|
|
||||||
|
// select all using fluent interface
|
||||||
|
var_dump($products->select('*')->orderBy('title')->fetchAll());
|
||||||
|
@@ -28,6 +28,13 @@ dibi::select('product_id')->as('id')
|
|||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
|
// SELECT ...
|
||||||
|
echo dibi::select('title')->as('id')
|
||||||
|
->from('products')
|
||||||
|
->fetchSingle();
|
||||||
|
|
||||||
|
echo "\n";
|
||||||
|
|
||||||
// INSERT ...
|
// INSERT ...
|
||||||
dibi::insert('products', $record)
|
dibi::insert('products', $record)
|
||||||
->setFlag('IGNORE')
|
->setFlag('IGNORE')
|
||||||
|
Reference in New Issue
Block a user