DBIx-Custom / lib / DBI / Custom.pm /
Newer Older
1071 lines | 27.684kb
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 query cache system
yuki-kimoto authored on 2009-11-02
173
    my $class = ref $self;
add test
yuki-kimoto authored on 2009-10-17
174
    
add various thins
yuki-kimoto authored on 2009-10-29
175
    # Create query from SQL template
add prepare
yuki-kimoto authored on 2009-10-31
176
    my $sql_template = $self->sql_template;
add test
yuki-kimoto authored on 2009-10-17
177
    
add query cache system
yuki-kimoto authored on 2009-11-02
178
    # Try to get cached query
179
    my $query = $class->_query_caches->{$template};
180
    
181
    # Create query
182
    unless ($query) {
183
        $query = eval{$sql_template->create_query($template)};
184
        croak($@) if $@;
185
        
186
        $query = DBI::Custom::Query->new($query);
187
        
188
        $class->_add_query_cache($template, $query);
189
    }
add various thins
yuki-kimoto authored on 2009-10-29
190
    
cleanup
yuki-kimoto authored on 2009-10-31
191
    # Connect if not
add tests
yuki-kimoto authored on 2009-10-31
192
    $self->connect unless $self->connected;
try varioud way
yuki-kimoto authored on 2009-10-17
193
    
add various thins
yuki-kimoto authored on 2009-10-29
194
    # Prepare statement handle
add tests
yuki-kimoto authored on 2009-10-31
195
    my $sth = $self->prepare($query->{sql});
add tests
yuki-kimoto authored on 2009-10-18
196
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
197
    # Set statement handle
add various thins
yuki-kimoto authored on 2009-10-29
198
    $query->sth($sth);
add tests
yuki-kimoto authored on 2009-10-18
199
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
200
    # Set bind filter
201
    $query->bind_filter($self->bind_filter);
202
    
203
    # Set no filter keys when binding
204
    $query->no_bind_filters($self->no_bind_filters);
add tests
yuki-kimoto authored on 2009-10-31
205
    
add no_bind_filters
yuki-kimoto authored on 2009-10-30
206
    # Set fetch filter
207
    $query->fetch_filter($self->fetch_filter);
208
    
209
    # Set no filter keys when fetching
210
    $query->no_fetch_filters($self->no_fetch_filters);
211
    
add various thins
yuki-kimoto authored on 2009-10-29
212
    return $query;
