2008-08-09 06:38:44 +02:00
< ? php
class SQLQueryTest extends SapphireTest {
static $fixture_file = null ;
2010-04-12 04:03:16 +02:00
protected $extraDataObjects = array (
'SQLQueryTest_DO' ,
);
2008-08-09 06:38:44 +02:00
function testEmptyQueryReturnsNothing () {
$query = new SQLQuery ();
$this -> assertEquals ( '' , $query -> sql ());
}
function testSelectFromBasicTable () {
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$this -> assertEquals ( " SELECT * FROM MyTable " , $query -> sql ());
$query -> from [] = " MyJoin " ;
2008-08-09 06:53:34 +02:00
$this -> assertEquals ( " SELECT * FROM MyTable MyJoin " , $query -> sql ());
2008-08-09 06:38:44 +02:00
}
function testSelectFromUserSpecifiedFields () {
$query = new SQLQuery ();
2012-05-01 06:42:14 +02:00
$query -> select ( array ( " Name " , " Title " , " Description " ));
2008-08-09 06:38:44 +02:00
$query -> from [] = " MyTable " ;
$this -> assertEquals ( " SELECT Name, Title, Description FROM MyTable " , $query -> sql ());
}
function testSelectWithWhereClauseFilter () {
$query = new SQLQuery ();
2012-05-01 06:42:14 +02:00
$query -> select ( array ( " Name " , " Meta " ));
2008-08-09 06:38:44 +02:00
$query -> from [] = " MyTable " ;
$query -> where [] = " Name = 'Name' " ;
$query -> where [] = " Meta = 'Test' " ;
$this -> assertEquals ( " SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test') " , $query -> sql ());
}
function testSelectWithConstructorParameters () {
$query = new SQLQuery ( array ( " Foo " , " Bar " ), " FooBarTable " );
$this -> assertEquals ( " SELECT Foo, Bar FROM FooBarTable " , $query -> sql ());
$query = new SQLQuery ( array ( " Foo " , " Bar " ), " FooBarTable " , array ( " Foo = 'Boo' " ));
$this -> assertEquals ( " SELECT Foo, Bar FROM FooBarTable WHERE (Foo = 'Boo') " , $query -> sql ());
}
function testSelectWithChainedMethods () {
$query = new SQLQuery ();
$query -> select ( " Name " , " Meta " ) -> from ( " MyTable " ) -> where ( " Name " , " Name " ) -> where ( " Meta " , " Test " );
$this -> assertEquals ( " SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test') " , $query -> sql ());
}
2010-04-13 04:16:50 +02:00
function testCanSortBy () {
$query = new SQLQuery ();
$query -> select ( " Name " , " Meta " ) -> from ( " MyTable " ) -> where ( " Name " , " Name " ) -> where ( " Meta " , " Test " );
$this -> assertTrue ( $query -> canSortBy ( 'Name ASC' ));
$this -> assertTrue ( $query -> canSortBy ( 'Name' ));
}
2008-08-09 06:38:44 +02:00
function testSelectWithChainedFilterParameters () {
$query = new SQLQuery ();
$query -> select ( array ( " Name " , " Meta " )) -> from ( " MyTable " );
$query -> where ( " Name = 'Name' " ) -> where ( " Meta " , " Test " ) -> where ( " Beta " , " != " , " Gamma " );
$this -> assertEquals ( " SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test') AND (Beta != 'Gamma') " , $query -> sql ());
}
function testSelectWithPredicateFilters () {
2011-03-21 09:37:55 +01:00
/* this is no longer part of this
2008-08-09 06:38:44 +02:00
$query = new SQLQuery ();
2008-08-09 07:57:44 +02:00
$query -> select ( array ( " Name " )) -> from ( " SQLQueryTest_DO " );
2008-08-09 06:38:44 +02:00
$match = new ExactMatchFilter ( " Name " , " Value " );
2008-08-09 07:57:44 +02:00
$match -> setModel ( 'SQLQueryTest_DO' );
2008-08-09 06:38:44 +02:00
$match -> apply ( $query );
2008-08-09 07:57:44 +02:00
2008-08-09 06:38:44 +02:00
$match = new PartialMatchFilter ( " Meta " , " Value " );
2008-08-09 07:57:44 +02:00
$match -> setModel ( 'SQLQueryTest_DO' );
2008-08-09 06:38:44 +02:00
$match -> apply ( $query );
2008-08-09 07:57:44 +02:00
2009-03-29 23:50:47 +02:00
$this -> assertEquals ( " SELECT Name FROM SQLQueryTest_DO WHERE ( \" SQLQueryTest_DO \" . \" Name \" = 'Value') AND ( \" SQLQueryTest_DO \" . \" Meta \" LIKE '%Value%') " , $query -> sql ());
2011-03-21 09:37:55 +01:00
*/
2008-08-09 06:38:44 +02:00
}
function testSelectWithLimitClause () {
2009-06-16 07:33:03 +02:00
// These are MySQL specific :-S
if ( DB :: getConn () instanceof MySQLDatabase ) {
// numeric limit
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
2012-03-09 02:02:37 +01:00
$query -> limit ( 99 );
2009-06-16 07:33:03 +02:00
$this -> assertEquals ( " SELECT * FROM MyTable LIMIT 99 " , $query -> sql ());
2008-08-09 07:57:44 +02:00
2009-06-16 07:33:03 +02:00
// array limit with start (MySQL specific)
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
2012-03-09 02:02:37 +01:00
$query -> limit ( 99 , 97 );
2009-06-16 07:33:03 +02:00
$this -> assertEquals ( " SELECT * FROM MyTable LIMIT 99 OFFSET 97 " , $query -> sql ());
}
2008-08-09 07:57:44 +02:00
}
function testSelectWithOrderbyClause () {
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
2012-04-15 10:34:10 +02:00
$query -> orderby ( 'MyName' );
2012-05-01 07:09:57 +02:00
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY MyName ASC' , $query -> sql ());
2008-08-09 07:57:44 +02:00
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
2012-04-15 10:34:10 +02:00
$query -> orderby ( 'MyName desc' );
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY MyName DESC' , $query -> sql ());
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( 'MyName ASC, Color DESC' );
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY MyName ASC, Color DESC' , $query -> sql ());
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( 'MyName ASC, Color' );
2012-05-01 07:09:57 +02:00
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY MyName ASC, Color ASC' , $query -> sql ());
2008-08-09 07:57:44 +02:00
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
2012-04-15 10:34:10 +02:00
$query -> orderby ( array ( 'MyName' => 'desc' ));
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY MyName DESC' , $query -> sql ());
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( array ( 'MyName' => 'desc' , 'Color' ));
2012-05-01 07:09:57 +02:00
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY MyName DESC, Color ASC' , $query -> sql ());
2012-04-15 10:34:10 +02:00
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( 'implode("MyName","Color")' );
2012-05-01 07:09:57 +02:00
$this -> assertEquals ( 'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY _SortColumn0 ASC' , $query -> sql ());
2012-04-15 10:34:10 +02:00
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( 'implode("MyName","Color") DESC' );
2012-05-01 07:09:57 +02:00
$this -> assertEquals ( 'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY _SortColumn0 DESC' , $query -> sql ());
2012-04-15 10:34:10 +02:00
$query = new SQLQuery ();
$query -> from [] = " MyTable " ;
$query -> orderby ( 'RAND()' );
2012-05-01 07:09:57 +02:00
$this -> assertEquals ( 'SELECT *, RAND() AS "_SortColumn0" FROM MyTable ORDER BY _SortColumn0 ASC' , $query -> sql ());
2008-08-09 06:38:44 +02:00
}
2012-04-15 10:34:10 +02:00
public function testReverseOrderBy () {
$query = new SQLQuery ();
$query -> from ( 'MyTable' );
// default is ASC
$query -> orderby ( " Name " );
$query -> reverseOrderBy ();
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY Name DESC' , $query -> sql ());
$query -> orderby ( " Name DESC " );
$query -> reverseOrderBy ();
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY Name ASC' , $query -> sql ());
$query -> orderby ( array ( " Name " => " ASC " ));
$query -> reverseOrderBy ();
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY Name DESC' , $query -> sql ());
$query -> orderby ( array ( " Name " => 'DESC' , 'Color' => 'asc' ));
$query -> reverseOrderBy ();
$this -> assertEquals ( 'SELECT * FROM MyTable ORDER BY Name ASC, Color DESC' , $query -> sql ());
$query -> orderby ( 'implode("MyName","Color") DESC' );
$query -> reverseOrderBy ();
2012-05-01 07:09:57 +02:00
$this -> assertEquals ( 'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY _SortColumn0 ASC' , $query -> sql ());
2008-08-09 07:57:44 +02:00
}
2012-04-15 10:34:10 +02:00
2009-01-10 12:35:50 +01:00
function testFiltersOnID () {
$query = new SQLQuery ();
$query -> where [] = " ID = 5 " ;
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with simple unquoted column name "
);
$query = new SQLQuery ();
$query -> where [] = " ID=5 " ;
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with simple unquoted column name and no spaces in equals sign "
);
/*
$query = new SQLQuery ();
$query -> where [] = " Foo='Bar' AND ID=5 " ;
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with combined SQL statements "
);
*/
$query = new SQLQuery ();
$query -> where [] = " Identifier = 5 " ;
$this -> assertFalse (
$query -> filtersOnID (),
" filtersOnID() is false with custom column name (starting with 'id') "
);
$query = new SQLQuery ();
$query -> where [] = " ParentID = 5 " ;
$this -> assertFalse (
$query -> filtersOnID (),
" filtersOnID() is false with column name ending in 'ID' "
);
$query = new SQLQuery ();
$query -> where [] = " MyTable.ID = 5 " ;
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with table and column name "
);
$query = new SQLQuery ();
2009-09-30 00:03:30 +02:00
$query -> where [] = " MyTable.ID= 5 " ;
2009-01-10 12:35:50 +01:00
$this -> assertTrue (
$query -> filtersOnID (),
" filtersOnID() is true with table and quoted column name "
);
}
function testFiltersOnFK () {
$query = new SQLQuery ();
$query -> where [] = " ID = 5 " ;
$this -> assertFalse (
$query -> filtersOnFK (),
" filtersOnFK() is true with simple unquoted column name "
);
$query = new SQLQuery ();
$query -> where [] = " Identifier = 5 " ;
$this -> assertFalse (
$query -> filtersOnFK (),
" filtersOnFK() is false with custom column name (starting with 'id') "
);
$query = new SQLQuery ();
$query -> where [] = " MyTable.ParentID = 5 " ;
$this -> assertTrue (
$query -> filtersOnFK (),
" filtersOnFK() is true with table and column name "
);
$query = new SQLQuery ();
$query -> where [] = " MyTable.`ParentID`= 5 " ;
$this -> assertTrue (
$query -> filtersOnFK (),
" filtersOnFK() is true with table and quoted column name "
);
}
2010-10-19 00:58:43 +02:00
public function testInnerJoin () {
$query = new SQLQuery ();
$query -> from ( 'MyTable' );
$query -> innerJoin ( 'MyOtherTable' , 'MyOtherTable.ID = 2' );
$query -> leftJoin ( 'MyLastTable' , 'MyOtherTable.ID = MyLastTable.ID' );
$this -> assertEquals ( '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' ,
2010-10-19 00:58:43 +02:00
$query -> sql ()
);
$query = new SQLQuery ();
$query -> from ( 'MyTable' );
$query -> innerJoin ( 'MyOtherTable' , 'MyOtherTable.ID = 2' , 'table1' );
$query -> leftJoin ( 'MyLastTable' , 'MyOtherTable.ID = MyLastTable.ID' , 'table2' );
$this -> assertEquals ( 'SELECT * FROM MyTable ' .
'INNER JOIN "MyOtherTable" AS "table1" ON MyOtherTable.ID = 2 ' .
'LEFT JOIN "MyLastTable" AS "table2" ON MyOtherTable.ID = MyLastTable.ID' ,
$query -> sql ()
);
}
2011-12-09 14:09:07 +01:00
public function testWhereAny () {
$query = new SQLQuery ();
$query -> from ( 'MyTable' );
$query -> whereAny ( array ( " Monkey = 'Chimp' " , " Color = 'Brown' " ));
$this -> assertEquals ( " SELECT * FROM MyTable WHERE (Monkey = 'Chimp' OR Color = 'Brown') " , $query -> sql ());
}
2008-08-09 07:57:44 +02:00
}
class SQLQueryTest_DO extends DataObject implements TestOnly {
static $db = array (
" Name " => " Varchar " ,
" Meta " => " Varchar " ,
);
2008-08-09 06:38:44 +02:00
}
2012-02-12 21:22:11 +01:00