1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\MySQL; |
4
|
|
|
|
5
|
|
|
class Query |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
protected $sql; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $sqlPrepared; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var mixed[] |
19
|
|
|
*/ |
20
|
|
|
protected $params = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
protected $escapeChars = [ |
26
|
|
|
"\x00" => "\\0", |
27
|
|
|
"\r" => "\\r", |
28
|
|
|
"\n" => "\\n", |
29
|
|
|
"\t" => "\\t", |
30
|
|
|
"'" => "\'", |
31
|
|
|
'"' => '\"', |
32
|
|
|
"\\" => "\\\\", |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $sql |
37
|
|
|
*/ |
38
|
|
|
public function __construct($sql) |
39
|
|
|
{ |
40
|
|
|
$this->sql = $sql; |
41
|
|
|
$this->sqlPrepared = ''; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Binding params for the query, mutiple arguments support. |
46
|
|
|
* |
47
|
|
|
* @param mixed $param |
|
|
|
|
48
|
|
|
* @return Query |
49
|
|
|
*/ |
50
|
|
|
public function bindParams() |
51
|
|
|
{ |
52
|
|
|
$this->sqlPrepared = null; |
53
|
|
|
$this->params = func_get_args(); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function bindParamsFromArray(array $params) |
59
|
|
|
{ |
60
|
|
|
$this->sqlPrepared = null; |
61
|
|
|
$this->params = $params; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function escape($str) |
67
|
|
|
{ |
68
|
|
|
return strtr($str, $this->escapeChars); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param mixed $value |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function resolveValueForSql($value) |
76
|
|
|
{ |
77
|
|
|
$type = gettype($value); |
78
|
|
|
|
79
|
|
|
switch ($type) |
80
|
|
|
{ |
81
|
|
|
case 'boolean': |
82
|
|
|
$value = (int) $value; |
83
|
|
|
break; |
84
|
|
|
case 'double': |
85
|
|
|
case 'integer': |
86
|
|
|
break; |
87
|
|
|
case 'string': |
88
|
|
|
$value = "'" . $this->escape($value) . "'"; |
89
|
|
|
break; |
90
|
|
|
case 'array': |
91
|
|
|
$nvalue = []; |
92
|
|
|
foreach ($value as $v) { |
93
|
|
|
$nvalue[] = $this->resolveValueForSql($v); |
94
|
|
|
} |
95
|
|
|
$value = implode(',', $nvalue); |
96
|
|
|
break; |
97
|
|
|
case 'null': |
98
|
|
|
$value = 'null'; |
99
|
|
|
break; |
100
|
|
|
default: |
101
|
|
|
throw new \InvalidArgumentException(sprintf('Not supportted value type of %s.', $type)); |
102
|
|
|
break; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function buildSql() |
109
|
|
|
{ |
110
|
|
|
$sql = $this->sql; |
111
|
|
|
$offset = strpos($sql, '?'); |
112
|
|
|
|
113
|
|
|
foreach ($this->params as $param) |
114
|
|
|
{ |
115
|
|
|
$replacement = $this->resolveValueForSql($param); |
116
|
|
|
$sql = substr_replace($sql, $replacement, $offset, 1); |
117
|
|
|
$offset = strpos($sql, '?', $offset + strlen($replacement)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($offset !== false) |
121
|
|
|
{ |
122
|
|
|
throw new \LogicException('Params not enough to build sql'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $sql; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the constructed and escaped sql string. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getSql() |
134
|
|
|
{ |
135
|
|
|
if ($this->sqlPrepared === null) |
136
|
|
|
{ |
137
|
|
|
$this->sqlPrepared = $this->buildSql(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->sqlPrepared; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.