DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
699 lines | 17.905kb
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;
cleanup
yuki-kimoto authored on 2009-10-29
8
use DBI::Custom::Query;
add tests
yuki-kimoto authored on 2009-10-31
9
use DBI::Custom::Result;
10
use DBI::Custom::SQL::Template;
11

            
add tests
yuki-kimoto authored on 2009-10-25
12

            
13
### Class-Object Accessors
update document
yuki-kimoto authored on 2009-10-27
14
sub user        : ClassObjectAttr { initialize => {clone => 'scalar'} }
15
sub password    : ClassObjectAttr { initialize => {clone => 'scalar'} }
16
sub data_source : 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
    
add tests
yuki-kimoto authored on 2009-10-31
69
    croak("Not yet connect to database") unless $self->dbh;
add various
yuki-kimoto authored on 2009-10-18
70
    
71
    if (@_) {
72
        $self->dbh->{AutoCommit} = $_[0];
73
        return $self;
74
    }
75
    return $self->dbh->{AutoCommit};
76
}
add test
yuki-kimoto authored on 2009-10-16
77

            
add various things
yuki-kimoto authored on 2009-10-17
78
# Connect
add some method
yuki-kimoto authored on 2009-10-14
79
sub connect {
80
    my $self = shift;
update document
yuki-kimoto authored on 2009-10-27
81
    my $data_source = $self->data_source;
82
    my $user        = $self->user;
83
    my $password    = $self->password;
cleanup
yuki-kimoto authored on 2009-10-29
84
    my $dbi_options  = $self->dbi_options;
add test
yuki-kimoto authored on 2009-10-16
85
    
add tests
yuki-kimoto authored on 2009-10-31
86
    my $dbh = eval{DBI->connect(
update document
yuki-kimoto authored on 2009-10-27
87
        $data_source,
88
        $user,
89
        $password,
add some method
yuki-kimoto authored on 2009-10-14
90
        {
91
            RaiseError => 1,
92
            PrintError => 0,
93
            AutoCommit => 1,
cleanup
yuki-kimoto authored on 2009-10-29
94
            %{$dbi_options || {} }
add some method
yuki-kimoto authored on 2009-10-14
95
        }
add tests
yuki-kimoto authored on 2009-10-31
96
    )};
97
    
98
    croak $@ if $@;
add some method
yuki-kimoto authored on 2009-10-14
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;
cleanup
yuki-kimoto authored on 2009-10-31
113
    return ref $self->{dbh} eq 'DBI::db';
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

            
cleanup
yuki-kimoto authored on 2009-10-31
132
# Prepare statement handle
add prepare
yuki-kimoto authored on 2009-10-31
133
sub prepare {
134
    my ($self, $sql) = @_;
cleanup
yuki-kimoto authored on 2009-10-31
135
    
136
    # Connect if not
add tests
yuki-kimoto authored on 2009-10-31
137
    $self->connect unless $self->connected;
add prepare
yuki-kimoto authored on 2009-10-31
138
    
cleanup
yuki-kimoto authored on 2009-10-31
139
    # Prepare
add prepare
yuki-kimoto authored on 2009-10-31
140
    my $sth = eval{$self->dbh->prepare($sql)};
add tests
yuki-kimoto authored on 2009-10-31
141
    
142
    # Error
143
    croak("$@<Your SQL>\n$sql") if $@;
144
    
add prepare
yuki-kimoto authored on 2009-10-31
145
    return $sth;
146
}
147

            
cleanup
yuki-kimoto authored on 2009-10-31
148
# Execute SQL directly
add prepare
yuki-kimoto authored on 2009-10-31
149
sub do{
150
    my ($self, $sql, @bind_values) = @_;
cleanup
yuki-kimoto authored on 2009-10-31
151
    
152
    # Connect if not
add tests
yuki-kimoto authored on 2009-10-31
153
    $self->connect unless $self->connected;
add prepare
yuki-kimoto authored on 2009-10-31
154
    
cleanup
yuki-kimoto authored on 2009-10-31
155
    # Do
add tests
yuki-kimoto authored on 2009-10-31
156
    my $ret_val = eval{$self->dbh->do($sql, @bind_values)};
157
    
158
    # Error
159
    if ($@) {
160
        my $error = $@;
161
        require Data::Dumper;
162
        
163
        my $bind_value_dump
164
          = Data::Dumper->Dump([\@bind_values], ['*bind_valuds']);
165
        
166
        croak("$error<Your SQL>\n$sql\n<Your bind values>\n$bind_value_dump\n");
167
    }
add prepare
yuki-kimoto authored on 2009-10-31
168
}
169

            
cleanup
yuki-kimoto authored on 2009-10-31
170
# Create query
add various thins
yuki-kimoto authored on 2009-10-29
171
sub create_query {
172
    my ($self, $template) = @_;
add test
yuki-kimoto authored on 2009-10-17
173
    
add various thins
yuki-kimoto authored on 2009-10-29
174
    # Create query from SQL template
add prepare
yuki-kimoto authored on 2009-10-31
175
    my $sql_template = $self->sql_template;
176
    my $query = eval{$sql_template->create_query($template)};
177
    croak($@) if $@;
add test
yuki-kimoto authored on 2009-10-17
178
    
add various thins
yuki-kimoto authored on 2009-10-29
179
    # Create Query object;
cleanup
yuki-kimoto authored on 2009-10-29
180
    $query = DBI::Custom::Query->new($query);
add various thins
yuki-kimoto authored on 2009-10-29
181
    
cleanup
yuki-kimoto authored on 2009-10-31
182
    # Connect if not
add tests
yuki-kimoto authored on 2009-10-31
183
    $self->connect unless $self->connected;
try varioud way
yuki-kimoto authored on 2009-10-17
184
    
add various thins
yuki-kimoto authored on 2009-10-29
185
    # Prepare statement handle
add tests
yuki-kimoto authored on 2009-10-31
186
    my $sth = $self->prepare($query->{sql});
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);
add tests
yuki-kimoto authored on 2009-10-31
196
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
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

            
cleanup
yuki-kimoto authored on 2009-10-31
206
# Execute query
add various thins
yuki-kimoto authored on 2009-10-29
207
sub execute {
208
    my ($self, $query, $params)  = @_;
add tests
yuki-kimoto authored on 2009-10-29
209
    $params ||= {};
try varioud way
yuki-kimoto authored on 2009-10-17
210
    
add tests
yuki-kimoto authored on 2009-10-31
211
    # First argument is SQL template
212
    if (!ref $query) {
213
        my $template = $query;
214
        $query = $self->create_query($template);
215
        my $query_edit_cb = $_[3];
216
        $query_edit_cb->($query) if ref $query_edit_cb eq 'CODE';
217
    }
218
    
add various thins
yuki-kimoto authored on 2009-10-29
219
    # Create bind value
220
    my $bind_values = $self->_build_bind_values($query, $params);
add tests
yuki-kimoto authored on 2009-10-18
221
    
cleanup
yuki-kimoto authored on 2009-10-18
222
    # Execute
cleanup
yuki-kimoto authored on 2009-10-29
223
    my $sth = $query->sth;
add tests
yuki-kimoto authored on 2009-10-31
224
    my $ret_val = eval{$sth->execute(@$bind_values)};
cleanup
yuki-kimoto authored on 2009-10-31
225
    
226
    # Execute error
add tests
yuki-kimoto authored on 2009-10-31
227
    if (my $execute_error = $@) {
add tests
yuki-kimoto authored on 2009-10-31
228
        require Data::Dumper;
229
        my $sql         = $query->{sql} || '';
230
        my $params_dump = Data::Dumper->Dump([$params], ['*params']);
231
        
add tests
yuki-kimoto authored on 2009-10-31
232
        croak("$execute_error<Your SQL>\n$sql\n<Your parameters>\n$params_dump");
add tests
yuki-kimoto authored on 2009-10-31
233
    }
add various things
yuki-kimoto authored on 2009-10-17
234
    
cleanup
yuki-kimoto authored on 2009-10-18
235
    # Return resultset if select statement is executed
add various things
yuki-kimoto authored on 2009-10-17
236
    if ($sth->{NUM_OF_FIELDS}) {
cleanup
yuki-kimoto authored on 2009-10-31
237
        
238
        # Get result class
add various things
yuki-kimoto authored on 2009-10-17
239
        my $result_class = $self->result_class;
cleanup
yuki-kimoto authored on 2009-10-31
240
        
241
        # Create result
add various
yuki-kimoto authored on 2009-10-18
242
        my $result = $result_class->new({
add no_bind_filters
yuki-kimoto authored on 2009-10-30
243
            sth              => $sth,
244
            fetch_filter     => $query->fetch_filter,
245
            no_fetch_filters => $query->no_fetch_filters
add various
yuki-kimoto authored on 2009-10-18
246
        });
add various things
yuki-kimoto authored on 2009-10-17
247
        return $result;
248
    }
add tests
yuki-kimoto authored on 2009-10-18
249
    return $ret_val;
add test
yuki-kimoto authored on 2009-10-17
250
}
251

            
cleanup
yuki-kimoto authored on 2009-10-31
252
# Build binding values
add various thins
yuki-kimoto authored on 2009-10-29
253
sub _build_bind_values {
254
    my ($self, $query, $params) = @_;
add no_bind_filters
yuki-kimoto authored on 2009-10-30
255
    my $key_infos           = $query->key_infos;
256
    my $bind_filter         = $query->bind_filter;
257
    my $no_bind_filters_map = $query->_no_bind_filters_map || {};
cleanup
yuki-kimoto authored on 2009-10-18
258
    
add various thins
yuki-kimoto authored on 2009-10-29
259
    # binding values
260
    my @bind_values;
cleanup
yuki-kimoto authored on 2009-10-18
261
    
add tests
yuki-kimoto authored on 2009-10-31
262
    # Create bind values
cleanup
yuki-kimoto authored on 2009-10-29
263
    foreach my $key_info (@$key_infos) {
cleanup
yuki-kimoto authored on 2009-10-31
264
        # Set variable
265
        my $access_keys  = $key_info->{access_keys};
cleanup
yuki-kimoto authored on 2009-10-29
266
        my $original_key = $key_info->{original_key} || '';
267
        my $table        = $key_info->{table}        || '';
268
        my $column       = $key_info->{column}       || '';
add various thins
yuki-kimoto authored on 2009-10-29
269
        
cleanup
yuki-kimoto authored on 2009-10-31
270
        # Key is found?
add tests
yuki-kimoto authored on 2009-10-31
271
        my $found;
cleanup
yuki-kimoto authored on 2009-10-31
272
        
273
        # Build bind values
add various thins
yuki-kimoto authored on 2009-10-29
274
        ACCESS_KEYS :
275
        foreach my $access_key (@$access_keys) {
cleanup
yuki-kimoto authored on 2009-10-31
276
            # Root parameter
add various thins
yuki-kimoto authored on 2009-10-29
277
            my $root_params = $params;
cleanup
yuki-kimoto authored on 2009-10-31
278
            
279
            # Search corresponding value
add various thins
yuki-kimoto authored on 2009-10-29
280
            for (my $i = 0; $i < @$access_key; $i++) {
cleanup
yuki-kimoto authored on 2009-10-31
281
                # Current key
282
                my $current_key = $access_key->[$i];
add various thins
yuki-kimoto authored on 2009-10-29
283
                
cleanup
yuki-kimoto authored on 2009-10-31
284
                # Last key
add various thins
yuki-kimoto authored on 2009-10-29
285
                if ($i == @$access_key - 1) {
cleanup
yuki-kimoto authored on 2009-10-31
286
                    # Key is array reference
287
                    if (ref $current_key eq 'ARRAY') {
288
                        # Filtering 
289
                        if ($bind_filter &&
290
                            !$no_bind_filters_map->{$original_key})
291
                        {
add no_bind_filters
yuki-kimoto authored on 2009-10-30
292
                            push @bind_values, 
cleanup
yuki-kimoto authored on 2009-10-31
293
                                 $bind_filter->($original_key, 
294
                                                $root_params->[$current_key->[0]],
cleanup
yuki-kimoto authored on 2009-10-30
295
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
296
                        }
cleanup
yuki-kimoto authored on 2009-10-31
297
                        # Not filtering
add various thins
yuki-kimoto authored on 2009-10-29
298
                        else {
cleanup
yuki-kimoto authored on 2009-10-31
299
                            push @bind_values,
300
                                 scalar $root_params->[$current_key->[0]];
add various thins
yuki-kimoto authored on 2009-10-29
301
                        }
302
                    }
cleanup
yuki-kimoto authored on 2009-10-31
303
                    # Key is string
add various thins
yuki-kimoto authored on 2009-10-29
304
                    else {
cleanup
yuki-kimoto authored on 2009-10-31
305
                        # Key is not found
306
                        next ACCESS_KEYS
307
                          unless exists $root_params->{$current_key};
308
                        
309
                        # Filtering
310
                        if ($bind_filter &&
311
                            !$no_bind_filters_map->{$original_key}) 
312
                        {
add no_bind_filters
yuki-kimoto authored on 2009-10-30
313
                            push @bind_values,
cleanup
yuki-kimoto authored on 2009-10-31
314
                                 $bind_filter->($original_key,
315
                                                $root_params->{$current_key}, 
cleanup
yuki-kimoto authored on 2009-10-30
316
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
317
                        }
cleanup
yuki-kimoto authored on 2009-10-31
318
                        # Not filtering
add various thins
yuki-kimoto authored on 2009-10-29
319
                        else {
cleanup
yuki-kimoto authored on 2009-10-31
320
                            push @bind_values,
321
                                 scalar $root_params->{$current_key};
add various thins
yuki-kimoto authored on 2009-10-29
322
                        }
323
                    }
cleanup
yuki-kimoto authored on 2009-10-31
324
                    
325
                    # Key is found
add tests
yuki-kimoto authored on 2009-10-31
326
                    $found = 1;
add various thins
yuki-kimoto authored on 2009-10-29
327
                }
cleanup
yuki-kimoto authored on 2009-10-31
328
                # First or middle key
add various thins
yuki-kimoto authored on 2009-10-29
329
                else {
cleanup
yuki-kimoto authored on 2009-10-31
330
                    # Key is array reference
331
                    if (ref $current_key eq 'ARRAY') {
332
                        # Go next key
333
                        $root_params = $root_params->[$current_key->[0]];
334
                    }
335
                    # Key is string
336
                    else {
337
                        # Not found
338
                        next ACCESS_KEYS
339
                          unless exists $root_params->{$current_key};
340
                        
341
                        # Go next key
342
                        $root_params = $root_params->{$current_key};
343
                    }
add various thins
yuki-kimoto authored on 2009-10-29
344
                }
345
            }
346
        }
add tests
yuki-kimoto authored on 2009-10-31
347
        
cleanup
yuki-kimoto authored on 2009-10-31
348
        # Key is not found
add tests
yuki-kimoto authored on 2009-10-31
349
        unless ($found) {
350
            require Data::Dumper;
add tests
yuki-kimoto authored on 2009-10-31
351
            my $key_info_dump  = Data::Dumper->Dump([$key_info], ['*key_info']);
add tests
yuki-kimoto authored on 2009-10-31
352
            my $params_dump    = Data::Dumper->Dump([$params], ['*params']);
add tests
yuki-kimoto authored on 2009-10-31
353
            croak("Corresponding key is not found in your parameters\n" . 
add tests
yuki-kimoto authored on 2009-10-31
354
                  "<Key information>\n$key_info_dump\n\n" .
355
                  "<Your parameters>\n$params_dump\n");
356
        }
update document
yuki-kimoto authored on 2009-10-27
357
    }
add tests
yuki-kimoto authored on 2009-10-31
358
    return \@bind_values;
add test
yuki-kimoto authored on 2009-10-17
359
}
360

            
add tests
yuki-kimoto authored on 2009-10-31
361
# Run tranzaction
362
sub run_tranzaction {
363
    my ($self, $tranzaction) = @_;
364
    
365
    # Check auto commit
366
    croak("AutoCommit must be true before tranzaction start")
367
      unless $self->_auto_commit;
368
    
369
    # Auto commit off
370
    $self->_auto_commit(0);
371
    
372
    # Run tranzaction
373
    eval {$tranzaction->()};
374
    
375
    # Tranzaction error
376
    my $tranzaction_error = $@;
377
    
378
    # Tranzaction is failed.
379
    if ($tranzaction_error) {
380
        # Rollback
381
        eval{$self->dbh->rollback};
382
        
383
        # Rollback error
384
        my $rollback_error = $@;
385
        
386
        # Auto commit on
387
        $self->_auto_commit(1);
388
        
389
        if ($rollback_error) {
390
            # Rollback is failed
391
            croak("${tranzaction_error}Rollback is failed : $rollback_error");
392
        }
393
        else {
394
            # Rollback is success
395
            croak("${tranzaction_error}Rollback is success");
396
        }
397
    }
398
    # Tranzaction is success
399
    else {
400
        # Commit
401
        eval{$self->dbh->commit};
402
        my $commit_error = $@;
403
        
404
        # Auto commit on
405
        $self->_auto_commit(1);
406
        
407
        # Commit is failed
408
        croak($commit_error) if $commit_error;
409
    }
410
}
add various thins
yuki-kimoto authored on 2009-10-29
411

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

            
first commit
yuki-kimoto authored on 2009-10-13
414
=head1 NAME
415

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

            
418
=head1 VERSION
419

            
add test
yuki-kimoto authored on 2009-10-16
420
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
421

            
cleanup
yuki-kimoto authored on 2009-10-31
422
=head1 CAUTION
423

            
424
This module is now experimental stage.
425

            
426
I want you to try this module
427
because I want this module stable, and not to damage your DB data by this module bug.
428

            
429
Please tell me bug if you find
first commit
yuki-kimoto authored on 2009-10-13
430

            
431
=head1 SYNOPSIS
432

            
add test
yuki-kimoto authored on 2009-10-16
433
  my $dbi = DBI::Custom->new;
add various thins
yuki-kimoto authored on 2009-10-29
434
  
435
  my $query = $dbi->create_query($template);
436
  $dbi->execute($query);
first commit
yuki-kimoto authored on 2009-10-13
437

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

            
update document
yuki-kimoto authored on 2009-10-27
440
=head2 user
441

            
442
    # Set and get database user name
443
    $self = $dbi->user($user);
444
    $user = $dbi->user;
445
    
446
    # Sample
447
    $dbi->user('taro');
448

            
449
=head2 password
450

            
451
    # Set and get database password
452
    $self     = $dbi->password($password);
453
    $password = $dbi->password;
454
    
455
    # Sample
456
    $dbi->password('lkj&le`@s');
457

            
458
=head2 data_source
459

            
460
    # Set and get database data source
461
    $self        = $dbi->data_source($data_soruce);
462
    $data_source = $dbi->data_source;
463
    
464
    # Sample(SQLite)
465
    $dbi->data_source(dbi:SQLite:dbname=$database);
466
    
467
    # Sample(MySQL);
468
    $dbi->data_source("dbi:mysql:dbname=$database");
469
    
470
    # Sample(PostgreSQL)
471
    $dbi->data_source("dbi:Pg:dbname=$database");
cleanup
yuki-kimoto authored on 2009-10-29
472
    
473
=head2 database
474

            
475
    # Set and get database name
476
    $self     = $dbi->database($database);
477
    $database = $dbi->database;
update document
yuki-kimoto authored on 2009-10-27
478

            
cleanup
yuki-kimoto authored on 2009-10-29
479
=head2 dbi_options
update document
yuki-kimoto authored on 2009-10-27
480

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

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

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

            
add prepare
yuki-kimoto authored on 2009-10-31
490
=head2 prepare
491

            
492
    $sth = $dbi->prepare($sql);
493

            
494
This method is same as DBI::prepare
495

            
496
=head2 do
497

            
498
    $dbi->do($sql, @bind_values);
499

            
500
This method is same as DBI::do
501

            
update document
yuki-kimoto authored on 2009-10-27
502
=head2 sql_template
503

            
504
    # Set and get SQL::Template object
505
    $self         = $dbi->sql_template($sql_template);
506
    $sql_template = $dbi->sql_template;
507
    
508
    # Sample
509
    $dbi->sql_template(DBI::Cutom::SQL::Template->new);
510

            
511
=head2 filters
512

            
513
    # Set and get filters
514
    $self    = $dbi->filters($filters);
515
    $filters = $dbi->filters;
first commit
yuki-kimoto authored on 2009-10-13
516

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

            
update document
yuki-kimoto authored on 2009-10-27
519
    # Set and get binding filter
520
    $self        = $dbi->bind_filter($bind_filter);
521
    $bind_filter = $dbi->bind_filter
first commit
yuki-kimoto authored on 2009-10-13
522

            
update document
yuki-kimoto authored on 2009-10-27
523
    # Sample
524
    $dbi->bind_filter($self->filters->{default_bind_filter});
525
    
first commit
yuki-kimoto authored on 2009-10-13
526

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

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

            
update document
yuki-kimoto authored on 2009-10-27
531
    # Set and get Fetch filter
532
    $self         = $dbi->fetch_filter($fetch_filter);
533
    $fetch_filter = $dbi->fetch_filter;
first commit
yuki-kimoto authored on 2009-10-13
534

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

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
538
=head2 no_bind_filters
539

            
540
    # Set and get no filter keys when binding
541
    $self            = $dbi->no_bind_filters($no_bind_filters);
542
    $no_bind_filters = $dbi->no_bind_filters;
543

            
544
=head2 no_fetch_filters
cleanup
yuki-kimoto authored on 2009-10-29
545

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
546
    # Set and get no filter keys when fetching
547
    $self             = $dbi->no_fetch_filters($no_fetch_filters);
548
    $no_fetch_filters = $dbi->no_fetch_filters;
cleanup
yuki-kimoto authored on 2009-10-29
549

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

            
update document
yuki-kimoto authored on 2009-10-27
552
    # Set and get resultset class
553
    $self         = $dbi->result_class($result_class);
554
    $result_class = $dbi->result_class;
555
    
556
    # Sample
557
    $dbi->result_class('DBI::Custom::Result');
add test
yuki-kimoto authored on 2009-10-17
558

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

            
update document
yuki-kimoto authored on 2009-10-27
561
    # Get database handle
562
    $dbh = $self->dbh;
add test
yuki-kimoto authored on 2009-10-17
563

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

            
update document
yuki-kimoto authored on 2009-10-27
566
=head2 connect
567

            
568
    # Connect to database
569
    $self = $dbi->connect;
570
    
571
    # Sample
572
    $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', 
573
                            data_soruce => "dbi:mysql:dbname=$database");
574
    $dbi->connect;
add tests
yuki-kimoto authored on 2009-10-18
575

            
576
=head2 disconnect
577

            
update document
yuki-kimoto authored on 2009-10-27
578
    # Disconnect database
579
    $dbi->disconnect;
580

            
581
If database is already disconnected, this method do noting.
582

            
add tests
yuki-kimoto authored on 2009-10-18
583
=head2 reconnect
584

            
update document
yuki-kimoto authored on 2009-10-27
585
    # Reconnect
586
    $dbi->reconnect;
587

            
588
=head2 connected
589

            
590
    # Check connected
591
    $dbi->connected
592

            
593
=head2 add_filter
594

            
595
    # Add filter (hash ref or hash can be recieve)
596
    $self = $dbi->add_filter({$filter_name => $filter, ...});
597
    $self = $dbi->add_filter($filetr_name => $filter, ...);
598
    
599
    # Sample
600
    $dbi->add_filter(
601
        decode_utf8 => sub {
cleanup
yuki-kimoto authored on 2009-10-30
602
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
603
            return Encode::decode('UTF-8', $value);
604
        },
605
        datetime_to_string => sub {
cleanup
yuki-kimoto authored on 2009-10-30
606
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
607
            return $value->strftime('%Y-%m-%d %H:%M:%S')
608
        },
609
        default_bind_filter => sub {
cleanup
yuki-kimoto authored on 2009-10-30
610
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
611
            if (ref $value eq 'Time::Piece') {
cleanup
yuki-kimoto authored on 2009-10-30
612
                return $dbi->filters->{datetime_to_string}->($value);
update document
yuki-kimoto authored on 2009-10-27
613
            }
614
            else {
cleanup
yuki-kimoto authored on 2009-10-30
615
                return $dbi->filters->{decode_utf8}->($value);
update document
yuki-kimoto authored on 2009-10-27
616
            }
617
        },
618
        
619
        encode_utf8 => sub {
cleanup
yuki-kimoto authored on 2009-10-30
620
            my ($key, $value) = @_;
update document
yuki-kimoto authored on 2009-10-27
621
            return Encode::encode('UTF-8', $value);
622
        },
623
        string_to_datetime => sub {
cleanup
yuki-kimoto authored on 2009-10-30
624
            my ($key, $value) = @_;
update document
yuki-kimoto authored on 2009-10-27
625
            return DateTime::Format::MySQL->parse_datetime($value);
626
        },
627
        default_fetch_filter => sub {
cleanup
yuki-kimoto authored on 2009-10-30
628
            my ($key, $value, $type, $sth, $i) = @_;
update document
yuki-kimoto authored on 2009-10-27
629
            if ($type eq 'DATETIME') {
cleanup
yuki-kimoto authored on 2009-10-30
630
                return $dbi->filters->{string_to_datetime}->($value);
update document
yuki-kimoto authored on 2009-10-27
631
            }
632
            else {
cleanup
yuki-kimoto authored on 2009-10-30
633
                return $dbi->filters->{encode_utf8}->($value);
update document
yuki-kimoto authored on 2009-10-27
634
            }
635
        }
636
    );
637

            
638
add_filter add filter to filters
add tests
yuki-kimoto authored on 2009-10-18
639

            
cleanup
yuki-kimoto authored on 2009-10-29
640
=head2 create_query
641
    
642
    # Create Query object from SQL template
643
    my $query = $dbi->create_query($template);
644
    
645
=head2 execute
update document
yuki-kimoto authored on 2009-10-27
646

            
647
    # Parse SQL template and execute SQL
cleanup
yuki-kimoto authored on 2009-10-29
648
    $result = $dbi->query($query, $params);
649
    $result = $dbi->query($template, $params); # Shorcut
update document
yuki-kimoto authored on 2009-10-27
650
    
651
    # Sample
652
    $result = $dbi->query("select * from authors where {= name} && {= age}", 
653
                          {author => 'taro', age => 19});
654
    
655
    while (my @row = $result->fetch) {
656
        # do something
657
    }
658

            
659
See also L<DBI::Custom::SQL::Template>
660

            
cleanup
yuki-kimoto authored on 2009-10-22
661
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
662

            
update document
yuki-kimoto authored on 2009-10-27
663
    # Run tranzaction
664
    $dbi->run_tranzaction(sub {
665
        # do something
666
    });
first commit
yuki-kimoto authored on 2009-10-13
667

            
update document
yuki-kimoto authored on 2009-10-27
668
If tranzaction is success, commit is execute. 
669
If tranzation is died, rollback is execute.
first commit
yuki-kimoto authored on 2009-10-13
670

            
add tests
yuki-kimoto authored on 2009-10-31
671
=head1 CAUTION
672

            
673
DBI::Custom have DIB object internal.
674
This module is work well in the following DBI condition.
675

            
676
    1. AutoCommit is true
677
    2. RaiseError is true
678

            
679
By default, Both AutoCommit and RaiseError is true.
680
You must not change these mode not to damage your data.
681

            
682
If you change these mode, 
683
you cannot get correct error message, 
684
or run_tranzaction may fail.
685

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

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
692
=head1 COPYRIGHT & LICENSE
693

            
694
Copyright 2009 Yuki Kimoto, all rights reserved.
695

            
696
This program is free software; you can redistribute it and/or modify it
697
under the same terms as Perl itself.
698

            
699
=cut