Refactoring, basic cursor support, additional syntax supported by HasMany
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQL.pm
CommitLineData
ea2e61bf 1package DBIx::Class::SQL;
2
3use strict;
4use warnings;
5
6use base qw/Class::Data::Inheritable/;
7
8use constant COLS => 0;
9use constant FROM => 1;
10use constant COND => 2;
11
34d52be2 12=head1 NAME
13
14DBIx::Class::SQL - SQL Specific methods for DBIx::Class
15
16=head1 SYNOPSIS
17
18=head1 DESCRIPTION
19
20This class contains methods that generates SQL queries for
21the rest of the L<DBIx::Class> hiarchy. It's also responsible
22for executing these.
23
24=cut
25
ea2e61bf 26__PACKAGE__->mk_classdata('_sql_statements',
27 {
28 'select' =>
8fe001e1 29 sub { "SELECT ".join(', ', @{$_[COLS]})." FROM $_[FROM] WHERE $_[COND]"; },
ea2e61bf 30 'update' =>
8fe001e1 31 sub { "UPDATE $_[FROM] SET ".join(', ', map { "$_ = ?" } @{$_[COLS]}).
ea2e61bf 32 " WHERE $_[COND]"; },
33 'insert' =>
8fe001e1 34 sub { "INSERT INTO $_[FROM] (".join(', ', @{$_[COLS]}).") VALUES (".
35 join(', ', map { '?' } @{$_[COLS]}).")"; },
ea2e61bf 36 'delete' =>
37 sub { "DELETE FROM $_[FROM] WHERE $_[COND]"; },
38 } );
39
40sub _get_sql {
41 my ($class, $name, $cols, $from, $cond) = @_;
8fe001e1 42 my $sql = $class->_sql_statements->{$name}->($cols, $from, $cond);
9bc6db13 43 #warn $sql;
8fe001e1 44 return $sql;
ea2e61bf 45}
46
47sub _sql_to_sth {
48 my ($class, $sql) = @_;
95a70f01 49 return $class->_get_dbh->prepare($sql);
ea2e61bf 50}
51
52sub _get_sth {
53 my $class = shift;
54 return $class->_sql_to_sth($class->_get_sql(@_));
55}
56
571;
34d52be2 58
59=head1 AUTHORS
60
61Matt S. Trout <perl-stuff@trout.me.uk>
62
63=head1 LICENSE
64
65You may distribute this code under the same terms as Perl itself.
66
67=cut