Skip to content

Commit 7fbf8db

Browse files
committed
Added support for ORDER and LIMIT statements.
1 parent 61eec9a commit 7fbf8db

3 files changed

Lines changed: 113 additions & 4 deletions

File tree

src/BlueDB.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* BlueDB.php
55
*
6-
* Copyright 2018 Grega Mohorko
6+
* Copyright 2019 Grega Mohorko
77
*
88
* Licensed under the Apache License, Version 2.0 (the "License");
99
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919
*
2020
* Bootstrap file for BlueDB library.
2121
*
22-
* Version 1.2.6.0
22+
* Version 1.2.7.0
2323
*
2424
* @project BlueDB
2525
* @author Grega Mohorko <grega@mohorko.info>

src/DataAccess/Criteria/Criteria.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ class Criteria
5050
* @var array Parameters for the prepared statement binding.
5151
*/
5252
public $PreparedParameters;
53+
/**
54+
* @var array A list of [field, bool] tuples, where the second item determines whether the order should be ascending.
55+
*/
56+
public $OrderingFields;
57+
/**
58+
* @var array The [int, int] array representing the LIMIT clause, where the first item is the offset and the second is the count.
59+
*/
60+
public $Limit;
5361

5462
/**
5563
* @param string $baseEntityClass
@@ -73,6 +81,84 @@ public function add($expression)
7381
$this->expressions[]=$expression;
7482
}
7583

84+
/**
85+
* @param string $field The field on which to order ascendingly.
86+
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
87+
*/
88+
public function orderBy($field)
89+
{
90+
if($this->OrderingFields!==null){
91+
throw new Exception('The orderBy has already been called. Call thenBy instead.');
92+
}
93+
$this->OrderingFields=[];
94+
$this->addOrdering($field, true);
95+
return $this;
96+
}
97+
98+
/**
99+
* @param string $field The field on which to order descendingly.
100+
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
101+
*/
102+
public function orderByDescending($field)
103+
{
104+
if($this->OrderingFields!==null){
105+
throw new Exception('The orderBy has already been called. Call thenByDescending instead.');
106+
}
107+
$this->OrderingFields=[];
108+
$this->addOrdering($field, false);
109+
return $this;
110+
}
111+
112+
/**
113+
* @param string $field The field on which to order ascendingly.
114+
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
115+
*/
116+
public function thenBy($field)
117+
{
118+
if($this->OrderingFields===null){
119+
throw new Exception('The orderBy has not yet been called. Call orderBy instead.');
120+
}
121+
$this->addOrdering($field, true);
122+
return $this;
123+
}
124+
125+
/**
126+
* @param string $field The field on which to order descendingly.
127+
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
128+
*/
129+
public function thenByDescending($field)
130+
{
131+
if($this->OrderingFields===null){
132+
throw new Exception('The orderBy has not yet been called. Call orderByDescending instead.');
133+
}
134+
$this->addOrdering($field, false);
135+
return $this;
136+
}
137+
138+
/**
139+
* @param string $field
140+
* @param bool $ascending
141+
*/
142+
private function addOrdering($field,$ascending)
143+
{
144+
if($field===null){
145+
throw new Exception('Field for order by must not be null.');
146+
}
147+
$this->OrderingFields[]=[$field,$ascending];
148+
}
149+
150+
/**
151+
* @param int $count Specifies the maximum number of rows to be returned.
152+
* @param int $offset Specifies the offset of the first row to be returned.
153+
*/
154+
public function limit($count, $offset=0)
155+
{
156+
if($this->Limit!==null){
157+
throw new Exception('Limit was already set.');
158+
}
159+
$this->Limit=[$offset,$count];
160+
}
161+
76162
public function prepare()
77163
{
78164
// Joins

src/Entity/DatabaseTable.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,34 @@ protected static function prepareSelectQuery($fieldEntityClass,$classToLoad,$joi
177177
$criteria->prepare();
178178
// joins
179179
if(!empty($criteria->PreparedQueryJoins)){
180-
$query.=" ".$criteria->PreparedQueryJoins;
180+
$query.=' '.$criteria->PreparedQueryJoins;
181181
}
182182
// conditions
183183
if(!empty($criteria->PreparedQueryRestrictions)){
184-
$query.=" WHERE ".$criteria->PreparedQueryRestrictions;
184+
$query.=' WHERE '.$criteria->PreparedQueryRestrictions;
185+
}
186+
// ordering
187+
if($criteria->OrderingFields!==null){
188+
$query.=' ORDER BY ';
189+
$count=count($criteria->OrderingFields);
190+
for($i=0;$i<$count;++$i){
191+
$orderingField=$criteria->OrderingFields[$i];
192+
if($i>0){
193+
$query.=', ';
194+
}
195+
$query.=$orderingField[0].' '.($orderingField[1]?'ASC':'DESC');
196+
}
197+
}
198+
// limit
199+
if($criteria->Limit!==null){
200+
$query.=' LIMIT ';
201+
$offset=$criteria->Limit[0];
202+
$count=$criteria->Limit[1];
203+
if($offset===0){
204+
$query.=$count;
205+
}else{
206+
$query.=$offset.', '.$count;
207+
}
185208
}
186209
}
187210

0 commit comments

Comments
 (0)