From: Jesper Krogh Date: Thu, 23 Feb 2006 19:14:42 +0000 (+0000) Subject: Double char quoting implemented, now supports stuff like [] (for MSSQL) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2437a1e37464dc79c36dca000fd6b4c439f4c7b2;p=dbsrgits%2FDBIx-Class-Historic.git Double char quoting implemented, now supports stuff like [] (for MSSQL) --- diff --git a/lib/DBIx/Class/Manual/Cookbook.pod b/lib/DBIx/Class/Manual/Cookbook.pod index f287e92..1ab0e31 100644 --- a/lib/DBIx/Class/Manual/Cookbook.pod +++ b/lib/DBIx/Class/Manual/Cookbook.pod @@ -601,4 +601,20 @@ in SQL::Abstract::Limit -documentation. The JDBC-bridge is one way of getting access to a MSSQL-server from a platform that Microsoft doesn't deliver native client libraries for. (e.g. Linux) +=head2 Setting quotes for the generated SQL. + +If the database contains columnames with spaces and/or reserved words, the +SQL-query needs to be quoted. This is done using: + + __PACKAGE__->storage->sql_maker->quote_char([ qw/[ ]/] ); + __PACKAGE__->storage->sql_maker->name_sep('.'); + +The first sets the quotesymbols. If the quote i "symmetric" as " or ' + + __PACKAGE__->storage->sql_maker->quote_char('"'); + +is enough. If the left qoute differs form the right quote, the first +notation should be used. name_sep needs to be set to allow the +SQL generator to put the quotes the correct place. + =cut diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 78eee9e..a197ed2 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -136,7 +136,16 @@ sub _join_condition { sub _quote { my ($self, $label) = @_; return '' unless defined $label; + return "*" if $label eq '*'; return $label unless $self->{quote_char}; + if(ref $self->{quote_char} eq "ARRAY"){ + return $self->{quote_char}->[0] . $label . $self->{quote_char}->[1] + if !defined $self->{name_sep}; + my $sep = $self->{name_sep}; + return join($self->{name_sep}, + map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1] } + split(/\Q$sep\E/,$label)); + } return $self->SUPER::_quote($label); } @@ -158,6 +167,21 @@ sub limit_dialect { return $self->{limit_dialect}; } +sub quote_char { + my $self = shift; + $self->{quote_char} = shift if @_; + return $self->{quote_char}; +} + +sub name_sep { + my $self = shift; + $self->{name_sep} = shift if @_; + return $self->{name_sep}; +} + + + + package DBIx::Class::Storage::DBI::DebugCallback; sub print { diff --git a/t/19quotes.t b/t/19quotes.t index 69ba5b5..70c8f8e 100644 --- a/t/19quotes.t +++ b/t/19quotes.t @@ -6,7 +6,7 @@ BEGIN { eval "use DBD::SQLite"; plan $@ ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 3 ); + : ( tests => 4 ); } use lib qw(t/lib); @@ -15,8 +15,8 @@ use_ok('DBICTest'); use_ok('DBICTest::HelperRels'); -DBICTest->schema->storage->sql_maker->{'quote_char'} = q!'!; -DBICTest->schema->storage->sql_maker->{'name_sep'} = '.'; +DBICTest->schema->storage->sql_maker->quote_char("'"); +DBICTest->schema->storage->sql_maker->name_sep('.'); my $rs = DBICTest::CD->search( { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, @@ -24,3 +24,14 @@ my $rs = DBICTest::CD->search( cmp_ok( $rs->count, '==', 1, "join with fields quoted"); +DBICTest->schema->storage->sql_maker->quote_char([qw/[ ]/]); +DBICTest->schema->storage->sql_maker->name_sep('.'); + +$rs = DBICTest::CD->search( + { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' }, + { join => 'artist' }); +cmp_ok($rs->count,'==', 1,"join quoted with brackets."); + + + +