Revision history for DBIx::Class
+ * Fixes
+ - Fix SkipFirst and FirstSkip limit dialects (Informix and Firebird)
+
0.08196 2011-11-29 05:35 (UTC)
* Fixes
- Fix tests for DBD::SQLite >= 1.34.
sub _assemble_binds {
my $self = shift;
- return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/select from where group having order limit/);
+ return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where group having order limit/);
}
my $for_syntax = {
return sprintf ('SELECT %s%s%s%s',
$offset
? do {
- push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset];
+ push @{$self->{pre_select_bind}}, [ $self->__offset_bindtype => $offset];
'SKIP ? '
}
: ''
,
do {
- push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
+ push @{$self->{pre_select_bind}}, [ $self->__rows_bindtype => $rows ];
'FIRST ? '
},
$sql,
return sprintf ('SELECT %s%s%s%s',
do {
- push @{$self->{limit_bind}}, [ $self->__rows_bindtype => $rows ];
+ push @{$self->{pre_select_bind}}, [ $self->__rows_bindtype => $rows ];
'FIRST ? '
},
$offset
? do {
- push @{$self->{limit_bind}}, [ $self->__offset_bindtype => $offset];
+ push @{$self->{pre_select_bind}}, [ $self->__offset_bindtype => $offset];
'SKIP ? '
}
: ''
$r{mid_sel} .= ', ' . $extra_order_sel->{$extra_col};
}
- # since whatever order bindvals there are, they will be realiased
- # and need to show up in front of the entire initial inner subquery
- # *unshift* the selector bind stack to make this happen (horrible,
- # horrible, but we don't have another mechanism yet)
- unshift @{$self->{select_bind}}, @{$self->{order_bind}};
+ # Whatever order bindvals there are, they will be realiased and
+ # need to show up in front of the entire initial inner subquery
+ push @{$self->{pre_select_bind}}, @{$self->{order_bind}};
}
# and this is order re-alias magic
sub _assemble_binds {
my $self = shift;
- return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/select from where oracle_connect_by group having order limit/);
+ return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where oracle_connect_by group having order limit/);
}
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+use DBIC::SqlMakerTest;
+use DBIx::Class::SQLMaker::LimitDialects;
+
+my ($LIMIT, $OFFSET) = (
+ DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
+ DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
+);
+
+my $schema = DBICTest->init_schema;
+
+$schema->storage->_sql_maker->limit_dialect ('FirstSkip');
+
+my $rs_selectas_col = $schema->resultset ('BooksInLibrary')->search ({}, {
+ '+select' => ['owner.name'],
+ '+as' => ['owner.name'],
+ join => 'owner',
+ rows => 1,
+ offset => 2,
+});
+
+is_same_sql_bind(
+ $rs_selectas_col->as_query,
+ '(
+ SELECT FIRST ? SKIP ? me.id, me.source, me.owner, me.title, me.price, owner.name
+ FROM books me
+ JOIN owners owner ON owner.id = me.owner
+ WHERE ( source = ? )
+ )',
+ [
+ [ $LIMIT => 1 ],
+ [ $OFFSET => 2 ],
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+);
+
+$schema->storage->_sql_maker->quote_char ([qw/ [ ] /]);
+$schema->storage->_sql_maker->name_sep ('.');
+
+my $rs_selectas_rel = $schema->resultset ('BooksInLibrary')->search ({}, {
+ '+select' => ['owner.name'],
+ '+as' => ['owner_name'],
+ join => 'owner',
+ rows => 1,
+ offset => 2,
+});
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '(
+ SELECT FIRST ? SKIP ? [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price], [owner].[name]
+ FROM [books] [me]
+ JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
+ WHERE ( [source] = ? )
+ )',
+ [
+ [ $LIMIT => 1 ],
+ [ $OFFSET => 2 ],
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+);
+
+{
+my $subq = $schema->resultset('Owners')->search({
+ 'count.id' => { -ident => 'owner.id' },
+ 'count.name' => 'fail', # no one would do this in real life, the rows makes even less sense
+}, { alias => 'owner', rows => 1 })->count_rs;
+
+my $rs_selectas_rel = $schema->resultset('BooksInLibrary')->search ({}, {
+ columns => [
+ { owner_name => 'owner.name' },
+ { owner_books => $subq->as_query },
+ ],
+ join => 'owner',
+ rows => 1,
+ offset => 2,
+});
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '(
+ SELECT FIRST ? SKIP ?
+ [owner].[name],
+ ( SELECT COUNT(*) FROM
+ ( SELECT FIRST ? [owner].[id] FROM [owners] [owner]
+ WHERE [count].[id] = [owner].[id] and [count].[name] = ?
+ ) [owner]
+ )
+ FROM [books] [me]
+ JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
+ WHERE ( [source] = ? )
+ )',
+ [
+ [ $LIMIT => 1 ], # outer
+ [ $OFFSET => 2 ], # outer
+ [ {%$LIMIT} => 1 ], # inner
+ [ { dbic_colname => 'count.name' } => 'fail' ],
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+)
+};
+
+{
+ my $rs = $schema->resultset('Artist')->search({}, {
+ columns => 'name',
+ offset => 1,
+ order_by => 'name',
+ });
+ local $rs->result_source->{name} = "weird \n newline/multi \t \t space containing \n table";
+
+ like (
+ ${$rs->as_query}->[0],
+ qr| weird \s \n \s newline/multi \s \t \s \t \s space \s containing \s \n \s table|x,
+ 'Newlines/spaces preserved in final sql',
+ );
+}
+
+{
+my $subq = $schema->resultset('Owners')->search({
+ 'books.owner' => { -ident => 'owner.id' },
+}, { alias => 'owner', select => ['id'], offset => 3, rows => 4 });
+
+my $rs_selectas_rel = $schema->resultset('BooksInLibrary')->search( { -exists => $subq->as_query }, { select => ['id','owner'], rows => 1, offset => 2 } );
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '(
+ SELECT FIRST ? SKIP ? [me].[id], [me].[owner]
+ FROM [books] [me]
+ WHERE ( ( (EXISTS (
+ SELECT FIRST ? SKIP ? [owner].[id] FROM [owners] [owner] WHERE ( [books].[owner] = [owner].[id] )
+ )) AND [source] = ? ) )
+ )',
+ [
+ [ $LIMIT => 1 ], #outer
+ [ $OFFSET => 2 ], #outer
+ [ {%$LIMIT} => 4 ], #inner
+ [ {%$OFFSET} => 3 ], #inner
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+ 'Pagination with sub-query in WHERE works'
+);
+
+}
+
+done_testing;
my $s = DBICTest->init_schema (no_deploy => 1, );
$s->storage->sql_maker->limit_dialect ('RowNum');
-my $rs = $s->resultset ('CD');
+my $rs = $s->resultset ('CD')->search({ id => 1 });
+
+my $where_bind = [ { dbic_colname => 'id' }, 1 ];
for my $test_set (
{
SELECT id, bar__id, bleh, ROWNUM rownum__index
FROM (
SELECT foo.id AS id, bar.id AS bar__id, TO_CHAR (foo.womble, "blah") AS bleh
- FROM cd me
+ FROM cd me
+ WHERE id = ?
) me
) me WHERE rownum__index BETWEEN ? AND ?
)',
binds => [
+ $where_bind,
[ $OFFSET => 4 ],
[ $TOTAL => 4 ],
],
SELECT id, bar__id, bleh, ROWNUM rownum__index
FROM (
SELECT foo.id AS id, bar.id AS bar__id, TO_CHAR(foo.womble, "blah") AS bleh
- FROM cd me
+ FROM cd me
+ WHERE id = ?
ORDER BY artist, title
) me
WHERE ROWNUM <= ?
WHERE rownum__index >= ?
)',
binds => [
+ $where_bind,
[ $TOTAL => 4 ],
[ $OFFSET => 4 ],
],
SELECT id, ends_with_me__id, ROWNUM rownum__index
FROM (
SELECT foo.id AS id, ends_with_me.id AS ends_with_me__id
- FROM cd me
+ FROM cd me
+ WHERE id = ?
) me
) me WHERE rownum__index BETWEEN ? AND ?
)',
binds => [
+ $where_bind,
[ $OFFSET => 4 ],
[ $TOTAL => 5 ],
],
SELECT id, ends_with_me__id, ROWNUM rownum__index
FROM (
SELECT foo.id AS id, ends_with_me.id AS ends_with_me__id
- FROM cd me
+ FROM cd me
+ WHERE id = ?
ORDER BY artist, title
) me
WHERE ROWNUM <= ?
WHERE rownum__index >= ?
)',
binds => [
+ $where_bind,
[ $TOTAL => 5 ],
[ $OFFSET => 4 ],
],
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+use DBIC::SqlMakerTest;
+use DBIx::Class::SQLMaker::LimitDialects;
+
+my ($LIMIT, $OFFSET) = (
+ DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
+ DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
+);
+
+my $schema = DBICTest->init_schema;
+
+$schema->storage->_sql_maker->limit_dialect ('SkipFirst');
+
+my $rs_selectas_col = $schema->resultset ('BooksInLibrary')->search ({}, {
+ '+select' => ['owner.name'],
+ '+as' => ['owner.name'],
+ join => 'owner',
+ rows => 1,
+ offset => 2,
+});
+
+is_same_sql_bind(
+ $rs_selectas_col->as_query,
+ '(
+ SELECT SKIP ? FIRST ? me.id, me.source, me.owner, me.title, me.price, owner.name
+ FROM books me
+ JOIN owners owner ON owner.id = me.owner
+ WHERE ( source = ? )
+ )',
+ [
+ [ $OFFSET => 2 ],
+ [ $LIMIT => 1 ],
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+);
+
+$schema->storage->_sql_maker->quote_char ([qw/ [ ] /]);
+$schema->storage->_sql_maker->name_sep ('.');
+
+my $rs_selectas_rel = $schema->resultset ('BooksInLibrary')->search ({}, {
+ '+select' => ['owner.name'],
+ '+as' => ['owner_name'],
+ join => 'owner',
+ rows => 1,
+ offset => 2,
+});
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '(
+ SELECT SKIP ? FIRST ? [me].[id], [me].[source], [me].[owner], [me].[title], [me].[price], [owner].[name]
+ FROM [books] [me]
+ JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
+ WHERE ( [source] = ? )
+ )',
+ [
+ [ $OFFSET => 2 ],
+ [ $LIMIT => 1 ],
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+);
+
+{
+my $subq = $schema->resultset('Owners')->search({
+ 'count.id' => { -ident => 'owner.id' },
+ 'count.name' => 'fail', # no one would do this in real life, the rows makes even less sense
+}, { alias => 'owner', rows => 1 })->count_rs;
+
+my $rs_selectas_rel = $schema->resultset('BooksInLibrary')->search ({}, {
+ columns => [
+ { owner_name => 'owner.name' },
+ { owner_books => $subq->as_query },
+ ],
+ join => 'owner',
+ rows => 1,
+ offset => 2,
+});
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '(
+ SELECT SKIP ? FIRST ?
+ [owner].[name],
+ ( SELECT COUNT(*) FROM
+ ( SELECT FIRST ? [owner].[id] FROM [owners] [owner]
+ WHERE [count].[id] = [owner].[id] and [count].[name] = ?
+ ) [owner]
+ )
+ FROM [books] [me]
+ JOIN [owners] [owner] ON [owner].[id] = [me].[owner]
+ WHERE ( [source] = ? )
+ )',
+ [
+ [ $OFFSET => 2 ], # outer
+ [ $LIMIT => 1 ], # outer
+ [ {%$LIMIT} => 1 ], # inner
+ [ { dbic_colname => 'count.name' } => 'fail' ],
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+)
+};
+
+{
+ my $rs = $schema->resultset('Artist')->search({}, {
+ columns => 'name',
+ offset => 1,
+ order_by => 'name',
+ });
+ local $rs->result_source->{name} = "weird \n newline/multi \t \t space containing \n table";
+
+ like (
+ ${$rs->as_query}->[0],
+ qr| weird \s \n \s newline/multi \s \t \s \t \s space \s containing \s \n \s table|x,
+ 'Newlines/spaces preserved in final sql',
+ );
+}
+
+{
+my $subq = $schema->resultset('Owners')->search({
+ 'books.owner' => { -ident => 'owner.id' },
+}, { alias => 'owner', select => ['id'], offset => 3, rows => 4 });
+
+my $rs_selectas_rel = $schema->resultset('BooksInLibrary')->search( { -exists => $subq->as_query }, { select => ['id','owner'], rows => 1, offset => 2 } );
+
+is_same_sql_bind(
+ $rs_selectas_rel->as_query,
+ '(
+ SELECT SKIP ? FIRST ? [me].[id], [me].[owner]
+ FROM [books] [me]
+ WHERE ( ( (EXISTS (
+ SELECT SKIP ? FIRST ? [owner].[id] FROM [owners] [owner] WHERE ( [books].[owner] = [owner].[id] )
+ )) AND [source] = ? ) )
+ )',
+ [
+ [ $OFFSET => 2 ], #outer
+ [ $LIMIT => 1 ], #outer
+ [ {%$OFFSET} => 3 ], #inner
+ [ {%$LIMIT} => 4 ], #inner
+ [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'source' } => 'Library' ],
+ ],
+ 'Pagination with sub-query in WHERE works'
+);
+
+}
+
+
+done_testing;