2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataQuery;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-11-13 08:35:43 +01:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2009-11-22 06:16:38 +01:00
|
|
|
class DataQueryTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
protected static $fixture_file = 'DataQueryTest.yml';
|
2012-12-10 06:11:07 +01:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
DataQueryTest\ObjectA::class,
|
|
|
|
DataQueryTest\ObjectB::class,
|
|
|
|
DataQueryTest\ObjectC::class,
|
|
|
|
DataQueryTest\ObjectD::class,
|
|
|
|
DataQueryTest\ObjectE::class,
|
|
|
|
DataQueryTest\ObjectF::class,
|
|
|
|
DataQueryTest\ObjectG::class,
|
2016-11-13 08:35:43 +01:00
|
|
|
SQLSelectTest\TestObject::class,
|
|
|
|
SQLSelectTest\TestBase::class,
|
|
|
|
SQLSelectTest\TestChild::class,
|
2012-12-10 06:11:07 +01:00
|
|
|
);
|
|
|
|
|
2014-08-26 04:56:45 +02:00
|
|
|
public function testSortByJoinedFieldRetainsSourceInformation() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$bar = new DataQueryTest\ObjectC();
|
2014-08-26 04:56:45 +02:00
|
|
|
$bar->Title = "Bar";
|
|
|
|
$bar->write();
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$foo = new DataQueryTest\ObjectB();
|
2014-08-26 04:56:45 +02:00
|
|
|
$foo->Title = "Foo";
|
|
|
|
$foo->TestC = $bar->ID;
|
|
|
|
$foo->write();
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectB::class);
|
2014-08-26 04:56:45 +02:00
|
|
|
$result = $query->leftJoin(
|
2016-11-13 08:35:43 +01:00
|
|
|
'DataQueryTest_C',
|
2014-08-26 04:56:45 +02:00
|
|
|
"\"DataQueryTest_B\".\"TestCID\" = \"DataQueryTest_B\".\"ID\""
|
|
|
|
)->sort('"DataQueryTest_B"."Title"', 'ASC');
|
|
|
|
|
|
|
|
$result = $result->execute()->record();
|
|
|
|
$this->assertEquals('Foo', $result['Title']);
|
|
|
|
}
|
|
|
|
|
2009-11-22 06:16:38 +01:00
|
|
|
/**
|
2011-10-29 06:11:27 +02:00
|
|
|
* Test the leftJoin() and innerJoin method of the DataQuery object
|
2009-11-22 06:16:38 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testJoins() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(Member::class);
|
2011-10-29 06:11:27 +02:00
|
|
|
$dq->innerJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains("INNER JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
|
|
|
|
$dq->sql($parameters));
|
2011-10-29 06:11:27 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(Member::class);
|
2011-10-29 06:11:27 +02:00
|
|
|
$dq->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains("LEFT JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
|
|
|
|
$dq->sql($parameters));
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
2012-06-25 01:34:02 +02:00
|
|
|
|
2015-03-02 10:52:39 +01:00
|
|
|
public function testApplyRelation() {
|
|
|
|
// Test applyRelation with two has_ones pointing to the same class
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(DataQueryTest\ObjectB::class);
|
2015-03-02 10:52:39 +01:00
|
|
|
$dq->applyRelation('TestC');
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertTrue($dq->query()->isJoinedTo('DataQueryTest_C'));
|
2015-03-02 10:52:39 +01:00
|
|
|
$this->assertContains('"DataQueryTest_C"."ID" = "DataQueryTest_B"."TestCID"', $dq->sql());
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(DataQueryTest\ObjectB::class);
|
2015-03-02 10:52:39 +01:00
|
|
|
$dq->applyRelation('TestCTwo');
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertTrue($dq->query()->isJoinedTo('DataQueryTest_C'));
|
2015-03-02 10:52:39 +01:00
|
|
|
$this->assertContains('"DataQueryTest_C"."ID" = "DataQueryTest_B"."TestCTwoID"', $dq->sql());
|
|
|
|
}
|
|
|
|
|
2014-11-10 17:32:58 +01:00
|
|
|
public function testApplyReplationDeepInheretence() {
|
2015-01-30 14:47:02 +01:00
|
|
|
//test has_one relation
|
2016-10-14 03:30:05 +02:00
|
|
|
$newDQ = new DataQuery(DataQueryTest\ObjectE::class);
|
2014-11-10 17:32:58 +01:00
|
|
|
//apply a relation to a relation from an ancestor class
|
|
|
|
$newDQ->applyRelation('TestA');
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertTrue($newDQ->query()->isJoinedTo('DataQueryTest_C'));
|
2014-11-18 23:05:07 +01:00
|
|
|
$this->assertContains('"DataQueryTest_A"."ID" = "DataQueryTest_C"."TestAID"', $newDQ->sql($params));
|
2015-01-30 14:47:02 +01:00
|
|
|
|
|
|
|
//test many_many relation
|
|
|
|
|
|
|
|
//test many_many with separate inheritance
|
2016-10-14 03:30:05 +02:00
|
|
|
$newDQ = new DataQuery(DataQueryTest\ObjectC::class);
|
|
|
|
$baseDBTable = DataObject::getSchema()->baseDataTable(DataQueryTest\ObjectC::class);
|
2015-01-30 14:47:02 +01:00
|
|
|
$newDQ->applyRelation('ManyTestAs');
|
|
|
|
//check we are "joined" to the DataObject's table (there is no distinction between FROM or JOIN clauses)
|
|
|
|
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
|
|
|
|
//check we are explicitly selecting "FROM" the DO's table
|
|
|
|
$this->assertContains("FROM \"$baseDBTable\"", $newDQ->sql());
|
|
|
|
|
|
|
|
//test many_many with shared inheritance
|
2016-10-14 03:30:05 +02:00
|
|
|
$newDQ = new DataQuery(DataQueryTest\ObjectE::class);
|
|
|
|
$baseDBTable = DataObject::getSchema()->baseDataTable(DataQueryTest\ObjectE::class);
|
2015-01-30 14:47:02 +01:00
|
|
|
//check we are "joined" to the DataObject's table (there is no distinction between FROM or JOIN clauses)
|
|
|
|
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
|
|
|
|
//check we are explicitly selecting "FROM" the DO's table
|
|
|
|
$this->assertContains("FROM \"$baseDBTable\"", $newDQ->sql(), 'The FROM clause is missing from the query');
|
|
|
|
$newDQ->applyRelation('ManyTestGs');
|
|
|
|
//confirm we are still joined to the base table
|
|
|
|
$this->assertTrue($newDQ->query()->isJoinedTo($baseDBTable));
|
|
|
|
//double check it is the "FROM" clause
|
|
|
|
$this->assertContains("FROM \"$baseDBTable\"", $newDQ->sql(), 'The FROM clause has been removed from the query');
|
|
|
|
//another (potentially less crude check) for checking "FROM" clause
|
|
|
|
$fromTables = $newDQ->query()->getFrom();
|
|
|
|
$this->assertEquals('"' . $baseDBTable . '"', $fromTables[$baseDBTable]);
|
2014-11-10 17:32:58 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRelationReturn() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(DataQueryTest\ObjectC::class);
|
|
|
|
$this->assertEquals(DataQueryTest\ObjectA::class, $dq->applyRelation('TestA'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(DataQueryTest\ObjectA::class, $dq->applyRelation('TestAs'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(DataQueryTest\ObjectA::class, $dq->applyRelation('ManyTestAs'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2012-06-25 01:34:02 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(DataQueryTest\ObjectB::class, $dq->applyRelation('TestB'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(DataQueryTest\ObjectB::class, $dq->applyRelation('TestBs'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->assertEquals(DataQueryTest\ObjectB::class, $dq->applyRelation('ManyTestBs'),
|
2012-09-26 23:34:00 +02:00
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2016-10-14 03:30:05 +02:00
|
|
|
$newDQ = new DataQuery(DataQueryTest\ObjectE::class);
|
|
|
|
$this->assertEquals(DataQueryTest\ObjectA::class, $newDQ->applyRelation('TestA'),
|
2014-11-10 17:32:58 +01:00
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2012-06-25 01:34:02 +02:00
|
|
|
}
|
2012-09-20 13:28:54 +02:00
|
|
|
|
2012-12-10 06:11:07 +01:00
|
|
|
public function testRelationOrderWithCustomJoin() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$dataQuery = new DataQuery(DataQueryTest\ObjectB::class);
|
2016-11-13 08:35:43 +01:00
|
|
|
$dataQuery->innerJoin('DataQueryTest_D', '"DataQueryTest_D"."RelationID" = "DataQueryTest_B"."ID"');
|
2012-12-10 06:11:07 +01:00
|
|
|
$dataQuery->execute();
|
|
|
|
}
|
2012-12-14 10:57:28 +01:00
|
|
|
|
2012-09-20 13:28:54 +02:00
|
|
|
public function testDisjunctiveGroup() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(DataQueryTest\ObjectA::class);
|
2012-09-20 13:28:54 +02:00
|
|
|
|
|
|
|
$dq->where('DataQueryTest_A.ID = 2');
|
|
|
|
$subDq = $dq->disjunctiveGroup();
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'John\'');
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'Bob\'');
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains(
|
2014-08-15 08:53:05 +02:00
|
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') OR (DataQueryTest_A.Name = 'Bob'))",
|
2013-06-21 00:32:08 +02:00
|
|
|
$dq->sql($parameters)
|
2012-10-03 18:08:34 +02:00
|
|
|
);
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConjunctiveGroup() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(DataQueryTest\ObjectA::class);
|
2012-09-20 13:28:54 +02:00
|
|
|
|
|
|
|
$dq->where('DataQueryTest_A.ID = 2');
|
|
|
|
$subDq = $dq->conjunctiveGroup();
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'John\'');
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'Bob\'');
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains(
|
2014-08-15 08:53:05 +02:00
|
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') AND (DataQueryTest_A.Name = 'Bob'))",
|
2013-06-21 00:32:08 +02:00
|
|
|
$dq->sql($parameters)
|
2012-10-03 18:08:34 +02:00
|
|
|
);
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* @todo Test paramaterised
|
|
|
|
*/
|
2012-09-20 13:28:54 +02:00
|
|
|
public function testNestedGroups() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(DataQueryTest\ObjectA::class);
|
2012-09-20 13:28:54 +02:00
|
|
|
|
|
|
|
$dq->where('DataQueryTest_A.ID = 2');
|
|
|
|
$subDq = $dq->disjunctiveGroup();
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'John\'');
|
|
|
|
$subSubDq = $subDq->conjunctiveGroup();
|
|
|
|
$subSubDq->where('DataQueryTest_A.Age = 18');
|
|
|
|
$subSubDq->where('DataQueryTest_A.Age = 50');
|
|
|
|
$subDq->where('DataQueryTest_A.Name = \'Bob\'');
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLContains(
|
2012-10-03 18:08:34 +02:00
|
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') OR ((DataQueryTest_A.Age = 18) "
|
2014-08-15 08:53:05 +02:00
|
|
|
. "AND (DataQueryTest_A.Age = 50)) OR (DataQueryTest_A.Name = 'Bob'))",
|
2013-06-21 00:32:08 +02:00
|
|
|
$dq->sql($parameters)
|
2012-10-03 18:08:34 +02:00
|
|
|
);
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmptySubgroup() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(DataQueryTest\ObjectA::class);
|
2012-09-20 13:28:54 +02:00
|
|
|
$dq->conjunctiveGroup();
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Empty groups should have no where condition at all
|
|
|
|
$this->assertSQLNotContains('WHERE', $dq->sql($parameters));
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubgroupHandoff() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$dq = new DataQuery(DataQueryTest\ObjectA::class);
|
2012-09-20 13:28:54 +02:00
|
|
|
$subDq = $dq->disjunctiveGroup();
|
|
|
|
|
|
|
|
$orgDq = clone $dq;
|
|
|
|
|
|
|
|
$subDq->sort('"DataQueryTest_A"."Name"');
|
|
|
|
$orgDq->sort('"DataQueryTest_A"."Name"');
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($dq->sql($parameters), $orgDq->sql($parameters));
|
2012-09-20 13:28:54 +02:00
|
|
|
|
|
|
|
$subDq->limit(5, 7);
|
|
|
|
$orgDq->limit(5, 7);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals($dq->sql($parameters), $orgDq->sql($parameters));
|
2012-09-20 13:28:54 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-03 03:20:31 +02:00
|
|
|
public function testOrderByMultiple() {
|
2016-11-13 08:35:43 +01:00
|
|
|
$dq = new DataQuery(SQLSelectTest\TestObject::class);
|
2013-10-03 03:20:31 +02:00
|
|
|
$dq = $dq->sort('"Name" ASC, MID("Name", 8, 1) DESC');
|
|
|
|
$this->assertContains(
|
2015-07-31 07:18:49 +02:00
|
|
|
'ORDER BY "SQLSelectTest_DO"."Name" ASC, "_SortColumn0" DESC',
|
2013-06-21 00:32:08 +02:00
|
|
|
$dq->sql($parameters)
|
2013-10-03 03:20:31 +02:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
public function testDefaultSort() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectE::class);
|
2014-04-08 22:46:52 +02:00
|
|
|
$result = $query->column('Title');
|
|
|
|
$this->assertEquals(array('First', 'Second', 'Last'), $result);
|
|
|
|
}
|
2014-09-03 08:53:47 +02:00
|
|
|
|
|
|
|
public function testDistinct() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectE::class);
|
2014-11-18 23:05:07 +01:00
|
|
|
$this->assertContains('SELECT DISTINCT', $query->sql($params), 'Query is set as distinct by default');
|
2014-09-03 08:53:47 +02:00
|
|
|
|
|
|
|
$query = $query->distinct(false);
|
2014-11-18 23:05:07 +01:00
|
|
|
$this->assertNotContains('SELECT DISTINCT', $query->sql($params), 'Query does not contain distinct');
|
2014-09-03 08:53:47 +02:00
|
|
|
|
|
|
|
$query = $query->distinct(true);
|
2014-11-18 23:05:07 +01:00
|
|
|
$this->assertContains('SELECT DISTINCT', $query->sql($params), 'Query contains distinct');
|
2014-08-05 04:46:06 +02:00
|
|
|
}
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2014-08-05 04:46:06 +02:00
|
|
|
public function testComparisonClauseInt() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"SortOrder\") VALUES (2)");
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectF::class);
|
2015-06-19 01:59:27 +02:00
|
|
|
$query->where(DB::get_conn()->comparisonClause('"SortOrder"', '2'));
|
2014-08-05 04:46:06 +02:00
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find SortOrder");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2014-08-05 04:46:06 +02:00
|
|
|
public function testComparisonClauseDateFull() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectF::class);
|
2015-06-19 01:59:27 +02:00
|
|
|
$query->where(DB::get_conn()->comparisonClause('"MyDate"', '1988-03-04%'));
|
2014-08-05 04:46:06 +02:00
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2014-08-05 04:46:06 +02:00
|
|
|
public function testComparisonClauseDateStartsWith() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectF::class);
|
2015-06-19 01:59:27 +02:00
|
|
|
$query->where(DB::get_conn()->comparisonClause('"MyDate"', '1988%'));
|
2014-08-05 04:46:06 +02:00
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2014-08-05 04:46:06 +02:00
|
|
|
public function testComparisonClauseDateStartsPartial() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectF::class);
|
2015-06-19 01:59:27 +02:00
|
|
|
$query->where(DB::get_conn()->comparisonClause('"MyDate"', '%03-04%'));
|
2014-08-05 04:46:06 +02:00
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2014-08-05 04:46:06 +02:00
|
|
|
public function testComparisonClauseTextCaseInsensitive() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyString\") VALUES ('HelloWorld')");
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectF::class);
|
2015-06-19 01:59:27 +02:00
|
|
|
$query->where(DB::get_conn()->comparisonClause('"MyString"', 'helloworld'));
|
2014-08-05 04:46:06 +02:00
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyString");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2014-08-05 04:46:06 +02:00
|
|
|
public function testComparisonClauseTextCaseSensitive() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyString\") VALUES ('HelloWorld')");
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectF::class);
|
2015-06-19 01:59:27 +02:00
|
|
|
$query->where(DB::get_conn()->comparisonClause('"MyString"', 'HelloWorld', false, false, true));
|
2014-08-05 04:46:06 +02:00
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyString");
|
2015-07-27 12:38:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$query2 = new DataQuery(DataQueryTest\ObjectF::class);
|
2015-06-19 01:59:27 +02:00
|
|
|
$query2->where(DB::get_conn()->comparisonClause('"MyString"', 'helloworld', false, false, true));
|
2014-08-05 04:46:06 +02:00
|
|
|
$this->assertEquals(0, $query2->count(), "Found mystring. Shouldn't be able too.");
|
|
|
|
$this->resetDBSchema(true);
|
2014-09-03 08:53:47 +02:00
|
|
|
}
|
|
|
|
|
2016-05-21 02:56:51 +02:00
|
|
|
/**
|
|
|
|
* Tests that getFinalisedQuery can include all tables
|
|
|
|
*/
|
|
|
|
public function testConditionsIncludeTables() {
|
|
|
|
// Including filter on parent table only doesn't pull in second
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectC::class);
|
2016-05-21 02:56:51 +02:00
|
|
|
$query->sort('"SortOrder"');
|
|
|
|
$query->where(array(
|
|
|
|
'"DataQueryTest_C"."Title" = ?' => array('First')
|
|
|
|
));
|
|
|
|
$result = $query->getFinalisedQuery(array('Title'));
|
|
|
|
$from = $result->getFrom();
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertContains('DataQueryTest_C', array_keys($from));
|
|
|
|
$this->assertNotContains('DataQueryTest_E', array_keys($from));
|
2016-05-21 02:56:51 +02:00
|
|
|
|
|
|
|
// Including filter on sub-table requires it
|
2016-10-14 03:30:05 +02:00
|
|
|
$query = new DataQuery(DataQueryTest\ObjectC::class);
|
2016-05-21 02:56:51 +02:00
|
|
|
$query->sort('"SortOrder"');
|
|
|
|
$query->where(array(
|
|
|
|
'"DataQueryTest_C"."Title" = ? OR "DataQueryTest_E"."SortOrder" > ?' => array(
|
|
|
|
'First', 2
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$result = $query->getFinalisedQuery(array('Title'));
|
|
|
|
$from = $result->getFrom();
|
|
|
|
|
|
|
|
// Check that including "SortOrder" prompted inclusion of DataQueryTest_E table
|
2016-11-13 08:35:43 +01:00
|
|
|
$this->assertContains('DataQueryTest_C', array_keys($from));
|
|
|
|
$this->assertContains('DataQueryTest_E', array_keys($from));
|
2016-05-21 02:56:51 +02:00
|
|
|
$arrayResult = iterator_to_array($result->execute());
|
|
|
|
$first = array_shift($arrayResult);
|
|
|
|
$this->assertNotNull($first);
|
|
|
|
$this->assertEquals('First', $first['Title']);
|
|
|
|
$second = array_shift($arrayResult);
|
|
|
|
$this->assertNotNull($second);
|
|
|
|
$this->assertEquals('Last', $second['Title']);
|
|
|
|
$this->assertEmpty(array_shift($arrayResult));
|
|
|
|
}
|
|
|
|
|
2012-06-25 01:34:02 +02:00
|
|
|
}
|