mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Moved deprecation of SQLQuery to 4.0
This commit is contained in:
parent
2aa1c1e11e
commit
19549d620f
@ -1819,7 +1819,7 @@ class LeftAndMainMarkingFilter {
|
||||
}
|
||||
}
|
||||
|
||||
return new SQLSelect(
|
||||
return new SQLQuery(
|
||||
array("ParentID", "ID"),
|
||||
'SiteTree',
|
||||
$where
|
||||
|
@ -144,9 +144,9 @@ class PaginatedList extends SS_ListDecorator {
|
||||
* Sets the page length, page start and total items from a query object's
|
||||
* limit, offset and unlimited count. The query MUST have a limit clause.
|
||||
*
|
||||
* @param SQLSelect $query
|
||||
* @param SQLQuery $query
|
||||
*/
|
||||
public function setPaginationFromQuery(SQLSelect $query) {
|
||||
public function setPaginationFromQuery(SQLQuery $query) {
|
||||
if ($limit = $query->getLimit()) {
|
||||
$this->setPageLength($limit['limit']);
|
||||
$this->setPageStart($limit['start']);
|
||||
|
@ -132,7 +132,7 @@ To set it back to be non-HTTP only, you need to set the `$httpOnly` argument to
|
||||
* Implementation of a parameterised query framework eliminating the need to manually escape variables for
|
||||
use in SQL queries. This has been integrated into nearly every level of the database ORM.
|
||||
* Refactor of database connectivity classes into separate components linked together through dependency injection
|
||||
* Refactor of `SQLQuery` into separate objects for each query type: `SQLSelect`, `SQLDelete`, `SQLUpdate` and `SQLInsert`
|
||||
* Refactor of `SQLQuery` into separate objects for each query type: `SQLQuery`, `SQLDelete`, `SQLUpdate` and `SQLInsert`
|
||||
* Rename of API methods to conform to coding conventions
|
||||
* PDO is now a standard connector, and is available for all database interfaces
|
||||
* Additional database and query generation tools
|
||||
@ -156,18 +156,18 @@ To set it back to be non-HTTP only, you need to set the `$httpOnly` argument to
|
||||
|
||||
### Update code that uses SQLQuery
|
||||
|
||||
SQLQuery is still implemented, but now extends the new SQLSelect class and has some methods
|
||||
deprecated. Previously this class was used for both selecting and deleting, but these
|
||||
have been superceded by the specialised SQLSelect and SQLDelete classes. Additionally,
|
||||
3.2 now provides SQLUpdate and SQLInsert to generate parameterised query friendly
|
||||
SQLQuery has been changed. Previously this class was used for both selecting and deleting, but
|
||||
deletion is now handled by the new SQLDelete class.
|
||||
|
||||
Additionally, 3.2 now provides SQLUpdate and SQLInsert to generate parameterised query friendly
|
||||
data updates.
|
||||
|
||||
SQLSelect, SQLDelete and SQLUpdate all inherit from SQLConditionalExpression, which
|
||||
SQLQuery, SQLDelete and SQLUpdate all inherit from SQLConditionalExpression, which
|
||||
implements toSelect, toDelete, and toUpdate to generate basic transformations
|
||||
between query types.
|
||||
|
||||
In the past SQLQuery->setDelete(true) would be used to turn a select into a delete,
|
||||
although now a new SQLDelete object should be created from a separate SQLSelect.
|
||||
although now a new SQLDelete object should be created from the original SQLQuery.
|
||||
|
||||
Before:
|
||||
|
||||
@ -192,24 +192,18 @@ Alternatively:
|
||||
|
||||
:::php
|
||||
<?php
|
||||
$query = SQLSelect::create()
|
||||
$query = SQLQuery::create()
|
||||
->setFrom('"SiteTree"')
|
||||
->setWhere(array('"SiteTree"."ShowInMenus"' => 0))
|
||||
->toDelete();
|
||||
$query->execute();
|
||||
|
||||
Also, take care for any code or functions which expect an object of type `SQLQuery`, as
|
||||
these references should be replaced with `SQLSelect`. Legacy code which generates
|
||||
`SQLQuery` can still communicate with new code that expects `SQLSelect` as it is a
|
||||
subclass of `SQLSelect`, but the inverse is not true.
|
||||
|
||||
### Update code that interacts with SQL strings to use parameters
|
||||
|
||||
The Silverstripe ORM (object relation model) has moved from using escaped SQL strings
|
||||
to query the database, to a combination of parameterised SQL expressions alongside
|
||||
a related list of parameter values. As a result of this, it is necessary to assume
|
||||
that any `SQLSelect` object (previously `SQLQuery`) may, and will usually, have
|
||||
un-injected parameters.
|
||||
that any `SQLQuery` object may, and will usually, have un-injected parameters.
|
||||
|
||||
All database queries performed through `DataList`, `DataQuery` and `SQLQuery` will continue
|
||||
to work, as will those through `DataObject::get()` (which returns a filterable `DataList`).
|
||||
@ -250,49 +244,7 @@ Examples of areas where queries should be upgraded are below:
|
||||
array($parentTitle)
|
||||
);
|
||||
|
||||
2. #### Querying the database through `SQLQuery` (deprecated)
|
||||
|
||||
Before:
|
||||
|
||||
Note: Use of SQLQuery would generate a deprecation notice if left un-upgraded.
|
||||
|
||||
:::php
|
||||
<?php
|
||||
|
||||
$query = new SQLQuery('*', '"SiteTree"', "\"URLSegment\" = '".Convert::raw2sql($testURL)."'");
|
||||
|
||||
$query->addWhere(array(
|
||||
'"ParentID" = \''.intval($parentID).'\'',
|
||||
'"ID" IN (SELECT "PageID" FROM "MyObject")'
|
||||
));
|
||||
|
||||
$query->addWhere("\"Title\" LIKE '%".Convert::raw2sql($myText)."' OR \"Title\" LIKE '".Convert::raw2sql($myText)."%'");
|
||||
|
||||
After, substituting `SQLSelect` for the deprecated `SQLQuery`:
|
||||
|
||||
Note: The inclusion of properly ANSI quoted symbols with the table name included,
|
||||
as per best coding practices.
|
||||
|
||||
:::php
|
||||
<?php
|
||||
|
||||
$query = SQLSelect::create('*', '"SiteTree"', array('"SiteTree"."URLSegment" = ?' => $testURL));
|
||||
|
||||
$query->addWhere(array(
|
||||
'"SiteTree"."ParentID"' => // Note that the " = ?" is optional for simple comparison
|
||||
array( // Syntax for parameter casting for supporting databases
|
||||
'value' => $parentID,
|
||||
'type' => 'integer'
|
||||
),
|
||||
'"SiteTree"."ID" IN (SELECT "MyObject"."PageID" FROM "MyObject")' // Raw SQL condition with no parameters
|
||||
));
|
||||
|
||||
// Multiple parameters may be assigned for a single query (this should not be associative)
|
||||
$query->addWhere(array(
|
||||
'"SiteTree"."Title" LIKE %? OR "SiteTree"."Title" LIKE %?' => array($myText, $myText)
|
||||
));
|
||||
|
||||
3. #### Querying the database through `DataList`, `DataQuery`, and `DataObject`
|
||||
2. #### Querying the database through `DataList`, `DataQuery`, `SQLQuery`, and `DataObject`
|
||||
|
||||
Before:
|
||||
|
||||
@ -318,10 +270,10 @@ Examples of areas where queries should be upgraded are below:
|
||||
'"Title" = ?', $title
|
||||
);
|
||||
|
||||
4. #### Interaction with `DataList::sql()`, `DataQuery::sql()`, `SQLSelect::sql()`, or `SQLSelect::getJoins()` methods
|
||||
3. #### Interaction with `DataList::sql()`, `DataQuery::sql()`, `SQLQuery::sql()`, or `SQLQuery::getJoins()` methods
|
||||
|
||||
The place where legacy code would almost certainly fail is any code that calls
|
||||
`SQLQuery::sql`, `DataList::sql`, `DataQuery::sql` or `SQLSelect::getJoins()`, as the api requires that user
|
||||
DataList::sql`, `DataQuery::sql`, `SQLQuery::sql` or `SQLQuery::getJoins()`, as the api requires that user
|
||||
code passes in an argument here to retrieve SQL parameters by value.
|
||||
|
||||
User code that assumes parameterless queries will likely fail, and need to be
|
||||
@ -334,7 +286,7 @@ Examples of areas where queries should be upgraded are below:
|
||||
|
||||
// Generate query
|
||||
$argument = 'whatever';
|
||||
$query = SQLSelect::create()
|
||||
$query = SQLQuery::create()
|
||||
->setFrom('"SiteTree"')
|
||||
->setWhere(array("\"SiteTree\".\"Title\" LIKE '" . Convert::raw2sql($argument) . "'"));
|
||||
|
||||
@ -352,7 +304,7 @@ Examples of areas where queries should be upgraded are below:
|
||||
|
||||
// Generate query
|
||||
$argument = 'whatever';
|
||||
$query = SQLSelect::create()
|
||||
$query = SQLQuery::create()
|
||||
->setFrom('"SiteTree"')
|
||||
->setWhere(array('"SiteTree"."Title" LIKE ?' => $argument));
|
||||
|
||||
@ -381,46 +333,7 @@ Examples of areas where queries should be upgraded are below:
|
||||
)
|
||||
DB::prepared_query('DELETE FROM "MyObject" WHERE ParentID = ? OR IsValid = ?', $parameters);
|
||||
|
||||
5. #### Update implementations of augmentSQL
|
||||
|
||||
Since this method now takes a `SQLSelect` as a first parameter, existing code referencing the deprecated `SQLQuery`
|
||||
type will raise a PHP error.
|
||||
|
||||
Furthermore, it's important to note that even though the signature of `SQLSelect::getWhere` is similar to the old
|
||||
`SQLQuery::getWhere` the result will actually be an associative array of SQL fragments mapped to arrays of
|
||||
parameters, and any transformation of these values will require parameters to be maintained.
|
||||
|
||||
If your code doesn't modify the parameters then `SQLSelect::getWhereParameterised` can be used in order to return
|
||||
these SQL statements as a simple array of strings. The resulting parameters are still maintained, but are
|
||||
instead be returned by referenced through the first parameter to this method.
|
||||
|
||||
E.g.
|
||||
|
||||
Before:
|
||||
|
||||
:::php
|
||||
function augmentSQL(SQLQuery $query, DataQuery $dataQuery = null) {
|
||||
$locale = Translatable::get_current_locale();
|
||||
if(!preg_match('/("|\'|`)Locale("|\'|`)/', implode(' ', $query->getWhere()))) {
|
||||
$qry = sprintf('"Locale" = \'%s\'', Convert::raw2sql($locale));
|
||||
$query->addWhere($qry);
|
||||
}
|
||||
}
|
||||
|
||||
After:
|
||||
|
||||
:::php
|
||||
function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) {
|
||||
$locale = Translatable::get_current_locale();
|
||||
if(!preg_match('/("|\'|`)Locale("|\'|`)/', implode(' ', $query->getWhereParameterised($parameters)))) {
|
||||
$query->addWhere(array(
|
||||
'"Locale"' => $locale
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
### Update code that interacts with the DB schema
|
||||
4. ### Update code that interacts with the DB schema
|
||||
|
||||
Updating database schema is now done by `updateSchema` with a callback, rather than relying
|
||||
on user code to call `beginSchemaUpdate` and `endSchemaUpdate` around the call.
|
||||
@ -546,4 +459,4 @@ coding conventions. E.g. `DB::requireTable` is now `DB::require_table`
|
||||
* create_table_options now uses constants as API specific filters rather than strings.
|
||||
This is in order to promote better referencing of elements across the codebase.
|
||||
See `FulltextSearchable->enable` for example.
|
||||
* `FormField` subclasses must now use `validate(Validator $validator)` as the interface has changed for this function
|
||||
* `FormField` subclasses must now use `validate(Validator $validator)` as the interface has changed for this function
|
||||
|
52
docs/en/changelogs/4.0.0.md
Normal file
52
docs/en/changelogs/4.0.0.md
Normal file
@ -0,0 +1,52 @@
|
||||
# 4.0.0 (unreleased)
|
||||
|
||||
## Overview
|
||||
|
||||
### Framework
|
||||
|
||||
* Deprecate `SQLQuery` in favour `SQLSelect`
|
||||
|
||||
## Upgrading
|
||||
|
||||
### Update code that uses SQLQuery
|
||||
|
||||
SQLQuery is still implemented, but now extends the new SQLSelect class and has some methods
|
||||
deprecated. Previously this class was used for both selecting and deleting, but these
|
||||
have been superceded by the specialised SQLSelect and SQLDelete classes.
|
||||
|
||||
Take care for any code or functions which expect an object of type `SQLQuery`, as
|
||||
these references should be replaced with `SQLSelect`. Legacy code which generates
|
||||
`SQLQuery` can still communicate with new code that expects `SQLSelect` as it is a
|
||||
subclass of `SQLSelect`, but the inverse is not true.
|
||||
|
||||
### Update implementations of augmentSQL
|
||||
|
||||
Since this method now takes a `SQLSelect` as a first parameter, existing code referencing the deprecated `SQLQuery`
|
||||
type will raise a PHP error.
|
||||
|
||||
E.g.
|
||||
|
||||
Before:
|
||||
|
||||
:::php
|
||||
function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {
|
||||
$locale = Translatable::get_current_locale();
|
||||
if(!preg_match('/("|\'|`)Locale("|\'|`)/', implode(' ', $query->getWhere()))) {
|
||||
$qry = sprintf('"Locale" = \'%s\'', Convert::raw2sql($locale));
|
||||
$query->addWhere($qry);
|
||||
}
|
||||
}
|
||||
|
||||
After:
|
||||
|
||||
:::php
|
||||
function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) {
|
||||
$locale = Translatable::get_current_locale();
|
||||
if(!preg_match('/("|\'|`)Locale("|\'|`)/', implode(' ', $query->getWhereParameterised($parameters)))) {
|
||||
$query->addWhere(array(
|
||||
'"Locale"' => $locale
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,8 @@ For information on how to upgrade to newer versions consult the [upgrading](/ins
|
||||
|
||||
## Stable Releases
|
||||
|
||||
* [4.0.0](4.0.0) - Unreleased
|
||||
|
||||
* [3.2.0](3.2.0) - Unreleased
|
||||
|
||||
* [3.1.8](3.1.8) - 18 November 2014
|
||||
|
@ -225,8 +225,8 @@ The other queries that you will want to customise are the selection queries,
|
||||
called by get & get_one. For example, the Versioned object has code to redirect
|
||||
every request to ClassName_live, if you are browsing the live site.
|
||||
|
||||
To do this, define the **augmentSQL(SQLSelect $query)** method. Again, the $query object is passed by reference and can
|
||||
be modified as needed by your method. Instead of a manipulation array, we have a `[api:SQLSelect]` object.
|
||||
To do this, define the **augmentSQL(SQLQuery &$query)** method. Again, the $query object is passed by reference and can
|
||||
be modified as needed by your method. Instead of a manipulation array, we have a `[api:SQLQuery]` object.
|
||||
|
||||
### Additional methods
|
||||
|
||||
|
@ -25,7 +25,7 @@ Reference articles complement our auto-generated [API docs](http://api.silverstr
|
||||
* [Site Reports](site-reports): Tabular reports in a specialized CMS interface
|
||||
* [SiteConfig](siteconfig): Global configuration stored in the database
|
||||
* [SiteTree](sitetree): Base class for a "page" in the CMS
|
||||
* [SQLSelect](sqlquery): Wrapper around a SQL query allowing modification before execution
|
||||
* [SQLQuery](sqlquery): Wrapper around a SQL query allowing modification before execution
|
||||
* [StaticPublisher](staticpublisher): Export a page tree as static HTML for better performance and portability
|
||||
* [TableField](tablefield): Add and edit records with inline edits in this form field
|
||||
* [TableListField](tablelistfield): View and delete records in the CMS
|
||||
|
@ -6,7 +6,7 @@ Manages searching of properties on one or more `[api:DataObject]` types, based o
|
||||
`[api:SearchContext]` is intentionally decoupled from any controller-logic,
|
||||
it just receives a set of search parameters and an object class it acts on.
|
||||
|
||||
The default output of a `[api:SearchContext]` is either a `[api:SQLSelect]` object for further refinement, or a
|
||||
The default output of a `[api:SearchContext]` is either a `[api:SQLQuery]` object for further refinement, or a
|
||||
`[api:DataObject]` instance.
|
||||
|
||||
In case you need multiple contexts, consider namespacing your request parameters by using `FieldList->namespace()` on
|
||||
@ -86,7 +86,7 @@ method, we're building our own `getCustomSearchContext()` variant.
|
||||
### Pagination
|
||||
|
||||
For pagination records on multiple pages, you need to wrap the results in a
|
||||
`PaginatedList` object. This object is also passed the generated `SQLSelect`
|
||||
`PaginatedList` object. This object is also passed the generated `SQLQuery`
|
||||
in order to read page limit information. It is also passed the current
|
||||
`SS_HTTPRequest` object so it can read the current page from a GET var.
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
# SQL Select
|
||||
# SQLQuery
|
||||
|
||||
## Introduction
|
||||
|
||||
@ -18,8 +18,8 @@ the following three statements are functionally equivalent:
|
||||
:::php
|
||||
// Through raw SQL
|
||||
$count = DB::query('SELECT COUNT(*) FROM "Member"')->value();
|
||||
// Through SQLSelect abstraction layer
|
||||
$query = new SQLSelect();
|
||||
// Through SQLQuery abstraction layer
|
||||
$query = new SQLQuery();
|
||||
$count = $query->setFrom('Member')->setSelect('COUNT(*)')->value();
|
||||
// Through the ORM
|
||||
$count = Member::get()->count();
|
||||
@ -45,7 +45,7 @@ how to properly prepare user input and variables for use in queries
|
||||
|
||||
### SELECT
|
||||
|
||||
Selection can be done by creating an instance of `SQLSelect`, which allows
|
||||
Selection can be done by creating an instance of `SQLQuery`, which allows
|
||||
management of all elements of a SQL SELECT query, including columns, joined tables,
|
||||
conditional filters, grouping, limiting, and sorting.
|
||||
|
||||
@ -54,36 +54,36 @@ E.g.
|
||||
:::php
|
||||
<?php
|
||||
|
||||
$sqlSelect = new SQLSelect();
|
||||
$sqlSelect->setFrom('Player');
|
||||
$sqlSelect->selectField('FieldName', 'Name');
|
||||
$sqlSelect->selectField('YEAR("Birthday")', 'Birthyear');
|
||||
$sqlSelect->addLeftJoin('Team','"Player"."TeamID" = "Team"."ID"');
|
||||
$sqlSelect->addWhere(array('YEAR("Birthday") = ?' => 1982));
|
||||
// $sqlSelect->setOrderBy(...);
|
||||
// $sqlSelect->setGroupBy(...);
|
||||
// $sqlSelect->setHaving(...);
|
||||
// $sqlSelect->setLimit(...);
|
||||
// $sqlSelect->setDistinct(true);
|
||||
$sqlQuery = new SQLQuery();
|
||||
$sqlQuery->setFrom('Player');
|
||||
$sqlQuery->selectField('FieldName', 'Name');
|
||||
$sqlQuery->selectField('YEAR("Birthday")', 'Birthyear');
|
||||
$sqlQuery->addLeftJoin('Team','"Player"."TeamID" = "Team"."ID"');
|
||||
$sqlQuery->addWhere(array('YEAR("Birthday") = ?' => 1982));
|
||||
// $sqlQuery->setOrderBy(...);
|
||||
// $sqlQuery->setGroupBy(...);
|
||||
// $sqlQuery->setHaving(...);
|
||||
// $sqlQuery->setLimit(...);
|
||||
// $sqlQuery->setDistinct(true);
|
||||
|
||||
// Get the raw SQL (optional) and parameters
|
||||
$rawSQL = $sqlSelect->sql($parameters);
|
||||
$rawSQL = $sqlQuery->sql($parameters);
|
||||
|
||||
// Execute and return a Query object
|
||||
$result = $sqlSelect->execute();
|
||||
$result = $sqlQuery->execute();
|
||||
|
||||
// Iterate over results
|
||||
foreach($result as $row) {
|
||||
echo $row['BirthYear'];
|
||||
}
|
||||
|
||||
The result of `SQLSelect::execute()` is an array lightly wrapped in a database-specific subclass of `[api:SS_Query]`.
|
||||
The result of `SQLQuery::execute()` is an array lightly wrapped in a database-specific subclass of `[api:SS_Query]`.
|
||||
This class implements the *Iterator*-interface, and provides convenience-methods for accessing the data.
|
||||
|
||||
### DELETE
|
||||
|
||||
Deletion can be done either by calling `DB::query`/`DB::prepared_query` directly,
|
||||
by creating a `SQLDelete` object, or by transforming a `SQLSelect` into a `SQLDelete`
|
||||
by creating a `SQLDelete` object, or by transforming a `SQLQuery` into a `SQLDelete`
|
||||
object instead.
|
||||
|
||||
For example, creating a `SQLDelete` object
|
||||
@ -96,12 +96,12 @@ For example, creating a `SQLDelete` object
|
||||
->setWhere(array('"SiteTree"."ShowInMenus"' => 0));
|
||||
$query->execute();
|
||||
|
||||
Alternatively, turning an existing `SQLSelect` into a delete
|
||||
Alternatively, turning an existing `SQLQuery` into a delete
|
||||
|
||||
:::php
|
||||
<?php
|
||||
|
||||
$query = SQLSelect::create()
|
||||
$query = SQLQuery::create()
|
||||
->setFrom('"SiteTree"')
|
||||
->setWhere(array('"SiteTree"."ShowInMenus"' => 0))
|
||||
->toDelete();
|
||||
@ -220,12 +220,12 @@ e.g. when you want a single column rather than a full-blown object representatio
|
||||
Example: Get the count from a relationship.
|
||||
|
||||
:::php
|
||||
$sqlSelect = new SQLSelect();
|
||||
$sqlSelect->setFrom('Player');
|
||||
$sqlSelect->addSelect('COUNT("Player"."ID")');
|
||||
$sqlSelect->addWhere(array('"Team"."ID"' => 99));
|
||||
$sqlSelect->addLeftJoin('Team', '"Team"."ID" = "Player"."TeamID"');
|
||||
$count = $sqlSelect->execute()->value();
|
||||
$sqlQuery = new SQLQuery();
|
||||
$sqlQuery->setFrom('Player');
|
||||
$sqlQuery->addSelect('COUNT("Player"."ID")');
|
||||
$sqlQuery->addWhere(array('"Team"."ID"' => 99));
|
||||
$sqlQuery->addLeftJoin('Team', '"Team"."ID" = "Player"."TeamID"');
|
||||
$count = $sqlQuery->execute()->value();
|
||||
|
||||
Note that in the ORM, this call would be executed in an efficient manner as well:
|
||||
|
||||
@ -240,14 +240,14 @@ This can be useful for creating dropdowns.
|
||||
Example: Show player names with their birth year, but set their birth dates as values.
|
||||
|
||||
:::php
|
||||
$sqlSelect = new SQLSelect();
|
||||
$sqlSelect->setFrom('Player');
|
||||
$sqlSelect->setSelect('Birthdate');
|
||||
$sqlSelect->selectField('CONCAT("Name", ' - ', YEAR("Birthdate")', 'NameWithBirthyear');
|
||||
$map = $sqlSelect->execute()->map();
|
||||
$sqlQuery = new SQLQuery();
|
||||
$sqlQuery->setFrom('Player');
|
||||
$sqlQuery->setSelect('Birthdate');
|
||||
$sqlQuery->selectField('CONCAT("Name", ' - ', YEAR("Birthdate")', 'NameWithBirthyear');
|
||||
$map = $sqlQuery->execute()->map();
|
||||
$field = new DropdownField('Birthdates', 'Birthdates', $map);
|
||||
|
||||
Note that going through SQLSelect is just necessary here
|
||||
Note that going through SQLQuery is just necessary here
|
||||
because of the custom SQL value transformation (`YEAR()`).
|
||||
An alternative approach would be a custom getter in the object definition.
|
||||
|
||||
|
@ -365,7 +365,7 @@ For example:
|
||||
:::php
|
||||
$members = Member::get()->where(array('"FirstName" = ?' => 'Sam'));
|
||||
|
||||
If using `SQLSelect` you should use `addWhere`, `setWhere`, `addWhereAny`, or `setWhereAny` to modify the query.
|
||||
If using `SQLQuery` you should use `addWhere`, `setWhere`, `addWhereAny`, or `setWhereAny` to modify the query.
|
||||
|
||||
Using the parameterised query syntax you can either provide a single variable as a parameter, an array of parameters
|
||||
if the SQL has multiple value placeholders, or simply pass an indexed array of strings for literal SQL.
|
||||
@ -443,7 +443,7 @@ E.g.
|
||||
}
|
||||
}
|
||||
|
||||
$query = SQLSelect::create()
|
||||
$query = SQLQuery::create()
|
||||
->setFrom('"MyObject"')
|
||||
->setWhere($condition = new RandomCondition());
|
||||
$condition->field = '"Score"';
|
||||
|
@ -33,7 +33,7 @@ Example:
|
||||
$records = DataObject::get_by_id('MyClass', 3);
|
||||
$records = DataObject::get_one('MyClass', array('"ID" = ?' => 3));
|
||||
$records = MyClass::get()->byID(3);
|
||||
$records = SQLSelect::create()->addWhere(array('"ID"' => 3))->execute();
|
||||
$records = SQLQuery::create()->addWhere(array('"ID"' => 3))->execute();
|
||||
|
||||
Parameterised updates and inserts are also supported, but the syntax is a little different
|
||||
|
||||
@ -98,7 +98,7 @@ As a rule of thumb, whenever you're creating SQL queries (or just chunks of SQL)
|
||||
but there may be cases where you need to take care of escaping yourself. See [coding-conventions](/misc/coding-conventions)
|
||||
and [datamodel](/topics/datamodel) for ways to parameterise, cast, and convert your data.
|
||||
|
||||
* `SQLSelect`
|
||||
* `SQLQuery`
|
||||
* `DB::query()`
|
||||
* `DB::preparedQuery()`
|
||||
* `Director::urlParams()`
|
||||
|
@ -23,9 +23,9 @@ abstract class DataExtension extends Extension {
|
||||
/**
|
||||
* Edit the given query object to support queries for this extension
|
||||
*
|
||||
* @param SQLSelect $query Query to augment.
|
||||
* @param SQLQuery $query Query to augment.
|
||||
*/
|
||||
public function augmentSQL(SQLSelect $query) {
|
||||
public function augmentSQL(SQLQuery &$query) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -182,12 +182,6 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
|
||||
* @param string The resulting SQL query (may be paramaterised)
|
||||
*/
|
||||
public function sql(&$parameters = array()) {
|
||||
if(func_num_args() == 0) {
|
||||
Deprecation::notice(
|
||||
'3.2',
|
||||
'DataList::sql() now may produce parameters which are necessary to execute this query'
|
||||
);
|
||||
}
|
||||
return $this->dataQuery->query()->sql($parameters);
|
||||
}
|
||||
|
||||
@ -195,8 +189,8 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
|
||||
* Return a new DataList instance with a WHERE clause added to this list's query.
|
||||
*
|
||||
* Supports parameterised queries.
|
||||
* See SQLSelect::addWhere() for syntax examples, although DataList
|
||||
* won't expand multiple method arguments as SQLSelect does.
|
||||
* See SQLQuery::addWhere() for syntax examples, although DataList
|
||||
* won't expand multiple method arguments as SQLQuery does.
|
||||
*
|
||||
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
|
||||
* paramaterised queries
|
||||
@ -213,8 +207,8 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
|
||||
* All conditions provided in the filter will be joined with an OR
|
||||
*
|
||||
* Supports parameterised queries.
|
||||
* See SQLSelect::addWhere() for syntax examples, although DataList
|
||||
* won't expand multiple method arguments as SQLSelect does.
|
||||
* See SQLQuery::addWhere() for syntax examples, although DataList
|
||||
* won't expand multiple method arguments as SQLQuery does.
|
||||
*
|
||||
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
|
||||
* paramaterised queries
|
||||
@ -279,7 +273,7 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
|
||||
* order set.
|
||||
*
|
||||
* @see SS_List::sort()
|
||||
* @see SQLSelect::orderby
|
||||
* @see SQLQuery::orderby
|
||||
* @example $list = $list->sort('Name'); // default ASC sorting
|
||||
* @example $list = $list->sort('Name DESC'); // DESC sorting
|
||||
* @example $list = $list->sort('Name', 'ASC');
|
||||
|
@ -2637,7 +2637,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
$groupList = implode(', ', $groups->column("ID"));
|
||||
|
||||
// TODO Fix relation table hardcoding
|
||||
$query = new SQLSelect(
|
||||
$query = new SQLQuery(
|
||||
"\"Page_Can$perm\".PageID",
|
||||
array("\"Page_Can$perm\""),
|
||||
"GroupID IN ($groupList)");
|
||||
@ -2646,7 +2646,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
|
||||
if($perm == "View") {
|
||||
// TODO Fix relation table hardcoding
|
||||
$query = new SQLSelect("\"SiteTree\".\"ID\"", array(
|
||||
$query = new SQLQuery("\"SiteTree\".\"ID\"", array(
|
||||
"\"SiteTree\"",
|
||||
"LEFT JOIN \"Page_CanView\" ON \"Page_CanView\".\"PageID\" = \"SiteTree\".\"ID\""
|
||||
), "\"Page_CanView\".\"PageID\" IS NULL");
|
||||
@ -2927,7 +2927,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
*
|
||||
* @param string $callerClass The class of objects to be returned
|
||||
* @param string|array $filter A filter to be inserted into the WHERE clause.
|
||||
* Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
|
||||
* Supports parameterised queries. See SQLQuery::addWhere() for syntax examples.
|
||||
* @param string|array $sort A sort expression to be inserted into the ORDER
|
||||
* BY clause. If omitted, self::$default_sort will be used.
|
||||
* @param string $join Deprecated 3.0 Join clause. Use leftJoin($table, $joinClause) instead.
|
||||
@ -2982,7 +2982,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
||||
*
|
||||
* @param string $callerClass The class of objects to be returned
|
||||
* @param string|array $filter A filter to be inserted into the WHERE clause.
|
||||
* Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
|
||||
* Supports parameterised queries. See SQLQuery::addWhere() for syntax examples.
|
||||
* @param boolean $cache Use caching
|
||||
* @param string $orderby A sort expression to be inserted into the ORDER BY clause.
|
||||
*
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* An object representing a query of data from the DataObject's supporting database.
|
||||
* Acts as a wrapper over {@link SQLSelect} and performs all of the query generation.
|
||||
* Acts as a wrapper over {@link SQLQuery} and performs all of the query generation.
|
||||
* Used extensively by {@link DataList}.
|
||||
*
|
||||
* Unlike DataList, modifiers on DataQuery modify the object rather than returning a clone.
|
||||
@ -19,7 +19,7 @@ class DataQuery {
|
||||
protected $dataClass;
|
||||
|
||||
/**
|
||||
* @var SQLSelect
|
||||
* @var SQLQuery
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
@ -65,7 +65,7 @@ class DataQuery {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link SQLSelect} object that represents the current query; note that it will
|
||||
* Return the {@link SQLQuery} object that represents the current query; note that it will
|
||||
* be a clone of the object.
|
||||
*/
|
||||
public function query() {
|
||||
@ -103,7 +103,7 @@ class DataQuery {
|
||||
// As each condition is a single length array, do a single
|
||||
// iteration to extract the predicate and parameters
|
||||
foreach($condition as $predicate => $parameters) {
|
||||
// @see SQLSelect::addWhere for why this is required here
|
||||
// @see SQLQuery::addWhere for why this is required here
|
||||
if(strpos($predicate, $fieldExpression) !== false) {
|
||||
unset($where[$i]);
|
||||
$matched = true;
|
||||
@ -146,7 +146,7 @@ class DataQuery {
|
||||
$baseClass = array_shift($tableClasses);
|
||||
|
||||
// Build our intial query
|
||||
$this->query = new SQLSelect(array());
|
||||
$this->query = new SQLQuery(array());
|
||||
$this->query->setDistinct(true);
|
||||
|
||||
if($sort = singleton($this->dataClass)->stat('default_sort')) {
|
||||
@ -167,7 +167,7 @@ class DataQuery {
|
||||
* Ensure that the query is ready to execute.
|
||||
*
|
||||
* @param array|null $queriedColumns Any columns to filter the query by
|
||||
* @return SQLSelect The finalised sql query
|
||||
* @return SQLQuery The finalised sql query
|
||||
*/
|
||||
public function getFinalisedQuery($queriedColumns = null) {
|
||||
if(!$queriedColumns) $queriedColumns = $this->queriedColumns;
|
||||
@ -279,7 +279,7 @@ class DataQuery {
|
||||
/**
|
||||
* Ensure that if a query has an order by clause, those columns are present in the select.
|
||||
*
|
||||
* @param SQLSelect $query
|
||||
* @param SQLQuery $query
|
||||
* @return null
|
||||
*/
|
||||
protected function ensureSelectContainsOrderbyColumns($query, $originalSelect = array()) {
|
||||
@ -322,7 +322,7 @@ class DataQuery {
|
||||
// add new columns sort
|
||||
$newOrderby[$qualCol] = $dir;
|
||||
|
||||
// To-do: Remove this if block once SQLSelect::$select has been refactored to store getSelect()
|
||||
// To-do: Remove this if block once SQLQuery::$select has been refactored to store getSelect()
|
||||
// format internally; then this check can be part of selectField()
|
||||
$selects = $query->getSelect();
|
||||
if(!isset($selects[$col]) && !in_array($qualCol, $selects)) {
|
||||
@ -361,12 +361,6 @@ class DataQuery {
|
||||
* @return string The resulting SQL query (may be paramaterised)
|
||||
*/
|
||||
public function sql(&$parameters = array()) {
|
||||
if(func_num_args() == 0) {
|
||||
Deprecation::notice(
|
||||
'3.2',
|
||||
'DataQuery::sql() now may produce parameters which are necessary to execute this query'
|
||||
);
|
||||
}
|
||||
return $this->getFinalisedQuery()->sql($parameters);
|
||||
}
|
||||
|
||||
@ -445,7 +439,7 @@ class DataQuery {
|
||||
/**
|
||||
* Update the SELECT clause of the query with the columns from the given table
|
||||
*/
|
||||
protected function selectColumnsFromTable(SQLSelect &$query, $tableClass, $columns = null) {
|
||||
protected function selectColumnsFromTable(SQLQuery &$query, $tableClass, $columns = null) {
|
||||
// Add SQL for multi-value fields
|
||||
$databaseFields = DataObject::database_fields($tableClass, false);
|
||||
$compositeFields = DataObject::composite_fields($tableClass, false);
|
||||
@ -514,8 +508,8 @@ class DataQuery {
|
||||
/**
|
||||
* Adds a WHERE clause.
|
||||
*
|
||||
* @see SQLSelect::addWhere() for syntax examples, although DataQuery
|
||||
* won't expand multiple arguments as SQLSelect does.
|
||||
* @see SQLQuery::addWhere() for syntax examples, although DataQuery
|
||||
* won't expand multiple arguments as SQLQuery does.
|
||||
*
|
||||
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
|
||||
* paramaterised queries
|
||||
@ -531,8 +525,8 @@ class DataQuery {
|
||||
/**
|
||||
* Append a WHERE with OR.
|
||||
*
|
||||
* @see SQLSelect::addWhere() for syntax examples, although DataQuery
|
||||
* won't expand multiple method arguments as SQLSelect does.
|
||||
* @see SQLQuery::addWhere() for syntax examples, although DataQuery
|
||||
* won't expand multiple method arguments as SQLQuery does.
|
||||
*
|
||||
* @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or
|
||||
* paramaterised queries
|
||||
@ -548,7 +542,7 @@ class DataQuery {
|
||||
/**
|
||||
* Set the ORDER BY clause of this query
|
||||
*
|
||||
* @see SQLSelect::orderby()
|
||||
* @see SQLQuery::orderby()
|
||||
*
|
||||
* @param String $sort Column to sort on (escaped SQL statement)
|
||||
* @param String $direction Direction ("ASC" or "DESC", escaped SQL statement)
|
||||
@ -770,7 +764,7 @@ class DataQuery {
|
||||
|
||||
/**
|
||||
* @param String $field Select statement identifier, either the unquoted column name,
|
||||
* the full composite SQL statement, or the alias set through {@link SQLSelect->selectField()}.
|
||||
* the full composite SQL statement, or the alias set through {@link SQLQuery->selectField()}.
|
||||
* @return String The expression used to query this field via this DataQuery
|
||||
*/
|
||||
protected function expressionForField($field) {
|
||||
@ -835,7 +829,7 @@ class DataQuery {
|
||||
/**
|
||||
* Represents a subgroup inside a WHERE clause in a {@link DataQuery}
|
||||
*
|
||||
* Stores the clauses for the subgroup inside a specific {@link SQLSelect} object.
|
||||
* Stores the clauses for the subgroup inside a specific {@link SQLQuery} object.
|
||||
* All non-where methods call their DataQuery versions, which uses the base
|
||||
* query object.
|
||||
*
|
||||
@ -848,7 +842,7 @@ class DataQuery_SubGroup extends DataQuery implements SQLConditionGroup {
|
||||
public function __construct(DataQuery $base, $connective) {
|
||||
$this->dataClass = $base->dataClass;
|
||||
$this->query = $base->query;
|
||||
$this->whereQuery = new SQLSelect();
|
||||
$this->whereQuery = new SQLQuery();
|
||||
$this->whereQuery->setConnective($connective);
|
||||
|
||||
$base->where($this);
|
||||
|
@ -38,15 +38,6 @@ class Hierarchy extends DataExtension {
|
||||
*/
|
||||
private static $node_threshold_leaf = 250;
|
||||
|
||||
public function augmentSQL(SQLSelect $query) {
|
||||
}
|
||||
|
||||
public function augmentDatabase() {
|
||||
}
|
||||
|
||||
public function augmentWrite(&$manipulation) {
|
||||
}
|
||||
|
||||
public static function get_extra_config($class, $extension, $args) {
|
||||
return array(
|
||||
'has_one' => array('Parent' => $class)
|
||||
|
@ -220,7 +220,7 @@ class ManyManyList extends RelationList {
|
||||
// With the current query, simply add the foreign and local conditions
|
||||
// The query can be a bit odd, especially if custom relation classes
|
||||
// don't join expected tables (@see Member_GroupSet for example).
|
||||
$query = new SQLSelect("*", "\"{$this->joinTable}\"");
|
||||
$query = new SQLQuery("*", "\"{$this->joinTable}\"");
|
||||
$query->addWhere($foreignFilter);
|
||||
$query->addWhere(array(
|
||||
"\"{$this->joinTable}\".\"{$this->localKey}\"" => $itemID
|
||||
@ -365,7 +365,7 @@ class ManyManyList extends RelationList {
|
||||
// @todo Optimize into a single query instead of one per extra field
|
||||
if($this->extraFields) {
|
||||
foreach($this->extraFields as $fieldName => $dbFieldSpec) {
|
||||
$query = new SQLSelect("\"{$fieldName}\"", "\"{$this->joinTable}\"");
|
||||
$query = new SQLQuery("\"{$fieldName}\"", "\"{$this->joinTable}\"");
|
||||
if($filter = $this->foreignIDWriteFilter($this->getForeignID())) {
|
||||
$query->setWhere($filter);
|
||||
} else {
|
||||
|
@ -162,10 +162,10 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
|
||||
* Amend freshly created DataQuery objects with versioned-specific
|
||||
* information.
|
||||
*
|
||||
* @param SQLSelect
|
||||
* @param SQLQuery
|
||||
* @param DataQuery
|
||||
*/
|
||||
public function augmentDataQueryCreation(SQLSelect &$query, DataQuery &$dataQuery) {
|
||||
public function augmentDataQueryCreation(SQLQuery &$query, DataQuery &$dataQuery) {
|
||||
$parts = explode('.', Versioned::get_reading_mode());
|
||||
|
||||
if($parts[0] == 'Archive') {
|
||||
@ -182,10 +182,10 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Augment the the SQLSelect that is created by the DataQuery
|
||||
* Augment the the SQLQuery that is created by the DataQuery
|
||||
* @todo Should this all go into VersionedDataQuery?
|
||||
*/
|
||||
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) {
|
||||
public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null) {
|
||||
if(!$dataQuery || !$dataQuery->getQueryParam('Versioned.mode')) {
|
||||
return;
|
||||
}
|
||||
@ -329,11 +329,11 @@ class Versioned extends DataExtension implements TemplateGlobalProvider {
|
||||
/**
|
||||
* For lazy loaded fields requiring extra sql manipulation, ie versioning.
|
||||
*
|
||||
* @param SQLSelect $query
|
||||
* @param SQLQuery $query
|
||||
* @param DataQuery $dataQuery
|
||||
* @param DataObject $dataObject
|
||||
*/
|
||||
public function augmentLoadLazyFields(SQLSelect &$query, DataQuery &$dataQuery = null, $dataObject) {
|
||||
public function augmentLoadLazyFields(SQLQuery &$query, DataQuery &$dataQuery = null, $dataObject) {
|
||||
// The VersionedMode local variable ensures that this decorator only applies to
|
||||
// queries that have originated from the Versioned object, and have the Versioned
|
||||
// metadata set on the query object. This prevents regular queries from
|
||||
|
@ -150,10 +150,10 @@ interface CompositeDBField {
|
||||
/**
|
||||
* Add all columns which are defined through {@link requireField()}
|
||||
* and {@link $composite_db}, or any additional SQL that is required
|
||||
* to get to these columns. Will mostly just write to the {@link SQLSelect->select}
|
||||
* to get to these columns. Will mostly just write to the {@link SQLQuery->select}
|
||||
* array.
|
||||
*
|
||||
* @param SQLSelect $query
|
||||
* @param SQLQuery $query
|
||||
*/
|
||||
public function addToQuery(&$query);
|
||||
|
||||
|
@ -366,7 +366,7 @@ abstract class SQLConditionalExpression extends SQLExpression {
|
||||
/**
|
||||
* Set a WHERE clause.
|
||||
*
|
||||
* @see SQLSelect::addWhere() for syntax examples
|
||||
* @see SQLQuery::addWhere() for syntax examples
|
||||
*
|
||||
* @param mixed $where Predicate(s) to set, as escaped SQL statements or paramaterised queries
|
||||
* @param mixed $where,... Unlimited additional predicates
|
||||
@ -468,7 +468,7 @@ abstract class SQLConditionalExpression extends SQLExpression {
|
||||
}
|
||||
|
||||
/**
|
||||
* @see SQLSelect::addWhere()
|
||||
* @see SQLQuery::addWhere()
|
||||
*
|
||||
* @param mixed $filters Predicate(s) to set, as escaped SQL statements or paramaterised queries
|
||||
* @param mixed $filters,... Unlimited additional predicates
|
||||
@ -482,7 +482,7 @@ abstract class SQLConditionalExpression extends SQLExpression {
|
||||
}
|
||||
|
||||
/**
|
||||
* @see SQLSelect::addWhere()
|
||||
* @see SQLQuery::addWhere()
|
||||
*
|
||||
* @param mixed $filters Predicate(s) to set, as escaped SQL statements or paramaterised queries
|
||||
* @param mixed $filters,... Unlimited additional predicates
|
||||
@ -689,12 +689,12 @@ abstract class SQLConditionalExpression extends SQLExpression {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an SQLSelect object using the currently specified parameters.
|
||||
* Generates an SQLQuery object using the currently specified parameters.
|
||||
*
|
||||
* @return SQLSelect
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function toSelect() {
|
||||
$select = new SQLSelect();
|
||||
$select = new SQLQuery();
|
||||
$this->copyTo($select);
|
||||
return $select;
|
||||
}
|
||||
|
@ -99,13 +99,6 @@ abstract class SQLExpression {
|
||||
* @return string The completed SQL query
|
||||
*/
|
||||
public function sql(&$parameters = array()) {
|
||||
if(func_num_args() == 0) {
|
||||
Deprecation::notice(
|
||||
'3.2',
|
||||
'SQLExpression::sql() now may produce parameters which are necessary to execute this query'
|
||||
);
|
||||
}
|
||||
|
||||
// Build each component as needed
|
||||
$sql = DB::build_sql($this, $parameters);
|
||||
|
||||
|
@ -6,18 +6,18 @@
|
||||
*
|
||||
* @package framework
|
||||
* @subpackage model
|
||||
* @deprecated since version 3.3
|
||||
* @deprecated since version 4.0
|
||||
*/
|
||||
class SQLQuery extends SQLSelect {
|
||||
|
||||
/**
|
||||
* @deprecated since version 3.3
|
||||
* @deprecated since version 4.0
|
||||
*/
|
||||
public function __construct($select = "*", $from = array(), $where = array(), $orderby = array(),
|
||||
$groupby = array(), $having = array(), $limit = array()
|
||||
) {
|
||||
parent::__construct($select, $from, $where, $orderby, $groupby, $having, $limit);
|
||||
Deprecation::notice('3.3', 'Use SQLSelect instead');
|
||||
Deprecation::notice('4.0', 'Use SQLSelect instead');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,7 +5,7 @@
|
||||
* SearchContext is intentionally decoupled from any controller-logic,
|
||||
* it just receives a set of search parameters and an object class it acts on.
|
||||
*
|
||||
* The default output of a SearchContext is either a {@link SQLSelect} object
|
||||
* The default output of a SearchContext is either a {@link SQLQuery} object
|
||||
* for further refinement, or a {@link SS_List} that can be used to display
|
||||
* search results, e.g. in a {@link TableListField} instance.
|
||||
*
|
||||
@ -88,7 +88,7 @@ class SearchContext extends Object {
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo move to SQLSelect
|
||||
* @todo move to SQLQuery
|
||||
* @todo fix hack
|
||||
*/
|
||||
protected function applyBaseTableFields() {
|
||||
|
@ -1600,7 +1600,7 @@ class Member_GroupSet extends ManyManyList {
|
||||
|
||||
// Find directly applied groups
|
||||
$manyManyFilter = parent::foreignIDFilter($id);
|
||||
$query = new SQLSelect('"Group_Members"."GroupID"', '"Group_Members"', $manyManyFilter);
|
||||
$query = new SQLQuery('"Group_Members"."GroupID"', '"Group_Members"', $manyManyFilter);
|
||||
$groupIDs = $query->execute()->column();
|
||||
|
||||
// Get all ancestors, iteratively merging these into the master set
|
||||
|
@ -95,7 +95,7 @@ class DbDatetimeTest extends FunctionalTest {
|
||||
$this->matchesRoughly($result, date('Y-m-d H:i:s', strtotime('+1 Day', $this->getDbNow())), 'tomorrow',
|
||||
$offset);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array());
|
||||
$query->selectField($this->adapter->datetimeIntervalClause('"Created"', '-15 Minutes'), 'test')
|
||||
->setFrom('"DbDateTimeTest_Team"')
|
||||
@ -123,7 +123,7 @@ class DbDatetimeTest extends FunctionalTest {
|
||||
$result = DB::query('SELECT ' . $clause)->value();
|
||||
$this->matchesRoughly($result, -45 * 60, 'now - 45 minutes ahead', $offset);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array());
|
||||
$query->selectField($this->adapter->datetimeDifferenceClause('"LastEdited"', '"Created"'), 'test')
|
||||
->setFrom('"DbDateTimeTest_Team"')
|
||||
|
@ -41,7 +41,7 @@ class PaginatedListTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testSetPaginationFromQuery() {
|
||||
$query = $this->getMock('SQLSelect');
|
||||
$query = $this->getMock('SQLQuery');
|
||||
$query->expects($this->once())
|
||||
->method('getLimit')
|
||||
->will($this->returnValue(array('limit' => 15, 'start' => 30)));
|
||||
|
@ -15,12 +15,12 @@ class SQLQueryTest extends SapphireTest {
|
||||
);
|
||||
|
||||
public function testEmptyQueryReturnsNothing() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$this->assertSQLEquals('', $query->sql($parameters));
|
||||
}
|
||||
|
||||
public function testSelectFromBasicTable() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('MyTable');
|
||||
$this->assertSQLEquals("SELECT * FROM MyTable", $query->sql($parameters));
|
||||
$query->addFrom('MyJoin');
|
||||
@ -28,14 +28,14 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testSelectFromUserSpecifiedFields() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array("Name", "Title", "Description"));
|
||||
$query->setFrom("MyTable");
|
||||
$this->assertSQLEquals("SELECT Name, Title, Description FROM MyTable", $query->sql($parameters));
|
||||
}
|
||||
|
||||
public function testSelectWithWhereClauseFilter() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array("Name","Meta"));
|
||||
$query->setFrom("MyTable");
|
||||
$query->setWhere("Name = 'Name'");
|
||||
@ -47,14 +47,14 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testSelectWithConstructorParameters() {
|
||||
$query = new SQLSelect(array("Foo", "Bar"), "FooBarTable");
|
||||
$query = new SQLQuery(array("Foo", "Bar"), "FooBarTable");
|
||||
$this->assertSQLEquals("SELECT Foo, Bar FROM FooBarTable", $query->sql($parameters));
|
||||
$query = new SQLSelect(array("Foo", "Bar"), "FooBarTable", array("Foo = 'Boo'"));
|
||||
$query = new SQLQuery(array("Foo", "Bar"), "FooBarTable", array("Foo = 'Boo'"));
|
||||
$this->assertSQLEquals("SELECT Foo, Bar FROM FooBarTable WHERE (Foo = 'Boo')", $query->sql($parameters));
|
||||
}
|
||||
|
||||
public function testSelectWithChainedMethods() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect("Name","Meta")->setFrom("MyTable")->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'");
|
||||
$this->assertSQLEquals(
|
||||
"SELECT Name, Meta FROM MyTable WHERE (Name = 'Name') AND (Meta = 'Test')",
|
||||
@ -63,14 +63,14 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testCanSortBy() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect("Name","Meta")->setFrom("MyTable")->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'");
|
||||
$this->assertTrue($query->canSortBy('Name ASC'));
|
||||
$this->assertTrue($query->canSortBy('Name'));
|
||||
}
|
||||
|
||||
public function testSelectWithChainedFilterParameters() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array("Name","Meta"))->setFrom("MyTable");
|
||||
$query->setWhere("Name = 'Name'")->addWhere("Meta = 'Test'")->addWhere("Beta != 'Gamma'");
|
||||
$this->assertSQLEquals(
|
||||
@ -85,71 +85,71 @@ class SQLQueryTest extends SapphireTest {
|
||||
$this->markTestIncomplete();
|
||||
}
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setLimit(99);
|
||||
$this->assertSQLEquals("SELECT * FROM MyTable LIMIT 99", $query->sql($parameters));
|
||||
|
||||
// array limit with start (MySQL specific)
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setLimit(99, 97);
|
||||
$this->assertSQLEquals("SELECT * FROM MyTable LIMIT 99 OFFSET 97", $query->sql($parameters));
|
||||
}
|
||||
|
||||
public function testSelectWithOrderbyClause() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy('MyName');
|
||||
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC', $query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy('MyName desc');
|
||||
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC', $query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy('MyName ASC, Color DESC');
|
||||
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC, Color DESC', $query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy('MyName ASC, Color');
|
||||
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName ASC, Color ASC', $query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy(array('MyName' => 'desc'));
|
||||
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC', $query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy(array('MyName' => 'desc', 'Color'));
|
||||
$this->assertSQLEquals('SELECT * FROM MyTable ORDER BY MyName DESC, Color ASC', $query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy('implode("MyName","Color")');
|
||||
$this->assertSQLEquals(
|
||||
'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC',
|
||||
$query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy('implode("MyName","Color") DESC');
|
||||
$this->assertSQLEquals(
|
||||
'SELECT *, implode("MyName","Color") AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" DESC',
|
||||
$query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setOrderBy('RAND()');
|
||||
$this->assertSQLEquals(
|
||||
'SELECT *, RAND() AS "_SortColumn0" FROM MyTable ORDER BY "_SortColumn0" ASC',
|
||||
$query->sql($parameters));
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->addFrom('INNER JOIN SecondTable USING (ID)');
|
||||
$query->addFrom('INNER JOIN ThirdTable USING (ID)');
|
||||
@ -163,7 +163,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testNullLimit() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setLimit(null);
|
||||
|
||||
@ -174,7 +174,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testZeroLimit() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setLimit(0);
|
||||
|
||||
@ -190,7 +190,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
$this->markTestIncomplete();
|
||||
}
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom("MyTable");
|
||||
$query->setLimit(0, 99);
|
||||
|
||||
@ -204,7 +204,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testNegativeLimit() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setLimit(-10);
|
||||
}
|
||||
|
||||
@ -212,7 +212,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testNegativeOffset() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setLimit(1, -10);
|
||||
}
|
||||
|
||||
@ -220,12 +220,12 @@ class SQLQueryTest extends SapphireTest {
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testNegativeOffsetAndLimit() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setLimit(-10, -10);
|
||||
}
|
||||
|
||||
public function testReverseOrderBy() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('MyTable');
|
||||
|
||||
// default is ASC
|
||||
@ -258,42 +258,42 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testFiltersOnID() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("ID = 5");
|
||||
$this->assertTrue(
|
||||
$query->filtersOnID(),
|
||||
"filtersOnID() is true with simple unquoted column name"
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("ID=5");
|
||||
$this->assertTrue(
|
||||
$query->filtersOnID(),
|
||||
"filtersOnID() is true with simple unquoted column name and no spaces in equals sign"
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("Identifier = 5");
|
||||
$this->assertFalse(
|
||||
$query->filtersOnID(),
|
||||
"filtersOnID() is false with custom column name (starting with 'id')"
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("ParentID = 5");
|
||||
$this->assertFalse(
|
||||
$query->filtersOnID(),
|
||||
"filtersOnID() is false with column name ending in 'ID'"
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("MyTable.ID = 5");
|
||||
$this->assertTrue(
|
||||
$query->filtersOnID(),
|
||||
"filtersOnID() is true with table and column name"
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("MyTable.ID = 5");
|
||||
$this->assertTrue(
|
||||
$query->filtersOnID(),
|
||||
@ -302,28 +302,28 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testFiltersOnFK() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("ID = 5");
|
||||
$this->assertFalse(
|
||||
$query->filtersOnFK(),
|
||||
"filtersOnFK() is true with simple unquoted column name"
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("Identifier = 5");
|
||||
$this->assertFalse(
|
||||
$query->filtersOnFK(),
|
||||
"filtersOnFK() is false with custom column name (starting with 'id')"
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("MyTable.ParentID = 5");
|
||||
$this->assertTrue(
|
||||
$query->filtersOnFK(),
|
||||
"filtersOnFK() is true with table and column name"
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setWhere("MyTable.`ParentID`= 5");
|
||||
$this->assertTrue(
|
||||
$query->filtersOnFK(),
|
||||
@ -332,7 +332,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testInnerJoin() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('MyTable');
|
||||
$query->addInnerJoin('MyOtherTable', 'MyOtherTable.ID = 2');
|
||||
$query->addLeftJoin('MyLastTable', 'MyOtherTable.ID = MyLastTable.ID');
|
||||
@ -343,7 +343,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
$query->sql($parameters)
|
||||
);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('MyTable');
|
||||
$query->addInnerJoin('MyOtherTable', 'MyOtherTable.ID = 2', 'table1');
|
||||
$query->addLeftJoin('MyLastTable', 'MyOtherTable.ID = MyLastTable.ID', 'table2');
|
||||
@ -377,7 +377,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testSetWhereAny() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('MyTable');
|
||||
|
||||
$query->setWhereAny(array(
|
||||
@ -391,7 +391,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
|
||||
public function testSelectFirst() {
|
||||
// Test first from sequence
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy('"Name"');
|
||||
$result = $query->firstRow()->execute();
|
||||
@ -405,7 +405,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
$this->assertEquals('Object 1', $records[0]['Name']);
|
||||
|
||||
// Test first from empty sequence
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy('"Name"');
|
||||
$query->setWhere(array('"Name"' => 'Nonexistent Object'));
|
||||
@ -419,7 +419,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
$this->assertCount(0, $records);
|
||||
|
||||
// Test that given the last item, the 'first' in this list matches the last
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy('"Name"');
|
||||
$query->setLimit(1, 1);
|
||||
@ -436,7 +436,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
|
||||
public function testSelectLast() {
|
||||
// Test last in sequence
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy('"Name"');
|
||||
$result = $query->lastRow()->execute();
|
||||
@ -450,7 +450,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
$this->assertEquals('Object 2', $records[0]['Name']);
|
||||
|
||||
// Test last from empty sequence
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy('"Name"');
|
||||
$query->setWhere(array("\"Name\" = 'Nonexistent Object'"));
|
||||
@ -464,7 +464,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
$this->assertCount(0, $records);
|
||||
|
||||
// Test that given the first item, the 'last' in this list matches the first
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy('"Name"');
|
||||
$query->setLimit(1);
|
||||
@ -483,7 +483,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
* Tests aggregate() function
|
||||
*/
|
||||
public function testAggregate() {
|
||||
$query = new SQLSelect('"Common"');
|
||||
$query = new SQLQuery('"Common"');
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setGroupBy('"Common"');
|
||||
|
||||
@ -496,7 +496,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
* Tests that an ORDER BY is only added if a LIMIT is set.
|
||||
*/
|
||||
public function testAggregateNoOrderByIfNoLimit() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy('Common');
|
||||
$query->setLimit(array());
|
||||
@ -506,7 +506,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
$this->assertEquals(array(), $aggregate->getOrderBy());
|
||||
$this->assertEquals(array(), $limit);
|
||||
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy('Common');
|
||||
$query->setLimit(2);
|
||||
@ -524,7 +524,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
* because a subselect needs to be done to query paginated data.
|
||||
*/
|
||||
public function testOrderByContainingAggregateAndLimitOffset() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array('"Name"', '"Meta"'));
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy(array('MAX("Date")'));
|
||||
@ -547,7 +547,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
*/
|
||||
public function testOrderByMultiple() {
|
||||
if(DB::get_conn() instanceof MySQLDatabase) {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array('"Name"', '"Meta"'));
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->setOrderBy(array('MID("Name", 8, 1) DESC', '"Name" ASC'));
|
||||
@ -571,7 +571,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
* Test passing in a LIMIT with OFFSET clause string.
|
||||
*/
|
||||
public function testLimitSetFromClauseString() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect('*');
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
|
||||
@ -582,7 +582,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testParameterisedInnerJoins() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array('"SQLQueryTest_DO"."Name"', '"SubSelect"."Count"'));
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->addInnerJoin(
|
||||
@ -605,7 +605,7 @@ class SQLQueryTest extends SapphireTest {
|
||||
}
|
||||
|
||||
public function testParameterisedLeftJoins() {
|
||||
$query = new SQLSelect();
|
||||
$query = new SQLQuery();
|
||||
$query->setSelect(array('"SQLQueryTest_DO"."Name"', '"SubSelect"."Count"'));
|
||||
$query->setFrom('"SQLQueryTest_DO"');
|
||||
$query->addLeftJoin(
|
||||
|
@ -322,7 +322,7 @@ class VersionedTest extends SapphireTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that SQLSelect::queriedTables() applies the version-suffixes properly.
|
||||
* Test that SQLQuery::queriedTables() applies the version-suffixes properly.
|
||||
*/
|
||||
public function testQueriedTables() {
|
||||
Versioned::reading_stage('Live');
|
||||
|
Loading…
Reference in New Issue
Block a user