Upgrade to 3.4.3
This commit is contained in:
@@ -8,7 +8,6 @@ use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\Str;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
@@ -39,7 +38,7 @@ class Database
|
||||
/**
|
||||
* The established connection
|
||||
*
|
||||
* @var PDO|null
|
||||
* @var \PDO|null
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
@@ -79,7 +78,7 @@ class Database
|
||||
/**
|
||||
* The last error
|
||||
*
|
||||
* @var Exception|null
|
||||
* @var \Exception|null
|
||||
*/
|
||||
protected $lastError;
|
||||
|
||||
@@ -114,16 +113,16 @@ class Database
|
||||
/**
|
||||
* The PDO query statement
|
||||
*
|
||||
* @var PDOStatement|null
|
||||
* @var \PDOStatement|null
|
||||
*/
|
||||
protected $statement;
|
||||
|
||||
/**
|
||||
* Whitelists for table names
|
||||
* List of existing tables in the database
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
protected $tableWhitelist;
|
||||
protected $tables;
|
||||
|
||||
/**
|
||||
* An array with all queries which are being made
|
||||
@@ -156,10 +155,10 @@ class Database
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one of the started instance
|
||||
* Returns one of the started instances
|
||||
*
|
||||
* @param string $id
|
||||
* @return self
|
||||
* @param string|null $id
|
||||
* @return self|null
|
||||
*/
|
||||
public static function instance(string $id = null)
|
||||
{
|
||||
@@ -180,7 +179,8 @@ 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 \Kirby\Database\Database
|
||||
* @return \PDO|null
|
||||
* @throws \Kirby\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function connect(array $params = null)
|
||||
{
|
||||
@@ -223,9 +223,9 @@ class Database
|
||||
/**
|
||||
* Returns the currently active connection
|
||||
*
|
||||
* @return \Kirby\Database\Database|null
|
||||
* @return \PDO|null
|
||||
*/
|
||||
public function connection()
|
||||
public function connection(): ?PDO
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
@@ -277,10 +277,10 @@ class Database
|
||||
/**
|
||||
* Adds a value to the db trace and also returns the entire trace if nothing is specified
|
||||
*
|
||||
* @param array $data
|
||||
* @param array|null $data
|
||||
* @return array
|
||||
*/
|
||||
public function trace($data = null): array
|
||||
public function trace(array $data = null): array
|
||||
{
|
||||
// return the full trace
|
||||
if ($data === null) {
|
||||
@@ -336,7 +336,7 @@ class Database
|
||||
/**
|
||||
* Returns the last db error
|
||||
*
|
||||
* @return Throwable
|
||||
* @return \Throwable
|
||||
*/
|
||||
public function lastError()
|
||||
{
|
||||
@@ -363,7 +363,6 @@ class Database
|
||||
*/
|
||||
protected function hit(string $query, array $bindings = []): bool
|
||||
{
|
||||
|
||||
// try to prepare and execute the sql
|
||||
try {
|
||||
$this->statement = $this->connection->prepare($query);
|
||||
@@ -401,7 +400,7 @@ class Database
|
||||
}
|
||||
|
||||
/**
|
||||
* Exectues a sql query, which is expected to return a set of results
|
||||
* Executes a sql query, which is expected to return a set of results
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
@@ -504,19 +503,19 @@ class Database
|
||||
*/
|
||||
public function validateTable(string $table): bool
|
||||
{
|
||||
if ($this->tableWhitelist === null) {
|
||||
// Get the table whitelist from the database
|
||||
if ($this->tables === null) {
|
||||
// Get the list of tables from the database
|
||||
$sql = $this->sql()->tables($this->database);
|
||||
$results = $this->query($sql['query'], $sql['bindings']);
|
||||
|
||||
if ($results) {
|
||||
$this->tableWhitelist = $results->pluck('name');
|
||||
$this->tables = $results->pluck('name');
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return in_array($table, $this->tableWhitelist) === true;
|
||||
return in_array($table, $this->tables) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -569,8 +568,8 @@ class Database
|
||||
}
|
||||
|
||||
// update cache
|
||||
if (in_array($table, $this->tableWhitelist) !== true) {
|
||||
$this->tableWhitelist[] = $table;
|
||||
if (in_array($table, $this->tables ?? []) !== true) {
|
||||
$this->tables[] = $table;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -582,7 +581,7 @@ class Database
|
||||
* @param string $table
|
||||
* @return bool
|
||||
*/
|
||||
public function dropTable($table): bool
|
||||
public function dropTable(string $table): bool
|
||||
{
|
||||
$sql = $this->sql()->dropTable($table);
|
||||
if ($this->execute($sql['query'], $sql['bindings']) !== true) {
|
||||
@@ -590,9 +589,9 @@ class Database
|
||||
}
|
||||
|
||||
// update cache
|
||||
$key = array_search($this->tableWhitelist, $table);
|
||||
$key = array_search($table, $this->tables ?? []);
|
||||
if ($key !== false) {
|
||||
unset($this->tableWhitelist[$key]);
|
||||
unset($this->tables[$key]);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -605,6 +604,7 @@ class Database
|
||||
*
|
||||
* @param mixed $method
|
||||
* @param mixed $arguments
|
||||
* @return \Kirby\Database\Query
|
||||
*/
|
||||
public function __call($method, $arguments = null)
|
||||
{
|
||||
|
@@ -26,7 +26,7 @@ class Db
|
||||
/**
|
||||
* The singleton Database object
|
||||
*
|
||||
* @var Database
|
||||
* @var \Kirby\Database\Database
|
||||
*/
|
||||
public static $connection = null;
|
||||
|
||||
@@ -76,7 +76,7 @@ class Db
|
||||
* @param string $table
|
||||
* @return \Kirby\Database\Query
|
||||
*/
|
||||
public static function table($table)
|
||||
public static function table(string $table)
|
||||
{
|
||||
$db = static::connect();
|
||||
return $db->table($table);
|
||||
@@ -117,8 +117,9 @@ class Db
|
||||
* @param string $method
|
||||
* @param mixed $arguments
|
||||
* @return mixed
|
||||
* @throws \Kirby\Exception\InvalidArgumentException
|
||||
*/
|
||||
public static function __callStatic($method, $arguments)
|
||||
public static function __callStatic(string $method, $arguments)
|
||||
{
|
||||
if (isset(static::$queries[$method])) {
|
||||
return static::$queries[$method](...$arguments);
|
||||
|
@@ -24,7 +24,7 @@ class Query
|
||||
/**
|
||||
* Parent Database object
|
||||
*
|
||||
* @var Database
|
||||
* @var \Kirby\Database\Database
|
||||
*/
|
||||
protected $database = null;
|
||||
|
||||
@@ -251,6 +251,7 @@ class Query
|
||||
*
|
||||
* @param string $table
|
||||
* @return \Kirby\Database\Query
|
||||
* @throws \Kirby\Exception\InvalidArgumentException if the table does not exist
|
||||
*/
|
||||
public function table(string $table)
|
||||
{
|
||||
@@ -293,7 +294,7 @@ class Query
|
||||
* @param string $table Name of the table, which should be joined
|
||||
* @param string $on The on clause for this join
|
||||
* @param string $type The join type. Uses an inner join by default
|
||||
* @return object
|
||||
* @return self
|
||||
*/
|
||||
public function join(string $table, string $on, string $type = 'JOIN')
|
||||
{
|
||||
@@ -362,7 +363,7 @@ class Query
|
||||
* Also can be used as getter for all attached bindings by not passing an argument.
|
||||
*
|
||||
* @param mixed $bindings Array of bindings or null to use this method as getter
|
||||
* @return array|Query
|
||||
* @return array|\Kirby\Database\Query
|
||||
*/
|
||||
public function bindings(array $bindings = null)
|
||||
{
|
||||
@@ -445,7 +446,7 @@ class Query
|
||||
/**
|
||||
* Attaches a group by clause
|
||||
*
|
||||
* @param string $group
|
||||
* @param string|null $group
|
||||
* @return \Kirby\Database\Query
|
||||
*/
|
||||
public function group(string $group = null)
|
||||
@@ -477,7 +478,7 @@ class Query
|
||||
/**
|
||||
* Attaches an order clause
|
||||
*
|
||||
* @param string $order
|
||||
* @param string|null $order
|
||||
* @return \Kirby\Database\Query
|
||||
*/
|
||||
public function order(string $order = null)
|
||||
@@ -489,7 +490,7 @@ class Query
|
||||
/**
|
||||
* Sets the offset for select clauses
|
||||
*
|
||||
* @param int $offset
|
||||
* @param int|null $offset
|
||||
* @return \Kirby\Database\Query
|
||||
*/
|
||||
public function offset(int $offset = null)
|
||||
@@ -501,7 +502,7 @@ class Query
|
||||
/**
|
||||
* Sets the limit for select clauses
|
||||
*
|
||||
* @param int $limit
|
||||
* @param int|null $limit
|
||||
* @return \Kirby\Database\Query
|
||||
*/
|
||||
public function limit(int $limit = null)
|
||||
@@ -515,9 +516,9 @@ class Query
|
||||
* This uses the SQL class to build stuff.
|
||||
*
|
||||
* @param string $type (select, update, insert)
|
||||
* @return string The final query
|
||||
* @return array The final query
|
||||
*/
|
||||
public function build($type)
|
||||
public function build(string $type)
|
||||
{
|
||||
$sql = $this->database->sql();
|
||||
|
||||
@@ -618,7 +619,7 @@ class Query
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $column
|
||||
* @param string $default An optional default value, which should be returned if the query fails
|
||||
* @param int $default An optional default value, which should be returned if the query fails
|
||||
* @return mixed
|
||||
*/
|
||||
public function aggregate(string $method, string $column = '*', $default = 0)
|
||||
@@ -812,7 +813,7 @@ class Query
|
||||
* @param string $column
|
||||
* @return mixed
|
||||
*/
|
||||
public function column($column)
|
||||
public function column(string $column)
|
||||
{
|
||||
// if there isn't already an explicit order, order by the primary key
|
||||
// instead of the column that was requested (which would be implied otherwise)
|
||||
@@ -850,7 +851,7 @@ class Query
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function findBy($column, $value)
|
||||
public function findBy(string $column, $value)
|
||||
{
|
||||
return $this->where([$column => $value])->first();
|
||||
}
|
||||
@@ -869,7 +870,7 @@ class Query
|
||||
/**
|
||||
* Fires an insert query
|
||||
*
|
||||
* @param array $values You can pass values here or set them with ->values() before
|
||||
* @param mixed $values You can pass values here or set them with ->values() before
|
||||
* @return mixed Returns the last inserted id on success or false.
|
||||
*/
|
||||
public function insert($values = null)
|
||||
@@ -886,7 +887,7 @@ class Query
|
||||
/**
|
||||
* Fires an update query
|
||||
*
|
||||
* @param array $values You can pass values here or set them with ->values() before
|
||||
* @param mixed $values You can pass values here or set them with ->values() before
|
||||
* @param mixed $where You can pass a where clause here or set it with ->where() before
|
||||
* @return bool
|
||||
*/
|
||||
@@ -927,10 +928,10 @@ class Query
|
||||
* Builder for where and having clauses
|
||||
*
|
||||
* @param array $args Arguments, see where() description
|
||||
* @param string $current Current value (like $this->where)
|
||||
* @param mixed $current Current value (like $this->where)
|
||||
* @return string
|
||||
*/
|
||||
protected function filterQuery($args, $current)
|
||||
protected function filterQuery(array $args, $current)
|
||||
{
|
||||
$mode = A::last($args);
|
||||
$result = '';
|
||||
|
@@ -175,6 +175,7 @@ abstract class Sql
|
||||
* - `unique`: Whether the index (or if not set the column itself) has a UNIQUE constraint
|
||||
* - `default`: Default value of this column
|
||||
* @return array Array with `query` and `key` strings, a `unique` boolean and a `bindings` array
|
||||
* @throws \Kirby\Exception\InvalidArgumentException if no column type is given or the column type is not supported.
|
||||
*/
|
||||
public function createColumn(string $name, array $column): array
|
||||
{
|
||||
@@ -408,7 +409,7 @@ abstract class Sql
|
||||
/**
|
||||
* Creates the having syntax
|
||||
*
|
||||
* @param string $having
|
||||
* @param string|null $having
|
||||
* @return array
|
||||
*/
|
||||
public function having(string $having = null): array
|
||||
@@ -455,6 +456,7 @@ abstract class Sql
|
||||
* @param string $type
|
||||
* @param string $on
|
||||
* @return array
|
||||
* @throws \Kirby\Exception\InvalidArgumentException if an invalid join type is given
|
||||
*/
|
||||
public function join(string $type, string $table, string $on): array
|
||||
{
|
||||
@@ -489,7 +491,7 @@ abstract class Sql
|
||||
/**
|
||||
* Create the syntax for multiple joins
|
||||
*
|
||||
* @param array $joins
|
||||
* @param array|null $joins
|
||||
* @return array
|
||||
*/
|
||||
public function joins(array $joins = null): array
|
||||
@@ -692,6 +694,7 @@ abstract class Sql
|
||||
* @param $table string Default table if the identifier is not qualified
|
||||
* @param $identifier string
|
||||
* @return array
|
||||
* @throws \Kirby\Exception\InvalidArgumentException if an invalid identifier is given
|
||||
*/
|
||||
public function splitIdentifier($table, $identifier): array
|
||||
{
|
||||
@@ -726,6 +729,7 @@ abstract class Sql
|
||||
*
|
||||
* @param string $table
|
||||
* @return string
|
||||
* @throws \Kirby\Exception\InvalidArgumentException if an invalid table name is given
|
||||
*/
|
||||
public function tableName(string $table): string
|
||||
{
|
||||
@@ -797,7 +801,6 @@ abstract class Sql
|
||||
* @param string $table
|
||||
* @param string $column
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the column is invalid
|
||||
*/
|
||||
public function validateColumn(string $table, string $column): bool
|
||||
@@ -882,6 +885,7 @@ abstract class Sql
|
||||
* @param string $separator
|
||||
* @param bool $enforceQualified
|
||||
* @param array
|
||||
* @return array
|
||||
*/
|
||||
public function valueSet(string $table, $values, string $separator = ',', bool $enforceQualified = false): array
|
||||
{
|
||||
|
Reference in New Issue
Block a user