DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
931 lines | 23.196kb
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 DESTROY {
474
    my $self = shift;
475
    
476
    # Disconnect
477
    $self->disconnect if $self->dbh;
478
}
479

            
480
=head1 NAME
481

            
482
DBIx::Custom - DBI with hash parameter binding and filtering system
483

            
484
=cut
485

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
486
our $VERSION = '0.1603';
removed reconnect method
yuki-kimoto authored on 2010-05-28
487

            
488
=head1 STABILITY
489

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
490
This module is not stable. Method name and implementations will be change.
removed reconnect method
yuki-kimoto authored on 2010-05-28
491

            
492
=head1 SYNOPSYS
493
    
494
    # Connect
495
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
496
                                    user => 'ken', password => '!LFKD%$&');
497
    
498
    # Disconnect
499
    $dbi->disconnect
500

            
501
    # Insert 
502
    $dbi->insert(table  => 'books',
503
                 param  => {title => 'perl', author => 'Ken'},
504
                 filter => {title => 'encode_utf8'});
505
    
506
    # Update 
507
    $dbi->update(table  => 'books', 
508
                 param  => {title => 'aaa', author => 'Ken'}, 
509
                 where  => {id => 5},
510
                 filter => {title => 'encode_utf8'});
511
    
512
    # Update all
513
    $dbi->update_all(table  => 'books',
514
                     param  => {title => 'aaa'},
515
                     filter => {title => 'encode_utf8'});
516
    
517
    # Delete
518
    $dbi->delete(table  => 'books',
519
                 where  => {author => 'Ken'},
520
                 filter => {title => 'encode_utf8'});
521
    
522
    # Delete all
523
    $dbi->delete_all(table => 'books');
524
    
525
    # Select
526
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
527
    
removed register_format()
yuki-kimoto authored on 2010-05-26
528
    # Select(more complex)
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
529
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
530
        table  => 'books',
531
        column => [qw/author title/],
532
        where  => {author => 'Ken'},
533
        append => 'order by id limit 1',
534
        filter => {tilte => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
535
    );
added commit method
yuki-kimoto authored on 2010-05-27
536
    
537
    # Select(Join table)
