Exemplo n.º 1
0
 public function testDelete()
 {
     $statement = $this->getStatementBuilder();
     $statement->delete('people', Condition::equals('id', 1));
     $this->assertSame($statement->queries, ['delete from people where id = ?']);
     $this->assertSame($statement->bindings, [1]);
 }
Exemplo n.º 2
0
 public function testManyOr()
 {
     $conditionA = Condition::greaterThanOrEquals('age', 51);
     $conditionB = Condition::lessThan('weight', 170);
     $conditionC = Condition::regex('temperature', '([^\\d]|^)98(.\\d+)?');
     $condition = Condition::combineManyOr([$conditionA, $conditionB, $conditionC]);
     $this->assertSame($condition->getLeft(), $conditionC);
     $this->assertFalse($condition->shouldEscapeLeft());
     $this->assertSame($condition->getOperator(), 'or');
     $this->assertEquals($condition->getRight(), Condition::combineOr($conditionB, $conditionA));
     $this->assertFalse($condition->shouldEscapeRight());
 }
Exemplo n.º 3
0
 public function testHasWithNoResults()
 {
     $db = $this->buildDatabase();
     $condition = Condition::equals('last_name', 'Bob');
     $entry = $db->has('presidents', $condition);
     $this->assertFalse($entry);
 }
Exemplo n.º 4
0
 /**
  * Returns all records that match the criteria.
  *
  * @param string         $table The table that will be accessed and written.
  * @param Condition|null $criteria The criteria that will filter the records.
  * @param array          $options The list of options that will help with finding the records.
  * @return array[] Multiple records from the table that match the criteria.
  */
 public function findAll($table, Condition $criteria = null, array $options = [])
 {
     $limit = null;
     if (isset($options['limit'])) {
         $limit = $options['limit'];
     }
     if ($limit !== null && $limit < 1 || !isset($this->tables[$table])) {
         return [];
     }
     // The code below is included for increased efficiency.
     // It will check to see if a primary key is being compared.
     if ($criteria !== null && $criteria->getOperator() === '=' && $criteria->getLeft() === $this->primaryKey) {
         $primaryKeyValue = $criteria->getRight();
         if (isset($this->tables[$table][$primaryKeyValue])) {
             return [$primaryKeyValue => $this->tables[$table][$primaryKeyValue]];
         }
         return [];
     }
     $count = 0;
     $records = [];
     foreach ($this->tables[$table] as $primaryKeyValue => $record) {
         if ($limit !== null && $count >= $limit) {
             // We have reached our limit!
             break;
         }
         if ($this->isRecordMatchingCondition($primaryKeyValue, $record, $criteria)) {
             $records[$primaryKeyValue] = $record;
             $count++;
         }
     }
     return $records;
 }
Exemplo n.º 5
0
 public function testHasSomethingNonExistent()
 {
     $db = $this->buildDatabase();
     $this->assertFalse($db->has('test', Condition::equals('last_name', 'Lincoln2')));
 }