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

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

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

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

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

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

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

            
51

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

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

            
64
# Auto commit
update document
yuki-kimoto authored on 2009-10-27
65
sub _auto_commit {
add various
yuki-kimoto authored on 2009-10-18
66
    my $self = shift;
67
    
68
    croak("Cannot change AutoCommit becouse of not connected")
cleanup
yuki-kimoto authored on 2009-10-31
69
      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 some method
yuki-kimoto authored on 2009-10-14
86
    my $dbh = 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
        }
96
    );
97
    
98
    $self->dbh($dbh);
add various
yuki-kimoto authored on 2009-10-18
99
    return $self;
add some method
yuki-kimoto authored on 2009-10-14
100
}
first commit
yuki-kimoto authored on 2009-10-13
101

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

            
add various things
yuki-kimoto authored on 2009-10-17
108
# Is connected?
109
sub connected {
110
    my $self = shift;
cleanup
yuki-kimoto authored on 2009-10-31
111
    return ref $self->{dbh} eq 'DBI::db';
add various things
yuki-kimoto authored on 2009-10-17
112
}
113

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

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

            
try various
yuki-kimoto authored on 2009-10-21
130
# Run tranzaction
131
sub run_tranzaction {
132
    my ($self, $tranzaction) = @_;
133
    
cleanup
yuki-kimoto authored on 2009-10-31
134
    # Auto commit off
update document
yuki-kimoto authored on 2009-10-27
135
    $self->_auto_commit(0);
try various
yuki-kimoto authored on 2009-10-21
136
    
cleanup
yuki-kimoto authored on 2009-10-31
137
    # Run tranzaction
138
    eval {$tranzaction->()};
try various
yuki-kimoto authored on 2009-10-21
139
    
cleanup
yuki-kimoto authored on 2009-10-31
140
    # Tranzaction error
141
    my $tranzaction_error = $@;
142
    
143
    # RaiseError on
144
    my $old_raise_error = $self->dbh->{RaiseError};
145
    $self->dbh->{RaiseError} = 1;
146
    
147
    # Tranzaction is failed.
148
    if ($tranzaction_error) {
149
        # Rollback
150
        eval{$self->dbh->rollback};
151
        
152
        # Rollback error
153
        my $rollback_error = $@;
try various
yuki-kimoto authored on 2009-10-21
154
        
cleanup
yuki-kimoto authored on 2009-10-31
155
        # Auto commit on
156
        $self->_auto_commit(1);
157
        
158
        # Restore RaiseError value
159
        $self->dbh->{RaiseError} = $old_raise_error;
160
        
161
        if ($rollback_error) {
162
            # Rollback is failed
163
            croak("${tranzaction_error}Rollback is failed : $rollback_error");
164
        }
165
        else {
166
            # Rollback is success
167
            croak("${tranzaction_error}Rollback is success");
168
        }
169
    }
170
    # Tranzaction is success
171
    else {
172
        # Commit
173
        eval{$self->dbh->commit};
174
        my $commit_error = $@;
175
        
176
        # Auto commit on
177
        $self->_auto_commit(1);
178
        
179
        # Restore RaiseError value
180
        $self->dbh->{RaiseError} = $old_raise_error;
181
        
182
        # Commit is failed
183
        croak($commit_error) if $commit_error;
try various
yuki-kimoto authored on 2009-10-21
184
    }
add tests
yuki-kimoto authored on 2009-10-18
185
}
186

            
cleanup
yuki-kimoto authored on 2009-10-31
187
# Prepare statement handle
add prepare
yuki-kimoto authored on 2009-10-31
188
sub prepare {
189
    my ($self, $sql) = @_;
cleanup
yuki-kimoto authored on 2009-10-31
190
    
191
    # Connect if not
add prepare
yuki-kimoto authored on 2009-10-31
192
    eval{$self->connect unless $self->connected};
193
    croak($@) if $@;
194
    
cleanup
yuki-kimoto authored on 2009-10-31
195
    # Prepare
add prepare
yuki-kimoto authored on 2009-10-31
196
    my $sth = eval{$self->dbh->prepare($sql)};
197
    croak($@) if $@;
198
    return $sth;
199
}
200

            
cleanup
yuki-kimoto authored on 2009-10-31
201
# Execute SQL directly
add prepare
yuki-kimoto authored on 2009-10-31
202
sub do{
203
    my ($self, $sql, @bind_values) = @_;
cleanup
yuki-kimoto authored on 2009-10-31
204
    
205
    # Connect if not
add prepare
yuki-kimoto authored on 2009-10-31
206
    eval{$self->connect unless $self->connected};
207
    croak($@) if $@;
208
    
cleanup
yuki-kimoto authored on 2009-10-31
209
    # Do
add prepare
yuki-kimoto authored on 2009-10-31
210
    eval{$self->dbh->do($sql, @bind_values)};
211
    croak($@) if $@;
212
}
213

            
cleanup
yuki-kimoto authored on 2009-10-31
214
# Create query
add various thins
yuki-kimoto authored on 2009-10-29
215
sub create_query {
216
    my ($self, $template) = @_;
add test
yuki-kimoto authored on 2009-10-17
217
    
add various thins
yuki-kimoto authored on 2009-10-29
218
    # Create query from SQL template
add prepare
yuki-kimoto authored on 2009-10-31
219
    my $sql_template = $self->sql_template;
220
    my $query = eval{$sql_template->create_query($template)};
221
    croak($@) if $@;
add test
yuki-kimoto authored on 2009-10-17
222
    
add various thins
yuki-kimoto authored on 2009-10-29
223
    # Create Query object;
cleanup
yuki-kimoto authored on 2009-10-29
224
    $query = DBI::Custom::Query->new($query);
add various thins
yuki-kimoto authored on 2009-10-29
225
    
cleanup
yuki-kimoto authored on 2009-10-31
226
    # Connect if not
227
    eval{$self->connect unless $self->connected};
228
    croak($@) if $@;
try varioud way
yuki-kimoto authored on 2009-10-17
229
    
add various thins
yuki-kimoto authored on 2009-10-29
230
    # Prepare statement handle
add prepare
yuki-kimoto authored on 2009-10-31
231
    my $sth = eval{$self->dbh->prepare($query->{sql})};
add tests
yuki-kimoto authored on 2009-10-31
232
    if ($@) {
233
        my $sql = $query->{sql} || '';
234
        my $message = "<Created SQL>\n$sql\n";
235
        croak("$@$message");
236
    }
add tests
yuki-kimoto authored on 2009-10-18
237
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
238
    # Set statement handle
add various thins
yuki-kimoto authored on 2009-10-29
239
    $query->sth($sth);
add tests
yuki-kimoto authored on 2009-10-18
240
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
241
    # Set bind filter
242
    $query->bind_filter($self->bind_filter);
243
    
244
    # Set no filter keys when binding
245
    $query->no_bind_filters($self->no_bind_filters);
add tests
yuki-kimoto authored on 2009-10-31
246
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
247
    # Set fetch filter
248
    $query->fetch_filter($self->fetch_filter);
249
    
250
    # Set no filter keys when fetching
251
    $query->no_fetch_filters($self->no_fetch_filters);
252
    
add various thins
yuki-kimoto authored on 2009-10-29
253
    return $query;
254
}
255

            
cleanup
yuki-kimoto authored on 2009-10-31
256
# Execute query
add various thins
yuki-kimoto authored on 2009-10-29
257
sub execute {
258
    my ($self, $query, $params)  = @_;
add tests
yuki-kimoto authored on 2009-10-29
259
    $params ||= {};
try varioud way
yuki-kimoto authored on 2009-10-17
260
    
add tests
yuki-kimoto authored on 2009-10-31
261
    # First argument is SQL template
262
    if (!ref $query) {
263
        my $template = $query;
264
        $query = $self->create_query($template);
265
        my $query_edit_cb = $_[3];
266
        $query_edit_cb->($query) if ref $query_edit_cb eq 'CODE';
267
    }
268
    
add various thins
yuki-kimoto authored on 2009-10-29
269
    # Create bind value
270
    my $bind_values = $self->_build_bind_values($query, $params);
add tests
yuki-kimoto authored on 2009-10-18
271
    
cleanup
yuki-kimoto authored on 2009-10-18
272
    # Execute
cleanup
yuki-kimoto authored on 2009-10-29
273
    my $sth = $query->sth;
add tests
yuki-kimoto authored on 2009-10-31
274
    my $ret_val = eval{$sth->execute(@$bind_values)};
cleanup
yuki-kimoto authored on 2009-10-31
275
    
276
    # Execute error
add tests
yuki-kimoto authored on 2009-10-31
277
    if ($@) {
278
        require Data::Dumper;
279
        my $sql         = $query->{sql} || '';
280
        my $params_dump = Data::Dumper->Dump([$params], ['*params']);
281
        
282
        my $message = "<Created SQL>\n$sql\n<Your parameters>$params_dump";
283
        croak("$@$message");
284
    }
add various things
yuki-kimoto authored on 2009-10-17
285
    
cleanup
yuki-kimoto authored on 2009-10-18
286
    # Return resultset if select statement is executed
add various things
yuki-kimoto authored on 2009-10-17
287
    if ($sth->{NUM_OF_FIELDS}) {
cleanup
yuki-kimoto authored on 2009-10-31
288
        
289
        # Get result class
add various things
yuki-kimoto authored on 2009-10-17
290
        my $result_class = $self->result_class;
cleanup
yuki-kimoto authored on 2009-10-31
291
        
292
        # Create result
add various
yuki-kimoto authored on 2009-10-18
293
        my $result = $result_class->new({
add no_bind_filters
yuki-kimoto authored on 2009-10-30
294
            sth              => $sth,
295
            fetch_filter     => $query->fetch_filter,
296
            no_fetch_filters => $query->no_fetch_filters
add various
yuki-kimoto authored on 2009-10-18
297
        });
add various things
yuki-kimoto authored on 2009-10-17
298
        return $result;
299
    }
add tests
yuki-kimoto authored on 2009-10-18
300
    return $ret_val;
add test
yuki-kimoto authored on 2009-10-17
301
}
302

            
cleanup
yuki-kimoto authored on 2009-10-31
303
# Build binding values
add various thins
yuki-kimoto authored on 2009-10-29
304
sub _build_bind_values {
305
    my ($self, $query, $params) = @_;
add no_bind_filters
yuki-kimoto authored on 2009-10-30
306
    my $key_infos           = $query->key_infos;
307
    my $bind_filter         = $query->bind_filter;
308
    my $no_bind_filters_map = $query->_no_bind_filters_map || {};
cleanup
yuki-kimoto authored on 2009-10-18
309
    
add various thins
yuki-kimoto authored on 2009-10-29
310
    # binding values
311
    my @bind_values;
cleanup
yuki-kimoto authored on 2009-10-18
312
    
add tests
yuki-kimoto authored on 2009-10-31
313
    # Create bind values
cleanup
yuki-kimoto authored on 2009-10-29
314
    foreach my $key_info (@$key_infos) {
cleanup
yuki-kimoto authored on 2009-10-31
315
        # Set variable
316
        my $access_keys  = $key_info->{access_keys};
cleanup
yuki-kimoto authored on 2009-10-29
317
        my $original_key = $key_info->{original_key} || '';
318
        my $table        = $key_info->{table}        || '';
319
        my $column       = $key_info->{column}       || '';
add various thins
yuki-kimoto authored on 2009-10-29
320
        
cleanup
yuki-kimoto authored on 2009-10-31
321
        # Key is found?
add tests
yuki-kimoto authored on 2009-10-31
322
        my $found;
cleanup
yuki-kimoto authored on 2009-10-31
323
        
324
        # Build bind values
add various thins
yuki-kimoto authored on 2009-10-29
325
        ACCESS_KEYS :
326
        foreach my $access_key (@$access_keys) {
cleanup
yuki-kimoto authored on 2009-10-31
327
            # Root parameter
add various thins
yuki-kimoto authored on 2009-10-29
328
            my $root_params = $params;
cleanup
yuki-kimoto authored on 2009-10-31
329
            
330
            # Search corresponding value
add various thins
yuki-kimoto authored on 2009-10-29
331
            for (my $i = 0; $i < @$access_key; $i++) {
cleanup
yuki-kimoto authored on 2009-10-31
332
                # Current key
333
                my $current_key = $access_key->[$i];
add various thins
yuki-kimoto authored on 2009-10-29
334
                
cleanup
yuki-kimoto authored on 2009-10-31
335
                # Each access key must be string or array reference
add various thins
yuki-kimoto authored on 2009-10-29
336
                croak("'access_keys' each value must be string or array reference")
cleanup
yuki-kimoto authored on 2009-10-31
337
                  unless (ref $current_key eq 'ARRAY' ||
338
                          ($current_key && !ref $current_key));
add various thins
yuki-kimoto authored on 2009-10-29
339
                
cleanup
yuki-kimoto authored on 2009-10-31
340
                # Last key
add various thins
yuki-kimoto authored on 2009-10-29
341
                if ($i == @$access_key - 1) {
cleanup
yuki-kimoto authored on 2009-10-31
342
                    # Key is array reference
343
                    if (ref $current_key eq 'ARRAY') {
344
                        # Filtering 
345
                        if ($bind_filter &&
346
                            !$no_bind_filters_map->{$original_key})
347
                        {
add no_bind_filters
yuki-kimoto authored on 2009-10-30
348
                            push @bind_values, 
cleanup
yuki-kimoto authored on 2009-10-31
349
                                 $bind_filter->($original_key, 
350
                                                $root_params->[$current_key->[0]],
cleanup
yuki-kimoto authored on 2009-10-30
351
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
352
                        }
cleanup
yuki-kimoto authored on 2009-10-31
353
                        # Not filtering
add various thins
yuki-kimoto authored on 2009-10-29
354
                        else {
cleanup
yuki-kimoto authored on 2009-10-31
355
                            push @bind_values,
356
                                 scalar $root_params->[$current_key->[0]];
add various thins
yuki-kimoto authored on 2009-10-29
357
                        }
358
                    }
cleanup
yuki-kimoto authored on 2009-10-31
359
                    # Key is string
add various thins
yuki-kimoto authored on 2009-10-29
360
                    else {
cleanup
yuki-kimoto authored on 2009-10-31
361
                        # Key is not found
362
                        next ACCESS_KEYS
363
                          unless exists $root_params->{$current_key};
364
                        
365
                        # Filtering
366
                        if ($bind_filter &&
367
                            !$no_bind_filters_map->{$original_key}) 
368
                        {
add no_bind_filters
yuki-kimoto authored on 2009-10-30
369
                            push @bind_values,
cleanup
yuki-kimoto authored on 2009-10-31
370
                                 $bind_filter->($original_key,
371
                                                $root_params->{$current_key}, 
cleanup
yuki-kimoto authored on 2009-10-30
372
                                                $table, $column);
add various thins
yuki-kimoto authored on 2009-10-29
373
                        }
cleanup
yuki-kimoto authored on 2009-10-31
374
                        # Not filtering
add various thins
yuki-kimoto authored on 2009-10-29
375
                        else {
cleanup
yuki-kimoto authored on 2009-10-31
376
                            push @bind_values,
377
                                 scalar $root_params->{$current_key};
add various thins
yuki-kimoto authored on 2009-10-29
378
                        }
379
                    }
cleanup
yuki-kimoto authored on 2009-10-31
380
                    
381
                    # Key is found
add tests
yuki-kimoto authored on 2009-10-31
382
                    $found = 1;
add various thins
yuki-kimoto authored on 2009-10-29
383
                }
cleanup
yuki-kimoto authored on 2009-10-31
384
                # First or middle key
add various thins
yuki-kimoto authored on 2009-10-29
385
                else {
cleanup
yuki-kimoto authored on 2009-10-31
386
                    # Key is array reference
387
                    if (ref $current_key eq 'ARRAY') {
388
                        # Go next key
389
                        $root_params = $root_params->[$current_key->[0]];
390
                    }
391
                    # Key is string
392
                    else {
393
                        # Not found
394
                        next ACCESS_KEYS
395
                          unless exists $root_params->{$current_key};
396
                        
397
                        # Go next key
398
                        $root_params = $root_params->{$current_key};
399
                    }
add various thins
yuki-kimoto authored on 2009-10-29
400
                }
401
            }
402
        }
add tests
yuki-kimoto authored on 2009-10-31
403
        
cleanup
yuki-kimoto authored on 2009-10-31
404
        # Key is not found
add tests
yuki-kimoto authored on 2009-10-31
405
        unless ($found) {
406
            require Data::Dumper;
add tests
yuki-kimoto authored on 2009-10-31
407
            my $key_info_dump  = Data::Dumper->Dump([$key_info], ['*key_info']);
add tests
yuki-kimoto authored on 2009-10-31
408
            my $params_dump    = Data::Dumper->Dump([$params], ['*params']);
add tests
yuki-kimoto authored on 2009-10-31
409
            croak("Key not found in your parameters\n" . 
add tests
yuki-kimoto authored on 2009-10-31
410
                  "<Key information>\n$key_info_dump\n\n" .
411
                  "<Your parameters>\n$params_dump\n");
412
        }
update document
yuki-kimoto authored on 2009-10-27
413
    }
add tests
yuki-kimoto authored on 2009-10-31
414
    return \@bind_values;
add test
yuki-kimoto authored on 2009-10-17
415
}
416

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
420
=head1 NAME
421

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

            
424
=head1 VERSION
425

            
add test
yuki-kimoto authored on 2009-10-16
426
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
427

            
cleanup
yuki-kimoto authored on 2009-10-31
428
=head1 CAUTION
429

            
430
This module is now experimental stage.
431

            
432
I want you to try this module
433
because I want this module stable, and not to damage your DB data by this module bug.
434

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

            
437
=head1 SYNOPSIS
438

            
add test
yuki-kimoto authored on 2009-10-16
439
  my $dbi = DBI::Custom->new;
add various thins
yuki-kimoto authored on 2009-10-29
440
  
441
  my $query = $dbi->create_query($template);
442
  $dbi->execute($query);
first commit
yuki-kimoto authored on 2009-10-13
443

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

            
update document
yuki-kimoto authored on 2009-10-27
446
=head2 user
447

            
448
    # Set and get database user name
449
    $self = $dbi->user($user);
450
    $user = $dbi->user;
451
    
452
    # Sample
453
    $dbi->user('taro');
454

            
455
=head2 password
456

            
457
    # Set and get database password
458
    $self     = $dbi->password($password);
459
    $password = $dbi->password;
460
    
461
    # Sample
462
    $dbi->password('lkj&le`@s');
463

            
464
=head2 data_source
465

            
466
    # Set and get database data source
467
    $self        = $dbi->data_source($data_soruce);
468
    $data_source = $dbi->data_source;
469
    
470
    # Sample(SQLite)
471
    $dbi->data_source(dbi:SQLite:dbname=$database);
472
    
473
    # Sample(MySQL);
474
    $dbi->data_source("dbi:mysql:dbname=$database");
475
    
476
    # Sample(PostgreSQL)
477
    $dbi->data_source("dbi:Pg:dbname=$database");
cleanup
yuki-kimoto authored on 2009-10-29
478
    
479
=head2 database
480

            
481
    # Set and get database name
482
    $self     = $dbi->database($database);
483
    $database = $dbi->database;
update document
yuki-kimoto authored on 2009-10-27
484

            
cleanup
yuki-kimoto authored on 2009-10-29
485
=head2 dbi_options
update document
yuki-kimoto authored on 2009-10-27
486

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

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

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

            
add prepare
yuki-kimoto authored on 2009-10-31
496
=head2 prepare
497

            
498
    $sth = $dbi->prepare($sql);
499

            
500
This method is same as DBI::prepare
501

            
502
=head2 do
503

            
504
    $dbi->do($sql, @bind_values);
505

            
506
This method is same as DBI::do
507

            
update document
yuki-kimoto authored on 2009-10-27
508
=head2 sql_template
509

            
510
    # Set and get SQL::Template object
511
    $self         = $dbi->sql_template($sql_template);
512
    $sql_template = $dbi->sql_template;
513
    
514
    # Sample
515
    $dbi->sql_template(DBI::Cutom::SQL::Template->new);
516

            
517
=head2 filters
518

            
519
    # Set and get filters
520
    $self    = $dbi->filters($filters);
521
    $filters = $dbi->filters;
first commit
yuki-kimoto authored on 2009-10-13
522

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

            
update document
yuki-kimoto authored on 2009-10-27
525
    # Set and get binding filter
526
    $self        = $dbi->bind_filter($bind_filter);
527
    $bind_filter = $dbi->bind_filter
first commit
yuki-kimoto authored on 2009-10-13
528

            
update document
yuki-kimoto authored on 2009-10-27
529
    # Sample
530
    $dbi->bind_filter($self->filters->{default_bind_filter});
531
    
first commit
yuki-kimoto authored on 2009-10-13
532

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

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

            
update document
yuki-kimoto authored on 2009-10-27
537
    # Set and get Fetch filter
538
    $self         = $dbi->fetch_filter($fetch_filter);
539
    $fetch_filter = $dbi->fetch_filter;
first commit
yuki-kimoto authored on 2009-10-13
540

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

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
544
=head2 no_bind_filters
545

            
546
    # Set and get no filter keys when binding
547
    $self            = $dbi->no_bind_filters($no_bind_filters);
548
    $no_bind_filters = $dbi->no_bind_filters;
549

            
550
=head2 no_fetch_filters
cleanup
yuki-kimoto authored on 2009-10-29
551

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
552
    # Set and get no filter keys when fetching
553
    $self             = $dbi->no_fetch_filters($no_fetch_filters);
554
    $no_fetch_filters = $dbi->no_fetch_filters;
cleanup
yuki-kimoto authored on 2009-10-29
555

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

            
update document
yuki-kimoto authored on 2009-10-27
558
    # Set and get resultset class
559
    $self         = $dbi->result_class($result_class);
560
    $result_class = $dbi->result_class;
561
    
562
    # Sample
563
    $dbi->result_class('DBI::Custom::Result');
add test
yuki-kimoto authored on 2009-10-17
564

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

            
update document
yuki-kimoto authored on 2009-10-27
567
    # Get database handle
568
    $dbh = $self->dbh;
add test
yuki-kimoto authored on 2009-10-17
569

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

            
update document
yuki-kimoto authored on 2009-10-27
572
=head2 connect
573

            
574
    # Connect to database
575
    $self = $dbi->connect;
576
    
577
    # Sample
578
    $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', 
579
                            data_soruce => "dbi:mysql:dbname=$database");
