mirror of
https://github.com/silverstripe/silverstripe-sqlite3
synced 2024-10-22 17:05:37 +02:00
MINOR Removing sqlQueryToString as it's not required anymore in SS 3.0, SQLQuery does all the work for us instead.
This commit is contained in:
parent
932ffec5b6
commit
e9edf8b381
@ -1040,117 +1040,6 @@ class SQLite3Database extends SS_Database {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a SQLQuery object into a SQL statement
|
||||
*/
|
||||
public function sqlQueryToString(SQLQuery $sqlQuery) {
|
||||
if (!$sqlQuery->from) return '';
|
||||
$distinct = $sqlQuery->distinct ? "DISTINCT " : "";
|
||||
if($sqlQuery->delete) {
|
||||
$text = "DELETE ";
|
||||
} else if($sqlQuery->select) {
|
||||
$text = "SELECT $distinct" . implode(", ", $sqlQuery->select);
|
||||
}
|
||||
if($sqlQuery->from) $text .= " FROM " . implode(" ", $sqlQuery->from);
|
||||
|
||||
// method_exists is done here for legacy reasons, so we can use this on SS 2.4 and 3.0
|
||||
if($sqlQuery->where) {
|
||||
if(method_exists($sqlQuery, 'prepareSelect')) {
|
||||
$text .= ' WHERE (' . $sqlQuery->prepareSelect() . ')';
|
||||
} else {
|
||||
$text .= ' WHERE (' . $sqlQuery->getFilter() . ')';
|
||||
}
|
||||
}
|
||||
if($sqlQuery->groupby) {
|
||||
if(method_exists($sqlQuery, 'prepareGroupBy')) {
|
||||
$text .= ' GROUP BY ' . $sqlQuery->prepareGroupBy();
|
||||
} else {
|
||||
$text .= ' GROUP BY ' . implode(', ', $sqlQuery->groupby);
|
||||
}
|
||||
}
|
||||
if($sqlQuery->having) {
|
||||
if(method_exists($sqlQuery, 'prepareHaving')) {
|
||||
$text .= ' HAVING ( ' . $sqlQuery->prepareHaving() . ' )';
|
||||
} else {
|
||||
$text .= ' HAVING ( ' . implode(' ) AND ( ', $sqlQuery->having) . ' )';
|
||||
}
|
||||
}
|
||||
if($sqlQuery->orderby) {
|
||||
if(method_exists($sqlQuery, 'prepareOrderBy')) {
|
||||
$text .= ' ORDER BY ' . $sqlQuery->prepareOrderBy();
|
||||
} else {
|
||||
// orderMoreSpecifically isn't needed in SS 3.0, as SQLQuery does this for us instead
|
||||
$text .= ' ORDER BY ' . $this->orderMoreSpecifically($sqlQuery->select, $sqlQuery->orderby);
|
||||
}
|
||||
}
|
||||
|
||||
if($sqlQuery->limit) {
|
||||
$limit = $sqlQuery->limit;
|
||||
// Pass limit as array or SQL string value
|
||||
if(is_array($limit)) {
|
||||
if(!array_key_exists('limit',$limit)) user_error('SQLQuery::limit(): Wrong format for $limit', E_USER_ERROR);
|
||||
|
||||
if(isset($limit['start']) && is_numeric($limit['start']) && isset($limit['limit']) && is_numeric($limit['limit'])) {
|
||||
$combinedLimit = "$limit[limit] OFFSET $limit[start]";
|
||||
} elseif(isset($limit['limit']) && is_numeric($limit['limit'])) {
|
||||
$combinedLimit = (int)$limit['limit'];
|
||||
} else {
|
||||
$combinedLimit = false;
|
||||
}
|
||||
if(!empty($combinedLimit)) $text .= " LIMIT " . $combinedLimit;
|
||||
|
||||
} else {
|
||||
$text .= " LIMIT " . $sqlQuery->limit;
|
||||
}
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* SQLite3 complains about ambiguous column names if the ORDER BY expression doesn't contain the table name
|
||||
* and the expression matches more than one expression in the SELECT expression.
|
||||
* assuming that there is no amibguity we just use the first table name
|
||||
*
|
||||
* used by SQLite3Database::sqlQueryToString()
|
||||
*
|
||||
* @param array $select SELECT expressions as of SQLquery
|
||||
* @param string $order ORDER BY expressions to be checked and augmented as of SQLquery
|
||||
* @return string fully specified ORDER BY expression
|
||||
*/
|
||||
protected function orderMoreSpecifically($select,$order) {
|
||||
|
||||
$altered = false;
|
||||
|
||||
// split expression into order terms
|
||||
$terms = explode(',', $order);
|
||||
|
||||
foreach($terms as $i => $term) {
|
||||
$term = trim($term);
|
||||
|
||||
// check if table is unspecified
|
||||
if(!preg_match('/\./', $term)) {
|
||||
$direction = '';
|
||||
if(preg_match('/( ASC)$|( DESC)$/i',$term)) list($term,$direction) = explode(' ', $term);
|
||||
|
||||
// find a match in the SELECT array and replace
|
||||
foreach($select as $s) {
|
||||
if(preg_match('/"[a-z0-9_]+"\.[\'"]?' . $term . '[\'"]?/i', trim($s))) {
|
||||
$terms[$i] = $s . ' ' . $direction;
|
||||
$altered = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return implode(',', $terms);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper functions to prepare DBMS specific SQL fragments for basic datetime operations
|
||||
*/
|
||||
|
||||
/**
|
||||
* Function to return an SQL datetime expression that can be used with SQLite3
|
||||
* used for querying a datetime in a certain format
|
||||
|
Loading…
Reference in New Issue
Block a user