From 1b17a4bfbb6bf435ddad8afd408fe27decf36d12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 12 Apr 2016 13:19:46 +0200 Subject: [PATCH 1/3] Added support for * event matching --- README.md | 2 ++ src/Pixie/EventHandler.php | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 82ca0ed..d280e5a 100644 --- a/README.md +++ b/README.md @@ -566,6 +566,8 @@ Pixie comes with powerful query events to supercharge your application. These ev #### Available Events + - after-* + - before-* - before-select - after-select - before-insert diff --git a/src/Pixie/EventHandler.php b/src/Pixie/EventHandler.php index 5d202ad..42c399f 100644 --- a/src/Pixie/EventHandler.php +++ b/src/Pixie/EventHandler.php @@ -34,6 +34,19 @@ public function getEvent($event, $table = ':any') if ($table instanceof Raw) { return null; } + + // Find event with * + if(isset($this->events[$table])) { + foreach($this->events[$table] as $name => $e) { + if (stripos($name, '*') > - 1) { + $name = substr($name, 0, strpos($name, '*')); + if (stripos($event, $name) > - 1) { + return $e; + } + } + } + } + return isset($this->events[$table][$event]) ? $this->events[$table][$event] : null; } @@ -95,4 +108,4 @@ public function fireEvents($queryBuilder, $event) } } } -} +} \ No newline at end of file From 6d07d8ab6506e2d578f005ee789c42a228284ad5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 2 Jun 2016 20:07:30 +0200 Subject: [PATCH 2/3] [FEATURE] Removed support for not defining table. --- .../QueryBuilder/Adapters/BaseAdapter.php | 26 ++++++--------- tests/Pixie/NoTableSubQueryTest.php | 33 +++++++++++++++++++ tests/Pixie/QueryBuilderTest.php | 9 +---- 3 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 tests/Pixie/NoTableSubQueryTest.php diff --git a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php index ade1a1b..90a3844 100644 --- a/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php +++ b/src/Pixie/QueryBuilder/Adapters/BaseAdapter.php @@ -32,14 +32,18 @@ public function __construct(Connection $connection) */ public function select($statements) { - if (!array_key_exists('tables', $statements)) { - throw new Exception('No table specified.', 3); - } elseif (!array_key_exists('selects', $statements)) { + if (!array_key_exists('selects', $statements)) { $statements['selects'][] = '*'; } // From - $tables = $this->arrayStr($statements['tables'], ', '); + $fromEnabled = false; + $tables = ''; + + if(isset($statements['tables'])) { + $tables = $this->arrayStr($statements['tables'], ', '); + $fromEnabled = true; + } // Select $selects = $this->arrayStr($statements['selects'], ', '); @@ -77,7 +81,7 @@ public function select($statements) $sqlArray = array( 'SELECT' . (isset($statements['distinct']) ? ' DISTINCT' : ''), $selects, - 'FROM', + (($fromEnabled) ? 'FROM' : ''), $tables, $joinString, $whereCriteria, @@ -129,10 +133,6 @@ public function criteriaOnly($statements, $bindValues = true) */ private function doInsert($statements, array $data, $type) { - if (!isset($statements['tables'])) { - throw new Exception('No table specified', 3); - } - $table = end($statements['tables']); $bindings = $keys = $values = array(); @@ -247,9 +247,7 @@ private function getUpdateStatement($data) */ public function update($statements, array $data) { - if (!isset($statements['tables'])) { - throw new Exception('No table specified', 3); - } elseif (count($data) < 1) { + if (count($data) < 1) { throw new Exception('No data given.', 4); } @@ -288,10 +286,6 @@ public function update($statements, array $data) */ public function delete($statements) { - if (!isset($statements['tables'])) { - throw new Exception('No table specified', 3); - } - $table = end($statements['tables']); // Wheres diff --git a/tests/Pixie/NoTableSubQueryTest.php b/tests/Pixie/NoTableSubQueryTest.php new file mode 100644 index 0000000..9dc0b92 --- /dev/null +++ b/tests/Pixie/NoTableSubQueryTest.php @@ -0,0 +1,33 @@ +builder = new QueryBuilderHandler($this->mockConnection); + } + + public function testRawQuery() + { + + $subQuery1 = $this->builder->table('mail')->select($this->builder->raw('COUNT(*)')); + $subQuery2 = $this->builder->table('event_message')->select($this->builder->raw('COUNT(*)')); + + $count = $this->builder->select($this->builder->subQuery($subQuery1, 'row1'), $this->builder->subQuery($subQuery2, 'row2'))->first(); + + $this->assertEquals('SELECT (SELECT COUNT(*) FROM `cb_mail`) as row1, (SELECT COUNT(*) FROM `cb_event_message`) as row2 LIMIT 1', $count); + + } + +} \ No newline at end of file diff --git a/tests/Pixie/QueryBuilderTest.php b/tests/Pixie/QueryBuilderTest.php index ec0ec73..224fb56 100644 --- a/tests/Pixie/QueryBuilderTest.php +++ b/tests/Pixie/QueryBuilderTest.php @@ -86,12 +86,5 @@ public function testInsertQueryReturnsNullForIgnoredInsert() $this->assertEquals(null, $id); } - - /** - * @expectedException \Pixie\Exception - * @expectedExceptionCode 3 - */ - public function testTableNotSpecifiedException(){ - $this->builder->where('a', 'b')->get(); - } + } \ No newline at end of file From e6ee96f407d6b78e7f0eb278cfd34047c292071c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 2 Jun 2016 20:35:01 +0200 Subject: [PATCH 3/3] Added section in readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index d280e5a..782b0b9 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ Library on [Packagist](https://packagist.org/packages/usmanhalalit/pixie). - [Get All](#get-all) - [Get First Row](#get-first-row) - [Get Rows Count](#get-rows-count) + - [Selects With Sub-Queries](#select-with-sub-queries) - [**Where**](#where) - [Where In](#where-in) - [Where Between](#where-between) @@ -258,6 +259,21 @@ $query = QB::table('my_table')->where('name', '=', 'Sana'); $query->count(); ``` +#### Select With Sub-Queries + +```PHP +$subQuery1 = $this->builder->table('mail')->select($this->builder->raw('COUNT(*)')); +$subQuery2 = $this->builder->table('event_message')->select($this->builder->raw('COUNT(*)')); + +$count = $this->builder->select($this->builder->subQuery($subQuery1, 'row1'), $this->builder->subQuery($subQuery2, 'row2'))->first(); +``` + +Will produce the following query: + +```sql +SELECT (SELECT COUNT(*) FROM `cb_mail`) as row1, (SELECT COUNT(*) FROM `cb_event_message`) as row2 LIMIT 1 +``` + ### Where Basic syntax is `(fieldname, operator, value)`, if you give two parameters then `=` operator is assumed. So `where('name', 'usman')` and `where('name', '=', 'usman')` is the same.