Quick synopsis to Schema system in POD
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / Storage / DBI.pm
CommitLineData
8b445e33 1package DBIx::Class::Storage::DBI;
2
3use DBI;
4
5use base qw/DBIx::Class/;
6
7__PACKAGE__->load_components(qw/SQL::Abstract SQL Exception AccessorGroup/);
8
9__PACKAGE__->mk_group_accessors('simple' => qw/connect_info _dbh/);
10
11sub new {
12 bless({}, ref $_[0] || $_[0]);
13}
14
15sub get_simple {
16 my ($self, $get) = @_;
17 return $self->{$get};
18}
19
20sub set_simple {
21 my ($self, $set, $val) = @_;
22 return $self->{$set} = $val;
23}
24
25=head1 NAME
26
27DBIx::Class::Storage::DBI - DBI storage handler
28
29=head1 SYNOPSIS
30
31=head1 DESCRIPTION
32
33This class represents the connection to the database
34
35=head1 METHODS
36
37=over 4
38
39=cut
40
41sub dbh {
42 my ($self) = @_;
43 my $dbh;
44 unless (($dbh = $self->_dbh) && $dbh->FETCH('Active') && $dbh->ping) {
45 $self->_populate_dbh;
46 }
47 return $self->_dbh;
48}
49
50sub _populate_dbh {
51 my ($self) = @_;
52 my @info = @{$self->connect_info || []};
53 $self->_dbh($self->_connect(@info));
54}
55
56sub _connect {
57 my ($self, @info) = @_;
58 return DBI->connect(@info);
59}
60
61=item commit
62
63 $class->commit;
64
65Issues a commit again the current dbh
66
67=cut
68
69sub commit { $_[0]->dbh->commit; }
70
71=item rollback
72
73 $class->rollback;
74
75Issues a rollback again the current dbh
76
77=cut
78
79sub rollback { $_[0]->dbh->rollback; }
80
81sub insert {
82 my ($self, $ident, $to_insert) = @_;
83 my $sql = $self->create_sql('insert', [ keys %{$to_insert} ], $ident, undef);
84 my $sth = $self->sth($sql);
85 $sth->execute(values %{$to_insert});
86 $self->throw( "Couldn't insert ".join(%to_insert)." into ${ident}" )
87 unless $sth->rows;
88 return $to_insert;
89}
90
91sub update {
92 my ($self, $ident, $to_update, $condition) = @_;
93 my $attrs = { };
94 my $set_sql = $self->_cond_resolve($to_update, $attrs, ',');
95 $set_sql =~ s/^\(//;
96 $set_sql =~ s/\)$//;
97 my $cond_sql = $self->_cond_resolve($condition, $attrs);
98 my $sql = $self->create_sql('update', $set_sql, $ident, $cond_sql);
99 my $sth = $self->sth($sql);
100 my $rows = $sth->execute( @{$attrs->{bind}||[]} );
101 return $rows;
102}
103
104sub delete {
105 my ($self, $ident, $condition) = @_;
106 my $attrs = { };
107 my $cond_sql = $self->_cond_resolve($condition, $attrs);
108 my $sql = $self->create_sql('delete', undef, $ident, $cond_sql);
109 #warn "$sql ".join(', ',@{$attrs->{bind}||[]});
110 my $sth = $self->sth($sql);
111 return $sth->execute( @{$attrs->{bind}||[]} );
112}
113
114sub select {
115 my ($self, $ident, $select, $condition, $attrs) = @_;
116 $attrs ||= { };
117 #my $select_sql = $self->_cond_resolve($select, $attrs, ',');
118 my $cond_sql = $self->_cond_resolve($condition, $attrs);
119 1 while $cond_sql =~ s/^\s*\(\s*(.*ORDER.*)\s*\)\s*$/$1/;
120 my $sql = $self->create_sql('select', $select, $ident, $cond_sql);
121 #warn $sql.' '.join(', ', @{$attrs->{bind}||[]});
122 my $sth = $self->sth($sql);
123 if (@{$attrs->{bind}||[]}) {
124 $sth->execute( @{$attrs->{bind}||[]} );
125 } else {
f8c4996c 126 # disable unexplained 'Use of uninitialized value in subroutine entry'
127 # warnings
128 no warnings 'uninitialized';
8b445e33 129 $sth->execute;
130 }
131 return $sth;
132}
133
134sub sth {
135 shift->dbh->prepare(@_);
136}
137
1381;
139
140=back
141
142=head1 AUTHORS
143
144Matt S. Trout <perl-stuff@trout.me.uk>
145
146=head1 LICENSE
147
148You may distribute this code under the same terms as Perl itself.
149
150=cut
151