DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1197 lines | 27.612kb
cleanup
yuki-kimoto authored on 2009-12-22
1
package DBIx::Custom;
2

            
3
use strict;
4
use warnings;
5

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
6
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
7

            
packaging one directory
yuki-kimoto authored on 2009-11-16
8
use Carp 'croak';
9
use DBI;
10
use DBIx::Custom::Result;
many many changes
yuki-kimoto authored on 2010-04-30
11
use DBIx::Custom::SQLTemplate;
cleanup
yuki-kimoto authored on 2010-02-11
12
use DBIx::Custom::Query;
packaging one directory
yuki-kimoto authored on 2009-11-16
13

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
14
__PACKAGE__->attr('dbh');
version 0.0901
yuki-kimoto authored on 2009-12-17
15

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
16
__PACKAGE__->class_attr(_query_caches     => sub { {} });
17
__PACKAGE__->class_attr(_query_cache_keys => sub { [] });
packaging one directory
yuki-kimoto authored on 2009-11-16
18

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-22
19
__PACKAGE__->class_attr('query_cache_max', default => 50,
20
                                           inherit => 'scalar_copy');
21

            
cleanup
yuki-kimoto authored on 2010-04-28
22
__PACKAGE__->attr([qw/user password data_source/]);
23
__PACKAGE__->attr([qw/database host port/]);
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
24
__PACKAGE__->attr([qw/default_query_filter default_fetch_filter options/]);
packaging one directory
yuki-kimoto authored on 2009-11-16
25

            
cleanup
yuki-kimoto authored on 2010-04-28
26
__PACKAGE__->dual_attr([qw/ filters formats/],
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-22
27
                       default => sub { {} }, inherit => 'hash_copy');
