Upgrade to 3.8.3
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
namespace Kirby\Database;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
@@ -22,131 +24,90 @@ class Database
|
||||
{
|
||||
/**
|
||||
* The number of affected rows for the last query
|
||||
*
|
||||
* @var int|null
|
||||
*/
|
||||
protected $affected;
|
||||
protected int|null $affected = null;
|
||||
|
||||
/**
|
||||
* Whitelist for column names
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $columnWhitelist = [];
|
||||
protected array $columnWhitelist = [];
|
||||
|
||||
/**
|
||||
* The established connection
|
||||
*
|
||||
* @var \PDO|null
|
||||
*/
|
||||
protected $connection;
|
||||
protected PDO|null $connection = null;
|
||||
|
||||
/**
|
||||
* A global array of started connections
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $connections = [];
|
||||
public static array $connections = [];
|
||||
|
||||
/**
|
||||
* Database name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $database;
|
||||
protected string $database;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $dsn;
|
||||
protected string $dsn;
|
||||
|
||||
/**
|
||||
* Set to true to throw exceptions on failed queries
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $fail = false;
|
||||
protected bool $fail = false;
|
||||
|
||||
/**
|
||||
* The connection id
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
protected string $id;
|
||||
|
||||
/**
|
||||
* The last error
|
||||
*
|
||||
* @var \Exception|null
|
||||
*/
|
||||
protected $lastError;
|
||||
protected Exception|null $lastError = null;
|
||||
|
||||
/**
|
||||
* The last insert id
|
||||
*
|
||||
* @var int|null
|
||||
*/
|
||||
protected $lastId;
|
||||
protected int|null $lastId = null;
|
||||
|
||||
/**
|
||||
* The last query
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $lastQuery;
|
||||
protected string $lastQuery;
|
||||
|
||||
/**
|
||||
* The last result set
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
protected $lastResult;
|
||||
|
||||
/**
|
||||
* Optional prefix for table names
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $prefix;
|
||||
protected string|null $prefix = null;
|
||||
|
||||
/**
|
||||
* The PDO query statement
|
||||
*
|
||||
* @var \PDOStatement|null
|
||||
*/
|
||||
protected $statement;
|
||||
protected PDOStatement|null $statement = null;
|
||||
|
||||
/**
|
||||
* List of existing tables in the database
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
protected $tables;
|
||||
protected array|null $tables = null;
|
||||
|
||||
/**
|
||||
* An array with all queries which are being made
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $trace = [];
|
||||
protected array $trace = [];
|
||||
|
||||
/**
|
||||
* The database type (mysql, sqlite)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type;
|
||||
protected string $type;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $types = [];
|
||||
public static array $types = [];
|
||||
|
||||
/**
|
||||
* Creates a new Database instance
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
@@ -155,19 +116,18 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns one of the started instances
|
||||
*
|
||||
* @param string|null $id
|
||||
* @return static|null
|
||||
*/
|
||||
public static function instance(string $id = null)
|
||||
public static function instance(string|null $id = null): static|null
|
||||
{
|
||||
return $id === null ? A::last(static::$connections) : static::$connections[$id] ?? null;
|
||||
if ($id === null) {
|
||||
return A::last(static::$connections);
|
||||
}
|
||||
|
||||
return static::$connections[$id] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all started instances
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function instances(): array
|
||||
{
|
||||
@@ -178,10 +138,9 @@ class Database
|
||||
* Connects to a database
|
||||
*
|
||||
* @param array|null $params This can either be a config key or an array of parameters for the connection
|
||||
* @return \PDO|null
|
||||
* @throws \Kirby\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function connect(array $params = null)
|
||||
public function connect(array|null $params = null): PDO|null
|
||||
{
|
||||
$defaults = [
|
||||
'database' => null,
|
||||
@@ -227,10 +186,8 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns the currently active connection
|
||||
*
|
||||
* @return \PDO|null
|
||||
*/
|
||||
public function connection(): ?PDO
|
||||
public function connection(): PDO|null
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
@@ -238,10 +195,9 @@ class Database
|
||||
/**
|
||||
* Sets the exception mode
|
||||
*
|
||||
* @param bool $fail
|
||||
* @return \Kirby\Database\Database
|
||||
* @return $this
|
||||
*/
|
||||
public function fail(bool $fail = true)
|
||||
public function fail(bool $fail = true): static
|
||||
{
|
||||
$this->fail = $fail;
|
||||
return $this;
|
||||
@@ -249,8 +205,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns the used database type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function type(): string
|
||||
{
|
||||
@@ -259,8 +213,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns the used table name prefix
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function prefix(): string|null
|
||||
{
|
||||
@@ -270,9 +222,6 @@ class Database
|
||||
/**
|
||||
* Escapes a value to be used for a safe query
|
||||
* NOTE: Prepared statements using bound parameters are more secure and solid
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function escape(string $value): string
|
||||
{
|
||||
@@ -280,12 +229,10 @@ class Database
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a value to the db trace and also returns the entire trace if nothing is specified
|
||||
*
|
||||
* @param array|null $data
|
||||
* @return array
|
||||
* Adds a value to the db trace and also
|
||||
* returns the entire trace if nothing is specified
|
||||
*/
|
||||
public function trace(array $data = null): array
|
||||
public function trace(array|null $data = null): array
|
||||
{
|
||||
// return the full trace
|
||||
if ($data === null) {
|
||||
@@ -300,8 +247,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns the number of affected rows for the last query
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function affected(): int|null
|
||||
{
|
||||
@@ -310,8 +255,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns the last id if available
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function lastId(): int|null
|
||||
{
|
||||
@@ -320,8 +263,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns the last query
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function lastQuery(): string|null
|
||||
{
|
||||
@@ -330,8 +271,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns the last set of results
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function lastResult()
|
||||
{
|
||||
@@ -340,18 +279,14 @@ class Database
|
||||
|
||||
/**
|
||||
* Returns the last db error
|
||||
*
|
||||
* @return \Throwable
|
||||
*/
|
||||
public function lastError()
|
||||
public function lastError(): Throwable
|
||||
{
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the database
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function name(): string|null
|
||||
{
|
||||
@@ -361,10 +296,6 @@ class Database
|
||||
/**
|
||||
* Private method to execute database queries.
|
||||
* This is used by the query() and execute() methods
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
* @return bool
|
||||
*/
|
||||
protected function hit(string $query, array $bindings = []): bool
|
||||
{
|
||||
@@ -405,11 +336,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Executes a sql query, which is expected to return a set of results
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
* @param array $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function query(string $query, array $bindings = [], array $params = [])
|
||||
{
|
||||
@@ -466,11 +392,8 @@ class Database
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a sql query, which is expected to not return a set of results
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
* @return bool
|
||||
* Executes a sql query, which is expected
|
||||
* to not return a set of results
|
||||
*/
|
||||
public function execute(string $query, array $bindings = []): bool
|
||||
{
|
||||
@@ -480,10 +403,8 @@ class Database
|
||||
/**
|
||||
* Returns the correct Sql generator instance
|
||||
* for the type of database
|
||||
*
|
||||
* @return \Kirby\Database\Sql
|
||||
*/
|
||||
public function sql()
|
||||
public function sql(): Sql
|
||||
{
|
||||
$className = static::$types[$this->type]['sql'] ?? 'Sql';
|
||||
return new $className($this);
|
||||
@@ -493,20 +414,14 @@ class Database
|
||||
* 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 function table(string $table)
|
||||
public function table(string $table): Query
|
||||
{
|
||||
return new Query($this, $this->prefix() . $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a table exists in the current database
|
||||
*
|
||||
* @param string $table
|
||||
* @return bool
|
||||
*/
|
||||
public function validateTable(string $table): bool
|
||||
{
|
||||
@@ -527,10 +442,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Checks if a column exists in a specified table
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $column
|
||||
* @return bool
|
||||
*/
|
||||
public function validateColumn(string $table, string $column): bool
|
||||
{
|
||||
@@ -556,12 +467,8 @@ class Database
|
||||
|
||||
/**
|
||||
* Creates a new table
|
||||
*
|
||||
* @param string $table
|
||||
* @param array $columns
|
||||
* @return bool
|
||||
*/
|
||||
public function createTable($table, $columns = []): bool
|
||||
public function createTable(string $table, array $columns = []): bool
|
||||
{
|
||||
$sql = $this->sql()->createTable($table, $columns);
|
||||
$queries = Str::split($sql['query'], ';');
|
||||
@@ -584,9 +491,6 @@ class Database
|
||||
|
||||
/**
|
||||
* Drops a table
|
||||
*
|
||||
* @param string $table
|
||||
* @return bool
|
||||
*/
|
||||
public function dropTable(string $table): bool
|
||||
{
|
||||
@@ -608,12 +512,8 @@ class Database
|
||||
* Magic way to start queries for tables by
|
||||
* using a method named like the table.
|
||||
* I.e. $db->users()->all()
|
||||
*
|
||||
* @param mixed $method
|
||||
* @param mixed $arguments
|
||||
* @return \Kirby\Database\Query
|
||||
*/
|
||||
public function __call($method, $arguments = null)
|
||||
public function __call(string $method, mixed $arguments = null): Query
|
||||
{
|
||||
return $this->table($method);
|
||||
}
|
||||
@@ -624,7 +524,7 @@ class Database
|
||||
*/
|
||||
Database::$types['mysql'] = [
|
||||
'sql' => 'Kirby\Database\Sql\Mysql',
|
||||
'dsn' => function (array $params) {
|
||||
'dsn' => function (array $params): string {
|
||||
if (isset($params['host']) === false && isset($params['socket']) === false) {
|
||||
throw new InvalidArgumentException('The mysql connection requires either a "host" or a "socket" parameter');
|
||||
}
|
||||
@@ -662,7 +562,7 @@ Database::$types['mysql'] = [
|
||||
*/
|
||||
Database::$types['sqlite'] = [
|
||||
'sql' => 'Kirby\Database\Sql\Sqlite',
|
||||
'dsn' => function (array $params) {
|
||||
'dsn' => function (array $params): string {
|
||||
if (isset($params['database']) === false) {
|
||||
throw new InvalidArgumentException('The sqlite connection requires a "database" parameter');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user