213
}
214

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

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

            
add tests
yuki-kimoto authored on 2009-10-31
375
# Run tranzaction
376
sub run_tranzaction {
377
    my ($self, $tranzaction) = @_;
378
    
379
    # Check auto commit
380
    croak("AutoCommit must be true before tranzaction start")
381
      unless $self->_auto_commit;
382
    
383
    # Auto commit off
384
    $self->_auto_commit(0);
385
    
386
    # Run tranzaction
387
    eval {$tranzaction->()};
388
    
389
    # Tranzaction error
390
    my $tranzaction_error = $@;
391
    
392
    # Tranzaction is failed.
393
    if ($tranzaction_error) {
394
        # Rollback
395
        eval{$self->dbh->rollback};
396
        
397
        # Rollback error
398
        my $rollback_error = $@;
399
        
400
        # Auto commit on
401
        $self->_auto_commit(1);
402
        
403
        if ($rollback_error) {
404
            # Rollback is failed
405
            croak("${tranzaction_error}Rollback is failed : $rollback_error");
406
        }
407
        else {
408
            # Rollback is success
409
            croak("${tranzaction_error}Rollback is success");
410
        }
411
    }
412
    # Tranzaction is success
413
    else {
414
        # Commit
415
        eval{$self->dbh->commit};
416
        my $commit_error = $@;
417
        
418
        # Auto commit on
419
        $self->_auto_commit(1);
420
        
421
        # Commit is failed
422
        croak($commit_error) if $commit_error;
423
    }
424
}
add various thins
yuki-kimoto authored on 2009-10-29
425

            
add methods
yuki-kimoto authored on 2009-11-02
426
# Get last insert id
427
sub last_insert_id {
428
    my $self = shift;
429
    
430
    # Not connected
431
    croak("Not yet connect to database")
432
      unless $self->connected;
433
    
434
    return $self->dbh->last_insert_id(@_);
435
}
436

            
437
# Insert
438
sub insert {
add tests
yuki-kimoto authored on 2009-11-02
439
    my ($self, $table, $insert_params, $query_edit_cb) = @_;
add tests
yuki-kimoto authored on 2009-11-03
440
    $table         ||= '';
add methods
yuki-kimoto authored on 2009-11-02
441
    $insert_params ||= {};
442
    
443
    # Insert keys
444
    my @insert_keys = keys %$insert_params;
445
    
446
    # Not exists insert keys
add tests
yuki-kimoto authored on 2009-11-03
447
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
add methods
yuki-kimoto authored on 2009-11-02
448
      unless @insert_keys;
449
    
450
    # Templte for insert
451
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
452
    
453
    # Create query
454
    my $query = $self->create_query($template);
455
    
add tests
yuki-kimoto authored on 2009-11-02
456
    # Query edit callback must be code reference
457
    croak("Query edit callback must be code reference")
458
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
add methods
yuki-kimoto authored on 2009-11-02
459
    
add tests
yuki-kimoto authored on 2009-11-02
460
    # Query edit if need
461
    $query_edit_cb->($query) if $query_edit_cb;
add methods
yuki-kimoto authored on 2009-11-02
462
    
463
    # Execute query
464
    my $ret_val = $self->execute($query, $insert_params);
465
    
466
    return $ret_val;
467
}
468

            
add tests
yuki-kimoto authored on 2009-11-03
469
# Update
add methods
yuki-kimoto authored on 2009-11-02
470
sub update {
471
    my ($self, $table, $update_params,
add tests
yuki-kimoto authored on 2009-11-02
472
        $where_params, $query_edit_cb, $options) = @_;
add methods
yuki-kimoto authored on 2009-11-02
473
    
add tests
yuki-kimoto authored on 2009-11-03
474
    $table         ||= '';
add methods
yuki-kimoto authored on 2009-11-02
475
    $update_params ||= {};
476
    $where_params  ||= {};
477
    
478
    # Update keys
add tests
yuki-kimoto authored on 2009-11-03
479
    my @update_keys = keys %$update_params;
add methods
yuki-kimoto authored on 2009-11-02
480
    
481
    # Not exists update kyes
add tests
yuki-kimoto authored on 2009-11-03
482
    croak("Key-value pairs for update must be specified to 'update' second argument")
add methods
yuki-kimoto authored on 2009-11-02
483
      unless @update_keys;
484
    
485
    # Where keys
486
    my @where_keys = keys %$where_params;
487
    
488
    # Not exists where keys
add tests
yuki-kimoto authored on 2009-11-03
489
    croak("Key-value pairs for where clause must be specified to 'update' third argument")
add methods
yuki-kimoto authored on 2009-11-02
490
      if !@where_keys && !$options->{allow_update_all};
491
    
492
    # Update clause
493
    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
494
    
495
    # Where clause
add tests
yuki-kimoto authored on 2009-11-03
496
    my $where_clause = '';
497
    if (@where_keys) {
498
        $where_clause = 'where ';
499
        foreach my $where_key (@where_keys) {
500
            $where_clause .= "{= $where_key} && ";
501
        }
502
        $where_clause =~ s/ && $//;
add methods
yuki-kimoto authored on 2009-11-02
503
    }
504
    
505
    # Template for update
506
    my $template = "update $table $update_clause $where_clause";
507
    
508
    # Create query
509
    my $query = $self->create_query($template);
510
    
add tests
yuki-kimoto authored on 2009-11-02
511
    # Query edit callback must be code reference
512
    croak("Query edit callback must be code reference")
513
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
add methods
yuki-kimoto authored on 2009-11-02
514
    
add tests
yuki-kimoto authored on 2009-11-02
515
    # Query edit if need
516
    $query_edit_cb->($query) if $query_edit_cb;
add methods
yuki-kimoto authored on 2009-11-02
517
    
518
    # Rearrange parammeters
519
    my $params = {'#update' => $update_params, %$where_params};
520
    
521
    # Execute query
522
    my $ret_val = $self->execute($query, $params);
523
    
524
    return $ret_val;
525
}
526

            
527
# Update all rows
528
sub update_all {
add tests
yuki-kimoto authored on 2009-11-02
529
    my ($self, $table, $update_params, $query_edit_cb) = @_;
add methods
yuki-kimoto authored on 2009-11-02
530
    
add tests
yuki-kimoto authored on 2009-11-02
531
    return $self->update($table, $update_params, {}, $query_edit_cb,
add methods
yuki-kimoto authored on 2009-11-02
532
                         {allow_update_all => 1});
533
}
534

            
535
# Delete
536
sub delete {
add tests
yuki-kimoto authored on 2009-11-02
537
    my ($self, $table, $where_params, $query_edit_cb, $options) = @_;
add tests
yuki-kimoto authored on 2009-11-03
538
    $table        ||= '';
add methods
yuki-kimoto authored on 2009-11-02
539
    $where_params ||= {};
540
    
541
    # Where keys
542
    my @where_keys = keys %$where_params;
543
    
544
    # Not exists where keys
add tests
yuki-kimoto authored on 2009-11-03
545
    croak("Key-value pairs for where clause must be specified to 'delete' second argument")
546
      if !@where_keys && !$options->{allow_delete_all};
add methods
yuki-kimoto authored on 2009-11-02
547
    
548
    # Where clause
add tests
yuki-kimoto authored on 2009-11-03
549
    my $where_clause = '';
550
    if (@where_keys) {
551
        $where_clause = 'where ';
552
        foreach my $where_key (@where_keys) {
553
            $where_clause .= "{= $where_key} && ";
554
        }
555
        $where_clause =~ s/ && $//;
add methods
yuki-kimoto authored on 2009-11-02
556
    }
557
    
558
    # Template for delete
559
    my $template = "delete from $table $where_clause";
560
    
561
    # Create query
562
    my $query = $self->create_query($template);
563
    
add tests
yuki-kimoto authored on 2009-11-02
564
    # Query edit callback must be code reference
565
    croak("Query edit callback must be code reference")
566
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
add methods
yuki-kimoto authored on 2009-11-02
567
    
add tests
yuki-kimoto authored on 2009-11-02
568
    # Query edit if need
569
    $query_edit_cb->($query) if $query_edit_cb;
add methods
yuki-kimoto authored on 2009-11-02
570
    
571
    # Execute query
572
    my $ret_val = $self->execute($query, $where_params);
573
    
574
    return $ret_val;
575
}
576

            
577
# Delete all rows
578
sub delete_all {
add tests
yuki-kimoto authored on 2009-11-03
579
    my ($self, $table) = @_;
580
    return $self->delete($table, {}, undef, {allow_delete_all => 1});
add methods
yuki-kimoto authored on 2009-11-02
581
}
582

            
add select
yuki-kimoto authored on 2009-11-05
583
sub _select_usage { return << 'EOS' }
add select
yuki-kimoto authored on 2009-11-05
584
Your select arguments is wrong.
585
select usage:
add select
yuki-kimoto authored on 2009-11-05
586
$dbi->select(
add select
yuki-kimoto authored on 2009-11-05
587
    $table,                # must be string or array ref
add select
yuki-kimoto authored on 2009-11-05
588
    [@$columns],           # must be array reference. this is optional
589
    {%$where_params},      # must be hash reference.  this is optional
590
    $append_statement,     # must be string.          this is optional
591
    $query_edit_callback   # must be code reference.  this is optional
592
);
593
EOS
594

            
595
sub select {
596
    my $self = shift;
597
    
598
    # Check argument
599
    croak($self->_select_usage) unless @_;
600
    
601
    # Arguments
602
    my $tables = shift || '';
603
    $tables    = [$tables] unless ref $tables;
604
    
605
    my $columns          = ref $_[0] eq 'ARRAY' ? shift : [];
606
    my $where_params     = ref $_[0] eq 'HASH'  ? shift : {};
607
    my $append_statement = shift unless ref $_[0] || '';
608
    my $query_edit_cb    = shift if ref $_[0] eq 'CODE';
609
    
610
    # Check rest argument
611
    croak($self->_select_usage) unless @_;
612
    
613
    # SQL template for select statement
614
    my $template = 'select ';
615
    
616
    # Join column clause
617
    if (@$columns) {
618
        foreach my $column (@$columns) {
619
            $template .= "$column, ";
620
        }
621
        $template .= s/, $/ /;
622
    }
623
    else {
624
        $template .= '* ';
625
    }
626
    
627
    # Join table
628
    foreach my $table (@$tables) {
629
        $template .= "$table, ";
630
    }
631
    $template =~ s/, / /;
632
    
633
    # Where clause keys
634
    my @where_keys = keys %$where_params;
635
    
636
    # Join where clause
637
    if (@where_keys) {
638
        $template .= 'where ';
639
        foreach my $where_key (@where_keys) {
640
            $template .= "{= $where_key} && ";
641
        }
642
    }
643
    $template =~ s/ && $//;
644
    
645
    # Append something to last of statement
646
    if ($append_statement =~ s/^where //) {
647
        if (@where_keys) {
648
            $template .= " && $append_statement";
649
        }
650
        else {
651
            $template .= " where $append_statement";
652
        }
653
    }
654
    else {
655
        $template .= " $append_statement";
656
    }
657
    
658
    # Create query
659
    my $query = $self->create_query($template);
660
    
661
    # Query edit
662
    $query_edit_cb->($query) if $query_edit_cb;
663
    
664
    # Execute query
665
    my $result = $self->execute($query, $where_params);
666
    
667
    return $result;
668
}
669

            
add query cache system
yuki-kimoto authored on 2009-11-02
670
sub _query_caches     : ClassAttr { type => 'hash',
671
                                    auto_build => sub {shift->_query_caches({}) } }
