diff --git a/dibi/dibi.php b/dibi/dibi.php index f037a19c..f7b89658 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -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); } diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 9c165075..ffdfb103 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -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'])) { diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 98690fc4..97684e59 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -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']); diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 1e0f147d..316e05ea 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -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! * diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index 27054cd4..3bcbf75b 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -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); } diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index c13b0625..fdb24c79 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -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 { diff --git a/dibi/libs/DibiTable.php b/dibi/libs/DibiTable.php index 3908bd3f..13204661 100644 --- a/dibi/libs/DibiTable.php +++ b/dibi/libs/DibiTable.php @@ -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); + } + } diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index 36995d25..cd3063f8 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -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] : '='; diff --git a/examples/dibi.table.php b/examples/dibi.table.php index 6d321b9a..01a1ea5c 100644 --- a/examples/dibi.table.php +++ b/examples/dibi.table.php @@ -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()); diff --git a/examples/fluent.test.php b/examples/fluent.test.php index 374a6363..75cd2739 100644 --- a/examples/fluent.test.php +++ b/examples/fluent.test.php @@ -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')