Upgrade to 3.7.1

This commit is contained in:
Bastian Allgeier
2022-07-12 13:33:21 +02:00
parent 7931eb5e47
commit 1ad1eaf387
377 changed files with 63981 additions and 63824 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -16,121 +16,121 @@ use Kirby\Toolkit\Config;
*/
class Db
{
/**
* Query shortcuts
*
* @var array
*/
public static $queries = [];
/**
* Query shortcuts
*
* @var array
*/
public static $queries = [];
/**
* The singleton Database object
*
* @var \Kirby\Database\Database
*/
public static $connection = null;
/**
* The singleton Database object
*
* @var \Kirby\Database\Database
*/
public static $connection = null;
/**
* (Re)connect the database
*
* @param array|null $params Pass `[]` to use the default params from the config,
* don't pass any argument to get the current connection
* @return \Kirby\Database\Database
*/
public static function connect(?array $params = null)
{
if ($params === null && static::$connection !== null) {
return static::$connection;
}
/**
* (Re)connect the database
*
* @param array|null $params Pass `[]` to use the default params from the config,
* don't pass any argument to get the current connection
* @return \Kirby\Database\Database
*/
public static function connect(?array $params = null)
{
if ($params === null && static::$connection !== null) {
return static::$connection;
}
// try to connect with the default
// connection settings if no params are set
$params ??= [
'type' => Config::get('db.type', 'mysql'),
'host' => Config::get('db.host', 'localhost'),
'user' => Config::get('db.user', 'root'),
'password' => Config::get('db.password', ''),
'database' => Config::get('db.database', ''),
'prefix' => Config::get('db.prefix', ''),
'port' => Config::get('db.port', '')
];
// try to connect with the default
// connection settings if no params are set
$params ??= [
'type' => Config::get('db.type', 'mysql'),
'host' => Config::get('db.host', 'localhost'),
'user' => Config::get('db.user', 'root'),
'password' => Config::get('db.password', ''),
'database' => Config::get('db.database', ''),
'prefix' => Config::get('db.prefix', ''),
'port' => Config::get('db.port', '')
];
return static::$connection = new Database($params);
}
return static::$connection = new Database($params);
}
/**
* Returns the current database connection
*
* @return \Kirby\Database\Database|null
*/
public static function connection()
{
return static::$connection;
}
/**
* Returns the current database connection
*
* @return \Kirby\Database\Database|null
*/
public static function connection()
{
return static::$connection;
}
/**
* Sets the current table which should be queried. Returns a
* Query object, which can be used to build a full query for
* that table.
*
* @param string $table
* @return \Kirby\Database\Query
*/
public static function table(string $table)
{
$db = static::connect();
return $db->table($table);
}
/**
* Sets the current table which should be queried. Returns a
* Query object, which can be used to build a full query for
* that table.
*
* @param string $table
* @return \Kirby\Database\Query
*/
public static function table(string $table)
{
$db = static::connect();
return $db->table($table);
}
/**
* Executes a raw SQL query which expects a set of results
*
* @param string $query
* @param array $bindings
* @param array $params
* @return mixed
*/
public static function query(string $query, array $bindings = [], array $params = [])
{
$db = static::connect();
return $db->query($query, $bindings, $params);
}
/**
* Executes a raw SQL query which expects a set of results
*
* @param string $query
* @param array $bindings
* @param array $params
* @return mixed
*/
public static function query(string $query, array $bindings = [], array $params = [])
{
$db = static::connect();
return $db->query($query, $bindings, $params);
}
/**
* Executes a raw SQL query which expects no set of results (i.e. update, insert, delete)
*
* @param string $query
* @param array $bindings
* @return bool
*/
public static function execute(string $query, array $bindings = []): bool
{
$db = static::connect();
return $db->execute($query, $bindings);
}
/**
* Executes a raw SQL query which expects no set of results (i.e. update, insert, delete)
*
* @param string $query
* @param array $bindings
* @return bool
*/
public static function execute(string $query, array $bindings = []): bool
{
$db = static::connect();
return $db->execute($query, $bindings);
}
/**
* Magic calls for other static Db methods are
* redirected to either a predefined query or
* the respective method of the Database object
*
* @param string $method
* @param mixed $arguments
* @return mixed
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function __callStatic(string $method, $arguments)
{
if (isset(static::$queries[$method])) {
return (static::$queries[$method])(...$arguments);
}
/**
* Magic calls for other static Db methods are
* redirected to either a predefined query or
* the respective method of the Database object
*
* @param string $method
* @param mixed $arguments
* @return mixed
* @throws \Kirby\Exception\InvalidArgumentException
*/
public static function __callStatic(string $method, $arguments)
{
if (isset(static::$queries[$method])) {
return (static::$queries[$method])(...$arguments);
}
if (static::$connection !== null && method_exists(static::$connection, $method) === true) {
return call_user_func_array([static::$connection, $method], $arguments);
}
if (static::$connection !== null && method_exists(static::$connection, $method) === true) {
return call_user_func_array([static::$connection, $method], $arguments);
}
throw new InvalidArgumentException('Invalid static Db method: ' . $method);
}
throw new InvalidArgumentException('Invalid static Db method: ' . $method);
}
}
// @codeCoverageIgnoreStart
@@ -147,7 +147,7 @@ class Db
* @return mixed
*/
Db::$queries['select'] = function (string $table, $columns = '*', $where = null, string $order = null, int $offset = 0, int $limit = null) {
return Db::table($table)->select($columns)->where($where)->order($order)->offset($offset)->limit($limit)->all();
return Db::table($table)->select($columns)->where($where)->order($order)->offset($offset)->limit($limit)->all();
};
/**
@@ -162,7 +162,7 @@ Db::$queries['select'] = function (string $table, $columns = '*', $where = null,
* @return mixed
*/
Db::$queries['first'] = Db::$queries['row'] = Db::$queries['one'] = function (string $table, $columns = '*', $where = null, string $order = null) {
return Db::table($table)->select($columns)->where($where)->order($order)->first();
return Db::table($table)->select($columns)->where($where)->order($order)->first();
};
/**
@@ -177,7 +177,7 @@ Db::$queries['first'] = Db::$queries['row'] = Db::$queries['one'] = function (st
* @return mixed
*/
Db::$queries['column'] = function (string $table, string $column, $where = null, string $order = null, int $offset = 0, int $limit = null) {
return Db::table($table)->where($where)->order($order)->offset($offset)->limit($limit)->column($column);
return Db::table($table)->where($where)->order($order)->offset($offset)->limit($limit)->column($column);
};
/**
@@ -188,7 +188,7 @@ Db::$queries['column'] = function (string $table, string $column, $where = null,
* @return mixed Returns the last inserted id on success or false
*/
Db::$queries['insert'] = function (string $table, array $values) {
return Db::table($table)->insert($values);
return Db::table($table)->insert($values);
};
/**
@@ -200,7 +200,7 @@ Db::$queries['insert'] = function (string $table, array $values) {
* @return bool
*/
Db::$queries['update'] = function (string $table, array $values, $where = null): bool {
return Db::table($table)->where($where)->update($values);
return Db::table($table)->where($where)->update($values);
};
/**
@@ -211,7 +211,7 @@ Db::$queries['update'] = function (string $table, array $values, $where = null):
* @return bool
*/
Db::$queries['delete'] = function (string $table, $where = null): bool {
return Db::table($table)->where($where)->delete();
return Db::table($table)->where($where)->delete();
};
/**
@@ -222,7 +222,7 @@ Db::$queries['delete'] = function (string $table, $where = null): bool {
* @return int
*/
Db::$queries['count'] = function (string $table, $where = null): int {
return Db::table($table)->where($where)->count();
return Db::table($table)->where($where)->count();
};
/**
@@ -234,7 +234,7 @@ Db::$queries['count'] = function (string $table, $where = null): int {
* @return float
*/
Db::$queries['min'] = function (string $table, string $column, $where = null): float {
return Db::table($table)->where($where)->min($column);
return Db::table($table)->where($where)->min($column);
};
/**
@@ -246,7 +246,7 @@ Db::$queries['min'] = function (string $table, string $column, $where = null): f
* @return float
*/
Db::$queries['max'] = function (string $table, string $column, $where = null): float {
return Db::table($table)->where($where)->max($column);
return Db::table($table)->where($where)->max($column);
};
/**
@@ -258,7 +258,7 @@ Db::$queries['max'] = function (string $table, string $column, $where = null): f
* @return float
*/
Db::$queries['avg'] = function (string $table, string $column, $where = null): float {
return Db::table($table)->where($where)->avg($column);
return Db::table($table)->where($where)->avg($column);
};
/**
@@ -270,7 +270,7 @@ Db::$queries['avg'] = function (string $table, string $column, $where = null): f
* @return float
*/
Db::$queries['sum'] = function (string $table, string $column, $where = null): float {
return Db::table($table)->where($where)->sum($column);
return Db::table($table)->where($where)->sum($column);
};
// @codeCoverageIgnoreEnd

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -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()
]
];
}
}

View File

@@ -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' => []
];
}
}