|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class StatementAbstract |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource StatementAbstract.php |
|
6
|
|
|
* @created 03.06.2017 |
|
7
|
|
|
* @package chillerlan\Database\Query |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2017 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\Database\Drivers\DBDriverInterface; |
|
16
|
|
|
|
|
17
|
|
|
abstract class StatementAbstract implements StatementInterface{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \chillerlan\Database\Drivers\DBDriverInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $DBDriver; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $dialect; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \string[] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $quotes; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $limit; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $offset; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $bindValues = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var bool |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $offsetlimit_bound = false; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var bool |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $multi = false; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* StatementAbstract constructor. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \chillerlan\Database\Drivers\DBDriverInterface $DBDriver |
|
63
|
|
|
* @param string $dialect |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct(DBDriverInterface $DBDriver, string $dialect){ |
|
66
|
|
|
$this->DBDriver = $DBDriver; |
|
67
|
|
|
$this->dialect = $dialect; |
|
68
|
|
|
$this->quotes = $this->DBDriver->quotes; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function sql():string { |
|
75
|
|
|
return '--NOT IMPLEMENTED'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function bindValues():array{ |
|
82
|
|
|
|
|
83
|
|
|
if(!$this->offsetlimit_bound){ |
|
84
|
|
|
if($this->offset){ |
|
85
|
|
|
$this->bindValues[] = $this->offset; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if($this->limit){ |
|
89
|
|
|
$this->bindValues[] = $this->limit; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->offsetlimit_bound = true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->bindValues; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string|null $index |
|
100
|
|
|
* @param array|null $values |
|
101
|
|
|
* @param callable|null $callback |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool|\chillerlan\Database\DBResult |
|
104
|
|
|
*/ |
|
105
|
|
|
public function execute(string $index = null, array $values = null, $callback = null){ |
|
106
|
|
|
|
|
107
|
|
|
return $this->multi || !is_null($values) |
|
108
|
|
|
? is_callable($callback) |
|
109
|
|
|
? $this->DBDriver->multi_callback($this->sql(), $values, $callback) |
|
|
|
|
|
|
110
|
|
|
: $this->DBDriver->multi($this->sql(), !empty($values) ? $values : $this->bindValues()) |
|
111
|
|
|
: $this->DBDriver->prepared($this->sql(), $this->bindValues(), $index); |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $str |
|
117
|
|
|
* @todo ... |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function quote(string $str):string{ |
|
121
|
|
|
$x = explode('.', $str); |
|
122
|
|
|
|
|
123
|
|
|
$str = $this->quotes[0].implode($this->quotes[1].'.'.$this->quotes[0], $x).$this->quotes[1]; |
|
124
|
|
|
return $str; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $name |
|
129
|
|
|
* |
|
130
|
|
|
* @return mixed |
|
|
|
|
|
|
131
|
|
|
*/ |
|
132
|
|
|
protected function getStatementClass(string $name){ |
|
133
|
|
|
$class = $this->dialect.'\\'.$name; |
|
134
|
|
|
|
|
135
|
|
|
return new $class($this->DBDriver, $this->dialect); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: