Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x i would like to pass an array to the where function #199

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -566,6 +582,8 @@ Pixie comes with powerful query events to supercharge your application. These ev

#### Available Events

- after-*
- before-*
- before-select
- after-select
- before-insert
Expand Down
15 changes: 14 additions & 1 deletion src/Pixie/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -95,4 +108,4 @@ public function fireEvents($queryBuilder, $event)
}
}
}
}
}
26 changes: 10 additions & 16 deletions src/Pixie/QueryBuilder/Adapters/BaseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], ', ');

Expand Down Expand Up @@ -77,7 +81,7 @@ public function select($statements)
$sqlArray = array(
'SELECT' . (isset($statements['distinct']) ? ' DISTINCT' : ''),
$selects,
'FROM',
(($fromEnabled) ? 'FROM' : ''),
$tables,
$joinString,
$whereCriteria,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions tests/Pixie/NoTableSubQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php namespace Pixie;

use PDO;
use Mockery as m;
use Pixie\QueryBuilder\QueryBuilderHandler;

class NoTableSubQueryTest extends TestCase
{
/**
* @var QueryBuilderHandler
*/
protected $builder;

public function setUp()
{
parent::setUp();

$this->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);

}

}
9 changes: 1 addition & 8 deletions tests/Pixie/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}