1 | <?php |
||
18 | class Quoter |
||
19 | { |
||
20 | /** |
||
21 | * |
||
22 | * The prefix to use when quoting identifier names. |
||
23 | * |
||
24 | * @var string |
||
25 | * |
||
26 | */ |
||
27 | protected $quote_name_prefix = '"'; |
||
28 | |||
29 | /** |
||
30 | * |
||
31 | * The suffix to use when quoting identifier names. |
||
32 | * |
||
33 | * @var string |
||
34 | * |
||
35 | */ |
||
36 | protected $quote_name_suffix = '"'; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | * Constructor. |
||
41 | * |
||
42 | * @param string $quote_name_prefix The prefix to use when quoting |
||
43 | * identifier names. |
||
44 | * |
||
45 | * @param string $quote_name_suffix The suffix to use when quoting |
||
46 | * identifier names. |
||
47 | * |
||
48 | */ |
||
49 | 389 | public function __construct($quote_name_prefix, $quote_name_suffix) |
|
54 | |||
55 | /** |
||
56 | * |
||
57 | * Returns the prefix to use when quoting identifier names. |
||
58 | * |
||
59 | * @return string |
||
60 | * |
||
61 | */ |
||
62 | 241 | public function getQuoteNamePrefix() |
|
66 | |||
67 | /** |
||
68 | * |
||
69 | * Returns the suffix to use when quoting identifier names. |
||
70 | * |
||
71 | * @return string |
||
72 | * |
||
73 | */ |
||
74 | 241 | public function getQuoteNameSuffix() |
|
78 | |||
79 | /** |
||
80 | * |
||
81 | * Quotes a single identifier name (table, table alias, table column, |
||
82 | * index, sequence). |
||
83 | * |
||
84 | * If the name contains `' AS '`, this method will separately quote the |
||
85 | * parts before and after the `' AS '`. |
||
86 | * |
||
87 | * If the name contains a space, this method will separately quote the |
||
88 | * parts before and after the space. |
||
89 | * |
||
90 | * If the name contains a dot, this method will separately quote the |
||
91 | * parts before and after the dot. |
||
92 | * |
||
93 | * @param string $spec The identifier name to quote. |
||
94 | * |
||
95 | * @return string|array The quoted identifier name. |
||
96 | * |
||
97 | * @see replaceName() |
||
98 | * |
||
99 | * @see quoteNameWithSeparator() |
||
100 | * |
||
101 | */ |
||
102 | 212 | public function quoteName($spec) |
|
103 | { |
||
104 | 212 | $spec = trim($spec); |
|
105 | 212 | $seps = array(' AS ', ' ', '.'); |
|
106 | 212 | foreach ($seps as $sep) { |
|
107 | 212 | $pos = strripos($spec, $sep); |
|
108 | 212 | if ($pos) { |
|
109 | 212 | return $this->quoteNameWithSeparator($spec, $sep, $pos); |
|
110 | } |
||
111 | } |
||
112 | 212 | return $this->replaceName($spec); |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * |
||
117 | * Quotes an identifier that has a separator. |
||
118 | * |
||
119 | * @param string $spec The identifier name to quote. |
||
120 | * |
||
121 | * @param string $sep The separator, typically a dot or space. |
||
122 | * |
||
123 | * @param int $pos The position of the separator. |
||
124 | * |
||
125 | * @return string The quoted identifier name. |
||
126 | * |
||
127 | */ |
||
128 | 26 | protected function quoteNameWithSeparator($spec, $sep, $pos) |
|
135 | |||
136 | /** |
||
137 | * |
||
138 | * Quotes all fully-qualified identifier names ("table.col") in a string, |
||
139 | * typically an SQL snippet for a SELECT clause. |
||
140 | * |
||
141 | * Does not quote identifier names that are string literals (i.e., inside |
||
142 | * single or double quotes). |
||
143 | * |
||
144 | * Looks for a trailing ' AS alias' and quotes the alias as well. |
||
145 | * |
||
146 | * @param string $text The string in which to quote fully-qualified |
||
147 | * identifier names to quote. |
||
148 | * |
||
149 | * @return string|array The string with names quoted in it. |
||
150 | * |
||
151 | * @see replaceNamesIn() |
||
152 | * |
||
153 | */ |
||
154 | 228 | public function quoteNamesIn($text) |
|
155 | { |
||
156 | 228 | $list = $this->getListForQuoteNamesIn($text); |
|
157 | 228 | $last = count($list) - 1; |
|
158 | 228 | $text = null; |
|
159 | 228 | foreach ($list as $key => $val) { |
|
160 | // skip elements 2, 5, 8, 11, etc. as artifacts of the back- |
||
161 | // referenced split; these are the trailing/ending quote |
||
162 | // portions, and already included in the previous element. |
||
163 | // this is the same as skipping every third element from zero. |
||
164 | 228 | if (($key+1) % 3) { |
|
165 | 228 | $text .= $this->quoteNamesInLoop($val, $key == $last); |
|
166 | } |
||
167 | } |
||
168 | 228 | return $text; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * |
||
173 | * Returns a list of candidate elements for quoting. |
||
174 | * |
||
175 | * @param string $text The text to split into quoting candidates. |
||
176 | * |
||
177 | * @return array |
||
178 | * |
||
179 | */ |
||
180 | 228 | protected function getListForQuoteNamesIn($text) |
|
181 | { |
||
182 | // look for ', ", \', or \" in the string. |
||
183 | // match closing quotes against the same number of opening quotes. |
||
184 | 228 | $apos = "'"; |
|
185 | 228 | $quot = '"'; |
|
186 | 228 | return preg_split( |
|
187 | 228 | "/(($apos+|$quot+|\\$apos+|\\$quot+).*?\\2)/", |
|
188 | $text, |
||
189 | 228 | -1, |
|
190 | 228 | PREG_SPLIT_DELIM_CAPTURE |
|
191 | ); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * |
||
196 | * The in-loop functionality for quoting identifier names. |
||
197 | * |
||
198 | * @param string $val The name to be quoted. |
||
199 | * |
||
200 | * @param bool $is_last Is this the last loop? |
||
201 | * |
||
202 | * @return string The quoted name. |
||
203 | * |
||
204 | */ |
||
205 | 228 | protected function quoteNamesInLoop($val, $is_last) |
|
212 | |||
213 | /** |
||
214 | * |
||
215 | * Replaces the names and alias in a string. |
||
216 | * |
||
217 | * @param string $val The name to be quoted. |
||
218 | * |
||
219 | * @return string The quoted name. |
||
220 | * |
||
221 | */ |
||
222 | 228 | protected function replaceNamesAndAliasIn($val) |
|
232 | |||
233 | /** |
||
234 | * |
||
235 | * Quotes an identifier name (table, index, etc); ignores empty values and |
||
236 | * values of '*'. |
||
237 | * |
||
238 | * @param string $name The identifier name to quote. |
||
239 | * |
||
240 | * @return string The quoted identifier name. |
||
241 | * |
||
242 | * @see quoteName() |
||
243 | * |
||
244 | */ |
||
245 | 223 | protected function replaceName($name) |
|
256 | |||
257 | /** |
||
258 | * |
||
259 | * Quotes all fully-qualified identifier names ("table.col") in a string. |
||
260 | * |
||
261 | * @param string $text The string in which to quote fully-qualified |
||
262 | * identifier names to quote. |
||
263 | * |
||
264 | * @return string|array The string with names quoted in it. |
||
265 | * |
||
266 | * @see quoteNamesIn() |
||
267 | * |
||
268 | */ |
||
269 | 228 | protected function replaceNamesIn($text) |
|
296 | |||
297 | } |
||
298 |