SQL-Abstract-Limit-0.143/0000755000000000000000000000000014030153742013472 5ustar rootrootSQL-Abstract-Limit-0.143/t/0000755000000000000000000000000014030153737013741 5ustar rootrootSQL-Abstract-Limit-0.143/t/test_data.csv0000644000000000000000000000155113756317037016441 0ustar rootroot"code","county" "ABD","Aberdeenshire" "ANS","Angus (formerly Forfarshire)" "ARL","Argyllshire (includes Islay, Jura and Mull)" "AYR","Ayrshire" "BAN","Banffshire" "BEW","Berwickshire" "BUT","Bute (includes Arran and Bute)" "CAI","Caithness" "CLK","Clackmannanshire" "DNB","Dunbartonshire" "DFS","Dumfriesshire" "ELN","East Lothian" "FIF","Fife" "INV","Inverness-shire (includes part of Lewis, North Uist, South Uist, and Skye)" "KCD","Kincardineshire" "KRS","Kinross-shire" "KKD","Kirkcudbrightshire" "LKS","Lanarkshire" "MLN","Midlothian" "MOR","Morayshire" "NAI","Nairn" "OKI","Orkney" "PEE","Peebles-shire" "PER","Perth" "RFW","Renfrewshire (includes part of Lewis)" "ROC","Ross and Cromarty" "ROX","Roxburghshire" "SEL","Selkirkshire" "SHI","Shetland" "STI","Stirlingshire" "SUT","Sutherland" "WLN","West Lothian" "WIG","Wigtownshire" SQL-Abstract-Limit-0.143/t/pod-coverage.t0000644000000000000000000000026213756317037016512 0ustar rootroot#!perl -T use Test::More; eval "use Test::Pod::Coverage 1.04"; plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; all_pod_coverage_ok(); SQL-Abstract-Limit-0.143/t/02.syntax.t0000644000000000000000000000427613756317037015716 0ustar rootroot#!/usr/bin/perl use strict; use warnings; use Test::More tests => 7; use Test::Exception; use SQL::Abstract::Limit; use Cwd; SKIP: { eval { require DBD::AnyData; require Class::DBI; }; skip( "need DBD::AnyData and Class::DBI to test syntax auto-detection", 7 ) if $@; =for notes LimitOffset PostgreSQL, MySQL (recent), SQLite LimitXY MySQL (older) LimitYX SQLite (optional) RowsTo InterBase/FireBird Top SQL/Server, MS Access RowNum Oracle FetchFirst DB2 # not implemented yet Skip Informix GenericSubQ Sybase, plus any databases not recognised by this module $dbh a DBI database handle CDBI subclass CDBI object %SQL::Abstract::Limit::Syntax = ( mssql => 'Top', access => 'Top', sybase => 'GenericSubQ', oracle => 'RowNum', ... =cut my $cwd = getcwd; { package TestApp; #use base 'Class::DBI'; # don't attempt to load if not installed our @ISA = ('Class::DBI'); my $dsn = 'dbi:AnyData(RaiseError=>1):'; __PACKAGE__->set_db( 'Main', $dsn, '', '' ); __PACKAGE__->db_Main->func( 'county', 'CSV', "$cwd/t/test_data.csv", 'ad_catalog'); __PACKAGE__->table( 'county' ); __PACKAGE__->columns( All => qw/ code county / ); } my $sql_ab = SQL::Abstract::Limit->new; my $inv = TestApp->retrieve( 'INV' ); like( $inv->county, qr(^Inverness), 'retrieved record' ); my ( $syntax, $db ); lives_ok { $syntax = $sql_ab->_find_syntax( 'TestApp' ) } '_find_syntax CDBI class'; like( $syntax, qr(^LimitXY$), 'CSV syntax from CDBI class' ); lives_ok { $syntax = $sql_ab->_find_syntax( $inv ) } '_find_syntax CDBI object'; like( $syntax, qr(^LimitXY$), 'CSV syntax from CDBI object' ); my ( $dbh ) = TestApp->db_handles; $dbh || die 'no dbh'; lives_ok { $syntax = $sql_ab->_find_syntax( $dbh ) } '_find_syntax $dbh'; like( $syntax, qr(^LimitXY$), 'CSV syntax from $dbh' ); # warn "syntax: $syntax"; } SQL-Abstract-Limit-0.143/t/pod.t0000644000000000000000000000022213756317037014715 0ustar rootroot#!perl -T use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok(); SQL-Abstract-Limit-0.143/t/04.args.t0000644000000000000000000000205713756317037015321 0ustar rootroot#!/usr/bin/perl use strict; use warnings; use Test::More tests => 3; use Test::Exception; use SQL::Abstract::Limit; use SQL::Abstract; # 1, 2 { my $sql = SQL::Abstract::Limit->new( limit_dialect => 'LimitOffset' ); # table fields where order rows offset my $stmt = $sql->select("MY_TABLE", "*", undef, ["id"], undef, undef); my $expect = 'SELECT * FROM MY_TABLE ORDER BY id'; like( $stmt, qr~\Q$expect\E~, 'no-LIMIT with ORDER BY' ); #my $sql_ab = SQL::Abstract->new; my $stmt2 = SQL::Abstract->new->select("MY_TABLE", "*", undef, ["id"]); like( $stmt2, qr~\Q$expect\E~, 'SQL::Abstract base stmt' ); } # 3 bug in pre-0.1: order clause missing if no limit clause specified { my $sql = SQL::Abstract::Limit->new( limit_dialect => 'LimitOffset' ); my $stmt = $sql->where( { fee => 'fi' }, ["id"] ); like( $stmt, qr/ORDER BY id/, 'got an order_by clause' ); #warn $stmt; } SQL-Abstract-Limit-0.143/t/03.subclass.t0000644000000000000000000000203414030152177016162 0ustar rootroot#!/usr/bin/perl use strict; use warnings; use Test::More tests => 3; use Test::Exception; { package MyLimit; use base 'SQL::Abstract::Limit'; sub emulate_limit { 'yah' } } my ( $sql, $stmt ); lives_ok { $sql = MyLimit->new; $stmt = $sql->select( 'table', [ 'col1', 'col2' ], { this => 'that' }, [ 'col3 ASC' ], 10, 100, ) } 'my own limit'; like( $stmt, qr(^yah$), 'custom LIMIT' ); $stmt = $sql->select( 'table', [ 'col1', 'col2' ], { this => 'that' }, ); like( $stmt, qr(^\QSELECT col1, col2 FROM table WHERE ( this = ? )\E$|^\QSELECT col1, col2 FROM table WHERE this = ?\E$), 'SQL::Abstract - no limit' ); SQL-Abstract-Limit-0.143/t/00.load.t0000644000000000000000000000022713756317037015275 0ustar rootrootuse Test::More tests => 1; BEGIN { use_ok( 'SQL::Abstract::Limit' ); } diag( "Testing SQL::Abstract::Limit $SQL::Abstract::Limit::VERSION" ); SQL-Abstract-Limit-0.143/t/lib/0000755000000000000000000000000014030154061014476 5ustar rootrootSQL-Abstract-Limit-0.143/t/lib/SQL/0000755000000000000000000000000014030153737015146 5ustar rootrootSQL-Abstract-Limit-0.143/t/lib/SQL/Abstract/0000755000000000000000000000000014030153737016711 5ustar rootrootSQL-Abstract-Limit-0.143/t/lib/SQL/Abstract/Limit/0000755000000000000000000000000014030153737017767 5ustar rootrootSQL-Abstract-Limit-0.143/t/lib/SQL/Abstract/Limit/Test.pm0000644000000000000000000000707413756317037021265 0ustar rootrootpackage SQL::Abstract::Limit::Test; # Lifted from DBIx::Class, originally was DBIC::SqlMakerTest. use strict; use warnings; use base qw/Test::Builder::Module Exporter/; use Exporter; our @EXPORT = qw/ &is_same_sql_bind &eq_sql &eq_bind /; { package DBIC::SqlMakerTest::SQLATest; # replacement for SQL::Abstract::Test if not available use strict; use warnings; use base qw/Test::Builder::Module Exporter/; use Scalar::Util qw(looks_like_number blessed reftype); use Data::Dumper; use Test::Builder; use Test::Deep qw(eq_deeply); our $tb = __PACKAGE__->builder; sub is_same_sql_bind { my ($sql1, $bind_ref1, $sql2, $bind_ref2, $msg) = @_; my $same_sql = eq_sql($sql1, $sql2); my $same_bind = eq_bind($bind_ref1, $bind_ref2); $tb->ok($same_sql && $same_bind, $msg); if (!$same_sql) { $tb->diag("SQL expressions differ\n" . " got: $sql1\n" . "expected: $sql2\n" ); } if (!$same_bind) { $tb->diag("BIND values differ\n" . " got: " . Dumper($bind_ref1) . "expected: " . Dumper($bind_ref2) ); } } sub eq_sql { my ($left, $right) = @_; $left =~ s/\s+//g; $right =~ s/\s+//g; return $left eq $right; } sub eq_bind { my ($bind_ref1, $bind_ref2) = @_; return eq_deeply($bind_ref1, $bind_ref2); } } eval "use SQL::Abstract::Test;"; if ($@ eq '') { # SQL::Abstract::Test available *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind; *eq_sql = \&SQL::Abstract::Test::eq_sql; *eq_bind = \&SQL::Abstract::Test::eq_bind; } else { # old SQL::Abstract *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind; *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql; *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind; } 1; __END__ =head1 NAME SQL::Abstract::Limit::Test - Helper package for testing generated SQL and bind values =head1 SYNOPSIS use Test::More; use SQL::Abstract::Limit::Test; my ($sql, @bind) = $schema->storage->sql_maker->select(%args); is_same_sql_bind( $sql, \@bind, $expected_sql, \@expected_bind, 'foo bar works' ); =head1 DESCRIPTION Exports functions that can be used to compare generated SQL and bind values. If L (packaged in L versions 1.50 and above) is available, then it is used to perform the comparisons (all functions are delegated to id). Otherwise uses simple string comparison for the SQL statements and simple L-like recursive stringification for comparison of bind values. =head1 FUNCTIONS =head2 is_same_sql_bind is_same_sql_bind( $given_sql, \@given_bind, $expected_sql, \@expected_bind, $test_msg ); Compares given and expected pairs of C<($sql, \@bind)>, and calls L on the result, with C<$test_msg> as message. =head2 eq_sql my $is_same = eq_sql($given_sql, $expected_sql); Compares the two SQL statements. Returns true IFF they are equivalent. =head2 eq_bind my $is_same = eq_sql(\@given_bind, \@expected_bind); Compares two lists of bind values. Returns true IFF their values are the same. =head1 SEE ALSO L, L, L. =head1 AUTHOR Norbert Buchmuller, =head1 COPYRIGHT AND LICENSE Copyright 2008 by Norbert Buchmuller. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SQL-Abstract-Limit-0.143/t/01.sql.t0000644000000000000000000001040713756317037015157 0ustar rootroot#!/usr/bin/perl use strict; use warnings; use Test::More tests => 17; use Test::Exception; use lib qw(t/lib); # dynamically load SQL::Abstract::Test; eval "use SQL::Abstract::Limit::Test; 1" or die $@; =for notes use SQL::Abstract::Limit; my $syntax = 'LimitOffset'; # others include: Top RowNum LimitXY Fetch RowsTo my $sql = SQL::Abstract::Limit->new( limit => $syntax ); my($stmt, @bind) = $sql->select($table, \@fields, \%where, \@order, $limit, $offset); =cut use SQL::Abstract::Limit; my @syntaxes = qw( LimitOffset LimitXY RowsTo Top RowNum GenericSubQ FetchFirst shgfh ); my @not_syntaxes = qw( Rank ); lives_ok { SQL::Abstract::Limit->new( limit => $_ ) for @syntaxes } 'survives constructor'; # query my $table = 'TheTable'; my $fields = [ qw( requestor worker colC colH ) ]; my $where = { requestor => 'inna', worker => ['nwiger', 'rcwe', 'sfz'], status => { '!=', 'completed' }, }; my $order = [ qw( pay age ) ]; my $limit = 10; # 10 per page my $offset = 70; # page 7 my $last = $offset + $limit; my $base_sql = 'requestor, worker, colC, colH FROM TheTable WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR ( worker = ? ) OR ( worker = ? ) ) )'; my @expected_bind = qw/inna completed nwiger rcwe sfz/; my $sql_ab = SQL::Abstract::Limit->new( limit_dialect => 'LimitOffset' ); my ( $stmt, @bind ); # LimitOffset lives_ok { ( $stmt, @bind ) = $sql_ab->select( $table, $fields, $where, $order, $limit, $offset) } 'select LimitOffset'; is_same_sql_bind( $stmt, \@bind, "SELECT $base_sql ORDER BY pay, age LIMIT $limit OFFSET $offset", \@expected_bind, 'LimitOffset SQL', ); # LimitXY lives_ok { ( $stmt, @bind ) = $sql_ab->select( $table, $fields, $where, $order, $limit, $offset, 'LimitXY' ) } 'select LimitXY'; is_same_sql_bind( $stmt, \@bind, "SELECT $base_sql ORDER BY pay, age LIMIT $offset, $limit", \@expected_bind, 'LimitXY SQL', ); # RowsTo lives_ok { ( $stmt, @bind ) = $sql_ab->select( $table, $fields, $where, $order, $limit, $offset, 'RowsTo' ) } 'select RowsTo'; is_same_sql_bind( $stmt, \@bind, "SELECT $base_sql ORDER BY pay, age ROWS $offset TO $last", \@expected_bind, 'RowsTo SQL', ); # Top lives_ok { ( $stmt, @bind ) = $sql_ab->select( $table, $fields, $where, $order, $limit, $offset, 'Top' ) } 'select Top'; is_same_sql_bind( $stmt, \@bind, "SELECT * FROM (" . "SELECT TOP $limit * FROM (" . "SELECT TOP $last $base_sql ORDER BY pay ASC, age ASC" . ") AS foo ORDER BY pay DESC, age DESC" .") AS bar ORDER BY pay ASC, age ASC", \@expected_bind, 'Top SQL', ); # RowNum lives_ok { ( $stmt, @bind ) = $sql_ab->select( $table, $fields, $where, $order, $limit, $offset, 'RowNum' ) } 'select RowNum'; is_same_sql_bind( $stmt, \@bind, "SELECT * FROM (" . "SELECT A.*, ROWNUM r FROM (" . "SELECT $base_sql ORDER BY pay, age" . ") A WHERE ROWNUM < @{[$last + 1]}" .") B WHERE r >= @{[$offset + 1]}", \@expected_bind, 'RowNum SQL', ); # GenericSubQ lives_ok { ( $stmt, @bind ) = $sql_ab->select( $table, $fields, $where, $order, $limit, $offset, 'GenericSubQ' ) } 'select GenericSubQ'; (my $gen_q_base_sql = $base_sql) =~ s/TheTable/TheTable X/; is_same_sql_bind( $stmt, \@bind, "SELECT $gen_q_base_sql AND" . "(SELECT COUNT(*) FROM TheTable WHERE requestor > X.requestor)" . " BETWEEN $offset AND $last ORDER BY requestor DESC", \@expected_bind, 'GenericSubQ SQL', ); # FetchFirst lives_ok { ( $stmt, @bind ) = $sql_ab->select( $table, $fields, $where, $order, $limit, $offset, 'FetchFirst' ) } 'select FetchFirst'; is_same_sql_bind( $stmt, \@bind, "SELECT * FROM (" . "SELECT * FROM (" . "SELECT $base_sql ORDER BY pay ASC, age ASC FETCH FIRST $last ROWS ONLY" . ") foo ORDER BY pay DESC, age DESC FETCH FIRST $limit ROWS ONLY" . ") bar ORDER BY pay ASC, age ASC", \@expected_bind, 'FetchFirst SQL', ); # Skip lives_ok { ( $stmt, @bind ) = $sql_ab->select( $table, $fields, $where, $order, $limit, $offset, 'Skip' ) } 'select Skip'; is_same_sql_bind( $stmt, \@bind, "select skip $offset limit $limit $base_sql ORDER BY pay, age", \@expected_bind, 'Skip SQL', ); SQL-Abstract-Limit-0.143/Changes0000644000000000000000000000523114030152601014757 0ustar rootrootRevision history for SQL-Abstract-Limit CHANGES ** indicates API changes 0.143 2021-03-28 - fix RT #134147: Failed test 'SQL::Abstract - no limit' / Thanks to Slaven Rezić and Thibault Duponchelle 0.142 2020-11-22 - module adopted by Alexander Becker - set up GitHub repository. See this repository for detailed changes. - fix RT #34847: Avoid double quoting of table name. Fix provided by @dekimsey / Daniel Kimsey - fix RT #50795: SQL::Abstract properties were ignored for select() if no limit was given. Thanks to Gunnar Hansson - fix RT #73444: Offset argument wasn't handled properly if the value was 0. Thanks to ALEXBYK - fix RT #62671: Properly handle 0 values in condition. - remove Build.PL, switch to EUMM 0.141 22nd December 2008, 22:13 - fixed pod syntax 0.14 22nd December 2008, 14:48 - added support for Informix, provided by Paul Falbe. 0.13 21st December 2008, 23:20 - updated test suite to play with the latest release of SQL::Abstract. Patches supplied by the SQL::Abstract dev team. 0.12 19th December 2005, 23:20 - removed hidden dependency on Class::DBI in the test suite. 0.11 11th October 2005, 12:40 - re-arranged order of tests in _find_syntax() to avoid the eval where possible - if the calling app has overridden die(), the eval may give spurious results - reported by support@2rad.net (rt bug #15000) 0.101 9th September 2005 - changed call to _table to match API modified in SQL::Abstract 1.20 - ** requires SQL::Abstract 1.20 or greater 0.1 18th August 2005 - fixed bug in where() - if an order clasue, but no limit clause, was specified, then the order clause was dropped - reported by Dan Sully - added 04.args.t to MANIFEST 0.033 15th March 2005 - fixed bug where the order-by argument was ignored if no limit arguments (rows, offset) were supplied (reported by Emmanuel Engelhart) 0.032 15th Jan 2005 - ** made the WHERE clause required - stopped checking that the WHERE clause is a hashref - SQL::Abstract accepts arrayrefs too 0.031 15th Jan 2005 - added Makefile.PL to the MANIFEST 0.03 15th Jan 2005 - removed underscore - this thing seems to work - fixed bug in _FetchFirst that was duplicating 'ORDER BY' (reported by Emanuele Zeppieri) - added a Makefile.PL option to Build.PL 0.02_2 15th Nov 2004 - really put the underscore in the version 0.02_1 15th Nov 2004 - added DB2 support - fixed bug in Build.PL - added developer release flag (the underscore) to version string 0.01 4th Nov 2004 - initial release SQL-Abstract-Limit-0.143/META.json0000644000000000000000000000250514030153742015115 0ustar rootroot{ "abstract" : "unknown", "author" : [ "Alexander Becker, C" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "SQL-Abstract-Limit", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "DBI" : "0", "Data::Dumper" : "0", "ExtUtils::MakeMaker" : "6.64", "SQL::Abstract" : "1.2", "Scalar::Util" : "0", "Test::Builder" : "0", "Test::Deep" : "0", "Test::Exception" : "0", "Test::More" : "0", "perl" : "5.006" } } }, "release_status" : "stable", "resources" : { "repository" : { "url" : "https://github.com/asb-capfan/SQL-Abstract-Limit" } }, "version" : "0.143", "x_serialization_backend" : "JSON::PP version 4.04" } SQL-Abstract-Limit-0.143/MANIFEST0000644000000000000000000000047014030153743014625 0ustar rootrootMakefile.PL Changes MANIFEST META.yml README.md lib/SQL/Abstract/Limit.pm t/00.load.t t/01.sql.t t/02.syntax.t t/03.subclass.t t/04.args.t t/test_data.csv t/pod-coverage.t t/pod.t t/lib/SQL/Abstract/Limit/Test.pm META.json Module JSON meta-data (added by MakeMaker) SQL-Abstract-Limit-0.143/README.md0000644000000000000000000000115713756434545014776 0ustar rootroot# SQL-Abstract-Limit Portability layer for LIMIT emulation. [![Build Status](https://travis-ci.org/asb-capfan/SQL-Abstract-Limit.svg?branch=master)](https://travis-ci.org/asb-capfan/SQL-Abstract-Limit) # INSTALLATION Install SQL::Abstract::Limit using your usual method for installing modules from CPAN. If you don't have one, have a look at: http://www.cpan.org/modules/INSTALL.html # COPYRIGHT AND LICENSE Copyright (C) 2004-2020 David Baird. Currently maintained by Alexander Becker. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SQL-Abstract-Limit-0.143/Makefile.PL0000644000000000000000000000147513756424601015464 0ustar rootrootuse strict; use warnings; use 5.006; use ExtUtils::MakeMaker; WriteMakefile ( 'NAME' => 'SQL::Abstract::Limit', 'VERSION_FROM' => 'lib/SQL/Abstract/Limit.pm', 'PREREQ_PM' => { 'ExtUtils::MakeMaker' => '6.64', 'DBI' => '0', 'Data::Dumper' => '0', 'SQL::Abstract' => '1.2', 'Scalar::Util' => '0', 'Test::Builder' => '0', 'Test::Deep' => '0', 'Test::Exception' => '0', 'Test::More' => '0', }, 'AUTHOR' => 'Alexander Becker, C', 'LICENSE' => 'perl_5', 'INSTALLDIRS' => 'site', 'PL_FILES' => {}, 'META_MERGE' => { 'resources' => { 'repository' => 'https://github.com/asb-capfan/SQL-Abstract-Limit', }, }, 'MIN_PERL_VERSION' => 5.006, ); SQL-Abstract-Limit-0.143/lib/0000755000000000000000000000000014030154061014233 5ustar rootrootSQL-Abstract-Limit-0.143/lib/SQL/0000755000000000000000000000000014030153737014703 5ustar rootrootSQL-Abstract-Limit-0.143/lib/SQL/Abstract/0000755000000000000000000000000014030153737016446 5ustar rootrootSQL-Abstract-Limit-0.143/lib/SQL/Abstract/Limit.pm0000644000000000000000000010524514030153375020067 0ustar rootrootpackage SQL::Abstract::Limit; use strict; use warnings; use Carp(); use DBI::Const::GetInfoType (); use SQL::Abstract 1.20; use base 'SQL::Abstract'; =head1 NAME SQL::Abstract::Limit - portable LIMIT emulation =cut our $VERSION = '0.143'; # additions / error reports welcome ! our %SyntaxMap = ( mssql => 'Top', access => 'Top', sybase => 'GenericSubQ', oracle => 'RowNum', db2 => 'FetchFirst', ingres => '', adabasd => '', informix => 'Skip', # asany => '', # more recent MySQL versions support LimitOffset as well mysql => 'LimitXY', mysqlpp => 'LimitXY', maxdb => 'LimitXY', # MySQL pg => 'LimitOffset', pgpp => 'LimitOffset', sqlite => 'LimitOffset', sqlite2 => 'LimitOffset', interbase => 'RowsTo', unify => '', primebase => '', mimer => '', # anything that uses SQL::Statement can use LimitXY, I think sprite => 'LimitXY', wtsprite => 'LimitXY', anydata => 'LimitXY', csv => 'LimitXY', ram => 'LimitXY', dbm => 'LimitXY', excel => 'LimitXY', google => 'LimitXY', ); =head1 SYNOPSIS use SQL::Abstract::Limit; my $sql = SQL::Abstract::Limit->new( limit_dialect => 'LimitOffset' );; # or autodetect from a DBI $dbh: my $sql = SQL::Abstract::Limit->new( limit_dialect => $dbh ); # or from a Class::DBI class: my $sql = SQL::Abstract::Limit->new( limit_dialect => 'My::CDBI::App' ); # or object: my $obj = My::CDBI::App->retrieve( $id ); my $sql = SQL::Abstract::Limit->new( limit_dialect => $obj ); # generate SQL: my ( $stmt, @bind ) = $sql->select( $table, \@fields, \%where, \@order, $limit, $offset ); # Then, use these in your DBI statements my $sth = $dbh->prepare( $stmt ); $sth->execute( @bind ); # Just generate the WHERE clause (only available for some syntaxes) my ( $stmt, @bind ) = $sql->where( \%where, \@order, $limit, $offset ); =head1 DESCRIPTION Portability layer for LIMIT emulation. =over 4 =item new( case => 'lower', cmp => 'like', logic => 'and', convert => 'upper', limit_dialect => 'Top' ) All settings are optional. =over 8 =item limit_dialect Sets the default syntax model to use for emulating a C clause. Default setting is C. You can still pass other syntax settings in method calls, this just sets the default. Possible values are: LimitOffset PostgreSQL, SQLite LimitXY MySQL, MaxDB, anything that uses SQL::Statement LimitYX SQLite (optional) RowsTo InterBase/FireBird Top SQL/Server, MS Access RowNum Oracle FetchFirst DB2 Skip Informix GenericSubQ Sybase, plus any databases not recognised by this module $dbh a DBI database handle CDBI subclass CDBI object other DBI-based thing The first group are implemented by appending a short clause to the end of the statement. The second group require more intricate wrapping of the original statement in subselects. You can pass a L database handle, and the module will figure out which dialect to use. You can pass a L subclass or object, and the module will find the C<$dbh> and use it to find the dialect. Anything else based on L can be easily added by locating the C<$dbh>. Patches or suggestions welcome. =back Other options are described in L. =item select( $table, \@fields, $where, [ \@order, [ $rows, [ $offset ], [ $dialect ] ] ] ) Same as C, but accepts additional C<$rows>, C<$offset> and C<$dialect> parameters. The C<$order> parameter is required if C<$rows> is specified. The C<$fields> parameter is required, but can be set to C, C<''> or C<'*'> (all these get set to C<'*'>). The C<$where> parameter is also required. It can be a hashref or an arrayref, or C. =cut sub select { my $self = shift; my $table = shift; my $fields = shift; my $where = shift; # if ref( $_[0] ) eq 'HASH'; my ( $order, $rows, $offset, $syntax ) = $self->_get_args( @_ ); $fields ||= '*'; # in case someone supplies '' or undef # with no LIMIT parameters, defer to SQL::Abstract return $self->SUPER::select( $table, $fields, $where, $order ) unless $rows; # with LIMIT parameters, get the basic SQL without the ORDER BY clause my ( $sql, @bind ) = $self->SUPER::select( $table, $fields, $where ); my $syntax_name = $self->_find_syntax( $syntax ); $sql = $self->_emulate_limit( $syntax_name, $sql, $order, $rows, $offset ); return wantarray ? ( $sql, @bind ) : $sql; } =item where( [ $where, [ \@order, [ $rows, [ $offset ], [ $dialect ] ] ] ] ) Same as C, but accepts additional C<$rows>, C<$offset> and C<$dialect> parameters. Some SQL dialects support syntaxes that can be applied as simple phrases tacked on to the end of the WHERE clause. These are: LimitOffset LimitXY LimitYX RowsTo This method returns a modified WHERE clause, if the limit syntax is set to one of these options (either in the call to C or in the constructor), and if C<$rows> is passed in. Dies via C if you try to use it for other syntaxes. C<$order> is required if C<$rows> is set. C<$where> is required if any other parameters are specified. It can be a hashref or an arrayref, or C. Returns a regular C clause if no limits are set. =cut sub where { my $self = shift; my $where = shift; # if ref( $_[0] ) eq 'HASH'; my ( $order, $rows, $offset, $syntax ) = $self->_get_args( @_ ); my ( $sql, @bind ); if ( defined $rows ) { ( $sql, @bind ) = $self->SUPER::where( $where ); my $syntax_name = $self->_find_syntax( $syntax ); Carp::croak( "can't build a stand-alone WHERE clause for $syntax_name" ) unless $syntax_name =~ /(?:LimitOffset|LimitXY|LimitYX|RowsTo)/i; $sql = $self->_emulate_limit( $syntax_name, $sql, $order, $rows, $offset ); } else { # ( $sql, @bind ) = $self->SUPER::where( $where, $order ); } return wantarray ? ( $sql, @bind ) : $sql; } sub _get_args { my $self = shift; my $order = shift; my $rows = shift; my $offset = shift if ( defined $_[0] && $_[0] =~ /^\d+$/ ); my $syntax = shift || $self->_default_limit_syntax; return $order, $rows, $offset, $syntax; } =item insert =item update =item delete =item values =item generate See L for these methods. C and C are not provided with any C emulation in this release, and no support is planned at the moment. But patches would be welcome. =back =cut sub _default_limit_syntax { $_[0]->{limit_dialect} || 'GenericSubQ' } sub _emulate_limit { my ( $self, $syntax, $sql, $order, $rows, $offset ) = @_; $offset ||= 0; Carp::croak( "rows must be a number (got $rows)" ) unless $rows =~ /^\d+$/; Carp::croak( "offset must be a number (got $offset)" ) unless $offset =~ /^\d+$/; my $method = $self->can( 'emulate_limit' ) || "_$syntax"; $sql = $self->$method( $sql, $order, $rows, $offset ); return $sql; } sub _find_syntax { my ($self, $syntax) = @_; # $syntax is a dialect name, database name, $dbh, or CDBI class or object Carp::croak('no syntax') unless $syntax; my $db; # note: tests arranged so that the eval isn't run against a scalar $syntax # see rt #15000 if (ref $syntax) # a $dbh or a CDBI object { if ( UNIVERSAL::isa($syntax => 'Class::DBI') ) { $db = $self->_find_database_from_cdbi($syntax); } elsif ( eval { $syntax->{Driver}->{Name} } ) # or use isa DBI::db ? { $db = $self->_find_database_from_dbh($syntax); } } else # string - CDBI class, db name, or dialect name { if (exists $SyntaxMap{lc $syntax}) { # the name of a database $db = $syntax; } elsif (UNIVERSAL::isa($syntax => 'Class::DBI')) { # a CDBI class $db = $self->_find_database_from_cdbi($syntax); } else { # or it's already a syntax dialect return $syntax; } } return $self->_find_syntax_from_database($db) if $db; # if you get here, you might like to provide a patch to determine the # syntax model for your object or ref e.g. by getting at the $dbh stored in it warn "can't determine syntax model for $syntax - using default"; return $self->_default_limit_syntax; } # most of this code modified from DBIx::AnyDBD::rebless sub _find_database_from_dbh { my ( $self, $dbh ) = @_; my $driver = ucfirst( $dbh->{Driver}->{Name} ) || Carp::croak( "no driver in $dbh" ); if ( $driver eq 'Proxy' ) { # Looking into the internals of DBD::Proxy is maybe a little questionable ( $driver ) = $dbh->{proxy_client}->{application} =~ /^DBI:(.+?):/; } # what about DBD::JDBC ? my ( $odbc, $ado ) = ( $driver eq 'ODBC', $driver eq 'ADO' ); if ( $odbc || $ado ) { my $name; # $name = $dbh->func( 17, 'GetInfo' ) if $odbc; $name = $dbh->get_info( $DBI::Const::GetInfoType::GetInfoType{SQL_DBMS_NAME} ) if $odbc; $name = $dbh->{ado_conn}->Properties->Item( 'DBMS Name' )->Value if $ado; die "can't determine driver name for ODBC or ADO handle: $dbh" unless $name; CASE: { $driver = 'MSSQL', last CASE if $name eq 'Microsoft SQL Server'; $driver = 'Sybase', last CASE if $name eq 'SQL Server'; $driver = 'Oracle', last CASE if $name =~ /Oracle/; $driver = 'ASAny', last CASE if $name eq 'Adaptive Server Anywhere'; $driver = 'AdabasD', last CASE if $name eq 'ADABAS D'; # this should catch Access (ACCESS) and Informix (Informix) $driver = lc( $name ); $driver =~ s/\b(\w)/uc($1)/eg; $driver =~ s/\s+/_/g; } } die "couldn't find DBD driver in $dbh" unless $driver; # $driver now holds a string identifying the database server - in the future, # it might return an object with extra information e.g. version return $driver; } # $cdbi can be a class or object sub _find_database_from_cdbi { my ($self, $cdbi) = @_; # inherits from Ima::DBI my ($dbh) = $cdbi->db_handles; Carp::croak "no \$dbh in $cdbi" unless $dbh; return $self->_find_database_from_dbh($dbh); } # currently expects a string (database moniker), but this may become an object # with e.g. version string etc. sub _find_syntax_from_database { my ( $self, $db ) = @_; my $syntax = $SyntaxMap{ lc( $db ) }; return $syntax if $syntax; my $msg = defined $syntax ? "no dialect known for $db - using GenericSubQ dialect" : "unknown database $db - using GenericSubQ dialect"; warn $msg; return 'GenericSubQ'; } # DBIx::SearchBuilder LIMIT emulation: # Oracle - RowNum # Pg - LimitOffset # Sybase - doesn't emulate # Informix - First - but can only retrieve 1st page # SQLite - default # MySQL - default # default - LIMIT $offset, $rows # or LIMIT $rows # if $offset == 0 # DBIx::Compat also tries, but only for the easy ones # --------------------------------- # LIMIT emulation routines # utility for some emulations sub _order_directions { my ( $self, $order ) = @_; return unless $order; my $ref = ref $order; my @order; CASE: { @order = @$order, last CASE if $ref eq 'ARRAY'; @order = ( $order ), last CASE unless $ref; @order = ( $$order ), last CASE if $ref eq 'SCALAR'; Carp::croak __PACKAGE__ . ": Unsupported data struct $ref for ORDER BY"; } my ( $order_by_up, $order_by_down ); foreach my $spec ( @order ) { my @spec = split ' ', $spec; Carp::croak( "bad column order spec: $spec" ) if @spec > 2; push( @spec, 'ASC' ) unless @spec == 2; my ( $col, $up ) = @spec; # or maybe down $up = uc( $up ); Carp::croak( "bad direction: $up" ) unless $up =~ /^(?:ASC|DESC)$/; $order_by_up .= ", $col $up"; my $down = $up eq 'ASC' ? 'DESC' : 'ASC'; $order_by_down .= ", $col $down"; } s/^,/ORDER BY/ for ( $order_by_up, $order_by_down ); return $order_by_up, $order_by_down; } # From http://phplens.com/lens/adodb/tips_portable_sql.htm # When writing SQL to retrieve the first 10 rows for paging, you could write... # Database SQL Syntax # DB2 select * from table fetch first 10 rows only # Informix select first 10 * from table # Microsoft SQL Server and Access select top 10 * from table # MySQL and PostgreSQL select * from table limit 10 # Oracle 8i select * from (select * from table) where rownum <= 10 =head2 Limit emulation The following dialects are available for emulating the LIMIT clause. In each case, C<$sql> represents the SQL statement generated by C, minus the ORDER BY clause, e.g. SELECT foo, bar FROM my_table WHERE some_conditions C<$sql_after_select> represents C<$sql> with the leading C clause (i.e. the first column in the C<\@fields> parameter). The results will be sorted by that unique column, so any C<$order> parameter is ignored, unless it matches the unique column, in which case the direction of the sort is honored. =over 8 =item Syntax SELECT field_list FROM $table X WHERE where_clause AND ( SELECT COUNT(*) FROM $table WHERE $pk > X.$pk ) BETWEEN $offset AND $last ORDER BY $pk $asc_desc C<$pk> is the first column in C. C<$asc_desc> is the opposite direction to that specified in the method call. So if you want the final results sorted C, say so, and it gets flipped internally, but the results come out as you'd expect. I think. The C clause is replaced with C $rows> if <$offset == 0>. =item Databases Sybase Anything not otherwise known to this module. =back =cut sub _GenericSubQ { my ( $self, $sql, $order, $rows, $offset ) = @_; my $last = $rows + $offset; my $order_by = $self->_order_by( $order ); my ( $pk, $table ) = $sql =~ /^\s*SELECT\s+(\w+),?.*\sFROM\s+([\w]+)/i; #warn "pk: $pk"; #warn "table: $table"; # get specified sort order and swap it to get the expected output (I think?) my ( $asc_desc ) = $order_by =~ /\b$pk\s+(ASC|DESC)\s*/i; $asc_desc = 'ASC' unless defined $asc_desc; $asc_desc = uc( $asc_desc ); $asc_desc = $asc_desc eq 'ASC' ? 'DESC' : 'ASC'; $sql =~ s/FROM $table /FROM $table X /; my $limit = $offset ? "BETWEEN $offset AND $last" : "< $rows"; $sql = <<""; $sql AND ( SELECT COUNT(*) FROM $table WHERE $pk > X.$pk ) $limit ORDER BY $pk $asc_desc return $sql; } =begin notes 1st page: SELECT id, field1, fieldn FROM table_xyz X WHERE ( SELECT COUNT(*) FROM table_xyz WHERE id > X.id ) < 100 ORDER BY id DESC Next page: SELECT id, field1, fieldn FROM table_xyz X WHERE ( SELECT COUNT(*) FROM table_xyz WHERE id > X.id ) BETWEEN 100 AND 199 ORDER BY id DESC http://expertanswercenter.techtarget.com/eac/knowledgebaseAnswer/0,,sid63_gci978197,00.html We can adapt the generic Top N query to this task. I would not use the generic method when TOP or LIMIT is available, but you're right, the previous answer is incomplete without this. Using the same table and column names, the top 100 ids are given by: SELECT id, field1, fieldn FROM table_xyz X WHERE ( SELECT COUNT(*) FROM table_xyz WHERE id > X.id ) < 100 ORDER BY id DESC The subquery is correlated, which means that it will be evaluated for each row of the outer query. The subquery says "count the number of rows that have an id that is greater than this id." Note that the sort order is descending, so we are looking for ids that are greater, i.e. higher up in the result set. If that number is less than 100, then this row must be one of the top 100. Simple, eh? Unfortunately, it runs quite slowly. Furthermore, it takes ties into consideration, which is good, but this means that the number of rows returned isn't always going to be exactly 100 -- there will be extra rows if there are ties extending across the 100th place. Next, we need the second set of 100: select id , field1 , fieldn from table_xyz X where ( select count(*) from table_xyz where id > X.id ) between 100 and 199 order by id desc See the pattern? Note that the same caveat applies about ties that extend across 200th place. =end notes =begin notes =item First =over 8 =item Syntax Looks to be identical to C, e.g. C. =item $order The C parameter passed to the C and C methods can be a number of things. The module will attempt to determine the appropriate syntax to use. Supported C<$dialect> things are: dialect name (e.g. LimitOffset, RowsTo, Top etc.) database moniker (e.g. Oracle, SQLite etc.) DBI database handle Class::DBI subclass or object =head1 CAVEATS Paging results sets is a complicated undertaking, with several competing factors to take into account. This module does B magically give you the optimum paging solution for your situation. It gives you a solution that may be good enough in many situations. But if your tables are large, the SQL generated here will often not be efficient. Or if your queries involve joins or other complications, you will probably need to look elsewhere. But if your tables aren't too huge, and your queries straightforward, you can just plug this module in and move on to your next task. =head1 ACKNOWLEDGEMENTS Thanks to Aaron Johnson for the Top syntax model (SQL/Server and MS Access). Thanks to Emanuele Zeppieri for the IBM DB2 syntax model. Thanks to Paul Falbe for the Informix implementation. =head1 TODO Find more syntaxes to implement. Test the syntaxes against real databases. I only have access to MySQL. Reports of success or failure would be great. =head1 DEPENDENCIES L, L, L. =head1 SEE ALSO L, L, L. =head1 BUGS Please report all bugs (patches welcome) via GitHub at L or via the CPAN Request Tracker at L. =head1 AUTHOR, COPYRIGHT AND LICENSE Copyright 2004-2020 by David Baird. Currently maintained by Alexander Becker. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 HOW IS IT DONE ELSEWHERE A few CPAN modules do this for a few databases, but the most comprehensive seem to be DBIx::SQLEngine, DBIx::SearchBuilder and DBIx::RecordSet. Have a look in the source code for my notes on how these modules tackle similar problems. =begin notes =over 4 =item DBIx::SearchBuilder::Handle::Oracle Transform an SQL query from: SELECT main.* FROM Tickets main WHERE ((main.EffectiveId = main.id)) AND ((main.Type = 'ticket')) AND ( ( (main.Status = 'new')OR(main.Status = 'open') ) AND ( (main.Queue = '1') ) ) to: SELECT * FROM ( SELECT limitquery.*,rownum limitrownum FROM ( SELECT main.* FROM Tickets main WHERE ((main.EffectiveId = main.id)) AND ((main.Type = 'ticket')) AND ( ( (main.Status = 'new')OR(main.Status = 'open') ) AND ( (main.Queue = '1') ) ) ) limitquery WHERE rownum <= 50 ) WHERE limitrownum >= 1 if ($per_page) { # Oracle orders from 1 not zero $first++; # Make current query a sub select $$statementref = "SELECT * FROM ( SELECT limitquery.*,rownum limitrownum FROM ( $$statementref ) limitquery WHERE rownum <= " . ($first + $per_page - 1) . " ) WHERE limitrownum >= " . $first; } =item DBIx::SQLEngine::Driver sub sql_limit { my $self = shift; my ( $limit, $offset, $sql, @params ) = @_; $sql .= " limit $limit" if $limit; $sql .= " offset $offset" if $offset; return ($sql, @params); } =item DBIx::SQLEngine::Driver::AnyData Also: DBIx::SQLEngine::Driver::CSV Adds support for SQL select limit clause. TODO: Needs workaround to support offset. sub sql_limit { my $self = shift; my ( $limit, $offset, $sql, @params ) = @_; # You can't apply "limit" to non-table fetches $sql .= " limit $limit" if ( $sql =~ / from / ); return ($sql, @params); } =item DBIx::SQLEngine::Driver::Informix - Support DBD::Informix and DBD::ODBC/Informix =item sql_limit() Not yet supported. Perhaps we should use "first $maxrows" and throw out the first $offset? =back =cut sub sql_limit { confess("Not yet supported") } =item DBIx::SQLEngine::Driver::MSSQL - Support DBD::ODBC with Microsoft SQL Server =item sql_limit() Adds support for SQL select limit clause. =back =cut sub sql_limit { my $self = shift; my ( $limit, $offset, $sql, @params ) = @_; # You can't apply "limit" to non-table fetches like "select LAST_INSERT_ID" if ( $sql =~ /\bfrom\b/ and defined $limit or defined $offset) { $sql .= " limit $limit" if $limit; $sql .= " offset $offset" if $offset; } return ($sql, @params); } =item DBIx::SQLEngine::Driver::Mysql - Support DBD::mysql =item sql_limit() Adds support for SQL select limit clause. =back =cut sub sql_limit { my $self = shift; my ( $limit, $offset, $sql, @params ) = @_; # You can't apply "limit" to non-table fetches like "select LAST_INSERT_ID" if ( $sql =~ /\bfrom\b/ and $limit or $offset) { $limit ||= 1_000_000; # MySQL select with offset requires a limit $sql .= " limit " . ( $offset ? "$offset," : '' ) . $limit; } return ($sql, @params); } =item DBIx::SQLEngine::Driver::Oracle - Support DBD::Oracle and DBD::ODBC/Oracle =item sql_limit() Adds support for SQL select limit clause. Implemented as a subselect with ROWNUM. =back =cut sub sql_limit { my $self = shift; my ( $limit, $offset, $sql, @params ) = @_; # remove tablealiases and group-functions from outer query properties my ($properties) = ($sql =~ /^\s*SELECT\s(.*?)\sFROM\s/i); $properties =~ s/[^\s]+\s*as\s*//ig; $properties =~ s/\w+\.//g; $offset ||= 0; my $position = ( $offset + $limit ); $sql = <<""; SELECT $properties FROM ( SELECT $properties, ROWNUM AS sqle_position FROM ( $sql ) ) WHERE sqle_position > $offset AND sqle_position <= $position return ($sql, @params); } =item DBIx::SQLEngine::Driver::Pg - Support DBD::Pg =head2 sql_limit $sqldb->sql_limit( $limit, $offset, $sql, @params ) : $sql, @params Adds support for SQL select limit clause. =cut sub sql_limit { my $self = shift; my ( $limit, $offset, $sql, @params ) = @_; # You can't apply "limit" to non-table fetches like "select LAST_INSERT_ID" if ( $sql =~ /\bfrom\b/ and defined $limit or defined $offset) { $sql .= " limit $limit" if $limit; $sql .= " offset $offset" if $offset; } return ($sql, @params); } =item DBIx::SQLEngine::Driver::SQLite - Support DBD::SQLite driver =head2 sql_limit Adds support for SQL select limit clause. =cut sub sql_limit { my $self = shift; my ( $limit, $offset, $sql, @params ) = @_; # You can't apply "limit" to non-table fetches like "select LAST_INSERT_ID" if ( $sql =~ /\bfrom\b/ and defined $limit or defined $offset) { $sql .= " limit $limit" if $limit; $sql .= " offset $offset" if $offset; } return ($sql, @params); } =item DBIx::SQLEngine::Driver::Sybase - Extends SQLEngine for DBMS Idiosyncrasies =item sql_limit() Not yet supported. See http://www.isug.com/Sybase_FAQ/ASE/section6.2.html#6.2.12 =back =cut sub sql_limit { confess("Not yet supported") } =item DBIx::SQLEngine::Driver::Sybase::MSSQL - Support DBD::Sybase with Microsoft SQL Nothing. =back =cut =end notes SQL-Abstract-Limit-0.143/META.yml0000644000000000000000000000143514030153740014744 0ustar rootroot--- abstract: unknown author: - 'Alexander Becker, C' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.44, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: SQL-Abstract-Limit no_index: directory: - t - inc requires: DBI: '0' Data::Dumper: '0' ExtUtils::MakeMaker: '6.64' SQL::Abstract: '1.2' Scalar::Util: '0' Test::Builder: '0' Test::Deep: '0' Test::Exception: '0' Test::More: '0' perl: '5.006' resources: repository: https://github.com/asb-capfan/SQL-Abstract-Limit version: '0.143' x_serialization_backend: 'CPAN::Meta::YAML version 0.018'