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
|
|
|
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 |
52
|
|
|
{ |
53
|
|
|
return $this->connection['name']; |
54
|
|
|
} |
55
|
164 |
|
public function option(string $key, $default = null) |
56
|
|
|
{ |
57
|
164 |
|
return isset($this->connection['opts'][$key]) ? $this->connection['opts'][$key] : $default; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function begin() : bool |
61
|
|
|
{ |
62
|
|
|
$this->connect(); |
63
|
|
|
try { |
64
|
|
|
$this->query("START TRANSACTION"); |
65
|
|
|
return true; |
66
|
|
|
} catch (DBException $e) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
public function commit() : bool |
71
|
|
|
{ |
72
|
|
|
try { |
73
|
|
|
$this->query("COMMIT"); |
74
|
|
|
return true; |
75
|
|
|
} catch (DBException $e) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
public function rollback() : bool |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
$this->query("ROLLBACK"); |
83
|
|
|
return true; |
84
|
|
|
} catch (DBException $e) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
public function raw(string $sql) |
89
|
|
|
{ |
90
|
|
|
$this->connect(); |
91
|
|
|
return $this->query($sql); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
abstract public function connect(); |
95
|
|
|
abstract public function prepare(string $sql) : StatementInterface; |
96
|
|
|
abstract public function test() : bool; |
97
|
|
|
public function disconnect() |
98
|
|
|
{ |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function table(string $table, bool $detectRelations = true) : Table |
102
|
|
|
{ |
103
|
|
|
throw new DBException('Not supported'); |
104
|
|
|
} |
105
|
|
|
public function tables() : array |
106
|
|
|
{ |
107
|
|
|
throw new DBException('Not supported'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|