mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 05:07:36 +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()
|
||||
{
|
||||
return new DibiFluent(self::getConnection());
|
||||
return self::getConnection()->command();
|
||||
}
|
||||
|
||||
|
||||
@@ -472,7 +472,7 @@ class dibi
|
||||
public static function select($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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
return self::command()->insert()
|
||||
->into('%n', $table, '(%n)', array_keys($args))->values('%l', array_values($args));
|
||||
return self::getConnection()->insert($table, $args);
|
||||
}
|
||||
|
||||
|
||||
@@ -508,7 +507,7 @@ class dibi
|
||||
*/
|
||||
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) {
|
||||
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($config['database'])) {
|
||||
|
@@ -124,14 +124,14 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
|
||||
$ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); // intentionally @
|
||||
if (!$ok) {
|
||||
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($config['sqlmode'])) {
|
||||
if (!@mysqli_query($this->connection, "SET sql_mode='$config[sqlmode]'")) { // intentionally @
|
||||
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$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!
|
||||
*
|
||||
|
@@ -236,7 +236,7 @@ class DibiFluent extends DibiObject
|
||||
if ($this->command === 'SELECT') {
|
||||
$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') {
|
||||
$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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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 ($leaf === '=') {
|
||||
$x = $row;
|
||||
$x = $row;
|
||||
} elseif ($leaf === TRUE || $leaf === '@') {
|
||||
$x = (object) $row;
|
||||
} else {
|
||||
|
@@ -132,6 +132,10 @@ abstract class DibiTable extends DibiObject
|
||||
|
||||
|
||||
|
||||
/********************* basic commands ****************d*g**/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Inserts row into a table.
|
||||
* @param array|object
|
||||
@@ -297,4 +301,31 @@ abstract class DibiTable extends DibiObject
|
||||
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 {
|
||||
foreach ($value as $k => $v) {
|
||||
$pair = explode('%', $k, 2); // split into identifier & modifier
|
||||
$k = $this->delimite($pair[0]);
|
||||
$k = $this->delimite($pair[0]);
|
||||
if (isset($pair[1])) {
|
||||
$pair = explode(' ', $pair[1], 2); // split into modifier & operator
|
||||
$op = isset($pair[1]) ? $pair[1] : '=';
|
||||
|
@@ -87,3 +87,7 @@ var_dump($id); // generated id
|
||||
$key = '3 OR 1=1';
|
||||
$products->delete($key);
|
||||
// --> 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";
|
||||
|
||||
// SELECT ...
|
||||
echo dibi::select('title')->as('id')
|
||||
->from('products')
|
||||
->fetchSingle();
|
||||
|
||||
echo "\n";
|
||||
|
||||
// INSERT ...
|
||||
dibi::insert('products', $record)
|
||||
->setFlag('IGNORE')
|
||||
|
Reference in New Issue
Block a user