DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
578 lines | 14.513kb
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'} }
cleanup
yuki-kimoto authored on 2009-10-29
17
sub dbi_options : ClassObjectAttr { initialize => {clone => 'hash', 
add tests
yuki-kimoto authored on 2009-10-31
18
                                                   default => sub { {} } } }
catch up Object::Simple upda...
yuki-kimoto authored on 2009-10-26
19

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

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

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

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

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

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

            
52

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

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

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

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

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

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

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

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

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

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

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

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

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

            
206
sub execute {
207
    my ($self, $query, $params)  = @_;
add tests
yuki-kimoto authored on 2009-10-29
208
    $params ||= {};
try varioud way
yuki-kimoto authored on 2009-10-17
209
    
add various thins
yuki-kimoto authored on 2009-10-29
210
    # Create bind value
211
    my $bind_values = $self->_build_bind_values($query, $params);
add tests
yuki-kimoto authored on 2009-10-18
212
    
cleanup
yuki-kimoto authored on 2009-10-18
213
    # Execute
cleanup
yuki-kimoto authored on 2009-10-29
214
    my $sth = $query->sth;
add tests
yuki-kimoto authored on 2009-10-31
215
    my $ret_val = eval{$sth->execute(@$bind_values)};
216
    croak($@) if $@;
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 tests
yuki-kimoto authored on 2009-10-31
241
    # Create 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
        
add tests
yuki-kimoto authored on 2009-10-31
250
        my $found;
add various thins
yuki-kimoto authored on 2009-10-29
251
        ACCESS_KEYS :
252
        foreach my $access_key (@$access_keys) {
253
            my $root_params = $params;
254
            for (my $i = 0; $i < @$access_key; $i++) {
255
                my $key = $access_key->[$i];
256
                
257
                croak("'access_keys' each value must be string or array reference")
258
                  unless (ref $key eq 'ARRAY' || ($key && !ref $key));
259
                
260
                if ($i == @$access_key - 1) {
261
                    if (ref $key eq 'ARRAY') {
add no_bind_filters
yuki-kimoto authored on 2009-10-30
262
                        if ($bind_filter && !$no_bind_filters_map->{$original_key}) {
263
                            push @bind_values, 
cleanup
yuki-kimoto authored on 2009-10-30
264
                                 $bind_filter->($original_key, $root_params->[$key->[0]],
265
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
266
                        }
267
                        else {
268
                            push @bind_values, scalar $root_params->[$key->[0]];
269
                        }
270
                    }
271
                    else {
272
                        next ACCESS_KEYS unless exists $root_params->{$key};
add no_bind_filters
yuki-kimoto authored on 2009-10-30
273
                        if ($bind_filter && !$no_bind_filters_map->{$original_key}) {
274
                            push @bind_values,
cleanup
yuki-kimoto authored on 2009-10-30
275
                                 $bind_filter->($original_key, $root_params->{$key}, 
276
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
277
                        }
278
                        else {
279
                            push @bind_values, scalar $root_params->{$key};
280
                        }
281
                    }
add tests
yuki-kimoto authored on 2009-10-31
282
                    $found = 1;
add various thins
yuki-kimoto authored on 2009-10-29
283
                }
284
                
285
                if ($key eq 'ARRAY') {
286
                    $root_params = $root_params->[$key->[0]];
287
                }
288
                else {
289
                    next ACCESS_KEYS unless exists $root_params->{$key};
290
                    $root_params = $root_params->{$key};
291
                }
292
            }
293
        }
add tests
yuki-kimoto authored on 2009-10-31
294
        
295
        unless ($found) {
296
            require Data::Dumper;
297
            my $key_info_dump = Data::Dumper->Dump([$key_info], ['*key_info']);
298
            my $params_dump    = Data::Dumper->Dump([$params], ['*params']);
299
            croak("Key not found\n\n" . 
300
                  "<Key information>\n$key_info_dump\n\n" .
301
                  "<Your parameters>\n$params_dump\n");
302
        }
update document
yuki-kimoto authored on 2009-10-27
303
    }
add tests
yuki-kimoto authored on 2009-10-31
304
    return \@bind_values;
add test
yuki-kimoto authored on 2009-10-17
305
}
306

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
310
=head1 NAME
311

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

            
314
=head1 VERSION
315

            
add test
yuki-kimoto authored on 2009-10-16
316
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
317

            
318
=cut
319

            
320
=head1 SYNOPSIS
321

            
add test
yuki-kimoto authored on 2009-10-16
322
  my $dbi = DBI::Custom->new;
add various thins
yuki-kimoto authored on 2009-10-29
323
  
324
  my $query = $dbi->create_query($template);
325
  $dbi->execute($query);
first commit
yuki-kimoto authored on 2009-10-13
326

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

            
update document
yuki-kimoto authored on 2009-10-27
329
=head2 user
330

            
331
    # Set and get database user name
332
    $self = $dbi->user($user);
333
    $user = $dbi->user;
334
    
335
    # Sample
336
    $dbi->user('taro');
337

            
338
=head2 password
339

            
340
    # Set and get database password
341
    $self     = $dbi->password($password);
342
    $password = $dbi->password;
343
    
344
    # Sample
345
    $dbi->password('lkj&le`@s');
346

            
347
=head2 data_source
348

            
349
    # Set and get database data source
350
    $self        = $dbi->data_source($data_soruce);
351
    $data_source = $dbi->data_source;
352
    
353
    # Sample(SQLite)
354
    $dbi->data_source(dbi:SQLite:dbname=$database);
355
    
356
    # Sample(MySQL);
357
    $dbi->data_source("dbi:mysql:dbname=$database");
358
    
359
    # Sample(PostgreSQL)
360
    $dbi->data_source("dbi:Pg:dbname=$database");
cleanup
yuki-kimoto authored on 2009-10-29
361
    
362
=head2 database
363

            
364
    # Set and get database name
365
    $self     = $dbi->database($database);
366
    $database = $dbi->database;
update document
yuki-kimoto authored on 2009-10-27
367

            
cleanup
yuki-kimoto authored on 2009-10-29
368
=head2 dbi_options
update document
yuki-kimoto authored on 2009-10-27
369

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

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

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

            
add prepare
yuki-kimoto authored on 2009-10-31
379
=head2 prepare
380

            
381
    $sth = $dbi->prepare($sql);
382

            
383
This method is same as DBI::prepare
384

            
385
=head2 do
386

            
387
    $dbi->do($sql, @bind_values);
388

            
389
This method is same as DBI::do
390

            
update document
yuki-kimoto authored on 2009-10-27
391
=head2 sql_template
392

            
393
    # Set and get SQL::Template object
394
    $self         = $dbi->sql_template($sql_template);
395
    $sql_template = $dbi->sql_template;
396
    
397
    # Sample
398
    $dbi->sql_template(DBI::Cutom::SQL::Template->new);
399

            
400
=head2 filters
401

            
402
    # Set and get filters
403
    $self    = $dbi->filters($filters);
404
    $filters = $dbi->filters;
first commit
yuki-kimoto authored on 2009-10-13
405

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

            
update document
yuki-kimoto authored on 2009-10-27
408
    # Set and get binding filter
409
    $self        = $dbi->bind_filter($bind_filter);
410
    $bind_filter = $dbi->bind_filter
first commit
yuki-kimoto authored on 2009-10-13
411

            
update document
yuki-kimoto authored on 2009-10-27
412
    # Sample
413
    $dbi->bind_filter($self->filters->{default_bind_filter});
414
    
first commit
yuki-kimoto authored on 2009-10-13
415

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

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

            
update document
yuki-kimoto authored on 2009-10-27
420
    # Set and get Fetch filter
421
    $self         = $dbi->fetch_filter($fetch_filter);
422
    $fetch_filter = $dbi->fetch_filter;
first commit
yuki-kimoto authored on 2009-10-13
423

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

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
427
=head2 no_bind_filters
428

            
429
    # Set and get no filter keys when binding
430
    $self            = $dbi->no_bind_filters($no_bind_filters);
431
    $no_bind_filters = $dbi->no_bind_filters;
432

            
433
=head2 no_fetch_filters
cleanup
yuki-kimoto authored on 2009-10-29
434

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
435
    # Set and get no filter keys when fetching
436
    $self             = $dbi->no_fetch_filters($no_fetch_filters);
437
    $no_fetch_filters = $dbi->no_fetch_filters;
cleanup
yuki-kimoto authored on 2009-10-29
438

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

            
update document
yuki-kimoto authored on 2009-10-27
441
    # Set and get resultset class
442
    $self         = $dbi->result_class($result_class);
443
    $result_class = $dbi->result_class;
444
    
445
    # Sample
446
    $dbi->result_class('DBI::Custom::Result');
add test
yuki-kimoto authored on 2009-10-17
447

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

            
update document
yuki-kimoto authored on 2009-10-27
450
    # Get database handle
451
    $dbh = $self->dbh;
add test
yuki-kimoto authored on 2009-10-17
452

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

            
update document
yuki-kimoto authored on 2009-10-27
455
=head2 connect
456

            
457
    # Connect to database
458
    $self = $dbi->connect;
459
    
460
    # Sample
461
    $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', 
462
                            data_soruce => "dbi:mysql:dbname=$database");
