PHP Класс Basecoat\DB

Показать файл Открыть проект

Открытые свойства

Свойство Тип Описание
$bulkInsertSize integer Large inserts are broken up into multiple insert of this size
$debug integer Debugging turned on(1) or off(0)
$errorCntr integer Number of errors that have been generated
$errorCode string The last error code that was generated
$errorMsg string The last error message that was generated
$errors array List of errors that have been generated
$insertId integer Last insert ID
$instanceCntr integer Number of instances that have been created
$lastQuery string The last query that was run
$maxProfiling integer Maximum number of items to retain in profiling list
$selectResult array Default storage variable for select result if no variable is provided
$useErrorLog integer Also log errors via error_log

Открытые методы

Метод Описание
__construct ( mixed $host, string $username = null, string $password = null, string $db = null, $label = 'n/a' ) Initialize an instance of the database class.
__destruct ( ) Actions to perform when class is destructed
clearResult ( ) Clear $selectResult variable
connect ( $useMaster = false, $reconnect = false ) : mixed Establish a connection to the database. Note: Connections are "on demand", you shouldn't need to call this function yourself Called automatically by execQuery if no connection.
delete ( $tablename, $filter, $filterBindings = null, boolean $useMaster = true, $modifiers = '' ) : integer Delete records from the database
disconnect ( boolean $useMaster = false ) Disconnect from the database
execute ( $bindings = [], boolean $useMaster = false ) : integer Execute a prepare statement to data binding values. Can be called repeatedly for the same prepared query, with different data bindings.
fetch ( boolean $useMaster = false, constant $method = null ) : array Fetch one row of data from the database after SELECT query is run
fetchAll ( variable &$resultVar, string $keyField = null, boolean $grouped = false, boolean $useMaster = false, constant $method = null ) : integer Fetch all rows from the database after SELECT query is run Rows can be fetched with a field as the index instead of sequential index By specifying a $keyField and $grouped, record will be returned grouped by the key field
fetchField ( string $fieldName, boolean $useMaster = false ) : array Fetch a single field from a select query. Use for when you just want a list of values, like IDs, for processing.
foundRows ( boolean $useMaster = false ) : integer Retrieve total rows found for last SELECT SQL_CALC_FOUND_ROWS query with LIMIT clause
getLastInsertId ( boolean $useMaster = true ) : integer Get the last insert if for prepare/execute statements
getProfiling ( ) : array Retrieve profiling information
getResult ( variable &$resultVar ) : array Retrieve the contents of the $selectResult class variable and clear it
getServerInstance ( mixed $serverKey, boolean $new = false ) Return an instance of the class connected to the specified server If any instance of the request server already exist, that instance will be returned
insert ( string $tablename, array &$data, string $modifiers = '', boolean $useMaster = true, string $action = 'INSERT' ) : integer Insert record(s) in database. The last insert ID is stored in the $insertId class variable
prepare ( string $query, boolean $useMaster = false, array $attr = null ) Prepare a query for execution by the execPrepared function
rawQuery ( string $query, boolean $useMaster = false ) : integer Execute query. Can be called directly for complex query.
resetProfiling ( ) Resets the profiling array
select ( string $query, array $bindings = null, boolean $useMaster = false, boolean $fetchAll = false ) : integer Run a SELECT query with optional data binding values
selectAll ( string $query, array $bindings = null, boolean $useMaster = false ) : mixed Run a SELECT query with optional data binding values, and retrieves ALL rows This will use a separate statement handle so other outstanding queries do not get overwritten
selectOne ( string $query, array $bindings = null, boolean $useMaster = false ) : mixed Run a SELECT query with optional data binding values, and retrieve only 1 row
setConnectParams ( mixed $host, string $username = null, string $password = null, string $db = null, string $label = 'n/a', array $attr = null ) Set database connection parameters. This does not establish a connection, only sets the parameters for a connection.
setConnectParamsMaster ( mixed $connectParams ) Set MASTER database connection parameters. This does not establish a connection, only sets the parameters for a connection.
setConnectTimeout ( integer $value, boolean $useMaster = false ) Set how long a db connection will be left open before disconnecting
setServerConfig ( array $servers, mixed $masterKey = null ) Set Server Connection Parameters The server list can have all the fields listed in $connectDefaults. At a minimum, it should have a lablel
update ( string $tablename, array $data, string $filter, string $filterBindings = null, boolean $useMaster = true ) : integer Update record(s) in database

Приватные методы

Метод Описание
connectMaster ( $reconnect = false ) : mixed Establish a connection to the Master server
explainQuery ( string $query, array $bindings = null, object &$dbh ) Run and EXPLAIN on a query for debugging puroposes.
logErrorInfo ( object $handle, $label = '' ) Log an error that occurred on a handle (database or statement)
updateProfiling ( string $query, array $bindings, float $qTime, string $result, string $label, mixed $debug = null ) Update profiling array with query information

Описание методов

__construct() публичный Метод

