Upgrade to 3.7.1
This commit is contained in:
@@ -15,45 +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');
|
||||
/**
|
||||
* 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;
|
||||
$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,
|
||||
]
|
||||
];
|
||||
}
|
||||
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');
|
||||
/**
|
||||
* 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()
|
||||
]
|
||||
];
|
||||
}
|
||||
return [
|
||||
'query' => 'SELECT TABLE_NAME AS name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ' . $binding,
|
||||
'bindings' => [
|
||||
$binding => $this->database->name()
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,130 +15,131 @@ use Kirby\Database\Sql;
|
||||
*/
|
||||
class Sqlite 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
|
||||
{
|
||||
return [
|
||||
'query' => 'PRAGMA table_info(' . $this->tableName($table) . ')',
|
||||
'bindings' => [],
|
||||
];
|
||||
}
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
return [
|
||||
'query' => 'PRAGMA table_info(' . $this->tableName($table) . ')',
|
||||
'bindings' => [],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstracted column types to simplify table
|
||||
* creation for multiple database drivers
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function columnTypes(): array
|
||||
{
|
||||
return [
|
||||
'id' => '{{ name }} INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE',
|
||||
'varchar' => '{{ name }} TEXT {{ null }} {{ default }} {{ unique }}',
|
||||
'text' => '{{ name }} TEXT {{ null }} {{ default }} {{ unique }}',
|
||||
'int' => '{{ name }} INTEGER {{ null }} {{ default }} {{ unique }}',
|
||||
'timestamp' => '{{ name }} INTEGER {{ null }} {{ default }} {{ unique }}'
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Abstracted column types to simplify table
|
||||
* creation for multiple database drivers
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function columnTypes(): array
|
||||
{
|
||||
return [
|
||||
'id' => '{{ name }} INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE',
|
||||
'varchar' => '{{ name }} TEXT {{ null }} {{ default }} {{ unique }}',
|
||||
'text' => '{{ name }} TEXT {{ null }} {{ default }} {{ unique }}',
|
||||
'int' => '{{ name }} INTEGER {{ null }} {{ default }} {{ unique }}',
|
||||
'timestamp' => '{{ name }} INTEGER {{ null }} {{ default }} {{ unique }}',
|
||||
'bool' => '{{ name }} INTEGER {{ null }} {{ default }} {{ unique }}'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines an identifier (table and column)
|
||||
*
|
||||
* @param $table string
|
||||
* @param $column string
|
||||
* @param $values bool 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 === true) {
|
||||
return $this->quoteIdentifier($column);
|
||||
}
|
||||
/**
|
||||
* Combines an identifier (table and column)
|
||||
*
|
||||
* @param $table string
|
||||
* @param $column string
|
||||
* @param $values bool 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 === true) {
|
||||
return $this->quoteIdentifier($column);
|
||||
}
|
||||
|
||||
return $this->quoteIdentifier($table) . '.' . $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);
|
||||
/**
|
||||
* 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(
|
||||
fn ($name) => $this->quoteIdentifier($name),
|
||||
$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(
|
||||
fn ($name) => $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 . ')';
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
$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']
|
||||
];
|
||||
}
|
||||
return [
|
||||
'query' => $query,
|
||||
'bindings' => $inner['bindings']
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Quotes an identifier (table *or* column)
|
||||
*
|
||||
* @param $identifier string
|
||||
* @return string
|
||||
*/
|
||||
public function quoteIdentifier(string $identifier): string
|
||||
{
|
||||
// * is special
|
||||
if ($identifier === '*') {
|
||||
return $identifier;
|
||||
}
|
||||
/**
|
||||
* Quotes an identifier (table *or* column)
|
||||
*
|
||||
* @param $identifier string
|
||||
* @return string
|
||||
*/
|
||||
public function quoteIdentifier(string $identifier): string
|
||||
{
|
||||
// * is special
|
||||
if ($identifier === '*') {
|
||||
return $identifier;
|
||||
}
|
||||
|
||||
// escape quotes inside the identifier name
|
||||
$identifier = str_replace('"', '""', $identifier);
|
||||
// escape quotes inside the identifier name
|
||||
$identifier = str_replace('"', '""', $identifier);
|
||||
|
||||
// wrap in quotes
|
||||
return '"' . $identifier . '"';
|
||||
}
|
||||
// wrap in quotes
|
||||
return '"' . $identifier . '"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a query to list the tables of the current database;
|
||||
* the query needs to return rows with a column `name`
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function tables(): array
|
||||
{
|
||||
return [
|
||||
'query' => 'SELECT name FROM sqlite_master WHERE type = "table" OR type = "view"',
|
||||
'bindings' => []
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Returns a query to list the tables of the current database;
|
||||
* the query needs to return rows with a column `name`
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function tables(): array
|
||||
{
|
||||
return [
|
||||
'query' => 'SELECT name FROM sqlite_master WHERE type = "table" OR type = "view"',
|
||||
'bindings' => []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user