summaryrefslogtreecommitdiff
path: root/lib/VNDB/DB/VN.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2009-11-27 14:48:20 +0100
committerYorhel <git@yorhel.nl>2009-11-27 14:52:20 +0100
commit9613533da2c58af3c64e3bd4ed7b92b22ffd442f (patch)
tree425ee689a98a555638431f92d1bc76cbef22eedd /lib/VNDB/DB/VN.pm
parent542c1e22daf2648b3babce2ab2050120eec0a9b9 (diff)
DB: Abstracted all ORDER BY clauses in the DB abstraction layer
The ORDER BY was previously specified using an 'order' argument, which would then be directly inserted into the query. The new method is the same as what I used for the public API: two 'sort' and 'reverse' arguments. This should be less error-prone and more readable. This changes quite a lot of code, so I hope I haven't forgotten to update something along the way.
Diffstat (limited to 'lib/VNDB/DB/VN.pm')
-rw-r--r--lib/VNDB/DB/VN.pm18
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/VNDB/DB/VN.pm b/lib/VNDB/DB/VN.pm
index 55df48ee..feaa9bd0 100644
--- a/lib/VNDB/DB/VN.pm
+++ b/lib/VNDB/DB/VN.pm
@@ -10,13 +10,13 @@ use Encode 'decode_utf8';
our @EXPORT = qw|dbVNGet dbVNAdd dbVNEdit dbVNImageId dbVNCache dbScreenshotAdd dbScreenshotGet dbScreenshotRandom|;
-# Options: id, rev, char, search, lang, platform, tags_include, tags_exclude, results, page, order, what
+# Options: id, rev, char, search, lang, platform, tags_include, tags_exclude, results, page, what, sort, reverse
# What: extended anime relations screenshots relgraph rating ranking changes
+# Sort: id rel pop rating title tagscore rand
sub dbVNGet {
my($self, %o) = @_;
$o{results} ||= 10;
$o{page} ||= 1;
- $o{order} ||= 'vr.title ASC';
$o{what} ||= '';
my %where = (
@@ -97,13 +97,23 @@ sub dbVNGet {
qq|(SELECT AVG(tvh.rating) FROM tags_vn_inherit tvh WHERE tvh.tag IN($tag_ids) AND tvh.vid = v.id AND spoiler <= $o{tags_include}[0] GROUP BY tvh.vid) AS tagscore| : (),
);
+ my $order = sprintf {
+ id => 'id %s',
+ rel => 'c_released %s',
+ pop => 'c_popularity %s NULLS LAST',
+ rating => 'c_rating %s NULLS LAST',
+ title => 'title %s',
+ tagscore => 'tagscore %s',
+ rand => 'RANDOM()',
+ }->{ $o{sort}||'title' }, $o{reverse} ? 'DESC' : 'ASC';
+
my($r, $np) = $self->dbPage(\%o, q|
SELECT !s
FROM vn_rev vr
!s
!W
- ORDER BY !s NULLS LAST|,
- join(', ', @select), join(' ', @join), \%where, $o{order},
+ ORDER BY !s|,
+ join(', ', @select), join(' ', @join), \%where, $order,
);
if($o{what} =~ /relgraph/) {