From 23591eec35afd4bd74a030bbcaba03ac813e1a6a Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Mon, 11 May 2009 02:48:24 +0000 Subject: [PATCH] BUGFIX Fixed BlogTree, ArchiveWidget SQL queries to work with ANSI and non-ANSI compatible queries --- code/ArchiveWidget.php | 43 ++++++++++++++++++++++++++------------ code/BlogTree.php | 47 +++++++++++++++++++++++++++++++++++------- 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/code/ArchiveWidget.php b/code/ArchiveWidget.php index 7cec3ae..7cf20df 100644 --- a/code/ArchiveWidget.php +++ b/code/ArchiveWidget.php @@ -51,21 +51,38 @@ class ArchiveWidget extends Widget { $stage = Versioned::current_stage(); $suffix = (!$stage || $stage == 'Stage') ? "" : "_$stage"; - if($this->DisplayMode == 'month') { - $sqlResults = DB::query(" - SELECT DISTINCT MONTH(`Date`) AS `Month`, YEAR(`Date`) AS `Year` - FROM `SiteTree$suffix` NATURAL JOIN `BlogEntry$suffix` - WHERE `ParentID` in (".implode(', ',$ids).") - ORDER BY `Date` DESC" - ); + if(defined('Database::USE_ANSI_SQL')) { + $sqlResults = DB::query(" + SELECT DISTINCT MONTH(\"Date\") AS \"Month\", YEAR(\"Date\") AS \"Year\", \"Date\" + FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\" + WHERE \"ParentID\" IN (" . implode(', ', $ids) . ") + ORDER BY \"Date\" DESC" + ); + } else { + $sqlResults = DB::query(" + SELECT DISTINCT MONTH(`Date`) AS `Month`, YEAR(`Date`) AS `Year` + FROM `SiteTree$suffix` NATURAL JOIN `BlogEntry$suffix` + WHERE `ParentID` IN (" . implode(', ', $ids) . ") + ORDER BY `Date` DESC" + ); + } } else { - $sqlResults = DB::query(" - SELECT DISTINCT YEAR(`Date`) AS `Year` - FROM `SiteTree$suffix` NATURAL JOIN `BlogEntry$suffix` - WHERE `ParentID` in (".implode(', ',$ids).") - ORDER BY `Date` DESC" - ); + if(defined('Database::USE_ANSI_SQL')) { + $sqlResults = DB::query(" + SELECT DISTINCT YEAR(\"Date\") AS \"Year\", \"Date\" + FROM \"SiteTree$suffix\" INNER JOIN \"BlogEntry$suffix\" ON \"SiteTree$suffix\".\"ID\" = \"BlogEntry$suffix\".\"ID\" + WHERE \"ParentID\" IN (" . implode(', ', $ids) . ") + ORDER BY \"Date\" DESC" + ); + } else { + $sqlResults = DB::query(" + SELECT DISTINCT YEAR(`Date`) AS `Year` + FROM `SiteTree$suffix` NATURAL JOIN `BlogEntry$suffix` + WHERE `ParentID` in (".implode(', ',$ids).") + ORDER BY `Date` DESC" + ); + } } if(!$sqlResults) return new DataObjectSet(); diff --git a/code/BlogTree.php b/code/BlogTree.php index a5f0dfe..0137385 100644 --- a/code/BlogTree.php +++ b/code/BlogTree.php @@ -46,7 +46,12 @@ class BlogTree extends Page { } // Try to find a top-level BlogTree - $top = DataObject::get_one('BlogTree','ParentId = 0'); + if(defined('Database::USE_ANSI_SQL')) { + $top = DataObject::get_one('BlogTree', "\"ParentID\" = '0'"); + } else { + $top = DataObject::get_one('BlogTree', 'ParentID = 0'); + } + if ($top) return $top; // Try to find any BlogTree that is not inside another BlogTree @@ -136,7 +141,11 @@ class BlogTree extends Page { if ($tag) { $SQL_tag = Convert::raw2sql($tag); - $tagCheck = "AND `BlogEntry`.Tags LIKE '%$SQL_tag%'"; + if(defined('Database::USE_ANSI_SQL')) { + $tagCheck = "AND \"BlogEntry\".\"Tags\" LIKE '%$SQL_tag%'"; + } else { + $tagCheck = "AND `BlogEntry`.Tags LIKE '%$SQL_tag%'"; + } } if ($date) { @@ -145,17 +154,29 @@ class BlogTree extends Page { $month = (int) substr($date, strpos($date, '-') + 1); if($year && $month) { - $dateCheck = "AND MONTH(`BlogEntry`.Date) = $month AND YEAR(`BlogEntry`.Date) = $year"; + if(defined('Database::USE_ANSI_SQL')) { + $dateCheck = "AND MONTH(\"BlogEntry\".\"Date\") = '$month' AND YEAR(\"BlogEntry\".\"Date\") = '$year'"; + } else { + $dateCheck = "AND MONTH(`BlogEntry`.Date) = $month AND YEAR(`BlogEntry`.Date) = $year"; + } } } else { $year = (int) $date; if($year) { - $dateCheck = "AND YEAR(`BlogEntry`.Date) = $year"; + if(defined('Database::USE_ANSI_SQL')) { + $dateCheck = "AND YEAR(\"BlogEntry\".\"Date\") = '$year'"; + } else { + $dateCheck = "AND YEAR(`BlogEntry`.Date) = $year"; + } } } } - elseif ($this->LandingPageFreshness) { - $dateCheck = "AND `BlogEntry`.Date > NOW() - INTERVAL " . $this->LandingPageFreshness; + elseif ($this->LandingPageFreshness) { + if(defined('Database::USE_ANSI_SQL')) { + $dateCheck = "AND \"BlogEntry\".\"Date\" > NOW() - INTERVAL " . $this->LandingPageFreshness; + } else { + $dateCheck = "AND `BlogEntry`.Date > NOW() - INTERVAL " . $this->LandingPageFreshness; + } } // Build a list of all IDs for BlogHolders that are children of us @@ -165,11 +186,21 @@ class BlogTree extends Page { if (empty($holderIDs)) return false; // Otherwise, do the actual query - $where = 'ParentID IN ('.implode(',', $holderIDs).") $tagCheck $dateCheck"; + if(defined('Database::USE_ANSI_SQL')) { + $where = '"ParentID" IN (' . implode(',', $holderIDs) . ") $tagCheck $dateCheck"; + } else { + $where = 'ParentID IN (' . implode(',', $holderIDs) . ") $tagCheck $dateCheck"; + } + + if(defined('Database::USE_ANSI_SQL')) { + $order = '"BlogEntry"."Date" DESC'; + } else { + $order = '`BlogEntry`.`Date` DESC'; + } // By specifying a callback, you can alter the SQL, or sort on something other than date. if ($retrieveCallback) return call_user_func($retrieveCallback, 'BlogEntry', $where, $limit); - else return DataObject::get('BlogEntry', $where, '`BlogEntry`.`Date` DESC', '', $limit); + else return DataObject::get('BlogEntry', $where, $order, '', $limit); } }