672
                                    
673
sub _query_cache_keys : ClassAttr { type => 'array',
674
                                    auto_build => sub {shift->_query_cache_keys([])} }
675
                                    
676
sub query_cache_max   : ClassAttr { auto_build => sub {shift->query_cache_max(50)} }
677

            
678
# Add query cahce
679
sub _add_query_cache {
680
    my ($class, $template, $query) = @_;
681
    my $query_cache_keys = $class->_query_cache_keys;
682
    my $query_caches     = $class->_query_caches;
683
    
684
    return $class if $query_caches->{$template};
685
    
686
    $query_caches->{$template} = $query;
687
    push @$query_cache_keys, $template;
688
    
689
    my $overflow = @$query_cache_keys - $class->query_cache_max;
690
    
691
    for (my $i = 0; $i < $overflow; $i++) {
692
        my $template = shift @$query_cache_keys;
693
        delete $query_caches->{$template};
694
    }
695
    
696
    return $class;
697
}
add methods
yuki-kimoto authored on 2009-11-02
698

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

            
first commit
yuki-kimoto authored on 2009-10-13
701
=head1 NAME
702

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

            
705
=head1 VERSION
706

            
add test
yuki-kimoto authored on 2009-10-16
707
Version 0.0101
first commit
yuki-kimoto authored on 2009-10-13
708

            
cleanup
yuki-kimoto authored on 2009-10-31
709
=head1 CAUTION
710

            
711
This module is now experimental stage.
712

            
713
I want you to try this module
714
because I want this module stable, and not to damage your DB data by this module bug.
715

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

            
718
=head1 SYNOPSIS
719

            
add test
yuki-kimoto authored on 2009-10-16
720
  my $dbi = DBI::Custom->new;
