Skip to content

Commit ff624c4

Browse files
committedFeb 8, 2016
NOT BETWEEN
1 parent 01de3cf commit ff624c4

14 files changed

+132
-59
lines changed
 

‎README.md

+22-22
Original file line numberDiff line numberDiff line change
@@ -768,28 +768,28 @@ SELECT user_email.* FROM user_email
768768
The following operators are available for filtering using WHERE conditionals:
769769

770770
```php
771-
public function subWhere($operator = 'OR');
772-
public function equals($column, $value);
773-
public function compare($column, $value, $operator);
774-
public function notEquals($column, $value);
775-
public function greaterThan($column, $value);
776-
public function greaterThanOrEqual($column, $value);
777-
public function lessThan($column, $value);
778-
public function lessThanOrEqual($column, $value);
779-
public function like($column, $value);
780-
public function notLike($column, $value);
781-
public function match(array $columns, array $values);
782-
public function matchBoolean(array $columns, array $values);
783-
public function matchWithQueryExpansion(array $columns, array $values);
784-
public function in($column, array $values);
785-
public function notIn($column, array $values);
786-
public function between($column, $a, $b);
787-
public function isNull($column);
788-
public function isNotNull($column);
789-
public function exists(Select $select);
790-
public function notExists(Select $select);
791-
public function addBitClause($column, $value);
792-
public function asLiteral($literal);
771+
public function subWhere($operator = 'OR');
772+
public function equals($column, $value);
773+
public function notEquals($column, $value);
774+
public function greaterThan($column, $value);
775+
public function greaterThanOrEqual($column, $value);
776+
public function lessThan($column, $value);
777+
public function lessThanOrEqual($column, $value);
778+
public function like($column, $value);
779+
public function notLike($column, $value);
780+
public function match(array $columns, array $values);
781+
public function matchBoolean(array $columns, array $values);
782+
public function matchWithQueryExpansion(array $columns, array $values);
783+
public function in($column, array $values);
784+
public function notIn($column, array $values);
785+
public function between($column, $a, $b);
786+
public function notBetween($column, $a, $b);
787+
public function isNull($column);
788+
public function isNotNull($column);
789+
public function exists(Select $select);
790+
public function notExists(Select $select);
791+
public function addBitClause($column, $value);
792+
public function asLiteral($literal);
793793
```
794794

795795
<a name="block4.2"></a>

‎src/Builder/GenericBuilder.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,37 @@ class GenericBuilder implements BuilderInterface
2828
*
2929
* @var \NilPortugues\Sql\QueryBuilder\Builder\Syntax\PlaceholderWriter
3030
*/
31-
private $placeholderWriter;
31+
protected $placeholderWriter;
3232

3333
/**
3434
* The Where writer.
3535
*
3636
* @var \NilPortugues\Sql\QueryBuilder\Builder\Syntax\WhereWriter
3737
*/
38-
private $whereWriter;
38+
protected $whereWriter;
3939

4040
/**
4141
* The SQL formatter.
4242
*
4343
* @var \NilPortugues\Sql\QueryFormatter\Formatter
4444
*/
45-
private $sqlFormatter;
45+
protected $sqlFormatter;
4646

4747
/**
4848
* Class namespace for the query pretty output formatter.
4949
* Required to create the instance only if required.
5050
*
5151
* @var string
5252
*/
53-
private $sqlFormatterClass = 'NilPortugues\Sql\QueryFormatter\Formatter';
53+
protected $sqlFormatterClass = 'NilPortugues\Sql\QueryFormatter\Formatter';
5454

