Documentation

DBHandler
in package

FinalYes

Clase para acceder a las bases de datos "select from table where col1 = ? and col2 = ?", ["aaa", 4] Metodos publicos de utilidad ([+] -> los mas usados): [+]public function selectOne(string $query, array|null $args): array [+]public function selectMany(string $query, array|null $args): array public function selectById(string $query, int $id): array [+]public function rawQuery(string $query, string|null $queryType = 'select_one', array|null $args = []): array public function insertOne(string $query, array $args): array public function insertManyUsingQuestionMarks(string $query, array $args, array|null $formatoColumnas = []): array public function delete(string $query, array $args): array public function update(string $query, array $args): array public function &getLink(): PDO public function beginTransaction(): bool public function commit(): bool public function rollback(): bool

Ejemplos de uso: $db = new DBHandler(); $exec = $db->rawQuery();

Table of Contents

Properties

$link  : mixed
$debugContents  : mixed
$respuesta  : mixed
$settings  : mixed
$statement  : mixed

Methods

__construct()  : mixed
Create new link like this: $link = new DataBaseHandler(app()->conf->DB->connections, 'localDev')
beginTransaction()  : bool
commit()  : bool
delete()  : array<string|int, mixed>
Executes the DELETE query
getLastError()  : array<string|int, mixed>
Get PDO error info from the last operation with error
getLink()  : PDO
Returns the PDO instance by ref
insertManyUsingQuestionMarks()  : array<string|int, mixed>
Inserts many rows using one query: insert into tableName (col1,col2) values (a,b), (c,d), (e,f);
insertOne()  : array<string|int, mixed>
Inserts one row at a time using named paramns: insert into tableName (col1,col2) values (:a, :b);
rawQuery()  : array<string|int, mixed>
Executes a raw query, can accept parameters
rollback()  : bool
selectById()  : array<string|int, mixed>
Same as selectOne, but it expects the parameter to be integer. Param id must be present in query as ":id"
selectMany()  : array<string|int, mixed>
Gets a multidimensional array, each row is a result
selectOne()  : array<string|int, mixed>|false
Gets a single row with results + return false on empty
update()  : array<string|int, mixed>
Executes the UPDATE query
addQueryLogIfEnabled()  : void
adds the query log to the response of the public methods
bindMultiRowParamsUsingQuestionMarks()  : void
binds the parameters to the query using question marks, used only to insert many
bindParamsByName()  : void
binds the parameters to the query by name
bindParamsUsingQuestionMarks()  : void
binds the parameters to the query using question marks, used only with raw query
checkIfConnectionIsUp()  : void
checks if the link object is null or not, if it is null then the database instance was not available
getResponse()  : array<string|int, mixed>
Gets the final response, adds the query log, adds debug
logQueryToFile()  : void
Logs the query and its bounded params to log file if app()->conf->logging->log_db_queries is true
ordenarDatosSegunFormato()  : array<string|int, mixed>
Ordena los datos en args segun el formatoColumnas para que cada dato vaya en su lugar correcto al momento de hacer el binding de los parametros
prepareMultiRowInsertQueryUsingQuestionMarks()  : string
adds the 'values' part of the query to the insert query $query = "insert into tableName (a,b,c) values" return = "insert into tableName (a,b,c) values (?,?,?), (?,?,?), ..."
validateQueryParams()  : bool
Valida que $params sea array o int, si es array debe ser 'associative array' y cada key debe estar presente en la query, a menos que use question marks, en dicho caso usa otra validacion

Properties

$respuesta

private mixed $respuesta = ['is_ok' => true, 'data' => null]

Methods

__construct()

Create new link like this: $link = new DataBaseHandler(app()->conf->DB->connections, 'localDev')

public __construct([object|null &$link = null ][, string|null $selectedConnection = null ]) : mixed
Parameters
$link : object|null = null
$selectedConnection : string|null = null
Tags
throws
Exception

beginTransaction()

public beginTransaction() : bool
Return values
bool

commit()

public commit() : bool
Return values
bool

delete()

Executes the DELETE query

public delete(string $query, array<string|int, mixed> $args) : array<string|int, mixed>
Parameters
$query : string
$args : array<string|int, mixed>
Return values
array<string|int, mixed>

getLastError()

Get PDO error info from the last operation with error

public getLastError() : array<string|int, mixed>
Return values
array<string|int, mixed>

Returns the PDO instance by ref

public & getLink() : PDO
Return values
PDO

insertManyUsingQuestionMarks()

Inserts many rows using one query: insert into tableName (col1,col2) values (a,b), (c,d), (e,f);

public insertManyUsingQuestionMarks(string $query, array<string|int, mixed> $args[, array<string|int, mixed>|null $formatoColumnas = null ]) : array<string|int, mixed>
  • Note: the insert query statement should end in "values"
  • Note: the "(val1, val2), (val3, val4), ..." will be added securely and programatically
  • Note: you don't have to pass the question marks, they are added programatically
