From: Michael G Schwern Date: Fri, 14 Mar 2008 03:16:36 +0000 (+0000) Subject: Improve add_constructor() support to handle ORDER BY and LIMIT with newlines in X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=28f7f7d31e0d17c5362935ac52f7e67a189ddf0f;p=dbsrgits%2FDBIx-Class-Historic.git Improve add_constructor() support to handle ORDER BY and LIMIT with newlines in the SQL --- diff --git a/lib/DBIx/Class/CDBICompat/Constructor.pm b/lib/DBIx/Class/CDBICompat/Constructor.pm index 4077224..f44079b 100644 --- a/lib/DBIx/Class/CDBICompat/Constructor.pm +++ b/lib/DBIx/Class/CDBICompat/Constructor.pm @@ -8,10 +8,15 @@ sub add_constructor { my ($class, $meth, $sql) = @_; $class = ref $class if ref $class; no strict 'refs'; + + my %attrs; + $attrs{rows} = $1 if $sql =~ s/LIMIT\s+(.*)\s+$//i; + $attrs{order_by} = $1 if $sql =~ s/ORDER BY\s+(.*)//i; + *{"${class}::${meth}"} = sub { my ($class, @args) = @_; - return $class->search_literal($sql, @args); + return $class->search_literal($sql, @args, \%attrs); }; } diff --git a/t/cdbi-t/02-Film.t b/t/cdbi-t/02-Film.t index 57ad411..ee28a68 100644 --- a/t/cdbi-t/02-Film.t +++ b/t/cdbi-t/02-Film.t @@ -9,7 +9,7 @@ BEGIN { next; } eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 96); + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 98); } INIT { @@ -126,6 +126,11 @@ is($blrunner_dc->NumExplodingSheep, undef, 'Sheep correct'); { Film->add_constructor(title_asc => "title LIKE ? ORDER BY title"); Film->add_constructor(title_desc => "title LIKE ? ORDER BY title DESC"); + Film->add_constructor(title_asc_nl => q{ + title LIKE ? + ORDER BY title + LIMIT 1 + }); { my @films = Film->title_asc("Bladerunner%"); @@ -137,6 +142,11 @@ is($blrunner_dc->NumExplodingSheep, undef, 'Sheep correct'); is @films, 2, "We have 2 Bladerunners"; is $films[0]->Title, $blrunner_dc->Title, "Ordered correctly"; } + { + my @films = Film->title_asc_nl("Bladerunner%"); + is @films, 1, "We have 2 Bladerunners"; + is $films[0]->Title, $blrunner->Title, "Ordered correctly"; + } } # Multi-column search