Solar_Sql_Adapter::quoteNamesIn PHP 메소드

quoteNamesIn() 공개 메소드

Does not quote identifier names that are string literals (i.e., inside single or double quotes). Looks for a trailing ' AS alias' and quotes the alias as well.
또한 보기: _quoteNamesIn()
public quoteNamesIn ( string | array $spec ) : string | array
$spec string | array The string in which to quote fully-qualified identifier names to quote. If an array, quotes names in each element in the array.
리턴 string | array The string (or array) with names quoted in it.
    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;
    }