Connections are not made at this time, only the parameters are set.
public __construct ( mixed $host, string $username = null, string $password = null, string $db = null, $label = 'n/a' )
$host mixed string (hostname) or associative array with parameter to use
$username string user name to use for login
$password string password to use for login
$db string optional database to select

__destruct() публичный Метод

Actions to perform when class is destructed
public __destruct ( )

clearResult() публичный Метод

Clear $selectResult variable
public clearResult ( )

connect() публичный Метод

Connection parameters must have already been set with setConnectParams Use $persistantConnections variable to toggle persistant connections (DON'T USE PERSISTANT CONNECTIONS UNLESS YOU KNOW WHY YOU SHOULDN'T)
public connect ( $useMaster = false, $reconnect = false ) : mixed
Результат mixed returns 1 on success or -1 on failure

delete() публичный Метод

Delete records from the database
public delete ( $tablename, $filter, $filterBindings = null, boolean $useMaster = true, $modifiers = '' ) : integer
$useMaster boolean set to TRUE to use the master server connection
Результат integer number of rows deleted

disconnect() публичный Метод

Disconnect from the database
public disconnect ( boolean $useMaster = false )
$useMaster boolean disconnect from master/slave

execute() публичный Метод

Execute a prepare statement to data binding values. Can be called repeatedly for the same prepared query, with different data bindings.
public execute ( $bindings = [], boolean $useMaster = false ) : integer
$useMaster boolean set to TRUE to use the master server connection
Результат integer returns number of rows that were affected, or -1 if query failed

fetch() публичный Метод

Fetch one row of data from the database after SELECT query is run
public fetch ( boolean $useMaster = false, constant $method = null ) : array
$useMaster boolean set to TRUE to use the master server connection
$method constant PDO method to use for fetch records (default is PDO::FETCH_ASSOC)
Результат array associative array of database record

fetchAll() публичный Метод

Fetch all rows from the database after SELECT query is run Rows can be fetched with a field as the index instead of sequential index By specifying a $keyField and $grouped, record will be returned grouped by the key field
public fetchAll ( variable &$resultVar, string $keyField = null, boolean $grouped = false, boolean $useMaster = false, constant $method = null ) : integer
$resultVar variable variable reference to store the result in
$keyField string field to use as the array key
$grouped boolean if $keyField specified, group by the key field
$useMaster boolean set to TRUE to use the master server connection
$method constant PDO method to use for fetch records (default is PDO::FETCH_ASSOC)
Результат integer 1 if successful, -1 if error. Fetched data is stored in $selectResult class variable by default

fetchField() публичный Метод

Fetch a single field from a select query. Use for when you just want a list of values, like IDs, for processing.
public fetchField ( string $fieldName, boolean $useMaster = false ) : array
$fieldName string field name to get
$useMaster boolean set to TRUE to use the master server connection
Результат array list of values fetched

foundRows() публичный Метод

Retrieve total rows found for last SELECT SQL_CALC_FOUND_ROWS query with LIMIT clause
public foundRows ( boolean $useMaster = false ) : integer
$useMaster boolean set to TRUE to use the master server connection
Результат integer number of total rows found

getLastInsertId() публичный Метод

Get the last insert if for prepare/execute statements
public getLastInsertId ( boolean $useMaster = true ) : integer
$useMaster boolean set to TRUE to use the master server connection
Результат integer lsat insert id

getProfiling() публичный Метод

Retrieve profiling information
public getProfiling ( ) : array
Результат array

getResult() публичный Метод

Retrieve the contents of the $selectResult class variable and clear it
public getResult ( variable &$resultVar ) : array
$resultVar variable variable reference to store the result in
Результат array contents of $selectResult variable

getServerInstance() публичный статический Метод

Return an instance of the class connected to the specified server If any instance of the request server already exist, that instance will be returned
public static getServerInstance ( mixed $serverKey, boolean $new = false )
$serverKey mixed the $servers key to use for connecting
$new boolean force the creation of a new instance

insert() публичный Метод

Insert record(s) in database. The last insert ID is stored in the $insertId class variable
public insert ( string $tablename, array &$data, string $modifiers = '', boolean $useMaster = true, string $action = 'INSERT' ) : integer
$tablename string name of table to insert into
$data array associative array of field/value pairs to insert, can be multi-dimensional for bulk inserts. All fields must be the same for each record for bulk inserts
$modifiers string modifier to use in query (i.e. IGNORE). Default is no modifier
$useMaster boolean set to TRUE to use the master server connection
$action string type of insert to perform (INSERT, REPLACE). Default is INSERT
Результат integer number of rows that were inserted

prepare() публичный Метод

Prepare a query for execution by the execPrepared function
public prepare ( string $query, boolean $useMaster = false, array $attr = null )
$query string the query string, may contain data binding placeholder for use by execPrepared
$useMaster boolean set to TRUE to use the master server connection
$attr array custom PDO attributes to set $return int whether statement was successfully prepared (1), or not (-1)

