Ejemplo n.º 1
0
 public function testNear()
 {
     $c = new MingoCriteria();
     // sf...
     $lat = 37.775;
     $long = -122.4183333;
     $c->nearField('foo', array($lat, $long), 50);
     $test_map = array('foo' => array('$near' => array($lat, $long), '$maxDistance' => 50));
     $this->assertTrue($c->hasWhere());
     $where_map = $c->getWhere();
     $this->assertSame($test_map, $where_map);
 }
Ejemplo n.º 2
0
 /**
  *  delete the records that match $where_criteria in $table
  *  
  *  @param  MingoTable  $table 
  *  @param  MingoCriteria $where_criteria
  *  @param  boolean $force  if true, then empty where criterias can run (deleting all rows from the table)   
  *  @return boolean
  */
 public function kill(MingoTable $table, MingoCriteria $where_criteria, $force = false)
 {
     // canary...
     $this->assure($table);
     if (!$where_criteria->hasWhere() && empty($force)) {
         throw new UnexpectedValueException('aborting delete because $where_criteria had no where clause');
     }
     //if
     $ret_bool = false;
     $where_criteria->killSort();
     // no need to sort when you're deleting
     $where_criteria->normalizeFields($table);
     $itable = $this->normalizeTable($table);
     $iwhere_criteria = $this->normalizeCriteria($table, $where_criteria);
     try {
         $ret_bool = $this->_kill($itable, $iwhere_criteria);
         if ($this->hasDebug()) {
             if (!is_bool($ret_bool)) {
                 throw new UnexpectedValueException(sprintf('_%s() expected to return: boolean, got: %s', __FUNCTION__, gettype($ret_bool)));
             }
             //if
         }
         //if
     } catch (Exception $e) {
         if ($this->handleException($e, $table)) {
             $ret_bool = $this->_kill($itable, $iwhere_criteria);
         }
         //if
     }
     //try/catch
     return $ret_bool;
 }
Ejemplo n.º 3
0
 /**
  *  find the index table name from the table and the list of fields the index comprises
  *  
  *  @param  MingoTable  $table  the main table's name
  *  @param  MingoCriteria $where_criteria   
  *  @return string  the index table name
  */
 protected function findIndexTableName(MingoTable $table, MingoCriteria $where_criteria)
 {
     if (!$where_criteria->hasWhere() && !$where_criteria->hasSort()) {
         return '';
     }
     //if
     $ret_str = '';
     $is_match = false;
     $where_map = $where_criteria->getWhere();
     $sort_map = $where_criteria->getSort();
     // php >= 5.3, use when Mingo is ported to namespaces...
     ///$field_list = array_keys(array_replace($where_map,$sort_map));
     // we need to get a list of all the fields used in the order they will be used
     $field_list = array_keys($where_map);
     if (!empty($sort_map)) {
         $field_list = array_unique(array_merge($field_list, array_keys($sort_map)));
     }
     //if
     // now go through the index and see if it matches all the fields...
     foreach ($table->getIndexes() as $index) {
         $field_i = 0;
         foreach ($index->getFieldNames() as $field) {
             if (isset($field_list[$field_i])) {
                 if ($field === $field_list[$field_i]) {
                     $is_match = true;
                     $field_i++;
                 } else {
                     $is_match = false;
                     break;
                 }
                 //if/else
             } else {
                 break;
             }
             //if/else
         }
         //foreach
         if ($is_match) {
             // we're done, we found a match...
             $ret_str = $this->getIndexTableName($table, $index);
             break;
         }
         //if
     }
     //foreach
     if (!$is_match) {
         // since we couldn't find an index table, make sure the query can be valid
         // on the main table
         // we are selecting on the main table (no index is being used) so we can only
         // select or sort on 4 fields (by default): _id, _created, and _updated
         foreach ($field_list as $field) {
             // if a field in the where map is not in the main table we've got trouble
             // since an index table couldn't be found
             if (!in_array($field, $this->non_body_fields)) {
                 $e_msg = sprintf('Could not match fields: [%s] sorted by fields: [%s] with an index table.', join(',', array_keys($where_map)), join(',', array_keys($sort_map)));
                 // list the available index tables if we are in debug mode...
                 if ($this->hasDebug()) {
                     $e_msg .= ' Indexes available: ';
                     $index_list = $this->getIndexes($table);
                     $e_index_list = array();
                     foreach ($index_list as $index) {
                         $e_index_list[] = sprintf('%s(%s)', $index->getName(), join(',', $index->getFieldNames()));
                     }
                     //foreach
                     $e_msg .= join(', ', $e_index_list);
                 }
                 //if
                 throw new RuntimeException($e_msg);
             }
             //if
         }
         //foreach
     }
     //if/else
     return $ret_str;
 }