SQLGlobal::buildWhere PHP Метод

buildWhere() защищенный Метод

protected buildWhere ( $originalWhere = null, $whereKeyword = null )
    protected function buildWhere($originalWhere = null, $whereKeyword = null)
    {
        $sql =& $this->_sql;
        $where = is_null($originalWhere) ? $this->where : $originalWhere;
        if (count($where) == 0) {
            return;
        }
        $sql[] = is_null($whereKeyword) ? $this->option['whereKeyword'] : $whereKeyword;
        $whereData = array();
        foreach ($where as $index => $value) {
            if (is_string($value)) {
                $whereData[] = $value;
                continue;
            }
            $eq = strtoupper($value[0]);
            if (in_array($eq, array('=', '<>', '>', '<', '>=', '<=', 'NOT LIKE', 'LIKE', 'ILIKE', 'NOT ILIKE'))) {
                $x = (string) $value[1];
                $y = $this->db->EscapeString((string) $value[2]);
                $whereData[] = " {$x} {$eq} '{$y}' ";
            } elseif ($eq == 'EXISTS' || $eq == 'NOT EXISTS') {
                if (!isset($value[2])) {
                    $whereData[] = " {$eq} ( {$value['1']} ) ";
                } else {
                    $whereData[] = " (( {$value['1']} {$eq} {$value['2']} )) ";
                }
            } elseif ($eq == 'BETWEEN') {
                $whereData[] = " ({$value['1']} BETWEEN '{$value['2']}' AND '{$value['3']}') ";
            } elseif ($eq == 'SEARCH') {
                $searchCount = count($value);
                $sqlSearch = array();
                for ($i = 1; $i <= $searchCount - 1 - 1; $i++) {
                    $x = (string) $value[$i];
                    $y = $this->db->EscapeString((string) $value[$searchCount - 1]);
                    $sqlSearch[] = " ({$x} LIKE '%{$y}%') ";
                }
                $whereData[] = " ((1 = 1) AND (" . implode(' OR ', $sqlSearch) . ') )';
            } elseif ($eq == 'ARRAY' || $eq == 'NOT ARRAY' || $eq == "LIKE ARRAY" || $eq == "ILIKE ARRAY" || $eq == 'ARRAY_LIKE' || $eq == 'ARRAY_ILIKE') {
                if ($eq == 'ARRAY') {
                    $symbol = '=';
                } elseif ($eq == 'NOT ARRAY') {
                    $symbol = '<>';
                } elseif ($eq == 'LIKE ARRAY' || $eq == 'ARRAY_LIKE') {
                    $symbol = 'LIKE';
                } elseif ($eq == 'ILIKE ARRAY' || $eq == 'ARRAY_ILIKE') {
                    $symbol = 'ILIKE';
                }
                $sqlArray = array();
                if (!is_array($value[1])) {
                    $whereData[] = " (1 = 1) ";
                    continue;
                }
                if (count($value[1]) == 0) {
                    $whereData[] = " (1 = 1) ";
                    continue;
                }
                foreach ($value[1] as $x => $y) {
                    $y[1] = $this->db->EscapeString($y[1]);
                    $sqlArray[] = " {$y['0']} {$symbol} '{$y['1']}' ";
                }
                $whereData[] = " ((1 = 1) AND (" . implode(' OR ', $sqlArray) . ') )';
            } elseif ($eq == 'IN' || $eq == 'NOT IN') {
                $sqlArray = array();
                if (!is_array($value[2])) {
                    if ($this->validateParamater($value[2])) {
                        $whereData[] = " ({$value['1']} {$eq} ({$value['2']})) ";
                        continue;
                    } else {
                        $whereData[] = " (1 = 1) ";
                    }
                    continue;
                }
                if (count($value[2]) == 0) {
                    $whereData[] = " (1 = 1) ";
                    continue;
                }
                foreach ($value[2] as $x => $y) {
                    $y = $this->db->EscapeString($y);
                    $sqlArray[] = " '{$y}' ";
                }
                $whereData[] = " ((1 = 1) AND ({$value['1']} {$eq} (" . implode(', ', $sqlArray) . ') ) )';
            } elseif ($eq == 'META_NAME') {
                if (count($value) != 3) {
                    $whereData[] = " (1 = 1) ";
                    continue;
                }
                $sqlMeta = 's:' . strlen($value[2]) . ':"' . $value[2] . '";';
                $sqlMeta = $this->db->EscapeString($sqlMeta);
                $whereData[] = "({$value['1']} LIKE '%{$sqlMeta}%')";
            } elseif ($eq == 'META_NAMEVALUE') {
                if (count($value) != 4) {
                    $whereData[] = " (1 = 1) ";
                    continue;
                }
                $sqlMeta = 's:' . strlen($value[2]) . ':"' . $value[2] . '";' . 's:' . strlen($value[3]) . ':"' . $value[3] . '"';
                $sqlMeta = $this->db->EscapeString($sqlMeta);
                $whereData[] = "({$value['1']} LIKE '%{$sqlMeta}%')";
            } elseif ($eq == "CUSTOM") {
                $whereData[] = $value[1];
            }
        }
        $sql[] = implode(' AND ', $whereData);
    }