Database
extends Db
in package
Table of Contents
Properties
- $error : string|null
- Error Message.
- $errorInfo : array<string|int, mixed>|null
- The array of error information.
- $options : array<string|int, mixed>
- These are the Medoo Object options to pass along
- $pdo : PDO
- The PDO object.
- $queryString : string
- The last query string was generated in test mode.
- $returnId : string
- The returned id for the insert.
- $type : string
- The type of database.
- $debugLogging : bool
- Determine should save debug logging.
- $debugLogs : array<string|int, mixed>
- The array of logs for debugging.
- $debugMode : bool
- Determine is in debug mode.
- $dsn : string
- The DSN connection string.
- $guid : int
- The unique global id.
- $logging : bool
- Determine should log the query or not.
- $logs : array<string|int, mixed>
- The array of logs.
- $prefix : string
- Table prefix.
- $statement : PDOStatement
- The PDO statement object.
- $testMode : bool
- Determine is in test mode.
Methods
- __construct() : Db
- Connect the database.
- __destruct() : mixed
- action() : void
- Start a transaction.
- avg() : string|null
- Calculate the average value of the column.
- avg() : string
- avg() : string
- beginDebug() : void
- Enable debug logging mode.
- builder() : Database
- columnQuote() : string
- Quote column name for use in a query.
- count() : int|null
- Count the number of rows from the table.
- count() : int
- create() : PDOStatement|null
- Create a table.
- debug() : Db
- Enable debug mode and output readable statement string.
- debugLog() : void
- Disable debug logging and return all readable statements.
- delete() : PDOStatement|null
- Delete data from the table.
- drop() : PDOStatement|null
- Drop a table.
- exec() : PDOStatement|null
- Execute the raw statement.
- get() : mixed
- Get only one record from the table.
- get() : mixed
- has() : bool
- Determine whether the target data existed from the table.
- has() : bool
- id() : string|null
- Return the ID for the last inserted row.
- info() : array<string|int, mixed>
- Get information about the database connection.
- insert() : PDOStatement|null
- Insert one or more records into the table.
- last() : string|null
- Return the last performed statement.
- lastId() : string|null
- Returns the id of the last inserted row
- log() : array<string|int, string>
- Return all executed statements.
- max() : string|null
- Get the maximum value of the column.
- max() : string
- max() : string
- min() : string|null
- Get the minimum value of the column.
- min() : string
- min() : string
- pdo() : PDO
- Returns the underlying pdo object From this, you can do anything you want with the pdo object
- query() : PDOStatement|null
- Execute customized raw statement.
- quote() : string
- Quote a string for use in a query.
- rand() : array<string|int, mixed>
- Randomly fetch data from the table.
- rand() : mixed
- raw() : Db::raw
- Build a raw object.
- replace() : PDOStatement|null
- Replace old data with a new one.
- select() : array<string|int, mixed>|null
- Select data from the table.
- select() : array<string|int, mixed>
- select() : null
- select() : null
- select() : null
- sum() : string|null
- Calculate the total value of the column.
- sum() : string
- sum() : string
- tableQuote() : string
- Quote table name for use in a query.
- update() : PDOStatement|null
- Modify data from the table.
- use() : Database
- This is a static method to use a Database connection. It will return a new Database instance with the connection passed.
- buildJoin() : string
- Build the join statement.
- buildRaw() : string|null
- Generate the actual query from the raw object.
- columnMap() : array<string|int, mixed>
- Mapping columns for the stack.
- columnPush() : string
- Build the statement part for the column stack.
- dataImplode() : string
- Implode the Where conditions.
- dataMap() : void
- Mapping the data from the table.
- generate() : string
- Generate readable statement.
- isJoin() : bool
- Determine the array with join syntax.
- isRaw() : bool
- Finds whether the object is raw.
- mapKey() : string
- Generate a new map key for the placeholder.
- selectContext() : string
- Build statement for the select query.
- typeMap() : array<string|int, mixed>
- Mapping the type name as PDO data type.
- whereClause() : string
- Build the where clause.
- aggregate() : string|null
- Build for the aggregate function.
- configure() : Database
- Resolves the Database connection to use. If the connection is passed, it will use that connection. If the options are passed, it will use those options. If the pdo is passed, it will use that pdo.
- returningQuery() : PDOStatement|null
- Build and execute returning query.
Properties
$error
Error Message.
public
string|null
$error
= null
$errorInfo
The array of error information.
public
array<string|int, mixed>|null
$errorInfo
= null
$options
These are the Medoo Object options to pass along
public
array<string|int, mixed>
$options
= []
$pdo
The PDO object.
public
PDO
$pdo
$queryString
The last query string was generated in test mode.
public
string
$queryString
$returnId
The returned id for the insert.
public
string
$returnId
= ''
$type
The type of database.
public
string
$type
$debugLogging
Determine should save debug logging.
protected
bool
$debugLogging
= false
$debugLogs
The array of logs for debugging.
protected
array<string|int, mixed>
$debugLogs
= []
$debugMode
Determine is in debug mode.
protected
bool
$debugMode
= false
$dsn
The DSN connection string.
protected
string
$dsn
$guid
The unique global id.
protected
int
$guid
= 0
$logging
Determine should log the query or not.
protected
bool
$logging
= false
$logs
The array of logs.
protected
array<string|int, mixed>
$logs
= []
$prefix
Table prefix.
protected
string
$prefix
$statement
The PDO statement object.
protected
PDOStatement
$statement
$testMode
Determine is in test mode.
protected
bool
$testMode
= false
Methods
__construct()
Connect the database.
public
__construct(array<string|int, mixed> $options) : Db
$database = new Db([
// required
'type' => 'mysql',
'database' => 'name',
'host' => 'localhost',
'username' => 'your_username',
'password' => 'your_password',
// [optional]
'charset' => 'utf8mb4',
'port' => 3306,
'prefix' => 'PREFIX_'
]);
Parameters
- $options : array<string|int, mixed>
-
Connection options
Tags
Return values
Db__destruct()
public
__destruct() : mixed
action()
Start a transaction.
public
action(callable $actions) : void
Parameters
- $actions : callable
Tags
avg()
Calculate the average value of the column.
public
avg(string $table, array<string|int, mixed> $join[, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullavg()
public
avg(string $table, string $column) : string
Parameters
- $table : string
- $column : string
Return values
stringavg()
public
avg(string $table, string $column, array<string|int, mixed> $where) : string
Parameters
- $table : string
- $column : string
- $where : array<string|int, mixed>
Return values
stringbeginDebug()
Enable debug logging mode.
public
beginDebug() : void
Tags
builder()
public
static builder([string|null $databaseConnection = null ][, mixed $options = null ][, PDO|null $pdo = null ]) : Database
Parameters
- $databaseConnection : string|null = null
- $options : mixed = null
- $pdo : PDO|null = null
Tags
Return values
DatabasecolumnQuote()
Quote column name for use in a query.
public
columnQuote(string $column) : string
Parameters
- $column : string
Return values
stringcount()
Count the number of rows from the table.
public
count(string $table[, array<string|int, mixed> $join = null ][, string $column = null ][, array<string|int, mixed> $where = null ]) : int|null
Parameters
- $table : string
- $join : array<string|int, mixed> = null
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
int|nullcount()
public
count(string $table, array<string|int, mixed> $where) : int
Parameters
- $table : string
- $where : array<string|int, mixed>
Return values
intcreate()
Create a table.
public
create(string $table, array<string|int, mixed> $columns[, array<string|int, mixed> $options = null ]) : PDOStatement|null
Parameters
- $table : string
- $columns : array<string|int, mixed>
-
Columns definition.
- $options : array<string|int, mixed> = null
-
Additional table options for creating a table.
Return values
PDOStatement|nulldebug()
Enable debug mode and output readable statement string.
public
debug() : Db
Tags
Return values
DbdebugLog()
Disable debug logging and return all readable statements.
public
debugLog() : void
Tags
delete()
Delete data from the table.
public
delete(string $table, array<string|int, mixed>|Raw $where) : PDOStatement|null
Parameters
- $table : string
- $where : array<string|int, mixed>|Raw
Return values
PDOStatement|nulldrop()
Drop a table.
public
drop(string $table) : PDOStatement|null
Parameters
- $table : string
Return values
PDOStatement|nullexec()
Execute the raw statement.
public
exec(string $statement[, array<string|int, mixed> $map = [] ][, callable $callback = null ]) : PDOStatement|null
Parameters
- $statement : string
-
The SQL statement.
- $map : array<string|int, mixed> = []
-
The array of input parameters value for prepared statement.
- $callback : callable = null
Tags
Return values
PDOStatement|nullget()
Get only one record from the table.
public
get(string $table[, array<string|int, mixed> $join = null ][, array<string|int, mixed>|string $columns = null ][, array<string|int, mixed> $where = null ]) : mixed
Parameters
- $table : string
- $join : array<string|int, mixed> = null
- $columns : array<string|int, mixed>|string = null
- $where : array<string|int, mixed> = null
get()
public
get(string $table, array<string|int, mixed>|string $columns, array<string|int, mixed> $where) : mixed
Parameters
- $table : string
- $columns : array<string|int, mixed>|string
- $where : array<string|int, mixed>
has()
Determine whether the target data existed from the table.
public
has(string $table, array<string|int, mixed> $join[, array<string|int, mixed> $where = null ]) : bool
Parameters
- $table : string
- $join : array<string|int, mixed>
- $where : array<string|int, mixed> = null
Return values
boolhas()
public
has(string $table, array<string|int, mixed> $where) : bool
Parameters
- $table : string
- $where : array<string|int, mixed>
Return values
boolid()
Return the ID for the last inserted row.
public
id([string $name = null ]) : string|null
Parameters
- $name : string = null
Tags
Return values
string|nullinfo()
Get information about the database connection.
public
info() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>insert()
Insert one or more records into the table.
public
insert(string $table, array<string|int, mixed> $values[, string $primaryKey = null ]) : PDOStatement|null
Parameters
- $table : string
- $values : array<string|int, mixed>
- $primaryKey : string = null
Return values
PDOStatement|nulllast()
Return the last performed statement.
public
last() : string|null
Tags
Return values
string|nulllastId()
Returns the id of the last inserted row
public
lastId() : string|null
Return values
string|nulllog()
Return all executed statements.
public
log() : array<string|int, string>
Tags
Return values
array<string|int, string>max()
Get the maximum value of the column.
public
max(string $table, array<string|int, mixed> $join[, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullmax()
public
max(string $table, string $column) : string
Parameters
- $table : string
- $column : string
Return values
stringmax()
public
max(string $table, string $column, array<string|int, mixed> $where) : string
Parameters
- $table : string
- $column : string
- $where : array<string|int, mixed>
Return values
stringmin()
Get the minimum value of the column.
public
min(string $table, array<string|int, mixed> $join[, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullmin()
public
min(string $table, string $column) : string
Parameters
- $table : string
- $column : string
Return values
stringmin()
public
min(string $table, string $column, array<string|int, mixed> $where) : string
Parameters
- $table : string
- $column : string
- $where : array<string|int, mixed>
Return values
stringpdo()
Returns the underlying pdo object From this, you can do anything you want with the pdo object
public
pdo() : PDO
Tags
Return values
PDOquery()
Execute customized raw statement.
public
query(string $statement[, array<string|int, mixed> $map = [] ]) : PDOStatement|null
Parameters
- $statement : string
-
The raw SQL statement.
- $map : array<string|int, mixed> = []
-
The array of input parameters value for prepared statement.
Return values
PDOStatement|nullquote()
Quote a string for use in a query.
public
quote(string $string) : string
Parameters
- $string : string
Return values
stringrand()
Randomly fetch data from the table.
public
rand(string $table[, array<string|int, mixed> $join = null ][, array<string|int, mixed>|string $columns = null ][, array<string|int, mixed> $where = null ]) : array<string|int, mixed>
Parameters
- $table : string
- $join : array<string|int, mixed> = null
- $columns : array<string|int, mixed>|string = null
- $where : array<string|int, mixed> = null
Return values
array<string|int, mixed>rand()
public
rand(string $table, array<string|int, mixed>|string $column, array<string|int, mixed> $where) : mixed
Parameters
- $table : string
- $column : array<string|int, mixed>|string
- $where : array<string|int, mixed>
raw()
Build a raw object.
public
static raw(string $string[, array<string|int, mixed> $map = [] ]) : Db::raw
Parameters
- $string : string
-
The raw string.
- $map : array<string|int, mixed> = []
-
The array of mapping data for the raw string.
Return values
Db::rawreplace()
Replace old data with a new one.
public
replace(string $table, array<string|int, mixed> $columns[, array<string|int, mixed> $where = null ]) : PDOStatement|null
Parameters
- $table : string
- $columns : array<string|int, mixed>
- $where : array<string|int, mixed> = null
Return values
PDOStatement|nullselect()
Select data from the table.
public
select(string $table, array<string|int, mixed> $join[, array<string|int, mixed>|string $columns = null ][, array<string|int, mixed> $where = null ]) : array<string|int, mixed>|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $columns : array<string|int, mixed>|string = null
- $where : array<string|int, mixed> = null
Return values
array<string|int, mixed>|nullselect()
public
select(string $table, array<string|int, mixed> $columns, array<string|int, mixed> $where) : array<string|int, mixed>
Parameters
- $table : string
- $columns : array<string|int, mixed>
- $where : array<string|int, mixed>
Return values
array<string|int, mixed>select()
public
select(string $table, array<string|int, mixed> $columns, callable $callback) : null
Parameters
- $table : string
- $columns : array<string|int, mixed>
- $callback : callable
Return values
nullselect()
public
select(string $table, array<string|int, mixed> $columns, array<string|int, mixed> $where, callable $callback) : null
Parameters
- $table : string
- $columns : array<string|int, mixed>
- $where : array<string|int, mixed>
- $callback : callable
Return values
nullselect()
public
select(string $table, array<string|int, mixed> $join, array<string|int, mixed> $columns, array<string|int, mixed> $where, callable $callback) : null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $columns : array<string|int, mixed>
- $where : array<string|int, mixed>
- $callback : callable
Return values
nullsum()
Calculate the total value of the column.
public
sum(string $table, array<string|int, mixed> $join[, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $table : string
- $join : array<string|int, mixed>
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullsum()
public
sum(string $table, string $column) : string
Parameters
- $table : string
- $column : string
Return values
stringsum()
public
sum(string $table, string $column, array<string|int, mixed> $where) : string
Parameters
- $table : string
- $column : string
- $where : array<string|int, mixed>
Return values
stringtableQuote()
Quote table name for use in a query.
public
tableQuote(string $table) : string
Parameters
- $table : string
Return values
stringupdate()
Modify data from the table.
public
update(string $table, array<string|int, mixed> $data[, array<string|int, mixed> $where = null ]) : PDOStatement|null
Parameters
- $table : string
- $data : array<string|int, mixed>
- $where : array<string|int, mixed> = null
Return values
PDOStatement|nulluse()
This is a static method to use a Database connection. It will return a new Database instance with the connection passed.
public
static use([string|null $databaseConnection = 'db' ]) : Database
Parameters
- $databaseConnection : string|null = 'db'
Tags
Return values
DatabasebuildJoin()
Build the join statement.
protected
buildJoin(string $table, array<string|int, mixed> $join, array<string|int, mixed> &$map) : string
Parameters
- $table : string
- $join : array<string|int, mixed>
- $map : array<string|int, mixed>
Return values
stringbuildRaw()
Generate the actual query from the raw object.
protected
buildRaw(mixed $raw, array<string|int, mixed> &$map) : string|null
Parameters
- $raw : mixed
- $map : array<string|int, mixed>
Return values
string|nullcolumnMap()
Mapping columns for the stack.
protected
columnMap(array<string|int, mixed>|string $columns, array<string|int, mixed> &$stack, bool $root) : array<string|int, mixed>
Parameters
- $columns : array<string|int, mixed>|string
- $stack : array<string|int, mixed>
- $root : bool
Return values
array<string|int, mixed>columnPush()
Build the statement part for the column stack.
protected
columnPush(array<string|int, mixed>|string &$columns, array<string|int, mixed> &$map, bool $root[, bool $isJoin = false ]) : string
Parameters
- $columns : array<string|int, mixed>|string
- $map : array<string|int, mixed>
- $root : bool
- $isJoin : bool = false
Return values
stringdataImplode()
Implode the Where conditions.
protected
dataImplode(array<string|int, mixed> $data, array<string|int, mixed> &$map, string $conjunctor) : string
Parameters
- $data : array<string|int, mixed>
- $map : array<string|int, mixed>
- $conjunctor : string
Return values
stringdataMap()
Mapping the data from the table.
protected
dataMap(array<string|int, mixed> $data, array<string|int, mixed> $columns, array<string|int, mixed> $columnMap, array<string|int, mixed> &$stack, bool $root[, array<string|int, mixed> &$result = null ]) : void
Parameters
- $data : array<string|int, mixed>
- $columns : array<string|int, mixed>
- $columnMap : array<string|int, mixed>
- $stack : array<string|int, mixed>
- $root : bool
- $result : array<string|int, mixed> = null
Tags
generate()
Generate readable statement.
protected
generate(string $statement, array<string|int, mixed> $map) : string
Parameters
- $statement : string
- $map : array<string|int, mixed>
Tags
Return values
stringisJoin()
Determine the array with join syntax.
protected
isJoin(mixed $join) : bool
Parameters
- $join : mixed
Return values
boolisRaw()
Finds whether the object is raw.
protected
isRaw(object $object) : bool
Parameters
- $object : object
Return values
boolmapKey()
Generate a new map key for the placeholder.
protected
mapKey() : string
Return values
stringselectContext()
Build statement for the select query.
protected
selectContext(string $table, array<string|int, mixed> &$map, array<string|int, mixed>|string $join[, array<string|int, mixed>|string &$columns = null ][, array<string|int, mixed> $where = null ][, string $columnFn = null ]) : string
Parameters
- $table : string
- $map : array<string|int, mixed>
- $join : array<string|int, mixed>|string
- $columns : array<string|int, mixed>|string = null
- $where : array<string|int, mixed> = null
- $columnFn : string = null
Return values
stringtypeMap()
Mapping the type name as PDO data type.
protected
typeMap(mixed $value, string $type) : array<string|int, mixed>
Parameters
- $value : mixed
- $type : string
Return values
array<string|int, mixed>whereClause()
Build the where clause.
protected
whereClause(array<string|int, mixed>|null $where, array<string|int, mixed> &$map) : string
Parameters
- $where : array<string|int, mixed>|null
- $map : array<string|int, mixed>
Return values
stringaggregate()
Build for the aggregate function.
private
aggregate(string $type, string $table[, array<string|int, mixed> $join = null ][, string $column = null ][, array<string|int, mixed> $where = null ]) : string|null
Parameters
- $type : string
- $table : string
- $join : array<string|int, mixed> = null
- $column : string = null
- $where : array<string|int, mixed> = null
Return values
string|nullconfigure()
Resolves the Database connection to use. If the connection is passed, it will use that connection. If the options are passed, it will use those options. If the pdo is passed, it will use that pdo.
private
static configure([mixed $connection = null ][, mixed $options = null ][, mixed $pdo = null ]) : Database
If all are passed, it will use the pdo connection and ignore the rest. If none are passed, it will use the default connection which is 'db'.
If extra options are passed, they will take precedence over the settings.ini file.
Parameters
- $connection : mixed = null
- $options : mixed = null
- $pdo : mixed = null
Tags
Return values
DatabasereturningQuery()
Build and execute returning query.
private
returningQuery(string $query, array<string|int, mixed> &$map, array<string|int, mixed> &$data) : PDOStatement|null
Parameters
- $query : string
- $map : array<string|int, mixed>
- $data : array<string|int, mixed>