Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSqlQueryRowList.php
More file actions
Latest commit
70 lines (57 loc) · 1.79 KB
/
Copy pathSqlQueryRowList.php
File metadata and controls
70 lines (57 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespaceRay\Query;
useAura\Sql\ExtendedPdoInterface;
usePDO;
usePDOStatement;
useRay\Query\Exception\QueryNumException;
usefunctionarray_pop;
usefunctioncount;
usefunctionexplode;
usefunctionpreg_replace;
usefunctionstrpos;
usefunctionstrtolower;
usefunctiontrim;
/** @psalm-api */
class SqlQueryRowList implements RowListInterface
{
publicconstQUERY_CLEANUP_REGEX = '/\/\*.*?\*\/|--.*$/m';
publicconstTRIM_CHARACTERS_REGEX = "\\\t\n\r\0\x0B";
/** @var ExtendedPdoInterface */
private$pdo;
/** @var string */
private$sql;
publicfunction__construct(ExtendedPdoInterface$pdo, string$sql)
{
$this->pdo = $pdo;
$this->sql = $sql;
}
/** @param array<string, mixed> ...$queries */
publicfunction__invoke(array ...$queries): iterable
{
if (strpos($this->sql, ';') === false) {
$this->sql .= ';';
}
$sqls = explode(';', trim($this->sql, self::TRIM_CHARACTERS_REGEX));
array_pop($sqls);
$numQueris = count($queries);
if (count($sqls) !== $numQueris) {
thrownewQueryNumException($this->sql); // @codeCoverageIgnore
}
$result = null;
for ($i = 0; $i < $numQueris; $i++) {
$sql = $sqls[$i];
$query = $queries[$i];
$result = $this->pdo->perform($sql, $query);
}
$lastQuery = $result
? strtolower(trim(
(string) preg_replace(self::QUERY_CLEANUP_REGEX, '', $result->queryString),
self::TRIM_CHARACTERS_REGEX,
)) : '';
if ($resultinstanceof PDOStatement && strpos($lastQuery, 'select') === 0) {
return$result->fetchAll(PDO::FETCH_ASSOC);
}
return [];
}
}