2009-11-22 06:16:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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(
|
|
|
|
'DataQueryTest_A',
|
|
|
|
'DataQueryTest_B',
|
2014-04-08 22:46:52 +02:00
|
|
|
'DataQueryTest_C',
|
2012-12-10 06:11:07 +01:00
|
|
|
'DataQueryTest_D',
|
2014-04-08 22:46:52 +02:00
|
|
|
'DataQueryTest_E',
|
2014-08-05 04:46:06 +02:00
|
|
|
'DataQueryTest_F',
|
2012-12-10 06:11:07 +01:00
|
|
|
);
|
|
|
|
|
2014-08-26 04:56:45 +02:00
|
|
|
|
|
|
|
public function testSortByJoinedFieldRetainsSourceInformation() {
|
|
|
|
$bar = new DataQueryTest_C();
|
|
|
|
$bar->Title = "Bar";
|
|
|
|
$bar->write();
|
|
|
|
|
|
|
|
$foo = new DataQueryTest_B();
|
|
|
|
$foo->Title = "Foo";
|
|
|
|
$foo->TestC = $bar->ID;
|
|
|
|
$foo->write();
|
|
|
|
|
|
|
|
$query = new DataQuery('DataQueryTest_B');
|
|
|
|
$result = $query->leftJoin(
|
|
|
|
'DataQueryTest_C',
|
|
|
|
"\"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() {
|
2009-11-22 06:16:38 +01:00
|
|
|
$dq = new DataQuery('Member');
|
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
|
|
|
|
|
|
|
$dq = new DataQuery('Member');
|
|
|
|
$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
|
|
|
|
2014-11-10 17:32:58 +01:00
|
|
|
public function testApplyReplationDeepInheretence() {
|
|
|
|
$newDQ = new DataQuery('DataQueryTest_E');
|
|
|
|
//apply a relation to a relation from an ancestor class
|
|
|
|
$newDQ->applyRelation('TestA');
|
|
|
|
$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));
|
2014-11-10 17:32:58 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRelationReturn() {
|
2012-06-25 01:34:02 +02:00
|
|
|
$dq = new DataQuery('DataQueryTest_C');
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('DataQueryTest_A', $dq->applyRelation('TestA'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
|
|
|
$this->assertEquals('DataQueryTest_A', $dq->applyRelation('TestAs'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
|
|
|
$this->assertEquals('DataQueryTest_A', $dq->applyRelation('ManyTestAs'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2012-06-25 01:34:02 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals('DataQueryTest_B', $dq->applyRelation('TestB'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
|
|
|
$this->assertEquals('DataQueryTest_B', $dq->applyRelation('TestBs'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
|
|
|
$this->assertEquals('DataQueryTest_B', $dq->applyRelation('ManyTestBs'),
|
|
|
|
'DataQuery::applyRelation should return the name of the related object.');
|
2014-11-10 17:32:58 +01:00
|
|
|
$newDQ = new DataQuery('DataQueryTest_E');
|
|
|
|
$this->assertEquals('DataQueryTest_A', $newDQ->applyRelation('TestA'),
|
|
|
|
'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() {
|
|
|
|
$dataQuery = new DataQuery('DataQueryTest_B');
|
|
|
|
$dataQuery->innerJoin('DataQueryTest_D', '"DataQueryTest_D"."RelationID" = "DataQueryTest_B"."ID"');
|
|
|
|
$dataQuery->execute();
|
|
|
|
}
|
2012-12-14 10:57:28 +01:00
|
|
|
|
2012-09-20 13:28:54 +02:00
|
|
|
public function testDisjunctiveGroup() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
|
|
|
|
$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() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
|
|
|
|
$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() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
|
|
|
|
$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() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
$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() {
|
|
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
|
|
$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() {
|
|
|
|
$dq = new DataQuery('SQLQueryTest_DO');
|
|
|
|
$dq = $dq->sort('"Name" ASC, MID("Name", 8, 1) DESC');
|
|
|
|
$this->assertContains(
|
2013-06-21 00:32:08 +02:00
|
|
|
'ORDER BY "SQLQueryTest_DO"."Name" ASC, "_SortColumn0" DESC',
|
|
|
|
$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() {
|
|
|
|
$query = new DataQuery('DataQueryTest_E');
|
|
|
|
$result = $query->column('Title');
|
|
|
|
$this->assertEquals(array('First', 'Second', 'Last'), $result);
|
|
|
|
}
|
2014-09-03 08:53:47 +02:00
|
|
|
|
|
|
|
public function testDistinct() {
|
|
|
|
$query = new DataQuery('DataQueryTest_E');
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function testComparisonClauseInt() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"SortOrder\") VALUES (2)");
|
|
|
|
$query = new DataQuery('DataQueryTest_F');
|
|
|
|
$query->where(DB::getConn()->comparisonClause('"SortOrder"', '2'));
|
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find SortOrder");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testComparisonClauseDateFull() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
|
|
|
|
$query = new DataQuery('DataQueryTest_F');
|
|
|
|
$query->where(DB::getConn()->comparisonClause('"MyDate"', '1988-03-04%'));
|
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testComparisonClauseDateStartsWith() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
|
|
|
|
$query = new DataQuery('DataQueryTest_F');
|
|
|
|
$query->where(DB::getConn()->comparisonClause('"MyDate"', '1988%'));
|
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testComparisonClauseDateStartsPartial() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyDate\") VALUES ('1988-03-04 06:30')");
|
|
|
|
$query = new DataQuery('DataQueryTest_F');
|
|
|
|
$query->where(DB::getConn()->comparisonClause('"MyDate"', '%03-04%'));
|
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyDate");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testComparisonClauseTextCaseInsensitive() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyString\") VALUES ('HelloWorld')");
|
|
|
|
$query = new DataQuery('DataQueryTest_F');
|
|
|
|
$query->where(DB::getConn()->comparisonClause('"MyString"', 'helloworld'));
|
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyString");
|
|
|
|
$this->resetDBSchema(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testComparisonClauseTextCaseSensitive() {
|
|
|
|
DB::query("INSERT INTO \"DataQueryTest_F\" (\"MyString\") VALUES ('HelloWorld')");
|
|
|
|
$query = new DataQuery('DataQueryTest_F');
|
|
|
|
$query->where(DB::getConn()->comparisonClause('"MyString"', 'HelloWorld', false, false, true));
|
|
|
|
$this->assertGreaterThan(0, $query->count(), "Couldn't find MyString");
|
|
|
|
|
|
|
|
$query2 = new DataQuery('DataQueryTest_F');
|
|
|
|
$query2->where(DB::getConn()->comparisonClause('"MyString"', 'helloworld', false, false, true));
|
|
|
|
$this->assertEquals(0, $query2->count(), "Found mystring. Shouldn't be able too.");
|
|
|
|
$this->resetDBSchema(true);
|
2014-09-03 08:53:47 +02:00
|
|
|
}
|
|
|
|
|
2012-06-25 01:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DataQueryTest_A extends DataObject implements TestOnly {
|
2014-08-26 04:56:45 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'Name' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'TestC' => 'DataQueryTest_C',
|
|
|
|
);
|
2009-11-22 06:16:38 +01:00
|
|
|
}
|
|
|
|
|
2014-08-26 04:56:45 +02:00
|
|
|
class DataQueryTest_B extends DataObject implements TestOnly {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'Title' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'TestC' => 'DataQueryTest_C',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DataQueryTest_C extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
private static $db = array(
|
|
|
|
'Title' => 'Varchar'
|
|
|
|
);
|
2012-12-10 06:11:07 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'TestA' => 'DataQueryTest_A',
|
|
|
|
'TestB' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'TestAs' => 'DataQueryTest_A',
|
|
|
|
'TestBs' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2012-06-25 01:34:02 +02:00
|
|
|
'ManyTestAs' => 'DataQueryTest_A',
|
|
|
|
'ManyTestBs' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
}
|
2012-12-10 06:11:07 +01:00
|
|
|
|
|
|
|
class DataQueryTest_D extends DataObject implements TestOnly {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-12-10 06:11:07 +01:00
|
|
|
'Relation' => 'DataQueryTest_B',
|
|
|
|
);
|
|
|
|
}
|
2014-04-08 22:46:52 +02:00
|
|
|
|
|
|
|
class DataQueryTest_E extends DataQueryTest_C implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
private static $db = array(
|
|
|
|
'SortOrder' => 'Int'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-08 22:46:52 +02:00
|
|
|
private static $default_sort = '"DataQueryTest_E"."SortOrder" ASC';
|
|
|
|
}
|
2014-08-05 04:46:06 +02:00
|
|
|
|
|
|
|
class DataQueryTest_F extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'SortOrder' => 'Int',
|
|
|
|
'MyDate' => 'SS_Datetime',
|
|
|
|
'MyString' => 'Text'
|
|
|
|
);
|
|
|
|
}
|