mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
d8e9af8af8
Database abstraction broken up into controller, connector, query builder, and schema manager, each independently configurable via YAML / Injector Creation of new DBQueryGenerator for database specific generation of SQL Support for parameterised queries, move of code base to use these over escaped conditions Refactor of SQLQuery into separate query classes for each of INSERT UPDATE DELETE and SELECT Support for PDO Installation process upgraded to use new ORM SS_DatabaseException created to handle database errors, maintaining details of raw sql and parameter details for user code designed interested in that data. Renamed DB static methods to conform correctly to naming conventions (e.g. DB::getConn -> DB::get_conn) 3.2 upgrade docs Performance Optimisation and simplification of code to use more concise API API Ability for database adapters to register extensions to ConfigureFromEnv.php
201 lines
5.8 KiB
PHP
201 lines
5.8 KiB
PHP
<?php
|
|
|
|
class DataQueryTest extends SapphireTest {
|
|
|
|
protected static $fixture_file = 'DataQueryTest.yml';
|
|
|
|
protected $extraDataObjects = array(
|
|
'DataQueryTest_A',
|
|
'DataQueryTest_B',
|
|
'DataQueryTest_C',
|
|
'DataQueryTest_D',
|
|
'DataQueryTest_E',
|
|
);
|
|
|
|
/**
|
|
* Test the leftJoin() and innerJoin method of the DataQuery object
|
|
*/
|
|
public function testJoins() {
|
|
$dq = new DataQuery('Member');
|
|
$dq->innerJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
|
|
$this->assertSQLContains("INNER JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
|
|
$dq->sql($parameters));
|
|
|
|
$dq = new DataQuery('Member');
|
|
$dq->leftJoin("Group_Members", "\"Group_Members\".\"MemberID\" = \"Member\".\"ID\"");
|
|
$this->assertSQLContains("LEFT JOIN \"Group_Members\" ON \"Group_Members\".\"MemberID\" = \"Member\".\"ID\"",
|
|
$dq->sql($parameters));
|
|
}
|
|
|
|
public function testRelationReturn() {
|
|
$dq = new DataQuery('DataQueryTest_C');
|
|
$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.');
|
|
|
|
$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.');
|
|
}
|
|
|
|
public function testRelationOrderWithCustomJoin() {
|
|
$dataQuery = new DataQuery('DataQueryTest_B');
|
|
$dataQuery->innerJoin('DataQueryTest_D', '"DataQueryTest_D"."RelationID" = "DataQueryTest_B"."ID"');
|
|
$dataQuery->execute();
|
|
}
|
|
|
|
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\'');
|
|
|
|
$this->assertSQLContains(
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') OR (DataQueryTest_A.Name = 'Bob'))",
|
|
$dq->sql($parameters)
|
|
);
|
|
}
|
|
|
|
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\'');
|
|
|
|
$this->assertSQLContains(
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') AND (DataQueryTest_A.Name = 'Bob'))",
|
|
$dq->sql($parameters)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @todo Test paramaterised
|
|
*/
|
|
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\'');
|
|
|
|
$this->assertSQLContains(
|
|
"WHERE (DataQueryTest_A.ID = 2) AND ((DataQueryTest_A.Name = 'John') OR ((DataQueryTest_A.Age = 18) "
|
|
. "AND (DataQueryTest_A.Age = 50)) OR (DataQueryTest_A.Name = 'Bob'))",
|
|
$dq->sql($parameters)
|
|
);
|
|
}
|
|
|
|
public function testEmptySubgroup() {
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
$dq->conjunctiveGroup();
|
|
|
|
// Empty groups should have no where condition at all
|
|
$this->assertSQLNotContains('WHERE', $dq->sql($parameters));
|
|
}
|
|
|
|
public function testSubgroupHandoff() {
|
|
$dq = new DataQuery('DataQueryTest_A');
|
|
$subDq = $dq->disjunctiveGroup();
|
|
|
|
$orgDq = clone $dq;
|
|
|
|
$subDq->sort('"DataQueryTest_A"."Name"');
|
|
$orgDq->sort('"DataQueryTest_A"."Name"');
|
|
|
|
$this->assertSQLEquals($dq->sql($parameters), $orgDq->sql($parameters));
|
|
|
|
$subDq->limit(5, 7);
|
|
$orgDq->limit(5, 7);
|
|
|
|
$this->assertSQLEquals($dq->sql($parameters), $orgDq->sql($parameters));
|
|
}
|
|
|
|
public function testOrderByMultiple() {
|
|
$dq = new DataQuery('SQLQueryTest_DO');
|
|
$dq = $dq->sort('"Name" ASC, MID("Name", 8, 1) DESC');
|
|
$this->assertContains(
|
|
'ORDER BY "SQLQueryTest_DO"."Name" ASC, "_SortColumn0" DESC',
|
|
$dq->sql($parameters)
|
|
);
|
|
}
|
|
|
|
public function testDefaultSort() {
|
|
$query = new DataQuery('DataQueryTest_E');
|
|
$result = $query->column('Title');
|
|
$this->assertEquals(array('First', 'Second', 'Last'), $result);
|
|
}
|
|
}
|
|
|
|
|
|
class DataQueryTest_A extends DataObject implements TestOnly {
|
|
private static $db = array(
|
|
'Name' => 'Varchar',
|
|
);
|
|
|
|
private static $has_one = array(
|
|
'TestC' => 'DataQueryTest_C',
|
|
);
|
|
}
|
|
|
|
class DataQueryTest_B extends DataQueryTest_A {
|
|
private static $db = array(
|
|
'Title' => 'Varchar',
|
|
);
|
|
|
|
private static $has_one = array(
|
|
'TestC' => 'DataQueryTest_C',
|
|
);
|
|
}
|
|
|
|
class DataQueryTest_C extends DataObject implements TestOnly {
|
|
|
|
private static $db = array(
|
|
'Title' => 'Varchar'
|
|
);
|
|
|
|
private static $has_one = array(
|
|
'TestA' => 'DataQueryTest_A',
|
|
'TestB' => 'DataQueryTest_B',
|
|
);
|
|
|
|
private static $has_many = array(
|
|
'TestAs' => 'DataQueryTest_A',
|
|
'TestBs' => 'DataQueryTest_B',
|
|
);
|
|
|
|
private static $many_many = array(
|
|
'ManyTestAs' => 'DataQueryTest_A',
|
|
'ManyTestBs' => 'DataQueryTest_B',
|
|
);
|
|
}
|
|
|
|
class DataQueryTest_D extends DataObject implements TestOnly {
|
|
|
|
private static $has_one = array(
|
|
'Relation' => 'DataQueryTest_B',
|
|
);
|
|
}
|
|
|
|
class DataQueryTest_E extends DataQueryTest_C implements TestOnly {
|
|
|
|
private static $db = array(
|
|
'SortOrder' => 'Int'
|
|
);
|
|
|
|
private static $default_sort = '"DataQueryTest_E"."SortOrder" ASC';
|
|
}
|