Parameters
$query : string
  • FORMAT: "INSERT INTO TABLE (COL1, COL2) VALUES"
$args : array<string|int, mixed>
  • a multidimensional array containing multiple rows to be inserted
$formatoColumnas : array<string|int, mixed>|null = null

(las columnas de cada row)

Return values
array<string|int, mixed>

insertOne()

Inserts one row at a time using named paramns: insert into tableName (col1,col2) values (:a, :b);

public insertOne(string $query, array<string|int, mixed> $args) : array<string|int, mixed>
Parameters
$query : string
$args : array<string|int, mixed>
Tags
throws
PDOException
Return values
array<string|int, mixed>

rawQuery()

Executes a raw query, can accept parameters

public rawQuery(string $query[, string|null $queryType = 'select_one' ][, array<string|int, mixed>|null $args = [] ]) : array<string|int, mixed>
Parameters
$query : string

(example: select * from table where col1 = ?)

$queryType : string|null = 'select_one'

(['select_one', 'select_scalar', 'select_many', 'update', 'insert', 'delete'])

$args : array<string|int, mixed>|null = []

(example: ['foo'])

Return values
array<string|int, mixed>

rollback()

public rollback() : bool
Return values
bool

selectById()

Same as selectOne, but it expects the parameter to be integer. Param id must be present in query as ":id"

public selectById(string $query, int|null $id) : array<string|int, mixed>
Parameters
$query : string
$id : int|null
Return values
array<string|int, mixed>

selectMany()

Gets a multidimensional array, each row is a result

public selectMany(string $query[, array<string|int, mixed>|null|null $args = null ]) : array<string|int, mixed>
Parameters
$query : string
$args : array<string|int, mixed>|null|null = null
  • binds by name
Return values
array<string|int, mixed>

selectOne()

Gets a single row with results + return false on empty

public selectOne(string $query, array<string|int, mixed>|null $args) : array<string|int, mixed>|false
Parameters
$query : string

(example: select * from table where col1 = :param1 and col2 > :param2)

$args : array<string|int, mixed>|null

(example: [':param1' => 'foo', ':param2' => 123])

Return values
array<string|int, mixed>|false

update()

Executes the UPDATE query

public update(string $query, array<string|int, mixed> $args) : array<string|int, mixed>
Parameters
$query : string
$args : array<string|int, mixed>
Return values
array<string|int, mixed>

addQueryLogIfEnabled()

adds the query log to the response of the public methods

private addQueryLogIfEnabled() : void

bindMultiRowParamsUsingQuestionMarks()

binds the parameters to the query using question marks, used only to insert many

private bindMultiRowParamsUsingQuestionMarks(array<string|int, mixed> $params) : void
Parameters
$params : array<string|int, mixed>

(multiple rows only)

bindParamsByName()

binds the parameters to the query by name

private bindParamsByName(array<string|int, mixed>|int $params) : void
Parameters
$params : array<string|int, mixed>|int

bindParamsUsingQuestionMarks()

binds the parameters to the query using question marks, used only with raw query

private bindParamsUsingQuestionMarks(array<string|int, mixed> $params) : void
Parameters
$params : array<string|int, mixed>

checkIfConnectionIsUp()

checks if the link object is null or not, if it is null then the database instance was not available

private checkIfConnectionIsUp() : void

getResponse()

Gets the final response, adds the query log, adds debug

private getResponse() : array<string|int, mixed>
Return values
array<string|int, mixed>

logQueryToFile()

Logs the query and its bounded params to log file if app()->conf->logging->log_db_queries is true

private logQueryToFile() : void

ordenarDatosSegunFormato()

Ordena los datos en args segun el formatoColumnas para que cada dato vaya en su lugar correcto al momento de hacer el binding de los parametros

private ordenarDatosSegunFormato(array<string|int, mixed> $args, array<string|int, mixed> $formatoColumnas) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>

(solo array asociativo para query de multiples lineas)

$formatoColumnas : array<string|int, mixed>
Return values
array<string|int, mixed>

prepareMultiRowInsertQueryUsingQuestionMarks()

adds the 'values' part of the query to the insert query $query = "insert into tableName (a,b,c) values" return = "insert into tableName (a,b,c) values (?,?,?), (?,?,?), ..."

private prepareMultiRowInsertQueryUsingQuestionMarks(string $query, int $numberOfRows, int $numberOfCols) : string
Parameters
$query : string
$numberOfRows : int
$numberOfCols : int
Return values
string

validateQueryParams()

Valida que $params sea array o int, si es array debe ser 'associative array' y cada key debe estar presente en la query, a menos que use question marks, en dicho caso usa otra validacion

private validateQueryParams(string $query, bool $uses_question_marks, array<string|int, mixed>|int $params) : bool
Parameters
$query : string
$uses_question_marks : bool
$params : array<string|int, mixed>|int
Return values
bool

        
On this page

Search results