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

            
4
our $VERSION = '0.0101';
5

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

            
12
### Class-Object Accessors
update document
yuki-kimoto authored on 2009-10-27
13
sub user        : ClassObjectAttr { initialize => {clone => 'scalar'} }
14
sub password    : ClassObjectAttr { initialize => {clone => 'scalar'} }
15
sub data_source : ClassObjectAttr { initialize => {clone => 'scalar'} }
add various thins
yuki-kimoto authored on 2009-10-29
16
sub database    : ClassObjectAttr { initialize => {clone => 'scalar'} }
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
17

            
cleanup
yuki-kimoto authored on 2009-10-29
18
sub dbi_options : ClassObjectAttr { initialize => {clone => 'hash', 
update document
yuki-kimoto authored on 2009-10-27
19
                                                  default => sub { {} } } }
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
20

            
update document
yuki-kimoto authored on 2009-10-27
21
sub bind_filter  : ClassObjectAttr { initialize => {clone => 'scalar'} }
22
sub fetch_filter : ClassObjectAttr { initialize => {clone => 'scalar'} }
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
23

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
24
sub no_bind_filters   : ClassObjectAttr { initialize => {clone => 'array'} }
25
sub no_fetch_filters  : ClassObjectAttr { initialize => {clone => 'array'} }
add various thins
yuki-kimoto authored on 2009-10-29
26

            
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
27
sub filters : ClassObjectAttr {
28
    type => 'hash',
29
    deref => 1,
30
    initialize => {
add tests
yuki-kimoto authored on 2009-10-25
31
        clone   => 'hash',
32
        default => sub { {} }
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
33
    }
34
}
first commit
yuki-kimoto authored on 2009-10-13
35

            
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
36
sub result_class : ClassObjectAttr {
37
    initialize => {
add tests
yuki-kimoto authored on 2009-10-25
38
        clone   => 'scalar',
39
        default => 'DBI::Custom::Result'
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
40
    }
41
}
cleanup
yuki-kimoto authored on 2009-10-14
42

            
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
43
sub sql_template : ClassObjectAttr {
44
    initialize => {
update document
yuki-kimoto authored on 2009-10-27
45
        clone   => sub {$_[0] ? $_[0]->clone : undef},
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
46
        default => sub {DBI::Custom::SQL::Template->new}
47
    }
48
}
cleanup
yuki-kimoto authored on 2009-10-15
49

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

            
53

            
54
### Methods
add various thins
yuki-kimoto authored on 2009-10-29
55

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

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

            
add various things
yuki-kimoto authored on 2009-10-17
80
# Connect
add some method
yuki-kimoto authored on 2009-10-14
81
sub connect {
82
    my $self = shift;
update document
yuki-kimoto authored on 2009-10-27
83
    my $data_source = $self->data_source;
84
    my $user        = $self->user;
85
    my $password    = $self->password;
cleanup
yuki-kimoto authored on 2009-10-29
86
    my $dbi_options  = $self->dbi_options;
add test
yuki-kimoto authored on 2009-10-16
87
    
add some method
yuki-kimoto authored on 2009-10-14
88
    my $dbh = DBI->connect(
update document
yuki-kimoto authored on 2009-10-27
89
        $data_source,
90
        $user,
91
        $password,
add some method
yuki-kimoto authored on 2009-10-14
92
        {
93
            RaiseError => 1,
94
            PrintError => 0,
95
            AutoCommit => 1,
cleanup
yuki-kimoto authored on 2009-10-29
96
            %{$dbi_options || {} }
add some method
yuki-kimoto authored on 2009-10-14
97
        }
98
    );
99
    
100
    $self->dbh($dbh);
add various
yuki-kimoto authored on 2009-10-18
101
    return $self;
add some method
yuki-kimoto authored on 2009-10-14
102
}
first commit
yuki-kimoto authored on 2009-10-13
103

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

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

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

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

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

            
add prepare
yuki-kimoto authored on 2009-10-31
152
sub prepare {
153
    my ($self, $sql) = @_;
154
    eval{$self->connect unless $self->connected};
155
    croak($@) if $@;
156
    
157
    my $sth = eval{$self->dbh->prepare($sql)};
158
    croak($@) if $@;
159
    return $sth;
160
}
161

            
162
sub do{
163
    my ($self, $sql, @bind_values) = @_;
164
    eval{$self->connect unless $self->connected};
165
    croak($@) if $@;
166
    
167
    eval{$self->dbh->do($sql, @bind_values)};
168
    croak($@) if $@;
169
}
170

            
add various thins
yuki-kimoto authored on 2009-10-29
171
sub create_query {
172
    my ($self, $template) = @_;
add test
yuki-kimoto authored on 2009-10-17
173
    
add various thins
yuki-kimoto authored on 2009-10-29
174
    # Create query from SQL template
add prepare
yuki-kimoto authored on 2009-10-31
175
    my $sql_template = $self->sql_template;
176
    my $query = eval{$sql_template->create_query($template)};
177
    croak($@) if $@;
add test
yuki-kimoto authored on 2009-10-17
178
    
add various thins
yuki-kimoto authored on 2009-10-29
179
    # Create Query object;
cleanup
yuki-kimoto authored on 2009-10-29
180
    $query = DBI::Custom::Query->new($query);
add various thins
yuki-kimoto authored on 2009-10-29
181
    
182
    # connect if not
183
    $self->connect unless $self->connected;
try varioud way
yuki-kimoto authored on 2009-10-17
184
    
add various thins
yuki-kimoto authored on 2009-10-29
185
    # Prepare statement handle
add prepare
yuki-kimoto authored on 2009-10-31
186
    my $sth = eval{$self->dbh->prepare($query->{sql})};
187
    croak($@) if $@;
add tests
yuki-kimoto authored on 2009-10-18
188
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
189
    # Set statement handle
add various thins
yuki-kimoto authored on 2009-10-29
190
    $query->sth($sth);
add tests
yuki-kimoto authored on 2009-10-18
191
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
192
    # Set bind filter
193
    $query->bind_filter($self->bind_filter);
194
    
195
    # Set no filter keys when binding
196
    $query->no_bind_filters($self->no_bind_filters);
197

            
198
    # Set fetch filter
199
    $query->fetch_filter($self->fetch_filter);
200
    
201
    # Set no filter keys when fetching
202
    $query->no_fetch_filters($self->no_fetch_filters);
203
    
add various thins
yuki-kimoto authored on 2009-10-29
204
    return $query;
205
}
206

            
207
sub execute {
208
    my ($self, $query, $params)  = @_;
add tests
yuki-kimoto authored on 2009-10-29
209
    $params ||= {};
try varioud way
yuki-kimoto authored on 2009-10-17
210
    
add various thins
yuki-kimoto authored on 2009-10-29
211
    # Create bind value
212
    my $bind_values = $self->_build_bind_values($query, $params);
add tests
yuki-kimoto authored on 2009-10-18
213
    
cleanup
yuki-kimoto authored on 2009-10-18
214
    # Execute
cleanup
yuki-kimoto authored on 2009-10-29
215
    my $sth = $query->sth;
216
    my $ret_val = $sth->execute(@$bind_values);
add various things
yuki-kimoto authored on 2009-10-17
217
    
cleanup
yuki-kimoto authored on 2009-10-18
218
    # Return resultset if select statement is executed
add various things
yuki-kimoto authored on 2009-10-17
219
    if ($sth->{NUM_OF_FIELDS}) {
220
        my $result_class = $self->result_class;
add various
yuki-kimoto authored on 2009-10-18
221
        my $result = $result_class->new({
add no_bind_filters
yuki-kimoto authored on 2009-10-30
222
            sth              => $sth,
223
            fetch_filter     => $query->fetch_filter,
224
            no_fetch_filters => $query->no_fetch_filters
add various
yuki-kimoto authored on 2009-10-18
225
        });
add various things
yuki-kimoto authored on 2009-10-17
226
        return $result;
227
    }
add tests
yuki-kimoto authored on 2009-10-18
228
    return $ret_val;
add test
yuki-kimoto authored on 2009-10-17
229
}
230

            
add various thins
yuki-kimoto authored on 2009-10-29
231
sub _build_bind_values {
232
    my ($self, $query, $params) = @_;
cleanup
yuki-kimoto authored on 2009-10-29
233
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
234
    my $key_infos           = $query->key_infos;
235
    my $bind_filter         = $query->bind_filter;
236
    my $no_bind_filters_map = $query->_no_bind_filters_map || {};
cleanup
yuki-kimoto authored on 2009-10-18
237
    
add various thins
yuki-kimoto authored on 2009-10-29
238
    # binding values
239
    my @bind_values;
cleanup
yuki-kimoto authored on 2009-10-18
240
    
add various thins
yuki-kimoto authored on 2009-10-29
241
    # Filter and sdd bind values
cleanup
yuki-kimoto authored on 2009-10-29
242
    foreach my $key_info (@$key_infos) {
243
        my $filtering_key = $key_info->{key};
244
        my $access_keys = $key_info->{access_keys};
add various thins
yuki-kimoto authored on 2009-10-29
245
        
cleanup
yuki-kimoto authored on 2009-10-29
246
        my $original_key = $key_info->{original_key} || '';
247
        my $table        = $key_info->{table}        || '';
248
        my $column       = $key_info->{column}       || '';
add various thins
yuki-kimoto authored on 2009-10-29
249
        
250
        ACCESS_KEYS :
251
        foreach my $access_key (@$access_keys) {
252
            my $root_params = $params;
253
            for (my $i = 0; $i < @$access_key; $i++) {
254
                my $key = $access_key->[$i];
255
                
256
                croak("'access_keys' each value must be string or array reference")
257
                  unless (ref $key eq 'ARRAY' || ($key && !ref $key));
258
                
259
                if ($i == @$access_key - 1) {
260
                    if (ref $key eq 'ARRAY') {
add no_bind_filters
yuki-kimoto authored on 2009-10-30
261
                        if ($bind_filter && !$no_bind_filters_map->{$original_key}) {
262
                            push @bind_values, 
cleanup
yuki-kimoto authored on 2009-10-30
263
                                 $bind_filter->($original_key, $root_params->[$key->[0]],
264
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
265
                        }
266
                        else {
267
                            push @bind_values, scalar $root_params->[$key->[0]];
268
                        }
269
                    }
270
                    else {
271
                        next ACCESS_KEYS unless exists $root_params->{$key};
add no_bind_filters
yuki-kimoto authored on 2009-10-30
272
                        if ($bind_filter && !$no_bind_filters_map->{$original_key}) {
273
                            push @bind_values,
cleanup
yuki-kimoto authored on 2009-10-30
274
                                 $bind_filter->($original_key, $root_params->{$key}, 
275
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
276
                        }
277
                        else {
278
                            push @bind_values, scalar $root_params->{$key};
279
                        }
280
                    }
281
                    return @bind_values;
282
                }
283
                
284
                if ($key eq 'ARRAY') {
285
                    $root_params = $root_params->[$key->[0]];
286
                }
287
                else {
288
                    next ACCESS_KEYS unless exists $root_params->{$key};
289
                    $root_params = $root_params->{$key};
290
                }
291
            }
292
        }
293
        croak("Cannot find key");
update document
yuki-kimoto authored on 2009-10-27
294
    }
add test
yuki-kimoto authored on 2009-10-17
295
}
296

            
add various thins
yuki-kimoto authored on 2009-10-29
297

            
add test
yuki-kimoto authored on 2009-10-17
298
Object::Simple->build_class;
299

            
first commit
yuki-kimoto authored on 2009-10-13
300
=head1 NAME
301

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

            
304
=head1 VERSION
305

            
add test
yuki-kimoto authored on 2009-10-16
306
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
307

            
308
=cut
309

            
310
=head1 SYNOPSIS
311

            
add test
yuki-kimoto authored on 2009-10-16
312
  my $dbi = DBI::Custom->new;
add various thins
yuki-kimoto authored on 2009-10-29
313
  
314
  my $query = $dbi->create_query($template);
315
  $dbi->execute($query);
first commit
yuki-kimoto authored on 2009-10-13
316

            
update document
yuki-kimoto authored on 2009-10-27
317
=head1 CLASS-OBJECT ACCESSORS
first commit
yuki-kimoto authored on 2009-10-13
318

            
update document
yuki-kimoto authored on 2009-10-27
319
=head2 user
320

            
321
    # Set and get database user name
322
    $self = $dbi->user($user);
323
    $user = $dbi->user;
324
    
325
    # Sample
326
    $dbi->user('taro');
327

            
328
=head2 password
329

            
330
    # Set and get database password
331
    $self     = $dbi->password($password);
332
    $password = $dbi->password;
333
    
334
    # Sample
335
    $dbi->password('lkj&le`@s');
336

            
337
=head2 data_source
338

            
339
    # Set and get database data source
340
    $self        = $dbi->data_source($data_soruce);
341
    $data_source = $dbi->data_source;
342
    
343
    # Sample(SQLite)
344
    $dbi->data_source(dbi:SQLite:dbname=$database);
345
    
346
    # Sample(MySQL);
347
    $dbi->data_source("dbi:mysql:dbname=$database");
348
    
349
    # Sample(PostgreSQL)
350
    $dbi->data_source("dbi:Pg:dbname=$database");
cleanup
yuki-kimoto authored on 2009-10-29
351
    
352
=head2 database
353

            
354
    # Set and get database name
355
    $self     = $dbi->database($database);
356
    $database = $dbi->database;
update document
yuki-kimoto authored on 2009-10-27
357

            
cleanup
yuki-kimoto authored on 2009-10-29
358
=head2 dbi_options
update document
yuki-kimoto authored on 2009-10-27
359

            
360
    # Set and get DBI option
cleanup
yuki-kimoto authored on 2009-10-29
361
    $self       = $dbi->dbi_options({$options => $value, ...});
362
    $dbi_options = $dbi->dbi_options;
update document
yuki-kimoto authored on 2009-10-27
363

            
364
    # Sample
cleanup
yuki-kimoto authored on 2009-10-29
365
    $dbi->dbi_options({PrintError => 0, RaiseError => 1});
update document
yuki-kimoto authored on 2009-10-27
366

            
cleanup
yuki-kimoto authored on 2009-10-29
367
dbi_options is used when you connect database by using connect.
update document
yuki-kimoto authored on 2009-10-27
368

            
add prepare
yuki-kimoto authored on 2009-10-31
369
=head2 prepare
370

            
371
    $sth = $dbi->prepare($sql);
372

            
373
This method is same as DBI::prepare
374

            
375
=head2 do
376

            
377
    $dbi->do($sql, @bind_values);
378

            
379
This method is same as DBI::do
380

            
update document
yuki-kimoto authored on 2009-10-27
381
=head2 sql_template
382

            
383
    # Set and get SQL::Template object
384
    $self         = $dbi->sql_template($sql_template);
385
    $sql_template = $dbi->sql_template;
386
    
387
    # Sample
388
    $dbi->sql_template(DBI::Cutom::SQL::Template->new);
389

            
390
=head2 filters
391

            
392
    # Set and get filters
393
    $self    = $dbi->filters($filters);
394
    $filters = $dbi->filters;
first commit
yuki-kimoto authored on 2009-10-13
395

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

            
update document
yuki-kimoto authored on 2009-10-27
398
    # Set and get binding filter
399
    $self        = $dbi->bind_filter($bind_filter);
400
    $bind_filter = $dbi->bind_filter
first commit
yuki-kimoto authored on 2009-10-13
401

            
update document
yuki-kimoto authored on 2009-10-27
402
    # Sample
403
    $dbi->bind_filter($self->filters->{default_bind_filter});
404
    
first commit
yuki-kimoto authored on 2009-10-13
405

            
update document
yuki-kimoto authored on 2009-10-27
406
you can get DBI database handle if you need.
first commit
yuki-kimoto authored on 2009-10-13
407

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

            
update document
yuki-kimoto authored on 2009-10-27
410
    # Set and get Fetch filter
411
    $self         = $dbi->fetch_filter($fetch_filter);
412
    $fetch_filter = $dbi->fetch_filter;
first commit
yuki-kimoto authored on 2009-10-13
413

            
update document
yuki-kimoto authored on 2009-10-27
414
    # Sample
415
    $dbi->fetch_filter($self->filters->{default_fetch_filter});
add test
yuki-kimoto authored on 2009-10-16
416

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
417
=head2 no_bind_filters
418

            
419
    # Set and get no filter keys when binding
420
    $self            = $dbi->no_bind_filters($no_bind_filters);
421
    $no_bind_filters = $dbi->no_bind_filters;
422

            
423
=head2 no_fetch_filters
cleanup
yuki-kimoto authored on 2009-10-29
424

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
425
    # Set and get no filter keys when fetching
426
    $self             = $dbi->no_fetch_filters($no_fetch_filters);
427
    $no_fetch_filters = $dbi->no_fetch_filters;
cleanup
yuki-kimoto authored on 2009-10-29
428

            
update document
yuki-kimoto authored on 2009-10-27
429
=head2 result_class
first commit
yuki-kimoto authored on 2009-10-13
430

            
update document
yuki-kimoto authored on 2009-10-27
431
    # Set and get resultset class
432
    $self         = $dbi->result_class($result_class);
433
    $result_class = $dbi->result_class;
434
    
435
    # Sample
436
    $dbi->result_class('DBI::Custom::Result');
add test
yuki-kimoto authored on 2009-10-17
437

            
update document
yuki-kimoto authored on 2009-10-27
438
=head2 dbh
add test
yuki-kimoto authored on 2009-10-17
439

            
update document
yuki-kimoto authored on 2009-10-27
440
    # Get database handle
441
    $dbh = $self->dbh;
add test
yuki-kimoto authored on 2009-10-17
442

            
update document
yuki-kimoto authored on 2009-10-27
443
=head1 METHODS
add tests
yuki-kimoto authored on 2009-10-18
444

            
update document
yuki-kimoto authored on 2009-10-27
445
=head2 connect
446

            
447
    # Connect to database
448
    $self = $dbi->connect;
449
    
450
    # Sample
451
    $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', 
452
                            data_soruce => "dbi:mysql:dbname=$database");
453
    $dbi->connect;
add tests
yuki-kimoto authored on 2009-10-18
454

            
455
=head2 disconnect
456

            
update document
yuki-kimoto authored on 2009-10-27
457
    # Disconnect database
458
    $dbi->disconnect;
459

            
460
If database is already disconnected, this method do noting.
461

            
add tests
yuki-kimoto authored on 2009-10-18
462
=head2 reconnect
463

            
update document
yuki-kimoto authored on 2009-10-27
464
    # Reconnect
465
    $dbi->reconnect;
466

            
467
=head2 connected
468

            
469
    # Check connected
470
    $dbi->connected
471

            
472
=head2 add_filter
473

            
474
    # Add filter (hash ref or hash can be recieve)
475
    $self = $dbi->add_filter({$filter_name => $filter, ...});
476
    $self = $dbi->add_filter($filetr_name => $filter, ...);
477
    
478
    # Sample
479
    $dbi->add_filter(
480
        decode_utf8 => sub {
cleanup
yuki-kimoto authored on 2009-10-30
481
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
482
            return Encode::decode('UTF-8', $value);
483
        },
484
        datetime_to_string => sub {
cleanup
yuki-kimoto authored on 2009-10-30
485
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
486
            return $value->strftime('%Y-%m-%d %H:%M:%S')
487
        },
488
        default_bind_filter => sub {
cleanup
yuki-kimoto authored on 2009-10-30
489
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
490
            if (ref $value eq 'Time::Piece') {
cleanup
yuki-kimoto authored on 2009-10-30
491
                return $dbi->filters->{datetime_to_string}->($value);
update document
yuki-kimoto authored on 2009-10-27
492
            }
493
            else {
cleanup
yuki-kimoto authored on 2009-10-30
494
                return $dbi->filters->{decode_utf8}->($value);
update document
yuki-kimoto authored on 2009-10-27
495
            }
496
        },
497
        
498
        encode_utf8 => sub {
cleanup
yuki-kimoto authored on 2009-10-30
499
            my ($key, $value) = @_;
update document
yuki-kimoto authored on 2009-10-27
500
            return Encode::encode('UTF-8', $value);
501
        },
502
        string_to_datetime => sub {
cleanup
yuki-kimoto authored on 2009-10-30
503
            my ($key, $value) = @_;
update document
yuki-kimoto authored on 2009-10-27
504
            return DateTime::Format::MySQL->parse_datetime($value);
505
        },
506
        default_fetch_filter => sub {
cleanup
yuki-kimoto authored on 2009-10-30
507
            my ($key, $value, $type, $sth, $i) = @_;
update document
yuki-kimoto authored on 2009-10-27
508
            if ($type eq 'DATETIME') {
cleanup
yuki-kimoto authored on 2009-10-30
509
                return $dbi->filters->{string_to_datetime}->($value);
update document
yuki-kimoto authored on 2009-10-27
510
            }
511
            else {
cleanup
yuki-kimoto authored on 2009-10-30
512
                return $dbi->filters->{encode_utf8}->($value);
update document
yuki-kimoto authored on 2009-10-27
513
            }
514
        }
515
    );
516

            
517
add_filter add filter to filters
add tests
yuki-kimoto authored on 2009-10-18
518

            
cleanup
yuki-kimoto authored on 2009-10-29
519
=head2 create_query
520
    
521
    # Create Query object from SQL template
522
    my $query = $dbi->create_query($template);
523
    
524
=head2 execute
update document
yuki-kimoto authored on 2009-10-27
525

            
526
    # Parse SQL template and execute SQL
cleanup
yuki-kimoto authored on 2009-10-29
527
    $result = $dbi->query($query, $params);
528
    $result = $dbi->query($template, $params); # Shorcut
update document
yuki-kimoto authored on 2009-10-27
529
    
530
    # Sample
531
    $result = $dbi->query("select * from authors where {= name} && {= age}", 
532
                          {author => 'taro', age => 19});
533
    
534
    while (my @row = $result->fetch) {
535
        # do something
536
    }
537

            
538
See also L<DBI::Custom::SQL::Template>
539

            
cleanup
yuki-kimoto authored on 2009-10-22
540
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
541

            
update document
yuki-kimoto authored on 2009-10-27
542
    # Run tranzaction
543
    $dbi->run_tranzaction(sub {
544
        # do something
545
    });
first commit
yuki-kimoto authored on 2009-10-13
546

            
update document
yuki-kimoto authored on 2009-10-27
547
If tranzaction is success, commit is execute. 
548
If tranzation is died, rollback is execute.
first commit
yuki-kimoto authored on 2009-10-13
549

            
cleanup
yuki-kimoto authored on 2009-10-29
550

            
551

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

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

            
556
=head1 COPYRIGHT & LICENSE
557

            
558
Copyright 2009 Yuki Kimoto, all rights reserved.
559

            
560
This program is free software; you can redistribute it and/or modify it
561
under the same terms as Perl itself.
562

            
563

            
564
=cut
565

            
566
1; # End of DBI::Custom