DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
532 lines | 13.402kb
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 various thins
yuki-kimoto authored on 2009-10-29
152
sub create_query {
153
    my ($self, $template) = @_;
add test
yuki-kimoto authored on 2009-10-17
154
    
add various thins
yuki-kimoto authored on 2009-10-29
155
    # Create query from SQL template
156
    my $query = $self->sql_template->create_query($template);
add test
yuki-kimoto authored on 2009-10-17
157
    
add various thins
yuki-kimoto authored on 2009-10-29
158
    # Create Query object;
cleanup
yuki-kimoto authored on 2009-10-29
159
    $query = DBI::Custom::Query->new($query);
add various thins
yuki-kimoto authored on 2009-10-29
160
    
161
    # connect if not
162
    $self->connect unless $self->connected;
try varioud way
yuki-kimoto authored on 2009-10-17
163
    
add various thins
yuki-kimoto authored on 2009-10-29
164
    # Prepare statement handle
165
    my $sth = $self->dbh->prepare($query->{sql});
add tests
yuki-kimoto authored on 2009-10-18
166
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
167
    # Set statement handle
add various thins
yuki-kimoto authored on 2009-10-29
168
    $query->sth($sth);
add tests
yuki-kimoto authored on 2009-10-18
169
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
170
    # Set bind filter
171
    $query->bind_filter($self->bind_filter);
172
    
173
    # Set no filter keys when binding
174
    $query->no_bind_filters($self->no_bind_filters);
175

            
176
    # Set fetch filter
177
    $query->fetch_filter($self->fetch_filter);
178
    
179
    # Set no filter keys when fetching
180
    $query->no_fetch_filters($self->no_fetch_filters);
181
    
add various thins
yuki-kimoto authored on 2009-10-29
182
    return $query;
183
}
184

            
185
sub execute {
186
    my ($self, $query, $params)  = @_;
add tests
yuki-kimoto authored on 2009-10-29
187
    $params ||= {};
try varioud way
yuki-kimoto authored on 2009-10-17
188
    
add various thins
yuki-kimoto authored on 2009-10-29
189
    # Create bind value
190
    my $bind_values = $self->_build_bind_values($query, $params);
add tests
yuki-kimoto authored on 2009-10-18
191
    
cleanup
yuki-kimoto authored on 2009-10-18
192
    # Execute
cleanup
yuki-kimoto authored on 2009-10-29
193
    my $sth = $query->sth;
194
    my $ret_val = $sth->execute(@$bind_values);
add various things
yuki-kimoto authored on 2009-10-17
195
    
cleanup
yuki-kimoto authored on 2009-10-18
196
    # Return resultset if select statement is executed
add various things
yuki-kimoto authored on 2009-10-17
197
    if ($sth->{NUM_OF_FIELDS}) {
198
        my $result_class = $self->result_class;
add various
yuki-kimoto authored on 2009-10-18
199
        my $result = $result_class->new({
add no_bind_filters
yuki-kimoto authored on 2009-10-30
200
            sth              => $sth,
201
            fetch_filter     => $query->fetch_filter,
202
            no_fetch_filters => $query->no_fetch_filters
add various
yuki-kimoto authored on 2009-10-18
203
        });
add various things
yuki-kimoto authored on 2009-10-17
204
        return $result;
205
    }
add tests
yuki-kimoto authored on 2009-10-18
206
    return $ret_val;
add test
yuki-kimoto authored on 2009-10-17
207
}
208

            
add various thins
yuki-kimoto authored on 2009-10-29
209
sub _build_bind_values {
210
    my ($self, $query, $params) = @_;
cleanup
yuki-kimoto authored on 2009-10-29
211
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
212
    my $key_infos           = $query->key_infos;
213
    my $bind_filter         = $query->bind_filter;
214
    my $no_bind_filters_map = $query->_no_bind_filters_map || {};
cleanup
yuki-kimoto authored on 2009-10-18
215
    
add various thins
yuki-kimoto authored on 2009-10-29
216
    # binding values
217
    my @bind_values;
cleanup
yuki-kimoto authored on 2009-10-18
218
    
add various thins
yuki-kimoto authored on 2009-10-29
219
    # Filter and sdd bind values
cleanup
yuki-kimoto authored on 2009-10-29
220
    foreach my $key_info (@$key_infos) {
221
        my $filtering_key = $key_info->{key};
222
        my $access_keys = $key_info->{access_keys};
add various thins
yuki-kimoto authored on 2009-10-29
223
        
cleanup
yuki-kimoto authored on 2009-10-29
224
        my $original_key = $key_info->{original_key} || '';
225
        my $table        = $key_info->{table}        || '';
226
        my $column       = $key_info->{column}       || '';
add various thins
yuki-kimoto authored on 2009-10-29
227
        
228
        ACCESS_KEYS :
229
        foreach my $access_key (@$access_keys) {
230
            my $root_params = $params;
231
            for (my $i = 0; $i < @$access_key; $i++) {
232
                my $key = $access_key->[$i];
233
                
234
                croak("'access_keys' each value must be string or array reference")
235
                  unless (ref $key eq 'ARRAY' || ($key && !ref $key));
236
                
237
                if ($i == @$access_key - 1) {
238
                    if (ref $key eq 'ARRAY') {
add no_bind_filters
yuki-kimoto authored on 2009-10-30
239
                        if ($bind_filter && !$no_bind_filters_map->{$original_key}) {
240
                            push @bind_values, 
cleanup
yuki-kimoto authored on 2009-10-30
241
                                 $bind_filter->($original_key, $root_params->[$key->[0]],
242
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
243
                        }
244
                        else {
245
                            push @bind_values, scalar $root_params->[$key->[0]];
246
                        }
247
                    }
248
                    else {
249
                        next ACCESS_KEYS unless exists $root_params->{$key};
add no_bind_filters
yuki-kimoto authored on 2009-10-30
250
                        if ($bind_filter && !$no_bind_filters_map->{$original_key}) {
251
                            push @bind_values,
cleanup
yuki-kimoto authored on 2009-10-30
252
                                 $bind_filter->($original_key, $root_params->{$key}, 
253
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
254
                        }
255
                        else {
256
                            push @bind_values, scalar $root_params->{$key};
257
                        }
258
                    }
259
                    return @bind_values;
260
                }
261
                
262
                if ($key eq 'ARRAY') {
263
                    $root_params = $root_params->[$key->[0]];
264
                }
265
                else {
266
                    next ACCESS_KEYS unless exists $root_params->{$key};
267
                    $root_params = $root_params->{$key};
268
                }
269
            }
270
        }
271
        croak("Cannot find key");
update document
yuki-kimoto authored on 2009-10-27
272
    }
add test
yuki-kimoto authored on 2009-10-17
273
}
274

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
278
=head1 NAME
279

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

            
282
=head1 VERSION
283

            
add test
yuki-kimoto authored on 2009-10-16
284
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
285

            
286
=cut
287

            
288
=head1 SYNOPSIS
289

            
add test
yuki-kimoto authored on 2009-10-16
290
  my $dbi = DBI::Custom->new;
add various thins
yuki-kimoto authored on 2009-10-29
291
  
292
  my $query = $dbi->create_query($template);
293
  $dbi->execute($query);
first commit
yuki-kimoto authored on 2009-10-13
294

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

            
update document
yuki-kimoto authored on 2009-10-27
297
=head2 user
298

            
299
    # Set and get database user name
300
    $self = $dbi->user($user);
301
    $user = $dbi->user;
302
    
303
    # Sample
304
    $dbi->user('taro');
305

            
306
=head2 password
307

            
308
    # Set and get database password
309
    $self     = $dbi->password($password);
310
    $password = $dbi->password;
311
    
312
    # Sample
313
    $dbi->password('lkj&le`@s');
314

            
315
=head2 data_source
316

            
317
    # Set and get database data source
318
    $self        = $dbi->data_source($data_soruce);
319
    $data_source = $dbi->data_source;
320
    
321
    # Sample(SQLite)
322
    $dbi->data_source(dbi:SQLite:dbname=$database);
323
    
324
    # Sample(MySQL);
325
    $dbi->data_source("dbi:mysql:dbname=$database");
326
    
327
    # Sample(PostgreSQL)
328
    $dbi->data_source("dbi:Pg:dbname=$database");
cleanup
yuki-kimoto authored on 2009-10-29
329
    
330
=head2 database
331

            
332
    # Set and get database name
333
    $self     = $dbi->database($database);
334
    $database = $dbi->database;
update document
yuki-kimoto authored on 2009-10-27
335

            
cleanup
yuki-kimoto authored on 2009-10-29
336
=head2 dbi_options
update document
yuki-kimoto authored on 2009-10-27
337

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

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

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

            
347
=head2 sql_template
348

            
349
    # Set and get SQL::Template object
350
    $self         = $dbi->sql_template($sql_template);
351
    $sql_template = $dbi->sql_template;
352
    
353
    # Sample
354
    $dbi->sql_template(DBI::Cutom::SQL::Template->new);
355

            
356
=head2 filters
357

            
358
    # Set and get filters
359
    $self    = $dbi->filters($filters);
360
    $filters = $dbi->filters;
first commit
yuki-kimoto authored on 2009-10-13
361

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

            
update document
yuki-kimoto authored on 2009-10-27
364
    # Set and get binding filter
365
    $self        = $dbi->bind_filter($bind_filter);
366
    $bind_filter = $dbi->bind_filter
first commit
yuki-kimoto authored on 2009-10-13
367

            
update document
yuki-kimoto authored on 2009-10-27
368
    # Sample
369
    $dbi->bind_filter($self->filters->{default_bind_filter});
370
    
first commit
yuki-kimoto authored on 2009-10-13
371

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

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

            
update document
yuki-kimoto authored on 2009-10-27
376
    # Set and get Fetch filter
377
    $self         = $dbi->fetch_filter($fetch_filter);
378
    $fetch_filter = $dbi->fetch_filter;
first commit
yuki-kimoto authored on 2009-10-13
379

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

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
383
=head2 no_bind_filters
384

            
385
    # Set and get no filter keys when binding
386
    $self            = $dbi->no_bind_filters($no_bind_filters);
387
    $no_bind_filters = $dbi->no_bind_filters;
388

            
389
=head2 no_fetch_filters
cleanup
yuki-kimoto authored on 2009-10-29
390

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
391
    # Set and get no filter keys when fetching
392
    $self             = $dbi->no_fetch_filters($no_fetch_filters);
393
    $no_fetch_filters = $dbi->no_fetch_filters;
cleanup
yuki-kimoto authored on 2009-10-29
394

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

            
update document
yuki-kimoto authored on 2009-10-27
397
    # Set and get resultset class
398
    $self         = $dbi->result_class($result_class);
399
    $result_class = $dbi->result_class;
400
    
401
    # Sample
402
    $dbi->result_class('DBI::Custom::Result');
add test
yuki-kimoto authored on 2009-10-17
403

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

            
update document
yuki-kimoto authored on 2009-10-27
406
    # Get database handle
407
    $dbh = $self->dbh;
add test
yuki-kimoto authored on 2009-10-17
408

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

            
update document
yuki-kimoto authored on 2009-10-27
411
=head2 connect
412

            
413
    # Connect to database
414
    $self = $dbi->connect;
415
    
416
    # Sample
417
    $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', 
418
                            data_soruce => "dbi:mysql:dbname=$database");
419
    $dbi->connect;
add tests
yuki-kimoto authored on 2009-10-18
420

            
421
=head2 disconnect
422

            
update document
yuki-kimoto authored on 2009-10-27
423
    # Disconnect database
424
    $dbi->disconnect;
425

            
426
If database is already disconnected, this method do noting.
427

            
add tests
yuki-kimoto authored on 2009-10-18
428
=head2 reconnect
429

            
update document
yuki-kimoto authored on 2009-10-27
430
    # Reconnect
431
    $dbi->reconnect;
432

            
433
=head2 connected
434

            
435
    # Check connected
436
    $dbi->connected
437

            
438
=head2 add_filter
439

            
440
    # Add filter (hash ref or hash can be recieve)
441
    $self = $dbi->add_filter({$filter_name => $filter, ...});
442
    $self = $dbi->add_filter($filetr_name => $filter, ...);
443
    
444
    # Sample
445
    $dbi->add_filter(
446
        decode_utf8 => sub {
cleanup
yuki-kimoto authored on 2009-10-30
447
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
448
            return Encode::decode('UTF-8', $value);
449
        },
450
        datetime_to_string => sub {
cleanup
yuki-kimoto authored on 2009-10-30
451
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
452
            return $value->strftime('%Y-%m-%d %H:%M:%S')
453
        },
454
        default_bind_filter => sub {
cleanup
yuki-kimoto authored on 2009-10-30
455
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
456
            if (ref $value eq 'Time::Piece') {
cleanup
yuki-kimoto authored on 2009-10-30
457
                return $dbi->filters->{datetime_to_string}->($value);
update document
yuki-kimoto authored on 2009-10-27
458
            }
459
            else {
cleanup
yuki-kimoto authored on 2009-10-30
460
                return $dbi->filters->{decode_utf8}->($value);
update document
yuki-kimoto authored on 2009-10-27
461
            }
462
        },
463
        
464
        encode_utf8 => sub {
cleanup
yuki-kimoto authored on 2009-10-30
465
            my ($key, $value) = @_;
update document
yuki-kimoto authored on 2009-10-27
466
            return Encode::encode('UTF-8', $value);
467
        },
468
        string_to_datetime => sub {
cleanup
yuki-kimoto authored on 2009-10-30
469
            my ($key, $value) = @_;
update document
yuki-kimoto authored on 2009-10-27
470
            return DateTime::Format::MySQL->parse_datetime($value);
471
        },
472
        default_fetch_filter => sub {
cleanup
yuki-kimoto authored on 2009-10-30
473
            my ($key, $value, $type, $sth, $i) = @_;
update document
yuki-kimoto authored on 2009-10-27
474
            if ($type eq 'DATETIME') {
cleanup
yuki-kimoto authored on 2009-10-30
475
                return $dbi->filters->{string_to_datetime}->($value);
update document
yuki-kimoto authored on 2009-10-27
476
            }
477
            else {
cleanup
yuki-kimoto authored on 2009-10-30
478
                return $dbi->filters->{encode_utf8}->($value);
update document
yuki-kimoto authored on 2009-10-27
479
            }
480
        }
481
    );
482

            
483
add_filter add filter to filters
add tests
yuki-kimoto authored on 2009-10-18
484

            
cleanup
yuki-kimoto authored on 2009-10-29
485
=head2 create_query
486
    
487
    # Create Query object from SQL template
488
    my $query = $dbi->create_query($template);
489
    
490
=head2 execute
update document
yuki-kimoto authored on 2009-10-27
491

            
492
    # Parse SQL template and execute SQL
cleanup
yuki-kimoto authored on 2009-10-29
493
    $result = $dbi->query($query, $params);
494
    $result = $dbi->query($template, $params); # Shorcut
update document
yuki-kimoto authored on 2009-10-27
495
    
496
    # Sample
497
    $result = $dbi->query("select * from authors where {= name} && {= age}", 
498
                          {author => 'taro', age => 19});
499
    
500
    while (my @row = $result->fetch) {
501
        # do something
502
    }
503

            
504
See also L<DBI::Custom::SQL::Template>
505

            
cleanup
yuki-kimoto authored on 2009-10-22
506
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
507

            
update document
yuki-kimoto authored on 2009-10-27
508
    # Run tranzaction
509
    $dbi->run_tranzaction(sub {
510
        # do something
511
    });
first commit
yuki-kimoto authored on 2009-10-13
512

            
update document
yuki-kimoto authored on 2009-10-27
513
If tranzaction is success, commit is execute. 
514
If tranzation is died, rollback is execute.
first commit
yuki-kimoto authored on 2009-10-13
515

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

            
517

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

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

            
522
=head1 COPYRIGHT & LICENSE
523

            
524
Copyright 2009 Yuki Kimoto, all rights reserved.
525

            
526
This program is free software; you can redistribute it and/or modify it
527
under the same terms as Perl itself.
528

            
529

            
530
=cut
531

            
532
1; # End of DBI::Custom