-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added conditions, which is part of the way towards issue #9
- Loading branch information
1 parent
47c7f87
commit 744b578
Showing
14 changed files
with
546 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Opulence/QueryBuilders/Conditions/BetweenCondition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2016 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
namespace Opulence\QueryBuilders\Conditions; | ||
|
||
use PDO; | ||
|
||
/** | ||
* Defines the BETWEEN condition | ||
*/ | ||
class BetweenCondition extends Condition | ||
{ | ||
/** @var mixed The min value */ | ||
protected $min = ""; | ||
/** @var mixed The min value */ | ||
protected $max = ""; | ||
/** @var int The data type of the min/max */ | ||
protected $dataType = PDO::PARAM_STR; | ||
|
||
/** | ||
* @inheritdoc | ||
* @param mixed $min The min value | ||
* @param mixed $max The max value | ||
* @param int $dataType The PDO data type for the min and max | ||
*/ | ||
public function __construct(string $column, $min, $max, $dataType = PDO::PARAM_STR) | ||
{ | ||
parent::__construct($column); | ||
|
||
$this->min = $min; | ||
$this->max = $max; | ||
$this->dataType = $dataType; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getParameters() : array | ||
{ | ||
return [[$this->min, $this->dataType], [$this->max, $this->dataType]]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getSql() : string | ||
{ | ||
return "{$this->column} BETWEEN ? AND ?"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2016 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
namespace Opulence\QueryBuilders\Conditions; | ||
|
||
/** | ||
* Defines the base condition | ||
*/ | ||
abstract class Condition implements ICondition | ||
{ | ||
/** @var string The column */ | ||
protected $column = ""; | ||
|
||
/** | ||
* @param string $column The column | ||
*/ | ||
public function __construct(string $column) | ||
{ | ||
$this->column = $column; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Opulence/QueryBuilders/Conditions/ConditionFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2016 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
namespace Opulence\QueryBuilders\Conditions; | ||
|
||
use InvalidArgumentException; | ||
use PDO; | ||
|
||
/** | ||
* Defines the condition factory | ||
*/ | ||
class ConditionFactory | ||
{ | ||
/** | ||
* Creates a new BETWEEN condition | ||
* | ||
* @param string $column The name of the column | ||
* @param mixed $min The min value | ||
* @param mixed $max The max value | ||
* @param int $dataType The PDO data type for the min and max | ||
* @return BetweenCondition The condition | ||
*/ | ||
public function between(string $column, $min, $max, $dataType = PDO::PARAM_STR) : BetweenCondition | ||
{ | ||
return new BetweenCondition($column, $min, $max, $dataType); | ||
} | ||
|
||
/** | ||
* Creates a new IN condition | ||
* | ||
* @param string $column The name of the column | ||
* @param array|string $parametersOrExpression Either the parameters or the sub-expression | ||
* @return InCondition The condition | ||
* @throws InvalidArgumentException Thrown if the parameters are not in the correct format | ||
*/ | ||
public function in(string $column, $parametersOrExpression) : InCondition | ||
{ | ||
return new InCondition($column, $parametersOrExpression); | ||
} | ||
|
||
/** | ||
* Creates a new NOT BETWEEN condition | ||
* | ||
* @param string $column The name of the column | ||
* @param mixed $min The min value | ||
* @param mixed $max The max value | ||
* @param int $dataType The PDO data type for the min and max | ||
* @return NotBetweenCondition The condition | ||
*/ | ||
public function notBetween(string $column, $min, $max, $dataType = PDO::PARAM_STR) : NotBetweenCondition | ||
{ | ||
return new NotBetweenCondition($column, $min, $max, $dataType); | ||
} | ||
|
||
/** | ||
* Creates a new NOT IN condition | ||
* | ||
* @param string $column The name of the column | ||
* @param array|string $parametersOrExpression Either the parameters or the sub-expression | ||
* @return NotInCondition The condition | ||
* @throws InvalidArgumentException Thrown if the parameters are not in the correct format | ||
*/ | ||
public function notIn(string $column, $parametersOrExpression) : NotInCondition | ||
{ | ||
return new NotInCondition($column, $parametersOrExpression); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2016 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
namespace Opulence\QueryBuilders\Conditions; | ||
|
||
/** | ||
* The condition | ||
*/ | ||
interface ICondition | ||
{ | ||
/** | ||
* Gets the parameters bound by the condition | ||
* | ||
* @return array The list of parameters | ||
*/ | ||
public function getParameters() : array; | ||
|
||
/** | ||
* Gets the SQL generated by the condition | ||
* | ||
* @return string The SQL | ||
*/ | ||
public function getSql() : string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2016 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
namespace Opulence\QueryBuilders\Conditions; | ||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Defines the IN condition | ||
*/ | ||
class InCondition extends Condition | ||
{ | ||
/** @var string The sub-expression */ | ||
protected $expression = ""; | ||
/** @var array The list of parameters bound to the query */ | ||
protected $parameters = []; | ||
/** @var bool True if we're using parameters, otherwise we're using a sub-expression */ | ||
protected $usingParameters = true; | ||
|
||
/** | ||
* @inheritdoc | ||
* @param array|string $parametersOrExpression Either the parameters or the sub-expression | ||
* @throws InvalidArgumentException Thrown if the parameters are not in the correct format | ||
*/ | ||
public function __construct(string $column, $parametersOrExpression) | ||
{ | ||
parent::__construct($column); | ||
|
||
if (is_string($parametersOrExpression)) { | ||
$this->usingParameters = false; | ||
$this->expression = $parametersOrExpression; | ||
} else if (is_array($parametersOrExpression)) { | ||
$this->usingParameters = true; | ||
$this->parameters = $parametersOrExpression; | ||
} else { | ||
throw new InvalidArgumentException("Must pass either parameters or sub-expression to IN condition"); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getParameters() : array | ||
{ | ||
return $this->parameters; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getSql() : string | ||
{ | ||
$sql = "{$this->column} IN ("; | ||
|
||
if ($this->usingParameters) { | ||
$sql .= implode(",", array_fill(0, count($this->parameters), "?")); | ||
} else { | ||
$sql .= $this->expression; | ||
} | ||
|
||
$sql .= ")"; | ||
|
||
return $sql; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Opulence/QueryBuilders/Conditions/NotBetweenCondition.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2016 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
namespace Opulence\QueryBuilders\Conditions; | ||
|
||
/** | ||
* Defines the NOT BETWEEN condition | ||
*/ | ||
class NotBetweenCondition extends BetweenCondition | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getSql() : string | ||
{ | ||
return "{$this->column} NOT BETWEEN ? AND ?"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2016 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
namespace Opulence\QueryBuilders\Conditions; | ||
|
||
/** | ||
* Defines the NOT IN condition | ||
*/ | ||
class NotInCondition extends InCondition | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getSql() : string | ||
{ | ||
$sql = "{$this->column} NOT IN ("; | ||
|
||
if ($this->usingParameters) { | ||
$sql .= implode(",", array_fill(0, count($this->parameters), "?")); | ||
} else { | ||
$sql .= $this->expression; | ||
} | ||
|
||
$sql .= ")"; | ||
|
||
return $sql; | ||
} | ||
} |
Oops, something went wrong.