Skip to content

Commit

Permalink
[update] - updated conditionals, added group and custom operations
Browse files Browse the repository at this point in the history
  • Loading branch information
iloElias committed Oct 5, 2024
1 parent d46a4b8 commit 1e4d98f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ilias/maestro",
"type": "library",
"version": "1.2.4",
"version": "1.2.5",
"description": "A PHP object-oriented postgres database manager",
"keywords": [
"postgres",
Expand Down
63 changes: 41 additions & 22 deletions src/Abstract/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ abstract class Query
protected string $query = '';
public const AND = 'AND';
public const OR = 'OR';
public const EQUALS = '=';
public const NOT_EQUAL = '!=';
public const GREATER_THAN = '>';
public const LESS_THAN = '<';
public const GREATER_THAN_OR_EQUAL = '>=';
public const LESS_THAN_OR_EQUAL = '<=';
public const LIKE = '*~';
public const NOT_LIKE = '!~';
public const NOT_LIKE_OR_EQUAL = '<>';

public function __construct(
protected string $behavior = Maestro::SQL_STRICT,
Expand All @@ -32,35 +41,45 @@ public function __construct(
* @param array $conditions An associative array of conditions for the WHERE clause.
* @return $this Returns the current instance for method chaining.
*/
public function where(array $conditions, string $operation = Select::AND, bool $group = false): static
public function where(string|array $conditions, string $operation = Select::AND, string $compaction = Select::EQUALS, bool $group = false): static
{
$where = [];
foreach ($conditions as $column => $value) {
$columnWhere = Utils::sanitizeForPostgres($column);
$paramName = str_replace('.', '_', ":where_{$columnWhere}");
$this->storeParameter($paramName, $value);
$where[] = "{$column} = {$paramName}";
if (is_array($conditions)) {
$where = [];
foreach ($conditions as $column => $value) {
$columnWhere = Utils::sanitizeForPostgres($column);
$paramName = str_replace('.', '_', ":where_{$columnWhere}");
$this->storeParameter($paramName, $value);
$where[] = "{$column} {$compaction} {$paramName}";
}
$clauses = implode(" {$operation} ", $where);
$this->where[] = ($group ? "({$clauses})" : $clauses);
}
if (is_string($conditions)) {
$this->where[] = $conditions;
}
$clauses = implode(" {$operation} ", $where);
$this->where[] = ($group ? "({$clauses})" : $clauses);
return $this;
}

public function in(array $conditions, string $operation = Select::AND, bool $group = false): static
public function in(string|array $conditions, string $operation = Select::AND, bool $group = false): static
{
$where = [];
foreach ($conditions as $column => $value) {
$inParams = array_map(function ($v, $k) use ($column) {
$columnIn = Utils::sanitizeForPostgres($column);
$paramName = ":in_{$columnIn}_{$k}";
$this->storeParameter($paramName, $v);
return $paramName;
}, $value, array_keys($value));
$inList = implode(",", $inParams);
$where[] = "{$column} IN({$inList})";
if (is_array($conditions)) {
$where = [];
foreach ($conditions as $column => $value) {
$inParams = array_map(function ($v, $k) use ($column) {
$columnIn = Utils::sanitizeForPostgres($column);
$paramName = ":in_{$columnIn}_{$k}";
$this->storeParameter($paramName, $v);
return $paramName;
}, $value, array_keys($value));
$inList = implode(",", $inParams);
$where[] = "{$column} IN({$inList})";
}
$clauses = implode(" {$operation} ", $where);
$this->where[] = ($group ? "({$clauses})" : $clauses);
}
if (is_string($conditions)) {
$this->where[] = $conditions;
}
$clauses = implode(" {$operation} ", $where);
$this->where[] = ($group ? "({$clauses})" : $clauses);
return $this;
}

Expand Down

0 comments on commit 1e4d98f

Please sign in to comment.