PHP Class ParagonIE\EasyDB\EasyDB

Show file Open project: paragonie/easydb Class Usage Examples

Protected Properties

Property Type Description
$allowSeparators boolean
$dbEngine string
$pdo PDO

Public Methods

Method Description
__construct ( PDO $pdo, string $dbEngine = '' ) Dependency-Injectable constructor
beginTransaction ( ) : boolean Initiates a transaction
cell ( string $statement, $params ) : mixed Variadic version of $this->single()
col ( string $statement, integer $offset, $params ) : mixed Variadic version of $this->column()
column ( string $statement, array $params = [], integer $offset ) : mixed Fetch a column
commit ( ) : boolean Commits a transaction
delete ( string $table, array $conditions ) : integer Delete rows in a database table.
errorCode ( ) : mixed Fetch the SQLSTATE associated with the last operation on the database handle
errorInfo ( ) : array Fetch extended error information associated with the last operation on the database handle
escapeIdentifier ( string $string, boolean $quote = true ) : string Make sure only valid characters make it in column/table names
escapeLikeValue ( string $value ) : string Escape a value that will be used as a LIKE condition.
escapeValueSet ( array $values, string $type = 'string' ) : string Create a parenthetical statement e.g. for NOT IN queries.
exec ( $args ) : integer Execute an SQL statement and return the number of affected rows
exists ( string $statement, $params ) : boolean Use with SELECT COUNT queries to determine if a record exists.
first ( string $statement, $params ) : mixed Get the first column of each row
getAttribute ( $args ) : mixed Retrieve a database connection attribute
getAvailableDrivers ( ) : array Return an array of available PDO drivers
getDriver ( ) : string Which database driver are we operating on?
getPdo ( ) : PDO Return a copy of the PDO object (to prevent it from being modified to disable safety/security features).
inTransaction ( ) : boolean Checks if inside a transaction
insert ( string $table, array $map ) : integer Insert a new row to a table in a database.
insertGet ( string $table, array $map, string $field ) : mixed Insert a new record then get a particular field from the new row
insertMany ( string $table, array $maps ) : integer Insert many new rows to a table in a database. using the same prepared statement
is1DArray ( array $params ) : boolean Make sure none of this array's elements are arrays
lastInsertId ( $args ) : string Returns the ID of the last inserted row or sequence value
prepare ( $args ) : PDOStatement Prepares a statement for execution and returns a statement object
q ( string $statement, $params ) : mixed Variadic shorthand for $this->safeQuery()
query ( $args ) : PDOStatement Executes an SQL statement, returning a result set as a PDOStatement object
quote ( $args ) : string Quotes a string for use in a query
rollBack ( ) : boolean Rolls back a transaction
row ( string $statement, $params ) : mixed Similar to $this->q() except it only returns a single row
run ( string $statement, $params ) : mixed Variadic shorthand for $this->safeQuery()
safeQuery ( string $statement, array $params = [], integer $fetchStyle = PDO::FETCH_ASSOC, boolean $returnNumAffected = false ) : array | integer Perform a Parametrized Query
setAllowSeparators ( boolean $value ) : self
setAttribute ( integer $attr, mixed $value ) : boolean Set an attribute
single ( string $statement, array $params = [] ) : mixed Fetch a single result -- useful for SELECT COUNT() queries
update ( string $table, array $changes, array $conditions ) : integer Update a row in a database table.

Method Details

__construct() public method

Dependency-Injectable constructor
public __construct ( PDO $pdo, string $dbEngine = '' )
$pdo PDO
$dbEngine string

beginTransaction() public method

Initiates a transaction
public beginTransaction ( ) : boolean
return boolean

cell() public method

Variadic version of $this->single()
public cell ( string $statement, $params ) : mixed
$statement string SQL query without user data
$params Parameters
return mixed

col() public method

Variadic version of $this->column()
public col ( string $statement, integer $offset, $params ) : mixed
$statement string SQL query without user data
$offset integer How many columns from the left are we grabbing from each row?
$params Parameters
return mixed

column() public method

Fetch a column
public column ( string $statement, array $params = [], integer $offset ) : mixed
$statement string SQL query without user data
$params array Parameters
$offset integer How many columns from the left are we grabbing from each row?
return mixed

commit() public method

Commits a transaction
public commit ( ) : boolean
return boolean

delete() public method

Delete rows in a database table.
public delete ( string $table, array $conditions ) : integer
$table string - table name
$conditions array - WHERE clause
return integer

errorCode() public method

Fetch the SQLSTATE associated with the last operation on the database handle
public errorCode ( ) : mixed
return mixed

errorInfo() public method

Fetch extended error information associated with the last operation on the database handle
public errorInfo ( ) : array
return array

