PDOExt extends PDO and adds:
- storing the original DSN and parsing it into
protocol+ parameters; - convenience helpers for common operations:
insert(),modify(),modifyEx(),get(),queryEx(); - a consistent error-handling style (via
setLastError()+error_log()), with an option to suppress logging (silent); - for SQLite: registration of user functions
mb_strtoupperandmb_levenshtein.
Signature: __construct($_dsn, $username = null, $password = null, $options = null)
Behavior:
- stores the DSN in a private
$dsnfield; - calls
parent::__construct(...); - sets
PDO::ATTR_ERRMODEtoPDO::ERRMODE_EXCEPTION; - detects the protocol via
parseDsn()["protocol"]and, forsqlite, registers functions:mb_strtoupper(1 argument),mb_levenshtein(2 arguments).
Important:
- for
sqlite, functions are always registered, so the runtime is expected to provide callablemb_strtoupperandmb_levenshtein.
Purpose: parses a DSN string into a protocol and a list of parameters.
Returns:
[
"protocol" => string,
"params" => array,
]Rules:
- DSN must contain
:(otherwise the process terminates viadie(...)). - The prefix (protocol) must match
/^[a-z\d]+$/i(otherwisedie(...)). - The part after the protocol is split by
;.- If the DSN contains
=, elements are parsed askey=value. - Otherwise
paramsbecomes a list with a single element (the whole substring after the prefix).
- If the DSN contains
Implementation note:
- the
=check is performed viastrpos($dsnWithoutPrefix, '=')on the whole string, not per element — so mixed formats are handled uniformly for all elements.
Trims whitespace for all values:
- if
$mapis a list (array_is_list($map)), trims each element; - if it's an associative array, trims each value;
- keeps
nullvalues intact.
Used by insert() / modify() / modifyEx() to pass predictable parameters to execute().
Purpose: execute an INSERT (or any query where lastInsertId() is needed).
Behavior:
prepare($query)→execute(trimParams($params))- on success, tries to return
$this->lastInsertId()- if
lastInsertId()is not available / throws, returns-1
- if
- if
execute()returnsfalse, returnsfalse - on errors:
- if
"silent"is not set: callssetLastError(...), logs the exception and the SQL - returns
false
- if
Return values:
string/int(as returned bylastInsertId()), or-1, orfalse.
Purpose: execute a modifying query (UPDATE/DELETE/INSERT without needing lastInsertId()).
Behavior:
prepare($query)→execute(trimParams($params))- on success returns
$sth->rowCount() - if
execute()returnsfalse, returnsfalse - error handling is the same as
insert()(including"silent").
Return values:
int(affected rows) orfalse.
Purpose: apply modifications for a set of fields (a partial update driven by a mapping).
Parameters:
$query: a template string forsprintf($query, $db, $db).- expected to contain 2 placeholders for the same DB column name
$db(e.g. forSET col=:coland/or forWHERE).
- expected to contain 2 placeholders for the same DB column name
$map:dbColumn => paramName$params: input parameters (keyed byparamName)
Logic:
- iterates over
$map - if
$paramscontains$paramName, buildssprintf($query, $db, $db) - executes with parameters
[$db => $params[$paramName]] - accumulates
$sth->rowCount()into$mod - returns
$mod(orfalseon error)
"silent" works the same way as in insert() / modify().
Purpose: execute a query without parameters and return all rows.
Behavior:
prepare($query)→execute()- on success:
fetchAll(PDO::FETCH_ASSOC) - on failure: returns an empty array
[]
Errors are not caught here — an exception can bubble up (because ERRMODE is EXCEPTION).
Purpose: a generic SELECT helper with optional result mapping and “collapse” options.
Execution branches:
- if
$paramsis not empty:prepare($query)→execute($params)(note:trimParams()is not used here)fetchAll(PDO::FETCH_ASSOC)
- otherwise:
- calls
queryEx($query)
- calls
Mapping ($map):
- if
$mapis provided, it is expected to besourceField => targetField - for each row it builds a new array:
[$targetField] = $row[$sourceField]
Options ($options):
"singlify": if the final dataset contains exactly 1 row — return that row, otherwisefalse"fieldlify": if the final dataset contains exactly 1 row — return the first field value of that row, otherwisefalse"silent": suppress error/SQL logging (forPDOException)
Return values:
array(list of rows, or a single row in"singlify"mode)- scalar in
"fieldlify"mode falseif expectations are not met (singlify/fieldlify) or on errors.
setLastError(...)must exist (used in exception handlers).- For
sqlite, callablemb_strtoupperandmb_levenshteinmust be available (they are registered as SQLite functions).