463
    $dbi->connect;
add tests
yuki-kimoto authored on 2009-10-18
464

            
465
=head2 disconnect
466

            
update document
yuki-kimoto authored on 2009-10-27
467
    # Disconnect database
468
    $dbi->disconnect;
469

            
470
If database is already disconnected, this method do noting.
471

            
add tests
yuki-kimoto authored on 2009-10-18
472
=head2 reconnect
473

            
update document
yuki-kimoto authored on 2009-10-27
474
    # Reconnect
475
    $dbi->reconnect;
476

            
477
=head2 connected
478

            
479
    # Check connected
480
    $dbi->connected
481

            
482
=head2 add_filter
483

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

            
527
add_filter add filter to filters
add tests
yuki-kimoto authored on 2009-10-18
528

            
cleanup
yuki-kimoto authored on 2009-10-29
529
=head2 create_query
530
    
531
    # Create Query object from SQL template
532
    my $query = $dbi->create_query($template);
533
    
534
=head2 execute
update document
yuki-kimoto authored on 2009-10-27
535

            
536
    # Parse SQL template and execute SQL
cleanup
yuki-kimoto authored on 2009-10-29
537
    $result = $dbi->query($query, $params);
538
    $result = $dbi->query($template, $params); # Shorcut
update document
yuki-kimoto authored on 2009-10-27
539
    
