Skip to content

Commit

Permalink
Added orderBy to genericSql
Browse files Browse the repository at this point in the history
  • Loading branch information
killua-eu committed Aug 8, 2024
1 parent 8c0f632 commit 8e3fab5
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ abstract class GenericSql
updated_at = CURRENT_TIMESTAMP
";

public string $orderBy;


public function __construct(PDO $pdo, string $table)
{
$this->pdo = $pdo;
Expand Down Expand Up @@ -213,7 +216,9 @@ public function createBatch(array $docs, $upsert = false): void
*/
public function get(string $uuid): bool | array
{
$this->stmt = $this->pdo->prepare("SELECT {$this->selectModifier} {$this->dataColumn} FROM {$this->schema}.{$this->table} WHERE {$this->uuidColumn} = :uuid");
$query = "SELECT {$this->selectModifier} {$this->dataColumn} FROM {$this->schema}.{$this->table} WHERE {$this->uuidColumn} = :uuid {$this->orderBy}";
$query .= !empty($this->orderBy) ? " ORDER BY {$this->orderBy}" : '';
$this->stmt = $this->pdo->prepare($query);
$this->stmt->bindParam(':uuid', $uuid);
$this->stmt->execute();
$res = $this->stmt->fetchColumn();
Expand Down Expand Up @@ -278,6 +283,7 @@ public function getAll(): array
foreach ($this->wheres as $c) { $conds[] = "{$c['column']} {$c['condition']} :" . md5($c['column']); }
$query .= " WHERE " . implode(" {$c['logicalOperator']} ", $conds);
}
$query .= !empty($this->orderBy) ? " ORDER BY {$this->orderBy}" : '';
$this->stmt = $this->pdo->prepare($query);
if (!empty($this->wheres)) {
foreach ($this->wheres as $c) { $this->stmt->bindValue(":" . md5($c['column']), $c['value']); }
Expand Down

0 comments on commit 8e3fab5

Please sign in to comment.