Пример #1
0
 function test_query_builder_select()
 {
     $builder = new Dataface_QueryBuilder('Profiles');
     $this->assertEquals($builder->select(), 'SELECT `Profiles`.`id`,`Profiles`.`fname`,`Profiles`.`lname`,`Profiles`.`title`,`Profiles`.`description`,`Profiles`.`dob`,`Profiles`.`phone1`,`Profiles`.`phone2`,`Profiles`.`fax`,`Profiles`.`email`,`Profiles`.`datecreated`,`Profiles`.`lastmodified`,`Profiles`.`favtime`,`Profiles`.`lastlogin`,`Profiles`.`photo_mimetype`,`Profiles`.`tablefield` FROM `Profiles`');
     $this->assertEquals($builder->select(array('id')), 'SELECT `Profiles`.`id` FROM `Profiles`');
     $this->assertEquals($builder->select(array('id', 'fname')), 'SELECT `Profiles`.`id`,`Profiles`.`fname` FROM `Profiles`');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1)), 'SELECT `Profiles`.`id` FROM `Profiles` LIMIT 1');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5)), 'SELECT `Profiles`.`id` FROM `Profiles` LIMIT 5,1');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5, 'fname' => 'John')), 'SELECT `Profiles`.`id` FROM `Profiles` WHERE `Profiles`.`fname` LIKE \'%John%\' LIMIT 5,1');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5, 'fname' => 'John', 'lname' => 'Smith', 'id' => 10)), 'SELECT `Profiles`.`id` FROM `Profiles` WHERE `Profiles`.`fname` LIKE \'%John%\' AND `Profiles`.`lname` LIKE \'%Smith%\' AND `Profiles`.`id` = \'10\' LIMIT 5,1');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5, 'fname' => 'John', 'lname' => '=Smith', 'id' => 10)), 'SELECT `Profiles`.`id` FROM `Profiles` WHERE `Profiles`.`fname` LIKE \'%John%\' AND `Profiles`.`lname` = \'Smith\' AND `Profiles`.`id` = \'10\' LIMIT 5,1');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5, 'fname' => 'John', 'lname' => '=Smith', 'id' => '10..20')), 'SELECT `Profiles`.`id` FROM `Profiles` WHERE `Profiles`.`fname` LIKE \'%John%\' AND `Profiles`.`lname` = \'Smith\' AND `Profiles`.`id` > \'10\' AND `Profiles`.`id` < \'20\' LIMIT 5,1');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5, 'fname' => 'John', 'lname' => '=Smith', 'id' => '>10')), 'SELECT `Profiles`.`id` FROM `Profiles` WHERE `Profiles`.`fname` LIKE \'%John%\' AND `Profiles`.`lname` = \'Smith\' AND `Profiles`.`id` > \'10\' LIMIT 5,1');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5, 'fname' => 'John', 'lname' => '=Smith', 'id' => '<10')), 'SELECT `Profiles`.`id` FROM `Profiles` WHERE `Profiles`.`fname` LIKE \'%John%\' AND `Profiles`.`lname` = \'Smith\' AND `Profiles`.`id` < \'10\' LIMIT 5,1');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5, 'fname' => 'John', 'lname' => '=Smith', 'id' => '!=10')), 'SELECT `Profiles`.`id` FROM `Profiles` WHERE `Profiles`.`fname` LIKE \'%John%\' AND `Profiles`.`lname` = \'Smith\' AND `Profiles`.`id` <> \'10\' LIMIT 5,1');
     $query = $builder->select(array('id'), array('-search' => 'foo'));
     //echo $query;
     $builder = new Dataface_QueryBuilder('Profiles', array('-skip' => 1, '-limit' => 1));
     $this->assertEquals($builder->select(array('id')), 'SELECT `Profiles`.`id` FROM `Profiles` LIMIT 1,1');
     $builder = new Dataface_QueryBuilder('Profiles', array('-limit' => 20));
     $this->assertEquals($builder->select(array('id')), 'SELECT `Profiles`.`id` FROM `Profiles` LIMIT 20');
     $builder = new Dataface_QueryBuilder('Profiles', array('-cursor' => 0));
     $res = $builder->select(array('photo'));
     $this->assertTrue(PEAR::isError($res), 'Trying to select blob column should return error');
     $builder = new Dataface_QueryBuilder('Profiles', array('-cursor' => 0));
     $builder->includeBlobs();
     $res = $builder->select(array('photo'));
     $this->assertTrue(!PEAR::isError($res), 'Trying to select blob column should return error');
     // Now test security features
     $builder->addSecurityConstraint('fname', '=John');
     $this->assertEquals($builder->select(array('id'), array('-limit' => 1, '-skip' => 5, 'fname' => 'John', 'lname' => '=Smith', 'id' => '!=10')), 'SELECT `Profiles`.`id` FROM `Profiles` WHERE `Profiles`.`fname` LIKE \'%John%\' AND `Profiles`.`lname` = \'Smith\' AND `Profiles`.`id` <> \'10\' AND `Profiles`.`fname` = \'John\' LIMIT 5,1');
 }