2008-08-09 06:38:44 +02:00
|
|
|
<?php
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2008-08-09 06:38:44 +02:00
|
|
|
class SQLQueryTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'SQLQueryTest.yml';
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'SQLQueryTest_DO',
|
2014-09-15 02:27:55 +02:00
|
|
|
'SQLQueryTestBase',
|
|
|
|
'SQLQueryTestChild'
|
2010-04-12 04:03:16 +02:00
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
protected $oldDeprecation = null;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->oldDeprecation = Deprecation::dump_settings();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
public function tearDown() {
|
|
|
|
Deprecation::restore_settings($this->oldDeprecation);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-07 19:55:37 +02:00
|
|
|
public function testCount() {
|
|
|
|
|
|
|
|
//basic counting
|
|
|
|
$qry = SQLQueryTest_DO::get()->dataQuery()->getFinalisedQuery();
|
|
|
|
$ids = $this->allFixtureIDs('SQLQueryTest_DO');
|
2016-05-10 12:34:44 +02:00
|
|
|
|
|
|
|
$count = $qry->count('"SQLQueryTest_DO"."ID"');
|
|
|
|
$this->assertEquals(count($ids), $count);
|
|
|
|
$this->assertInternalType("int", $count);
|
|
|
|
|
|
|
|
//test with `having`
|
|
|
|
if (DB::get_conn() instanceof MySQLDatabase) {
|
2017-02-08 03:44:55 +01:00
|
|
|
$qry->setSelect(array(
|
|
|
|
'Date' => 'MAX("Date")',
|
|
|
|
'Common' => '"Common"',
|
|
|
|
));
|
|
|
|
$qry->setGroupBy('"Common"');
|
2016-05-10 12:34:44 +02:00
|
|
|
$qry->setHaving('"Date" > 2012-02-01');
|
|
|
|
$count = $qry->count('"SQLQueryTest_DO"."ID"');
|
|
|
|
$this->assertEquals(1, $count);
|
|
|
|
$this->assertInternalType("int", $count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnlimitedRowCount() {
|
|
|
|
//basic counting
|
|
|
|
$qry = SQLQueryTest_DO::get()->dataQuery()->getFinalisedQuery();
|
|
|
|
$ids = $this->allFixtureIDs('SQLQueryTest_DO');
|
|
|
|
$qry->setLimit(1);
|
|
|
|
|
|
|
|
$count = $qry->unlimitedRowCount('"SQLQueryTest_DO"."ID"');
|
|
|
|
$this->assertEquals(count($ids), $count);
|
|
|
|
$this->assertInternalType("int", $count);
|
|
|
|
|
|
|
|
// Test without column - SQLSelect has different logic for this
|
|
|
|
$count = $qry->unlimitedRowCount();
|
|
|
|
$this->assertEquals(2, $count);
|
|
|
|
$this->assertInternalType("int", $count);
|
2015-05-07 19:55:37 +02:00
|
|
|
|
|
|
|
//test with `having`
|
2015-06-02 09:13:38 +02:00
|
|
|
if (DB::get_conn() instanceof MySQLDatabase) {
|
2015-05-07 19:55:37 +02:00
|
|
|
$qry->setHaving('"Date" > 2012-02-01');
|
2016-05-10 12:34:44 +02:00
|
|
|
$count = $qry->unlimitedRowCount('"SQLQueryTest_DO"."ID"');
|
|
|
|
$this->assertEquals(1, $count);
|
|
|
|
$this->assertInternalType("int", $count);
|
2015-05-07 19:55:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testEmptyQueryReturnsNothing() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('', $query->sql($parameters));
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelectFromBasicTable() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom('MyTable');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals("SELECT * FROM MyTable", $query->sql($parameters));
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->addFrom('MyJoin');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals("SELECT * FROM MyTable MyJoin", $query->sql($parameters));
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelectFromUserSpecifiedFields() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setSelect(array("Name", "Title", "Description"));
|
|
|
|
$query->setFrom("MyTable");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals("SELECT Name, Title, Description FROM MyTable", $query->sql($parameters));
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelectWithWhereClauseFilter() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setSelect(array("Name","Meta"));
|
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setWhere("Name = 'Name'");
|
|
|
|
$query->addWhere("Meta = 'Test'");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
|
|
|
"SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test')",
|
|
|
|
$query->sql($parameters)
|
|
|
|
);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelectWithConstructorParameters() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery(array("Foo", "Bar"), "FooBarTable");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals("SELECT Foo, Bar FROM FooBarTable", $query->sql($parameters));
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery(array("Foo", "Bar"), "FooBarTable", array("Foo = 'Boo'"));
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals("SELECT Foo, Bar FROM FooBarTable WHERE (Foo = 'Boo')", $query->sql($parameters));
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelectWithChainedMethods() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setSelect("Name","Meta")->setFrom("MyTable")->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
|
|
|
"SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test')",
|
|
|
|
$query->sql($parameters)
|
|
|
|
);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCanSortBy() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setSelect("Name","Meta")->setFrom("MyTable")->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'");
|
2010-04-13 04:16:50 +02:00
|
|
|
$this->assertTrue($query->canSortBy('Name ASC'));
|
|
|
|
$this->assertTrue($query->canSortBy('Name'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-01-18 03:03:44 +01:00
|
|
|
/**
|
|
|
|
* Test multiple order by SQL clauses.
|
|
|
|
*/
|
|
|
|
public function testAddOrderBy() {
|
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setSelect('ID', "Title")->setFrom('Page')->addOrderBy('(ID % 2) = 0', 'ASC')->addOrderBy('ID > 50', 'ASC');
|
|
|
|
$this->assertSQLEquals(
|
|
|
|
'SELECT ID, Title, (ID % 2) = 0 AS "_SortColumn0", ID > 50 AS "_SortColumn1" FROM Page ORDER BY "_SortColumn0" ASC, "_SortColumn1" ASC',
|
|
|
|
$query->sql($parameters)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelectWithChainedFilterParameters() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setSelect(array("Name","Meta"))->setFrom("MyTable");
|
|
|
|
$query->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'")->addWhere("Beta != 'Gamma'");
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2012-09-26 23:34:00 +02:00
|
|
|
"SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test') AND (Beta != 'Gamma')",
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters)
|
|
|
|
);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelectWithLimitClause() {
|
2014-08-15 08:53:05 +02:00
|
|
|
if(!(DB::get_conn() instanceof MySQLDatabase || DB::get_conn() instanceof SQLite3Database
|
2013-06-21 00:32:08 +02:00
|
|
|
|| DB::get_conn() instanceof PostgreSQLDatabase)) {
|
2012-05-03 09:34:16 +02:00
|
|
|
$this->markTestIncomplete();
|
2009-06-16 07:33:03 +02:00
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setLimit(99);
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals("SELECT * FROM MyTable LIMIT 99", $query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
// array limit with start (MySQL specific)
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setLimit(99, 97);
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals("SELECT * FROM MyTable LIMIT 99 OFFSET 97", $query->sql($parameters));
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSelectWithOrderbyClause() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy('MyName');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC', $query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy('MyName desc');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC', $query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy('MyName ASC, Color DESC');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC, Color DESC', $query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy('MyName ASC, Color');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC, Color ASC', $query->sql($parameters));
|
2008-08-09 07:57:44 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy(array('MyName' => 'desc'));
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC', $query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy(array('MyName' => 'desc', 'Color'));
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC, Color ASC', $query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy('implode("MyName","Color")');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2014-08-15 08:53:05 +02:00
|
|
|
'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy('implode("MyName","Color") DESC');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2012-09-26 23:34:00 +02:00
|
|
|
'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" DESC',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setOrderBy('RAND()');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2012-09-26 23:34:00 +02:00
|
|
|
'SELECT *, RAND() AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters));
|
2013-09-17 22:06:39 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-09-17 22:06:39 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->addFrom('INNER JOIN SecondTable USING (ID)');
|
|
|
|
$query->addFrom('INNER JOIN ThirdTable USING (ID)');
|
|
|
|
$query->setOrderBy('MyName');
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2013-09-17 22:06:39 +02:00
|
|
|
'SELECT * FROM MyTable '
|
|
|
|
. 'INNER JOIN SecondTable USING (ID) '
|
|
|
|
. 'INNER JOIN ThirdTable USING (ID) '
|
|
|
|
. 'ORDER BY MyName ASC',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters));
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
2012-06-29 09:40:28 +02:00
|
|
|
|
2013-06-06 15:18:01 +02:00
|
|
|
public function testNullLimit() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-06-06 15:18:01 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setLimit(null);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2013-06-06 15:18:01 +02:00
|
|
|
'SELECT * FROM MyTable',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters)
|
2013-06-06 15:18:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testZeroLimit() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-06-06 15:18:01 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setLimit(0);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2013-06-06 15:18:01 +02:00
|
|
|
'SELECT * FROM MyTable',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters)
|
2013-06-06 15:18:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testZeroLimitWithOffset() {
|
2014-08-15 08:53:05 +02:00
|
|
|
if(!(DB::get_conn() instanceof MySQLDatabase || DB::get_conn() instanceof SQLite3Database
|
2013-06-21 00:32:08 +02:00
|
|
|
|| DB::get_conn() instanceof PostgreSQLDatabase)) {
|
2013-09-13 05:50:36 +02:00
|
|
|
$this->markTestIncomplete();
|
|
|
|
}
|
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-06-06 15:18:01 +02:00
|
|
|
$query->setFrom("MyTable");
|
|
|
|
$query->setLimit(0, 99);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2013-06-06 15:18:01 +02:00
|
|
|
'SELECT * FROM MyTable LIMIT 0 OFFSET 99',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters)
|
2013-06-06 15:18:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-06-29 09:40:28 +02:00
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testNegativeLimit() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-06-29 09:40:28 +02:00
|
|
|
$query->setLimit(-10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testNegativeOffset() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-06-29 09:40:28 +02:00
|
|
|
$query->setLimit(1, -10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testNegativeOffsetAndLimit() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-06-29 09:40:28 +02:00
|
|
|
$query->setLimit(-10, -10);
|
|
|
|
}
|
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
public function testReverseOrderBy() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom('MyTable');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-15 10:34:10 +02:00
|
|
|
// default is ASC
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setOrderBy("Name");
|
2012-04-15 10:34:10 +02:00
|
|
|
$query->reverseOrderBy();
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY Name DESC',$query->sql($parameters));
|
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setOrderBy("Name DESC");
|
2012-04-15 10:34:10 +02:00
|
|
|
$query->reverseOrderBy();
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY Name ASC',$query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setOrderBy(array("Name" => "ASC"));
|
2012-04-15 10:34:10 +02:00
|
|
|
$query->reverseOrderBy();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY Name DESC',$query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setOrderBy(array("Name" => 'DESC', 'Color' => 'asc'));
|
2012-04-15 10:34:10 +02:00
|
|
|
$query->reverseOrderBy();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY Name ASC, Color DESC',$query->sql($parameters));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setOrderBy('implode("MyName","Color") DESC');
|
2012-04-15 10:34:10 +02:00
|
|
|
$query->reverseOrderBy();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals(
|
2012-09-26 23:34:00 +02:00
|
|
|
'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters));
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
2012-04-15 10:34:10 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFiltersOnID() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("ID = 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with simple unquoted column name"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-17 08:30:33 +02:00
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setWhere('"ID" = 5');
|
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with simple quoted column name"
|
|
|
|
);
|
|
|
|
|
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setWhere(array('"ID"' => 4));
|
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with parameterised quoted column name"
|
|
|
|
);
|
|
|
|
|
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setWhere(array('"ID" = ?' => 4));
|
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with parameterised quoted column name"
|
|
|
|
);
|
|
|
|
|
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setWhere('"ID" IN (5,4)');
|
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with WHERE ID IN"
|
|
|
|
);
|
|
|
|
|
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setWhere(array('"ID" IN ?' => array(1,2)));
|
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with parameterised WHERE ID IN"
|
|
|
|
);
|
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("ID=5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with simple unquoted column name and no spaces in equals sign"
|
|
|
|
);
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("Identifier = 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is false with custom column name (starting with 'id')"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("ParentID = 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is false with column name ending in 'ID'"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("MyTable.ID = 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with table and column name"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("MyTable.ID = 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnID(),
|
|
|
|
"filtersOnID() is true with table and quoted column name "
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFiltersOnFK() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("ID = 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$query->filtersOnFK(),
|
|
|
|
"filtersOnFK() is true with simple unquoted column name"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("Identifier = 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
$query->filtersOnFK(),
|
|
|
|
"filtersOnFK() is false with custom column name (starting with 'id')"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("MyTable.ParentID = 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnFK(),
|
|
|
|
"filtersOnFK() is true with table and column name"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setWhere("MyTable.`ParentID`= 5");
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$query->filtersOnFK(),
|
|
|
|
"filtersOnFK() is true with table and quoted column name "
|
|
|
|
);
|
|
|
|
}
|
2010-10-19 00:58:43 +02:00
|
|
|
|
|
|
|
public function testInnerJoin() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom('MyTable');
|
|
|
|
$query->addInnerJoin('MyOtherTable', 'MyOtherTable.ID = 2');
|
|
|
|
$query->addLeftJoin('MyLastTable', 'MyOtherTable.ID = MyLastTable.ID');
|
2010-10-19 00:58:43 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable '.
|
2011-10-29 06:06:42 +02:00
|
|
|
'INNER JOIN "MyOtherTable" ON MyOtherTable.ID = 2 '.
|
|
|
|
'LEFT JOIN "MyLastTable" ON MyOtherTable.ID = MyLastTable.ID',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters)
|
2010-10-19 00:58:43 +02:00
|
|
|
);
|
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom('MyTable');
|
|
|
|
$query->addInnerJoin('MyOtherTable', 'MyOtherTable.ID = 2', 'table1');
|
|
|
|
$query->addLeftJoin('MyLastTable', 'MyOtherTable.ID = MyLastTable.ID', 'table2');
|
2010-10-19 00:58:43 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertSQLEquals('SELECT * FROM MyTable '.
|
2010-10-19 00:58:43 +02:00
|
|
|
'INNER JOIN "MyOtherTable" AS "table1" ON MyOtherTable.ID = 2 '.
|
|
|
|
'LEFT JOIN "MyLastTable" AS "table2" ON MyOtherTable.ID = MyLastTable.ID',
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->sql($parameters)
|
2010-10-19 00:58:43 +02:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-17 03:27:28 +02:00
|
|
|
public function testJoinSubSelect() {
|
|
|
|
|
2015-03-18 04:09:09 +01:00
|
|
|
// Test sub-select works
|
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setFrom('"MyTable"');
|
|
|
|
$query->addInnerJoin('(SELECT * FROM "MyOtherTable")',
|
|
|
|
'"Mot"."MyTableID" = "MyTable"."ID"', 'Mot');
|
|
|
|
$query->addLeftJoin('(SELECT "MyLastTable"."MyOtherTableID", COUNT(1) as "MyLastTableCount" '
|
|
|
|
. 'FROM "MyLastTable" GROUP BY "MyOtherTableID")',
|
|
|
|
'"Mlt"."MyOtherTableID" = "Mot"."ID"', 'Mlt');
|
|
|
|
$query->setOrderBy('COALESCE("Mlt"."MyLastTableCount", 0) DESC');
|
2014-07-17 03:27:28 +02:00
|
|
|
|
2015-03-31 08:54:15 +02:00
|
|
|
$this->assertSQLEquals('SELECT *, COALESCE("Mlt"."MyLastTableCount", 0) AS "_SortColumn0" FROM "MyTable" '.
|
2015-03-18 04:09:09 +01:00
|
|
|
'INNER JOIN (SELECT * FROM "MyOtherTable") AS "Mot" ON "Mot"."MyTableID" = "MyTable"."ID" ' .
|
|
|
|
'LEFT JOIN (SELECT "MyLastTable"."MyOtherTableID", COUNT(1) as "MyLastTableCount" FROM "MyLastTable" '
|
|
|
|
. 'GROUP BY "MyOtherTableID") AS "Mlt" ON "Mlt"."MyOtherTableID" = "Mot"."ID" ' .
|
2014-07-17 03:27:28 +02:00
|
|
|
'ORDER BY "_SortColumn0" DESC',
|
2014-08-13 07:06:38 +02:00
|
|
|
$query->sql($parameters)
|
2014-07-17 03:27:28 +02:00
|
|
|
);
|
|
|
|
|
2015-03-18 04:09:09 +01:00
|
|
|
// Test that table names do not get mistakenly identified as sub-selects
|
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setFrom('"MyTable"');
|
|
|
|
$query->addInnerJoin('NewsArticleSelected', '"News"."MyTableID" = "MyTable"."ID"', 'News');
|
2015-03-31 08:54:15 +02:00
|
|
|
$this->assertSQLEquals(
|
2015-03-18 04:09:09 +01:00
|
|
|
'SELECT * FROM "MyTable" INNER JOIN "NewsArticleSelected" AS "News" ON '.
|
|
|
|
'"News"."MyTableID" = "MyTable"."ID"',
|
2014-07-17 03:27:28 +02:00
|
|
|
$query->sql()
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-22 04:34:06 +02:00
|
|
|
public function testSetWhereAny() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-05-03 09:34:16 +02:00
|
|
|
$query->setFrom('MyTable');
|
2011-12-09 14:09:07 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->setWhereAny(array(
|
|
|
|
'Monkey' => 'Chimp',
|
|
|
|
'Color' => 'Brown'
|
|
|
|
));
|
|
|
|
$sql = $query->sql($parameters);
|
|
|
|
$this->assertSQLEquals("SELECT * FROM MyTable WHERE ((Monkey = ?) OR (Color = ?))", $sql);
|
|
|
|
$this->assertEquals(array('Chimp', 'Brown'), $parameters);
|
2011-12-09 14:09:07 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-16 01:27:51 +01:00
|
|
|
public function testSelectFirst() {
|
|
|
|
// Test first from sequence
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-11-16 01:27:51 +01:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy('"Name"');
|
|
|
|
$result = $query->firstRow()->execute();
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$records = array();
|
2012-11-16 01:27:51 +01:00
|
|
|
foreach($result as $row) {
|
2014-02-10 01:32:39 +01:00
|
|
|
$records[] = $row;
|
2012-11-16 01:27:51 +01:00
|
|
|
}
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$this->assertCount(1, $records);
|
|
|
|
$this->assertEquals('Object 1', $records[0]['Name']);
|
|
|
|
|
2012-11-16 01:27:51 +01:00
|
|
|
// Test first from empty sequence
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-11-16 01:27:51 +01:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy('"Name"');
|
2013-06-21 00:32:08 +02:00
|
|
|
$query->setWhere(array('"Name"' => 'Nonexistent Object'));
|
2012-11-16 01:27:51 +01:00
|
|
|
$result = $query->firstRow()->execute();
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$records = array();
|
|
|
|
foreach($result as $row) {
|
|
|
|
$records[] = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertCount(0, $records);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-16 01:27:51 +01:00
|
|
|
// Test that given the last item, the 'first' in this list matches the last
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-11-16 01:27:51 +01:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy('"Name"');
|
|
|
|
$query->setLimit(1, 1);
|
|
|
|
$result = $query->firstRow()->execute();
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$records = array();
|
2012-11-16 01:27:51 +01:00
|
|
|
foreach($result as $row) {
|
2014-02-10 01:32:39 +01:00
|
|
|
$records[] = $row;
|
2012-11-16 01:27:51 +01:00
|
|
|
}
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$this->assertCount(1, $records);
|
|
|
|
$this->assertEquals('Object 2', $records[0]['Name']);
|
2012-11-16 01:27:51 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-16 01:27:51 +01:00
|
|
|
public function testSelectLast() {
|
|
|
|
// Test last in sequence
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-11-16 01:27:51 +01:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy('"Name"');
|
|
|
|
$result = $query->lastRow()->execute();
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$records = array();
|
2012-11-16 01:27:51 +01:00
|
|
|
foreach($result as $row) {
|
2014-02-10 01:32:39 +01:00
|
|
|
$records[] = $row;
|
2012-11-16 01:27:51 +01:00
|
|
|
}
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$this->assertCount(1, $records);
|
|
|
|
$this->assertEquals('Object 2', $records[0]['Name']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-16 01:27:51 +01:00
|
|
|
// Test last from empty sequence
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-11-16 01:27:51 +01:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy('"Name"');
|
|
|
|
$query->setWhere(array("\"Name\" = 'Nonexistent Object'"));
|
|
|
|
$result = $query->lastRow()->execute();
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$records = array();
|
|
|
|
foreach($result as $row) {
|
|
|
|
$records[] = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertCount(0, $records);
|
|
|
|
|
2012-11-16 01:27:51 +01:00
|
|
|
// Test that given the first item, the 'last' in this list matches the first
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-11-16 01:27:51 +01:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy('"Name"');
|
|
|
|
$query->setLimit(1);
|
|
|
|
$result = $query->lastRow()->execute();
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$records = array();
|
2012-11-16 01:27:51 +01:00
|
|
|
foreach($result as $row) {
|
2014-02-10 01:32:39 +01:00
|
|
|
$records[] = $row;
|
2012-11-16 01:27:51 +01:00
|
|
|
}
|
2014-02-10 01:32:39 +01:00
|
|
|
|
|
|
|
$this->assertCount(1, $records);
|
|
|
|
$this->assertEquals('Object 1', $records[0]['Name']);
|
2012-11-16 01:27:51 +01:00
|
|
|
}
|
2014-02-10 01:32:39 +01:00
|
|
|
|
2012-12-20 14:04:22 +01:00
|
|
|
/**
|
|
|
|
* Tests aggregate() function
|
|
|
|
*/
|
|
|
|
public function testAggregate() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery('"Common"');
|
2012-12-20 14:04:22 +01:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
2013-03-28 18:17:43 +01:00
|
|
|
$query->setGroupBy('"Common"');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-20 14:04:22 +01:00
|
|
|
$queryClone = $query->aggregate('COUNT(*)', 'cnt');
|
|
|
|
$result = $queryClone->execute();
|
|
|
|
$this->assertEquals(array(2), $result->column('cnt'));
|
|
|
|
}
|
2012-05-03 09:34:16 +02:00
|
|
|
|
2013-09-06 08:01:52 +02:00
|
|
|
/**
|
|
|
|
* Tests that an ORDER BY is only added if a LIMIT is set.
|
|
|
|
*/
|
|
|
|
public function testAggregateNoOrderByIfNoLimit() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-09-06 08:01:52 +02:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy('Common');
|
|
|
|
$query->setLimit(array());
|
|
|
|
|
|
|
|
$aggregate = $query->aggregate('MAX("ID")');
|
|
|
|
$limit = $aggregate->getLimit();
|
|
|
|
$this->assertEquals(array(), $aggregate->getOrderBy());
|
|
|
|
$this->assertEquals(array(), $limit);
|
|
|
|
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-09-06 08:01:52 +02:00
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy('Common');
|
|
|
|
$query->setLimit(2);
|
|
|
|
|
|
|
|
$aggregate = $query->aggregate('MAX("ID")');
|
|
|
|
$limit = $aggregate->getLimit();
|
|
|
|
$this->assertEquals(array('Common' => 'ASC'), $aggregate->getOrderBy());
|
|
|
|
$this->assertEquals(array('start' => 0, 'limit' => 2), $limit);
|
|
|
|
}
|
|
|
|
|
2012-12-20 03:52:46 +01:00
|
|
|
/**
|
|
|
|
* Test that "_SortColumn0" is added for an aggregate in the ORDER BY
|
|
|
|
* clause, in combination with a LIMIT and GROUP BY clause.
|
|
|
|
* For some databases, like MSSQL, this is a complicated scenario
|
|
|
|
* because a subselect needs to be done to query paginated data.
|
|
|
|
*/
|
|
|
|
public function testOrderByContainingAggregateAndLimitOffset() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2012-12-20 03:52:46 +01:00
|
|
|
$query->setSelect(array('"Name"', '"Meta"'));
|
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
2013-04-02 12:07:41 +02:00
|
|
|
$query->setOrderBy(array('MAX("Date")'));
|
2012-12-20 03:52:46 +01:00
|
|
|
$query->setGroupBy(array('"Name"', '"Meta"'));
|
|
|
|
$query->setLimit('1', '1');
|
|
|
|
|
|
|
|
$records = array();
|
|
|
|
foreach($query->execute() as $record) {
|
|
|
|
$records[] = $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertCount(1, $records);
|
|
|
|
|
|
|
|
$this->assertEquals('Object 2', $records[0]['Name']);
|
|
|
|
$this->assertEquals('2012-05-01 09:00:00', $records['0']['_SortColumn0']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-03 03:20:31 +02:00
|
|
|
/**
|
|
|
|
* Test that multiple order elements are maintained in the given order
|
|
|
|
*/
|
|
|
|
public function testOrderByMultiple() {
|
2013-06-21 00:32:08 +02:00
|
|
|
if(DB::get_conn() instanceof MySQLDatabase) {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-10-03 03:20:31 +02:00
|
|
|
$query->setSelect(array('"Name"', '"Meta"'));
|
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setOrderBy(array('MID("Name", 8, 1) DESC', '"Name" ASC'));
|
|
|
|
|
|
|
|
$records = array();
|
|
|
|
foreach($query->execute() as $record) {
|
|
|
|
$records[] = $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertCount(2, $records);
|
|
|
|
|
|
|
|
$this->assertEquals('Object 2', $records[0]['Name']);
|
|
|
|
$this->assertEquals('2', $records[0]['_SortColumn0']);
|
|
|
|
|
|
|
|
$this->assertEquals('Object 1', $records[1]['Name']);
|
|
|
|
$this->assertEquals('1', $records[1]['_SortColumn0']);
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 03:52:46 +01:00
|
|
|
|
2015-05-22 04:18:57 +02:00
|
|
|
public function testSelect() {
|
|
|
|
$query = new SQLQuery('"Title"', '"MyTable"');
|
|
|
|
$query->addSelect('"TestField"');
|
2015-06-02 10:19:12 +02:00
|
|
|
$this->assertSQLEquals(
|
2015-05-22 04:18:57 +02:00
|
|
|
'SELECT "Title", "TestField" FROM "MyTable"',
|
|
|
|
$query->sql()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test replacement of select
|
|
|
|
$query->setSelect(array(
|
|
|
|
'Field' => '"Field"',
|
|
|
|
'AnotherAlias' => '"AnotherField"'
|
|
|
|
));
|
2015-06-02 10:19:12 +02:00
|
|
|
$this->assertSQLEquals(
|
2015-05-22 04:18:57 +02:00
|
|
|
'SELECT "Field", "AnotherField" AS "AnotherAlias" FROM "MyTable"',
|
|
|
|
$query->sql()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check that ' as ' selects don't get mistaken as aliases
|
|
|
|
$query->addSelect(array(
|
|
|
|
'Relevance' => "MATCH (Title, MenuTitle) AGAINST ('Two as One')"
|
|
|
|
));
|
2015-06-02 10:19:12 +02:00
|
|
|
$this->assertSQLEquals(
|
2015-05-22 04:18:57 +02:00
|
|
|
'SELECT "Field", "AnotherField" AS "AnotherAlias", MATCH (Title, MenuTitle) AGAINST (' .
|
|
|
|
'\'Two as One\') AS "Relevance" FROM "MyTable"',
|
|
|
|
$query->sql()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-09-13 05:44:02 +02:00
|
|
|
/**
|
|
|
|
* Test passing in a LIMIT with OFFSET clause string.
|
|
|
|
*/
|
|
|
|
public function testLimitSetFromClauseString() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2013-09-13 05:44:02 +02:00
|
|
|
$query->setSelect('*');
|
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
|
|
|
|
$query->setLimit('20 OFFSET 10');
|
|
|
|
$limit = $query->getLimit();
|
|
|
|
$this->assertEquals(20, $limit['limit']);
|
|
|
|
$this->assertEquals(10, $limit['start']);
|
|
|
|
}
|
|
|
|
|
2014-09-15 02:27:55 +02:00
|
|
|
public function testParameterisedInnerJoins() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2014-09-15 02:27:55 +02:00
|
|
|
$query->setSelect(array('"SQLQueryTest_DO"."Name"', '"SubSelect"."Count"'));
|
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->addInnerJoin(
|
|
|
|
'(SELECT "Title", COUNT(*) AS "Count" FROM "SQLQueryTestBase" GROUP BY "Title" HAVING "Title" NOT LIKE ?)',
|
|
|
|
'"SQLQueryTest_DO"."Name" = "SubSelect"."Title"',
|
|
|
|
'SubSelect',
|
|
|
|
20,
|
|
|
|
array('%MyName%')
|
|
|
|
);
|
|
|
|
$query->addWhere(array('"SQLQueryTest_DO"."Date" > ?' => '2012-08-08 12:00'));
|
|
|
|
|
|
|
|
$this->assertSQLEquals('SELECT "SQLQueryTest_DO"."Name", "SubSelect"."Count"
|
|
|
|
FROM "SQLQueryTest_DO" INNER JOIN (SELECT "Title", COUNT(*) AS "Count" FROM "SQLQueryTestBase"
|
|
|
|
GROUP BY "Title" HAVING "Title" NOT LIKE ?) AS "SubSelect" ON "SQLQueryTest_DO"."Name" =
|
|
|
|
"SubSelect"."Title"
|
|
|
|
WHERE ("SQLQueryTest_DO"."Date" > ?)', $query->sql($parameters)
|
|
|
|
);
|
|
|
|
$this->assertEquals(array('%MyName%', '2012-08-08 12:00'), $parameters);
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testParameterisedLeftJoins() {
|
2014-12-03 21:20:39 +01:00
|
|
|
$query = new SQLQuery();
|
2014-09-15 02:27:55 +02:00
|
|
|
$query->setSelect(array('"SQLQueryTest_DO"."Name"', '"SubSelect"."Count"'));
|
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->addLeftJoin(
|
|
|
|
'(SELECT "Title", COUNT(*) AS "Count" FROM "SQLQueryTestBase" GROUP BY "Title" HAVING "Title" NOT LIKE ?)',
|
|
|
|
'"SQLQueryTest_DO"."Name" = "SubSelect"."Title"',
|
|
|
|
'SubSelect',
|
|
|
|
20,
|
|
|
|
array('%MyName%')
|
|
|
|
);
|
|
|
|
$query->addWhere(array('"SQLQueryTest_DO"."Date" > ?' => '2012-08-08 12:00'));
|
|
|
|
|
|
|
|
$this->assertSQLEquals('SELECT "SQLQueryTest_DO"."Name", "SubSelect"."Count"
|
|
|
|
FROM "SQLQueryTest_DO" LEFT JOIN (SELECT "Title", COUNT(*) AS "Count" FROM "SQLQueryTestBase"
|
|
|
|
GROUP BY "Title" HAVING "Title" NOT LIKE ?) AS "SubSelect" ON "SQLQueryTest_DO"."Name" =
|
|
|
|
"SubSelect"."Title"
|
|
|
|
WHERE ("SQLQueryTest_DO"."Date" > ?)', $query->sql($parameters)
|
|
|
|
);
|
|
|
|
$this->assertEquals(array('%MyName%', '2012-08-08 12:00'), $parameters);
|
|
|
|
$query->execute();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
/**
|
|
|
|
* Test deprecation of SQLQuery::getWhere working appropriately
|
|
|
|
*/
|
|
|
|
public function testDeprecatedGetWhere() {
|
|
|
|
// Temporarily disable deprecation
|
|
|
|
Deprecation::notification_version(null);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setSelect(array('"SQLQueryTest_DO"."Name"'));
|
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->addWhere(array(
|
|
|
|
'"SQLQueryTest_DO"."Date" > ?' => '2012-08-08 12:00'
|
|
|
|
));
|
|
|
|
$query->addWhere('"SQLQueryTest_DO"."Name" = \'Richard\'');
|
|
|
|
$query->addWhere(array(
|
|
|
|
'"SQLQueryTest_DO"."Meta" IN (?, \'Who?\', ?)' => array('Left', 'Right')
|
|
|
|
));
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
$expectedSQL = <<<EOS
|
|
|
|
SELECT "SQLQueryTest_DO"."Name"
|
|
|
|
FROM "SQLQueryTest_DO"
|
|
|
|
WHERE ("SQLQueryTest_DO"."Date" > ?)
|
|
|
|
AND ("SQLQueryTest_DO"."Name" = 'Richard')
|
|
|
|
AND ("SQLQueryTest_DO"."Meta" IN (?, 'Who?', ?))
|
|
|
|
EOS
|
|
|
|
;
|
|
|
|
$expectedParameters = array('2012-08-08 12:00', 'Left', 'Right');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
// Check sql evaluation of this query maintains the parameters
|
|
|
|
$sql = $query->sql($parameters);
|
|
|
|
$this->assertSQLEquals($expectedSQL, $sql);
|
|
|
|
$this->assertEquals($expectedParameters, $parameters);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
// Check that ->toAppropriateExpression()->setWhere doesn't modify the query
|
|
|
|
$query->setWhere($query->toAppropriateExpression()->getWhere());
|
|
|
|
$sql = $query->sql($parameters);
|
|
|
|
$this->assertSQLEquals($expectedSQL, $sql);
|
|
|
|
$this->assertEquals($expectedParameters, $parameters);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
// Check that getWhere are all flattened queries
|
|
|
|
$expectedFlattened = array(
|
|
|
|
'"SQLQueryTest_DO"."Date" > \'2012-08-08 12:00\'',
|
|
|
|
'"SQLQueryTest_DO"."Name" = \'Richard\'',
|
|
|
|
'"SQLQueryTest_DO"."Meta" IN (\'Left\', \'Who?\', \'Right\')'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedFlattened, $query->getWhere());
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
/**
|
|
|
|
* Test deprecation of SQLQuery::setDelete/getDelete
|
|
|
|
*/
|
|
|
|
public function testDeprecatedSetDelete() {
|
|
|
|
// Temporarily disable deprecation
|
|
|
|
Deprecation::notification_version(null);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
$query = new SQLQuery();
|
|
|
|
$query->setSelect(array('"SQLQueryTest_DO"."Name"'));
|
|
|
|
$query->setFrom('"SQLQueryTest_DO"');
|
|
|
|
$query->setWhere(array('"SQLQueryTest_DO"."Name"' => 'Andrew'));
|
|
|
|
|
|
|
|
// Check SQL for select
|
|
|
|
$this->assertSQLEquals(<<<EOS
|
|
|
|
SELECT "SQLQueryTest_DO"."Name" FROM "SQLQueryTest_DO"
|
|
|
|
WHERE ("SQLQueryTest_DO"."Name" = ?)
|
|
|
|
EOS
|
|
|
|
,
|
|
|
|
$query->sql($parameters)
|
|
|
|
);
|
|
|
|
$this->assertEquals(array('Andrew'), $parameters);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
// Check setDelete works
|
|
|
|
$query->setDelete(true);
|
|
|
|
$this->assertSQLEquals(<<<EOS
|
|
|
|
DELETE FROM "SQLQueryTest_DO"
|
|
|
|
WHERE ("SQLQueryTest_DO"."Name" = ?)
|
|
|
|
EOS
|
|
|
|
,
|
|
|
|
$query->sql($parameters)
|
|
|
|
);
|
|
|
|
$this->assertEquals(array('Andrew'), $parameters);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-15 08:35:01 +02:00
|
|
|
// Check that setDelete back to false restores the state
|
|
|
|
$query->setDelete(false);
|
|
|
|
$this->assertSQLEquals(<<<EOS
|
|
|
|
SELECT "SQLQueryTest_DO"."Name" FROM "SQLQueryTest_DO"
|
|
|
|
WHERE ("SQLQueryTest_DO"."Name" = ?)
|
|
|
|
EOS
|
|
|
|
,
|
|
|
|
$query->sql($parameters)
|
|
|
|
);
|
|
|
|
$this->assertEquals(array('Andrew'), $parameters);
|
|
|
|
}
|
2008-08-09 07:57:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SQLQueryTest_DO extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2008-08-09 07:57:44 +02:00
|
|
|
"Name" => "Varchar",
|
|
|
|
"Meta" => "Varchar",
|
2012-12-20 14:04:22 +01:00
|
|
|
"Common" => "Varchar",
|
2012-12-20 03:52:46 +01:00
|
|
|
"Date" => "SS_Datetime"
|
2008-08-09 07:57:44 +02:00
|
|
|
);
|
2008-08-09 06:38:44 +02:00
|
|
|
}
|
|
|
|
|
2012-12-10 06:11:07 +01:00
|
|
|
class SQLQueryTestBase extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-12-10 06:11:07 +01:00
|
|
|
"Title" => "Varchar",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class SQLQueryTestChild extends SQLQueryTestBase {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-12-10 06:11:07 +01:00
|
|
|
"Name" => "Varchar",
|
|
|
|
);
|
2012-02-12 21:22:11 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2012-12-10 06:11:07 +01:00
|
|
|
);
|
|
|
|
}
|