rawQuery() публичный Метод

Used by various other functions (i.e. select, update, insert) to run a query/
public rawQuery ( string $query, boolean $useMaster = false ) : integer
$query string SQL query to run
$useMaster boolean set to TRUE to use the master server connection
Результат integer result of running the query (not data)

resetProfiling() публичный Метод

Resets the profiling array
public resetProfiling ( )

select() публичный Метод

Run a SELECT query with optional data binding values
public select ( string $query, array $bindings = null, boolean $useMaster = false, boolean $fetchAll = false ) : integer
$query string SQL Select query to run
$bindings array array of values to bind to the query. Can be an associative array to bind by name
$useMaster boolean set to TRUE to use the master server connection
$fetchAll boolean 1 to fetch data into an associated array ($this->selectResult), 0 to just run the query (use on of the fetch/fetchAll functions to retrieve data)
Результат integer returns number of rows selected. Use SQL_CALC_FOUND_ROWS with qFoundRows if using LIMIT, to get total rows found.

selectAll() публичный Метод

Run a SELECT query with optional data binding values, and retrieves ALL rows This will use a separate statement handle so other outstanding queries do not get overwritten
public selectAll ( string $query, array $bindings = null, boolean $useMaster = false ) : mixed
$query string SQL Select query to run
$bindings array array of values to bind to the query. Can be an associative array to bind by name
$useMaster boolean set to TRUE to use the master server connection
Результат mixed returns associative array of data on success, negative int on failure

selectOne() публичный Метод

Run a SELECT query with optional data binding values, and retrieve only 1 row
public selectOne ( string $query, array $bindings = null, boolean $useMaster = false ) : mixed
$query string SQL Select query to run
$bindings array array of values to bind to the query. Can be an associative array to bind by name
$useMaster boolean set to TRUE to use the master server connection
Результат mixed returns associative array of data on success, negative int on failure

setConnectParams() публичный Метод

Set database connection parameters. This does not establish a connection, only sets the parameters for a connection.
public setConnectParams ( mixed $host, string $username = null, string $password = null, string $db = null, string $label = 'n/a', array $attr = null )
$host mixed string (hostname) or associative array with parameter to use
$username string user name to use for login
$password string password to use for login
$db string optional database to select
$label string a "name" reference for the connection, used for profiling
$attr array connection attribute list (i.e. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")

setConnectParamsMaster() публичный статический Метод

Set MASTER database connection parameters. This does not establish a connection, only sets the parameters for a connection.
public static setConnectParamsMaster ( mixed $connectParams )
$connectParams mixed associative array with parameters to use, see setConnectParams function

setConnectTimeout() публичный Метод

Set how long a db connection will be left open before disconnecting
public setConnectTimeout ( integer $value, boolean $useMaster = false )
$value integer seconds before timeout
$useMaster boolean set timeout value on master/slave connection

setServerConfig() публичный статический Метод

Set Server Connection Parameters The server list can have all the fields listed in $connectDefaults. At a minimum, it should have a lablel
public static setServerConfig ( array $servers, mixed $masterKey = null )
$servers array list of servers to connect to. The array key should be the "id" of the server.
$masterKey mixed the array key in $servers that acts as the Master server

update() публичный Метод

Update record(s) in database
public update ( string $tablename, array $data, string $filter, string $filterBindings = null, boolean $useMaster = true ) : integer
$tablename string name of table to update
$data array associative array of field/value pairs to update. Note only raw data is supported, not formulas
$filter string SQL filter to apply to update, should be properly escaped
$filterBindings string associative array name/value pairs to use for binding. CAUTION: may override $data bindings if same names are used
$useMaster boolean set to TRUE to use the master server connection
Результат integer number of rows updated, or negative if an error occurred

Описание свойств

$bulkInsertSize публичное свойство

Large inserts are broken up into multiple insert of this size
public int $bulkInsertSize
Результат integer

$debug публичное свойство

Debugging turned on(1) or off(0)
public int $debug
Результат integer

$errorCntr публичное статическое свойство

Number of errors that have been generated
public static int $errorCntr
Результат integer

$errorCode публичное свойство

The last error code that was generated
public string $errorCode
Результат string

$errorMsg публичное свойство

The last error message that was generated
public string $errorMsg
Результат string

$errors публичное статическое свойство

List of errors that have been generated
public static array $errors
Результат array

$insertId публичное свойство

Last insert ID
public int $insertId
Результат integer

$instanceCntr публичное статическое свойство

Number of instances that have been created
public static int $instanceCntr
Результат integer

$lastQuery публичное свойство

The last query that was run
public string $lastQuery
Результат string

$maxProfiling публичное статическое свойство

Maximum number of items to retain in profiling list
public static int $maxProfiling
Результат integer

$selectResult публичное свойство

Default storage variable for select result if no variable is provided
public array $selectResult
Результат array

$useErrorLog публичное свойство

Also log errors via error_log
public int $useErrorLog
Результат integer