public function testWithObjects()
 {
     $criteria = Criteria::create(TestUser::dao())->add(Expression::containsIp(IpRange::create('192.168.1.1-192.168.1.255'), 'ip'))->addProjection(Projection::property('id'));
     $this->assertEquals($criteria->toDialectString(PostgresDialect::me()), 'SELECT "test_user"."id" FROM "test_user" WHERE "test_user"."ip" <<= \'192.168.1.1-192.168.1.255\'');
     $criteria = Criteria::create(TestInternetProvider::dao())->add(Expression::containsIp('range', IpAddress::create('42.42.42.42')))->addProjection(Projection::property('id'));
     $this->assertEquals($criteria->toDialectString(PostgresDialect::me()), 'SELECT "test_internet_provider"."id" FROM "test_internet_provider" WHERE \'42.42.42.42\' <<= "test_internet_provider"."range"');
 }
 public function testLazy()
 {
     foreach (DBTestPool::me()->getPool() as $db) {
         DBPool::me()->setDefault($db);
         $parent = TestParentObject::create();
         $child = TestChildObject::create()->setParent($parent);
         $parent->dao()->add($parent);
         $child->dao()->add($child);
         $this->assertEquals($parent->getId(), Criteria::create(TestChildObject::dao())->setProjection(Projection::property('parent.id', 'parentId'))->add(Expression::eq('id', $child->getId()))->getCustom('parentId'));
     }
 }
 public function testIpRangeProperty()
 {
     foreach (DBTestPool::me()->getPool() as $db) {
         DBPool::me()->setDefault($db);
         $akado = TestInternetProvider::create()->setName('Akada')->setRange(IpRange::create(IpAddress::create('192.168.1.1'), IpAddress::create('192.168.1.42')));
         TestInternetProvider::dao()->add($akado);
         $plainRange = Criteria::create(TestInternetProvider::dao())->addProjection(Projection::property('range'))->add(Expression::eq('name', 'Akada'))->getCustom();
         $this->assertEquals($plainRange['range'], '192.168.1.1-192.168.1.42');
         TestInternetProvider::dao()->add(TestInternetProvider::create()->setName('DomRu')->setRange(IpRange::create('192.168.2.0/24')));
         $list = Criteria::create(TestInternetProvider::dao())->addOrder('id')->getList();
         $this->assertEquals(count($list), 2);
     }
 }
 public function testForgottenDao()
 {
     $criteria = Criteria::create()->add(Expression::eq('id', 42));
     $listCriteria = clone $criteria;
     try {
         $listCriteria->getList();
         $this->fail();
     } catch (WrongStateException $e) {
         /*it's good*/
     }
     $customCriteria = clone $criteria;
     try {
         $customCriteria->addProjection(Projection::property('id'))->getCustomList();
         $this->fail();
     } catch (WrongStateException $e) {
         /*it's good*/
     }
 }
 public function testProperties()
 {
     $query = OQL::select('from TestUser');
     $criteria = Criteria::create(TestUser::dao());
     $this->assertCriteria($query, $criteria);
     $this->assertCriteria($query->addProperties(OQL::properties('id, count(id) as count')), $criteria->addProjection(Projection::property('id'))->addProjection(Projection::count('id', 'count')));
     $this->assertCriteria($query->addProperties(OQL::properties('city.id')), $criteria->addProjection(Projection::property('city.id')));
     $properties = OQL::properties('id');
     $this->assertFalse($properties->isDistinct());
     $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('id')));
     $properties = OQL::properties('id, distinct name');
     $this->assertTrue($properties->isDistinct());
     $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('id'))->add(Projection::property('name')));
     $properties = OQL::properties('$1')->bind(1, 'foo');
     $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('foo')));
     $properties->bind(1, 'bar');
     $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('bar')));
     $this->assertCriteria(OQL::select('from TestUser')->addProperties($properties->bind(1, 'foo'))->bind(1, 'bar'), Criteria::create(TestUser::dao())->addProjection(Projection::property('bar')));
     $properties = OQL::properties('id, count(distinct city.id + $0), avg(some) as someAverage, ' . 'name not like "%Ы%", foo and (bar or baz), $1 / $2, ' . 'a in ($3, $0)')->bindAll(array(1, 2, 'num', 'test'));
     $this->assertFalse($properties->isDistinct());
     $this->assertEquals($properties->toProjection(), Projection::chain()->add(Projection::property('id'))->add(Projection::distinctCount(Expression::add('city.id', 1)))->add(Projection::avg('some', 'someAverage'))->add(Projection::property(Expression::notLike('name', '%Ы%')))->add(Projection::property(Expression::expAnd('foo', Expression::expOr('bar', 'baz'))))->add(Projection::property(Expression::div(2, 'num')))->add(Projection::property(Expression::in('a', array('test', 1)))));
 }
 /**
  * Gets the value of the entity's property. The value is obtained according to the current
  * setting of EntityQuery and the FetchStrategy set inside DAO of the entity
  * @param string name of the property
  * @return scalar
  */
 function getProperty($property)
 {
     return $this->entity->getDao()->getProperty($this->makeSelect(Projection::property($property)));
 }
 public function testQuery()
 {
     $criteria = Criteria::create(TestUser::dao())->setProjection(Projection::property('id'))->add(Expression::isTrue('id'));
     $this->assertCriteria('id from TestUser where id is true', $criteria)->assertCriteria('id from TestUser where id is true order by id asc', $criteria->addOrder(OrderBy::create('id')->asc()))->assertCriteria('id from TestUser where id is true order by id asc limit 10 offset 1', $criteria->setLimit(10)->setOffset(1))->assertCriteria('id from TestUser where id is true group by id order by id asc limit 10 offset 1', $criteria->setProjection(Projection::chain()->add(Projection::property('id'))->add(Projection::group('id'))))->assertCriteria('id from TestUser where id is true group by id order by id asc having id = 1 limit 10 offset 1', $criteria->setProjection(Projection::chain()->add(Projection::property('id'))->add(Projection::group('id'))->add(Projection::having(Expression::eq('id', 1)))))->assertCriteria('count(id) as count from TestUser group by id having count = 2', Criteria::create(TestUser::dao())->setProjection(Projection::chain()->add(Projection::count('id', 'count'))->add(Projection::group('id'))->add(Projection::having(Expression::eq('count', 2)))));
 }
 function getPropertyList($property, ISqlSelectQuery $query = null)
 {
     $property = $this->logicalSchema->getProperty($property);
     $type = $property->getType();
     if (!$query) {
         $query = EntityQuery::create($this->entity)->get(Projection::property($property));
     }
     $rows = $this->getRows($query);
     $propertySet = $type->assebmleSet($rows, $this->getFetchStrategy());
     return $propertySet;
 }