Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Lots of tweaks.

  • Loading branch information...
commit 71db5478d25e504de38245c750577192c574d695 1 parent 83a58a1
@bluefeet authored
Showing with 84 additions and 86 deletions.
  1. +84 −86 lib/DBIx/Dialect.pm
View
170 lib/DBIx/Dialect.pm
@@ -12,11 +12,9 @@ DBIx::Dialect - Auto-detection of SQL quirks.
my $dialect = DBIx::Dialect->new( $dbh );
- print $dialect->type();
- print $dialect->limit();
+ if ($dialect->supports('limit-xy')) { ... }
print $dialect->quote_char();
- print $dialect->sep_char();
- print $dialect->auto_id();
+ ...
=head1 DESCRIPTION
@@ -24,6 +22,11 @@ This module detects the SQL dialect of a L<DBI> database handle and
exposes a handful of properties describing the features and quirks of
that dialect.
+=cut
+
+use Moose::Util::TypeConstraints;
+use List::MoreUtils qw( uniq );
+
=head1 CONSTRUCTOR
# Auto-detect the appropriate dialect from a DBI handle:
@@ -32,39 +35,18 @@ that dialect.
# Explicitly set the dialect that you want:
my $dialect = DBIx::Dialect->new( 'oracle' );
- # The "standard" dialect is the default:
+ # The "default" dialect is the default:
my $dialect = DBIx::Dialect->new();
Each implementation, or dialect, of SQL has quirks that slightly (or in some cases
drastically) change the way that the SQL must be written to get a particular task
done. In order for this module to know which particular set of quirks it should
-use a dialect must be declared. The dialect will default to "standard" which will
-match the ISO SQL standards. But, none of the currently supported dialects actually
-match the standards so you shouldn't be using this.
-
-=cut
-
-use Moose::Util::TypeConstraints;
-use List::MoreUtils qw( uniq );
-
-around 'BUILDARGS' => sub{
- my $orig = shift;
- my $self = shift;
-
- if (@_ == 1) {
- return $self->$orig( dialect => $_[0] );
- }
-
- return $self->$orig( @_ );
-};
-
-=head1 ARGUMENTS
-
-=head2 type
+use a dialect must be declared. The dialect will default to "default" which is very
+limited and only declares the bare minimum of features.
Currently a dialect type can be one of:
- standard
+ default
mysql
oracle
postgresql
@@ -83,13 +65,19 @@ the list of supported DBI Driver is limited to:
If the driver that you are using is not in the above list then please contact the
author and work with them to get it added.
-The type of dialect should never be used directly. Instead the derived attributes that
-specify the quirks and features that the dialect supports should be used. This way your
-code will be more flexible in the long run. If there is a quirk or feature that you wish
-this module would detect then please contact the author and work with them to get it added.
-
=cut
+around 'BUILDARGS' => sub{
+ my $orig = shift;
+ my $self = shift;
+
+ if (@_ == 1) {
+ return $self->$orig( type => $_[0] );
+ }
+
+ return $self->$orig( @_ );
+};
+
my $dbd_dialects = {
'mysql' => 'mysql',
'Oracle' => 'oracle',
@@ -99,9 +87,7 @@ my $dbd_dialects = {
};
my $dialects = {
- standard => {
- limit => 'offset',
- sequences => 1,
+ default => {
quote_char => q["],
sep_char => q[.],
},
@@ -140,42 +126,62 @@ coerce 'DBIx::Dialect::Types::Type',
from class_type('DBI::db'),
via { $dbd_dialects->{ $_->{Driver}->{Name} } };
-has type => (
- is => 'ro',
- isa => 'DBIx::Dialect::Types::Type',
- coerce => 1,
- default => 'standard',
+has _type => (
+ is => 'ro',
+ isa => 'DBIx::Dialect::Types::Type',
+ coerce => 1,
+ default => 'default',
+ init_arg => 'type',
);
-=head1 ATTRIBUTES
+=head1 STATEMENT ATTRIBUTES
-=head2 limit
-
-This is the dialect that is used to limit results for a select. The
-possible values are:
-
- offset (standard and postgresql dialect)
- xy (mysql dialect)
- rownum (oracle dialect)
+These attributes describe the supported statement clauses and quirks.
-=head2 auto_id
+=head2 limit
-The dialect of auto incrementing ID retrieval.
+The dialect of the LIMIT clause.
- last_insert_id (mysql)
- last_insert_rowid (sqlite)
- returning_select (postgresql)
- sequence (oracle)
+ offset (postgresql, sqlite)
+ xy (mysql)
=head2 returning
-The dialect of INSERT/UPDATE/DELETE ... RETURNING syntax:
+The dialect of INSERT/UPDATE/DELETE ... RETURNING syntax.
into (oracle)
select (postgresql)
-An undefined value will be returned if the dialect does not support
-returning at all.
+=head1 DATABASE ATTRIBUTES
+
+=head2 sequences
+
+Whether the database supports sequences.
+
+ postgresql
+ oracle
+
+=head1 FUNCTION ATTRIBUTES
+
+=head2 last_insert_id
+
+Whether the LAST_INSERT_ID() function is supported.
+
+ mysql
+
+=head2 last_insert_rowid
+
+Whether the LAST_INSERT_ROWID() function is supported.
+
+ sqlite
+
+=head1 OTHER ATTRIBUTES
+
+=head2 rownum
+
+Returns true if the dialect supports the rownum pseudo column.
+
+ oracle
=head2 quote_char
@@ -224,7 +230,7 @@ a table name followed by a column name.
"_build_$feature",
sub{
my ($self) = @_;
- return $dialects->{ $self->type() }->{ $feature };
+ return $dialects->{ $self->_type() }->{ $feature };
},
);
}
@@ -236,35 +242,35 @@ a table name followed by a column name.
# Do something if the dialect supports any form of limit and
# only the select flavor of returning:
- if ($dialect->supports('limit', 'returning_select')) { ... }
+ if ($dialect->supports('limit', 'returning-select')) { ... }
+
+Given a list of feature names, optionally dash-suffixed with a specific quirk, this will
+return true or false if the dialect supports them all.
-Given a list of feature names, optionally suffixed with values, this will return
-true or false if the dialect supports them all.
+=cut
sub supports {
my ($self, @strings) = @_;
foreach my $string (@strings) {
- my $found = 0;
-
- foreach my $feature (@$features) {
- if ($string =~ m{^$feature(.*)$}) {
- my $value = $1;
- $found = 1;
- if ($value) {
- $found = 0 if
- }
- }
- }
+ my ($feature, $wanted_quirk) = split(/-/, $string);
- return 0 if !$found;
+ my $actual_quirk = $self->$feature();
+
+ if (!$wanted_quirk) {
+ return 0 if !defined $actual_quirk;
+ }
+ else {
+ next unless defined($wanted_quirk) or defined($actual_quirk);
+ return 0 if !defined($wanted_quirk);
+ return 0 if !defined($actual_quirk);
+ return 0 if $wanted_quirk ne $actual_quirk;
+ }
}
return 1;
}
-=cut
-
__PACKAGE__->meta->make_immutable;
1;
__END__
@@ -273,11 +279,6 @@ __END__
=over
-=item A single dialect is capable of supporting more than one form of
-each feature (for example, postgresql support both the sequence and
-returning_select auto_id flavors). This module currently does not
-support this. One must be picked.
-
=item * Add more dialects and supported DBI drivers! If anyone wants
to help with this I'd greatly appreciate it.
@@ -287,9 +288,6 @@ is available (MySQL's LOAD INFILE versus O;racle SQL*Loader, etc), information
about what functions to use for date math, which DBD drivers return the
number of records inserted/deleted/selected, etc.
-=item * Possible move the quirk and feature detection logic in to modules
-dedicated to each dialect type, such as DBIx::Dialect::Oracle.
-
=back
=head1 REFERENCES
Please sign in to comment.
Something went wrong with that request. Please try again.