DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
308 lines | 5.868kb
first commit
yuki-kimoto authored on 2009-10-13
1
package DBI::Custom;
2
use Object::Simple;
add test
yuki-kimoto authored on 2009-10-16
3

            
4
our $VERSION = '0.0101';
5

            
6
use Carp 'croak';
add some method
yuki-kimoto authored on 2009-10-14
7
use DBI;
add tests
yuki-kimoto authored on 2009-10-25
8
use DBI::Custom::SQL::Template;
add tests
yuki-kimoto authored on 2009-10-25
9
use DBI::Custom::Result;
add tests
yuki-kimoto authored on 2009-10-25
10

            
11
### Class-Object Accessors
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
12
sub connect_info : ClassObjectAttr {
13
    type => 'hash',
14
    initialize => {
add tests
yuki-kimoto authored on 2009-10-25
15
        clone => sub {
add tests
yuki-kimoto authored on 2009-10-25
16
            my $value = shift;
17
            my $new_value = \%{$value || {}};
18
            $new_value->{options} = $value->{options} if $value->{options};
19
            return $new_value;
add tests
yuki-kimoto authored on 2009-10-25
20
        },
21
        default => sub { {} },
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
22
    }
23
}
24

            
25
sub bind_filter  : ClassObjectAttr {
26
    initialize => {clone => 'scalar'}
27
}
28

            
29
sub fetch_filter : ClassObjectAttr {
30
    initialize => {clone => 'scalar'}
31
}
32

            
33
sub filters : ClassObjectAttr {
34
    type => 'hash',
35
    deref => 1,
36
    initialize => {
add tests
yuki-kimoto authored on 2009-10-25
37
        clone   => 'hash',
38
        default => sub { {} }
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
39
    }
40
}
first commit
yuki-kimoto authored on 2009-10-13
41

            
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
42
sub result_class : ClassObjectAttr {
43
    initialize => {
add tests
yuki-kimoto authored on 2009-10-25
44
        clone   => 'scalar',
45
        default => 'DBI::Custom::Result'
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
46
    }
47
}
cleanup
yuki-kimoto authored on 2009-10-14
48

            
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
49
sub sql_template : ClassObjectAttr {
50
    initialize => {
add tests
yuki-kimoto authored on 2009-10-25
51
        clone   => sub {my $value = shift; $value ? $value->clone : undef},
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
52
        default => sub {DBI::Custom::SQL::Template->new}
53
    }
54
}
cleanup
yuki-kimoto authored on 2009-10-15
55

            
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
56
sub valid_connect_info : ClassObjectAttr {
57
    type => 'hash',
58
    deref => 1,
59
    initialize => {
add tests
yuki-kimoto authored on 2009-10-25
60
        clone => 'hash',
add tests
yuki-kimoto authored on 2009-10-25
61
        default => sub { return {map {$_ => 1} qw/data_source user password options/} },
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
62
    }
63
}
cleanup
yuki-kimoto authored on 2009-10-15
64

            
add tests
yuki-kimoto authored on 2009-10-25
65
### Object Accessor
add tests
yuki-kimoto authored on 2009-10-18
66
sub dbh          : Attr {}
add tests
yuki-kimoto authored on 2009-10-25
67

            
68

            
69
### Methods
70
# Add filter
71
sub add_filter {
72
    my $invocant = shift;
73
    
74
    my %old_filters = $invocant->filters;
75
    my %new_filters = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
76
    $invocant->filters(%old_filters, %new_filters);
77
}
add various
yuki-kimoto authored on 2009-10-18
78

            
79
# Auto commit
80
sub auto_commit {
81
    my $self = shift;
82
    
83
    croak("Cannot change AutoCommit becouse of not connected")
84
        unless $self->dbh;
85
    
86
    if (@_) {
87
        $self->dbh->{AutoCommit} = $_[0];
88
        return $self;
89
    }
90
    return $self->dbh->{AutoCommit};
91
}
add test
yuki-kimoto authored on 2009-10-16
92

            
add various things
yuki-kimoto authored on 2009-10-17
93
# Connect
add some method
yuki-kimoto authored on 2009-10-14
94
sub connect {
95
    my $self = shift;
96
    my $connect_info = $self->connect_info;
97
    
add test
yuki-kimoto authored on 2009-10-16
98
    foreach my $key (keys %{$self->connect_info}) {
add test module
yuki-kimoto authored on 2009-10-19
99
        croak("connect_info '$key' is wrong name")
try various
yuki-kimoto authored on 2009-10-21
100
          unless $self->valid_connect_info->{$key};
add test
yuki-kimoto authored on 2009-10-16
101
    }
102
    
add some method
yuki-kimoto authored on 2009-10-14
103
    my $dbh = DBI->connect(
add test
yuki-kimoto authored on 2009-10-16
104
        $connect_info->{data_source},
add some method
yuki-kimoto authored on 2009-10-14
105
        $connect_info->{user},
106
        $connect_info->{password},
107
        {
108
            RaiseError => 1,
109
            PrintError => 0,
110
            AutoCommit => 1,
111
            %{$connect_info->{options} || {} }
112
        }
113
    );
114
    
115
    $self->dbh($dbh);
add various
yuki-kimoto authored on 2009-10-18
116
    return $self;
add some method
yuki-kimoto authored on 2009-10-14
117
}
first commit
yuki-kimoto authored on 2009-10-13
118

            
add tests
yuki-kimoto authored on 2009-10-25
119
# DESTROY
add tests
yuki-kimoto authored on 2009-10-18
120
sub DESTROY {
121
    my $self = shift;
add tests
yuki-kimoto authored on 2009-10-18
122
    $self->disconnect if $self->connected;
add tests
yuki-kimoto authored on 2009-10-18
123
}
124

            
add various things
yuki-kimoto authored on 2009-10-17
125
# Is connected?
126
sub connected {
127
    my $self = shift;
add tests
yuki-kimoto authored on 2009-10-18
128
    return exists $self->{dbh} && eval {$self->{dbh}->can('prepare')};
add various things
yuki-kimoto authored on 2009-10-17
129
}
130

            
131
# Disconnect
132
sub disconnect {
133
    my $self = shift;
add tests
yuki-kimoto authored on 2009-10-18
134
    if ($self->connected) {
add various things
yuki-kimoto authored on 2009-10-17
135
        $self->dbh->disconnect;
136
        delete $self->{dbh};
137
    }
138
}
139

            
140
# Reconnect
141
sub reconnect {
142
    my $self = shift;
add tests
yuki-kimoto authored on 2009-10-18
143
    $self->disconnect if $self->connected;
add various things
yuki-kimoto authored on 2009-10-17
144
    $self->connect;
145
}
146

            
try various
yuki-kimoto authored on 2009-10-21
147
# Run tranzaction
148
sub run_tranzaction {
149
    my ($self, $tranzaction) = @_;
150
    
151
    $self->auto_commit(0);
152
    
153
    eval {
154
        $tranzaction->();
155
        $self->dbh->commit;
156
    };
157
    
158
    if ($@) {
159
        my $tranzaction_error = $@;
160
        
161
        $self->dbh->rollback or croak("$@ and rollback also failed");
162
        croak("$tranzaction_error");
163
    }
164
    $self->auto_commit(1);
add tests
yuki-kimoto authored on 2009-10-18
165
}
166

            
cleanup
yuki-kimoto authored on 2009-10-18
167
# Create SQL from SQL template
add test
yuki-kimoto authored on 2009-10-17
168
sub create_sql {
169
    my $self = shift;
170
    
171
    my ($sql, @bind) = $self->sql_template->create_sql(@_);
172
    
173
    return ($sql, @bind);
174
}
175

            
cleanup
yuki-kimoto authored on 2009-10-18
176
# Prepare and execute SQL
add some method
yuki-kimoto authored on 2009-10-14
177
sub query {
try varioud way
yuki-kimoto authored on 2009-10-17
178
    my ($self, $template, $values, $filter)  = @_;
179
    
add tests
yuki-kimoto authored on 2009-10-18
180
    my $sth_options;
181
    
182
    # Rearrange when argumets is hash referecne 
183
    if (ref $template eq 'HASH') {
184
        my $args = $template;
185
        ($template, $values, $filter, $sth_options)
186
          = @{$args}{qw/template values filter sth_options/};
187
    }
188
    
try varioud way
yuki-kimoto authored on 2009-10-17
189
    $filter ||= $self->bind_filter;
190
    
add various things
yuki-kimoto authored on 2009-10-17
191
    my ($sql, @bind) = $self->create_sql($template, $values, $filter);
add tests
yuki-kimoto authored on 2009-10-18
192
    
193
    $self->connect unless $self->connected;
194
    
add various things
yuki-kimoto authored on 2009-10-17
195
    my $sth = $self->dbh->prepare($sql);
add tests
yuki-kimoto authored on 2009-10-18
196
    
197
    if ($sth_options) {
198
        foreach my $key (keys %$sth_options) {
199
            $sth->{$key} = $sth_options->{$key};
200
        }
201
    }
202
    
cleanup
yuki-kimoto authored on 2009-10-18
203
    # Execute
add tests
yuki-kimoto authored on 2009-10-18
204
    my $ret_val = $sth->execute(@bind);
add various things
yuki-kimoto authored on 2009-10-17
205
    
cleanup
yuki-kimoto authored on 2009-10-18
206
    # Return resultset if select statement is executed
add various things
yuki-kimoto authored on 2009-10-17
207
    if ($sth->{NUM_OF_FIELDS}) {
208
        my $result_class = $self->result_class;
add various
yuki-kimoto authored on 2009-10-18
209
        my $result = $result_class->new({
210
            sth => $sth,
211
            fetch_filter => $self->fetch_filter
212
        });
add various things
yuki-kimoto authored on 2009-10-17
213
        return $result;
214
    }
add tests
yuki-kimoto authored on 2009-10-18
215
    return $ret_val;
add test
yuki-kimoto authored on 2009-10-17
216
}
217

            
cleanup
yuki-kimoto authored on 2009-10-18
218
# Prepare and execute raw SQL
add test
yuki-kimoto authored on 2009-10-17
219
sub query_raw_sql {
cleanup
yuki-kimoto authored on 2009-10-18
220
    my ($self, $sql, @bind_values) = @_;
add tests
yuki-kimoto authored on 2009-10-18
221
    
cleanup
yuki-kimoto authored on 2009-10-18
222
    # Connect
add various
yuki-kimoto authored on 2009-10-18
223
    $self->connect unless $self->connected;
cleanup
yuki-kimoto authored on 2009-10-18
224
    
225
    # Add semicolon if not exist;
add tests
yuki-kimoto authored on 2009-10-18
226
    $sql .= ';' unless $sql =~ /;$/;
cleanup
yuki-kimoto authored on 2009-10-18
227
    
228
    # Prepare
add various things
yuki-kimoto authored on 2009-10-17
229
    my $sth = $self->dbh->prepare($sql);
cleanup
yuki-kimoto authored on 2009-10-18
230
    
231
    # Execute
232
    $sth->execute(@bind_values);
233
    
add various things
yuki-kimoto authored on 2009-10-17
234
    return $sth;
add test
yuki-kimoto authored on 2009-10-17
235
}
236

            
237
Object::Simple->build_class;
238

            
first commit
yuki-kimoto authored on 2009-10-13
239
=head1 NAME
240

            
add test
yuki-kimoto authored on 2009-10-17
241
DBI::Custom - Customizable simple DBI
first commit
yuki-kimoto authored on 2009-10-13
242

            
243
=head1 VERSION
244

            
add test
yuki-kimoto authored on 2009-10-16
245
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
246

            
247
=cut
248

            
249
=head1 SYNOPSIS
250

            
add test
yuki-kimoto authored on 2009-10-16
251
  my $dbi = DBI::Custom->new;
