|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace vakata\database; |
|
4
|
|
|
|
|
5
|
|
|
use \vakata\database\schema\Table; |
|
6
|
|
|
use \vakata\database\schema\TableQuery; |
|
7
|
|
|
|
|
8
|
|
|
abstract class DriverAbstract implements DriverInterface |
|
9
|
|
|
{ |
|
10
|
1 |
|
protected function expand(string $sql, $par = null) : array |
|
11
|
|
|
{ |
|
12
|
1 |
|
$new = ''; |
|
13
|
1 |
|
$par = array_values($par); |
|
14
|
1 |
|
if (substr_count($sql, '?') === 2 && !is_array($par[0])) { |
|
15
|
|
|
$par = [ $par ]; |
|
16
|
|
|
} |
|
17
|
1 |
|
$parts = explode('??', $sql); |
|
18
|
1 |
|
$index = 0; |
|
19
|
1 |
|
foreach ($parts as $part) { |
|
20
|
1 |
|
$tmp = explode('?', $part); |
|
21
|
1 |
|
$new .= $part; |
|
22
|
1 |
|
$index += count($tmp) - 1; |
|
23
|
1 |
|
if (isset($par[$index])) { |
|
24
|
1 |
|
if (!is_array($par[$index])) { |
|
25
|
|
|
$par[$index] = [ $par[$index] ]; |
|
26
|
|
|
} |
|
27
|
1 |
|
$params = $par[$index]; |
|
28
|
1 |
|
array_splice($par, $index, 1, $params); |
|
29
|
1 |
|
$index += count($params); |
|
30
|
1 |
|
$new .= implode(',', array_fill(0, count($params), '?')); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
1 |
|
return [ $new, $par ]; |
|
34
|
|
|
} |
|
35
|
|
|
/** |
|
36
|
|
|
* Run a query (prepare & execute). |
|
37
|
|
|
* @param string $sql SQL query |
|
38
|
|
|
* @param array $data parameters (optional) |
|
|
|
|
|
|
39
|
|
|
* @return ResultInterface the result of the execution |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function query(string $sql, $par = null) : ResultInterface |
|
42
|
|
|
{ |
|
43
|
1 |
|
$par = isset($par) ? (is_array($par) ? $par : [$par]) : []; |
|
44
|
1 |
|
if (strpos($sql, '??') && count($par)) { |
|
45
|
1 |
|
list($sql, $par) = $this->expand($sql, $par); |
|
46
|
|
|
} |
|
47
|
1 |
|
return $this->prepare($sql)->execute($par); |
|
48
|
|
|
} |
|
49
|
|
|
public function name() : string |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->connection['name']; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
13 |
|
public function option($key, $default = null) |
|
54
|
|
|
{ |
|
55
|
13 |
|
return isset($this->connection['opts'][$key]) ? $this->connection['opts'][$key] : $default; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function begin() : bool |
|
59
|
|
|
{ |
|
60
|
|
|
$this->connect(); |
|
61
|
|
|
try { |
|
62
|
|
|
$this->query("START TRANSACTION"); |
|
63
|
|
|
return true; |
|
64
|
|
|
} catch (DBException $e) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
public function commit() : bool |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
|
|
$this->query("COMMIT"); |
|
72
|
|
|
return true; |
|
73
|
|
|
} catch (DBException $e) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
public function rollback() : bool |
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
|
|
$this->query("ROLLBACK"); |
|
81
|
|
|
return true; |
|
82
|
|
|
} catch (DBException $e) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
abstract public function prepare(string $sql) : StatementInterface; |
|
87
|
|
|
public function table(string $table, bool $detectRelations = true) : Table |
|
88
|
|
|
{ |
|
89
|
|
|
throw new DBException('Not supported'); |
|
90
|
|
|
} |
|
91
|
|
|
public function tables() : array |
|
92
|
|
|
{ |
|
93
|
|
|
throw new DBException('Not supported'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.