예제 #1
0
파일: SqlTableTest.php 프로젝트: jivoo/data
 public function testUpdate()
 {
     $db = $this->getDb();
     $table = new SqlTable($db, 'Foo');
     $selection = new UpdateSelectionBuilder($table);
     $selection = $selection->set('a', 'foo');
     // Update all
     $db->expects($this->exactly(3))->method('execute')->withConsecutive([$this->equalTo('UPDATE {Foo} SET a = "foo", b = "baz", c = a + b')], [$this->equalTo('UPDATE {Foo} SET a = "foo" WHERE group = "user" ORDER BY name DESC')], [$this->equalTo('UPDATE {Foo} SET a = "foo" LIMIT 10')])->willReturn(0);
     $selection->set('b', 'baz')->set('c', E::e('a + b'))->update();
     // Update with predicate and ordering
     $selection->where('group = "user"')->orderByDescending('name')->update();
     // Update with limit
     $selection->limit(10)->update();
 }
예제 #2
0
 public function testUpdate()
 {
     $data = [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar'], ['id' => 3, 'name' => 'foobar']];
     // Update all
     $source = new ArrayDataSource($data);
     $selection = new UpdateSelectionBuilder($source);
     $selection->set('name', 'baz')->update();
     foreach ($source->getData() as $record) {
         $this->assertEquals('baz', $record['name']);
     }
     // Update using expression
     $selection = new UpdateSelectionBuilder($source);
     $selection->set('name', E::e('id'))->update();
     foreach ($source->getData() as $record) {
         $this->assertEquals($record['id'], $record['name']);
     }
 }
예제 #3
0
파일: SqlTable.php 프로젝트: jivoo/data
 /**
  * Interpolate variables.
  * See {@see Condition::interpolate}.
  *
  * @param string $query
  *            Query.
  * @param array $vars
  *            Variables.
  * @return string Interpolated query.
  */
 protected function escapeQuery($query, $vars = array())
 {
     return E::interpolate($query, $vars, $this->owner);
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function toString(Quoter $quoter)
 {
     return E::interpolate($this->expr, $this->vars, $quoter);
 }
예제 #5
0
 /**
  * Convert a schema type to an SQLite type
  *
  * @param DataType $type
  *            Type.
  * @param bool $isPrimaryKey
  *            True if primary key.
  * @return string SQLite type.
  */
 public function fromDataType(DataType $type, $isPrimaryKey = false)
 {
     $primaryKey = '';
     if ($isPrimaryKey) {
         $primaryKey = ' PRIMARY KEY';
     }
     switch ($type->type) {
         case DataType::INTEGER:
             if ($type->size == DataType::BIG) {
                 $column = 'INTEGER(8)';
             } elseif ($type->size == DataType::SMALL) {
                 $column = 'INTEGER(2)';
             } elseif ($type->size == DataType::TINY) {
                 $column = 'INTEGER(1)';
             } else {
                 $column = 'INTEGER';
             }
             if ($isPrimaryKey and $type->serial) {
                 $primaryKey .= ' AUTOINCREMENT';
             }
             break;
         case DataType::FLOAT:
             $column = 'REAL';
             break;
         case DataType::STRING:
             $column = 'TEXT(' . $type->length . ')';
             break;
         case DataType::BOOLEAN:
             $column = 'INTEGER(1)';
             break;
         case DataType::BINARY:
             $column = 'BLOB';
             break;
         case DataType::DATE:
             $column = 'INTEGER';
             break;
         case DataType::DATETIME:
             $column = 'INTEGER';
             break;
         case DataType::TEXT:
         case DataType::ENUM:
         case DataType::OBJECT:
         default:
             $column = 'TEXT';
             break;
     }
     $column .= $primaryKey;
     if ($type->notNull) {
         $column .= ' NOT';
     }
     $column .= ' NULL';
     if (isset($type->default)) {
         $column .= E::interpolate(' DEFAULT %_', array($type, $type->default), $this->db);
     }
     return $column;
 }