add various thins
yuki-kimoto authored on 2009-10-29
721
  
722
  my $query = $dbi->create_query($template);
723
  $dbi->execute($query);
first commit
yuki-kimoto authored on 2009-10-13
724

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

            
update document
yuki-kimoto authored on 2009-10-27
727
=head2 user
728

            
729
    # Set and get database user name
730
    $self = $dbi->user($user);
731
    $user = $dbi->user;
732
    
733
    # Sample
734
    $dbi->user('taro');
735

            
736
=head2 password
737

            
738
    # Set and get database password
739
    $self     = $dbi->password($password);
740
    $password = $dbi->password;
741
    
742
    # Sample
743
    $dbi->password('lkj&le`@s');
744

            
745
=head2 data_source
746

            
747
    # Set and get database data source
748
    $self        = $dbi->data_source($data_soruce);
749
    $data_source = $dbi->data_source;
750
    
751
    # Sample(SQLite)
752
    $dbi->data_source(dbi:SQLite:dbname=$database);
753
    
754
    # Sample(MySQL);
755
    $dbi->data_source("dbi:mysql:dbname=$database");
756
    
757
    # Sample(PostgreSQL)
758
    $dbi->data_source("dbi:Pg:dbname=$database");
cleanup
yuki-kimoto authored on 2009-10-29
759
    
