Database class
Framework\Database\Database
Class Database.
Defined in libraries/database/src/Database.php
Properties
-
$mysqli
- -
-
$config
- Connection configurations.
-
$failoverIndex
- The current $config failover index to be used in a connection.
-
$inTransaction
- -
-
$lastQuery
- -
-
$logger
- -
-
$debugCollector
- -
$mysqli
protected ?mysqli $mysqli
$config
protected array $config = array (
)
Connection configurations.
$failoverIndex
protected ?int $failoverIndex = NULL
The current $config failover index to be used in a connection.
$inTransaction
protected bool $inTransaction = false
$lastQuery
protected string $lastQuery = ''
$logger
protected ?\Framework\Log\Logger $logger
$debugCollector
protected \Framework\Database\Debug\DatabaseCollector $debugCollector
Methods
-
__construct()
- Database constructor.
-
__destruct()
- -
-
alterSchema()
- Call a ALTER SCHEMA statement.
-
alterTable()
- Call a ALTER TABLE statement.
-
close()
- Closes the connection if it is open.
-
createSchema()
- Call a CREATE SCHEMA statement.
-
createTable()
- Call a CREATE TABLE statement.
-
delete()
- Call a DELETE statement.
-
dropSchema()
- Call a DROP SCHEMA statement.
-
dropTable()
- Call a DROP TABLE statement.
-
exec()
- Executes an SQL statement and return the number of affected rows.
-
getConfig()
- -
-
getConnection()
- Gets the MySQLi connection.
-
getError()
- Get latest error.
-
getErrors()
- Get a list of the latest errors.
-
getInsertId()
- Gets the LAST_INSERT_ID().
-
getLastQuery()
- -
-
getWarnings()
- -
-
getWarningsCount()
- -
-
insert()
- Call a INSERT statement.
-
isOpen()
- Tells if the connection is open.
-
loadData()
- Call a LOAD DATA statement.
-
ping()
- Pings the server, or tries to reconnect if the connection has gone down.
-
prepare()
- Prepares a statement for execution and returns a PreparedStatement object.
-
protectIdentifier()
- Protect identifier.
-
query()
- Executes an SQL statement, returning a result set as a Result object.
-
quote()
- Quote SQL values.
-
reconnect()
- Closes the current and opens a new connection with the last config.
-
replace()
- Call a REPLACE statement.
-
select()
- Call a SELECT statement.
-
setDebugCollector()
- -
-
transaction()
- Run statements in a transaction.
-
update()
- Call a UPDATE statement.
-
use()
- -
-
with()
- Call a WITH statement.
-
addToDebug()
- -
-
connect()
- -
-
finalizeAddToDebug()
- -
-
log()
- -
-
makeConfig()
- Make Base Connection configurations.
-
setCollations()
- -
-
setTimezone()
- -
__construct()
public function __construct(array|string $username, ?string $password = NULL, ?string $schema = NULL, string $host = 'localhost', int $port = 3306, ?\Framework\Log\Logger $logger = NULL, ?\Framework\Database\Debug\DatabaseCollector $collector = NULL)
Database constructor.
Parameters
-
array|string
$username
-
?string
$password
-
?string
$schema
-
string
$host
-
int
$port
-
?\Framework\Log\Logger
$logger
-
?\Framework\Database\Debug\DatabaseCollector
$collector
- throws
mysqli_sql_exception if connections fail
__destruct()
public function __destruct()
alterSchema()
public function alterSchema(?string $schemaName = NULL): \Framework\Database\Definition\AlterSchema
Call a ALTER SCHEMA statement.
Returns
\Framework\Database\Definition\AlterSchema
alterTable()
public function alterTable(?string $tableName = NULL): \Framework\Database\Definition\AlterTable
Call a ALTER TABLE statement.
Returns
\Framework\Database\Definition\AlterTable
close()
public function close(): bool
Closes the connection if it is open.
Returns
bool
createSchema()
public function createSchema(?string $schemaName = NULL): \Framework\Database\Definition\CreateSchema
Call a CREATE SCHEMA statement.
Returns
\Framework\Database\Definition\CreateSchema
createTable()
public function createTable(?string $tableName = NULL): \Framework\Database\Definition\CreateTable
Call a CREATE TABLE statement.
Returns
\Framework\Database\Definition\CreateTable
delete()
public function delete(Closure|array|string|null $reference = NULL, Closure|array|string ...$references): \Framework\Database\Manipulation\Delete
Call a DELETE statement.
Parameters
-
Closure|array|string|null
$reference
-
Closure|array|string
$references
Returns
\Framework\Database\Manipulation\Delete
dropSchema()
public function dropSchema(?string $schemaName = NULL): \Framework\Database\Definition\DropSchema
Call a DROP SCHEMA statement.
Returns
\Framework\Database\Definition\DropSchema
dropTable()
public function dropTable(?string $table = NULL, string ...$tables): \Framework\Database\Definition\DropTable
Call a DROP TABLE statement.
Parameters
-
?string
$table
-
string
$tables
Returns
\Framework\Database\Definition\DropTable
exec()
public function exec(string $statement): string|int
Executes an SQL statement and return the number of affected rows.
Returns
string|int
getConfig()
public function getConfig(): array
Returns
array
getConnection()
public function getConnection(): mysqli
Gets the MySQLi connection.
Returns
mysqli
getError()
public function getError(): ?string
Get latest error.
Returns
?string
getErrors()
public function getErrors(): array
Get a list of the latest errors.
Returns
array
getInsertId()
public function getInsertId(): string|int
Gets the LAST_INSERT_ID().
Note: When an insert has many rows, this function returns the id of the
first row inserted!
That is default on MySQL.
Returns
string|int
getLastQuery()
public function getLastQuery(): string
Returns
string
getWarnings()
public function getWarnings(): mysqli_warning|false
Returns
mysqli_warning|false
getWarningsCount()
public function getWarningsCount(): int
Returns
int
insert()
public function insert(?string $intoTable = NULL): \Framework\Database\Manipulation\Insert
Call a INSERT statement.
Returns
\Framework\Database\Manipulation\Insert
isOpen()
public function isOpen(): bool
Tells if the connection is open.
Returns
bool
loadData()
public function loadData(?string $intoTable = NULL): \Framework\Database\Manipulation\LoadData
Call a LOAD DATA statement.
Returns
\Framework\Database\Manipulation\LoadData
ping()
public function ping(): bool
Pings the server, or tries to reconnect if the connection has gone down.
Returns
bool
prepare()
public function prepare(string $statement): \Framework\Database\PreparedStatement
Prepares a statement for execution and returns a PreparedStatement object.
Returns
\Framework\Database\PreparedStatement
- throws
RuntimeException if prepared statement fail
protectIdentifier()
public function protectIdentifier(string $identifier): string
Protect identifier.
Returns
string
query()
public function query(string $statement, bool $buffered = true): \Framework\Database\Result
Executes an SQL statement, returning a result set as a Result object.
Must be: SELECT, SHOW, DESCRIBE or EXPLAIN
Parameters
-
string
$statement
-
bool
$buffered
Returns
\Framework\Database\Result
- throws
InvalidArgumentException if $statement does not return result
quote()
public function quote(string|int|float|bool|null $value): string|int|float
Quote SQL values.
Parameters
-
string|int|float|bool|null
$value
- Value to be quoted
Returns
string|int|float
- If the value is null, returns a string containing the word "NULL". If is false, "FALSE". If is true, "TRUE". If is a string, returns the quoted string. The types int or float returns the same input value.
- throws
InvalidArgumentException For invalid value type
reconnect()
public function reconnect(): static
Closes the current and opens a new connection with the last config.
Returns
static
replace()
public function replace(?string $intoTable = NULL): \Framework\Database\Manipulation\Replace
Call a REPLACE statement.
Returns
\Framework\Database\Manipulation\Replace
select()
public function select(Closure|array|string|null $reference = NULL, Closure|array|string ...$references): \Framework\Database\Manipulation\Select
Call a SELECT statement.
Parameters
-
Closure|array|string|null
$reference
-
Closure|array|string
$references
Returns
\Framework\Database\Manipulation\Select
setDebugCollector()
public function setDebugCollector(\Framework\Database\Debug\DatabaseCollector $collector): static
Parameters
-
\Framework\Database\Debug\DatabaseCollector
$collector
Returns
static
transaction()
public function transaction(callable $statements): static
Run statements in a transaction.
Returns
static
- throws
Exception if statements fail
- throws
LogicException if transaction already is active
update()
public function update(Closure|array|string|null $reference = NULL, Closure|array|string ...$references): \Framework\Database\Manipulation\Update
Call a UPDATE statement.
Parameters
-
Closure|array|string|null
$reference
-
Closure|array|string
$references
Returns
\Framework\Database\Manipulation\Update
use()
public function use(string $schema): static
Returns
static
- throws
mysqli_sql_exception if schema is unknown
with()
public function with(): \Framework\Database\Manipulation\With
Call a WITH statement.
Returns
\Framework\Database\Manipulation\With
addToDebug()
protected function addToDebug(Closure $function): mixed
Returns
mixed
connect()
protected function connect(array|string $username, ?string $password = NULL, ?string $schema = NULL, string $host = 'localhost', int $port = 3306): static
Parameters
-
array|string
$username
-
?string
$password
-
?string
$schema
-
string
$host
-
int
$port
Returns
static
- throws
mysqli_sql_exception if connection fail
finalizeAddToDebug()
protected function finalizeAddToDebug(float $start, ?string $description = NULL): void
Parameters
-
float
$start
-
?string
$description
Returns
void
log()
protected function log(string $message, \Framework\Log\LogLevel $level = \Framework\Log\LogLevel::ERROR): void
Parameters
-
string
$message
-
\Framework\Log\LogLevel
$level
Returns
void
makeConfig()
protected function makeConfig(array $config): array
Make Base Connection configurations.
Returns
array
setCollations()
protected function setCollations(string $charset, string $collation): bool
Parameters
-
string
$charset
-
string
$collation
Returns
bool
setTimezone()
protected function setTimezone(string $timezone): bool
Returns
bool