5555
/**
5656
* Array holding the writers for each query part. Methods are called upon request and stored in
5757
* the $queryWriterInstances array.
5858
*
5959
* @var array
6060
*/
61-
private $queryWriterArray = [
61+
protected $queryWriterArray = [
6262
'SELECT' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createSelectWriter',
6363
'INSERT' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createInsertWriter',
6464
'UPDATE' => '\NilPortugues\Sql\QueryBuilder\Builder\Syntax\WriterFactory::createUpdateWriter',
@@ -74,7 +74,7 @@ class GenericBuilder implements BuilderInterface
7474
*
7575
* @var array
7676
*/
77-
private $queryWriterInstances = [
77+
protected $queryWriterInstances = [
7878
'SELECT' => null,
7979
'INSERT' => null,
8080
'UPDATE' => null,
@@ -107,7 +107,7 @@ public function select($table = null, array $columns = null)
107107
/**
108108
* @param \NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery
109109
*
110-
*@return \NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery
110+
* @return \NilPortugues\Sql\QueryBuilder\Manipulation\AbstractBaseQuery
111111
*/
112112
protected function injectBuilder(AbstractBaseQuery $query)
113113
{

‎src/Builder/Syntax/ColumnWriter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class ColumnWriter
2323
/**
2424
* @var \NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder
2525
*/
26-
private $writer;
26+
protected $writer;
2727

2828
/**
2929
* @var PlaceholderWriter
3030
*/
31-
private $placeholderWriter;
31+
protected $placeholderWriter;
3232

3333
/**
3434
* @param GenericBuilder $writer

‎src/Builder/Syntax/DeleteWriter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class DeleteWriter
2121
/**
2222
* @var GenericBuilder
2323
*/
24-
private $writer;
24+
protected $writer;
2525

2626
/**
2727
* @var PlaceholderWriter
2828
*/
29-
private $placeholderWriter;
29+
protected $placeholderWriter;
3030

3131
/**
3232
* @param GenericBuilder $writer

‎src/Builder/Syntax/InsertWriter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class InsertWriter
2222
/**
2323
* @var GenericBuilder
2424
*/
25-
private $writer;
25+
protected $writer;
2626

2727
/**
2828
* @var ColumnWriter
2929
*/
30-
private $columnWriter;
30+
protected $columnWriter;
3131

3232
/**
3333
* @param GenericBuilder $writer

‎src/Builder/Syntax/MinusWriter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MinusWriter
2121
/**
2222
* @var GenericBuilder
2323
*/
24-
private $writer;
24+
protected $writer;
2525

2626
/**
2727
* @param GenericBuilder $writer

‎src/Builder/Syntax/UpdateWriter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function write(Update $update)
5050
*
5151
* @return string
5252
*/
53-
private function writeUpdateValues(Update $update)
53+
protected function writeUpdateValues(Update $update)
5454
{
5555
$assigns = [];
5656
foreach ($update->getValues() as $column => $value) {

‎src/Builder/Syntax/WhereWriter.php

+27
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function writeWhereClauses(Where $where)
5151
$this->writeWhereIns($where, $whereArray);
5252
$this->writeWhereNotIns($where, $whereArray);
5353
$this->writeWhereBetweens($where, $whereArray);
54+
$this->writeWhereNotBetweens($where, $whereArray);
5455
$this->writeWhereComparisons($where, $whereArray);
5556
$this->writeWhereIsNulls($where, $whereArray);
5657
$this->writeWhereIsNotNulls($where, $whereArray);
@@ -183,6 +184,32 @@ function (&$between) {
183184
$whereArray = \array_merge($whereArray, $between);
184185
}
185186

187+
/**
188+
* @param Where $where
189+
* @param array $whereArray
190+
*
191+
* @return array
192+
*/
193+
protected function writeWhereNotBetweens(Where $where, array &$whereArray)
194+
{
195+
$between = $where->getNotBetweens();
196+
\array_walk(
197+
$between,
198+
function (&$between) {
199+
200+
$between = '('
201+
.$this->columnWriter->writeColumn($between['subject'])
202+
.' NOT BETWEEN '
203+
.$this->writer->writePlaceholderValue($between['a'])
204+
.' AND '
205+
.$this->writer->writePlaceholderValue($between['b'])
206+
.')';
207+
}
208+
);
209+
210+
$whereArray = \array_merge($whereArray, $between);
211+
}
212+
186213
/**
187214
* @param Where $where
188215
* @param array $whereArray

‎src/Manipulation/AbstractBaseQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface
3131
/**
3232
* @var \NilPortugues\Sql\QueryBuilder\Builder\BuilderInterface
3333
*/
34-
private $builder;
34+
protected $builder;
3535

3636
/**
3737
* @var string

‎src/Manipulation/Intersect.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Intersect implements QueryInterface, QueryPartInterface
2222
/**
2323
* @var array
2424
*/
25-
private $intersect = [];
25+
protected $intersect = [];
2626

2727
/**
2828
* @return string

‎src/Manipulation/Minus.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class Minus implements QueryInterface, QueryPartInterface
2222
/**
2323
* @var Select
2424
*/
25-
private $first;
25+
protected $first;
2626

2727
/**
2828
* @var Select
2929
*/
30-
private $second;
30+
protected $second;
3131

3232
/**
3333
* @return string

‎src/Syntax/SyntaxFactory.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,10 @@ public static function createColumn(array &$argument, $table = null)
7474
*/
7575
public static function createTable($table)
7676
{
77+
$tableName = $table;
7778
if (\is_array($table)) {
7879
$tableName = \current($table);
7980
$tableAlias = \key($table);
80-
} else {
81-
$tableName = $table;
8281
}
8382

8483
$newTable = new Table($tableName);

‎src/Syntax/Where.php

+44-16
Original file line numberDiff line numberDiff line change
@@ -38,72 +38,77 @@ class Where
3838
/**
3939
* @var array
4040
*/
41-
private $comparisons = [];
41+
protected $comparisons = [];
4242

4343
/**
4444
* @var array
4545
*/
46-
private $betweens = [];
46+
protected $betweens = [];
4747

4848
/**
4949
* @var array
5050
*/
51-
private $isNull = [];
51+
protected $isNull = [];
5252

5353
/**
5454
* @var array
5555
*/
56-
private $isNotNull = [];
56+
protected $isNotNull = [];
5757

5858
/**
5959
* @var array
6060
*/
61-
private $booleans = [];
61+
protected $booleans = [];
6262

6363
/**
6464
* @var array
6565
*/
66-
private $match = [];
66+
protected $match = [];
6767

6868
/**
6969
* @var array
7070
*/
71-
private $ins = [];
71+
protected $ins = [];
7272

7373
/**
7474
* @var array
7575
*/
76-
private $notIns = [];
76+
protected $notIns = [];
7777

7878
/**
7979
* @var array
8080
*/
81-
private $subWheres = [];
81+
protected $subWheres = [];
8282

8383
/**
8484
* @var string
8585
*/
86-
private $conjunction = self::CONJUNCTION_AND;
86+
protected $conjunction = self::CONJUNCTION_AND;
8787

8888
/**
8989
* @var QueryInterface
9090
*/
91-
private $query;
91+
protected $query;
9292

9393
/**
9494
* @var Table
9595
*/
96-
private $table;
96+
protected $table;
9797

9898
/**
9999
* @var array
100100
*/
101-
private $exists = [];
101+
protected $exists = [];
102102

103103
/**
104104
* @var array
105105
*/
106-
private $notExists = [];
106+
protected $notExists = [];
107+
108+
/**
109+
* @var array
110+
*/
111+
protected $notBetweens = [];
107112

108113
/**
109114
* @param QueryInterface $query
@@ -252,7 +257,7 @@ public function equals($column, $value)
252257
*
253258
* @return $this
254259
*/
255-
public function compare($column, $value, $operator)
260+
protected function compare($column, $value, $operator)
256261
{
257262
$column = $this->prepareColumn($column);
258263

@@ -270,7 +275,7 @@ public function compare($column, $value, $operator)
270275
*
271276
* @return Column|Select
272277
*/
273-
private function prepareColumn($column)
278+
protected function prepareColumn($column)
274279
{
275280
//This condition handles the "Select as a a column" special case.
276281
if ($column instanceof Select) {
@@ -463,6 +468,21 @@ public function between($column, $a, $b)
463468
return $this;
464469
}
465470

471+
/**
472+
* @param string $column
473+
* @param int $a
474+
* @param int $b
475+
*
476+
* @return $this
477+
*/
478+
public function notBetween($column, $a, $b)
479+
{
480+
$column = $this->prepareColumn($column);
481+
$this->notBetweens[] = ['subject' => $column, 'a' => $a, 'b' => $b];
482+
483+
return $this;
484+
}
485+
466486
/**
467487
* @param string $column
468488
*
@@ -575,6 +595,14 @@ public function getBetweens()
575595
return $this->betweens;
576596
}
577597

598+
/**
599+
* @return array
600+
*/
601+
public function getNotBetweens()
602+
{
603+
return $this->notBetweens;
604+
}
605+
578606
/**
579607
* @return array
580608
*/

‎tests/Builder/Syntax/WhereWriterTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,25 @@ public function itShouldBeAbleToLetWhereStatementWriteBetweenConditions()
272272
$this->assertEquals($expected, $this->writer->getValues());
273273
}
274274

275+
/**
276+
* @test
277+
*/
278+
public function itShouldBeAbleToLetWhereStatementWriteNotBetweenConditions()
279+
{
280+
$column = 'user_id';
281+
282+
$this->query
283+
->setTable('user')
284+
->where()
285+
->notBetween($column, 1, 2);
286+
287+
$expected = 'SELECT user.* FROM user WHERE (user.user_id NOT BETWEEN :v1 AND :v2)';
288+
$this->assertSame($expected, $this->writer->write($this->query));
289+
290+
$expected = array(':v1' => 1, ':v2' => 2);
291+
$this->assertEquals($expected, $this->writer->getValues());
292+
}
293+
275294
/**
276295
* @test
277296
*/

0 commit comments

Comments
 (0)
Please sign in to comment.