Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
+177
-180
@@ -10,71 +10,83 @@
|
||||
|
||||
namespace IPS\Db;
|
||||
|
||||
use function preg_replace;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
|
||||
use Countable;
|
||||
use InvalidArgumentException;
|
||||
use IPS\Db;
|
||||
use Iterator;
|
||||
use UnderflowException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
use function is_callable;
|
||||
use function is_string;
|
||||
use function substr;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Database SELECT Statement
|
||||
*/
|
||||
class _Select implements \Iterator, \Countable
|
||||
class Select implements Iterator, Countable
|
||||
{
|
||||
/**
|
||||
* @brief The query
|
||||
*/
|
||||
public $query;
|
||||
|
||||
public string $query;
|
||||
|
||||
/**
|
||||
* @brief Non-JOIN Binds
|
||||
*/
|
||||
public $binds = array();
|
||||
|
||||
public array $binds = array();
|
||||
|
||||
/**
|
||||
* @brief JOIN Binds (need to separate them because the JOIN clauses come before the WHERE clause)
|
||||
*/
|
||||
protected $joinBinds = array();
|
||||
|
||||
protected array $joinBinds = array();
|
||||
|
||||
/**
|
||||
* @brief The database object
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
protected Db $db;
|
||||
|
||||
/**
|
||||
* @brief The statement
|
||||
*/
|
||||
protected $stmt;
|
||||
|
||||
protected mixed $stmt = NULL;
|
||||
|
||||
/**
|
||||
* @brief If TRUE, will return the result as a multidimensional array, with each joined table separately
|
||||
*/
|
||||
protected $multiDimensional = FALSE;
|
||||
|
||||
protected bool $multiDimensional = FALSE;
|
||||
|
||||
/**
|
||||
* @brief If read/write separation is enabled, SELECTs normally go to the read server - this flag will set this query to use the write server
|
||||
*/
|
||||
protected $useWriteServer = FALSE;
|
||||
protected bool $useWriteServer = FALSE;
|
||||
|
||||
/**
|
||||
* @brief Is this a union query? - this should only be set by the UNION method in \IPS\Db
|
||||
*/
|
||||
public $isUnion = FALSE;
|
||||
|
||||
public bool $isUnion = FALSE;
|
||||
|
||||
/**
|
||||
* Constuctor
|
||||
*
|
||||
* @param string $query The query
|
||||
* @param string $query The query
|
||||
* @param array $binds Binds
|
||||
* @param \IPS\Db $db The database object
|
||||
* @param bool $multiDimensional If TRUE, will return the result as a multidimensional array, with each joined table separately
|
||||
* @param bool $useWriteServer If read/write sepration is enabled, SELECTs normally go to the read server - this flag will set thia query to use the write server
|
||||
* @param Db $db The database object
|
||||
* @param bool $multiDimensional If TRUE, will return the result as a multidimensional array, with each joined table separately
|
||||
* @param bool $useWriteServer If read/write sepration is enabled, SELECTs normally go to the read server - this flag will set thia query to use the write server
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $query, array $binds, \IPS\Db $db, $multiDimensional = FALSE, $useWriteServer = FALSE )
|
||||
public function __construct( string $query, array $binds, Db $db, bool $multiDimensional=FALSE, bool $useWriteServer=FALSE )
|
||||
{
|
||||
$this->query = $query;
|
||||
$this->binds = $binds;
|
||||
@@ -98,35 +110,18 @@ class _Select implements \Iterator, \Countable
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function returnFullQuery()
|
||||
public function returnFullQuery(): string
|
||||
{
|
||||
return \IPS\Db::_replaceBinds( $this->query, array_merge( $this->joinBinds, $this->binds ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a max execution time for this query
|
||||
*
|
||||
* @param int $milliseconds Number of milliseconds to set as the max execution time
|
||||
* @return \IPS\Db\Select
|
||||
*/
|
||||
public function setExecutionTime( int $milliseconds )
|
||||
{
|
||||
/* This feature was introduced in MySQL 5.7.4 @note https://dev.mysql.com/doc/refman/8.4/en/optimizer-hints.html#optimizer-hints-execution-time */
|
||||
if ( ! mb_stristr( \IPS\Db::i()->server_info, '-MariaDB' ) and \IPS\Db::i()->server_version >= 50704 )
|
||||
{
|
||||
$this->query = preg_replace( '/(^SELECT\s+)([a-z]|\*)/', "$1 /*+ MAX_EXECUTION_TIME({$milliseconds}) */ $2", $this->query, 1 );
|
||||
}
|
||||
|
||||
return $this;
|
||||
return Db::_replaceBinds( $this->query, array_merge( $this->joinBinds, $this->binds ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Force an index
|
||||
*
|
||||
* @param string $index Index name to force
|
||||
* @return \IPS\Db\Select
|
||||
* @param string $index Index name to force
|
||||
* @return Select
|
||||
*/
|
||||
public function forceIndex( $index )
|
||||
public function forceIndex( string $index ): Select
|
||||
{
|
||||
$this->query = preg_replace( '/(FROM `(.+?)`( AS `(.+?)`)?)/', "$1 FORCE INDEX(`" . $index . "`)", $this->query, 1 );
|
||||
|
||||
@@ -136,32 +131,32 @@ class _Select implements \Iterator, \Countable
|
||||
/**
|
||||
* Add Join
|
||||
*
|
||||
* @param array|string|\IPS\Db\Select $table The table to select from. Either (string) table_name or (array) ( name, alias ) or \IPS\Db\Select object
|
||||
* @param array|string|Select $table The table to select from. Either (string) table_name or (array) ( name, alias ) or \IPS\Db\Select object
|
||||
* @param mixed $on The on clause for the join
|
||||
* @param string $type Type of join (left, right, inner, cross, straight_join)
|
||||
* @param bool $using Whether to append the using clause for the join
|
||||
* @return \IPS\Db\Select
|
||||
* @param string $type Type of join (left, right, inner, cross, straight_join)
|
||||
* @param bool $using Whether to append the using clause for the join
|
||||
* @return Select
|
||||
*/
|
||||
public function join( $table, $on, $type='LEFT', $using=FALSE )
|
||||
public function join( Select|array|string $table, mixed $on, string $type='LEFT', bool $using=FALSE ): Select
|
||||
{
|
||||
$query = '';
|
||||
$joinConditionIsOptional = TRUE;
|
||||
|
||||
|
||||
switch ( $type )
|
||||
{
|
||||
case 'INNER':
|
||||
case 'CROSS':
|
||||
$query .= ' INNER JOIN ';
|
||||
break;
|
||||
|
||||
|
||||
case 'STRAIGHT_JOIN':
|
||||
$query .= ' STRAIGHT_JOIN';
|
||||
if ( $using )
|
||||
{
|
||||
throw new \InvalidArgumentException; // USING cannot be used with STRAIGHT_JOIN
|
||||
throw new InvalidArgumentException; // USING cannot be used with STRAIGHT_JOIN
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case 'LEFT':
|
||||
case 'RIGHT':
|
||||
$query .= ' ' . $type . ' JOIN';
|
||||
@@ -172,11 +167,11 @@ class _Select implements \Iterator, \Countable
|
||||
break;
|
||||
}
|
||||
|
||||
if ( $table instanceof \IPS\Db\Select )
|
||||
{
|
||||
if ( $table instanceof Select)
|
||||
{
|
||||
$tableQuery = $table->query;
|
||||
preg_match( '/FROM `(.+?)`( AS `(.+?)`)?/', $tableQuery, $matches );
|
||||
|
||||
|
||||
if ( isset( $matches[2] ) )
|
||||
{
|
||||
$query .= "( " . str_replace( $matches[2], '', $tableQuery ) . " ) AS `{$matches[3]}`";
|
||||
@@ -186,9 +181,9 @@ class _Select implements \Iterator, \Countable
|
||||
$query .= "( {$tableQuery} ) AS `{$matches[1]}`";
|
||||
}
|
||||
}
|
||||
elseif ( \is_array( $table ) )
|
||||
elseif ( is_array( $table ) )
|
||||
{
|
||||
if ( \substr( $table[0], 0, 1 ) === '(' )
|
||||
if ( substr( $table[0], 0, 1 ) === '(' )
|
||||
{
|
||||
$query .= " {$table[0]} AS `{$table[1]}`";
|
||||
}
|
||||
@@ -201,21 +196,21 @@ class _Select implements \Iterator, \Countable
|
||||
{
|
||||
$query .= $this->db->prefix ? " `{$this->db->prefix}{$table}` AS `{$table}`" : " `{$table}`";
|
||||
}
|
||||
|
||||
|
||||
if ( $on )
|
||||
{
|
||||
if ( $using )
|
||||
{
|
||||
$query .= ' USING ( ' . implode( ', ', array_map( function( $col )
|
||||
{
|
||||
return '`' . $col . '`';
|
||||
}, $on ) ) . ' ) ';
|
||||
{
|
||||
return '`' . $col . '`';
|
||||
}, $on ) ) . ' ) ';
|
||||
}
|
||||
else if ( \is_array( $on ) AND isset($on[1]) and $on[1] instanceof \IPS\Db\Select )
|
||||
else if ( is_array( $on ) AND isset($on[1]) and $on[1] instanceof Select)
|
||||
{
|
||||
$query .= ' ON ' . $on[0] . '=(' . $on[1]->query . ')';
|
||||
|
||||
if( \count( $on[1]->binds ) )
|
||||
if( count( $on[1]->binds ) )
|
||||
{
|
||||
foreach ( $on[1]->binds as $bind )
|
||||
{
|
||||
@@ -235,7 +230,7 @@ class _Select implements \Iterator, \Countable
|
||||
}
|
||||
elseif ( !$joinConditionIsOptional )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
|
||||
/* If we are joining on a sub-query already, remove it temporarily to ensure our regex below works correctly */
|
||||
@@ -265,223 +260,223 @@ class _Select implements \Iterator, \Countable
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Columns in the resultset
|
||||
*/
|
||||
protected $columns = array();
|
||||
|
||||
protected array $columns = array();
|
||||
|
||||
/**
|
||||
* @brief Key Field
|
||||
*/
|
||||
protected $keyField = NULL;
|
||||
|
||||
protected ?string $keyField = NULL;
|
||||
|
||||
/**
|
||||
* @brief Key Table
|
||||
*/
|
||||
protected $keyTable = NULL;
|
||||
|
||||
protected ?string $keyTable = NULL;
|
||||
|
||||
/**
|
||||
* @brief Value Field
|
||||
*/
|
||||
protected $valueField = NULL;
|
||||
|
||||
protected ?string $valueField = NULL;
|
||||
|
||||
/**
|
||||
* @brief Value Table
|
||||
*/
|
||||
protected $valueTable = NULL;
|
||||
|
||||
protected ?string $valueTable = NULL;
|
||||
|
||||
/**
|
||||
* Set key field
|
||||
*
|
||||
* @param string $column Column to treat as the key
|
||||
* @param string|NULL $table The table, if this is a multidimensional select
|
||||
* @return \IPS\Db\Select
|
||||
* @param callable|string $column Column to treat as the key
|
||||
* @param string|null $table The table, if this is a multidimensional select
|
||||
* @return Select
|
||||
*/
|
||||
public function setKeyField( $column, $table=NULL )
|
||||
public function setKeyField( callable|string $column, string $table=NULL ): Select
|
||||
{
|
||||
if ( !$this->stmt )
|
||||
{
|
||||
$this->runQuery();
|
||||
}
|
||||
|
||||
if ( \is_string( $column ) )
|
||||
if ( is_string( $column ) )
|
||||
{
|
||||
if ( $this->multiDimensional )
|
||||
{
|
||||
if ( !isset( $this->columns[ $table ] ) or !\in_array( $column, $this->columns[ $table ] ) )
|
||||
if ( !isset( $this->columns[ $table ] ) or !in_array( $column, $this->columns[ $table ] ) )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !\in_array( $column, $this->columns ) )
|
||||
if ( !in_array( $column, $this->columns ) )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$this->keyField = $column;
|
||||
$this->keyTable = $table;
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set value field
|
||||
*
|
||||
* @param string|callback $column Column to treat as the value. Callback to determine on a per-row basis.
|
||||
* @param string|NULL $table The table, if this is a multidimensional select
|
||||
* @return \IPS\Db\Select
|
||||
* @param callback|string $column Column to treat as the value. Callback to determine on a per-row basis.
|
||||
* @param string|null $table The table, if this is a multidimensional select
|
||||
* @return Select
|
||||
*/
|
||||
public function setValueField( $column, $table=NULL )
|
||||
public function setValueField( callable|string $column, string $table=NULL ): Select
|
||||
{
|
||||
if ( !$this->stmt )
|
||||
{
|
||||
$this->runQuery();
|
||||
}
|
||||
|
||||
if ( \is_string( $column ) )
|
||||
|
||||
if ( is_string( $column ) )
|
||||
{
|
||||
if ( $this->multiDimensional )
|
||||
{
|
||||
if ( !isset( $this->columns[ $table ] ) or !\in_array( $column, $this->columns[ $table ] ) )
|
||||
if ( !isset( $this->columns[ $table ] ) or !in_array( $column, $this->columns[ $table ] ) )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !\in_array( $column, $this->columns ) )
|
||||
if ( !in_array( $column, $this->columns ) )
|
||||
{
|
||||
throw new \InvalidArgumentException;
|
||||
throw new InvalidArgumentException;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$this->valueField = $column;
|
||||
$this->valueTable = $table;
|
||||
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief The current row
|
||||
*/
|
||||
protected $row;
|
||||
|
||||
protected mixed $row = NULL;
|
||||
|
||||
/**
|
||||
* @brief The current key
|
||||
*/
|
||||
protected $key;
|
||||
|
||||
protected ?int $key = null;
|
||||
|
||||
/**
|
||||
* Get first record
|
||||
*
|
||||
* @return array|int|string
|
||||
* @throws \UnderflowException
|
||||
* @return mixed
|
||||
* @throws UnderflowException
|
||||
*/
|
||||
public function first()
|
||||
{
|
||||
public function first(): mixed
|
||||
{
|
||||
/* Move to the first result */
|
||||
$this->rewind();
|
||||
|
||||
|
||||
/* Return it */
|
||||
if ( !$this->valid() )
|
||||
{
|
||||
throw new \UnderflowException;
|
||||
throw new UnderflowException;
|
||||
}
|
||||
return $this->current();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Run the query
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function runQuery()
|
||||
protected function runQuery() : void
|
||||
{
|
||||
/* Run the query */
|
||||
$this->stmt = $this->db->preparedQuery( $this->query, array_merge( $this->joinBinds, $this->binds ), !$this->useWriteServer );
|
||||
|
||||
|
||||
/* Populate $this->row which we read into */
|
||||
$this->row = array();
|
||||
$params = array();
|
||||
$meta = $this->stmt->result_metadata();
|
||||
$meta = $this->stmt->result_metadata();
|
||||
|
||||
if ( !$meta )
|
||||
{
|
||||
throw new \IPS\Db\Exception( $this->db->errno ? $this->db->error : "No result metadata - possible table crash", $this->db->errno ?: -1, NULL, $this->query, array_merge( $this->joinBinds, $this->binds ) );
|
||||
throw new Exception( $this->db->errno ? $this->db->error : "No result metadata - possible table crash", $this->db->errno ?: -1, NULL, $this->query, array_merge( $this->joinBinds, $this->binds ) );
|
||||
}
|
||||
|
||||
while ( $field = $meta->fetch_field() )
|
||||
{
|
||||
if ( $this->multiDimensional )
|
||||
{
|
||||
$params[] = &$this->row[ $field->table ][ $field->name ];
|
||||
}
|
||||
else
|
||||
{
|
||||
$params[] = &$this->row[ $field->name ];
|
||||
}
|
||||
}
|
||||
|
||||
$meta->free_result();
|
||||
while ( $field = $meta->fetch_field() )
|
||||
{
|
||||
if ( $this->multiDimensional )
|
||||
{
|
||||
$params[] = &$this->row[ $field->table ][ $field->name ];
|
||||
}
|
||||
else
|
||||
{
|
||||
$params[] = &$this->row[ $field->name ];
|
||||
}
|
||||
}
|
||||
|
||||
if ( $this->multiDimensional )
|
||||
{
|
||||
foreach ( $this->row as $table => $columns )
|
||||
{
|
||||
$this->columns[ $table ] = array_keys( $columns );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->columns = array_keys( $this->row );
|
||||
}
|
||||
|
||||
$stmtReference = $this->stmt;
|
||||
$stmtReference->bind_result( ...$params );
|
||||
|
||||
/* Set counts */
|
||||
$this->count = $this->stmt->num_rows;
|
||||
|
||||
/* Set the key to -1 (i.e. we haven't fetched any row yet) */
|
||||
$this->key = -1;
|
||||
$meta->free_result();
|
||||
|
||||
if ( $this->multiDimensional )
|
||||
{
|
||||
foreach ( $this->row as $table => $columns )
|
||||
{
|
||||
$this->columns[ $table ] = array_keys( $columns );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->columns = array_keys( $this->row );
|
||||
}
|
||||
|
||||
$stmtReference = $this->stmt;
|
||||
$stmtReference->bind_result( ...$params );
|
||||
|
||||
/* Set counts */
|
||||
$this->count = $this->stmt->num_rows;
|
||||
|
||||
/* Set the key to -1 (i.e. we haven't fetched any row yet) */
|
||||
$this->key = -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* [Iterator] Rewind - will (re-)execute statement
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
public function rewind(): void
|
||||
{
|
||||
/* If we haven't run the query yet, or we have already traversed the result set beyond the first result, (re-)run the query */
|
||||
if ( !$this->stmt or $this->key > 0 )
|
||||
{
|
||||
$this->runQuery();
|
||||
}
|
||||
|
||||
|
||||
/* Move to the first result if we haven't already */
|
||||
if ( $this->key === -1 )
|
||||
{
|
||||
$this->next();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* [Iterator] Get current row
|
||||
*
|
||||
* @return array
|
||||
* @return mixed
|
||||
*/
|
||||
public function current()
|
||||
public function current(): mixed
|
||||
{
|
||||
if ( $this->valueField )
|
||||
{
|
||||
if ( !\is_string( $this->valueField ) and \is_callable( $this->valueField ) )
|
||||
if ( !is_string( $this->valueField ) and is_callable( $this->valueField ) )
|
||||
{
|
||||
$valueField = $this->valueField;
|
||||
return $valueField( $this->row );
|
||||
@@ -498,7 +493,7 @@ class _Select implements \Iterator, \Countable
|
||||
}
|
||||
}
|
||||
}
|
||||
elseif ( \count( $this->row ) === 1 )
|
||||
elseif ( count( $this->row ) === 1 )
|
||||
{
|
||||
foreach ( $this->row as $v )
|
||||
{
|
||||
@@ -510,7 +505,7 @@ class _Select implements \Iterator, \Countable
|
||||
$row = array();
|
||||
foreach ( $this->row as $k => $v )
|
||||
{
|
||||
if ( \is_array( $v ) )
|
||||
if ( is_array( $v ) )
|
||||
{
|
||||
foreach ( $v as $k2 => $v2 )
|
||||
{
|
||||
@@ -524,18 +519,20 @@ class _Select implements \Iterator, \Countable
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* [Iterator] Get current key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function key()
|
||||
public function key(): mixed
|
||||
{
|
||||
if ( $this->keyField )
|
||||
{
|
||||
if ( \is_string( $this->keyField ) )
|
||||
if ( is_string( $this->keyField ) )
|
||||
{
|
||||
if ( $this->keyTable )
|
||||
{
|
||||
@@ -557,50 +554,50 @@ class _Select implements \Iterator, \Countable
|
||||
return $this->key;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* [Iterator] Fetch next result
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
public function next(): void
|
||||
{
|
||||
$fetch = $this->stmt->fetch();
|
||||
if ( $fetch === NULL )
|
||||
{
|
||||
$this->stmt->close();
|
||||
$this->row = NULL;
|
||||
}
|
||||
$this->key++;
|
||||
if ( $fetch === NULL )
|
||||
{
|
||||
$this->stmt->close();
|
||||
$this->row = NULL;
|
||||
}
|
||||
$this->key++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* [Iterator] Is the current row valid?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
public function valid(): bool
|
||||
{
|
||||
return ( $this->row !== NULL );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Number of rows in this set
|
||||
*/
|
||||
protected $count;
|
||||
protected ?int $count = NULL;
|
||||
|
||||
/**
|
||||
* [Countable] Get number of rows
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
public function count(): int
|
||||
{
|
||||
if ( !$this->stmt )
|
||||
{
|
||||
$this->runQuery();
|
||||
}
|
||||
|
||||
|
||||
return $this->count;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user