packaging one directory
yuki-kimoto authored on 2009-11-16
28

            
cleanup
yuki-kimoto authored on 2010-04-28
29
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
many many changes
yuki-kimoto authored on 2010-04-30
30
__PACKAGE__->attr(sql_tmpl => sub { DBIx::Custom::SQLTemplate->new });
packaging one directory
yuki-kimoto authored on 2009-11-16
31

            
many many changes
yuki-kimoto authored on 2010-04-30
32
sub resist_filter {
packaging one directory
yuki-kimoto authored on 2009-11-16
33
    my $invocant = shift;
34
    
update document
yuki-kimoto authored on 2010-01-30
35
    # Add filter
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
36
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
37
    $invocant->filters({%{$invocant->filters}, %$filters});
38
    
packaging one directory
yuki-kimoto authored on 2009-11-16
39
    return $invocant;
40
}
41

            
many many changes
yuki-kimoto authored on 2010-04-30
42
sub resist_format{
packaging one directory
yuki-kimoto authored on 2009-11-16
43
    my $invocant = shift;
44
    
update document
yuki-kimoto authored on 2010-01-30
45
    # Add format
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
46
    my $formats = ref $_[0] eq 'HASH' ? $_[0] : {@_};
47
    $invocant->formats({%{$invocant->formats}, %$formats});
48

            
packaging one directory
yuki-kimoto authored on 2009-11-16
49
    return $invocant;
50
}
51

            
52
sub _auto_commit {
53
    my $self = shift;
54
    
update document
yuki-kimoto authored on 2010-01-30
55
    # Not connected
packaging one directory
yuki-kimoto authored on 2009-11-16
56
    croak("Not yet connect to database") unless $self->dbh;
57
    
58
    if (@_) {
update document
yuki-kimoto authored on 2010-01-30
59
        
60
        # Set AutoCommit
packaging one directory
yuki-kimoto authored on 2009-11-16
61
        $self->dbh->{AutoCommit} = $_[0];
update document
yuki-kimoto authored on 2010-01-30
62
        
packaging one directory
yuki-kimoto authored on 2009-11-16
63
        return $self;
64
    }
65
    return $self->dbh->{AutoCommit};
66
}
67

            
68
sub connect {
69
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
70
    
71
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
72
    my $data_source = $self->data_source;
73
    my $user        = $self->user;
74
    my $password    = $self->password;
version 0.0901
yuki-kimoto authored on 2009-12-17
75
    my $options     = $self->options;
packaging one directory
yuki-kimoto authored on 2009-11-16
76
    
update document
yuki-kimoto authored on 2010-01-30
77
    # Connect
packaging one directory
yuki-kimoto authored on 2009-11-16
78
    my $dbh = eval{DBI->connect(
79
        $data_source,
80
        $user,
81
        $password,
82
        {
83
            RaiseError => 1,
84
            PrintError => 0,
85
            AutoCommit => 1,
version 0.0901
yuki-kimoto authored on 2009-12-17
86
            %{$options || {} }
packaging one directory
yuki-kimoto authored on 2009-11-16
87
        }
88
    )};
89
    
update document
yuki-kimoto authored on 2010-01-30
90
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
91
    croak $@ if $@;
92
    
update document
yuki-kimoto authored on 2010-01-30
93
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
94
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
95
    
packaging one directory
yuki-kimoto authored on 2009-11-16
96
    return $self;
97
}
98

            
99
sub DESTROY {
100
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
101
    
102
    # Disconnect
packaging one directory
yuki-kimoto authored on 2009-11-16
103
    $self->disconnect if $self->connected;
104
}
105

            
update document
yuki-kimoto authored on 2010-01-30
106
sub connected { ref shift->{dbh} eq 'DBI::db' }
packaging one directory
yuki-kimoto authored on 2009-11-16
107

            
108
sub disconnect {
109
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
110
    
packaging one directory
yuki-kimoto authored on 2009-11-16
111
    if ($self->connected) {
update document
yuki-kimoto authored on 2010-01-30
112
        
113
        # Disconnect
packaging one directory
yuki-kimoto authored on 2009-11-16
114
        $self->dbh->disconnect;
115
        delete $self->{dbh};
116
    }
update document
yuki-kimoto authored on 2010-01-30
117
    
118
    return $self;
packaging one directory
yuki-kimoto authored on 2009-11-16
119
}
120

            
121
sub reconnect {
122
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
123
    
124
    # Reconnect
packaging one directory
yuki-kimoto authored on 2009-11-16
125
    $self->disconnect if $self->connected;
126
    $self->connect;
update document
yuki-kimoto authored on 2010-01-30
127
    
128
    return $self;
packaging one directory
yuki-kimoto authored on 2009-11-16
129
}
130

            
131
sub prepare {
132
    my ($self, $sql) = @_;
133
    
134
    # Connect if not
135
    $self->connect unless $self->connected;
136
    
137
    # Prepare
138
    my $sth = eval{$self->dbh->prepare($sql)};
139
    
140
    # Error
141
    croak("$@<Your SQL>\n$sql") if $@;
142
    
143
    return $sth;
144
}
145

            
146
sub do{
147
    my ($self, $sql, @bind_values) = @_;
148
    
149
    # Connect if not
150
    $self->connect unless $self->connected;
151
    
152
    # Do
version 0.0901
yuki-kimoto authored on 2009-12-17
153
    my $affected = eval{$self->dbh->do($sql, @bind_values)};
packaging one directory
yuki-kimoto authored on 2009-11-16
154
    
155
    # Error
156
    if ($@) {
157
        my $error = $@;
158
        require Data::Dumper;
159
        
160
        my $bind_value_dump
161
          = Data::Dumper->Dump([\@bind_values], ['*bind_valuds']);
162
        
163
        croak("$error<Your SQL>\n$sql\n<Your bind values>\n$bind_value_dump\n");
164
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
165
    
166
    return $affected;
packaging one directory
yuki-kimoto authored on 2009-11-16
167
}
168

            
169
sub create_query {
170
    my ($self, $template) = @_;
cleanup
yuki-kimoto authored on 2010-02-11
171
    
packaging one directory
yuki-kimoto authored on 2009-11-16
172
    my $class = ref $self;
173
    
cleanup
yuki-kimoto authored on 2010-02-11
174
    if (ref $template eq 'ARRAY') {
175
        $template = $template->[1];
176
    }
177
    
packaging one directory
yuki-kimoto authored on 2009-11-16
178
    # Create query from SQL template
version 0.0901
yuki-kimoto authored on 2009-12-17
179
    my $sql_tmpl = $self->sql_tmpl;
packaging one directory
yuki-kimoto authored on 2009-11-16
180
    
181
    # Try to get cached query
many many changes
yuki-kimoto authored on 2010-04-30
182
    my $cached_query = $class->_query_caches->{"$template"};
packaging one directory
yuki-kimoto authored on 2009-11-16
183
    
184
    # Create query
fix timeformat tests
yuki-kimoto authored on 2009-11-23
185
    my $query;
cleanup
yuki-kimoto authored on 2010-02-11
186
    if ($cached_query) {
187
        $query = DBIx::Custom::Query->new(
188
            sql       => $cached_query->sql,
189
            key_infos => $cached_query->key_infos
190
        );
fix timeformat tests
yuki-kimoto authored on 2009-11-23
191
    }
192
    else {
Simplify key search
yuki-kimoto authored on 2010-02-11
193
        $query = eval{$sql_tmpl->create_query($template)};
packaging one directory
yuki-kimoto authored on 2009-11-16
194
        croak($@) if $@;
195
        
many many changes
yuki-kimoto authored on 2010-04-30
196
        $class->_add_query_cache("$template", $query);
packaging one directory
yuki-kimoto authored on 2009-11-16
197
    }
198
    
199
    # Connect if not
200
    $self->connect unless $self->connected;
201
    
202
    # Prepare statement handle
203
    my $sth = $self->prepare($query->{sql});
204
    
205
    # Set statement handle
206
    $query->sth($sth);
207
    
208
    return $query;
209
}
210

            
version 0.0901
yuki-kimoto authored on 2009-12-17
211
sub query{
many many changes
yuki-kimoto authored on 2010-04-30
212
    my ($self, $query, $params, $args)  = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
213
    $params ||= {};
214
    
215
    # First argument is SQL template
Simplify key search
yuki-kimoto authored on 2010-02-11
216
    unless (ref $query eq 'DBIx::Custom::Query') {
217
        my $template;
218
        
219
        if (ref $query eq 'ARRAY') {
many many changes
yuki-kimoto authored on 2010-04-30
220
            $template = $query->[0];
Simplify key search
yuki-kimoto authored on 2010-02-11
221
        }
222
        else { $template = $query }
223
        
many many changes
yuki-kimoto authored on 2010-04-30
224
        $query = $self->create_query($template);
packaging one directory
yuki-kimoto authored on 2009-11-16
225
    }
many many changes
yuki-kimoto authored on 2010-04-30
226

            
many change
yuki-kimoto authored on 2010-04-30
227
    # Filter
228
    my $filter = $args->{filter} || $query->filter || {};
229

            
packaging one directory
yuki-kimoto authored on 2009-11-16
230
    # Create bind value
many many changes
yuki-kimoto authored on 2010-04-30
231
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
232
    
233
    # Execute
version 0.0901
yuki-kimoto authored on 2009-12-17
234
    my $sth      = $query->sth;
235
    my $affected = eval{$sth->execute(@$bind_values)};
packaging one directory
yuki-kimoto authored on 2009-11-16
236
    
237
    # Execute error
238
    if (my $execute_error = $@) {
239
        require Data::Dumper;
240
        my $sql              = $query->{sql} || '';
241
        my $key_infos_dump   = Data::Dumper->Dump([$query->key_infos], ['*key_infos']);
242
        my $params_dump      = Data::Dumper->Dump([$params], ['*params']);
243
        
244
        croak("$execute_error" . 
245
              "<Your SQL>\n$sql\n" . 
246
              "<Your parameters>\n$params_dump");
247
    }
248
    
249
    # Return resultset if select statement is executed
250
    if ($sth->{NUM_OF_FIELDS}) {
251
        
252
        # Get result class
253
        my $result_class = $self->result_class;
254
        
255
        # Create result
256
        my $result = $result_class->new({
many many changes
yuki-kimoto authored on 2010-04-30
257
            sth             => $sth,
258
            default_filter  => $self->default_fetch_filter,
259
            filters         => $self->filters
packaging one directory
yuki-kimoto authored on 2009-11-16
260
        });
261
        return $result;
262
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
263
    return $affected;
packaging one directory
yuki-kimoto authored on 2009-11-16
264
}
265

            
266
sub _build_bind_values {
many many changes
yuki-kimoto authored on 2010-04-30
267
    my ($self, $query, $params, $filter) = @_;
268
    my $key_infos      = $query->key_infos;
many change
yuki-kimoto authored on 2010-04-30
269
    my $default_filter = $self->default_query_filter || '';
many many changes
yuki-kimoto authored on 2010-04-30
270
    my $filters        = $self->filters;
many change
yuki-kimoto authored on 2010-04-30
271
    $filter            ||= {};
packaging one directory
yuki-kimoto authored on 2009-11-16
272
    
273
    # binding values
274
    my @bind_values;
275
    
Simplify key search
yuki-kimoto authored on 2010-02-11
276
    # Build bind values
packaging one directory
yuki-kimoto authored on 2009-11-16
277
    foreach my $key_info (@$key_infos) {
many many changes
yuki-kimoto authored on 2010-04-30
278
        my $column       = $key_info->{column};
279
        my $pos          = $key_info->{pos};
packaging one directory
yuki-kimoto authored on 2009-11-16
280
        
Simplify key search
yuki-kimoto authored on 2010-02-11
281
        # Value
many many changes
yuki-kimoto authored on 2010-04-30
282
        my $value = defined $pos ? $params->{$column}->[$pos] : $params->{$column};
packaging one directory
yuki-kimoto authored on 2009-11-16
283
        
Simplify key search
yuki-kimoto authored on 2010-02-11
284
        # Filter
many change
yuki-kimoto authored on 2010-04-30
285
        my $fname = $filter->{$column} || $default_filter || '';
many many changes
yuki-kimoto authored on 2010-04-30
286
        
many change
yuki-kimoto authored on 2010-04-30
287
        push @bind_values, $filters->{$fname}
288
                         ? $filters->{$fname}->($value)
289
                         : $value;
cleanup
yuki-kimoto authored on 2010-02-11
290
    }
291
    
Simplify key search
yuki-kimoto authored on 2010-02-11
292
    return \@bind_values;
cleanup
yuki-kimoto authored on 2010-02-11
293
}
294

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
295
sub transaction { DBIx::Custom::Transaction->new(dbi => shift) }
packaging one directory
yuki-kimoto authored on 2009-11-16
296

            
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
297
sub run_transaction {
298
    my ($self, $transaction) = @_;
299
    
300
    # Shorcut
many change
yuki-kimoto authored on 2010-02-11
301
    return unless $self;
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
302
    
303
    # Check auto commit
304
    croak("AutoCommit must be true before transaction start")
many change
yuki-kimoto authored on 2010-02-11
305
      unless $self->_auto_commit;
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
306
    
307
    # Auto commit off
many change
yuki-kimoto authored on 2010-02-11
308
    $self->_auto_commit(0);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
309
    
310
    # Run transaction
311
    eval {$transaction->()};
312
    
313
    # Tranzaction error
314
    my $transaction_error = $@;
315
    
316
    # Tranzaction is failed.
317
    if ($transaction_error) {
318
        # Rollback
many change
yuki-kimoto authored on 2010-02-11
319
        eval{$self->dbh->rollback};
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
320
        
321
        # Rollback error
322
        my $rollback_error = $@;
323
        
324
        # Auto commit on
many change
yuki-kimoto authored on 2010-02-11
325
        $self->_auto_commit(1);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
326
        
327
        if ($rollback_error) {
328
            # Rollback is failed
329
            croak("${transaction_error}Rollback is failed : $rollback_error");
330
        }
331
        else {
332
            # Rollback is success
333
            croak("${transaction_error}Rollback is success");
334
        }
335
    }
336
    # Tranzaction is success
337
    else {
338
        # Commit
many change
yuki-kimoto authored on 2010-02-11
339
        eval{$self->dbh->commit};
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
340
        my $commit_error = $@;
341
        
342
        # Auto commit on
many change
yuki-kimoto authored on 2010-02-11
343
        $self->_auto_commit(1);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
344
        
345
        # Commit is failed
346
        croak($commit_error) if $commit_error;
347
    }
348
}
349

            
version 0.0901
yuki-kimoto authored on 2009-12-17
350
sub create_table {
351
    my ($self, $table, @column_definitions) = @_;
352
    
353
    # Create table
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
354
    my $sql = "create table $table (";
version 0.0901
yuki-kimoto authored on 2009-12-17
355
    
356
    # Column definitions
357
    foreach my $column_definition (@column_definitions) {
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
358
        $sql .= "$column_definition,";
version 0.0901
yuki-kimoto authored on 2009-12-17
359
    }
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
360
    $sql =~ s/,$//;
version 0.0901
yuki-kimoto authored on 2009-12-17
361
    
362
    # End
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
363
    $sql .= ");";
version 0.0901
yuki-kimoto authored on 2009-12-17
364
    
365
    # Do query
366
    return $self->do($sql);
367
}
368

            
369
sub drop_table {
370
    my ($self, $table) = @_;
371
    
372
    # Drop table
373
    my $sql = "drop table $table;";
374

            
375
    # Do query
376
    return $self->do($sql);
377
}
378

            
many many changes
yuki-kimoto authored on 2010-04-30
379
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/append filter/;
cleanup insert
yuki-kimoto authored on 2010-04-28
380

            
packaging one directory
yuki-kimoto authored on 2009-11-16
381
sub insert {
cleanup insert
yuki-kimoto authored on 2010-04-28
382
    my ($self, $table, $insert_params, $args) = @_;
383
    
384
    # Table
385
    $table ||= '';
386
    
387
    # Insert params
388
    $insert_params ||= {};
389
    
390
    # Arguments
391
    $args ||= {};
392
    
393
    # Check arguments
394
    foreach my $name (keys %$args) {
395
        croak "\"$name\" is invalid name"
396
          unless $VALID_INSERT_ARGS{$name};
397
    }
398
    
399
    my $append_statement = $args->{append} || '';
many many changes
yuki-kimoto authored on 2010-04-30
400
    my $filter           = $args->{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
401
    
402
    # Insert keys
403
    my @insert_keys = keys %$insert_params;
404
    
405
    # Not exists insert keys
406
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
407
      unless @insert_keys;
408
    
409
    # Templte for insert
410
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
411
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
412
    
413
    # Execute query
many many changes
yuki-kimoto authored on 2010-04-30
414
    my $ret_val = $self->query($template, $insert_params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
415
    
416
    return $ret_val;
417
}
418

            
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
419
our %VALID_UPDATE_ARGS
many many changes
yuki-kimoto authored on 2010-04-30
420
  = map { $_ => 1 } qw/where append filter allow_update_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
421

            
packaging one directory
yuki-kimoto authored on 2009-11-16
422
sub update {
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
423
    my ($self, $table, $update_params, $args) = @_;
424
    
425
    # Check arguments
426
    foreach my $name (keys %$args) {
427
        croak "\"$name\" is invalid name"
428
          unless $VALID_UPDATE_ARGS{$name};
429
    }
430
    
431
    # Arguments
432
    my $where_params     = $args->{where} || {};
433
    my $append_statement = $args->{append} || '';
many many changes
yuki-kimoto authored on 2010-04-30
434
    my $filter           = $args->{filter};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
435
    my $allow_update_all = $args->{allow_update_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
436
    
437
    # Update keys
438
    my @update_keys = keys %$update_params;
439
    
440
    # Not exists update kyes
441
    croak("Key-value pairs for update must be specified to 'update' second argument")
442
      unless @update_keys;
443
    
444
    # Where keys
445
    my @where_keys = keys %$where_params;
446
    
447
    # Not exists where keys
448
    croak("Key-value pairs for where clause must be specified to 'update' third argument")
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
449
      if !@where_keys && !$allow_update_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
450
    
451
    # Update clause
452
    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
453
    
454
    # Where clause
455
    my $where_clause = '';
many change
yuki-kimoto authored on 2010-04-30
456
    my $new_where_params = {};
457
    
packaging one directory
yuki-kimoto authored on 2009-11-16
458
    if (@where_keys) {
459
        $where_clause = 'where ';
460
        foreach my $where_key (@where_keys) {
many change
yuki-kimoto authored on 2010-04-30
461
            $new_where_params->{"$where_key@where"}
462
              = $where_params->{$where_key};
many change
yuki-kimoto authored on 2010-02-11
463

            
many change
yuki-kimoto authored on 2010-04-30
464
            $where_clause .= "{= $where_key@where} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
465
        }
466
        $where_clause =~ s/ and $//;
467
    }
468
    
469
    # Template for update
470
    my $template = "update $table $update_clause $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
471
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
472
    
473
    # Rearrange parammeters
many change
yuki-kimoto authored on 2010-04-30
474
    my $params = {%$update_params, %$new_where_params};
packaging one directory
yuki-kimoto authored on 2009-11-16
475
    
476
    # Execute query
many many changes
yuki-kimoto authored on 2010-04-30
477
    my $ret_val = $self->query($template, $params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
478
    
479
    return $ret_val;
480
}
481

            
482
sub update_all {
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
483
    my ($self, $table, $update_params, $args) = @_;
484
    
refactoring select
yuki-kimoto authored on 2010-04-28
485
    # Allow all update
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
486
    $args ||= {};
487
    $args->{allow_update_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
488
    
update document
yuki-kimoto authored on 2010-01-30
489
    # Update all rows
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
490
    return $self->update($table, $update_params, $args);
packaging one directory
yuki-kimoto authored on 2009-11-16
491
}
492

            
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
493
our %VALID_DELETE_ARGS
many many changes
yuki-kimoto authored on 2010-04-30
494
  = map { $_ => 1 } qw/where append filter allow_delete_all/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
495

            
packaging one directory
yuki-kimoto authored on 2009-11-16
496
sub delete {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
497
    my ($self, $table, $args) = @_;
498
    
499
    # Table
500
    $table            ||= '';
501

            
502
    # Check arguments
503
    foreach my $name (keys %$args) {
504
        croak "\"$name\" is invalid name"
505
          unless $VALID_DELETE_ARGS{$name};
506
    }
507
    
508
    # Arguments
509
    my $where_params     = $args->{where} || {};
510
    my $append_statement = $args->{append};
many many changes
yuki-kimoto authored on 2010-04-30
511
    my $filter    = $args->{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
512
    my $allow_delete_all = $args->{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
513
    
514
    # Where keys
515
    my @where_keys = keys %$where_params;
516
    
517
    # Not exists where keys
518
    croak("Key-value pairs for where clause must be specified to 'delete' second argument")
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
519
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
520
    
521
    # Where clause
522
    my $where_clause = '';
523
    if (@where_keys) {
524
        $where_clause = 'where ';
525
        foreach my $where_key (@where_keys) {
526
            $where_clause .= "{= $where_key} and ";
527
        }
528
        $where_clause =~ s/ and $//;
529
    }
530
    
531
    # Template for delete
532
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
533
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
534
    
535
    # Execute query
many many changes
yuki-kimoto authored on 2010-04-30
536
    my $ret_val = $self->query($template, $where_params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
537
    
538
    return $ret_val;
539
}
540

            
541
sub delete_all {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
542
    my ($self, $table, $args) = @_;
543
    
refactoring select
yuki-kimoto authored on 2010-04-28
544
    # Allow all delete
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
545
    $args ||= {};
546
    $args->{allow_delete_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
547
    
update document
yuki-kimoto authored on 2010-01-30
548
    # Delete all rows
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
549
    return $self->delete($table, $args);
packaging one directory
yuki-kimoto authored on 2009-11-16
550
}
551

            
refactoring select
yuki-kimoto authored on 2010-04-28
552
our %VALID_SELECT_ARGS
many many changes
yuki-kimoto authored on 2010-04-30
553
  = map { $_ => 1 } qw/columns where append filter/;
refactoring select
yuki-kimoto authored on 2010-04-28
554

            
packaging one directory
yuki-kimoto authored on 2009-11-16
555
sub select {
refactoring select
yuki-kimoto authored on 2010-04-28
556
    my ($self, $tables, $args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
557
    
refactoring select
yuki-kimoto authored on 2010-04-28
558
    # Table
559
    $tables ||= '';
560
    $tables = [$tables] unless ref $tables;
packaging one directory
yuki-kimoto authored on 2009-11-16
561
    
refactoring select
yuki-kimoto authored on 2010-04-28
562
    # Check arguments
563
    foreach my $name (keys %$args) {
564
        croak "\"$name\" is invalid name"
565
          unless $VALID_SELECT_ARGS{$name};
566
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
567
    
refactoring select
yuki-kimoto authored on 2010-04-28
568
    # Arguments
569
    my $columns          = $args->{columns} || [];
570
    my $where_params     = $args->{where} || {};
571
    my $append_statement = $args->{append} || '';
many many changes
yuki-kimoto authored on 2010-04-30
572
    my $filter    = $args->{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
573
    
574
    # SQL template for select statement
575
    my $template = 'select ';
576
    
577
    # Join column clause
578
    if (@$columns) {
579
        foreach my $column (@$columns) {
580
            $template .= "$column, ";
581
        }
582
        $template =~ s/, $/ /;
583
    }
584
    else {
585
        $template .= '* ';
586
    }
587
    
588
    # Join table
589
    $template .= 'from ';
590
    foreach my $table (@$tables) {
591
        $template .= "$table, ";
592
    }
593
    $template =~ s/, $/ /;
594
    
595
    # Where clause keys
596
    my @where_keys = keys %$where_params;
597
    
many change
yuki-kimoto authored on 2010-02-11
598
    my $where_params_new = {};
599
    
packaging one directory
yuki-kimoto authored on 2009-11-16
600
    # Join where clause
601
    if (@where_keys) {
602
        $template .= 'where ';
603
        foreach my $where_key (@where_keys) {
many change
yuki-kimoto authored on 2010-02-11
604
            my $key_info = DBIx::Custom::KeyInfo->new($where_key);
605
            
606
            my $table_new = $key_info->table || $tables->[0];
607
            my $column = $table_new . '.' . $key_info->column
608
                         . '#' . $table_new;
609
                      
610
            $template .= "{= $column} and ";
611
            
612
            $where_params_new->{$table_new} ||= {};
613
            $where_params_new->{$table_new}->{$key_info->column}
614
              = $where_params->{$where_key};
packaging one directory
yuki-kimoto authored on 2009-11-16
615
        }
616
    }
617
    $template =~ s/ and $//;
618
    
619
    # Append something to last of statement
620
    if ($append_statement =~ s/^where //) {
621
        if (@where_keys) {
622
            $template .= " and $append_statement";
623
        }
624
        else {
625
            $template .= " where $append_statement";
626
        }
627
    }
628
    else {
629
        $template .= " $append_statement";
630
    }
631
    
632
    # Execute query
many many changes
yuki-kimoto authored on 2010-04-30
633
    my $result = $self->query($template, $where_params_new, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
634
    
635
    return $result;
636
}
637

            
638
sub _add_query_cache {
639
    my ($class, $template, $query) = @_;
update document
yuki-kimoto authored on 2010-01-30
640
    
641
    # Query information
packaging one directory
yuki-kimoto authored on 2009-11-16
642
    my $query_cache_keys = $class->_query_cache_keys;
643
    my $query_caches     = $class->_query_caches;
644
    
update document
yuki-kimoto authored on 2010-01-30
645
    # Already cached
packaging one directory
yuki-kimoto authored on 2009-11-16
646
    return $class if $query_caches->{$template};
647
    
update document
yuki-kimoto authored on 2010-01-30
648
    # Cache
packaging one directory
yuki-kimoto authored on 2009-11-16
649
    $query_caches->{$template} = $query;
650
    push @$query_cache_keys, $template;
651
    
update document
yuki-kimoto authored on 2010-01-30
652
    # Check cache overflow
packaging one directory
yuki-kimoto authored on 2009-11-16
653
    my $overflow = @$query_cache_keys - $class->query_cache_max;
654
    for (my $i = 0; $i < $overflow; $i++) {
655
        my $template = shift @$query_cache_keys;
656
        delete $query_caches->{$template};
657
    }
658
    
659
    return $class;
660
}
661

            
662
=head1 NAME
663

            
version 0.0901
yuki-kimoto authored on 2009-12-17
664
DBIx::Custom - Customizable DBI
packaging one directory
yuki-kimoto authored on 2009-11-16
665

            
version 0.0901
yuki-kimoto authored on 2009-12-17
666
=head1 VERSION
packaging one directory
yuki-kimoto authored on 2009-11-16
667

            
many change
yuki-kimoto authored on 2010-02-11
668
Version 0.1201
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
669

            
670
=cut
671

            
many change
yuki-kimoto authored on 2010-02-11
672
our $VERSION = '0.1201';
packaging one directory
yuki-kimoto authored on 2009-11-16
673

            
cleanup
yuki-kimoto authored on 2010-02-11
674
=head1 STATE
675

            
676
This module is not stable. Method name and functionality will be change.
677

            
version 0.0901
yuki-kimoto authored on 2009-12-17
678
=head1 SYNOPSYS
679
    
680
    # New
681
    my $dbi = DBIx::Custom->new(data_source => "dbi:mysql:database=books"
682
                                user => 'ken', password => '!LFKD%$&');
683
    
684
    # Query
685
    $dbi->query("select title from books");
686
    
687
    # Query with parameters
688
    $dbi->query("select id from books where {= author} && {like title}",
689
                {author => 'ken', title => '%Perl%'});
690
    
691
    # Insert 
692
    $dbi->insert('books', {title => 'perl', author => 'Ken'});
693
    
694
    # Update 
695
    $dbi->update('books', {title => 'aaa', author => 'Ken'}, {id => 5});
696
    
697
    # Delete
698
    $dbi->delete('books', {author => 'Ken'});
699
    
700
    # Select
701
    $dbi->select('books');
702
    $dbi->select('books', {author => 'taro'}); 
703
    $dbi->select('books', [qw/author title/], {author => 'Ken'});
704
    $dbi->select('books', [qw/author title/], {author => 'Ken'},
705
                 'order by id limit 1');
packaging one directory
yuki-kimoto authored on 2009-11-16
706

            
update document
yuki-kimoto authored on 2010-01-30
707
=head1 ATTRIBUTES
packaging one directory
yuki-kimoto authored on 2009-11-16
708

            
709
=head2 user
710

            
update document
yuki-kimoto authored on 2010-01-30
711
Database user name
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
712
    
version 0.0901
yuki-kimoto authored on 2009-12-17
713
    $dbi  = $dbi->user('Ken');
714
    $user = $dbi->user;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
715
    
packaging one directory
yuki-kimoto authored on 2009-11-16
716
=head2 password
717

            
update document
yuki-kimoto authored on 2010-01-30
718
Database password
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
719
    
version 0.0901
yuki-kimoto authored on 2009-12-17
720
    $dbi      = $dbi->password('lkj&le`@s');
721
    $password = $dbi->password;
packaging one directory
yuki-kimoto authored on 2009-11-16
722

            
723
=head2 data_source
724

            
update document
yuki-kimoto authored on 2010-01-30
725
Database data source
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
726
    
version 0.0901
yuki-kimoto authored on 2009-12-17
727
    $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
728
    $data_source = $dbi->data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
729
    
version 0.0901
yuki-kimoto authored on 2009-12-17
730
If you know data source more, See also L<DBI>.
731

            
packaging one directory
yuki-kimoto authored on 2009-11-16
732
=head2 database
733

            
update document
yuki-kimoto authored on 2010-01-30
734
Database name
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
735

            
version 0.0901
yuki-kimoto authored on 2009-12-17
736
    $dbi      = $dbi->database('books');
737
    $database = $dbi->database;
packaging one directory
yuki-kimoto authored on 2009-11-16
738

            
add port and host method
yuki-kimoto authored on 2009-11-16
739
=head2 host
740

            
update document
yuki-kimoto authored on 2010-01-30
741
Host name
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
742

            
version 0.0901
yuki-kimoto authored on 2009-12-17
743
    $dbi  = $dbi->host('somehost.com');
744
    $host = $dbi->host;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
745

            
version 0.0901
yuki-kimoto authored on 2009-12-17
746
You can also set IP address like '127.03.45.12'.
add port and host method
yuki-kimoto authored on 2009-11-16
747

            
748
=head2 port
749

            
update document
yuki-kimoto authored on 2010-01-30
750
Port number
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
751

            
version 0.0901
yuki-kimoto authored on 2009-12-17
752
    $dbi  = $dbi->port(1198);
753
    $port = $dbi->port;
add port and host method
yuki-kimoto authored on 2009-11-16
754

            
version 0.0901
yuki-kimoto authored on 2009-12-17
755
=head2 options
packaging one directory
yuki-kimoto authored on 2009-11-16
756

            
update document
yuki-kimoto authored on 2010-01-30
757
DBI options
packaging one directory
yuki-kimoto authored on 2009-11-16
758

            
version 0.0901
yuki-kimoto authored on 2009-12-17
759
    $dbi     = $dbi->options({PrintError => 0, RaiseError => 1});
760
    $options = $dbi->options;
packaging one directory
yuki-kimoto authored on 2009-11-16
761

            
version 0.0901
yuki-kimoto authored on 2009-12-17
762
=head2 sql_tmpl
packaging one directory
yuki-kimoto authored on 2009-11-16
763

            
many many changes
yuki-kimoto authored on 2010-04-30
764
SQLTemplate object
packaging one directory
yuki-kimoto authored on 2009-11-16
765

            
many many changes
yuki-kimoto authored on 2010-04-30
766
    $dbi      = $dbi->sql_tmpl(DBIx::Cutom::SQLTemplate->new);
version 0.0901
yuki-kimoto authored on 2009-12-17
767
    $sql_tmpl = $dbi->sql_tmpl;
packaging one directory
yuki-kimoto authored on 2009-11-16
768

            
many many changes
yuki-kimoto authored on 2010-04-30
769
See also L<DBIx::Custom::SQLTemplate>.
packaging one directory
yuki-kimoto authored on 2009-11-16
770

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
771
=head2 filters
packaging one directory
yuki-kimoto authored on 2009-11-16
772

            
update document
yuki-kimoto authored on 2010-01-30
773
Filters
packaging one directory
yuki-kimoto authored on 2009-11-16
774

            
version 0.0901
yuki-kimoto authored on 2009-12-17
775
    $dbi     = $dbi->filters({filter1 => sub { }, filter2 => sub {}});
776
    $filters = $dbi->filters;
packaging one directory
yuki-kimoto authored on 2009-11-16
777
    
version 0.0901
yuki-kimoto authored on 2009-12-17
778
This method is generally used to get a filter.
779

            
780
    $filter = $dbi->filters->{encode_utf8};
781

            
many many changes
yuki-kimoto authored on 2010-04-30
782
If you add filter, use resist_filter method.
packaging one directory
yuki-kimoto authored on 2009-11-16
783

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
784
=head2 formats
packaging one directory
yuki-kimoto authored on 2009-11-16
785

            
update document
yuki-kimoto authored on 2010-01-30
786
Formats
packaging one directory
yuki-kimoto authored on 2009-11-16
787

            
version 0.0901
yuki-kimoto authored on 2009-12-17
788
    $dbi     = $dbi->formats({format1 => sub { }, format2 => sub {}});
789
    $formats = $dbi->formats;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
790

            
version 0.0901
yuki-kimoto authored on 2009-12-17
791
This method is generally used to get a format.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
792

            
version 0.0901
yuki-kimoto authored on 2009-12-17
793
    $filter = $dbi->formats->{datetime};
794

            
many many changes
yuki-kimoto authored on 2010-04-30
795
If you add format, use resist_format method.
packaging one directory
yuki-kimoto authored on 2009-11-16
796

            
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
797
=head2 default_query_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
798

            
update document
yuki-kimoto authored on 2010-01-30
799
Binding filter
packaging one directory
yuki-kimoto authored on 2009-11-16
800

            
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
801
    $dbi                 = $dbi->default_query_filter($default_query_filter);
802
    $default_query_filter = $dbi->default_query_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
803

            
version 0.0901
yuki-kimoto authored on 2009-12-17
804
The following is bind filter sample
cleanup
yuki-kimoto authored on 2010-04-28
805
    
many many changes
yuki-kimoto authored on 2010-04-30
806
    $dbi->resist_filter(encode_utf8 => sub {
cleanup
yuki-kimoto authored on 2010-04-28
807
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
808
        
cleanup
yuki-kimoto authored on 2010-04-28
809
        require Encode 'encode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
810
        
cleanup
yuki-kimoto authored on 2010-04-28
811
        return encode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
812
    });
cleanup
yuki-kimoto authored on 2010-04-28
813
    
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
814
    $dbi->default_query_filter('encode_utf8')
packaging one directory
yuki-kimoto authored on 2009-11-16
815

            
version 0.0901
yuki-kimoto authored on 2009-12-17
816
Bind filter arguemts is
817

            
818
    1. $value : Value
819
    2. $key   : Key
820
    3. $dbi   : DBIx::Custom object
821
    4. $infos : {table => $table, column => $column}
822

            
cleanup
yuki-kimoto authored on 2010-04-28
823
=head2 default_fetch_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
824

            
update document
yuki-kimoto authored on 2010-01-30
825
Fetching filter
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
826

            
cleanup
yuki-kimoto authored on 2010-04-28
827
    $dbi                  = $dbi->default_fetch_filter($default_fetch_filter);
828
    $default_fetch_filter = $dbi->default_fetch_filter;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
829

            
version 0.0901
yuki-kimoto authored on 2009-12-17
830
The following is fetch filter sample
packaging one directory
yuki-kimoto authored on 2009-11-16
831

            
many many changes
yuki-kimoto authored on 2010-04-30
832
    $dbi->resist_filter(decode_utf8 => sub {
cleanup
yuki-kimoto authored on 2010-04-28
833
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
834
        
cleanup
yuki-kimoto authored on 2010-04-28
835
        require Encode 'decode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
836
        
cleanup
yuki-kimoto authored on 2010-04-28
837
        return decode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
838
    });
packaging one directory
yuki-kimoto authored on 2009-11-16
839

            
cleanup
yuki-kimoto authored on 2010-04-28
840
    $dbi->default_fetch_filter('decode_utf8');
841

            
version 0.0901
yuki-kimoto authored on 2009-12-17
842
Bind filter arguemts is
843

            
844
    1. $value : Value
845
    2. $key   : Key
846
    3. $dbi   : DBIx::Custom object
847
    4. $infos : {type => $table, sth => $sth, index => $index}
848

            
packaging one directory
yuki-kimoto authored on 2009-11-16
849
=head2 result_class
850

            
update document
yuki-kimoto authored on 2010-01-30
851
Resultset class
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
852

            
version 0.0901
yuki-kimoto authored on 2009-12-17
853
    $dbi          = $dbi->result_class('DBIx::Custom::Result');
packaging one directory
yuki-kimoto authored on 2009-11-16
854
    $result_class = $dbi->result_class;
855

            
update document
yuki-kimoto authored on 2010-01-30
856
Default is L<DBIx::Custom::Result>
857

            
packaging one directory
yuki-kimoto authored on 2009-11-16
858
=head2 dbh
859

            
update document
yuki-kimoto authored on 2010-01-30
860
Database handle
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
861
    
version 0.0901
yuki-kimoto authored on 2009-12-17
862
    $dbi = $dbi->dbh($dbh);
863
    $dbh = $dbi->dbh;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
864
    
865
=head2 query_cache_max
866

            
update document
yuki-kimoto authored on 2010-01-30
867
Query cache max
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
868

            
version 0.0901
yuki-kimoto authored on 2009-12-17
869
    $class           = DBIx::Custom->query_cache_max(50);
870
    $query_cache_max = DBIx::Custom->query_cache_max;
871

            
872
Default value is 50
873

            
update document
yuki-kimoto authored on 2010-01-30
874
=head1 METHODS
875

            
876
This class is L<Object::Simple> subclass.
877
You can use all methods of L<Object::Simple>
packaging one directory
yuki-kimoto authored on 2009-11-16
878

            
879
=head2 connect
880

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
881
Connect to database
882

            
packaging one directory
yuki-kimoto authored on 2009-11-16
883
    $dbi->connect;
884

            
885
=head2 disconnect
886

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
887
Disconnect database
888

            
packaging one directory
yuki-kimoto authored on 2009-11-16
889
    $dbi->disconnect;
890

            
version 0.0901
yuki-kimoto authored on 2009-12-17
891
If database is already disconnected, this method do nothing.
packaging one directory
yuki-kimoto authored on 2009-11-16
892

            
893
=head2 reconnect
894

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
895
Reconnect to database
896

            
packaging one directory
yuki-kimoto authored on 2009-11-16
897
    $dbi->reconnect;
898

            
899
=head2 connected
900

            
version 0.0901
yuki-kimoto authored on 2009-12-17
901
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
902
    
version 0.0901
yuki-kimoto authored on 2009-12-17
903
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
904
    
many many changes
yuki-kimoto authored on 2010-04-30
905
=head2 resist_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
906

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
907
Resist filter
packaging one directory
yuki-kimoto authored on 2009-11-16
908
    
many many changes
yuki-kimoto authored on 2010-04-30
909
    $dbi->resist_filter($fname1 => $filter1, $fname => $filter2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
910
    
many many changes
yuki-kimoto authored on 2010-04-30
911
The following is resist_filter sample
version 0.0901
yuki-kimoto authored on 2009-12-17
912

            
many many changes
yuki-kimoto authored on 2010-04-30
913
    $dbi->resist_filter(
packaging one directory
yuki-kimoto authored on 2009-11-16
914
        encode_utf8 => sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
915
            my ($value, $key, $dbi, $infos) = @_;
916
            utf8::upgrade($value) unless Encode::is_utf8($value);
917
            return encode('UTF-8', $value);
packaging one directory
yuki-kimoto authored on 2009-11-16
918
        },
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
919
        decode_utf8 => sub {
920
            my ($value, $key, $dbi, $infos) = @_;
921
            return decode('UTF-8', $value)
packaging one directory
yuki-kimoto authored on 2009-11-16
922
        }
923
    );
924

            
many many changes
yuki-kimoto authored on 2010-04-30
925
=head2 resist_format
packaging one directory
yuki-kimoto authored on 2009-11-16
926

            
version 0.0901
yuki-kimoto authored on 2009-12-17
927
Add format
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
928

            
many many changes
yuki-kimoto authored on 2010-04-30
929
    $dbi->resist_format($fname1 => $format, $fname2 => $format2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
930
    
many many changes
yuki-kimoto authored on 2010-04-30
931
The following is resist_format sample.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
932

            
many many changes
yuki-kimoto authored on 2010-04-30
933
    $dbi->resist_format(date => '%Y:%m:%d', datetime => '%Y-%m-%d %H:%M:%S');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
934

            
version 0.0901
yuki-kimoto authored on 2009-12-17
935
=head2 create_query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
936
    
version 0.0901
yuki-kimoto authored on 2009-12-17
937
Create Query object parsing SQL template
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
938

            
version 0.0901
yuki-kimoto authored on 2009-12-17
939
    my $query = $dbi->create_query("select * from authors where {= name} and {= age}");
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
940

            
version 0.0901
yuki-kimoto authored on 2009-12-17
941
$query is <DBIx::Query> object. This is executed by query method as the following
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
942

            
version 0.0901
yuki-kimoto authored on 2009-12-17
943
    $dbi->query($query, $params);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
944

            
many many changes
yuki-kimoto authored on 2010-04-30
945
If you know SQL template, see also L<DBIx::Custom::SQLTemplate>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
946

            
version 0.0901
yuki-kimoto authored on 2009-12-17
947
=head2 query
packaging one directory
yuki-kimoto authored on 2009-11-16
948

            
version 0.0901
yuki-kimoto authored on 2009-12-17
949
Query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
950

            
version 0.0901
yuki-kimoto authored on 2009-12-17
951
    $result = $dbi->query($template, $params);
packaging one directory
yuki-kimoto authored on 2009-11-16
952

            
version 0.0901
yuki-kimoto authored on 2009-12-17
953
The following is query sample
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
954

            
packaging one directory
yuki-kimoto authored on 2009-11-16
955
    $result = $dbi->query("select * from authors where {= name} and {= age}", 
956
                          {author => 'taro', age => 19});
957
    
958
    while (my @row = $result->fetch) {
959
        # do something
960
    }
961

            
many many changes
yuki-kimoto authored on 2010-04-30
962
If you now syntax of template, See also L<DBIx::Custom::SQLTemplate>
version 0.0901
yuki-kimoto authored on 2009-12-17
963

            
964
Return value of query method is L<DBIx::Custom::Result> object
965

            
966
See also L<DBIx::Custom::Result>.
packaging one directory
yuki-kimoto authored on 2009-11-16
967

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
968
=head2 transaction
packaging one directory
yuki-kimoto authored on 2009-11-16
969

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
970
Get L<DBIx::Custom::Transaction> object, and you run a transaction.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
971

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
972
    $dbi->transaction->run(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
973
        my $dbi = shift;
974
        
packaging one directory
yuki-kimoto authored on 2009-11-16
975
        # do something
976
    });
977

            
978
If transaction is success, commit is execute. 
979
If tranzation is died, rollback is execute.
980

            
version 0.0901
yuki-kimoto authored on 2009-12-17
981
=head2 create_table
982

            
983
Create table
984

            
985
    $dbi->create_table(
986
        'books',
987
        'name char(255)',
988
        'age  int'
989
    );
990

            
991
First argument is table name. Rest arguments is column definition.
992

            
993
=head2 drop_table
994

            
995
Drop table
996

            
997
    $dbi->drop_table('books');
998

            
packaging one directory
yuki-kimoto authored on 2009-11-16
999
=head2 insert
1000

            
update document
yuki-kimoto authored on 2009-11-19
1001
Insert row
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1002

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1003
    $affected = $dbi->insert($table, \%$insert_params);
1004
    $affected = $dbi->insert($table, \%$insert_params, $append);
update document
yuki-kimoto authored on 2009-11-19
1005

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1006
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
1007
    
version 0.0901
yuki-kimoto authored on 2009-12-17
1008
The following is insert sample.
1009

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1010
    $dbi->insert('books', {title => 'Perl', author => 'Taro'});
1011

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1012
You can add statement.
1013

            
1014
    $dbi->insert('books', {title => 'Perl', author => 'Taro'}, "some statement");
1015

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1016
=head2 update
1017

            
update document
yuki-kimoto authored on 2009-11-19
1018
Update rows
1019

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1020
    $affected = $dbi->update($table, \%update_params, \%where);
1021
    $affected = $dbi->update($table, \%update_params, \%where, $append);
1022

            
1023
Retrun value is affected rows count
update document
yuki-kimoto authored on 2009-11-19
1024

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1025
The following is update sample.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1026

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1027
    $dbi->update('books', {title => 'Perl', author => 'Taro'}, {id => 5});
1028

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1029
You can add statement.
1030

            
1031
    $dbi->update('books', {title => 'Perl', author => 'Taro'},
1032
                 {id => 5}, "some statement");
1033

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1034
=head2 update_all
1035

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1036
Update all rows
1037

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1038
    $affected = $dbi->update_all($table, \%updat_params);
update document
yuki-kimoto authored on 2009-11-19
1039

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1040
Retrun value is affected rows count
1041

            
1042
The following is update_all sample.
update document
yuki-kimoto authored on 2009-11-19
1043

            
1044
    $dbi->update_all('books', {author => 'taro'});
packaging one directory
yuki-kimoto authored on 2009-11-16
1045

            
1046
=head2 delete
1047

            
update document
yuki-kimoto authored on 2009-11-19
1048
Delete rows
1049

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1050
    $affected = $dbi->delete($table, \%where);
1051
    $affected = $dbi->delete($table, \%where, $append);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1052

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1053
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
1054
    
version 0.0901
yuki-kimoto authored on 2009-12-17
1055
The following is delete sample.
1056

            
update document
yuki-kimoto authored on 2009-11-19
1057
    $dbi->delete('books', {id => 5});
packaging one directory
yuki-kimoto authored on 2009-11-16
1058

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1059
You can add statement.
1060

            
1061
    $dbi->delete('books', {id => 5}, "some statement");
1062

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1063
=head2 delete_all
1064

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1065
Delete all rows
1066

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1067
    $affected = $dbi->delete_all($table);
packaging one directory
yuki-kimoto authored on 2009-11-16
1068

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1069
Retrun value is affected rows count
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1070

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1071
The following is delete_all sample.
1072

            
1073
    $dbi->delete_all('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1074

            
1075
=head2 select
1076
    
update document
yuki-kimoto authored on 2009-11-19
1077
Select rows
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1078

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1079
    $resut = $dbi->select(
packaging one directory
yuki-kimoto authored on 2009-11-16
1080
        $table,                # must be string or array;
version 0.0901
yuki-kimoto authored on 2009-12-17
1081
        \@$columns,            # must be array reference. this can be ommited
1082
        \%$where_params,       # must be hash reference.  this can be ommited
1083
        $append_statement,     # must be string.          this can be ommited
1084
        $query_edit_callback   # must be code reference.  this can be ommited
packaging one directory
yuki-kimoto authored on 2009-11-16
1085
    );
update document
yuki-kimoto authored on 2009-11-19
1086

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1087
$reslt is L<DBIx::Custom::Result> object
update document
yuki-kimoto authored on 2009-11-19
1088

            
1089
The following is some select samples
1090

            
1091
    # select * from books;
1092
    $result = $dbi->select('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1093
    
update document
yuki-kimoto authored on 2009-11-19
1094
    # select * from books where title = 'Perl';
1095
    $result = $dbi->select('books', {title => 1});
1096
    
1097
    # select title, author from books where id = 1 for update;
1098
    $result = $dbi->select(
1099
        'books',              # table
1100
        ['title', 'author'],  # columns
1101
        {id => 1},            # where clause
1102
        'for update',         # append statement
1103
    );
1104

            
1105
You can join multi tables
1106
    
1107
    $result = $dbi->select(
1108
        ['table1', 'table2'],                # tables
1109
        ['table1.id as table1_id', 'title'], # columns (alias is ok)
1110
        {table1.id => 1},                    # where clase
1111
        "where table1.id = table2.id",       # join clause (must start 'where')
1112
    );
1113

            
1114
You can also edit query
1115
        
packaging one directory
yuki-kimoto authored on 2009-11-16
1116
    $dbi->select(
update document
yuki-kimoto authored on 2009-11-19
1117
        'books',
1118
        # column, where clause, append statement,
packaging one directory
yuki-kimoto authored on 2009-11-16
1119
        sub {
1120
            my $query = shift;
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
1121
            $query->query_filter(sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
1122
                # ...
1123
            });
1124
        }
update document
yuki-kimoto authored on 2009-11-19
1125
    }
1126

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1127
=head2 prepare
1128

            
1129
Prepare statement handle.
1130

            
1131
    $sth = $dbi->prepare('select * from books;');
1132

            
1133
This method is same as DBI prepare method.
1134

            
1135
See also L<DBI>.
1136

            
1137
=head2 do
1138

            
1139
Execute SQL
1140

            
1141
    $affected = $dbi->do('insert into books (title, author) values (?, ?)',
1142
                        'Perl', 'taro');
1143

            
1144
Retrun value is affected rows count.
1145

            
1146
This method is same as DBI do method.
1147

            
1148
See also L<DBI>
1149

            
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
1150
=head2 run_transaction
1151

            
1152

            
1153

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1154
=head1 DBIx::Custom default configuration
packaging one directory
yuki-kimoto authored on 2009-11-16
1155

            
update document
yuki-kimoto authored on 2009-11-19
1156
DBIx::Custom have DBI object.
packaging one directory
yuki-kimoto authored on 2009-11-16
1157
This module is work well in the following DBI condition.
1158

            
1159
    1. AutoCommit is true
1160
    2. RaiseError is true
1161

            
1162
By default, Both AutoCommit and RaiseError is true.
1163
You must not change these mode not to damage your data.
1164

            
1165
If you change these mode, 
1166
you cannot get correct error message, 
1167
or run_transaction may fail.
1168

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1169
=head1 Inheritance of DBIx::Custom
1170

            
1171
DBIx::Custom is customizable DBI.
1172
You can inherit DBIx::Custom and custumize attributes.
1173

            
1174
    package DBIx::Custom::Yours;
1175
    use base DBIx::Custom;
1176
    
1177
    my $class = __PACKAGE__;
1178
    
1179
    $class->user('your_name');
1180
    $class->password('your_password');
1181

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1182
=head1 AUTHOR
1183

            
1184
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
1185

            
1186
Github L<http://github.com/yuki-kimoto>
1187

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1188
I develope this module L<http://github.com/yuki-kimoto/DBIx-Custom>
1189

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1190
=head1 COPYRIGHT & LICENSE
1191

            
1192
Copyright 2009 Yuki Kimoto, all rights reserved.
1193

            
1194
This program is free software; you can redistribute it and/or modify it
1195
under the same terms as Perl itself.
1196

            
1197
=cut