DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
597 lines | 15.165kb
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)};
add tests
yuki-kimoto authored on 2009-10-31
228
    if ($@) {
229
        require Data::Dumper;
230
        my $sql         = $query->{sql} || '';
231
        my $params_dump = Data::Dumper->Dump([$params], ['*params']);
232
        
233
        my $message = "<Created SQL>\n$sql\n<Your parameters>$params_dump";
234
        croak("$@$message");
235
    }
add various things
yuki-kimoto authored on 2009-10-17
236
    
cleanup
yuki-kimoto authored on 2009-10-18
237
    # Return resultset if select statement is executed
add various things
yuki-kimoto authored on 2009-10-17
238
    if ($sth->{NUM_OF_FIELDS}) {
239
        my $result_class = $self->result_class;
add various
yuki-kimoto authored on 2009-10-18
240
        my $result = $result_class->new({
add no_bind_filters
yuki-kimoto authored on 2009-10-30
241
            sth              => $sth,
242
            fetch_filter     => $query->fetch_filter,
243
            no_fetch_filters => $query->no_fetch_filters
add various
yuki-kimoto authored on 2009-10-18
244
        });
add various things
yuki-kimoto authored on 2009-10-17
245
        return $result;
246
    }
add tests
yuki-kimoto authored on 2009-10-18
247
    return $ret_val;
add test
yuki-kimoto authored on 2009-10-17
248
}
249

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

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
329
=head1 NAME
330

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

            
333
=head1 VERSION
334

            
add test
yuki-kimoto authored on 2009-10-16
335
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
336

            
337
=cut
338

            
339
=head1 SYNOPSIS
340

            
add test
yuki-kimoto authored on 2009-10-16
341
  my $dbi = DBI::Custom->new;
add various thins
yuki-kimoto authored on 2009-10-29
342
  
343
  my $query = $dbi->create_query($template);
344
  $dbi->execute($query);
first commit
yuki-kimoto authored on 2009-10-13
345

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

            
update document
yuki-kimoto authored on 2009-10-27
348
=head2 user
349

            
350
    # Set and get database user name
351
    $self = $dbi->user($user);
352
    $user = $dbi->user;
353
    
354
    # Sample
355
    $dbi->user('taro');
356

            
357
=head2 password
358

            
359
    # Set and get database password
360
    $self     = $dbi->password($password);
361
    $password = $dbi->password;
362
    
363
    # Sample
364
    $dbi->password('lkj&le`@s');
365

            
366
=head2 data_source
367

            
368
    # Set and get database data source
369
    $self        = $dbi->data_source($data_soruce);
370
    $data_source = $dbi->data_source;
371
    
372
    # Sample(SQLite)
373
    $dbi->data_source(dbi:SQLite:dbname=$database);
374
    
375
    # Sample(MySQL);
376
    $dbi->data_source("dbi:mysql:dbname=$database");
377
    
378
    # Sample(PostgreSQL)
379
    $dbi->data_source("dbi:Pg:dbname=$database");
cleanup
yuki-kimoto authored on 2009-10-29
380
    
381
=head2 database
382

            
383
    # Set and get database name
384
    $self     = $dbi->database($database);
385
    $database = $dbi->database;
update document
yuki-kimoto authored on 2009-10-27
386

            
cleanup
yuki-kimoto authored on 2009-10-29
387
=head2 dbi_options
update document
yuki-kimoto authored on 2009-10-27
388

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

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

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

            
add prepare
yuki-kimoto authored on 2009-10-31
398
=head2 prepare
399

            
400
    $sth = $dbi->prepare($sql);
401

            
402
This method is same as DBI::prepare
403

            
404
=head2 do
405

            
406
    $dbi->do($sql, @bind_values);
407

            
408
This method is same as DBI::do
409

            
update document
yuki-kimoto authored on 2009-10-27
410
=head2 sql_template
411

            
412
    # Set and get SQL::Template object
413
    $self         = $dbi->sql_template($sql_template);
414
    $sql_template = $dbi->sql_template;
415
    
416
    # Sample
417
    $dbi->sql_template(DBI::Cutom::SQL::Template->new);
418

            
419
=head2 filters
420

            
421
    # Set and get filters
422
    $self    = $dbi->filters($filters);
423
    $filters = $dbi->filters;
