1 package DBIx::Class::SQLMaker;
8 DBIx::Class::SQLMaker - An SQL::Abstract-based SQL maker class
12 This module is a subclass of L<SQL::Abstract> and includes a number of
13 DBIC-specific workarounds, not yet suitable for inclusion into the
14 L<SQL::Abstract> core. It also provides all (and more than) the functionality
15 of L<SQL::Abstract::Limit>, see L<DBIx::Class::SQLMaker::LimitDialects> for
18 Currently the enhancements to L<SQL::Abstract> are:
22 =item * Support for C<JOIN> statements (via extended C<table/from> support)
24 =item * Support of functions in C<SELECT> lists
26 =item * C<GROUP BY>/C<HAVING> support (via extensions to the order_by parameter)
28 =item * Support of C<...FOR UPDATE> type of select statement modifiers
30 =item * The L</-ident> operator
32 =item * The L</-value> operator
40 DBIx::Class::SQLMaker::LimitDialects
44 use Module::Runtime qw(use_module);
45 use Sub::Name 'subname';
46 use DBIx::Class::Carp;
47 use DBIx::Class::Exception;
51 has limit_dialect => (
52 is => 'rw', default => sub { 'LimitOffset' },
54 $_[0]->clear_renderer_class;
55 $_[0]->clear_converter;
60 if ($_[0]->can('emulate_limit')) {
62 The ancient and horrible emulate_limit method was deprecated for many moons.
63 Now, it is no more. Time to rewrite the code in ${\ref($_[0])}
68 our %LIMIT_DIALECT_MAP = (
69 'GenericSubQ' => 'GenericSubquery',
72 sub mapped_limit_dialect {
74 my $unmapped = $self->limit_dialect;
75 $LIMIT_DIALECT_MAP{$unmapped}||$unmapped;
78 around _build_renderer_roles => sub {
79 my ($orig, $self) = (shift, shift);
82 'Data::Query::Renderer::SQL::Slice::'.$self->mapped_limit_dialect
86 # for when I need a normalized l/r pair
89 { defined $_ ? $_ : '' }
90 ( ref $_[0]->{quote_char} ? (@{$_[0]->{quote_char}}) : ( ($_[0]->{quote_char}) x 2 ) )
94 sub _build_converter_class {
95 Module::Runtime::use_module('DBIx::Class::SQLMaker::Converter')
98 # FIXME when we bring in the storage weaklink, check its schema
99 # weaklink and channel through $schema->throw_exception
100 sub throw_exception { DBIx::Class::Exception->throw($_[1]) }
103 # reinstall the belch()/puke() functions of SQL::Abstract with custom versions
104 # that use DBIx::Class::Carp/DBIx::Class::Exception instead of plain Carp
105 no warnings qw/redefine/;
107 *SQL::Abstract::belch = subname 'SQL::Abstract::belch' => sub (@) {
108 my($func) = (caller(1))[3];
109 carp "[$func] Warning: ", @_;
112 *SQL::Abstract::puke = subname 'SQL::Abstract::puke' => sub (@) {
113 my($func) = (caller(1))[3];
114 __PACKAGE__->throw_exception("[$func] Fatal: " . join ('', @_));
117 # Current SQLA pollutes its namespace - clean for the time being
118 namespace::clean->clean_subroutines(qw/SQL::Abstract carp croak confess/);
121 # the "oh noes offset/top without limit" constant
122 # limited to 31 bits for sanity (and consistency,
123 # since it may be handed to the like of sprintf %u)
125 # Also *some* builds of SQLite fail the test
126 # some_column BETWEEN ? AND ?: 1, 4294967295
127 # with the proper integer bind attrs
129 # Implemented as a method, since ::Storage::DBI also
130 # refers to it (i.e. for the case of software_limit or
131 # as the value to abuse with MSSQL ordered subqueries)
132 sub __max_int () { 0x7FFFFFFF };
134 # poor man's de-qualifier
136 $_[0]->next::method( ( $_[0]{_dequalify_idents} and ! ref $_[1] )
137 ? $_[1] =~ / ([^\.]+) $ /x
143 carp_unique ("-nest in search conditions is deprecated, you most probably wanted:\n"
144 .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
147 shift->next::method(@_);
150 around _converter_args => sub {
151 my ($orig, $self) = (shift, shift);
154 name_sep => $self->name_sep,
155 limit_dialect => $self->mapped_limit_dialect,
156 slice_stability => { $self->renderer->slice_stability },
157 slice_subquery => { $self->renderer->slice_subquery },
161 # Handle limit-dialect selection
164 my ($table, $fields, $where, $rs_attrs, $limit, $offset) = @_;
166 my ($sql, @bind) = $self->next::method(@_);
168 $sql .= $self->_lock_select ($rs_attrs->{for})
171 return wantarray ? ($sql, @bind) : $sql;
174 sub _assemble_binds {
176 return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where group having order limit/);
180 update => 'FOR UPDATE',
181 shared => 'FOR SHARE',
184 my ($self, $type) = @_;
187 if (ref($type) eq 'SCALAR') {
191 $sql = $for_syntax->{$type} || $self->throw_exception( "Unknown SELECT .. FO
192 R type '$type' requested" );
199 scalar shift->_render_sqla(table => \@_);
208 Used to explicitly specify an SQL identifier. Takes a plain string as value
209 which is then invariably treated as a column name (and is being properly
210 quoted if quoting has been requested). Most useful for comparison of two
214 priority => { '<', 2 },
215 requestor => { -ident => 'submitter' }
220 $stmt = 'WHERE "priority" < ? AND "requestor" = "submitter"';
225 The -value operator signals that the argument to the right is a raw bind value.
226 It will be passed straight to DBI, without invoking any of the SQL::Abstract
227 condition-parsing logic. This allows you to, for example, pass an array as a
228 column value for databases that support array datatypes, e.g.:
231 array => { -value => [1, 2, 3] }
236 $stmt = 'WHERE array = ?';
241 See L<DBIx::Class/CONTRIBUTORS>.
245 You may distribute this code under the same terms as Perl itself.