PHP 클래스 Basecoat\DB

파일 보기 프로젝트 열기: shutterstock/basecoat

공개 프로퍼티들

프로퍼티 타입 설명
$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