760
=head2 database
761

            
762
    # Set and get database name
763
    $self     = $dbi->database($database);
764
    $database = $dbi->database;
update document
yuki-kimoto authored on 2009-10-27
765

            
cleanup
yuki-kimoto authored on 2009-10-29
766
=head2 dbi_options
update document
yuki-kimoto authored on 2009-10-27
767

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

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

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

            
add prepare
yuki-kimoto authored on 2009-10-31
777
=head2 prepare
778

            
779
    $sth = $dbi->prepare($sql);
780

            
781
This method is same as DBI::prepare
782

            
783
=head2 do
784

            
785
    $dbi->do($sql, @bind_values);
786

            
787
This method is same as DBI::do
788

            
update document
yuki-kimoto authored on 2009-10-27
789
=head2 sql_template
790

            
791
    # Set and get SQL::Template object
792
    $self         = $dbi->sql_template($sql_template);
793
    $sql_template = $dbi->sql_template;
794
    
795
    # Sample
796
    $dbi->sql_template(DBI::Cutom::SQL::Template->new);
797

            
798
=head2 filters
799

            
800
    # Set and get filters
801
    $self    = $dbi->filters($filters);
802
    $filters = $dbi->filters;
first commit
yuki-kimoto authored on 2009-10-13
803

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

            
update document
yuki-kimoto authored on 2009-10-27
806
    # Set and get binding filter
807
    $self        = $dbi->bind_filter($bind_filter);
808
    $bind_filter = $dbi->bind_filter
first commit
yuki-kimoto authored on 2009-10-13
809

            
update document
yuki-kimoto authored on 2009-10-27
810
    # Sample
811
    $dbi->bind_filter($self->filters->{default_bind_filter});
812
    
first commit
yuki-kimoto authored on 2009-10-13
813

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

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

            
update document
yuki-kimoto authored on 2009-10-27
818
    # Set and get Fetch filter
819
    $self         = $dbi->fetch_filter($fetch_filter);
820
    $fetch_filter = $dbi->fetch_filter;
first commit
yuki-kimoto authored on 2009-10-13
821

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

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
825
=head2 no_bind_filters
826

            
827
    # Set and get no filter keys when binding
828
    $self            = $dbi->no_bind_filters($no_bind_filters);
829
    $no_bind_filters = $dbi->no_bind_filters;
830

            
831
=head2 no_fetch_filters
cleanup
yuki-kimoto authored on 2009-10-29
832

            
add no_bind_filters
yuki-kimoto authored on 2009-10-30
833
    # Set and get no filter keys when fetching
834
    $self             = $dbi->no_fetch_filters($no_fetch_filters);
835
    $no_fetch_filters = $dbi->no_fetch_filters;
cleanup
yuki-kimoto authored on 2009-10-29
836

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

            
update document
yuki-kimoto authored on 2009-10-27
839
    # Set and get resultset class
840
    $self         = $dbi->result_class($result_class);
841
    $result_class = $dbi->result_class;
842
    
843
    # Sample
844
    $dbi->result_class('DBI::Custom::Result');
add test
yuki-kimoto authored on 2009-10-17
845

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

            
update document
yuki-kimoto authored on 2009-10-27
848
    # Get database handle
849
    $dbh = $self->dbh;
add test
yuki-kimoto authored on 2009-10-17
850

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

            
update document
yuki-kimoto authored on 2009-10-27
853
=head2 connect
854

            
855
    # Connect to database
856
    $self = $dbi->connect;
857
    
858
    # Sample
859
    $dbi = DBI::Custom->new(user => 'taro', password => 'lji8(', 
860
                            data_soruce => "dbi:mysql:dbname=$database");
