protected function optionsToSQL(array $options) { // Check table (either options['from'] or me-self) $table = array_key_exists("from", $options) ? $options['from'] : $this->getFullyQualifiedName(); // Create new SQL query $sql = new SQL($this->connection, $table); // Any table joins? if (array_key_exists("joins", $options)) { // //@TODO Join tables throw new Exception("Table JOINS not yet implemented.", 1); } // Select fields? if (array_key_exists("select", $options)) { // Apply select $sql->Select($options['select']); } // Conditions given? if (array_key_exists("conditions", $options)) { // Is it a field hash? if (ArrayUtil::IsHash($options['conditions'])) { // Do a where with the hash $sql->Where($options['conditions']); } else { // Is it a single string? if (is_string($options['conditions'])) { // Wrap in array $options['conditions'] = array($options['conditions']); } // Do a where with the string(s) as arguments call_user_func_array(array($sql, "Where"), $options['conditions']); } } // Order. if (array_key_exists("order", $options)) { $sql->Order($options['order']); } // Limit if (array_key_exists("limit", $options)) { $sql->Limit($options['limit']); } // Offset if (array_key_exists("offset", $options)) { $sql->Offset($options['offset']); } // Grouping if (array_key_exists("group", $options)) { $sql->Group($options['group']); } // Having if (array_key_exists("having", $options)) { $sql->Having($options['having']); } // Done return $sql; }
/** * Check whether given array was a search options hash * @param array $array The hash to validate * @return boolean True is valid, otherwise false */ protected static function isOptionsHash($array) { // Hash at all? if (ArrayUtil::IsHash($array)) { // Check difference with valid-options array $keys = array_keys($array); $diff = array_diff($keys, self::$VALID_OPTIONS); // Difference found? if (!empty($diff)) { throw new ActiveRecordException("Unkown key(s): " . explode(', ', $diff)); } // Is there a resemblance then? (Not an empty array, or something..?) $intersect = array_intersect($keys, self::$VALID_OPTIONS); if (!empty($intersect)) { return true; } } return false; }
protected function applyWhereConditions($args) { // A hash given? if (count($args) == 1 && ArrayUtil::IsHash($args[0])) { // More than 1 table used? => Use table names in fields $hash = $this->quoteFields($args[0], is_null($this->joins)); // Create where expressions $clauses = array(); foreach ($hash as $key => &$value) { // Array? if (is_array($value)) { $clauses[] = $key .= ' IN (?)'; } else { $clauses[] = $key .= ' = ?'; } // Simplify value $value = $this->valueToString($value); } // Store it $this->where = implode(", ", $clauses); $this->where_values = array_values($hash); } elseif (count($args) > 0) { throw new Exception("Not yet implemented.", 1); } }