public function searchItems(EarthIT_Storage_Search $search, array $options = array()) { $rcName = $search->getResourceClass()->getName(); $filter = $search->getFilter(); $matched = array(); if (isset($this->items[$rcName])) { foreach ($this->items[$rcName] as $item) { if ($filter->matches($item)) { $matched[] = $item; } } } usort($matched, $search->getComparator()); return array_slice($matched, $search->getSkip(), $search->getLimit()); }
public function makeSearchQuery(EarthIT_Storage_Search $search, array $options = array()) { $rc = $search->getResourceClass(); $params = array(); $PB = new EarthIT_DBC_ParamsBuilder($params); $params['table'] = $this->rcTableExpression($rc); $conditions = $search->getFilter()->toSql('stuff', $this->dbObjectNamer, $PB); // TODO: only select certain fields if fieldsOfInterest given $selects = $this->makeDbExternalFieldValueSqls(EarthIT_Storage_Util::storableFields($rc), $rc, 'stuff', $PB); $selectSqls = EarthIT_Storage_Util::formatSelectComponents($selects, $PB); if (count($selectSqls) == 0) { throw new Exception("Can't select zero stuff."); } $orderBys = array(); $comparator = $search->getComparator(); if ($comparator instanceof EarthIT_Storage_FieldwiseComparator) { foreach ($comparator->getComponents() as $cc) { $columnName = $this->dbObjectNamer->getColumnName($rc, $rc->getField($cc->getFieldName())); $orderBys[] = '{' . $PB->newParam('c', new EarthIT_DBC_SQLIdentifier($columnName)) . '} ' . $cc->getDirection(); } } else { throw new Exception("Don't know how to order based on a " . get_class($comparator)); } $skip = $search->getSkip(); $limit = $search->getLimit(); $limitStuff = ''; if ($limit !== null) { $limitStuff .= "LIMIT " . (int) $limit; } if ($skip !== null) { $limitStuff .= "OFFSET " . (int) $skip; } return EarthIT_DBC_SQLExpressionUtil::expression("SELECT\n\t" . implode(",\n\t", $selectSqls) . "\n" . "FROM {table} AS stuff\n" . "WHERE {$conditions}\n" . ($orderBys ? "ORDER BY " . implode(', ', $orderBys) . "\n" : '') . $limitStuff, $params); }