580
    $dbi->connect;
add tests
yuki-kimoto authored on 2009-10-18
581

            
582
=head2 disconnect
583

            
update document
yuki-kimoto authored on 2009-10-27
584
    # Disconnect database
585
    $dbi->disconnect;
586

            
587
If database is already disconnected, this method do noting.
588

            
add tests
yuki-kimoto authored on 2009-10-18
589
=head2 reconnect
590

            
update document
yuki-kimoto authored on 2009-10-27
591
    # Reconnect
592
    $dbi->reconnect;
593

            
594
=head2 connected
595

            
596
    # Check connected
597
    $dbi->connected
598

            
599
=head2 add_filter
600

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

            
644
add_filter add filter to filters
add tests
yuki-kimoto authored on 2009-10-18
645

            
cleanup
yuki-kimoto authored on 2009-10-29
646
=head2 create_query
647
    
648
    # Create Query object from SQL template
649
    my $query = $dbi->create_query($template);
650
    
651
=head2 execute
update document
yuki-kimoto authored on 2009-10-27
652

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

            
665
See also L<DBI::Custom::SQL::Template>
666

            
cleanup
yuki-kimoto authored on 2009-10-22
667
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
668

            
update document
yuki-kimoto authored on 2009-10-27
669
    # Run tranzaction
670
    $dbi->run_tranzaction(sub {
671
        # do something
672
    });
first commit
yuki-kimoto authored on 2009-10-13
673

            
update document
yuki-kimoto authored on 2009-10-27
674
If tranzaction is success, commit is execute. 
675
If tranzation is died, rollback is execute.
first commit
yuki-kimoto authored on 2009-10-13
676

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

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
683
=head1 COPYRIGHT & LICENSE
684

            
685
Copyright 2009 Yuki Kimoto, all rights reserved.
686

            
687
This program is free software; you can redistribute it and/or modify it
688
under the same terms as Perl itself.
689

            
690
=cut