Helper for execute queries

package salt\sql

 Methods

Start a transaction with PDO

beginTransaction() 

Exceptions

\salt\SaltException if PDO->beginTransaction() failed

Check every transaction are ended

checkAllTransactionsEnded() 
Static

Exceptions

\salt\SaltException if one transaction is in progress

Check a password for a registered database

checkPassword(string $name, string $pass) : boolean
Static

Parameters

$name

string

id of a registered database

$pass

string

password to check

Returns

booleanTRUE if successfull connect to database with this password

Commit a transaction. Only first level transaction are effective.<br/> If any nested transaction call rollback(), the commit will rollback

commit() 

Exceptions

\salt\SaltException

Execute a SELECT count(*) query

execCountQuery(\salt\Query $query) : integer

Parameters

$query

\salt\Query

the query to execute

Returns

integernumber of rows

Execute a CREATE TABLE query

execCreate(\salt\CreateTableQuery $query) 

Parameters

$query

\salt\CreateTableQuery

the CreateTable query

Exceptions

\salt\SaltException if called during a transaction

Execute a DELETE query

execDelete(\salt\DeleteQuery $query, integer $expected = -1) : integer

Parameters

$query

\salt\DeleteQuery

the query to execute

$expected

integer

number of expected delete, NULL for unknown

Exceptions

\salt\RowCountException if delete don't change the expected number of rows

Returns

integerthe number of rows deleted

Execute an INSERT query

execInsert(\salt\InsertQuery $query) : string

Parameters

$query

\salt\InsertQuery

the query to execute

Exceptions

\salt\RowCountException if query don't insert the expected number of objects

Returns

string\PDOStatement::lastInsertId()

Execute a SELECT query

execQuery(\salt\Query $query, \salt\Pagination $pagination = NULL, \salt\Base $bindingObject = NULL) : \salt\DBResult

If $pagination is provided and not locked, a count query is also executed

Parameters

$query

\salt\Query

the query

$pagination

\salt\Pagination

Pagination object.

$bindingObject

\salt\Base

(Optional) bind to another object type. All returned objects are in NEW state instead of LOADED

Returns

\salt\DBResultresult of the query

Execute a query from a SQL text

execSQL(string $sql, array $binds = array()) : \PDOStatement

Parameters

$sql

string

sql text

$binds

array

array of placeholder (key => value). If we want to set the type for bind a value, we can suffix the key by @ followed by a PDOPARAM* constant
For example : Par exemple, array(':param@'.PDO::PARAM_INT => 3)
value can also be an array with two keys for compatibily with classic queries : array('value' => value, 'type' => FieldType)

Returns

\PDOStatementPDOStatement after query execution

Execute an UPDATE query

execUpdate(\salt\UpdateQuery $query, integer $expected = -1) : integer

Parameters

$query

\salt\UpdateQuery

$expected

integer

number of expected modified rows. NULL if unknown

Returns

integernumber of modified rows.

Return database name

getDatabase(string $type = NULL) : string
Static

Parameters

$type

string

id of a previously registered database, or NULL for default registered database

Exceptions

\salt\SaltException if $type is unknown

Returns

stringdatabase name

Retrieve an instance of DBHelper

getInstance(string $type = NULL) : \salt\DBHelper
Static

Parameters

$type

string

id of a previously registered database, or NULL for default registered database

Exceptions

\salt\SaltException if $type is unknown or connexion failed

Returns

\salt\DBHelperthe database resource

Check transaction is active

inTransaction() : boolean

Returns

booleanTRUE if we are in a transaction

Register a database

register(string $name, string $host, string $port, string $db, string $user, string $pass, string $charset, array $options = array()
Static
see

Parameters

$name

string

the id of the database. Used for retrieve the instance with DBHelper::getInstance(name)

$host

string

host name

$port

string

port

$db

string

database name

$user

string

user name

$pass

string

password of user

$charset

string

charset of database

$options

array

PDO options array

Register a default database

registerDefault(string $name, string $host, string $port, string $db, string $user, string $pass, string $charset, array $options = array()
Static
see

Parameters

$name

string

the id of the database. Used for retrieve the instance with DBHelper::getInstance()

$host

string

host name

$port

string

port

$db

string

database name

$user

string

user name

$pass

string

password of user

$charset

string

charset of database

$options

array

PDO options array

Exceptions

\salt\SaltException if database already defined

Rollback in first level transaction

rollback() 

Exceptions

\salt\SaltException

Create a new DBHelper

__construct(\PDO $pdo, string $type) 

Parameters

$pdo

\PDO

the PDO instance to use in this DBHelper instance

$type

string

type of the instance

Add some information about query in Benchmark data

addDebugData(string $sql, array<mixed,mixed> $binds, float|NULL $temps) 

Parameters

$sql

string

SQL text query (can be count or not count query)

$binds

array<mixed,mixed>

placeholders (key => value)

$temps

floatNULL

execution time or NULL if query failed

Construct and execute a query

exec(\salt\Query $query, boolean $count = FALSE, \salt\Pagination $pagination = NULL) : \PDOStatement

Parameters

$query

\salt\Query

the query

$count

boolean

true if count query have to be executed

$pagination

\salt\Pagination

pagination if required

Exceptions

\salt\DBException if prepare or execute query failed with a PDOException
\salt\SaltException if something else failed

Returns

\PDOStatementthe PDOStatement after execution

 Properties

 

List of all registed DB name=>DBConnexion

$allDatas : array<mixed,\salt\DBConnexion>

Default

array()
Static
 

List of all connected DB : name=>DBHelper

$allInstances : array<mixed,\salt\DBHelper>

Default

array()
Static
 

instance of DB connexion

$base : \PDO

Default

NULL
 

name of the default DBHelper

$default : string

Default

NULL
Static
 

transaction level. 0 is effective level

$txLevel : integer

Default

0
 

TRUE if a rollback is called during transaction processing

$txRollback : boolean

Default

false
 

type of instance

$type : string

Default

NULL