Helper for execute queries

package salt\sql

 Methods

Start a transaction with PDO

beginTransaction() 

Exceptions

\Exception 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

\Exception 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\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.

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)

Returns

\PDOStatement\PDOStatement after query execution

Execute an UPDATE query

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

Parameters

$query

\salt\UpdateQuery

$expected

integer

number of expected modified rows. NULL if unknown

Returns

\salt\numberof modified rows.

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

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

Returns

\salt\DBHelperthe database resource

Register a database

register(string $name, string $host, string $port, string $db, string $user, string $pass, string $charset = CHARSET
Static

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

Exceptions

\Exception if database already defined

Register a default database

registerDefault(string $name, string $host, string $port, string $db, string $user, string $pass, string $charset = CHARSET
Static

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

Exceptions

\Exception if database already defined

Rollback in first level transaction

rollback() 

Exceptions

\salt\SaltException

Create a new DBHelper

__construct(\PDO $pdo) 

Parameters

$pdo

\PDO

the PDO instance to use in this DBHelper 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
\Exception 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