/** * merge one MingoCriteria instance into this MingoCriteria instance * * @since 1-3-12 * @param \MingoCriteria $criteria the criteria to merge into this one * @return self */ public function merge(MingoCriteria $criteria) { $this->command_symbol = $criteria->getCommandSymbol(); $this->map_where = $criteria->getWhere(); $this->map_sort = $criteria->getSort(); $this->field_map = $criteria->getFields(); if ($criteria->hasLimit()) { $this->setLimit($criteria->getLimit()); } //if if ($criteria->hasPage()) { $this->setPage($criteria->getPage()); } //if if ($criteria->hasOffset()) { $this->setOffset($criteria->getOffset()); } //if return $this; }
/** * this should be used to take the generic $where_criteria and turn it into something * the interface can use (eg, for a SQL interface, the $where_criteria would be turned * into a valid SQL string). * * @param MingoTable $table * @param MingoCriteria $where_criteria * @return mixed return whatever you want, however you want to return it, whatever is easiest for you */ protected function normalizeCriteria(MingoTable $table, MingoCriteria $where_criteria = null) { $ret_map = array(); $ret_map['select_str'] = '*'; $ret_map['table_str'] = $this->normalizeTableSQL($table); $ret_map['where_criteria'] = $where_criteria; $ret_map['where_str'] = ''; $ret_map['where_params'] = array(); $ret_map['sort_str'] = ''; $ret_map['limit_str'] = ''; $ret_map['limit'] = array(0, 0); // canary if (empty($where_criteria)) { return $ret_map; } //if $ret_where = $ret_sort = ''; $criteria_where = $where_criteria->getWhere(); $criteria_sort = $where_criteria->getSort(); $command_symbol = $where_criteria->getCommandSymbol(); foreach ($criteria_where as $name => $map) { $where_sql = ''; $where_val = array(); $name_sql = $this->normalizeNameSQL($name); if (is_array($map)) { $total_map = count($map); // go through each map val and append it to the sql string... foreach ($map as $command => $val) { if ($where_criteria->isCommand($command)) { $command_bare = $where_criteria->getCommand($command); $command_sql = ''; $command_val = array(); // build the sql... if (isset($this->method_map[$command_bare])) { $symbol = empty($this->method_map[$command_bare]['symbol']) ? '' : $this->method_map[$command_bare]['symbol']; if (!empty($this->method_map[$command_bare]['arg'])) { $callback = $this->method_map[$command_bare]['arg']; list($command_sql, $command_val) = $this->{$callback}($symbol, $name_sql, $map[$command]); } //if list($where_sql, $where_val) = $this->appendSql('AND', $command_sql, $command_val, $where_sql, $where_val); } //if } else { throw new UnexpectedValueException(sprintf('there is an error in the internal structure of your %s instance', get_class($where_criteria))); } //if/else } //foreach // we want to parenthesize the sql since there was more than one value for the field if ($total_map > 1) { $where_sql = sprintf(' (%s)', trim($where_sql)); } //if } else { // we have a NAME=VAL (an is* method call)... list($where_sql, $where_val) = $this->normalizeValSql('=', $name_sql, $map); } //if/else list($ret_map['where_str'], $ret_map['where_params']) = $this->appendSql('AND', $where_sql, $where_val, $ret_map['where_str'], $ret_map['where_params']); } //foreach if (!empty($ret_map['where_params'])) { $ret_map['where_str'] = sprintf('WHERE%s', $ret_map['where_str']); } //if // build the sort sql... foreach ($criteria_sort as $name => $direction) { $name_sql = $this->normalizeNameSQL($name); $dir_sql = $direction > 0 ? 'ASC' : 'DESC'; if (empty($ret_map['sort_sql'])) { $ret_map['sort_str'] = sprintf('ORDER BY %s %s', $name_sql, $dir_sql); } else { $ret_map['sort_str'] = sprintf('%s,%s %s', $ret_map['sort_sql'], $name_sql, $dir_sql); } //if/else } //foreach $ret_map = $this->normalizeLimitCriteria($ret_map, $where_criteria->getLimit(), $where_criteria->getOffset()); return $ret_map; }