first commit
yuki-kimoto authored on 2009-10-13
424

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

            
update document
yuki-kimoto authored on 2009-10-27
427
    # Set and get binding filter
428
    $self        = $dbi->bind_filter($bind_filter);
429
    $bind_filter = $dbi->bind_filter
first commit
yuki-kimoto authored on 2009-10-13
430

            
update document
yuki-kimoto authored on 2009-10-27
431
    # Sample
432
    $dbi->bind_filter($self->filters->{default_bind_filter});
433
    
first commit
yuki-kimoto authored on 2009-10-13
434

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

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

            
update document
yuki-kimoto authored on 2009-10-27
439
    # Set and get Fetch filter
440
    $self         = $dbi->fetch_filter($fetch_filter);
441
    $fetch_filter = $dbi->fetch_filter;
first commit
yuki-kimoto authored on 2009-10-13
442

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

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
446
=head2 no_bind_filters
447

            
448
    # Set and get no filter keys when binding
449
    $self            = $dbi->no_bind_filters($no_bind_filters);
450
    $no_bind_filters = $dbi->no_bind_filters;
451

            
452
=head2 no_fetch_filters
cleanup
yuki-kimoto authored on 2009-10-29
453

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
454
    # Set and get no filter keys when fetching
455
    $self             = $dbi->no_fetch_filters($no_fetch_filters);
456
    $no_fetch_filters = $dbi->no_fetch_filters;
cleanup
yuki-kimoto authored on 2009-10-29
457

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

            
update document
yuki-kimoto authored on 2009-10-27
460
    # Set and get resultset class
461
    $self         = $dbi->result_class($result_class);
462
    $result_class = $dbi->result_class;
463
    
464
    # Sample
465
    $dbi->result_class('DBI::Custom::Result');
add test
yuki-kimoto authored on 2009-10-17
466

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

            
update document
yuki-kimoto authored on 2009-10-27
469
    # Get database handle
470
    $dbh = $self->dbh;
add test
yuki-kimoto authored on 2009-10-17
471

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

            
update document
yuki-kimoto authored on 2009-10-27
474
=head2 connect
475

            
476
    # Connect to database
477
    $self = $dbi->connect;
478
    
479
    # Sample
480
    $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', 
481
                            data_soruce => "dbi:mysql:dbname=$database");
482
    $dbi->connect;
add tests
yuki-kimoto authored on 2009-10-18
483

            
484
=head2 disconnect
485

            
update document
yuki-kimoto authored on 2009-10-27
486
    # Disconnect database
487
    $dbi->disconnect;
488

            
489
If database is already disconnected, this method do noting.
490

            
add tests
yuki-kimoto authored on 2009-10-18
491
=head2 reconnect
492

            
update document
yuki-kimoto authored on 2009-10-27
493
    # Reconnect
494
    $dbi->reconnect;
495

            
496
=head2 connected
497

            
498
    # Check connected
499
    $dbi->connected
500

            
501
=head2 add_filter
502

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

            
546
add_filter add filter to filters
add tests
yuki-kimoto authored on 2009-10-18
547

            
cleanup
yuki-kimoto authored on 2009-10-29
548
=head2 create_query
549
    
550
    # Create Query object from SQL template
551
    my $query = $dbi->create_query($template);
552
    
553
=head2 execute
update document
yuki-kimoto authored on 2009-10-27
554

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

            
567
See also L<DBI::Custom::SQL::Template>
568

            
cleanup
yuki-kimoto authored on 2009-10-22
569
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
570

            
update document
yuki-kimoto authored on 2009-10-27
571
    # Run tranzaction
572
    $dbi->run_tranzaction(sub {
573
        # do something
574
    });
first commit
yuki-kimoto authored on 2009-10-13
575

            
update document
yuki-kimoto authored on 2009-10-27
576
If tranzaction is success, commit is execute. 
577
If tranzation is died, rollback is execute.
first commit
yuki-kimoto authored on 2009-10-13
578

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

            
580

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

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
587
=head1 COPYRIGHT & LICENSE
588

            
589
Copyright 2009 Yuki Kimoto, all rights reserved.
590

            
591
This program is free software; you can redistribute it and/or modify it
592
under the same terms as Perl itself.
593

            
594

            
595
=cut
596

            
597
1; # End of DBI::Custom