From 731874772c8798825857224918b05378b3c5433d Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Thu, 3 May 2012 12:01:50 +1200 Subject: [PATCH] BUGFIX Fixing broken SS3 query code in MSSQLDatabase --- code/MSSQLDatabase.php | 50 ++++++++++-------------------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/code/MSSQLDatabase.php b/code/MSSQLDatabase.php index af55bb8..59c610d 100644 --- a/code/MSSQLDatabase.php +++ b/code/MSSQLDatabase.php @@ -1214,8 +1214,6 @@ class MSSQLDatabase extends SS_Database { // DELETE queries if($sqlQuery->delete) { $text = 'DELETE '; - - // SELECT queries } else { $distinct = $sqlQuery->distinct ? "DISTINCT " : ""; @@ -1227,10 +1225,11 @@ class MSSQLDatabase extends SS_Database { // If there's a limit and an offset, then we need to do a subselect } else if($limit && $offset) { if($sqlQuery->orderby) { - $orderByFields = (method_exists($sqlQuery, 'prepareOrderBy')) ? $sqlQuery->prepareOrderBy() : $sqlQuery->orderby; + $orderByFields = $sqlQuery->prepareOrderBy(); $rowNumber = "ROW_NUMBER() OVER (ORDER BY $orderByFields) AS Number"; } else { - $firstCol = reset($sqlQuery->select); + $selects = $sqlQuery->itemisedSelect(); + $firstCol = reset($selects); $rowNumber = "ROW_NUMBER() OVER (ORDER BY $firstCol) AS Number"; } $text = "SELECT * FROM ( SELECT $distinct$rowNumber, "; @@ -1244,40 +1243,16 @@ class MSSQLDatabase extends SS_Database { } // Now add the columns to be selected - $text .= implode(", ", $sqlQuery->select); + $text .= $sqlQuery->prepareSelect(); } if($sqlQuery->from) $text .= ' FROM ' . implode(' ', $sqlQuery->from); + if($sqlQuery->where) $text .= ' WHERE (' . $sqlQuery->prepareWhere() . ')'; + if($sqlQuery->groupby) $text .= ' GROUP BY ' . $sqlQuery->prepareGroupBy(); + if($sqlQuery->having) $text .= ' HAVING ( ' . $sqlQuery->prepareHaving() . ' )'; - // 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, 'prepareWhere')) { - $text .= ' WHERE (' . $sqlQuery->prepareWhere() . ')'; - } 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(!$nestedQuery && $sqlQuery->orderby) { - if(method_exists($sqlQuery, 'prepareOrderBy')) { - $text .= ' ORDER BY ' . $sqlQuery->prepareOrderBy(); - } else { - $text .= ' ORDER BY ' . $sqlQuery->orderby; - } + $text .= ' ORDER BY ' . $sqlQuery->prepareOrderBy(); } // $suffixText is used by the nested queries to create an offset limit @@ -1803,7 +1778,6 @@ class MSSQLQuery extends SS_Query { } public function nextRecord() { - if(!is_resource($this->handle)) return false; // Coalesce rather than replace common fields. @@ -1826,7 +1800,7 @@ class MSSQLQuery extends SS_Query { return $output; } } else { - if($this->handle && $data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_NUMERIC)) { + if($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_NUMERIC)) { $fields = sqlsrv_field_metadata($this->handle); foreach($fields as $columnIdx => $field) { $value = $data[$columnIdx]; @@ -1842,10 +1816,8 @@ class MSSQLQuery extends SS_Query { return $output; } else { // Free the handle if there are no more results - sqlsrv crashes if there are too many handles - if($this->handle) { - sqlsrv_free_stmt($this->handle); - $this->handle = null; - } + sqlsrv_free_stmt($this->handle); + $this->handle = null; } }