X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSQLAHacks.pm;h=c03adbb303c01452eb2653f1edff0dd55275b3ae;hb=ca791b95e618ab76bddbaf58441e3d6873336850;hp=e6ae5b55660d6c16c156987f81fdd32f470dd973;hpb=e2db8319f8e463bc3136e4aec9372a1e8305ac26;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/SQLAHacks.pm b/lib/DBIx/Class/SQLAHacks.pm index e6ae5b5..c03adbb 100644 --- a/lib/DBIx/Class/SQLAHacks.pm +++ b/lib/DBIx/Class/SQLAHacks.pm @@ -1,6 +1,10 @@ package # Hide from PAUSE DBIx::Class::SQLAHacks; +# This module is a subclass of SQL::Abstract::Limit and includes a number +# of DBIC-specific workarounds, not yet suitable for inclusion into the +# SQLA core + use base qw/SQL::Abstract::Limit/; use strict; use warnings; @@ -28,6 +32,9 @@ BEGIN { } } + +# Tries to determine limit dialect. +# sub new { my $self = shift->SUPER::new(@_); @@ -40,14 +47,12 @@ sub new { $self; } - # Some databases (sqlite) do not handle multiple parenthesis -# around in/between arguments. A tentative x IN ( ( 1, 2 ,3) ) +# around in/between arguments. A tentative x IN ( (1, 2 ,3) ) # is interpreted as x IN 1 or something similar. # # Since we currently do not have access to the SQLA AST, resort # to barbaric mutilation of any SQL supplied in literal form - sub _strip_outer_paren { my ($self, $arg) = @_; @@ -135,8 +140,11 @@ sub _Top { } my $name_sep = $self->name_sep || '.'; - $name_sep = "\Q$name_sep\E"; - my $col_re = qr/ ^ (?: (.+) $name_sep )? ([^$name_sep]+) $ /x; + my $esc_name_sep = "\Q$name_sep\E"; + my $col_re = qr/ ^ (?: (.+) $esc_name_sep )? ([^$esc_name_sep]+) $ /x; + + my $rs_alias = $self->{_dbic_rs_attrs}{alias}; + my $quoted_rs_alias = $self->_quote ($rs_alias); # construct the new select lists, rename(alias) some columns if necessary my (@outer_select, @inner_select, %seen_names, %col_aliases, %outer_col_aliases); @@ -162,7 +170,8 @@ sub _Top { $quoted_alias = $self->_quote ('column_' . (@inner_select + 1) ); } # column name seen more than once - alias it - elsif ($orig_colname && ($seen_names{$orig_colname} > 1) ) { + elsif ($orig_colname && + ($seen_names{$orig_colname} && $seen_names{$orig_colname} > 1) ) { $quoted_alias = $self->_quote ("${table}__${orig_colname}"); } @@ -219,7 +228,6 @@ sub _Top { $limit_order = $req_order; } else { - my $rs_alias = $self->{_dbic_rs_attrs}{alias}; $limit_order = [ map { join ('', $rs_alias, $name_sep, $_ ) } ( $self->{_dbic_rs_attrs}{_source_handle}->resolve->primary_columns ) @@ -246,7 +254,7 @@ sub _Top { } } for my $col (keys %col_aliases) { - $order_by_inner =~ s/\s+$col\s+/$col_aliases{$col}/g; + $order_by_inner =~ s/\s+$col\s+/ $col_aliases{$col} /g; } @@ -260,7 +268,7 @@ sub _Top { SELECT TOP $rows $outer_select FROM ( $sql - ) AS me + ) $quoted_rs_alias $order_by_outer SQL @@ -270,12 +278,13 @@ SQL $sql = <<"SQL"; SELECT $outer_select FROM - ( $sql ) AS me - $order_by_requested; + ( $sql ) $quoted_rs_alias + $order_by_requested SQL } + $sql =~ s/\s*\n\s*/ /g; # parsing out multiline statements is harder than a single line return $sql; } @@ -313,6 +322,8 @@ my $for_syntax = { update => 'FOR UPDATE', shared => 'FOR SHARE', }; +# Quotes table names, handles "limit" dialects (e.g. where rownum between x and +# y), supports SELECT ... FOR UPDATE and SELECT ... FOR SHARE. sub select { my ($self, $table, $fields, $where, $order, @rest) = @_; @@ -339,6 +350,7 @@ sub select { return wantarray ? ($sql, @{$self->{from_bind}}, @where_bind, @{$self->{having_bind}}, @{$self->{order_bind}} ) : $sql; } +# Quotes table names, and handles default inserts sub insert { my $self = shift; my $table = shift; @@ -354,6 +366,7 @@ sub insert { $self->SUPER::insert($table, @_); } +# Just quotes table names. sub update { my $self = shift; my $table = shift; @@ -361,6 +374,7 @@ sub update { $self->SUPER::update($table, @_); } +# Just quotes table names. sub delete { my $self = shift; my $table = shift; @@ -589,12 +603,15 @@ sub limit_dialect { return $self->{limit_dialect}; } +# Set to an array-ref to specify separate left and right quotes for table names. +# A single scalar is equivalen to [ $char, $char ] sub quote_char { my $self = shift; $self->{quote_char} = shift if @_; return $self->{quote_char}; } +# Character separating quoted table names. sub name_sep { my $self = shift; $self->{name_sep} = shift if @_; @@ -602,50 +619,3 @@ sub name_sep { } 1; - -__END__ - -=pod - -=head1 NAME - -DBIx::Class::SQLAHacks - This module is a subclass of SQL::Abstract::Limit -and includes a number of DBIC-specific workarounds, not yet suitable for -inclusion into SQLA proper. - -=head1 METHODS - -=head2 new - -Tries to determine limit dialect. - -=head2 select - -Quotes table names, handles "limit" dialects (e.g. where rownum between x and -y), supports SELECT ... FOR UPDATE and SELECT ... FOR SHARE. - -=head2 insert update delete - -Just quotes table names. - -=head2 limit_dialect - -Specifies the dialect of used for implementing an SQL "limit" clause for -restricting the number of query results returned. Valid values are: RowNum. - -See L for details. - -=head2 name_sep - -Character separating quoted table names. - -See L for details. - -=head2 quote_char - -Set to an array-ref to specify separate left and right quotes for table names. - -See L for details. - -=cut -