861
    $dbi->connect;
add tests
yuki-kimoto authored on 2009-10-18
862

            
863
=head2 disconnect
864

            
update document
yuki-kimoto authored on 2009-10-27
865
    # Disconnect database
866
    $dbi->disconnect;
867

            
868
If database is already disconnected, this method do noting.
869

            
add tests
yuki-kimoto authored on 2009-10-18
870
=head2 reconnect
871

            
update document
yuki-kimoto authored on 2009-10-27
872
    # Reconnect
873
    $dbi->reconnect;
874

            
875
=head2 connected
876

            
877
    # Check connected
878
    $dbi->connected
879

            
880
=head2 add_filter
881

            
882
    # Add filter (hash ref or hash can be recieve)
883
    $self = $dbi->add_filter({$filter_name => $filter, ...});
884
    $self = $dbi->add_filter($filetr_name => $filter, ...);
885
    
886
    # Sample
887
    $dbi->add_filter(
888
        decode_utf8 => sub {
cleanup
yuki-kimoto authored on 2009-10-30
889
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
890
            return Encode::decode('UTF-8', $value);
891
        },
892
        datetime_to_string => sub {
cleanup
yuki-kimoto authored on 2009-10-30
893
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
894
            return $value->strftime('%Y-%m-%d %H:%M:%S')
895
        },
896
        default_bind_filter => sub {
cleanup
yuki-kimoto authored on 2009-10-30
897
            my ($key, $value, $table, $column) = @_;
update document
yuki-kimoto authored on 2009-10-27
898
            if (ref $value eq 'Time::Piece') {
cleanup
yuki-kimoto authored on 2009-10-30
899
                return $dbi->filters->{datetime_to_string}->($value);
update document
yuki-kimoto authored on 2009-10-27
900
            }
901
            else {
cleanup
yuki-kimoto authored on 2009-10-30
902
                return $dbi->filters->{decode_utf8}->($value);
update document
yuki-kimoto authored on 2009-10-27
903
            }
904
        },
905
        
906
        encode_utf8 => sub {
cleanup
yuki-kimoto authored on 2009-10-30
907
            my ($key, $value) = @_;
update document
yuki-kimoto authored on 2009-10-27
908
            return Encode::encode('UTF-8', $value);
909
        },
910
        string_to_datetime => sub {
cleanup
yuki-kimoto authored on 2009-10-30
911
            my ($key, $value) = @_;
update document
yuki-kimoto authored on 2009-10-27
912
            return DateTime::Format::MySQL->parse_datetime($value);
913
        },
914
        default_fetch_filter => sub {
cleanup
yuki-kimoto authored on 2009-10-30
915
            my ($key, $value, $type, $sth, $i) = @_;
update document
yuki-kimoto authored on 2009-10-27
916
            if ($type eq 'DATETIME') {
cleanup
yuki-kimoto authored on 2009-10-30
917
                return $dbi->filters->{string_to_datetime}->($value);
update document
yuki-kimoto authored on 2009-10-27
918
            }
919
            else {
cleanup
yuki-kimoto authored on 2009-10-30
920
                return $dbi->filters->{encode_utf8}->($value);
update document
yuki-kimoto authored on 2009-10-27
921
            }
922
        }
923
    );
924

            
925
add_filter add filter to filters
add tests
yuki-kimoto authored on 2009-10-18
926

            
cleanup
yuki-kimoto authored on 2009-10-29
927
=head2 create_query
928
    
929
    # Create Query object from SQL template
930
    my $query = $dbi->create_query($template);
931
    
932
=head2 execute
update document
yuki-kimoto authored on 2009-10-27
933

            
934
    # Parse SQL template and execute SQL
cleanup
yuki-kimoto authored on 2009-10-29
935
    $result = $dbi->query($query, $params);
936
    $result = $dbi->query($template, $params); # Shorcut
update document
yuki-kimoto authored on 2009-10-27
937
    
938
    # Sample
939
    $result = $dbi->query("select * from authors where {= name} && {= age}", 
940
                          {author => 'taro', age => 19});
941
    
942
    while (my @row = $result->fetch) {
943
        # do something
944
    }
