From: Brion Vibber Date: Fri, 17 Dec 2010 21:03:18 +0000 (-0800) Subject: Notice::whereSinceId() and Notice::whereMaxId() encapsulate logic for building where... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9e8bbff8ac3825dc789bcd19b1751fe24017a789;p=quix0rs-gnu-social.git Notice::whereSinceId() and Notice::whereMaxId() encapsulate logic for building where clauses for since_id/max_id parameters. Can override the field names from 'id' and 'created'. --- diff --git a/classes/Notice.php b/classes/Notice.php index b5a1b45971..14a91977df 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -668,14 +668,14 @@ class Notice extends Memcached_DataObject $notice->whereAdd('is_local !='. Notice::GATEWAY); } - $since = Notice::getAsTimestamp($since_id); + $since = Notice::whereSinceId($since_id); if ($since) { - $notice->whereAdd(sprintf("(created = '%s' and id > %d) or (created > '%s')", $since, $since_id, $since)); + $notice->whereAdd($since); } - $max = Notice::getAsTimestamp($max_id); + $max = Notice::whereMaxId($max_id); if ($max) { - $notice->whereAdd(sprintf("(created < '%s') or (created = '%s' and id <= %d)", $max, $max, $max_id)); + $notice->whereAdd($max); } $ids = array(); @@ -2006,4 +2006,44 @@ class Notice extends Memcached_DataObject return false; } + + /** + * Build an SQL 'where' fragment for timestamp-based sorting from a since_id + * parameter, matching notices posted after the given one (exclusive). + * + * If the referenced notice can't be found, will return false. + * + * @param int $id + * @param string $idField + * @param string $createdField + * @return mixed string or false if no match + */ + public static function whereSinceId($id, $idField='id', $createdField='created') + { + $since = Notice::getAsTimestamp($id); + if ($since) { + return sprintf("($createdField = '%s' and $idField > %d) or ($createdField > '%s')", $since, $id, $since); + } + return false; + } + + /** + * Build an SQL 'where' fragment for timestamp-based sorting from a max_id + * parameter, matching notices posted before the given one (inclusive). + * + * If the referenced notice can't be found, will return false. + * + * @param int $id + * @param string $idField + * @param string $createdField + * @return mixed string or false if no match + */ + public static function whereMaxId($id, $idField='id', $createdField='created') + { + $max = Notice::getAsTimestamp($id); + if ($max) { + return sprintf("($createdField < '%s') or ($createdField = '%s' and $idField <= %d)", $max, $max, $id); + } + return false; + } }