-
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.
Manually merged #125 (expression support in query builders)
- Loading branch information
1 parent
002553f
commit 3917c8f
Showing
12 changed files
with
191 additions
and
140 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/** | ||
* Opulence | ||
* | ||
* @link https://www.opulencephp.com | ||
* @copyright Copyright (C) 2020 David Young | ||
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Opulence\QueryBuilders; | ||
|
||
/** | ||
* Expression is designed to be used for setting values in INSERT and UPDATE statements | ||
* It is not intended to be used in WHERE clauses or as columns in SELECT queries | ||
*/ | ||
class Expression | ||
{ | ||
/** @var string The expression to use */ | ||
protected string $expression; | ||
/** @var array[] */ | ||
protected array $values = []; | ||
|
||
/** | ||
* Expression constructor. | ||
* | ||
* @param string $expression | ||
* @param mixed ...$values | ||
* | ||
* @throws InvalidQueryException | ||
*/ | ||
public function __construct(string $expression, ...$values) | ||
{ | ||
$this->expression = $expression; | ||
|
||
foreach ($values as $value) { | ||
if (is_scalar($value)) { | ||
$value = [$value, \PDO::PARAM_STR]; | ||
} | ||
|
||
if (!is_array($value) || count($value) !== 2) { | ||
throw new InvalidQueryException('Incorrect number of items in expression value array'); | ||
} | ||
|
||
if (!array_key_exists(0, $value) || !array_key_exists(1, $value)) { | ||
throw new InvalidQueryException('Incorrect keys in expression value array'); | ||
} | ||
|
||
if (!is_scalar($value[0]) || !is_numeric($value[1]) || $value[1] < 0) { | ||
throw new InvalidQueryException('Incorrect expression values'); | ||
} | ||
|
||
$this->values[] = $value; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the parameters in the expression | ||
* | ||
* @return array The list of parameters | ||
*/ | ||
public function getParameters(): array | ||
{ | ||
return $this->values; | ||
} | ||
|
||
/** | ||
* Gets the SQL that makes up the expression | ||
* | ||
* @return string The SQL of the expression | ||
*/ | ||
public function getSql(): string | ||
{ | ||
return $this->expression; | ||
} | ||
} |
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
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
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
Oops, something went wrong.