Skip to content
Merged
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
16 changes: 16 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
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();
}

}