first commit
yuki-kimoto authored on 2009-10-13
252

            
add test
yuki-kimoto authored on 2009-10-16
253
=head1 METHODS
first commit
yuki-kimoto authored on 2009-10-13
254

            
add test
yuki-kimoto authored on 2009-10-16
255
=head2 add_filter
first commit
yuki-kimoto authored on 2009-10-13
256

            
add test
yuki-kimoto authored on 2009-10-16
257
=head2 bind_filter
first commit
yuki-kimoto authored on 2009-10-13
258

            
add test
yuki-kimoto authored on 2009-10-16
259
=head2 connect
first commit
yuki-kimoto authored on 2009-10-13
260

            
add test
yuki-kimoto authored on 2009-10-16
261
=head2 connect_info
first commit
yuki-kimoto authored on 2009-10-13
262

            
add test
yuki-kimoto authored on 2009-10-16
263
=head2 dbh
first commit
yuki-kimoto authored on 2009-10-13
264

            
add test
yuki-kimoto authored on 2009-10-16
265
=head2 fetch_filter
first commit
yuki-kimoto authored on 2009-10-13
266

            
add test
yuki-kimoto authored on 2009-10-16
267
=head2 filters
first commit
yuki-kimoto authored on 2009-10-13
268

            
add test
yuki-kimoto authored on 2009-10-16
269
=head2 new
270

            
271
=head2 query
first commit
yuki-kimoto authored on 2009-10-13
272

            
add test
yuki-kimoto authored on 2009-10-17
273
=head2 create_sql
274

            
275
=head2 query_raw_sql
276

            
277
=head2 sql_template
278

            
add tests
yuki-kimoto authored on 2009-10-18
279
=head2 auto_commit
280

            
281
=head2 connected
282

            
283
=head2 disconnect
284

            
285
=head2 reconnect
286

            
287
=head2 result_class
288

            
cleanup
yuki-kimoto authored on 2009-10-22
289
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
290

            
cleanup
yuki-kimoto authored on 2009-10-22
291
=head2 valid_connect_info
first commit
yuki-kimoto authored on 2009-10-13
292

            
293

            
add various
yuki-kimoto authored on 2009-10-18
294
=head1 AUTHOR
first commit
yuki-kimoto authored on 2009-10-13
295

            
add various
yuki-kimoto authored on 2009-10-18
296
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
first commit
yuki-kimoto authored on 2009-10-13
297

            
298
=head1 COPYRIGHT & LICENSE
299

            
300
Copyright 2009 Yuki Kimoto, all rights reserved.
301

            
302
This program is free software; you can redistribute it and/or modify it
303
under the same terms as Perl itself.
304

            
305

            
306
=cut
307

            
308
1; # End of DBI::Custom