DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1195 lines | 27.19kb
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,
simplify filtering system
yuki-kimoto authored on 2010-05-01
189
            columns => $cached_query->columns
cleanup
yuki-kimoto authored on 2010-02-11
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 $params_dump      = Data::Dumper->Dump([$params], ['*params']);
242
        
243
        croak("$execute_error" . 
244
              "<Your SQL>\n$sql\n" . 
245
              "<Your parameters>\n$params_dump");
246
    }
247
    
248
    # Return resultset if select statement is executed
249
    if ($sth->{NUM_OF_FIELDS}) {
250
        
251
        # Get result class
252
        my $result_class = $self->result_class;
253
        
254
        # Create result
255
        my $result = $result_class->new({
many many changes
yuki-kimoto authored on 2010-04-30
256
            sth             => $sth,
257
            default_filter  => $self->default_fetch_filter,
258
            filters         => $self->filters
packaging one directory
yuki-kimoto authored on 2009-11-16
259
        });
260
        return $result;
261
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
262
    return $affected;
packaging one directory
yuki-kimoto authored on 2009-11-16
263
}
264

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

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

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

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

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

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
423
sub update {
simplify filtering system
yuki-kimoto authored on 2010-05-01
424
    my ($self, $table, $params, $args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
425
    
426
    # Check arguments
427
    foreach my $name (keys %$args) {
428
        croak "\"$name\" is invalid name"
429
          unless $VALID_UPDATE_ARGS{$name};
430
    }
431
    
432
    # Arguments
433
    my $where_params     = $args->{where} || {};
434
    my $append_statement = $args->{append} || '';
many many changes
yuki-kimoto authored on 2010-04-30
435
    my $filter           = $args->{filter};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
436
    my $allow_update_all = $args->{allow_update_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
437
    
438
    # Update keys
simplify filtering system
yuki-kimoto authored on 2010-05-01
439
    my @update_keys = keys %$params;
packaging one directory
yuki-kimoto authored on 2009-11-16
440
    
441
    # Not exists update kyes
442
    croak("Key-value pairs for update must be specified to 'update' second argument")
443
      unless @update_keys;
444
    
445
    # Where keys
446
    my @where_keys = keys %$where_params;
447
    
448
    # Not exists where keys
449
    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
450
      if !@where_keys && !$allow_update_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
451
    
452
    # Update clause
453
    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
454
    
455
    # Where clause
456
    my $where_clause = '';
simplify filtering system
yuki-kimoto authored on 2010-05-01
457
    my $new_where = {};
many change
yuki-kimoto authored on 2010-04-30
458
    
packaging one directory
yuki-kimoto authored on 2009-11-16
459
    if (@where_keys) {
460
        $where_clause = 'where ';
461
        foreach my $where_key (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
462
            
463
            $where_clause .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
464
        }
465
        $where_clause =~ s/ and $//;
466
    }
467
    
468
    # Template for update
469
    my $template = "update $table $update_clause $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
470
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
471
    
472
    # Rearrange parammeters
simplify filtering system
yuki-kimoto authored on 2010-05-01
473
    foreach my $where_key (@where_keys) {
474
        
475
        if (exists $params->{$where_key}) {
476
            $params->{$where_key} = [$params->{$where_key}]
477
              unless ref $params->{$where_key} eq 'ARRAY';
478
            
compile success
yuki-kimoto authored on 2010-05-01
479
            push @{$params->{$where_key}}, $where_params->{$where_key};
simplify filtering system
yuki-kimoto authored on 2010-05-01
480
        }
add tests
yuki-kimoto authored on 2010-05-01
481
        else {
482
            $params->{$where_key} = $where_params->{$where_key};
483
        }
simplify filtering system
yuki-kimoto authored on 2010-05-01
484
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
485
    
486
    # Execute query
many many changes
yuki-kimoto authored on 2010-04-30
487
    my $ret_val = $self->query($template, $params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
488
    
489
    return $ret_val;
490
}
491

            
492
sub update_all {
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
493
    my ($self, $table, $update_params, $args) = @_;
494
    
refactoring select
yuki-kimoto authored on 2010-04-28
495
    # Allow all update
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
496
    $args ||= {};
497
    $args->{allow_update_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
498
    
update document
yuki-kimoto authored on 2010-01-30
499
    # Update all rows
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
500
    return $self->update($table, $update_params, $args);
packaging one directory
yuki-kimoto authored on 2009-11-16
501
}
502

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
506
sub delete {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
507
    my ($self, $table, $args) = @_;
508
    
509
    # Table
510
    $table            ||= '';
511

            
512
    # Check arguments
513
    foreach my $name (keys %$args) {
514
        croak "\"$name\" is invalid name"
515
          unless $VALID_DELETE_ARGS{$name};
516
    }
517
    
518
    # Arguments
519
    my $where_params     = $args->{where} || {};
520
    my $append_statement = $args->{append};
many many changes
yuki-kimoto authored on 2010-04-30
521
    my $filter    = $args->{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
522
    my $allow_delete_all = $args->{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
523
    
524
    # Where keys
525
    my @where_keys = keys %$where_params;
526
    
527
    # Not exists where keys
528
    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
529
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
530
    
531
    # Where clause
532
    my $where_clause = '';
533
    if (@where_keys) {
534
        $where_clause = 'where ';
535
        foreach my $where_key (@where_keys) {
536
            $where_clause .= "{= $where_key} and ";
537
        }
538
        $where_clause =~ s/ and $//;
539
    }
540
    
541
    # Template for delete
542
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
543
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
544
    
545
    # Execute query
many many changes
yuki-kimoto authored on 2010-04-30
546
    my $ret_val = $self->query($template, $where_params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
547
    
548
    return $ret_val;
549
}
550

            
551
sub delete_all {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
552
    my ($self, $table, $args) = @_;
553
    
refactoring select
yuki-kimoto authored on 2010-04-28
554
    # Allow all delete
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
555
    $args ||= {};
556
    $args->{allow_delete_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
557
    
update document
yuki-kimoto authored on 2010-01-30
558
    # Delete all rows
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
559
    return $self->delete($table, $args);
packaging one directory
yuki-kimoto authored on 2009-11-16
560
}
561

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

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

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

            
660
=head1 NAME
661

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

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

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

            
668
=cut
669

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

            
cleanup
yuki-kimoto authored on 2010-02-11
672
=head1 STATE
673

            
674
This module is not stable. Method name and functionality will be change.
675

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

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

            
707
=head2 user
708

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

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

            
721
=head2 data_source
722

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
730
=head2 database
731

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

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

            
add port and host method
yuki-kimoto authored on 2009-11-16
737
=head2 host
738

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

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

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

            
746
=head2 port
747

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

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

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

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

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

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

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

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

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

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

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

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

            
778
    $filter = $dbi->filters->{encode_utf8};
779

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

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

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

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

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

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

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

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
814
Bind filter arguemts is
815

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

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

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

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
840
Bind filter arguemts is
841

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
847
=head2 result_class
848

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
856
=head2 dbh
857

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

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

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

            
870
Default value is 50
871

            
update document
yuki-kimoto authored on 2010-01-30
872
=head1 METHODS
873

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

            
877
=head2 connect
878

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

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

            
883
=head2 disconnect
884

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
885
Disconnect database
886

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

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

            
891
=head2 reconnect
892

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

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

            
897
=head2 connected
898

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

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

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

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

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

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

            
many many changes
yuki-kimoto authored on 2010-04-30
931
    $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
932

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
939
$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
940

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

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

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

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

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

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

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

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

            
962
Return value of query method is L<DBIx::Custom::Result> object
963

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

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

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

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

            
976
If transaction is success, commit is execute. 
977
If tranzation is died, rollback is execute.
978

            
version 0.0901
yuki-kimoto authored on 2009-12-17
979
=head2 create_table
980

            
981
Create table
982

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

            
989
First argument is table name. Rest arguments is column definition.
990

            
991
=head2 drop_table
992

            
993
Drop table
994

            
995
    $dbi->drop_table('books');
996

            
packaging one directory
yuki-kimoto authored on 2009-11-16
997
=head2 insert
998

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1010
You can add statement.
1011

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1014
=head2 update
1015

            
update document
yuki-kimoto authored on 2009-11-19
1016
Update rows
1017

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1027
You can add statement.
1028

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1032
=head2 update_all
1033

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

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

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

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

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

            
1044
=head2 delete
1045

            
update document
yuki-kimoto authored on 2009-11-19
1046
Delete rows
1047

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1057
You can add statement.
1058

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1061
=head2 delete_all
1062

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

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

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

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

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

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

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

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

            
1087
The following is some select samples
1088

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1125
=head2 prepare
1126

            
1127
Prepare statement handle.
1128

            
1129
    $sth = $dbi->prepare('select * from books;');
1130

            
1131
This method is same as DBI prepare method.
1132

            
1133
See also L<DBI>.
1134

            
1135
=head2 do
1136

            
1137
Execute SQL
1138

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

            
1142
Retrun value is affected rows count.
1143

            
1144
This method is same as DBI do method.
1145

            
1146
See also L<DBI>
1147

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

            
1150

            
1151

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

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

            
1157
    1. AutoCommit is true
1158
    2. RaiseError is true
1159

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

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

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

            
1169
DBIx::Custom is customizable DBI.
1170
You can inherit DBIx::Custom and custumize attributes.
1171

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1180
=head1 AUTHOR
1181

            
1182
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
1183

            
1184
Github L<http://github.com/yuki-kimoto>
1185

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

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

            
1190
Copyright 2009 Yuki Kimoto, all rights reserved.
1191

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

            
1195
=cut