public function testRead() { $db = $this->getDb(); $table = new SqlTable($db, 'Foo'); // Select all $selection = new ReadSelectionBuilder($table); $db->expects($this->exactly(7))->method('query')->withConsecutive([$this->equalTo('SELECT {Foo}.* FROM {Foo}')], [$this->equalTo('SELECT {Foo}.* FROM {Foo} WHERE group = "user" ORDER BY name DESC')], [$this->equalTo('SELECT {Foo}.* FROM {Foo} GROUP BY group, name HAVING name IN ("foo", "bar", "foobar")')], [$this->equalTo('SELECT {Foo}.* FROM {Foo} LIMIT 10 OFFSET 5')], [$this->equalTo('SELECT f.* FROM {Foo} AS f LEFT JOIN {Foo} AS o ON f.id = o.id')], [$this->equalTo('SELECT DISTINCT a FROM {Foo}')], [$this->equalTo('SELECT {Foo}.*, 2 + 2 AS ans FROM {Foo}')])->willReturn($this->getMockBuilder('Jivoo\\Data\\Database\\ResultSet')->getMock()); $selection->toArray(); // Select with a predicate and ordering $selection->where('group = "user"')->orderByDescending('name')->toArray(); // Select with groups $selection->groupBy(['group', 'name'], E::e('name IN %s()', ['foo', 'bar', 'foobar']))->toArray(); // Select with limit and offset $selection->limit(10)->offset(5)->toArray(); // Select with join and alias $selection->alias('f')->leftJoin($table, 'f.id = o.id', 'o')->toArray(); // Select with projection and distinct $selection->distinct()->select('a'); // Select with additional fields $selection->with('ans', '2 + 2')->toArray(); }
/** * Append an extra virtual field to the returned records. * * @param string $field * Name of new field. * @param Expression|string $expression * Expression for field, e.g. 'COUNT(*)'. * @param DataType|null $type * Optional type of field. * @return ReadSelectionBuilder A read selection. */ public function with($field, $expression, DataType $type = null) { $selection = new ReadSelectionBuilder($this->getSource()); return $selection->with($field, $expression, $type); }