|
|
|
@ -9,19 +9,6 @@ use PDOStatement;
|
|
|
|
|
|
|
|
|
|
class PdoWrapper extends PDO
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* How you create the connection for the database
|
|
|
|
|
*
|
|
|
|
|
* @param string $dsn - Ex: 'mysql:host=localhost;port=3306;dbname=testdb;charset=utf8mb4'
|
|
|
|
|
* @param string $username - Ex: 'root'
|
|
|
|
|
* @param string $password - Ex: 'password'
|
|
|
|
|
* @param array<int,mixed> $options - PDO options you can pass in
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(string $dsn, ?string $username = null, ?string $password = null, array $options = [])
|
|
|
|
|
{
|
|
|
|
|
parent::__construct($dsn, $username, $password, $options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use this for INSERTS, UPDATES, or if you plan on using a SELECT in a while loop
|
|
|
|
|
*
|
|
|
|
@ -35,6 +22,7 @@ class PdoWrapper extends PDO
|
|
|
|
|
*
|
|
|
|
|
* @param string $sql - Ex: "SELECT * FROM table WHERE something = ?"
|
|
|
|
|
* @param array<int|string,mixed> $params - Ex: [ $something ]
|
|
|
|
|
*
|
|
|
|
|
* @return PDOStatement
|
|
|
|
|
*/
|
|
|
|
|
public function runQuery(string $sql, array $params = []): PDOStatement
|
|
|
|
@ -54,6 +42,7 @@ class PdoWrapper extends PDO
|
|
|
|
|
*
|
|
|
|
|
* @param string $sql - Ex: "SELECT id FROM table WHERE something = ?"
|
|
|
|
|
* @param array<int|string,mixed> $params - Ex: [ $something ]
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function fetchField(string $sql, array $params = [])
|
|
|
|
@ -69,6 +58,7 @@ class PdoWrapper extends PDO
|
|
|
|
|
*
|
|
|
|
|
* @param string $sql - Ex: "SELECT * FROM table WHERE something = ?"
|
|
|
|
|
* @param array<int|string,mixed> $params - Ex: [ $something ]
|
|
|
|
|
*
|
|
|
|
|
* @return array<string,mixed>
|
|
|
|
|
*/
|
|
|
|
|
public function fetchRow(string $sql, array $params = []): array
|
|
|
|
@ -88,6 +78,7 @@ class PdoWrapper extends PDO
|
|
|
|
|
*
|
|
|
|
|
* @param string $sql - Ex: "SELECT * FROM table WHERE something = ?"
|
|
|
|
|
* @param array<int|string,mixed> $params - Ex: [ $something ]
|
|
|
|
|
*
|
|
|
|
|
* @return array<int,array<string,mixed>>
|
|
|
|
|
*/
|
|
|
|
|
public function fetchAll(string $sql, array $params = []): array
|
|
|
|
@ -109,48 +100,42 @@ class PdoWrapper extends PDO
|
|
|
|
|
*
|
|
|
|
|
* @param string $sql the sql statement
|
|
|
|
|
* @param array<int|string,mixed> $params the params for the sql statement
|
|
|
|
|
*
|
|
|
|
|
* @return array<string,string|array<int|string,mixed>>
|
|
|
|
|
*/
|
|
|
|
|
protected function processInStatementSql(string $sql, array $params = []): array
|
|
|
|
|
{
|
|
|
|
|
/* Handle "IN(?)". This is to be used with a comma delimited string, but can also be used with an array.
|
|
|
|
|
Remove the spaces in variations of "IN ( ? )" where the space after IN is optional, and any number of
|
|
|
|
|
spaces before and after the question mark is optional.
|
|
|
|
|
Then loop through each "IN(?)" in the query and replace the single question mark with the correct
|
|
|
|
|
number of question marks. */
|
|
|
|
|
$sql = preg_replace('/IN\s*\(\s*\?\s*\)/i', 'IN(?)', $sql);
|
|
|
|
|
$current_index = 0;
|
|
|
|
|
while (($current_index = strpos($sql, 'IN(?)', $current_index)) !== false) {
|
|
|
|
|
$preceeding_count = substr_count($sql, '?', 0, $current_index - 1);
|
|
|
|
|
|
|
|
|
|
$param = $params[$preceeding_count];
|
|
|
|
|
$question_marks = '?';
|
|
|
|
|
|
|
|
|
|
// If param is a string, explode it and replace the question mark with the correct number of question marks
|
|
|
|
|
if (is_string($param) || is_array($param)) {
|
|
|
|
|
$params_to_use = $param;
|
|
|
|
|
if (is_string($param)) {
|
|
|
|
|
$params_to_use = explode(',', $param);
|
|
|
|
|
}
|
|
|
|
|
// Replace "IN(?)" with "IN(?,?,?)"
|
|
|
|
|
$sql = preg_replace('/IN\s*\(\s*\?\s*\)/i', 'IN(?)', $sql);
|
|
|
|
|
|
|
|
|
|
foreach ($params_to_use as $key => $value) {
|
|
|
|
|
if (is_string($value)) {
|
|
|
|
|
$params_to_use[$key] = trim($value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$current_index = 0;
|
|
|
|
|
while (($current_index = strpos($sql, 'IN(?)', $current_index)) !== false) {
|
|
|
|
|
$preceeding_count = substr_count($sql, '?', 0, $current_index - 1);
|
|
|
|
|
|
|
|
|
|
// Replace the single question mark with the appropriate number of question marks.
|
|
|
|
|
$question_marks = join(',', array_fill(0, count($params_to_use), '?'));
|
|
|
|
|
$sql = substr_replace($sql, $question_marks, $current_index + 3, 1);
|
|
|
|
|
$param = $params[$preceeding_count];
|
|
|
|
|
$question_marks = '?';
|
|
|
|
|
|
|
|
|
|
// Insert the new params into the params array.
|
|
|
|
|
array_splice($params, $preceeding_count, 1, $params_to_use);
|
|
|
|
|
if (is_string($param) || is_array($param)) {
|
|
|
|
|
$params_to_use = $param;
|
|
|
|
|
if (is_string($param)) {
|
|
|
|
|
$params_to_use = explode(',', $param);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Increment by the length of the question marks and accounting for the length of "IN()"
|
|
|
|
|
$current_index += strlen($question_marks) + 4;
|
|
|
|
|
foreach ($params_to_use as $key => $value) {
|
|
|
|
|
if (is_string($value)) {
|
|
|
|
|
$params_to_use[$key] = trim($value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$question_marks = join(',', array_fill(0, count($params_to_use), '?'));
|
|
|
|
|
$sql = substr_replace($sql, $question_marks, $current_index + 3, 1);
|
|
|
|
|
|
|
|
|
|
array_splice($params, $preceeding_count, 1, $params_to_use);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [ 'sql' => $sql, 'params' => $params ];
|
|
|
|
|
$current_index += strlen($question_marks) + 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [ 'sql' => $sql, 'params' => $params ];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|