Skip to content

Instantly share code, notes, and snippets.

@index0h
Created April 3, 2024 04:02
Show Gist options
  • Save index0h/d1b0de0a0288e1c8aeb0a30f7e5fa365 to your computer and use it in GitHub Desktop.
Save index0h/d1b0de0a0288e1c8aeb0a30f7e5fa365 to your computer and use it in GitHub Desktop.
QueryBuiler.php
<?php
declare(strict_types=1);
namespace index0h\Experiments\Service\TinyDb;
class QueryBuilder
{
private array $select = [];
private ?string $fromTableName = null;
private ?string $fromAlias = null;
private array $joins = [];
private array $where = [];
private array $group = [];
public function select(string ...$fields): self
{
$this->select = $fields;
return $this;
}
public function from(string $tableName, string $alias): self
{
$this->fromTableName = $tableName;
$this->fromAlias = $alias;
return $this;
}
public function innerJoin(string $tableName, string $alias, string $on): self
{
$this->joins[] = [
'type' => 'inner',
'tableName' => $tableName,
'alias' => $alias,
'on' => $on,
];
return $this;
}
public function leftJoin(string $tableName, string $alias, string $on): self
{
$this->joins[] = [
'type' => 'left',
'tableName' => $tableName,
'alias' => $alias,
'on' => $on,
];
return $this;
}
public function rightJoin(string $tableName, string $alias, string $on): self
{
$this->joins[] = [
'type' => 'right',
'tableName' => $tableName,
'alias' => $alias,
'on' => $on,
];
return $this;
}
public function where(string $where): self
{
$this->where = ['where' => $where];
return $this;
}
public function andWhere(string $where): self
{
$this->where[] = ['where' => $where, 'join' => 'and'];
return $this;
}
public function orWhere(string $where): self
{
$this->where[] = ['where' => $where, 'join' => 'or'];
return $this;
}
public function groupBy(string ...$fields): self
{
$this->group = $fields;
return $this;
}
public function having(string $having): self
{
return $this;
}
public function andHaving(string $having): self
{
return $this;
}
public function orHaving(string $having): self
{
return $this;
}
public function orderBy(string $field, string $order = 'ASC'): self
{
return $this;
}
public function limit(int $limit): self
{
return $this;
}
public function offset(int $offset): self
{
return $this;
}
public function getSql(): string
{
// TODO
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment