DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
298 lines | 6.148kb
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;
9

            
10
### Class-Object Accessors
11
sub connect_info : ClassObjectAttr { type => 'hash',  auto_build => sub {
12
    shift->Object::Simple::initialize_class_object_attr(
13
        default => sub { {} }, clone => sub {
14
            my $value = shift;
15
            my $new_value = \%{$value || {}};
16
            $new_value->{options} = $value->{options} if $value->{options};
17
            return $new_value;
18
        }
19
    )
20
}}
first commit
yuki-kimoto authored on 2009-10-13
21

            
add tests
yuki-kimoto authored on 2009-10-25
22
sub bind_filter  : ClassObjectAttr { auto_build => sub {
23
    shift->Object::Simple::initialize_class_object_attr(clone => 'scalar')
24
}}
25
sub fetch_filter : ClassObjectAttr { auto_build => sub {
26
    shift->Object::Simple::initialize_class_object_attr(clone => 'scalar')
try various
yuki-kimoto authored on 2009-10-21
27
}}
first commit
yuki-kimoto authored on 2009-10-13
28

            
add tests
yuki-kimoto authored on 2009-10-25
29
sub filters : ClassObjectAttr { type => 'hash', deref => 1, auto_build => sub {
30
    shift->Object::Simple::initialize_class_object_attr(clone => 'hash')
31
}}
first commit
yuki-kimoto authored on 2009-10-13
32

            
add tests
yuki-kimoto authored on 2009-10-25
33
sub result_class : ClassObjectAttr { auto_build => sub {
34
    shift->Object::Simple::initialize_class_object_attr(clone => 'scalar')
35
}}
cleanup
yuki-kimoto authored on 2009-10-14
36

            
add tests
yuki-kimoto authored on 2009-10-25
37
sub sql_template : ClassObjectAttr { auto_build => sub {
38
    shift->Object::Simple::initialize_class_object_attr(
39
        clone   => sub {my $value = shift; $value ? $value->clone : undef},
40
        default => sub { DBI::Custom::SQL::Template->new }
41
    )
42
}}
cleanup
yuki-kimoto authored on 2009-10-15
43

            
add tests
yuki-kimoto authored on 2009-10-25
44
sub valid_connect_info : ClassObjectAttr { type => 'hash', deref => 1, auto_build => sub {
45
    shift->Object::Simple::initialize_class_object_attr(
46
        default => sub { return {map {$_ => 1} qw/data_source user password options/} },
47
        clone => 'hash'
48
    )
49
}}
cleanup
yuki-kimoto authored on 2009-10-15
50

            
add tests
yuki-kimoto authored on 2009-10-25
51
### Object Accessor
add tests
yuki-kimoto authored on 2009-10-18
52
sub dbh          : Attr {}
add tests
yuki-kimoto authored on 2009-10-25
53

            
54

            
55
### Methods
56
# Add filter
57
sub add_filter {
58
    my $invocant = shift;
59
    
60
    my %old_filters = $invocant->filters;
61
    my %new_filters = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
62
    $invocant->filters(%old_filters, %new_filters);
63
}
add various
yuki-kimoto authored on 2009-10-18
64

            
65
# Auto commit
66
sub auto_commit {
67
    my $self = shift;
68
    
69
    croak("Cannot change AutoCommit becouse of not connected")
70
        unless $self->dbh;
71
    
72
    if (@_) {
73
        $self->dbh->{AutoCommit} = $_[0];
74
        return $self;
75
    }
76
    return $self->dbh->{AutoCommit};
77
}
add test
yuki-kimoto authored on 2009-10-16
78

            
add various things
yuki-kimoto authored on 2009-10-17
79
# Connect
add some method
yuki-kimoto authored on 2009-10-14
80
sub connect {
81
    my $self = shift;
82
    my $connect_info = $self->connect_info;
83
    
add test
yuki-kimoto authored on 2009-10-16
84
    foreach my $key (keys %{$self->connect_info}) {
add test module
yuki-kimoto authored on 2009-10-19
85
        croak("connect_info '$key' is wrong name")
try various
yuki-kimoto authored on 2009-10-21
86
          unless $self->valid_connect_info->{$key};
add test
yuki-kimoto authored on 2009-10-16
87
    }
88
    
add some method
yuki-kimoto authored on 2009-10-14
89
    my $dbh = DBI->connect(
add test
yuki-kimoto authored on 2009-10-16
90
        $connect_info->{data_source},
add some method
yuki-kimoto authored on 2009-10-14
91
        $connect_info->{user},
92
        $connect_info->{password},
93
        {
94
            RaiseError => 1,
95
            PrintError => 0,
96
            AutoCommit => 1,
97
            %{$connect_info->{options} || {} }
98
        }
99
    );
100
    
101
    $self->dbh($dbh);
add various
yuki-kimoto authored on 2009-10-18
102
    return $self;
add some method
yuki-kimoto authored on 2009-10-14
103
}
first commit
yuki-kimoto authored on 2009-10-13
104

            
add tests
yuki-kimoto authored on 2009-10-25
105
# DESTROY
add tests
yuki-kimoto authored on 2009-10-18
106
sub DESTROY {
107
    my $self = shift;
add tests
yuki-kimoto authored on 2009-10-18
108
    $self->disconnect if $self->connected;
add tests
yuki-kimoto authored on 2009-10-18
109
}
110

            
add various things
yuki-kimoto authored on 2009-10-17
111
# Is connected?
112
sub connected {
113
    my $self = shift;
add tests
yuki-kimoto authored on 2009-10-18
114
    return exists $self->{dbh} && eval {$self->{dbh}->can('prepare')};
add various things
yuki-kimoto authored on 2009-10-17
115
}
116

            
117
# Disconnect
118
sub disconnect {
119
    my $self = shift;
add tests
yuki-kimoto authored on 2009-10-18
120
    if ($self->connected) {
add various things
yuki-kimoto authored on 2009-10-17
121
        $self->dbh->disconnect;
122
        delete $self->{dbh};
123
    }
124
}
125

            
126
# Reconnect
127
sub reconnect {
128
    my $self = shift;
add tests
yuki-kimoto authored on 2009-10-18
129
    $self->disconnect if $self->connected;
add various things
yuki-kimoto authored on 2009-10-17
130
    $self->connect;
131
}
132

            
try various
yuki-kimoto authored on 2009-10-21
133
# Run tranzaction
134
sub run_tranzaction {
135
    my ($self, $tranzaction) = @_;
136
    
137
    $self->auto_commit(0);
138
    
139
    eval {
140
        $tranzaction->();
141
        $self->dbh->commit;
142
    };
143
    
144
    if ($@) {
145
        my $tranzaction_error = $@;
146
        
147
        $self->dbh->rollback or croak("$@ and rollback also failed");
148
        croak("$tranzaction_error");
149
    }
150
    $self->auto_commit(1);
add tests
yuki-kimoto authored on 2009-10-18
151
}
152

            
cleanup
yuki-kimoto authored on 2009-10-18
153
# Create SQL from SQL template
add test
yuki-kimoto authored on 2009-10-17
154
sub create_sql {
155
    my $self = shift;
156
    
157
    my ($sql, @bind) = $self->sql_template->create_sql(@_);
158
    
159
    return ($sql, @bind);
160
}
161

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

            
cleanup
yuki-kimoto authored on 2009-10-18
204
# Prepare and execute raw SQL
add test
yuki-kimoto authored on 2009-10-17
205
sub query_raw_sql {
cleanup
yuki-kimoto authored on 2009-10-18
206
    my ($self, $sql, @bind_values) = @_;
add tests
yuki-kimoto authored on 2009-10-18
207
    
cleanup
yuki-kimoto authored on 2009-10-18
208
    # Connect
add various
yuki-kimoto authored on 2009-10-18
209
    $self->connect unless $self->connected;
cleanup
yuki-kimoto authored on 2009-10-18
210
    
211
    # Add semicolon if not exist;
add tests
yuki-kimoto authored on 2009-10-18
212
    $sql .= ';' unless $sql =~ /;$/;
cleanup
yuki-kimoto authored on 2009-10-18
213
    
214
    # Prepare
add various things
yuki-kimoto authored on 2009-10-17
215
    my $sth = $self->dbh->prepare($sql);
cleanup
yuki-kimoto authored on 2009-10-18
216
    
217
    # Execute
218
    $sth->execute(@bind_values);
219
    
add various things
yuki-kimoto authored on 2009-10-17
220
    return $sth;
add test
yuki-kimoto authored on 2009-10-17
221
}
222

            
223
Object::Simple->build_class;
224

            
first commit
yuki-kimoto authored on 2009-10-13
225
=head1 NAME
226

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

            
229
=head1 VERSION
230

            
add test
yuki-kimoto authored on 2009-10-16
231
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
232

            
233
=cut
234

            
235
=head1 SYNOPSIS
236

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

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

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

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

            
add test
yuki-kimoto authored on 2009-10-16
245
=head2 clone
first commit
yuki-kimoto authored on 2009-10-13
246

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2009-10-21
257
=head2 prototype
first commit
yuki-kimoto authored on 2009-10-13
258

            
add test
yuki-kimoto authored on 2009-10-16
259
=head2 new
260

            
261
=head2 query
first commit
yuki-kimoto authored on 2009-10-13
262

            
add test
yuki-kimoto authored on 2009-10-17
263
=head2 create_sql
264

            
265
=head2 query_raw_sql
266

            
267
=head2 sql_template
268

            
add tests
yuki-kimoto authored on 2009-10-18
269
=head2 auto_commit
270

            
271
=head2 connected
272

            
273
=head2 disconnect
274

            
275
=head2 reconnect
276

            
277
=head2 result_class
278

            
cleanup
yuki-kimoto authored on 2009-10-22
279
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
280

            
cleanup
yuki-kimoto authored on 2009-10-22
281
=head2 valid_connect_info
first commit
yuki-kimoto authored on 2009-10-13
282

            
283

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

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

            
288
=head1 COPYRIGHT & LICENSE
289

            
290
Copyright 2009 Yuki Kimoto, all rights reserved.
291

            
292
This program is free software; you can redistribute it and/or modify it
293
under the same terms as Perl itself.
294

            
295

            
296
=cut
297

            
298
1; # End of DBI::Custom