/** * @return QueryStatement */ private static function processCondition($left = null, $right = null, $operator = Query::EQUAL, $quote = null) { if ($left instanceof QueryStatement && 1 === func_num_args()) { return $left; } $statement = new QueryStatement(); // Left can be a Condition if ($left instanceof self) { $clause_statement = $left->getQueryStatement(); if (null === $clause_statement) { return null; } $clause_statement->string = '(' . $clause_statement->string . ')'; return $clause_statement; } if (null === $quote) { // You can skip $operator and specify $quote with parameter 3 if (is_int($operator)) { $quote = $operator; $operator = Query::EQUAL; } else { $quote = self::QUOTE_RIGHT; } } if (Query::BEGINS_WITH === $operator) { $right .= '%'; $operator = Query::LIKE; } elseif (Query::ENDS_WITH === $operator) { $right = '%' . $right; $operator = Query::LIKE; } elseif (Query::CONTAINS === $operator) { $right = '%' . $right . '%'; $operator = Query::LIKE; } // Escape $left if ($quote === self::QUOTE_LEFT || $quote === self::QUOTE_BOTH) { $statement->addParam($left); $left = QueryStatement::PARAM; } else { $statement->addIdentifier($left); $left = QueryStatement::IDENTIFIER; } $is_query = $right instanceof Query; $is_array = false === $is_query && is_array($right); if ($is_array || $is_query) { if (false === $is_query || 1 !== $right->getLimit()) { // Convert any sort of equality operator to something suitable for arrays switch ($operator) { // Various forms of equal case Query::IN: break; case Query::EQUAL: $operator = Query::IN; break; case Query::BETWEEN: break; // Various forms of not equal // Various forms of not equal case Query::NOT_IN: break; case Query::NOT_EQUAL: case Query::ALT_NOT_EQUAL: $operator = Query::NOT_IN; break; default: throw new Exception('Operator "' . $operator . '" cannot be used for comparing an array.'); } } // Right can be a Query, if you're trying to nest queries, like "WHERE MyColumn = (SELECT OtherColumn From MyTable LIMIT 1)" if ($is_query) { if (!$right->getTable()) { throw new Exception('right does not have a table, so it cannot be nested.'); } $clause_statement = $right->getQuery(); if (null === $clause_statement) { return null; } $right = '(' . $clause_statement->string . ')'; $statement->addParams($clause_statement->params); $statement->addIdentifiers($clause_statement->identifiers); if ($quote !== self::QUOTE_LEFT) { $quote = self::QUOTE_NONE; } } elseif ($is_array) { $array_len = count($right); // BETWEEN if (2 === $array_len && $operator === Query::BETWEEN) { $statement->string = $left . ' ' . $operator . ' ' . QueryStatement::PARAM . ' AND ' . QueryStatement::PARAM; $statement->addParams($right); return $statement; } elseif (0 === $array_len) { // Handle empty arrays if ($operator === Query::IN) { $statement->string = '(0 = 1)'; return $statement; } elseif ($operator === Query::NOT_IN) { return null; } } elseif ($quote === self::QUOTE_RIGHT || $quote === self::QUOTE_BOTH) { $statement->addParams($right); $r_string = '('; for ($x = 0; $x < $array_len; ++$x) { if (0 < $x) { $r_string .= ','; } $r_string .= QueryStatement::PARAM; } $right = $r_string . ')'; } } } else { if (null === $right) { if ($operator === Query::NOT_EQUAL || $operator === Query::ALT_NOT_EQUAL) { // IS NOT NULL $operator = Query::IS_NOT_NULL; } elseif ($operator === Query::EQUAL) { // IS NULL $operator = Query::IS_NULL; } } if ($operator === Query::IS_NULL || $operator === Query::IS_NOT_NULL) { $right = null; } elseif ($quote === self::QUOTE_RIGHT || $quote == self::QUOTE_BOTH) { $statement->addParam($right); $right = QueryStatement::PARAM; } } if (!in_array($operator, Query::$operators, true)) { throw new InvalidArgumentException('"' . $operator . '" is not a valid SQL operator.'); } $statement->string = $left . ' ' . $operator . ' ' . $right; return $statement; }
/** * Protected for now. Likely to be public in the future. * @return QueryStatement */ protected function getGroupByClause($conn = null) { $statement = new QueryStatement($conn); if ($this->_groups) { $groups = $this->_groups; foreach ($groups as &$group) { $statement->addIdentifier($group); $group = QueryStatement::IDENTIFIER; } $statement->string = "\nGROUP BY " . implode(', ', $groups); } return $statement; }
/** * @param DABLPDO $conn * @return QueryStatement */ function getQueryStatement(DABLPDO $conn = null) { $statement = new QueryStatement($conn); $table = $this->_table; $on_clause = $this->_onClause; $join_type = $this->_joinType; $alias = $this->_alias; if ($table instanceof Query) { $table_statement = $table->getQuery($conn); $table = '(' . $table_statement->getString() . ')'; $statement->addParams($table_statement->getParams()); $statement->addIdentifiers($table_statement->getIdentifiers()); } else { $statement->addIdentifier($table); $table = QueryStatement::IDENTIFIER; } if ($alias) { $table .= " AS {$alias}"; } if ($this->_isLikePropel) { $statement->addIdentifiers(array($this->_leftColumn, $this->_rightColumn)); $on_clause = QueryStatement::IDENTIFIER . ' = ' . QueryStatement::IDENTIFIER; } elseif (null === $on_clause) { $on_clause = '1 = 1'; } elseif ($on_clause instanceof Condition) { $on_clause_statement = $on_clause->getQueryStatement(); $on_clause = $on_clause_statement->getString(); $statement->addParams($on_clause_statement->getParams()); $statement->addIdentifiers($on_clause_statement->getIdentifiers()); } if ('' !== $on_clause) { $on_clause = "ON ({$on_clause})"; } $statement->setString("{$join_type} {$table} {$on_clause}"); return $statement; }