protected function where_sql(Dao $dao, &$from, Q $q, array $self_columns, $require_where = null, $alias = true) { if ($q->is_block()) { $vars = $and_block_sql = $or_block_sql = array(); $where_sql = ''; foreach ($q->ar_and_block() as $qa) { list($where, $var) = $this->where_sql($dao, $from, $qa, $self_columns, null, $alias); if (!empty($where)) { $and_block_sql[] = $where; $vars = array_merge($vars, $var); } } if (!empty($and_block_sql)) { $where_sql .= ' (' . implode(' and ', $and_block_sql) . ') '; } foreach ($q->ar_or_block() as $or_block) { list($where, $var) = $this->where_sql($dao, $from, $or_block, $self_columns, null, $alias); if (!empty($where)) { $or_block_sql[] = $where; $vars = array_merge($vars, $var); } } if (!empty($or_block_sql)) { $where_sql .= (empty($where_sql) ? '' : ' and ') . ' (' . implode(' or ', $or_block_sql) . ') '; } if (empty($where_sql)) { $where_sql = $require_where; } else { if (!empty($require_where)) { $where_sql = '(' . $require_where . ') and (' . $where_sql . ')'; } } return array($where_sql, $vars); } if ($q->type() == Q::MATCH) { $query = new Q(); foreach ($q->ar_arg1() as $cond) { if (strpos($cond, '=') !== false) { list($column, $value) = explode('=', $cond); $not = substr($value, 0, 1) == '!'; $value = $not ? strlen($value) > 1 ? substr($value, 1) : '' : $value; if ($value === '') { $query->add($not ? Q::neq($column, '') : Q::eq($column, '')); } else { $query->add($not ? Q::contains($column, $value, $q->param() | Q::NOT) : Q::contains($column, $value, $q->param())); } } else { $columns = array(); foreach ($self_columns as $column) { $columns[] = $column->name(); } $query->add(Q::contains(implode(',', $columns), explode(' ', $cond), $q->param())); } } return $this->where_sql($dao, $from, $query, $self_columns, null, $alias); } $and = $vars = array(); foreach ($q->ar_arg2() as $base_value) { $or = array(); foreach ($q->ar_arg1() as $column_str) { $value = $base_value; $column = $this->get_column($column_str, $self_columns); $column_alias = $this->column_alias_sql($dao, $column, $q, $alias); $is_add_value = true; switch ($q->type()) { case Q::EQ: if ($value === null) { $is_add_value = false; $column_alias .= ' is null'; break; } $column_alias .= ' = ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::NEQ: if ($value === null) { $is_add_value = false; $column_alias .= ' is not null'; break; } $column_alias .= ' <> ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::GT: $column_alias .= ' > ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::GTE: $column_alias .= ' >= ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::LT: $column_alias .= ' < ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::LTE: $column_alias .= ' <= ' . ($value instanceof Daq ? '(' . $value->unique_sql() . ')' : '?'); break; case Q::CONTAINS: case Q::START_WITH: case Q::END_WITH: $column_alias = $this->format_column_alias_sql($dao, $column, $q, $alias); $column_alias .= ($q->not() ? ' not' : '') . ' like(?)'; $value = ($q->type() == Q::CONTAINS || $q->type() == Q::END_WITH ? '%' : '') . $value . ($q->type() == Q::CONTAINS || $q->type() == Q::START_WITH ? '%' : ''); break; case Q::IN: $column_alias .= ($q->not() ? ' not' : '') . ($value instanceof Daq ? ' in(' . $value->unique_sql() . ')' : ' in(' . substr(str_repeat('?,', sizeof($value)), 0, -1) . ')'); break; } if ($value instanceof Daq) { $is_add_value = false; $vars = array_merge($vars, $value->ar_vars()); } $add_join_conds = $dao->join_conds($column->name()); if (!empty($add_join_conds)) { $column_alias .= ' and ' . $this->where_cond_columns($add_join_conds, $from); } $or[] = $column_alias; if ($is_add_value) { if (is_array($value)) { $values = array(); foreach ($value as $v) { $values[] = $q->ignore_case() ? strtoupper($this->column_value($dao, $column->name(), $v)) : $this->column_value($dao, $column->name(), $v); } $vars = array_merge($vars, $values); } else { $vars[] = $q->ignore_case() ? strtoupper($this->column_value($dao, $column->name(), $value)) : $this->column_value($dao, $column->name(), $value); } } } $and[] = ' (' . implode(' or ', $or) . ') '; } return array(implode(' and ', $and), $vars); }