5
use Upside\Db\Schema\AlterTable;
6
use Upside\Db\Schema\CreateTable;
10
protected Connection $connection;
13
protected ?array $table_list = null;
15
/** Currently used database name. */
16
protected ?string $current_database = null;
19
protected array $columns = [];
21
public function __construct(Connection $connection)
23
$this->connection = $connection;
27
* Get the name of the currently used database
31
public function get_current_database(): string
33
if ($this->current_database === null) {
34
$compiler = $this->connection->schema_compiler();
35
$result = $compiler->current_database($this->connection->get_dsn());
37
if (isset($result['result'])) {
38
$this->current_database = $result['result'];
40
$this->current_database = $this->connection->column($result['sql'], $result['params']);
44
return $this->current_database;
48
* Check if the specified table exists
50
* @param string $table Table name
51
* @param boolean $clear (optional) Refresh table list
55
public function has_table(string $table, bool $clear = false): bool
57
$list = $this->get_tables($clear);
59
return isset($list[\strtolower($table)]);
63
* Get a list with all tables that belong to the currently used database
65
* @param boolean $clear (optional) Refresh table list
70
public function get_tables(bool $clear = false): array
73
$this->table_list = null;
76
if ($this->table_list === null) {
77
$compiler = $this->connection->schema_compiler();
79
$database = $this->get_current_database();
81
$sql = $compiler->get_tables($database);
83
$results = $this->connection
84
->query($sql['sql'], $sql['params'])
88
$this->table_list = [];
90
foreach ($results as $result) {
91
$this->table_list[\strtolower($result[0])] = $result[0];
95
return $this->table_list;
99
* Get a list with all columns that belong to the specified table
101
* @param string $table
102
* @param boolean $clear (optional) Refresh column list
103
* @param boolean $names (optional) Return only the column names
105
* @return false|string[]
108
public function get_columns(string $table, bool $clear = false, bool $names = true): array|false
111
unset($this->columns[$table]);
114
if (!$this->has_table($table, $clear)) {
118
if (!isset($this->columns[$table])) {
119
$compiler = $this->connection->schema_compiler();
121
$database = $this->get_current_database();
123
$sql = $compiler->get_columns($database, $table);
125
$results = $this->connection
126
->query($sql['sql'], $sql['params'])
132
foreach ($results as $ord => &$col) {
133
$columns[$col['name']] = [
134
'name' => $col['name'],
135
'type' => $col['type'],
139
$this->columns[$table] = $columns;
142
return $names ? \array_keys($this->columns[$table]) : $this->columns[$table];
146
* Creates a new table
148
* @param string $table Table name
149
* @param callable $callback A callback that will define table's fields and indexes
152
public function create(string $table, callable $callback)
154
$compiler = $this->connection->schema_compiler();
156
$schema = new CreateTable($table);
160
foreach ($compiler->create($schema) as $result) {
161
$this->connection->command($result['sql'], $result['params']);
165
$this->table_list = null;
169
* Alters a table's definition
171
* @param string $table Table name
172
* @param callable $callback A callback that will add or remove fields or indexes
175
public function alter(string $table, callable $callback)
177
$compiler = $this->connection->schema_compiler();
179
$schema = new AlterTable($table);
183
unset($this->columns[\strtolower($table)]);
185
foreach ($compiler->alter($schema) as $result) {
186
$this->connection->command($result['sql'], $result['params']);
191
* Change a table's name
193
* @param string $table The table
194
* @param string $name The new name of the table
197
public function rename_table(string $table, string $name)
199
$result = $this->connection->schema_compiler()->rename_table($table, $name);
200
$this->connection->command($result['sql'], $result['params']);
201
$this->table_list = null;
202
unset($this->columns[\strtolower($table)]);
208
public function drop(string $table)
210
$compiler = $this->connection->schema_compiler();
212
$result = $compiler->drop($table);
214
$this->connection->command($result['sql'], $result['params']);
217
$this->table_list = null;
218
unset($this->columns[strtolower($table)]);
222
* Deletes all records from a table
224
public function truncate(string $table)
226
$compiler = $this->connection->schema_compiler();
228
$result = $compiler->truncate($table);
230
$this->connection->command($result['sql'], $result['params']);