DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
590 lines | 14.901kb
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})};
add tests
yuki-kimoto authored on 2009-10-31
186
    if ($@) {
187
        my $sql = $query->{sql} || '';
188
        my $message = "<Created SQL>\n$sql\n";
189
        croak("$@$message");
190
    }
add tests
yuki-kimoto authored on 2009-10-18
191
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
192
    # Set statement handle
add various thins
yuki-kimoto authored on 2009-10-29
193
    $query->sth($sth);
add tests
yuki-kimoto authored on 2009-10-18
194
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
195
    # Set bind filter
196
    $query->bind_filter($self->bind_filter);
197
    
198
    # Set no filter keys when binding
199
    $query->no_bind_filters($self->no_bind_filters);
add tests
yuki-kimoto authored on 2009-10-31
200
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
201
    # Set fetch filter
202
    $query->fetch_filter($self->fetch_filter);
203
    
204
    # Set no filter keys when fetching
205
    $query->no_fetch_filters($self->no_fetch_filters);
206
    
add various thins
yuki-kimoto authored on 2009-10-29
207
    return $query;
208
}
209

            
210
sub execute {
211
    my ($self, $query, $params)  = @_;
add tests
yuki-kimoto authored on 2009-10-29
212
    $params ||= {};
try varioud way
yuki-kimoto authored on 2009-10-17
213
    
add tests
yuki-kimoto authored on 2009-10-31
214
    # First argument is SQL template
215
    if (!ref $query) {
216
        my $template = $query;
217
        $query = $self->create_query($template);
218
        my $query_edit_cb = $_[3];
219
        $query_edit_cb->($query) if ref $query_edit_cb eq 'CODE';
220
    }
221
    
add various thins
yuki-kimoto authored on 2009-10-29
222
    # Create bind value
223
    my $bind_values = $self->_build_bind_values($query, $params);
add tests
yuki-kimoto authored on 2009-10-18
224
    
cleanup
yuki-kimoto authored on 2009-10-18
225
    # Execute
cleanup
yuki-kimoto authored on 2009-10-29
226
    my $sth = $query->sth;
add tests
yuki-kimoto authored on 2009-10-31
227
    my $ret_val = eval{$sth->execute(@$bind_values)};
228
    croak($@) if $@;
add various things
yuki-kimoto authored on 2009-10-17
229
    
cleanup
yuki-kimoto authored on 2009-10-18
230
    # Return resultset if select statement is executed
add various things
yuki-kimoto authored on 2009-10-17
231
    if ($sth->{NUM_OF_FIELDS}) {
232
        my $result_class = $self->result_class;
add various
yuki-kimoto authored on 2009-10-18
233
        my $result = $result_class->new({
add no_bind_filters
yuki-kimoto authored on 2009-10-30
234
            sth              => $sth,
235
            fetch_filter     => $query->fetch_filter,
236
            no_fetch_filters => $query->no_fetch_filters
add various
yuki-kimoto authored on 2009-10-18
237
        });
add various things
yuki-kimoto authored on 2009-10-17
238
        return $result;
239
    }
add tests
yuki-kimoto authored on 2009-10-18
240
    return $ret_val;
add test
yuki-kimoto authored on 2009-10-17
241
}
242

            
add various thins
yuki-kimoto authored on 2009-10-29
243
sub _build_bind_values {
244
    my ($self, $query, $params) = @_;
cleanup
yuki-kimoto authored on 2009-10-29
245
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
246
    my $key_infos           = $query->key_infos;
247
    my $bind_filter         = $query->bind_filter;
248
    my $no_bind_filters_map = $query->_no_bind_filters_map || {};
cleanup
yuki-kimoto authored on 2009-10-18
249
    
add various thins
yuki-kimoto authored on 2009-10-29
250
    # binding values
251
    my @bind_values;
cleanup
yuki-kimoto authored on 2009-10-18
252
    
add tests
yuki-kimoto authored on 2009-10-31
253
    # Create bind values
cleanup
yuki-kimoto authored on 2009-10-29
254
    foreach my $key_info (@$key_infos) {
255
        my $filtering_key = $key_info->{key};
256
        my $access_keys = $key_info->{access_keys};
add various thins
yuki-kimoto authored on 2009-10-29
257
        
cleanup
yuki-kimoto authored on 2009-10-29
258
        my $original_key = $key_info->{original_key} || '';
259
        my $table        = $key_info->{table}        || '';
260
        my $column       = $key_info->{column}       || '';
add various thins
yuki-kimoto authored on 2009-10-29
261
        
add tests
yuki-kimoto authored on 2009-10-31
262
        my $found;
add various thins
yuki-kimoto authored on 2009-10-29
263
        ACCESS_KEYS :
264
        foreach my $access_key (@$access_keys) {
265
            my $root_params = $params;
266
            for (my $i = 0; $i < @$access_key; $i++) {
267
                my $key = $access_key->[$i];
268
                
269
                croak("'access_keys' each value must be string or array reference")
270
                  unless (ref $key eq 'ARRAY' || ($key && !ref $key));
271
                
272
                if ($i == @$access_key - 1) {
273
                    if (ref $key eq 'ARRAY') {
add no_bind_filters
yuki-kimoto authored on 2009-10-30
274
                        if ($bind_filter && !$no_bind_filters_map->{$original_key}) {
275
                            push @bind_values, 
cleanup
yuki-kimoto authored on 2009-10-30
276
                                 $bind_filter->($original_key, $root_params->[$key->[0]],
277
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
278
                        }
279
                        else {
280
                            push @bind_values, scalar $root_params->[$key->[0]];
281
                        }
282
                    }
283
                    else {
284
                        next ACCESS_KEYS unless exists $root_params->{$key};
add no_bind_filters
yuki-kimoto authored on 2009-10-30
285
                        if ($bind_filter && !$no_bind_filters_map->{$original_key}) {
286
                            push @bind_values,
cleanup
yuki-kimoto authored on 2009-10-30
287
                                 $bind_filter->($original_key, $root_params->{$key}, 
288
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
289
                        }
290
                        else {
291
                            push @bind_values, scalar $root_params->{$key};
292
                        }
293
                    }
add tests
yuki-kimoto authored on 2009-10-31
294
                    $found = 1;
add various thins
yuki-kimoto authored on 2009-10-29
295
                }
296
                
297
                if ($key eq 'ARRAY') {
298
                    $root_params = $root_params->[$key->[0]];
299
                }
300
                else {
301
                    next ACCESS_KEYS unless exists $root_params->{$key};
302
                    $root_params = $root_params->{$key};
303
                }
304
            }
305
        }
add tests
yuki-kimoto authored on 2009-10-31
306
        
307
        unless ($found) {
308
            require Data::Dumper;
add tests
yuki-kimoto authored on 2009-10-31
309
            my $key_info_dump  = Data::Dumper->Dump([$key_info], ['*key_info']);
add tests
yuki-kimoto authored on 2009-10-31
310
            my $params_dump    = Data::Dumper->Dump([$params], ['*params']);
add tests
yuki-kimoto authored on 2009-10-31
311
            croak("Key not found in your parameters\n" . 
add tests
yuki-kimoto authored on 2009-10-31
312
                  "<Key information>\n$key_info_dump\n\n" .
313
                  "<Your parameters>\n$params_dump\n");
314
        }
update document
yuki-kimoto authored on 2009-10-27
315
    }
add tests
yuki-kimoto authored on 2009-10-31
316
    return \@bind_values;
add test
yuki-kimoto authored on 2009-10-17
317
}
318

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
322
=head1 NAME
323

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

            
326
=head1 VERSION
327

            
add test
yuki-kimoto authored on 2009-10-16
328
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
329

            
330
=cut
331

            
332
=head1 SYNOPSIS
333

            
add test
yuki-kimoto authored on 2009-10-16
334
  my $dbi = DBI::Custom->new;
add various thins
yuki-kimoto authored on 2009-10-29
335
  
336
  my $query = $dbi->create_query($template);
337
  $dbi->execute($query);
first commit
yuki-kimoto authored on 2009-10-13
338

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

            
update document
yuki-kimoto authored on 2009-10-27
341
=head2 user
342

            
343
    # Set and get database user name
344
    $self = $dbi->user($user);
345
    $user = $dbi->user;
346
    
347
    # Sample
348
    $dbi->user('taro');
349

            
350
=head2 password
351

            
352
    # Set and get database password
353
    $self     = $dbi->password($password);
354
    $password = $dbi->password;
355
    
356
    # Sample
357
    $dbi->password('lkj&le`@s');
358

            
359
=head2 data_source
360

            
361
    # Set and get database data source
362
    $self        = $dbi->data_source($data_soruce);
363
    $data_source = $dbi->data_source;
364
    
365
    # Sample(SQLite)
366
    $dbi->data_source(dbi:SQLite:dbname=$database);
367
    
368
    # Sample(MySQL);
369
    $dbi->data_source("dbi:mysql:dbname=$database");
370
    
371
    # Sample(PostgreSQL)
372
    $dbi->data_source("dbi:Pg:dbname=$database");
cleanup
yuki-kimoto authored on 2009-10-29
373
    
374
=head2 database
375

            
376
    # Set and get database name
377
    $self     = $dbi->database($database);
378
    $database = $dbi->database;
update document
yuki-kimoto authored on 2009-10-27
379

            
cleanup
yuki-kimoto authored on 2009-10-29
380
=head2 dbi_options
update document
yuki-kimoto authored on 2009-10-27
381

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

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

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

            
add prepare
yuki-kimoto authored on 2009-10-31
391
=head2 prepare
392

            
393
    $sth = $dbi->prepare($sql);
394

            
395
This method is same as DBI::prepare
396

            
397
=head2 do
398

            
399
    $dbi->do($sql, @bind_values);
400

            
401
This method is same as DBI::do
402

            
update document
yuki-kimoto authored on 2009-10-27
403
=head2 sql_template
404

            
405
    # Set and get SQL::Template object
406
    $self         = $dbi->sql_template($sql_template);
407
    $sql_template = $dbi->sql_template;
408
    
409
    # Sample
410
    $dbi->sql_template(DBI::Cutom::SQL::Template->new);
411

            
412
=head2 filters
413

            
414
    # Set and get filters
415
    $self    = $dbi->filters($filters);
416
    $filters = $dbi->filters;
first commit
yuki-kimoto authored on 2009-10-13
417

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

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

            
update document
yuki-kimoto authored on 2009-10-27
424
    # Sample
425
    $dbi->bind_filter($self->filters->{default_bind_filter});
426
    
first commit
yuki-kimoto authored on 2009-10-13
427

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

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

            
update document
yuki-kimoto authored on 2009-10-27
432
    # Set and get Fetch filter
433
    $self         = $dbi->fetch_filter($fetch_filter);
434
    $fetch_filter = $dbi->fetch_filter;
first commit
yuki-kimoto authored on 2009-10-13
435

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

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
439
=head2 no_bind_filters
440

            
441
    # Set and get no filter keys when binding
442
    $self            = $dbi->no_bind_filters($no_bind_filters);
443
    $no_bind_filters = $dbi->no_bind_filters;
444

            
445
=head2 no_fetch_filters
cleanup
yuki-kimoto authored on 2009-10-29
446

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
447
    # Set and get no filter keys when fetching
448
    $self             = $dbi->no_fetch_filters($no_fetch_filters);
449
    $no_fetch_filters = $dbi->no_fetch_filters;
cleanup
yuki-kimoto authored on 2009-10-29
450

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

            
update document
yuki-kimoto authored on 2009-10-27
453
    # Set and get resultset class
454
    $self         = $dbi->result_class($result_class);
455
    $result_class = $dbi->result_class;
456
    
457
    # Sample
458
    $dbi->result_class('DBI::Custom::Result');
add test
yuki-kimoto authored on 2009-10-17
459

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

            
update document
yuki-kimoto authored on 2009-10-27
462
    # Get database handle
463
    $dbh = $self->dbh;
add test
yuki-kimoto authored on 2009-10-17
464

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

            
update document
yuki-kimoto authored on 2009-10-27
467
=head2 connect
468

            
469
    # Connect to database
470
    $self = $dbi->connect;
471
    
472
    # Sample
473
    $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', 
474
                            data_soruce => "dbi:mysql:dbname=$database");
475
    $dbi->connect;
add tests
yuki-kimoto authored on 2009-10-18
476

            
477
=head2 disconnect
478

            
update document
yuki-kimoto authored on 2009-10-27
479
    # Disconnect database
480
    $dbi->disconnect;
481

            
482
If database is already disconnected, this method do noting.
483

            
add tests
yuki-kimoto authored on 2009-10-18
484
=head2 reconnect
485

            
update document
yuki-kimoto authored on 2009-10-27
486
    # Reconnect
487
    $dbi->reconnect;
488

            
489
=head2 connected
490

            
491
    # Check connected
492
    $dbi->connected
493

            
494
=head2 add_filter
495

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

            
539
add_filter add filter to filters
add tests
yuki-kimoto authored on 2009-10-18
540

            
cleanup
yuki-kimoto authored on 2009-10-29
541
=head2 create_query
542
    
543
    # Create Query object from SQL template
544
    my $query = $dbi->create_query($template);
545
    
546
=head2 execute
update document
yuki-kimoto authored on 2009-10-27
547

            
548
    # Parse SQL template and execute SQL
cleanup
yuki-kimoto authored on 2009-10-29
549
    $result = $dbi->query($query, $params);
550
    $result = $dbi->query($template, $params); # Shorcut
update document
yuki-kimoto authored on 2009-10-27
551
    
552
    # Sample
553
    $result = $dbi->query("select * from authors where {= name} && {= age}", 
554
                          {author => 'taro', age => 19});
555
    
556
    while (my @row = $result->fetch) {
557
        # do something
558
    }
559

            
560
See also L<DBI::Custom::SQL::Template>
561

            
cleanup
yuki-kimoto authored on 2009-10-22
562
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
563

            
update document
yuki-kimoto authored on 2009-10-27
564
    # Run tranzaction
565
    $dbi->run_tranzaction(sub {
566
        # do something
567
    });
first commit
yuki-kimoto authored on 2009-10-13
568

            
update document
yuki-kimoto authored on 2009-10-27
569
If tranzaction is success, commit is execute. 
570
If tranzation is died, rollback is execute.
first commit
yuki-kimoto authored on 2009-10-13
571

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

            
573

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

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
580
=head1 COPYRIGHT & LICENSE
581

            
582
Copyright 2009 Yuki Kimoto, all rights reserved.
583

            
584
This program is free software; you can redistribute it and/or modify it
585
under the same terms as Perl itself.
586

            
587

            
588
=cut
589

            
590
1; # End of DBI::Custom