|
12 | 12 | /** @SuppressWarnings(PHPMD.ExcessiveClassComplexity) */
|
13 | 13 | class SqlQuery extends Query {
|
14 | 14 | const SPECIAL_BINDINGS = [
|
15 |
| - "limit", |
16 |
| - "offset", |
17 |
| - "groupBy", |
18 |
| - "orderBy", |
| 15 | + "field" => ["groupBy", "orderBy"], |
| 16 | + "int" => ["limit", "offset"], |
| 17 | + "string" => ["infileName"], |
19 | 18 | ];
|
20 | 19 |
|
21 | 20 | /** @param array<string, mixed>|array<mixed> $bindings */
|
@@ -72,8 +71,8 @@ public function execute(array $bindings = []):ResultSet {
|
72 | 71 | }
|
73 | 72 | catch(PDOException $exception) {
|
74 | 73 | throw new PreparedStatementException(
|
75 |
| - $exception->getMessage(), |
76 |
| - $exception->getCode(), |
| 74 | + $exception->getMessage() . " (" . $exception->getCode(), |
| 75 | + 0, |
77 | 76 | $exception
|
78 | 77 | );
|
79 | 78 | }
|
@@ -108,24 +107,37 @@ public function injectSpecialBindings(
|
108 | 107 | string $sql,
|
109 | 108 | array $bindings
|
110 | 109 | ):string {
|
111 |
| - foreach(self::SPECIAL_BINDINGS as $special) { |
112 |
| - $specialPlaceholder = ":" . $special; |
| 110 | + foreach(self::SPECIAL_BINDINGS as $type => $specialList) { |
| 111 | + foreach($specialList as $special) { |
| 112 | + $specialPlaceholder = ":" . $special; |
113 | 113 |
|
114 |
| - if(!array_key_exists($special, $bindings)) { |
115 |
| - continue; |
116 |
| - } |
| 114 | + if(!array_key_exists($special, $bindings)) { |
| 115 | + continue; |
| 116 | + } |
117 | 117 |
|
118 |
| - $replacement = $this->escapeSpecialBinding( |
119 |
| - $bindings[$special], |
120 |
| - $special |
121 |
| - ); |
| 118 | + if($type !== "string") { |
| 119 | + $replacement = $this->escapeSpecialBinding( |
| 120 | + $bindings[$special], |
| 121 | + $special |
| 122 | + ); |
| 123 | + } |
122 | 124 |
|
123 |
| - $sql = str_replace( |
124 |
| - $specialPlaceholder, |
125 |
| - $replacement, |
126 |
| - $sql |
127 |
| - ); |
128 |
| - unset($bindings[$special]); |
| 125 | + if($type === "field") { |
| 126 | + $words = explode(" ", $bindings[$special]); |
| 127 | + $words[0] = "`" . $words[0] . "`"; |
| 128 | + $replacement = implode(" ", $words); |
| 129 | + } |
| 130 | + elseif($type === "string") { |
| 131 | + $replacement = "'" . $bindings[$special] . "'"; |
| 132 | + } |
| 133 | + |
| 134 | + $sql = str_replace( |
| 135 | + $specialPlaceholder, |
| 136 | + $replacement, |
| 137 | + $sql |
| 138 | + ); |
| 139 | + unset($bindings[$special]); |
| 140 | + } |
129 | 141 | }
|
130 | 142 |
|
131 | 143 | foreach($bindings as $key => $value) {
|
|
0 commit comments