3
namespace Upside\Db\SQL;
5
use Upside\Db\Connection;
7
class Query extends BaseStatement
9
protected Connection $connection;
11
protected array $tables;
13
public function __construct(Connection $connection, array|string $tables, ?SQLStatement $statement = null)
15
parent::__construct($statement);
17
$this->tables = \is_array($tables) ? $tables : [$tables];
18
$this->connection = $connection;
21
protected function build_select(): Select
23
return new Select($this->connection, $this->tables, $this->sql);
26
protected function build_delete(): Delete
28
return new Delete($this->connection, $this->tables, $this->sql);
31
public function distinct(bool $value = true): SelectStatement
33
return $this->build_select()->distinct($value);
36
public function group_by(Expression|array|\Closure|string $columns): Select
38
return $this->build_select()->group_by($columns);
41
public function having(Expression|string|\Closure $column, ?\Closure $value = null): Select
43
return $this->build_select()->having($column, $value);
46
public function and_having(Expression|string|\Closure $column, ?\Closure $value = null): Select
48
return $this->build_select()->and_having($column, $value);
51
public function or_having(Expression|string|\Closure $column, ?\Closure $value = null): Select
53
return $this->build_select()->or_having($column, $value);
56
public function order_by(Expression|array|\Closure|string $columns, string $order = 'ASC', ?string $nulls = null): SelectStatement
58
return $this->build_select()->order_by($columns, $order, $nulls);
61
public function limit(int $value): SelectStatement
63
return $this->build_select()->limit($value);
66
public function offset(int $value): SelectStatement
68
return $this->build_select()->offset($value);
71
public function into(string $table, ?string $database = null): SelectStatement
73
return $this->build_select()->into($table, $database);
77
* @param array $columns (optional)
79
* @return \Upside\Db\ResultSet
81
public function select($columns = [])
83
return $this->build_select()->select($columns);
86
public function column(Expression|string|\Closure $name): mixed
88
return $this->build_select()->column($name);
94
public function count(Expression|\Closure|string $column = '*', bool $distinct = false)
96
return $this->build_select()->count($column, $distinct);
102
public function avg(Expression|string|\Closure $column, bool $distinct = false)
104
return $this->build_select()->avg($column, $distinct);
110
public function sum(Expression|string|\Closure $column, bool $distinct = false)
112
return $this->build_select()->sum($column, $distinct);
118
public function min(Expression|string|\Closure $column, bool $distinct = false)
120
return $this->build_select()->min($column, $distinct);
126
public function max(Expression|string|\Closure $column, bool $distinct = false)
128
return $this->build_select()->max($column, $distinct);
132
* @param string[] $tables (optional)
136
public function delete(array $tables = [])
138
return $this->build_delete()->delete($tables);