BUGFIX Fixed BlogTree, ArchiveWidget SQL queries to work with ANSI and non-ANSI compatible queries

This commit is contained in:
Sean Harvey 2009-05-11 02:48:24 +00:00
parent 58775ea256
commit 23591eec35
2 changed files with 69 additions and 21 deletions

View File

@ -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();

View File

@ -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);
}
}