945

            
946
See also L<DBI::Custom::SQL::Template>
947

            
cleanup
yuki-kimoto authored on 2009-10-22
948
=head2 run_tranzaction
first commit
yuki-kimoto authored on 2009-10-13
949

            
update document
yuki-kimoto authored on 2009-10-27
950
    # Run tranzaction
951
    $dbi->run_tranzaction(sub {
952
        # do something
953
    });
first commit
yuki-kimoto authored on 2009-10-13
954

            
update document
yuki-kimoto authored on 2009-10-27
955
If tranzaction is success, commit is execute. 
956
If tranzation is died, rollback is execute.
first commit
yuki-kimoto authored on 2009-10-13
957

            
add methods
yuki-kimoto authored on 2009-11-02
958
=head2 insert
959

            
960
    # Insert
961
    $dbi->insert($table, $insert_values);
962
    
963
    # Sample
964
    $dbi->insert('books', {title => 'Perl', author => 'Taro'});
965

            
966
=head2 update
967

            
968
    # Update
969
    $dbi->update($table, $update_values, $where);
970
    
971
    # Sample
972
    $dbi->update('books', {title => 'Perl', author => 'Taro'}, {id => 5});
973

            
974
=head2 update_all
975

            
976
    # Update all rows
977
    $dbi->update($table, $updat_values);
978

            
979
=head2 delete
980

            
981
    # Delete
982
    $dbi->delete($table, $where);
983
    
984
    # Sample
985
    $dbi->delete('Books', {id => 5});
986

            
987
=head2 delete_all
988

            
989
    # Delete all rows
990
    $dbi->delete_all($table);
991

            
992
=head2 last_insert_id
993

            
994
    # Get last insert id
995
    $last_insert_id = $dbi->last_insert_id;
996
    
997
This method is same as DBI last_insert_id;
998

            
add select
yuki-kimoto authored on 2009-11-05
999
=head2 select
1000
    
1001
    # Select
1002
    $dbi->select(
1003
        $table,                # must be string or array;
1004
        [@$columns],           # must be array reference. this is optional
1005
        {%$where_params},      # must be hash reference.  this is optional
1006
        $append_statement,     # must be string.          this is optional
1007
        $query_edit_callback   # must be code reference.  this is optional
1008
    );
1009
    
1010
    # Sample
1011
    $dbi->select(
1012
        'Books',
1013
        ['title', 'author'],
1014
        {id => 1},
1015
        "for update",
1016
        sub {
1017
            my $query = shift;
1018
            $query->bind_filter(sub {
1019
                # ...
1020
            });
1021
        }
1022
    );
1023
    
1024
    # The way to join multi tables
1025
    $dbi->select(
1026
        ['table1', 'table2'],
1027
        ['table1.id as table1_id', 'title'],
1028
        {table1.id => 1},
1029
        "where table1.id = table2.id",
1030
    );
1031

            
add query cache system
yuki-kimoto authored on 2009-11-02
1032
=head1 Class Accessors
1033

            
1034
=head2 query_cache_max
1035

            
1036
    # Max query cache count
1037
    $class           = $class->query_cache_max($query_cache_max);
1038
    $query_cache_max = $class->query_cache_max;
1039
    
1040
    # Sample
1041
    DBI::Custom->query_cache_max(50);
1042

            
add tests
yuki-kimoto authored on 2009-10-31
1043
=head1 CAUTION
1044

            
1045
DBI::Custom have DIB object internal.
1046
This module is work well in the following DBI condition.
1047

            
1048
    1. AutoCommit is true
1049
    2. RaiseError is true
1050

            
1051
By default, Both AutoCommit and RaiseError is true.
1052
You must not change these mode not to damage your data.
1053

            
1054
If you change these mode, 
1055
you cannot get correct error message, 
1056
or run_tranzaction may fail.
1057

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

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

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

            
first commit
yuki-kimoto authored on 2009-10-13
1064
=head1 COPYRIGHT & LICENSE
1065

            
1066
Copyright 2009 Yuki Kimoto, all rights reserved.
1067

            
1068
This program is free software; you can redistribute it and/or modify it
1069
under the same terms as Perl itself.
1070

            
1071
=cut