3.4.0
This commit is contained in:
@@ -5,7 +5,7 @@ namespace Kirby\Database\Sql;
|
||||
use Kirby\Database\Sql;
|
||||
|
||||
/**
|
||||
* Mysql query builder
|
||||
* MySQL query builder
|
||||
*
|
||||
* @package Kirby Database
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
@@ -15,4 +15,45 @@ use Kirby\Database\Sql;
|
||||
*/
|
||||
class Mysql extends Sql
|
||||
{
|
||||
/**
|
||||
* Returns a query to list the columns of a specified table;
|
||||
* the query needs to return rows with a column `name`
|
||||
*
|
||||
* @param string $table Table name
|
||||
* @return array
|
||||
*/
|
||||
public function columns(string $table): array
|
||||
{
|
||||
$databaseBinding = $this->bindingName('database');
|
||||
$tableBinding = $this->bindingName('table');
|
||||
|
||||
$query = 'SELECT COLUMN_NAME AS name FROM INFORMATION_SCHEMA.COLUMNS ';
|
||||
$query .= 'WHERE TABLE_SCHEMA = ' . $databaseBinding . ' AND TABLE_NAME = ' . $tableBinding;
|
||||
|
||||
return [
|
||||
'query' => $query,
|
||||
'bindings' => [
|
||||
$databaseBinding => $this->database->name(),
|
||||
$tableBinding => $table,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a query to list the tables of the current database;
|
||||
* the query needs to return rows with a column `name`
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function tables(): array
|
||||
{
|
||||
$binding = $this->bindingName('database');
|
||||
|
||||
return [
|
||||
'query' => 'SELECT TABLE_NAME AS name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' . $binding,
|
||||
'bindings' => [
|
||||
$binding => $this->database->name()
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Kirby\Database\Sql;
|
||||
use Kirby\Database\Sql;
|
||||
|
||||
/**
|
||||
* Sqlite query builder
|
||||
* SQLite query builder
|
||||
*
|
||||
* @package Kirby Database
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>
|
||||
@@ -16,11 +16,11 @@ use Kirby\Database\Sql;
|
||||
class Sqlite extends Sql
|
||||
{
|
||||
/**
|
||||
* Returns a list of columns for a specified table
|
||||
* SQLite version
|
||||
* Returns a query to list the columns of a specified table;
|
||||
* the query needs to return rows with a column `name`
|
||||
*
|
||||
* @param string $table The table name
|
||||
* @return string
|
||||
* @param string $table Table name
|
||||
* @return array
|
||||
*/
|
||||
public function columns(string $table): array
|
||||
{
|
||||
@@ -30,30 +30,10 @@ class Sqlite extends Sql
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional key definition for the column.
|
||||
*
|
||||
* @param array $column
|
||||
* @return array
|
||||
*/
|
||||
public function columnKey(array $column): array
|
||||
{
|
||||
if (isset($column['key']) === false || $column['key'] === 'INDEX') {
|
||||
return [
|
||||
'query' => null,
|
||||
'bindings' => []
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'query' => $column['key'],
|
||||
'bindings' => []
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstracted column types to simplify table
|
||||
* creation for multiple database drivers
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -61,33 +41,72 @@ class Sqlite extends Sql
|
||||
{
|
||||
return [
|
||||
'id' => '{{ name }} INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE',
|
||||
'varchar' => '{{ name }} TEXT {{ null }} {{ key }} {{ default }}',
|
||||
'text' => '{{ name }} TEXT {{ null }} {{ key }} {{ default }}',
|
||||
'int' => '{{ name }} INTEGER {{ null }} {{ key }} {{ default }}',
|
||||
'timestamp' => '{{ name }} INTEGER {{ null }} {{ key }} {{ default }}'
|
||||
'varchar' => '{{ name }} TEXT {{ null }} {{ default }} {{ unique }}',
|
||||
'text' => '{{ name }} TEXT {{ null }} {{ default }} {{ unique }}',
|
||||
'int' => '{{ name }} INTEGER {{ null }} {{ default }} {{ unique }}',
|
||||
'timestamp' => '{{ name }} INTEGER {{ null }} {{ default }} {{ unique }}'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines an identifier (table and column)
|
||||
* SQLite version
|
||||
*
|
||||
* @param $table string
|
||||
* @param $column string
|
||||
* @param $values boolean Whether the identifier is going to be used for a values clause
|
||||
* Only relevant for SQLite
|
||||
* @param $values boolean Whether the identifier is going to be used for a VALUES clause;
|
||||
* only relevant for SQLite
|
||||
* @return string
|
||||
*/
|
||||
public function combineIdentifier(string $table, string $column, bool $values = false): string
|
||||
{
|
||||
// SQLite doesn't support qualified column names for VALUES clauses
|
||||
if ($values) {
|
||||
if ($values === true) {
|
||||
return $this->quoteIdentifier($column);
|
||||
}
|
||||
|
||||
return $this->quoteIdentifier($table) . '.' . $this->quoteIdentifier($column);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a CREATE TABLE query
|
||||
*
|
||||
* @param string $table Table name
|
||||
* @param array $columns Array of column definition arrays, see `Kirby\Database\Sql::createColumn()`
|
||||
* @return array Array with a `query` string and a `bindings` array
|
||||
*/
|
||||
public function createTable(string $table, array $columns = []): array
|
||||
{
|
||||
$inner = $this->createTableInner($columns);
|
||||
|
||||
// add keys
|
||||
$keys = [];
|
||||
foreach ($inner['keys'] as $key => $columns) {
|
||||
// quote each column name and make a list string out of the column names
|
||||
$columns = implode(', ', array_map(function ($name) {
|
||||
return $this->quoteIdentifier($name);
|
||||
}, $columns));
|
||||
|
||||
if ($key === 'primary') {
|
||||
$inner['query'] .= ',' . PHP_EOL . 'PRIMARY KEY (' . $columns . ')';
|
||||
} else {
|
||||
// SQLite only supports index creation using a separate CREATE INDEX query
|
||||
$unique = isset($inner['unique'][$key]) === true ? 'UNIQUE ' : '';
|
||||
$keys[] = 'CREATE ' . $unique . 'INDEX ' . $this->quoteIdentifier($table . '_index_' . $key) .
|
||||
' ON ' . $this->quoteIdentifier($table) . ' (' . $columns . ')';
|
||||
}
|
||||
}
|
||||
|
||||
$query = 'CREATE TABLE ' . $this->quoteIdentifier($table) . ' (' . PHP_EOL . $inner['query'] . PHP_EOL . ')';
|
||||
if (empty($keys) === false) {
|
||||
$query .= ';' . PHP_EOL . implode(';' . PHP_EOL, $keys);
|
||||
}
|
||||
|
||||
return [
|
||||
'query' => $query,
|
||||
'bindings' => $inner['bindings']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Quotes an identifier (table *or* column)
|
||||
*
|
||||
@@ -101,7 +120,7 @@ class Sqlite extends Sql
|
||||
return $identifier;
|
||||
}
|
||||
|
||||
// replace every quote with two quotes
|
||||
// escape quotes inside the identifier name
|
||||
$identifier = str_replace('"', '""', $identifier);
|
||||
|
||||
// wrap in quotes
|
||||
@@ -109,10 +128,9 @@ class Sqlite extends Sql
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of tables of the database
|
||||
* SQLite version
|
||||
* Returns a query to list the tables of the current database;
|
||||
* the query needs to return rows with a column `name`
|
||||
*
|
||||
* @param string $database The database name
|
||||
* @return string
|
||||
*/
|
||||
public function tables(): array
|
||||
|
||||
Reference in New Issue
Block a user