1 | <?php |
||
8 | abstract class DriverAbstract implements DriverInterface |
||
9 | { |
||
10 | protected $connection; |
||
11 | |||
12 | protected function expand(string $sql, $par = null) : array |
||
13 | { |
||
14 | $new = ''; |
||
15 | $par = array_values($par); |
||
16 | if (substr_count($sql, '?') === 2 && !is_array($par[0])) { |
||
17 | $par = [ $par ]; |
||
18 | } |
||
19 | $parts = explode('??', $sql); |
||
20 | $index = 0; |
||
21 | foreach ($parts as $part) { |
||
22 | $tmp = explode('?', $part); |
||
23 | $new .= $part; |
||
24 | $index += count($tmp) - 1; |
||
25 | if (isset($par[$index])) { |
||
26 | if (!is_array($par[$index])) { |
||
27 | $par[$index] = [ $par[$index] ]; |
||
28 | } |
||
29 | $params = $par[$index]; |
||
30 | array_splice($par, $index, 1, $params); |
||
31 | $index += count($params); |
||
32 | $new .= implode(',', array_fill(0, count($params), '?')); |
||
33 | } |
||
34 | } |
||
35 | return [ $new, $par ]; |
||
36 | } |
||
37 | /** |
||
38 | * Run a query (prepare & execute). |
||
39 | * @param string $sql SQL query |
||
40 | * @param mixed $par parameters (optional) |
||
41 | * @return ResultInterface the result of the execution |
||
42 | */ |
||
43 | 4 | public function query(string $sql, $par = null, bool $buff = true) : ResultInterface |
|
44 | { |
||
45 | 4 | $par = isset($par) ? (is_array($par) ? $par : [$par]) : []; |
|
46 | 4 | if (strpos($sql, '??') && count($par)) { |
|
47 | list($sql, $par) = $this->expand($sql, $par); |
||
48 | } |
||
49 | 4 | return $this->prepare($sql)->execute($par, $buff); |
|
50 | } |
||
51 | public function name() : string |
||
59 | |||
60 | public function begin() : bool |
||
88 | public function raw(string $sql) |
||
89 | { |
||
90 | $this->connect(); |
||
93 | |||
94 | abstract public function connect(); |
||
100 | |||
101 | public function table(string $table, bool $detectRelations = true) : Table |
||
109 | } |
||
110 |