Skip to content

Commit

Permalink
Added support for ORDER and LIMIT statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Feb 19, 2019
1 parent 61eec9a commit 7fbf8db
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/BlueDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* BlueDB.php
*
* Copyright 2018 Grega Mohorko
* Copyright 2019 Grega 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,7 +19,7 @@
*
* Bootstrap file for BlueDB library.
*
* Version 1.2.6.0
* Version 1.2.7.0
*
* @project BlueDB
* @author Grega Mohorko <grega@mohorko.info>
Expand Down
86 changes: 86 additions & 0 deletions src/DataAccess/Criteria/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class Criteria
* @var array Parameters for the prepared statement binding.
*/
public $PreparedParameters;
/**
* @var array A list of [field, bool] tuples, where the second item determines whether the order should be ascending.
*/
public $OrderingFields;
/**
* @var array The [int, int] array representing the LIMIT clause, where the first item is the offset and the second is the count.
*/
public $Limit;

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

/**
* @param string $field The field on which to order ascendingly.
* @return Criteria The same criteria, so that you can chain orderBy and thenBy clauses.
*/
public function orderBy($field)
{
if($this->OrderingFields!==null){
throw new Exception('The orderBy has already been called. Call thenBy instead.');
}
$this->OrderingFields=[];
$this->addOrdering($field, 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.
*/
public function orderByDescending($field)
{
if($this->OrderingFields!==null){
throw new Exception('The orderBy has already been called. Call thenByDescending instead.');
}
$this->OrderingFields=[];
$this->addOrdering($field, 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.
*/
public function thenBy($field)
{
if($this->OrderingFields===null){
throw new Exception('The orderBy has not yet been called. Call orderBy instead.');
}
$this->addOrdering($field, 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.
*/
public function thenByDescending($field)
{
if($this->OrderingFields===null){
throw new Exception('The orderBy has not yet been called. Call orderByDescending instead.');
}
$this->addOrdering($field, false);
return $this;
}

/**
* @param string $field
* @param bool $ascending
*/
private function addOrdering($field,$ascending)
{
if($field===null){
throw new Exception('Field for order by must not be null.');
}
$this->OrderingFields[]=[$field,$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.
*/
public function limit($count, $offset=0)
{
if($this->Limit!==null){
throw new Exception('Limit was already set.');
}
$this->Limit=[$offset,$count];
}

public function prepare()
{
// Joins
Expand Down
27 changes: 25 additions & 2 deletions src/Entity/DatabaseTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,34 @@ protected static function prepareSelectQuery($fieldEntityClass,$classToLoad,$joi
$criteria->prepare();
// joins
if(!empty($criteria->PreparedQueryJoins)){
$query.=" ".$criteria->PreparedQueryJoins;
$query.=' '.$criteria->PreparedQueryJoins;
}
// conditions
if(!empty($criteria->PreparedQueryRestrictions)){
$query.=" WHERE ".$criteria->PreparedQueryRestrictions;
$query.=' WHERE '.$criteria->PreparedQueryRestrictions;
}
// ordering
if($criteria->OrderingFields!==null){
$query.=' ORDER BY ';
$count=count($criteria->OrderingFields);
for($i=0;$i<$count;++$i){
$orderingField=$criteria->OrderingFields[$i];
if($i>0){
$query.=', ';
}
$query.=$orderingField[0].' '.($orderingField[1]?'ASC':'DESC');
}
}
// limit
if($criteria->Limit!==null){
$query.=' LIMIT ';
$offset=$criteria->Limit[0];
$count=$criteria->Limit[1];
if($offset===0){
$query.=$count;
}else{
$query.=$offset.', '.$count;
}
}
}

Expand Down

0 comments on commit 7fbf8db

Please sign in to comment.