Skip to content

Commit f86d0a5

Browse files
committed
Added DatabaseTable::loadRowCount.
Enabled order by multiple columns at once. Disabled escaping of strings, because prepared statements already do that.
1 parent 75862ac commit f86d0a5

13 files changed

Lines changed: 228 additions & 72 deletions
15 Bytes
Binary file not shown.
1.9 KB
Loading

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright {yyyy} {name of copyright owner}
189+
Copyright 2020 Gregor Mohorko
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ PHP version >= 5.5
6868

6969
## Author and License
7070

71-
Grega Mohorko ([www.mohorko.info](https://www.mohorko.info))
71+
Gregor Mohorko ([www.mohorko.info](https://www.mohorko.info))
7272

73-
Copyright (c) 2019 Grega Mohorko
73+
Copyright (c) 2020 Gregor Mohorko
7474

7575
[Apache License 2.0](./LICENSE)

nbproject/licenseheader.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ${licenseFirst}
33
</#if>
44
${licensePrefix}${nameAndExt}
55
${licensePrefix}
6-
${licensePrefix}Copyright ${date?date?string("yyyy")} Grega Mohorko
6+
${licensePrefix}Copyright ${date?date?string("yyyy")} Gregor Mohorko
77
${licensePrefix}
88
${licensePrefix}Licensed under the Apache License, Version 2.0 (the "License");
99
${licensePrefix}you may not use this file except in compliance with the License.

src/BlueDB.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* BlueDB.php
55
*
6-
* Copyright 2019 Grega Mohorko
6+
* Copyright 2020 Gregor 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,10 +19,10 @@
1919
*
2020
* Bootstrap file for BlueDB library.
2121
*
22-
* Version 1.2.9.1
22+
* Version 1.3.0.0
2323
*
2424
* @project BlueDB
25-
* @author Grega Mohorko <grega@mohorko.info>
25+
* @author Gregor Mohorko <grega@mohorko.info>
2626
* @copyright Mar 14, 2017 Grega Mohorko
2727
*/
2828

@@ -39,6 +39,7 @@
3939
\BlueDB\Configuration\BlueDBProperties::init($config);
4040

4141
// include all files
42+
require_once BLUEDB_DIR.'DataAccess/Criteria/OrderByMultipleFieldOperator.php';
4243
require_once BLUEDB_DIR.'DataAccess/Criteria/Expression.php';
4344
require_once BLUEDB_DIR.'DataAccess/Criteria/Criteria.php';
4445
require_once BLUEDB_DIR.'DataAccess/JoinType.php';

src/DataAccess/Criteria/Criteria.php

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Criteria
5151
*/
5252
public $PreparedParameters;
5353
/**
54-
* @var array A list of [field, bool] tuples, where the second item determines whether the order should be ascending.
54+
* @var array A list of [field, bool] tuples, where the second item determines whether the order should be ascending OR [OrderByMultipleFieldOperator, fields[], bool].
5555
*/
5656
public $OrderingFields;
5757
/**
@@ -95,6 +95,23 @@ public function orderBy($field)
9595
return $this;
9696
}
9797

98+
/**
99+
* Creates a ORDER BY (field1 OPERATOR field2 OPERATOR field3 ...).
100+
*
101+
* @param OrderByMultipleFieldOperator $multipleFieldOperator The operator that will be put between all the fields.
102+
* @param string[] $fields The fields.
103+
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
104+
*/
105+
public function orderByMultipleFields($multipleFieldOperator, $fields)
106+
{
107+
if($this->OrderingFields!==null){
108+
throw new Exception('The orderBy has already been called. Call thenBy instead.');
109+
}
110+
$this->OrderingFields=[];
111+
$this->addOrderingMultipleFields($multipleFieldOperator, $fields, true);
112+
return $this;
113+
}
114+
98115
/**
99116
* @param string $field The field on which to order descendingly.
100117
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
@@ -109,6 +126,23 @@ public function orderByDescending($field)
109126
return $this;
110127
}
111128

129+
/**
130+
* Creates a ORDER BY (field1 OPERATOR field2 OPERATOR field3 ...) DESC.
131+
*
132+
* @param OrderByMultipleFieldOperator $multipleFieldOperator The operator that will be put between all the fields.
133+
* @param string[] $fields The fields.
134+
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
135+
*/
136+
public function orderByMultipleFieldsDescending($multipleFieldOperator, $fields)
137+
{
138+
if($this->OrderingFields!==null){
139+
throw new Exception('The orderBy has already been called. Call thenByDescending instead.');
140+
}
141+
$this->OrderingFields=[];
142+
$this->addOrderingMultipleFields($multipleFieldOperator, $fields, false);
143+
return $this;
144+
}
145+
112146
/**
113147
* @param string $field The field on which to order ascendingly.
114148
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
@@ -122,6 +156,22 @@ public function thenBy($field)
122156
return $this;
123157
}
124158

159+
/**
160+
* Creates a (field1 OPERATOR field2 OPERATOR field3 ...).
161+
*
162+
* @param OrderByMultipleFieldOperator $multipleFieldOperator The operator that will be put between all the fields.
163+
* @param string[] $fields The fields.
164+
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
165+
*/
166+
public function thenByMultipleFields($multipleFieldOperator, $fields)
167+
{
168+
if($this->OrderingFields===null){
169+
throw new Exception('The orderBy has not yet been called. Call orderBy instead.');
170+
}
171+
$this->addOrderingMultipleFields($multipleFieldOperator, $fields, true);
172+
return $this;
173+
}
174+
125175
/**
126176
* @param string $field The field on which to order descendingly.
127177
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
@@ -135,6 +185,22 @@ public function thenByDescending($field)
135185
return $this;
136186
}
137187

188+
/**
189+
* Creates a (field1 OPERATOR field2 OPERATOR field3 ...) DESC.
190+
*
191+
* @param OrderByMultipleFieldOperator $multipleFieldOperator The operator that will be put between all the fields.
192+
* @param string[] $fields The fields.
193+
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
194+
*/
195+
public function thenByMultipleFieldsDescending($multipleFieldOperator, $fields)
196+
{
197+
if($this->OrderingFields===null){
198+
throw new Exception('The orderBy has not yet been called. Call orderByDescending instead.');
199+
}
200+
$this->addOrderingMultipleFields($multipleFieldOperator, $fields, false);
201+
return $this;
202+
}
203+
138204
/**
139205
* @param string $field
140206
* @param bool $ascending
@@ -147,6 +213,26 @@ private function addOrdering($field,$ascending)
147213
$this->OrderingFields[]=[$field,$ascending];
148214
}
149215

216+
/**
217+
* @param OrderByMultipleFieldOperator $multipleFieldOperator
218+
* @param string[] $fields
219+
* @param bool $ascending
220+
*/
221+
private function addOrderingMultipleFields($multipleFieldOperator, $fields, $ascending)
222+
{
223+
switch($multipleFieldOperator){
224+
case OrderByMultipleFieldOperator::ANDD:
225+
case OrderByMultipleFieldOperator::ORR:
226+
break;
227+
default:
228+
throw new Exception('Unsupported multiple field operator: "'.$multipleFieldOperator.'".');
229+
}
230+
if($fields === null || empty($fields)){
231+
throw new Exception('The count of fields for order by multiple fields must not be empty or null.');
232+
}
233+
$this->OrderingFields[]=[$multipleFieldOperator, $fields, $ascending];
234+
}
235+
150236
/**
151237
* @param int $count Specifies the maximum number of rows to be returned.
152238
* @param int $offset Specifies the offset of the first row to be returned.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* OrderByMultipleFieldOperator.php
5+
*
6+
* Copyright 2020 Gregor Mohorko
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* @project BlueDB
21+
* @author Gregor Mohorko <grega@mohorko.info>
22+
* @copyright Jun 17, 2020 Gregor Mohorko
23+
*/
24+
25+
namespace BlueDB\DataAccess\Criteria;
26+
27+
abstract class OrderByMultipleFieldOperator
28+
{
29+
const ANDD='AND';
30+
const ORR='OR';
31+
}

src/DataAccess/MySQL.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ private function __construct()
9292
/**
9393
* Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection.
9494
*
95+
* DO NOT use this when using prepared statements.
96+
*
9597
* This function calls real_escape_string of mysqli.
9698
*
9799
* @param type $string The string to be escaped. Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.
@@ -167,6 +169,7 @@ public static function select($selectQuery,$resultMode = MYSQLI_STORE_RESULT,$re
167169
{
168170
$instance=self::instance();
169171

172+
/** @var mysqli_result $result */
170173
$result=$instance->Source->query($selectQuery,$resultMode);
171174
if(!$result){
172175
throw new Exception("Error while executing select query '".$selectQuery."': [".$instance->Source->errno."] ".$instance->Source->error,$instance->Source->errno);
@@ -189,6 +192,7 @@ public static function selectSingle($selectQuery,$resultMode = MYSQLI_STORE_RESU
189192
{
190193
$instance=self::instance();
191194

195+
/** @var mysqli_result $result */
192196
$result=$instance->Source->query($selectQuery, $resultMode);
193197
if(!$result){
194198
throw new Exception("Error while executing select query '".$selectQuery."': [".$instance->Source->errno."] ".$instance->Source->error,$instance->Source->errno);

src/Entity/DatabaseTable.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@
3333

3434
abstract class DatabaseTable implements IDatabaseTable
3535
{
36+
/**
37+
* Returns the number of rows in this database table.
38+
*
39+
* @return int The number of rows in this database table.
40+
*/
41+
public static function loadRowCount()
42+
{
43+
$query = 'select count(*) from '.self::getTableName().';';
44+
$result = MySQL::selectSingle($query);
45+
var_dump($result);
46+
die();
47+
}
48+
3649
/**
3750
* Checks if values are set or if they have to be set to default values as specified in the configuration file.
3851
*
@@ -192,7 +205,29 @@ protected static function prepareSelectQuery($fieldEntityClass,$classToLoad,$joi
192205
if($i>0){
193206
$query.=', ';
194207
}
195-
$query.=$orderingField[0].' '.($orderingField[1]?'ASC':'DESC');
208+
switch(count($orderingField)){
209+
case 2:
210+
$fieldToOrderBy = constant("$classToLoad::$orderingField[0]Column");
211+
$ascending=$orderingField[1];
212+
break;
213+
case 3:
214+
$orderByOperator=$orderingField[0];
215+
$fieldsToOrderBy=$orderingField[1];
216+
$ascending=$orderingField[2];
217+
$fieldToOrderBy='('.constant("$classToLoad::$fieldsToOrderBy[0]Column");
218+
$countJ=count($fieldsToOrderBy);
219+
for($j=1;$j<$countJ;++$j){
220+
$fieldToOrderBy.=" $orderByOperator ".constant("$classToLoad::$fieldsToOrderBy[$j]Column");
221+
}
222+
$fieldToOrderBy.=')';
223+
break;
224+
default:
225+
throw new Exception('Invalid ordering field in criteria.');
226+
}
227+
$query.=$fieldToOrderBy;
228+
if(!$ascending){
229+
$query.=' DESC';
230+
}
196231
}
197232
}
198233
// limit

0 commit comments

Comments
 (0)