538
    my $result = $dbi->select(
539
        table => ['books', 'rental'],
540
        column => ['books.name as book_name']
541
        relation => {'books.id' => 'rental.book_id'}
542
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
543
    
removed register_format()
yuki-kimoto authored on 2010-05-26
544
    # Execute SQL
545
    $dbi->execute("select title from books");
546
    
547
    # Execute SQL with parameters and filter
548
    $dbi->execute("select id from books where {= author} && {like title}",
549
                  param  => {author => 'ken', title => '%Perl%'},
550
                  filter => {tilte => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
551

            
552
    # Create query and execute it
553
    my $query = $dbi->create_query(
554
        "select id from books where {= author} && {like title}"
555
    );
556
    $dbi->execute($query, param => {author => 'ken', title => '%Perl%'})
removed register_format()
yuki-kimoto authored on 2010-05-26
557
    
558
    # Default filter
559
    $dbi->default_query_filter('encode_utf8');
560
    $dbi->default_fetch_filter('decode_utf8');
561
    
562
    # Fetch
563
    while (my $row = $result->fetch) {
564
        # ...
565
    }
566
    
567
    # Fetch hash
568
    while (my $row = $result->fetch_hash) {
569
        
570
    }
571
    
update document
yuki-kimoto authored on 2010-05-27
572
    # DBI instance
573
    my $dbh = $dbi->dbh;
removed reconnect method
yuki-kimoto authored on 2010-05-28
574

            
575
=head1 DESCRIPTION
576

            
577
L<DBIx::Custom> is useful L<DBI> extention.
578
This module have hash parameter binding and filtering system.
579

            
580
Normally, binding parameter is array.
581
L<DBIx::Custom> enable you to pass binding parameter as hash.
582

            
583
This module also provide filtering system.
584
You can filter the binding parameter
585
or the value of fetching row.
586

            
587
And have useful method such as insert(), update(), delete(), and select().
588

            
589
=head2 Features
590

            
591
=over 4
592

            
593
=item 1. Hash parameter binding.
594

            
595
=item 2. Value filtering.
596

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

            
599
=back
600

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
603
=head2 C<user>
packaging one directory
yuki-kimoto authored on 2009-11-16
604

            
update document
yuki-kimoto authored on 2010-05-27
605
Database user name.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
606
    
version 0.0901
yuki-kimoto authored on 2009-12-17
607
    $dbi  = $dbi->user('Ken');
608
    $user = $dbi->user;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
609
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
610
=head2 C<password>
packaging one directory
yuki-kimoto authored on 2009-11-16
611

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
617
=head2 C<data_source>
packaging one directory
yuki-kimoto authored on 2009-11-16
618

            
update document
yuki-kimoto authored on 2010-05-27
619
Database data source.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
620
    
version 0.0901
yuki-kimoto authored on 2009-12-17
621
    $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
622
    $data_source = $dbi->data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
623
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
624
=head2 C<dbh>
packaging one directory
yuki-kimoto authored on 2009-11-16
625

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
626
Database handle. This is the innstance of L<DBI>
627
    
628
    $dbi = $dbi->dbh($dbh);
629
    $dbh = $dbi->dbh;
packaging one directory
yuki-kimoto authored on 2009-11-16
630

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
633
    my $sth    = $dbi->dbh->prepare("...");
634
    my $errstr = $dbi->dbh->errstr;
635
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
636
=head2 C<filters>
packaging one directory
yuki-kimoto authored on 2009-11-16
637

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

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
648
=head2 C<default_query_filter>
packaging one directory
yuki-kimoto authored on 2009-11-16
649

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
655
=head2 C<default_fetch_filter>
packaging one directory
yuki-kimoto authored on 2009-11-16
656

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
662
=head2 C<result_class>
packaging one directory
yuki-kimoto authored on 2009-11-16
663

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
671
=head2 C<sql_template>
added commit method
yuki-kimoto authored on 2010-05-27
672

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

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

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
687
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
688

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
697
=head2 C<disconnect>
packaging one directory
yuki-kimoto authored on 2009-11-16
698

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
705
=head2 C<insert>
packaging one directory
yuki-kimoto authored on 2009-11-16
706

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
709
    $affected = $dbi->insert(table  => $table, 
710
                             param  => {%param},
711
                             append => $append,
712
                             filter => {%filter});
update document
yuki-kimoto authored on 2009-11-19
713

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
718
    # insert
719
    $dbi->insert(table  => 'books', 
720
                 param  => {title => 'Perl', author => 'Taro'},
721
                 append => "some statement",
722
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
723

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
724
=head2 C<update>
packaging one directory
yuki-kimoto authored on 2009-11-16
725

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
728
    $affected = $dbi->update(table  => $table, 
729
                             param  => {%params},
730
                             where  => {%where},
731
                             append => $append,
732
                             filter => {%filter})
version 0.0901
yuki-kimoto authored on 2009-12-17
733

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
738
    #update
739
    $dbi->update(table  => 'books',
740
                 param  => {title => 'Perl', author => 'Taro'},
741
                 where  => {id => 5},
742
                 append => "some statement",
added commit method
yuki-kimoto authored on 2010-05-27
743
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
744

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
745
=head2 C<update_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
746

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
749
    $affected = $dbi->update_all(table  => $table, 
750
                                 param  => {%params},
751
                                 filter => {%filter},
752
                                 append => $append);
update document
yuki-kimoto authored on 2009-11-19
753

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
758
    # update_all
759
    $dbi->update_all(table  => 'books', 
760
                     param  => {author => 'taro'},
761
                     filter => {author => 'encode_utf8'});
packaging one directory
yuki-kimoto authored on 2009-11-16
762

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
763
=head2 C<delete>
packaging one directory
yuki-kimoto authored on 2009-11-16
764

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
767
    $affected = $dbi->delete(table  => $table,
768
                             where  => {%where},
added commit method
yuki-kimoto authored on 2010-05-27
769
                             append => $append,
removed register_format()
yuki-kimoto authored on 2010-05-26
770
                             filter => {%filter});
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
771

            
version 0.0901
yuki-kimoto authored on 2009-12-17
772
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
773
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
774
Example:
packaging one directory
yuki-kimoto authored on 2009-11-16
775

            
removed register_format()
yuki-kimoto authored on 2010-05-26
776
    # delete
777
    $dbi->delete(table  => 'books',
778
                 where  => {id => 5},
779
                 append => 'some statement',
removed reconnect method
yuki-kimoto authored on 2010-05-28
780
                 filter => {id => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
781

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
782
=head2 C<delete_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
783

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
790
Example:
removed register_format()
yuki-kimoto authored on 2010-05-26
791
    
792
    # delete_all
removed reconnect method
yuki-kimoto authored on 2010-05-28
793
    $dbi->delete_all(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
794

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
795
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
796
    
added commit method
yuki-kimoto authored on 2010-05-27
797
Select rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
798

            
added commit method
yuki-kimoto authored on 2010-05-27
799
    $result = $dbi->select(table    => $table,
800
                           column   => [@column],
801
                           where    => {%where},
802
                           append   => $append,
removed reconnect method
yuki-kimoto authored on 2010-05-28
803
                           relation => {%relation},
added commit method
yuki-kimoto authored on 2010-05-27
804
                           filter   => {%filter});
update document
yuki-kimoto authored on 2009-11-19
805

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

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

            
added commit method
yuki-kimoto authored on 2010-05-27
810
    # select * from books;
removed reconnect method
yuki-kimoto authored on 2010-05-28
811
    $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
812
    
update document
yuki-kimoto authored on 2009-11-19
813
    # select * from books where title = 'Perl';
removed reconnect method
yuki-kimoto authored on 2010-05-28
814
    $result = $dbi->select(table => 'books', where => {title => 1});
update document
yuki-kimoto authored on 2009-11-19
815
    
816
    # select title, author from books where id = 1 for update;
817
    $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
818
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
819
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
820
        where  => {id => 1},
821
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
822
    );
823
    
added commit method
yuki-kimoto authored on 2010-05-27
824
    # select books.name as book_name from books, rental 
825
    # where books.id = rental.book_id;
826
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
827
        table    => ['books', 'rental'],
828
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
829
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
830
    );
831

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
832
=head2 C<create_query>
removed reconnect method
yuki-kimoto authored on 2010-05-28
833
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
834
    my $query = $dbi->create_query(
835
        "select * from authors where {= name} and {= age};"
836
    );
837

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
838
Create the instance of L<DBIx::Custom::Query>. 
839
This receive the string written by SQL template.
packaging one directory
yuki-kimoto authored on 2009-11-16
840

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
841
=head2 C<execute>
removed reconnect method
yuki-kimoto authored on 2010-05-28
842

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
843
    $result = $dbi->execute($query,    param => $params, filter => {%filter});
844
    $result = $dbi->execute($template, param => $params, filter => {%filter});
removed reconnect method
yuki-kimoto authored on 2010-05-28
845

            
846
Execute the instace of L<DBIx::Custom::Query> or
847
the string written by SQL template.
848
Return value is the instance of L<DBIx::Custom::Result>.
849

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
850
B<Example:>
removed reconnect method
yuki-kimoto authored on 2010-05-28
851

            
852
    $result = $dbi->execute("select * from authors where {= name} and {= age}", 
853
                            param => {name => 'taro', age => 19});
854
    
855
    while (my $row = $result->fetch) {
856
        # do something
857
    }
858

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
861
=head2 C<register_filter>
removed reconnect method
yuki-kimoto authored on 2010-05-28
862

            
863
    $dbi->register_filter(%filters);
864
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
865
Resister filter.
866

            
867
B<Example:>
packaging one directory
yuki-kimoto authored on 2009-11-16
868

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
869
    $dbi->register_filter(
870
        encode_utf8 => sub {
871
            my $value = shift;
872
            
873
            require Encode;
874
            
875
            return Encode::encode('UTF-8', $value);
876
        },
877
        decode_utf8 => sub {
878
            my $value = shift;
879
            
880
            require Encode;
881
            
882
            return Encode::decode('UTF-8', $value)
883
        }
884
    );
885

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
886
=head2 C<cache>
added cache_method attribute
yuki-kimoto authored on 2010-06-25
887

            
888
    $dbi   = $dbi->cache(1);
889
    $cache = $dbi->cache;
890

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
891
Cache the result of parsing SQL template.
added cache_method attribute
yuki-kimoto authored on 2010-06-25
892
Default to 1.
893

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
894
=head2 C<cache_method>
895

            
896
    $dbi          = $dbi->cache_method(\&cache_method);
897
    $cache_method = $dbi->cache_method
added cache_method attribute
yuki-kimoto authored on 2010-06-25
898

            
899
Method for cache.
900

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
901
B<Example:>
added cache_method attribute
yuki-kimoto authored on 2010-06-25
902

            
903
    $dbi->cache_method(
904
        sub {
905
            my $self = shift;
906
            
907
            $self->{_cached} ||= {};
908
            
909
            if (@_ > 1) {
910
                $self->{_cached}{$_[0]} = $_[1] 
911
            }
912
            else {
913
                return $self->{_cached}{$_[0]}
914
            }
915
        }
916
    );
917

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
918
=head1 AUTHOR
919

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

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

            
924
Copyright 2009 Yuki Kimoto, all rights reserved.
925

            
926
This program is free software; you can redistribute it and/or modify it
927
under the same terms as Perl itself.
928

            
929
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
930

            
931