escapeIdentifier() public method

Make sure only valid characters make it in column/table names
public escapeIdentifier ( string $string, boolean $quote = true ) : string
$string string - table or column name
$quote boolean - certain SQLs escape column names (i.e. mysql with `backticks`)
return string

escapeLikeValue() public method

Input: ("string_not%escaped") Output: "string\_not\%escaped" WARNING: This function always escapes wildcards using backslash!
public escapeLikeValue ( string $value ) : string
$value string
return string

escapeValueSet() public method

Input: ([1, 2, 3, 5], int) Output: "(1,2,3,5)"
public escapeValueSet ( array $values, string $type = 'string' ) : string
$values array
$type string
return string

exec() public method

Execute an SQL statement and return the number of affected rows
public exec ( $args ) : integer
$args
return integer

exists() public method

Use with SELECT COUNT queries to determine if a record exists.
public exists ( string $statement, $params ) : boolean
$statement string
$params
return boolean

first() public method

Get the first column of each row
public first ( string $statement, $params ) : mixed
$statement string
$params
return mixed

getAttribute() public method

Retrieve a database connection attribute
public getAttribute ( $args ) : mixed
$args
return mixed

getAvailableDrivers() public method

Return an array of available PDO drivers
public getAvailableDrivers ( ) : array
return array

getDriver() public method

Which database driver are we operating on?
public getDriver ( ) : string
return string

getPdo() public method

Return a copy of the PDO object (to prevent it from being modified to disable safety/security features).
public getPdo ( ) : PDO
return PDO

inTransaction() public method

Checks if inside a transaction
public inTransaction ( ) : boolean
return boolean

insert() public method

Insert a new row to a table in a database.
public insert ( string $table, array $map ) : integer
$table string - table name
$map array - associative array of which values should be assigned to each field
return integer

insertGet() public method

Insert a new record then get a particular field from the new row
public insertGet ( string $table, array $map, string $field ) : mixed
$table string
$map array
$field string
return mixed

insertMany() public method

Insert many new rows to a table in a database. using the same prepared statement
public insertMany ( string $table, array $maps ) : integer
$table string - table name
$maps array - array of associative array specifying values should be assigned to each field
return integer

is1DArray() public method

Make sure none of this array's elements are arrays
public is1DArray ( array $params ) : boolean
$params array
return boolean

lastInsertId() public method

Returns the ID of the last inserted row or sequence value
public lastInsertId ( $args ) : string
$args
return string

prepare() public method

Prepares a statement for execution and returns a statement object
public prepare ( $args ) : PDOStatement
$args
return PDOStatement

q() public method

Variadic shorthand for $this->safeQuery()
public q ( string $statement, $params ) : mixed
$statement string SQL query without user data
$params Parameters
return mixed

query() public method

Executes an SQL statement, returning a result set as a PDOStatement object
public query ( $args ) : PDOStatement
$args
return PDOStatement

quote() public method

Quotes a string for use in a query
public quote ( $args ) : string
$args
return string

rollBack() public method

Rolls back a transaction
public rollBack ( ) : boolean
return boolean

row() public method

Similar to $this->q() except it only returns a single row
public row ( string $statement, $params ) : mixed
$statement string SQL query without user data
$params Parameters
return mixed

run() public method

Variadic shorthand for $this->safeQuery()
public run ( string $statement, $params ) : mixed
$statement string SQL query without user data
$params Parameters
return mixed - If successful, a 2D array

safeQuery() public method

Perform a Parametrized Query
public safeQuery ( string $statement, array $params = [], integer $fetchStyle = PDO::FETCH_ASSOC, boolean $returnNumAffected = false ) : array | integer
$statement string The query string (hopefully untainted by user input)
$params array The parameters (used in prepared statements)
$fetchStyle integer PDO::FETCH_STYLE
$returnNumAffected boolean Return the number of rows affected?
return array | integer

setAllowSeparators() public method

public setAllowSeparators ( boolean $value ) : self
$value boolean
return self

setAttribute() public method

Set an attribute
public setAttribute ( integer $attr, mixed $value ) : boolean
$attr integer
$value mixed
return boolean

single() public method

Fetch a single result -- useful for SELECT COUNT() queries
public single ( string $statement, array $params = [] ) : mixed
$statement string
$params array
return mixed

update() public method

Update a row in a database table.
public update ( string $table, array $changes, array $conditions ) : integer
$table string Table name
$changes array Associative array of which values should be assigned to each field
$conditions array WHERE clause
return integer

Property Details

$allowSeparators protected property

protected bool $allowSeparators
return boolean

$dbEngine protected property

protected string $dbEngine
return string

$pdo protected property

protected PDO $pdo
return PDO