540
    # Sample
541
    $result = $dbi->query("select * from authors where {= name} && {= age}", 
542
                          {author => 'taro', age => 19});
543
    
544
    while (my @row = $result->fetch) {
545
        # do something
546
    }
547

            
548
See also L<DBI::Custom::SQL::Template>
549

            
cleanup
yuki-kimoto authored on 2009-10-22
550
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
551

            
update document
yuki-kimoto authored on 2009-10-27
552
    # Run tranzaction
553
    $dbi->run_tranzaction(sub {
554
        # do something
555
    });
first commit
yuki-kimoto authored on 2009-10-13
556

            
update document
yuki-kimoto authored on 2009-10-27
557
If tranzaction is success, commit is execute. 
558
If tranzation is died, rollback is execute.
first commit
yuki-kimoto authored on 2009-10-13
559

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

            
561

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

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

            
add tests
yuki-kimoto authored on 2009-10-31
566
Github L<http://github.com/yuki-kimoto>
567

            
first commit
yuki-kimoto authored on 2009-10-13
568
=head1 COPYRIGHT & LICENSE
569

            
570
Copyright 2009 Yuki Kimoto, all rights reserved.
571

            
572
This program is free software; you can redistribute it and/or modify it
573
under the same terms as Perl itself.
574

            
575

            
576
=cut
577

            
578
1; # End of DBI::Custom