MysqlCommonConnection extends Connection
Class Connection
Base abstract class for database connections.
Table of Contents
Constants
- DEFERRED = 2
- INDEX_FULLTEXT = 'FULLTEXT'
- INDEX_SPATIAL = 'SPATIAL'
- INDEX_UNIQUE = 'UNIQUE'
- PERSISTENT = 1
Methods
- __construct() : mixed
- $configuration may contain following keys: <ul> <li>host <li>database <li>login <li>password <li>initCommand <li>options </ul>
- add() : int
- Adds row to table and returns ID of the added row.
- addMulti() : int
- clearCaches() : void
- Clears all internal caches which may be used by some dictionary functions.
- commitTransaction() : void
- Commits started database transaction.
- connect() : void
- Connects to the database.
- createIndex() : Result|false
- Creates index on column(s)
- createPrimaryIndex() : Result
- Creates primary index on column(s)
- createQueryException() : mixed
- Creates an exception by the error code.
- createTable() : void
- disableQueryExecuting() : void
- Temporary disables query executing. All queries being collected in disabledQueryExecutingDump
- disconnect() : void
- Disconnects from the database.
- dropColumn() : void
- Drops a column. This column must exist and must be not the part of primary constraint.
- dropTable() : void
- Drops the table.
- enableQueryExecuting() : void
- Enables query executing after it has been temporary disabled
- executeSqlBatch() : array<string|int, mixed>
- Parses the string containing multiple queries and executes the queries one by one.
- getAffectedRowsCount() : int
- Returns affected rows count from last executed query.
- getConfiguration() : array<string|int, mixed>
- Returns the array with the connection parameters.
- getDatabase() : string
- Returns database name.
- getDbName() : string
- getDisabledQueryExecutingDump() : null|array<string|int, string>
- Returns queries that were collected while Query Executing was disabled and clears the dump.
- getErrorCode() : int|string
- Returns the error code of the last failed database operation.
- getErrorMessage() : string
- Returns error message of last failed database operation.
- getHost() : string
- Returns database host.
- getIndexName() : string|null
- Returns the name of an index.
- getInsertedId() : int
- getLogin() : string
- Returns database login.
- getMaxAllowedPacket() : int
- Returns max packet length to send to or receive from the database server.
- getNodeId() : string|null
- Returns connection node identifier.
- getPassword() : string
- Returns database password.
- getResource() : resource
- Returns the resource of the connection.
- getSqlHelper() : MysqliSqlHelper|PgsqlSqlHelper
- Returns database-depended SqlHelper object.
- getTableField() : ScalarField|null
- Returns an object for the single column according to the column type.
- getTableFields() : array<string|int, ScalarField>
- Returns fields objects according to the columns of a table.
- getTracker() : null|SqlTracker
- Returns an object with information about queries executed.
- getType() : string
- Returns database type.
- getVersion() : array<string|int, mixed>
- Returns connected database version.
- isConnected() : bool
- Returns the state of the connection.
- isDeferred() : bool
- Returns true if the connection is deferred.
- isIndexExists() : bool
- Checks if an index exists.
- isPersistent() : bool
- Returns true if the connection is persistent.
- isQueryExecutingEnabled() : bool
- isTableExists() : bool
- Checks if a table exists.
- isUtf8mb4() : bool
- Returns utfmb4 flag for the specific table/column.
- lock() : bool
- Sets a global named lock. Currently only Mysql is supported.
- parseSqlBatch() : array<string|int, mixed>
- Parses the text containing sqls into separate queries.
- query() : Result
- Executes a query to the database.
- queryExecute() : void
- Executes a query without returning result, i.e. INSERT, UPDATE, DELETE
- queryScalar() : string|null
- Executes a query, fetches a row and returns single field value from the first column of the result.
- renameTable() : void
- Renames the table. Renamed table must exist and new name must not be occupied by any database object.
- rollbackTransaction() : void
- Rollbacks started database transaction.
- selectDatabase() : bool
- Selects the default database for database queries.
- setNodeId() : void
- Sets connection node identifier.
- setStorageEngine() : void
- Sets default storage engine for all consequent CREATE TABLE statements and all other relevant DDL.
- setTracker() : void
- Sets new sql tracker.
- startTracker() : SqlTracker
- Starts collecting information about all queries executed.
- startTransaction() : void
- Starts new database transaction.
- stopTracker() : void
- Stops collecting information about all queries executed.
- truncateTable() : Result
- Truncates all table data.
- unlock() : bool
- Releases a global named lock. Currently only Mysql is supported.
Constants
DEFERRED
public
mixed
DEFERRED
= 2
INDEX_FULLTEXT
public
mixed
INDEX_FULLTEXT
= 'FULLTEXT'
INDEX_SPATIAL
public
mixed
INDEX_SPATIAL
= 'SPATIAL'
INDEX_UNIQUE
public
mixed
INDEX_UNIQUE
= 'UNIQUE'
PERSISTENT
public
mixed
PERSISTENT
= 1
Methods
__construct()
$configuration may contain following keys: <ul> <li>host <li>database <li>login <li>password <li>initCommand <li>options </ul>
public
__construct(array<string|int, mixed> $configuration) : mixed
Parameters
- $configuration : array<string|int, mixed>
-
Array of Name => Value pairs.
Tags
add()
Adds row to table and returns ID of the added row.
public
add(string $tableName, array<string|int, mixed> $data[, string $identity = "ID" ]) : int
$identity parameter must be null when table does not have autoincrement column.
Parameters
- $tableName : string
-
Name of the table for insertion of new row.
- $data : array<string|int, mixed>
-
Array of columnName => Value pairs.
- $identity : string = "ID"
-
For Oracle only.
Tags
Return values
intaddMulti()
public
addMulti(string $tableName, array<string|int, mixed> $rows[, string $identity = "ID" ]) : int
Parameters
- $tableName : string
- $rows : array<string|int, mixed>
- $identity : string = "ID"
Tags
Return values
intclearCaches()
Clears all internal caches which may be used by some dictionary functions.
public
clearCaches() : void
commitTransaction()
Commits started database transaction.
public
commitTransaction() : void
Tags
connect()
Connects to the database.
public
connect() : void
createIndex()
Creates index on column(s)
public
createIndex(mixed $tableName, mixed $indexName, mixed $columnNames[, mixed $columnLengths = null ][, mixed $indexType = null ]) : Result|false
Parameters
- $tableName : mixed
-
Name of the table.
- $indexName : mixed
-
Name of the new index.
- $columnNames : mixed
-
Name of the column or array of column names to be included into the index.
- $columnLengths : mixed = null
- $indexType : mixed = null
Tags
Return values
Result|falsecreatePrimaryIndex()
Creates primary index on column(s)
public
createPrimaryIndex(string $tableName, string|array<string|int, string> $columnNames) : Result
Parameters
- $tableName : string
-
Name of the table.
- $columnNames : string|array<string|int, string>
-
Name of the column or array of column names to be included into the index.
Tags
Return values
ResultcreateQueryException()
Creates an exception by the error code.
public
createQueryException([mixed $code = 0 ][, mixed $databaseMessage = '' ][, mixed $query = '' ]) : mixed
Parameters
- $code : mixed = 0
- $databaseMessage : mixed = ''
- $query : mixed = ''
Tags
createTable()
public
createTable(mixed $tableName, mixed $fields[, mixed $primary = [] ][, mixed $autoincrement = [] ]) : void
Parameters
- $tableName : mixed
-
Name of the new table.
- $fields : mixed
-
Array with columns descriptions.
- $primary : mixed = []
-
Array with primary key column names.
- $autoincrement : mixed = []
-
Which columns will be auto incremented ones.
Tags
disableQueryExecuting()
Temporary disables query executing. All queries being collected in disabledQueryExecutingDump
public
disableQueryExecuting() : void
Tags
disconnect()
Disconnects from the database.
public
disconnect() : void
dropColumn()
Drops a column. This column must exist and must be not the part of primary constraint.
public
dropColumn(string $tableName, string $columnName) : void
and must be not the last one in the table.
Parameters
- $tableName : string
-
Name of the table to which column will be dropped.
- $columnName : string
-
Name of the column to be dropped.
Tags
dropTable()
Drops the table.
public
dropTable(mixed $tableName) : void
Parameters
- $tableName : mixed
-
Name of the table to be dropped.
Tags
enableQueryExecuting()
Enables query executing after it has been temporary disabled
public
enableQueryExecuting() : void
Tags
executeSqlBatch()
Parses the string containing multiple queries and executes the queries one by one.
public
executeSqlBatch(string $sqlBatch[, bool $stopOnError = false ]) : array<string|int, mixed>
Queries delimiter depends on database type.
Parameters
- $sqlBatch : string
-
String with queries, separated by database-specific delimiters.
- $stopOnError : bool = false
-
Whether return after the first error.
Tags
Return values
array<string|int, mixed> —Array of errors or empty array on success.
getAffectedRowsCount()
Returns affected rows count from last executed query.
public
abstract getAffectedRowsCount() : int
Return values
intgetConfiguration()
Returns the array with the connection parameters.
public
getConfiguration() : array<string|int, mixed>
Return values
array<string|int, mixed>getDatabase()
Returns database name.
public
getDatabase() : string
Return values
stringgetDbName()
public
getDbName() : string
Use getDatabase()
Return values
stringgetDisabledQueryExecutingDump()
Returns queries that were collected while Query Executing was disabled and clears the dump.
public
getDisabledQueryExecutingDump() : null|array<string|int, string>
Tags
Return values
null|array<string|int, string>getErrorCode()
Returns the error code of the last failed database operation.
public
getErrorCode() : int|string
Return values
int|stringgetErrorMessage()
Returns error message of last failed database operation.
public
abstract getErrorMessage() : string
Return values
stringgetHost()
Returns database host.
public
getHost() : string
Return values
stringgetIndexName()
Returns the name of an index.
public
getIndexName(mixed $tableName, array<string|int, mixed> $columns[, mixed $strict = false ]) : string|null
Parameters
- $tableName : mixed
-
A table name.
- $columns : array<string|int, mixed>
-
An array of columns in the index.
- $strict : mixed = false
-
The flag indicating that the columns in the index must exactly match the columns in the $arColumns parameter.
Tags
Return values
string|null —Name of the index or null if the index doesn't exist.
getInsertedId()
public
abstract getInsertedId() : int
Return values
intgetLogin()
Returns database login.
public
getLogin() : string
Return values
stringgetMaxAllowedPacket()
Returns max packet length to send to or receive from the database server.
public
getMaxAllowedPacket() : int
Return values
intgetNodeId()
Returns connection node identifier.
public
getNodeId() : string|null
Return values
string|nullgetPassword()
Returns database password.
public
getPassword() : string
Return values
stringgetResource()
Returns the resource of the connection.
public
getResource() : resource
Return values
resourcegetSqlHelper()
Returns database-depended SqlHelper object.
public
getSqlHelper() : MysqliSqlHelper|PgsqlSqlHelper
Creates new one on the first call per Connection object instance.
Return values
MysqliSqlHelper|PgsqlSqlHelpergetTableField()
Returns an object for the single column according to the column type.
public
getTableField(string $tableName, string $columnName) : ScalarField|null
Parameters
- $tableName : string
-
Name of the table.
- $columnName : string
-
Name of the column.
Tags
Return values
ScalarField|nullgetTableFields()
Returns fields objects according to the columns of a table.
public
getTableFields(mixed $tableName) : array<string|int, ScalarField>
Parameters
- $tableName : mixed
-
The table name.
Tags
Return values
array<string|int, ScalarField> —An array of objects with columns information.
getTracker()
Returns an object with information about queries executed.
public
getTracker() : null|SqlTracker
or null if no tracking was started.
Return values
null|SqlTrackergetType()
Returns database type.
public
getType() : string
Tags
Return values
stringgetVersion()
Returns connected database version.
public
abstract getVersion() : array<string|int, mixed>
Version presented in array of two elements.
- First (with index 0) is database version.
- Second (with index 1) is true when light/express version of database is used.
Tags
Return values
array<string|int, mixed>isConnected()
Returns the state of the connection.
public
isConnected() : bool
Return values
boolisDeferred()
Returns true if the connection is deferred.
public
isDeferred() : bool
Return values
boolisIndexExists()
Checks if an index exists.
public
isIndexExists(mixed $tableName, array<string|int, mixed> $columns) : bool
Parameters
- $tableName : mixed
-
A table name.
- $columns : array<string|int, mixed>
-
An array of columns in the index.
Tags
Return values
boolisPersistent()
Returns true if the connection is persistent.
public
isPersistent() : bool
Return values
boolisQueryExecutingEnabled()
public
isQueryExecutingEnabled() : bool
Tags
Return values
boolisTableExists()
Checks if a table exists.
public
isTableExists(mixed $tableName) : bool
Parameters
- $tableName : mixed
-
The table name.
Tags
Return values
boolisUtf8mb4()
Returns utfmb4 flag for the specific table/column.
public
isUtf8mb4([string|null $table = null ][, string|null $column = null ]) : bool
Parameters
- $table : string|null = null
- $column : string|null = null
Return values
boollock()
Sets a global named lock. Currently only Mysql is supported.
public
lock(mixed $name[, mixed $timeout = 0 ]) : bool
Parameters
- $name : mixed
-
The lock name.
- $timeout : mixed = 0
Tags
Return values
boolparseSqlBatch()
Parses the text containing sqls into separate queries.
public
parseSqlBatch(string $sqlBatch) : array<string|int, mixed>
Parameters
- $sqlBatch : string
Return values
array<string|int, mixed>query()
Executes a query to the database.
public
query(string $sql) : Result
- query($sql)
- query($sql, $limit)
- query($sql, $offset, $limit)
- query($sql, $binds)
- query($sql, $binds, $limit)
- query($sql, $binds, $offset, $limit)
Parameters
- $sql : string
-
Sql query.
Tags
Return values
ResultqueryExecute()
Executes a query without returning result, i.e. INSERT, UPDATE, DELETE
public
queryExecute(string $sql[, array<string|int, mixed>|null $binds = null ]) : void
Parameters
- $sql : string
-
Sql text.
- $binds : array<string|int, mixed>|null = null
-
Binding array.
Tags
queryScalar()
Executes a query, fetches a row and returns single field value from the first column of the result.
public
queryScalar(string $sql[, array<string|int, mixed>|null $binds = null ]) : string|null
Parameters
- $sql : string
-
Sql text.
- $binds : array<string|int, mixed>|null = null
-
Binding array.
Tags
Return values
string|nullrenameTable()
Renames the table. Renamed table must exist and new name must not be occupied by any database object.
public
renameTable(mixed $currentName, mixed $newName) : void
Parameters
- $currentName : mixed
-
Old name of the table.
- $newName : mixed
-
New name of the table.
Tags
rollbackTransaction()
Rollbacks started database transaction.
public
rollbackTransaction() : void
Tags
selectDatabase()
Selects the default database for database queries.
public
abstract selectDatabase(string $database) : bool
Parameters
- $database : string
-
Database name.
Return values
boolsetNodeId()
Sets connection node identifier.
public
setNodeId(string $nodeId) : void
Parameters
- $nodeId : string
-
Node identifier.
setStorageEngine()
Sets default storage engine for all consequent CREATE TABLE statements and all other relevant DDL.
public
setStorageEngine() : void
Storage engine read from .settings.php file. It is 'engine' key of the 'default' from the 'connections'.
setTracker()
Sets new sql tracker.
public
setTracker([null|SqlTracker $sqlTracker = null ]) : void
Parameters
- $sqlTracker : null|SqlTracker = null
-
New tracker.
startTracker()
Starts collecting information about all queries executed.
public
startTracker([bool $reset = false ]) : SqlTracker
Parameters
- $reset : bool = false
-
Clears all previously collected information when set to true.
Return values
SqlTrackerstartTransaction()
Starts new database transaction.
public
startTransaction() : void
Tags
stopTracker()
Stops collecting information about all queries executed.
public
stopTracker() : void
truncateTable()
Truncates all table data.
public
truncateTable(string $tableName) : Result
Parameters
- $tableName : string
-
Name of the table.
Return values
Resultunlock()
Releases a global named lock. Currently only Mysql is supported.
public
unlock(mixed $name) : bool
Parameters
- $name : mixed
-
The lock name.