Esempio n. 1
0
 protected function build_where($test, $operator = 'AND', $supertable = FALSE)
 {
     $sql = array();
     $operator = strtoupper($operator);
     $sub_prefix = $supertable ? $this->protect_names($supertable) . '.' : '';
     foreach ($test as $key => $val) {
         if (is_numeric($key)) {
             if (!\Grocery\Helpers::is_assoc($val)) {
                 $raw = array_shift($val);
                 if ($val && strpos($raw, '?')) {
                     $sql[] = $this->prepare($raw, $val);
                 } else {
                     array_unshift($val, $raw) && ($sql[] = join("\n", $val));
                 }
             } else {
                 $sql[] = is_array($val) ? $this->build_where($val, $operator, $supertable) : $val;
             }
         } elseif (\Grocery\Helpers::is_keyword($key)) {
             $sql[] = '(' . trim($this->build_where($val, strtoupper($key), $supertable)) . ')';
         } elseif (preg_match('/_(?:and|or)_/i', $key, $match)) {
             $sub = array();
             foreach (explode($match[0], $key) as $one) {
                 $sub[$one] = $val;
             }
             $sql[] = '(' . trim($this->build_where($sub, strtoupper(trim($match[0], '_')), $supertable)) . ')';
         } elseif (preg_match('/^(.+?)(?:\\s+(!=?|[<>]=?|<>|NOT|R?LIKE)\\s*)?$/', $key, $match)) {
             $sub = '';
             $key = $this->protect_names($match[1]);
             if ($val === NULL) {
                 $sub = 'IS NULL';
             } else {
                 $val = $this->fixate_value($val, TRUE);
                 $sub = !empty($match[2]) ? $match[2] == '!' ? '!=' : $match[2] : '=';
             }
             if (is_array($val) && sizeof($val) > 1) {
                 $key .= in_array($sub, array('!=', '<>')) ? ' NOT' : '';
                 $sql[] = " {$sub_prefix}{$key} IN(" . join(', ', $val) . ")";
             } else {
                 $val = is_array($val) ? array_shift($val) : $val;
                 $sql[] = " {$sub_prefix}{$key} {$sub} {$val}";
             }
         }
     }
     return join("\n{$operator}\n", $sql);
 }