public function quoteNamesIn($spec)
{
if (is_array($spec)) {
foreach ($spec as $key => $val) {
$spec[$key] = $this->quoteNamesIn($val);
}
return $spec;
}
// single and double quotes
$apos = "'";
$quot = '"';
// look for ', ", \', or \" in the string.
// match closing quotes against the same number of opening quotes.
$list = preg_split("/(({$apos}+|{$quot}+|\\{$apos}+|\\{$quot}+).*?\\2)/", $spec, -1, PREG_SPLIT_DELIM_CAPTURE);
// concat the pieces back together, quoting names as we go.
$spec = null;
$last = count($list) - 1;
foreach ($list as $key => $val) {
// skip elements 2, 5, 8, 11, etc. as artifacts of the back-
// referenced split; these are the trailing/ending quote
// portions, and already included in the previous element.
// this is the same as every third element from zero.
if (($key + 1) % 3 == 0) {
continue;
}
// is there an apos or quot anywhere in the part?
$is_string = strpos($val, $apos) !== false || strpos($val, $quot) !== false;
if ($is_string) {
// string literal
$spec .= $val;
} else {
// sql language.
// look for an AS alias if this is the last element.
if ($key == $last) {
// note the 'rr' in strripos
$pos = strripos($val, ' AS ');
if ($pos) {
// quote the alias name directly
$alias = $this->_quoteName(substr($val, $pos + 4));
$val = substr($val, 0, $pos) . " AS {$alias}";
}
}
// now quote names in the language.
$spec .= $this->_quoteNamesIn($val);
}
}
// done!
return $spec;
}