DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
994 lines | 24.035kb
cleanup
yuki-kimoto authored on 2009-12-22
1
package DBIx::Custom;
2

            
3
use strict;
4
use warnings;
5

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
6
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
7

            
packaging one directory
yuki-kimoto authored on 2009-11-16
8
use Carp 'croak';
9
use DBI;
10
use DBIx::Custom::Result;
many many changes
yuki-kimoto authored on 2010-04-30
11
use DBIx::Custom::SQLTemplate;
cleanup
yuki-kimoto authored on 2010-02-11
12
use DBIx::Custom::Query;
update document
yuki-kimoto authored on 2010-05-27
13
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
14

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
15
__PACKAGE__->attr('dbh');
cleanup
yuki-kimoto authored on 2010-04-28
16
__PACKAGE__->attr([qw/user password data_source/]);
update document
yuki-kimoto authored on 2010-05-27
17
__PACKAGE__->attr([qw/default_query_filter default_fetch_filter/]);
packaging one directory
yuki-kimoto authored on 2009-11-16
18

            
removed register_format()
yuki-kimoto authored on 2010-05-26
19
__PACKAGE__->dual_attr('filters', default => sub { {} },
20
                                  inherit => 'hash_copy');
21
__PACKAGE__->register_filter(
update document
yuki-kimoto authored on 2010-05-27
22
    encode_utf8 => sub { encode_utf8($_[0]) },
23
    decode_utf8 => sub { decode_utf8($_[0]) }
removed register_format()
yuki-kimoto authored on 2010-05-26
24
);
packaging one directory
yuki-kimoto authored on 2009-11-16
25

            
cleanup
yuki-kimoto authored on 2010-04-28
26
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
removed register_format()
yuki-kimoto authored on 2010-05-26
27
__PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new });
added cache_method attribute
yuki-kimoto authored on 2010-06-25
28

            
add cache attribute
yuki-kimoto authored on 2010-06-14
29
__PACKAGE__->attr(cache => 1);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
30
__PACKAGE__->attr(cache_method => sub {
31
    sub {
32
        my $self = shift;
33
        
34
        $self->{_cached} ||= {};
35
        
36
        if (@_ > 1) {
37
            $self->{_cached}{$_[0]} = $_[1] 
38
        }
39
        else {
40
            return $self->{_cached}{$_[0]}
41
        }
42
    }
43
});
removed register_format()
yuki-kimoto authored on 2010-05-26
44

            
packaging one directory
yuki-kimoto authored on 2009-11-16
45
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
46
    my $proto = shift;
47
    
48
    # Create
49
    my $self = ref $proto ? $proto : $proto->new(@_);
update document
yuki-kimoto authored on 2010-01-30
50
    
51
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
52
    my $data_source = $self->data_source;
53
    my $user        = $self->user;
54
    my $password    = $self->password;
55
    
update document
yuki-kimoto authored on 2010-01-30
56
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
57
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
58
        $data_source,
59
        $user,
60
        $password,
61
        {
62
            RaiseError => 1,
63
            PrintError => 0,
64
            AutoCommit => 1,
65
        }
66
    )};
67
    
update document
yuki-kimoto authored on 2010-01-30
68
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
69
    croak $@ if $@;
70
    
update document
yuki-kimoto authored on 2010-01-30
71
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
72
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
73
    
packaging one directory
yuki-kimoto authored on 2009-11-16
74
    return $self;
