Skip to content

Commit

Permalink
Added DatabaseTable::loadRowCount.
Browse files Browse the repository at this point in the history
Enabled order by multiple columns at once.
Disabled escaping of strings, because prepared statements already do that.
  • Loading branch information
GregaMohorko committed Jun 28, 2020
1 parent 75862ac commit f86d0a5
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 72 deletions.
Binary file modified Documentation/Diagrams/Database Entities Diagram.dia
Binary file not shown.
Binary file modified Documentation/Diagrams/Database Entities Diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright 2020 Gregor Mohorko

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ PHP version >= 5.5

## Author and License

Grega Mohorko ([www.mohorko.info](https://www.mohorko.info))
Gregor Mohorko ([www.mohorko.info](https://www.mohorko.info))

Copyright (c) 2019 Grega Mohorko
Copyright (c) 2020 Gregor Mohorko

[Apache License 2.0](./LICENSE)
2 changes: 1 addition & 1 deletion nbproject/licenseheader.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ${licenseFirst}
</#if>
${licensePrefix}${nameAndExt}
${licensePrefix}
${licensePrefix}Copyright ${date?date?string("yyyy")} Grega Mohorko
${licensePrefix}Copyright ${date?date?string("yyyy")} Gregor Mohorko
${licensePrefix}
${licensePrefix}Licensed under the Apache License, Version 2.0 (the "License");
${licensePrefix}you may not use this file except in compliance with the License.
Expand Down
7 changes: 4 additions & 3 deletions src/BlueDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* BlueDB.php
*
* Copyright 2019 Grega Mohorko
* Copyright 2020 Gregor Mohorko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,10 +19,10 @@
*
* Bootstrap file for BlueDB library.
*
* Version 1.2.9.1
* Version 1.3.0.0
*
* @project BlueDB
* @author Grega Mohorko <grega@mohorko.info>
* @author Gregor Mohorko <grega@mohorko.info>
* @copyright Mar 14, 2017 Grega Mohorko
*/

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

// include all files
require_once BLUEDB_DIR.'DataAccess/Criteria/OrderByMultipleFieldOperator.php';
require_once BLUEDB_DIR.'DataAccess/Criteria/Expression.php';
require_once BLUEDB_DIR.'DataAccess/Criteria/Criteria.php';
require_once BLUEDB_DIR.'DataAccess/JoinType.php';
Expand Down
88 changes: 87 additions & 1 deletion src/DataAccess/Criteria/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Criteria
*/
public $PreparedParameters;
/**
* @var array A list of [field, bool] tuples, where the second item determines whether the order should be ascending.
* @var array A list of [field, bool] tuples, where the second item determines whether the order should be ascending OR [OrderByMultipleFieldOperator, fields[], bool].
*/
public $OrderingFields;
/**
Expand Down Expand Up @@ -95,6 +95,23 @@ public function orderBy($field)
return $this;
}

/**
* Creates a ORDER BY (field1 OPERATOR field2 OPERATOR field3 ...).
*
* @param OrderByMultipleFieldOperator $multipleFieldOperator The operator that will be put between all the fields.
* @param string[] $fields The fields.
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
*/
public function orderByMultipleFields($multipleFieldOperator, $fields)
{
if($this->OrderingFields!==null){
throw new Exception('The orderBy has already been called. Call thenBy instead.');
}
$this->OrderingFields=[];
$this->addOrderingMultipleFields($multipleFieldOperator, $fields, true);
return $this;
}

/**
* @param string $field The field on which to order descendingly.
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
Expand All @@ -109,6 +126,23 @@ public function orderByDescending($field)
return $this;
}

/**
* Creates a ORDER BY (field1 OPERATOR field2 OPERATOR field3 ...) DESC.
*
* @param OrderByMultipleFieldOperator $multipleFieldOperator The operator that will be put between all the fields.
* @param string[] $fields The fields.
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
*/
public function orderByMultipleFieldsDescending($multipleFieldOperator, $fields)
{
if($this->OrderingFields!==null){
throw new Exception('The orderBy has already been called. Call thenByDescending instead.');
}
$this->OrderingFields=[];
$this->addOrderingMultipleFields($multipleFieldOperator, $fields, false);
return $this;
}

/**
* @param string $field The field on which to order ascendingly.
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
Expand All @@ -122,6 +156,22 @@ public function thenBy($field)
return $this;
}

/**
* Creates a (field1 OPERATOR field2 OPERATOR field3 ...).
*
* @param OrderByMultipleFieldOperator $multipleFieldOperator The operator that will be put between all the fields.
* @param string[] $fields The fields.
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
*/
public function thenByMultipleFields($multipleFieldOperator, $fields)
{
if($this->OrderingFields===null){
throw new Exception('The orderBy has not yet been called. Call orderBy instead.');
}
$this->addOrderingMultipleFields($multipleFieldOperator, $fields, true);
return $this;
}

/**
* @param string $field The field on which to order descendingly.
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
Expand All @@ -135,6 +185,22 @@ public function thenByDescending($field)
return $this;
}

/**
* Creates a (field1 OPERATOR field2 OPERATOR field3 ...) DESC.
*
* @param OrderByMultipleFieldOperator $multipleFieldOperator The operator that will be put between all the fields.
* @param string[] $fields The fields.
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
*/
public function thenByMultipleFieldsDescending($multipleFieldOperator, $fields)
{
if($this->OrderingFields===null){
throw new Exception('The orderBy has not yet been called. Call orderByDescending instead.');
}
$this->addOrderingMultipleFields($multipleFieldOperator, $fields, false);
return $this;
}

/**
* @param string $field
* @param bool $ascending
Expand All @@ -147,6 +213,26 @@ private function addOrdering($field,$ascending)
$this->OrderingFields[]=[$field,$ascending];
}

/**
* @param OrderByMultipleFieldOperator $multipleFieldOperator
* @param string[] $fields
* @param bool $ascending
*/
private function addOrderingMultipleFields($multipleFieldOperator, $fields, $ascending)
{
switch($multipleFieldOperator){
case OrderByMultipleFieldOperator::ANDD:
case OrderByMultipleFieldOperator::ORR:
break;
default:
throw new Exception('Unsupported multiple field operator: "'.$multipleFieldOperator.'".');
}
if($fields === null || empty($fields)){
throw new Exception('The count of fields for order by multiple fields must not be empty or null.');
}
$this->OrderingFields[]=[$multipleFieldOperator, $fields, $ascending];
}

/**
* @param int $count Specifies the maximum number of rows to be returned.
* @param int $offset Specifies the offset of the first row to be returned.
Expand Down
31 changes: 31 additions & 0 deletions src/DataAccess/Criteria/OrderByMultipleFieldOperator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* OrderByMultipleFieldOperator.php
*
* Copyright 2020 Gregor Mohorko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @project BlueDB
* @author Gregor Mohorko <grega@mohorko.info>
* @copyright Jun 17, 2020 Gregor Mohorko
*/

namespace BlueDB\DataAccess\Criteria;

abstract class OrderByMultipleFieldOperator
{
const ANDD='AND';
const ORR='OR';
}
4 changes: 4 additions & 0 deletions src/DataAccess/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private function __construct()
/**
* Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection.
*
* DO NOT use this when using prepared statements.
*
* This function calls real_escape_string of mysqli.
*
* @param type $string The string to be escaped. Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.
Expand Down Expand Up @@ -167,6 +169,7 @@ public static function select($selectQuery,$resultMode = MYSQLI_STORE_RESULT,$re
{
$instance=self::instance();

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

/** @var mysqli_result $result */
$result=$instance->Source->query($selectQuery, $resultMode);
if(!$result){
throw new Exception("Error while executing select query '".$selectQuery."': [".$instance->Source->errno."] ".$instance->Source->error,$instance->Source->errno);
Expand Down
37 changes: 36 additions & 1 deletion src/Entity/DatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@

abstract class DatabaseTable implements IDatabaseTable
{
/**
* Returns the number of rows in this database table.
*
* @return int The number of rows in this database table.
*/
public static function loadRowCount()
{
$query = 'select count(*) from '.self::getTableName().';';
$result = MySQL::selectSingle($query);
var_dump($result);
die();
}

/**
* Checks if values are set or if they have to be set to default values as specified in the configuration file.
*
Expand Down Expand Up @@ -192,7 +205,29 @@ protected static function prepareSelectQuery($fieldEntityClass,$classToLoad,$joi
if($i>0){
$query.=', ';
}
$query.=$orderingField[0].' '.($orderingField[1]?'ASC':'DESC');
switch(count($orderingField)){
case 2:
$fieldToOrderBy = constant("$classToLoad::$orderingField[0]Column");
$ascending=$orderingField[1];
break;
case 3:
$orderByOperator=$orderingField[0];
$fieldsToOrderBy=$orderingField[1];
$ascending=$orderingField[2];
$fieldToOrderBy='('.constant("$classToLoad::$fieldsToOrderBy[0]Column");
$countJ=count($fieldsToOrderBy);
for($j=1;$j<$countJ;++$j){
$fieldToOrderBy.=" $orderByOperator ".constant("$classToLoad::$fieldsToOrderBy[$j]Column");
}
$fieldToOrderBy.=')';
break;
default:
throw new Exception('Invalid ordering field in criteria.');
}
$query.=$fieldToOrderBy;
if(!$ascending){
$query.=' DESC';
}
}
}
// limit
Expand Down
5 changes: 5 additions & 0 deletions src/Entity/IDatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ interface IDatabaseTable
* @return string
*/
static function getTableName();

/**
* @return int The number of rows in this database table.
*/
static function loadRowCount();
}
Loading

0 comments on commit f86d0a5

Please sign in to comment.