75
}
76

            
77
sub disconnect {
78
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
79
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
80
    # Disconnect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
81
    my $ret = eval { $self->dbh->disconnect };
82
    croak $@ if $@;
removed reconnect method
yuki-kimoto authored on 2010-05-28
83
    $self->dbh(undef);
update document
yuki-kimoto authored on 2010-01-30
84
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
85
    return $ret;
packaging one directory
yuki-kimoto authored on 2009-11-16
86
}
87

            
removed register_format()
yuki-kimoto authored on 2010-05-26
88
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/;
cleanup insert
yuki-kimoto authored on 2010-04-28
89

            
packaging one directory
yuki-kimoto authored on 2009-11-16
90
sub insert {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
91
    my ($self, %args) = @_;
removed register_format()
yuki-kimoto authored on 2010-05-26
92

            
cleanup insert
yuki-kimoto authored on 2010-04-28
93
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
94
    foreach my $name (keys %args) {
cleanup insert
yuki-kimoto authored on 2010-04-28
95
        croak "\"$name\" is invalid name"
96
          unless $VALID_INSERT_ARGS{$name};
97
    }
98
    
removed register_format()
yuki-kimoto authored on 2010-05-26
99
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
100
    my $table  = $args{table} || '';
101
    my $param  = $args{param} || {};
102
    my $append = $args{append} || '';
103
    my $filter = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
104
    
105
    # Insert keys
removed register_format()
yuki-kimoto authored on 2010-05-26
106
    my @insert_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
107
    
108
    # Not exists insert keys
109
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
110
      unless @insert_keys;
111
    
112
    # Templte for insert
113
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
removed register_format()
yuki-kimoto authored on 2010-05-26
114
    $template .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
115
    
116
    # Execute query
removed register_format()
yuki-kimoto authored on 2010-05-26
117
    my $ret_val = $self->execute($template, param  => $param, 
118
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
119
    
120
    return $ret_val;
121
}
122

            
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
123
our %VALID_UPDATE_ARGS
removed register_format()
yuki-kimoto authored on 2010-05-26
124
  = map { $_ => 1 } qw/table param where append filter allow_update_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
125

            
packaging one directory
yuki-kimoto authored on 2009-11-16
126
sub update {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
127
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
128
    
129
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
130
    foreach my $name (keys %args) {
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
131
        croak "\"$name\" is invalid name"
132
          unless $VALID_UPDATE_ARGS{$name};
133
    }
134
    
135
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
136
    my $table            = $args{table} || '';
137
    my $param            = $args{param} || {};
138
    my $where            = $args{where} || {};
139
    my $append_statement = $args{append} || '';
140
    my $filter           = $args{filter};
141
    my $allow_update_all = $args{allow_update_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
142
    
143
    # Update keys
removed register_format()
yuki-kimoto authored on 2010-05-26
144
    my @update_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
145
    
146
    # Not exists update kyes
147
    croak("Key-value pairs for update must be specified to 'update' second argument")
148
      unless @update_keys;
149
    
150
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
151
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
152
    
153
    # Not exists where keys
154
    croak("Key-value pairs for where clause must be specified to 'update' third argument")
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
155
      if !@where_keys && !$allow_update_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
156
    
157
    # Update clause
158
    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
159
    
160
    # Where clause
161
    my $where_clause = '';
simplify filtering system
yuki-kimoto authored on 2010-05-01
162
    my $new_where = {};
many change
yuki-kimoto authored on 2010-04-30
163
    
packaging one directory
yuki-kimoto authored on 2009-11-16
164
    if (@where_keys) {
165
        $where_clause = 'where ';
166
        foreach my $where_key (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
167
            
168
            $where_clause .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
169
        }
170
        $where_clause =~ s/ and $//;
171
    }
172
    
173
    # Template for update
174
    my $template = "update $table $update_clause $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
175
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
176
    
177
    # Rearrange parammeters
removed register_format()
yuki-kimoto authored on 2010-05-26
178
    foreach my $wkey (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
179
        
removed register_format()
yuki-kimoto authored on 2010-05-26
180
        if (exists $param->{$wkey}) {
181
            $param->{$wkey} = [$param->{$wkey}]
182
              unless ref $param->{$wkey} eq 'ARRAY';
simplify filtering system
yuki-kimoto authored on 2010-05-01
183
            
removed register_format()
yuki-kimoto authored on 2010-05-26
184
            push @{$param->{$wkey}}, $where->{$wkey};
simplify filtering system
yuki-kimoto authored on 2010-05-01
185
        }
add tests
yuki-kimoto authored on 2010-05-01
186
        else {
removed register_format()
yuki-kimoto authored on 2010-05-26
187
            $param->{$wkey} = $where->{$wkey};
add tests
yuki-kimoto authored on 2010-05-01
188
        }
simplify filtering system
yuki-kimoto authored on 2010-05-01
189
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
190
    
191
    # Execute query
removed register_format()
yuki-kimoto authored on 2010-05-26
192
    my $ret_val = $self->execute($template, param  => $param, 
193
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
194
    
195
    return $ret_val;
196
}
197

            
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
198
sub update_all { shift->update(allow_update_all => 1, @_) };
packaging one directory
yuki-kimoto authored on 2009-11-16
199

            
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
200
our %VALID_DELETE_ARGS
removed register_format()
yuki-kimoto authored on 2010-05-26
201
  = map { $_ => 1 } qw/table where append filter allow_delete_all/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
202

            
packaging one directory
yuki-kimoto authored on 2009-11-16
203
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
204
    my ($self, %args) = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
205
    
206
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
207
    foreach my $name (keys %args) {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
208
        croak "\"$name\" is invalid name"
209
          unless $VALID_DELETE_ARGS{$name};
210
    }
211
    
212
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
213
    my $table            = $args{table} || '';
214
    my $where            = $args{where} || {};
215
    my $append_statement = $args{append};
216
    my $filter           = $args{filter};
217
    my $allow_delete_all = $args{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
218
    
219
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
220
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
221
    
222
    # Not exists where keys
223
    croak("Key-value pairs for where clause must be specified to 'delete' second argument")
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
224
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
225
    
226
    # Where clause
227
    my $where_clause = '';
228
    if (@where_keys) {
229
        $where_clause = 'where ';
removed register_format()
yuki-kimoto authored on 2010-05-26
230
        foreach my $wkey (@where_keys) {
231
            $where_clause .= "{= $wkey} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
232
        }
233
        $where_clause =~ s/ and $//;
234
    }
235
    
236
    # Template for delete
237
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
238
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
239
    
240
    # Execute query
removed register_format()
yuki-kimoto authored on 2010-05-26
241
    my $ret_val = $self->execute($template, param  => $where, 
242
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
243
    
244
    return $ret_val;
245
}
246

            
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
247
sub delete_all { shift->delete(allow_delete_all => 1, @_) }
packaging one directory
yuki-kimoto authored on 2009-11-16
248

            
refactoring select
yuki-kimoto authored on 2010-04-28
249
our %VALID_SELECT_ARGS
added commit method
yuki-kimoto authored on 2010-05-27
250
  = map { $_ => 1 } qw/table column where append relation filter param/;
refactoring select
yuki-kimoto authored on 2010-04-28
251

            
packaging one directory
yuki-kimoto authored on 2009-11-16
252
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
253
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
254
    
refactoring select
yuki-kimoto authored on 2010-04-28
255
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
256
    foreach my $name (keys %args) {
refactoring select
yuki-kimoto authored on 2010-04-28
257
        croak "\"$name\" is invalid name"
258
          unless $VALID_SELECT_ARGS{$name};
259
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
260
    
refactoring select
yuki-kimoto authored on 2010-04-28
261
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
262
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
263
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
264
    my $columns  = $args{column} || [];
265
    my $where    = $args{where} || {};
266
    my $relation = $args{relation};
267
    my $append   = $args{append};
268
    my $filter   = $args{filter};
269
    my $param    = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
270
    
271
    # SQL template for select statement
272
    my $template = 'select ';
273
    
added commit method
yuki-kimoto authored on 2010-05-27
274
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
275
    if (@$columns) {
276
        foreach my $column (@$columns) {
277
            $template .= "$column, ";
278
        }
279
        $template =~ s/, $/ /;
280
    }
281
    else {
282
        $template .= '* ';
283
    }
284
    
added commit method
yuki-kimoto authored on 2010-05-27
285
    # Table
packaging one directory
yuki-kimoto authored on 2009-11-16
286
    $template .= 'from ';
287
    foreach my $table (@$tables) {
288
        $template .= "$table, ";
289
    }
290
    $template =~ s/, $/ /;
291
    
added commit method
yuki-kimoto authored on 2010-05-27
292
    # Where clause
293
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
294
    if (@where_keys) {
295
        $template .= 'where ';
296
        foreach my $where_key (@where_keys) {
compile success
yuki-kimoto authored on 2010-05-01
297
            $template .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
298
        }
299
    }
300
    $template =~ s/ and $//;
301
    
added commit method
yuki-kimoto authored on 2010-05-27
302
    # Relation
303
    if ($relation) {
304
        $template .= @where_keys ? "and " : "where ";
305
        foreach my $rkey (keys %$relation) {
306
            $template .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
307
        }
308
    }
added commit method
yuki-kimoto authored on 2010-05-27
309
    $template =~ s/ and $//;
310
    
311
    # Append some statement
312
    $template .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
313
    
314
    # Execute query
added commit method
yuki-kimoto authored on 2010-05-27
315
    my $result = $self->execute($template, param  => $where, 
removed register_format()
yuki-kimoto authored on 2010-05-26
316
                                           filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
317
    
318
    return $result;
319
}
320

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
321
sub create_query {
322
    my ($self, $template) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
323
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
324
    # Create query from SQL template
325
    my $sql_template = $self->sql_template;
removed register_format()
yuki-kimoto authored on 2010-05-26
326
    
added cache_method attribute
yuki-kimoto authored on 2010-06-25
327
    my $cache = $self->cache;
version 0.0901
yuki-kimoto authored on 2009-12-17
328
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
329
    # Create query
330
    my $query;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
331
    if ($cache) {
332
        
333
        # Cached query
334
        my $q = $self->cache_method->($self, $template);
335
        
336
        # Create query
337
        $query = DBIx::Custom::Query->new($q) if $q;
removed reconnect method
yuki-kimoto authored on 2010-05-28
338
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
339
    
340
    unless ($query) {
341
        
342
        # Create query
removed reconnect method
yuki-kimoto authored on 2010-05-28
343
        $query = eval{$sql_template->create_query($template)};
344
        croak($@) if $@;
345
        
added cache_method attribute
yuki-kimoto authored on 2010-06-25
346
        # Cache query
347
        $self->cache_method->($self, $template,
348
                             {sql     => $query->sql, 
349
                              columns => $query->columns})
350
          if $cache;
removed reconnect method
yuki-kimoto authored on 2010-05-28
351
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
352
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
353
    # Prepare statement handle
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
354
    my $sth = eval {$self->dbh->prepare($query->{sql})};
355
    croak $@ if $@;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
356
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
357
    # Set statement handle
358
    $query->sth($sth);
359
    
360
    return $query;
361
}
362

            
363
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
364

            
365
sub execute{
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
366
    my ($self, $query, %args)  = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
367
    
368
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
369
    foreach my $name (keys %args) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
370
        croak "\"$name\" is invalid name"
371
          unless $VALID_EXECUTE_ARGS{$name};
372
    }
373
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
374
    my $params = $args{param} || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
375
    
376
    # First argument is SQL template
377
    unless (ref $query eq 'DBIx::Custom::Query') {
378
        my $template;
379
        
380
        if (ref $query eq 'ARRAY') {
381
            $template = $query->[0];
382
        }
383
        else { $template = $query }
384
        
385
        $query = $self->create_query($template);
386
    }
387
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
388
    my $filter = $args{filter} || $query->filter || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
389
    
390
    # Create bind value
391
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
392
    
393
    # Execute
394
    my $sth      = $query->sth;
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
395
    my $affected = eval {$sth->execute(@$bind_values)};
396
    croak $@ if $@;
removed reconnect method
yuki-kimoto authored on 2010-05-28
397
    
398
    # Return resultset if select statement is executed
399
    if ($sth->{NUM_OF_FIELDS}) {
400
        
401
        # Get result class
402
        my $result_class = $self->result_class;
403
        
404
        # Create result
405
        my $result = $result_class->new({
406
            sth             => $sth,
407
            default_filter  => $self->default_fetch_filter,
408
            filters         => $self->filters
409
        });
410
        return $result;
411
    }
412
    return $affected;
413
}
414

            
415
sub _build_bind_values {
416
    my ($self, $query, $params, $filter) = @_;
417
    
418
    # binding values
419
    my @bind_values;
420
    
421
    # Build bind values
422
    my $count = {};
423
    foreach my $column (@{$query->columns}) {
424
        
425
        croak "\"$column\" is not exists in params"
426
          unless exists $params->{$column};
427
        
428
        # Value
429
        my $value = ref $params->{$column} eq 'ARRAY'
430
                  ? $params->{$column}->[$count->{$column} || 0]
431
                  : $params->{$column};
432
        
433
        # Filter
434
        $filter ||= {};
435
        
436
        # Filter name
437
        my $fname = $filter->{$column} || $self->default_query_filter || '';
438
        
439
        my $filter_func;
440
        if ($fname) {
441
            
442
            if (ref $fname eq 'CODE') {
443
                $filter_func = $fname;
444
            }
445
            else {
446
                my $filters = $self->filters;
447
                croak "Not exists filter \"$fname\"" unless exists $filters->{$fname};
448
                $filter_func = $filters->{$fname};
449
            }            
450
        }
451
        
452
        push @bind_values, $filter_func
453
                         ? $filter_func->($value)
454
                         : $value;
455
        
456
        # Count up 
457
        $count->{$column}++;
458
    }
459
    
460
    return \@bind_values;
461
}
462

            
463
sub register_filter {
464
    my $invocant = shift;
465
    
466
    # Add filter
467
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
468
    $invocant->filters({%{$invocant->filters}, %$filters});
469
    
470
    return $invocant;
471
}
472

            
473
sub auto_commit {
474
    my $self = shift;
475
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
476
    # Not connected
477
    croak "Not connected" unless $self->dbh;
478
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
479
    if (@_) {
480
        
481
        # Set AutoCommit
482
        $self->dbh->{AutoCommit} = $_[0];
483
        
484
        return $self;
485
    }
486
    return $self->dbh->{AutoCommit};
487
}
488

            
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
489
sub commit   { 
490
    my $ret = eval { shift->dbh->commit };
491
    croak $@ if $@;
492
    return $ret;
493
}
494
sub rollback {
495
    my $ret = eval { shift->dbh->rollback };
496
    croak $@ if $@;
497
    return $ret;
498
}
removed reconnect method
yuki-kimoto authored on 2010-05-28
499

            
500
sub DESTROY {
501
    my $self = shift;
502
    
503
    # Disconnect
504
    $self->disconnect if $self->dbh;
505
}
506

            
507
=head1 NAME
508

            
509
DBIx::Custom - DBI with hash parameter binding and filtering system
510

            
511
=head1 VERSION
512

            
added cache_method attribute
yuki-kimoto authored on 2010-06-25
513
Version 0.1602
removed reconnect method
yuki-kimoto authored on 2010-05-28
514

            
515
=cut
516

            
added cache_method attribute
yuki-kimoto authored on 2010-06-25
517
our $VERSION = '0.1602';
removed reconnect method
yuki-kimoto authored on 2010-05-28
518
$VERSION = eval $VERSION;
519

            
520
=head1 STABILITY
521

            
522
This module is not stable. Method name and functionality will be change.
523

            
524
=head1 SYNOPSYS
525
    
526
    # Connect
527
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
528
                                    user => 'ken', password => '!LFKD%$&');
529
    
530
    # Disconnect
531
    $dbi->disconnect
532

            
533
    # Insert 
534
    $dbi->insert(table  => 'books',
535
                 param  => {title => 'perl', author => 'Ken'},
536
                 filter => {title => 'encode_utf8'});
537
    
538
    # Update 
539
    $dbi->update(table  => 'books', 
540
                 param  => {title => 'aaa', author => 'Ken'}, 
541
                 where  => {id => 5},
542
                 filter => {title => 'encode_utf8'});
543
    
544
    # Update all
545
    $dbi->update_all(table  => 'books',
546
                     param  => {title => 'aaa'},
547
                     filter => {title => 'encode_utf8'});
548
    
549
    # Delete
550
    $dbi->delete(table  => 'books',
551
                 where  => {author => 'Ken'},
552
                 filter => {title => 'encode_utf8'});
553
    
554
    # Delete all
555
    $dbi->delete_all(table => 'books');
556
    
557
    # Select
558
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
559
    
removed register_format()
yuki-kimoto authored on 2010-05-26
560
    # Select(more complex)
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
561
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
562
        table  => 'books',
563
        column => [qw/author title/],
564
        where  => {author => 'Ken'},
565
        append => 'order by id limit 1',
566
        filter => {tilte => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
567
    );
added commit method
yuki-kimoto authored on 2010-05-27
568
    
569
    # Select(Join table)
570
    my $result = $dbi->select(
571
        table => ['books', 'rental'],
572
        column => ['books.name as book_name']
573
        relation => {'books.id' => 'rental.book_id'}
574
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
575
    
removed register_format()
yuki-kimoto authored on 2010-05-26
576
    # Execute SQL
577
    $dbi->execute("select title from books");
578
    
579
    # Execute SQL with parameters and filter
580
    $dbi->execute("select id from books where {= author} && {like title}",
581
                  param  => {author => 'ken', title => '%Perl%'},
582
                  filter => {tilte => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
583

            
584
    # Create query and execute it
585
    my $query = $dbi->create_query(
586
        "select id from books where {= author} && {like title}"
587
    );
588
    $dbi->execute($query, param => {author => 'ken', title => '%Perl%'})
removed register_format()
yuki-kimoto authored on 2010-05-26
589
    
590
    # Default filter
591
    $dbi->default_query_filter('encode_utf8');
592
    $dbi->default_fetch_filter('decode_utf8');
593
    
594
    # Fetch
595
    while (my $row = $result->fetch) {
596
        # ...
597
    }
598
    
599
    # Fetch hash
600
    while (my $row = $result->fetch_hash) {
601
        
602
    }
603
    
update document
yuki-kimoto authored on 2010-05-27
604
    # DBI instance
605
    my $dbh = $dbi->dbh;
removed reconnect method
yuki-kimoto authored on 2010-05-28
606

            
607
=head1 DESCRIPTION
608

            
609
L<DBIx::Custom> is useful L<DBI> extention.
610
This module have hash parameter binding and filtering system.
611

            
612
Normally, binding parameter is array.
613
L<DBIx::Custom> enable you to pass binding parameter as hash.
614

            
615
This module also provide filtering system.
616
You can filter the binding parameter
617
or the value of fetching row.
618

            
619
And have useful method such as insert(), update(), delete(), and select().
620

            
621
=head2 Features
622

            
623
=over 4
624

            
625
=item 1. Hash parameter binding.
626

            
627
=item 2. Value filtering.
628

            
629
=item 3. Useful methos such as insert(), update(), delete(), and select().
630

            
631
=back
632

            
update document
yuki-kimoto authored on 2010-01-30
633
=head1 ATTRIBUTES
packaging one directory
yuki-kimoto authored on 2009-11-16
634

            
635
=head2 user
636

            
update document
yuki-kimoto authored on 2010-05-27
637
Database user name.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
638
    
version 0.0901
yuki-kimoto authored on 2009-12-17
639
    $dbi  = $dbi->user('Ken');
640
    $user = $dbi->user;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
641
    
packaging one directory
yuki-kimoto authored on 2009-11-16
642
=head2 password
643

            
update document
yuki-kimoto authored on 2010-05-27
644
Database password.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
645
    
version 0.0901
yuki-kimoto authored on 2009-12-17
646
    $dbi      = $dbi->password('lkj&le`@s');
647
    $password = $dbi->password;
packaging one directory
yuki-kimoto authored on 2009-11-16
648

            
649
=head2 data_source
650

            
update document
yuki-kimoto authored on 2010-05-27
651
Database data source.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
652
    
version 0.0901
yuki-kimoto authored on 2009-12-17
653
    $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
654
    $data_source = $dbi->data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
655
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
656
=head2 dbh
packaging one directory
yuki-kimoto authored on 2009-11-16
657

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
658
Database handle. This is the innstance of L<DBI>
659
    
660
    $dbi = $dbi->dbh($dbh);
661
    $dbh = $dbi->dbh;
packaging one directory
yuki-kimoto authored on 2009-11-16
662

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
663
You can use all methods of L<DBI>
packaging one directory
yuki-kimoto authored on 2009-11-16
664

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
665
    my $sth    = $dbi->dbh->prepare("...");
666
    my $errstr = $dbi->dbh->errstr;
667
    
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
668
=head2 filters
packaging one directory
yuki-kimoto authored on 2009-11-16
669

            
update document
yuki-kimoto authored on 2010-01-30
670
Filters
packaging one directory
yuki-kimoto authored on 2009-11-16
671

            
update document
yuki-kimoto authored on 2010-05-27
672
    $dbi     = $dbi->filters({%filters});
version 0.0901
yuki-kimoto authored on 2009-12-17
673
    $filters = $dbi->filters;
674

            
update document
yuki-kimoto authored on 2010-05-27
675
encode_utf8 and decode_utf8 is set to this attribute by default.
version 0.0901
yuki-kimoto authored on 2009-12-17
676

            
update document
yuki-kimoto authored on 2010-05-27
677
    $encode_utf8 = $dbi->filters->{encode_utf8};
678
    $decode_utf8 = $dbi->filters->{decode_utf8};
packaging one directory
yuki-kimoto authored on 2009-11-16
679

            
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
680
=head2 default_query_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
681

            
update document
yuki-kimoto authored on 2010-05-27
682
Default query filter.
packaging one directory
yuki-kimoto authored on 2009-11-16
683

            
update document
yuki-kimoto authored on 2010-05-27
684
    $dbi                  = $dbi->default_query_filter('encode_utf8');
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
685
    $default_query_filter = $dbi->default_query_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
686

            
cleanup
yuki-kimoto authored on 2010-04-28
687
=head2 default_fetch_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
688

            
update document
yuki-kimoto authored on 2010-05-27
689
Fetching filter.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
690

            
update document
yuki-kimoto authored on 2010-05-27
691
    $dbi                  = $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-04-28
692
    $default_fetch_filter = $dbi->default_fetch_filter;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
693

            
packaging one directory
yuki-kimoto authored on 2009-11-16
694
=head2 result_class
695

            
update document
yuki-kimoto authored on 2010-05-27
696
Result class.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
697

            
version 0.0901
yuki-kimoto authored on 2009-12-17
698
    $dbi          = $dbi->result_class('DBIx::Custom::Result');
packaging one directory
yuki-kimoto authored on 2009-11-16
699
    $result_class = $dbi->result_class;
700

            
update document
yuki-kimoto authored on 2010-05-27
701
L<DBIx::Custom::Result> is set to this attribute by default.
update document
yuki-kimoto authored on 2010-01-30
702

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
703
=head2 sql_template
added commit method
yuki-kimoto authored on 2010-05-27
704

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
705
SQLTemplate instance. sql_template attribute must be 
706
the instance of L<DBIx::Cutom::SQLTemplate> subclass.
added commit method
yuki-kimoto authored on 2010-05-27
707

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
708
    $dbi          = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new);
709
    $sql_template = $dbi->sql_template;
added commit method
yuki-kimoto authored on 2010-05-27
710

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
711
the instance of DBIx::Cutom::SQLTemplate is set to 
712
this attribute by default.
added commit method
yuki-kimoto authored on 2010-05-27
713

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
714
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
715

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
716
This class is L<Object::Simple> subclass.
717
You can use all methods of L<Object::Simple>
added commit method
yuki-kimoto authored on 2010-05-27
718

            
packaging one directory
yuki-kimoto authored on 2009-11-16
719
=head2 connect
720

            
update document
yuki-kimoto authored on 2010-05-27
721
Connect to database.
722
    
723
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
724
                                    user => 'ken', password => '!LFKD%$&');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
725

            
update document
yuki-kimoto authored on 2010-05-27
726
"AutoCommit" and "RaiseError" option is true, 
727
and "PrintError" option is false by dfault.
packaging one directory
yuki-kimoto authored on 2009-11-16
728

            
729
=head2 disconnect
730

            
update document
yuki-kimoto authored on 2010-05-27
731
Disconnect database.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
732

            
packaging one directory
yuki-kimoto authored on 2009-11-16
733
    $dbi->disconnect;
734

            
version 0.0901
yuki-kimoto authored on 2009-12-17
735
If database is already disconnected, this method do nothing.
packaging one directory
yuki-kimoto authored on 2009-11-16
736

            
737
=head2 insert
738

            
added commit method
yuki-kimoto authored on 2010-05-27
739
Insert row.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
740

            
removed register_format()
yuki-kimoto authored on 2010-05-26
741
    $affected = $dbi->insert(table  => $table, 
742
                             param  => {%param},
743
                             append => $append,
744
                             filter => {%filter});
update document
yuki-kimoto authored on 2009-11-19
745

            
added commit method
yuki-kimoto authored on 2010-05-27
746
Retruned value is affected rows count.
packaging one directory
yuki-kimoto authored on 2009-11-16
747
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
748
Example:
version 0.0901
yuki-kimoto authored on 2009-12-17
749

            
removed register_format()
yuki-kimoto authored on 2010-05-26
750
    # insert
751
    $dbi->insert(table  => 'books', 
752
                 param  => {title => 'Perl', author => 'Taro'},
753
                 append => "some statement",
754
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
755

            
packaging one directory
yuki-kimoto authored on 2009-11-16
756
=head2 update
757

            
added commit method
yuki-kimoto authored on 2010-05-27
758
Update rows.
update document
yuki-kimoto authored on 2009-11-19
759

            
removed register_format()
yuki-kimoto authored on 2010-05-26
760
    $affected = $dbi->update(table  => $table, 
761
                             param  => {%params},
762
                             where  => {%where},
763
                             append => $append,
764
                             filter => {%filter})
version 0.0901
yuki-kimoto authored on 2009-12-17
765

            
added commit method
yuki-kimoto authored on 2010-05-27
766
Retruned value is affected rows count
update document
yuki-kimoto authored on 2009-11-19
767

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
768
Example:
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
769

            
removed register_format()
yuki-kimoto authored on 2010-05-26
770
    #update
771
    $dbi->update(table  => 'books',
772
                 param  => {title => 'Perl', author => 'Taro'},
773
                 where  => {id => 5},
774
                 append => "some statement",
added commit method
yuki-kimoto authored on 2010-05-27
775
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
776

            
packaging one directory
yuki-kimoto authored on 2009-11-16
777
=head2 update_all
778

            
added commit method
yuki-kimoto authored on 2010-05-27
779
Update all rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
780

            
removed register_format()
yuki-kimoto authored on 2010-05-26
781
    $affected = $dbi->update_all(table  => $table, 
782
                                 param  => {%params},
783
                                 filter => {%filter},
784
                                 append => $append);
update document
yuki-kimoto authored on 2009-11-19
785

            
added commit method
yuki-kimoto authored on 2010-05-27
786
Retruned value is affected rows count.
version 0.0901
yuki-kimoto authored on 2009-12-17
787

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
788
Example:
update document
yuki-kimoto authored on 2009-11-19
789

            
removed register_format()
yuki-kimoto authored on 2010-05-26
790
    # update_all
791
    $dbi->update_all(table  => 'books', 
792
                     param  => {author => 'taro'},
793
                     filter => {author => 'encode_utf8'});
packaging one directory
yuki-kimoto authored on 2009-11-16
794

            
795
=head2 delete
796

            
added commit method
yuki-kimoto authored on 2010-05-27
797
Delete rows.
update document
yuki-kimoto authored on 2009-11-19
798

            
removed register_format()
yuki-kimoto authored on 2010-05-26
799
    $affected = $dbi->delete(table  => $table,
800
                             where  => {%where},
added commit method
yuki-kimoto authored on 2010-05-27
801
                             append => $append,
removed register_format()
yuki-kimoto authored on 2010-05-26
802
                             filter => {%filter});
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
803

            
version 0.0901
yuki-kimoto authored on 2009-12-17
804
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
805
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
806
Example:
packaging one directory
yuki-kimoto authored on 2009-11-16
807

            
removed register_format()
yuki-kimoto authored on 2010-05-26
808
    # delete
809
    $dbi->delete(table  => 'books',
810
                 where  => {id => 5},
811
                 append => 'some statement',
removed reconnect method
yuki-kimoto authored on 2010-05-28
812
                 filter => {id => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
813

            
packaging one directory
yuki-kimoto authored on 2009-11-16
814
=head2 delete_all
815

            
added commit method
yuki-kimoto authored on 2010-05-27
816
Delete all rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
817

            
removed register_format()
yuki-kimoto authored on 2010-05-26
818
    $affected = $dbi->delete_all(table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
819

            
added commit method
yuki-kimoto authored on 2010-05-27
820
Retruned value is affected rows count.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
821

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
822
Example:
removed register_format()
yuki-kimoto authored on 2010-05-26
823
    
824
    # delete_all
removed reconnect method
yuki-kimoto authored on 2010-05-28
825
    $dbi->delete_all(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
826

            
827
=head2 select
828
    
added commit method
yuki-kimoto authored on 2010-05-27
829
Select rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
830

            
added commit method
yuki-kimoto authored on 2010-05-27
831
    $result = $dbi->select(table    => $table,
832
                           column   => [@column],
833
                           where    => {%where},
834
                           append   => $append,
removed reconnect method
yuki-kimoto authored on 2010-05-28
835
                           relation => {%relation},
added commit method
yuki-kimoto authored on 2010-05-27
836
                           filter   => {%filter});
update document
yuki-kimoto authored on 2009-11-19
837

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
838
Return value is the instance of L<DBIx::Custom::Result>.
update document
yuki-kimoto authored on 2009-11-19
839

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
840
Example:
update document
yuki-kimoto authored on 2009-11-19
841

            
added commit method
yuki-kimoto authored on 2010-05-27
842
    # select * from books;
removed reconnect method
yuki-kimoto authored on 2010-05-28
843
    $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
844
    
update document
yuki-kimoto authored on 2009-11-19
845
    # select * from books where title = 'Perl';
removed reconnect method
yuki-kimoto authored on 2010-05-28
846
    $result = $dbi->select(table => 'books', where => {title => 1});
update document
yuki-kimoto authored on 2009-11-19
847
    
848
    # select title, author from books where id = 1 for update;
849
    $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
850
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
851
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
852
        where  => {id => 1},
853
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
854
    );
855
    
added commit method
yuki-kimoto authored on 2010-05-27
856
    # select books.name as book_name from books, rental 
857
    # where books.id = rental.book_id;
858
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
859
        table    => ['books', 'rental'],
860
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
861
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
862
    );
863

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
864
=head2 create_query
865
    
866
Create the instance of L<DBIx::Custom::Query>. 
867
This receive the string written by SQL template.
packaging one directory
yuki-kimoto authored on 2009-11-16
868

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
869
    my $query = $dbi->create_query("select * from authors where {= name} and {= age}");
870

            
871
=head2 execute
872

            
873
Execute the instace of L<DBIx::Custom::Query> or
874
the string written by SQL template.
875
Return value is the instance of L<DBIx::Custom::Result>.
876

            
877
    $result = $dbi->execute($query,    param => $params, filter => {%filter});
878
    $result = $dbi->execute($template, param => $params, filter => {%filter});
879

            
880
Example:
881

            
882
    $result = $dbi->execute("select * from authors where {= name} and {= age}", 
883
                            param => {name => 'taro', age => 19});
884
    
885
    while (my $row = $result->fetch) {
886
        # do something
887
    }
888

            
889
See also L<DBIx::Custom::SQLTemplate> to know how to write SQL template.
890

            
891
=head2 register_filter
892

            
893
Resister filter.
894
    
895
    $dbi->register_filter(%filters);
896
    
897
Example:
packaging one directory
yuki-kimoto authored on 2009-11-16
898

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
899
    $dbi->register_filter(
900
        encode_utf8 => sub {
901
            my $value = shift;
902
            
903
            require Encode;
904
            
905
            return Encode::encode('UTF-8', $value);
906
        },
907
        decode_utf8 => sub {
908
            my $value = shift;
909
            
910
            require Encode;
911
            
912
            return Encode::decode('UTF-8', $value)
913
        }
914
    );
915

            
916
=head2 auto_commit
917

            
918
Auto commit.
packaging one directory
yuki-kimoto authored on 2009-11-16
919

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
920
    $self        = $dbi->auto_commit(1);
921
    $auto_commit = $dbi->auto_commit;
922

            
923
This is equal to
924

            
925
    $dbi->dbh->{AutoCommit} = 1;
926
    $auto_commit = $dbi->dbh->{AutoCommit};
927

            
928
=head2 commit
929

            
930
Commit.
931

            
932
    $dbi->commit;
933

            
934
This is equal to
935

            
936
    $dbi->dbh->commit;
937

            
938
=head2 rollback
939

            
940
Rollback.
941

            
942
    $dbi->rollback
943

            
944
This is equal to
945

            
946
    $dbi->dbh->rollback;
947

            
added cache_method attribute
yuki-kimoto authored on 2010-06-25
948
=head2 cache
949

            
950
Cache the result of parsing SQL template.
951

            
952
    $dbi   = $dbi->cache(1);
953
    $cache = $dbi->cache;
954

            
955
Default to 1.
956

            
957
=head2 cache_method
958

            
959
Method for cache.
960

            
961
    $dbi          = $dbi->cache_method(sub { ... });
962
    $cache_method = $dbi->cache_method
963

            
964
Example:
965
    
966
    $dbi->cache_method(
967
        sub {
968
            my $self = shift;
969
            
970
            $self->{_cached} ||= {};
971
            
972
            if (@_ > 1) {
973
                $self->{_cached}{$_[0]} = $_[1] 
974
            }
975
            else {
976
                return $self->{_cached}{$_[0]}
977
            }
978
        }
979
    );
980

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
981
=head1 AUTHOR
982

            
983
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
version 0.0901
yuki-kimoto authored on 2009-12-17
984

            
packaging one directory
yuki-kimoto authored on 2009-11-16
985
=head1 COPYRIGHT & LICENSE
986

            
987
Copyright 2009 Yuki Kimoto, all rights reserved.
988

            
989
This program is free software; you can redistribute it and/or modify it
990
under the same terms as Perl itself.
991

            
992
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
993

            
994