DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1198 lines | 27.657kb
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;
many change
yuki-kimoto authored on 2010-02-11
13
use DBIx::Custom::KeyInfo;
packaging one directory
yuki-kimoto authored on 2009-11-16
14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
267
sub _build_bind_values {
many many changes
yuki-kimoto authored on 2010-04-30
268
    my ($self, $query, $params, $filter) = @_;
269
    my $key_infos      = $query->key_infos;
270
    my $default_filter = $self->default_query_filter;
271
    my $filters        = $self->filters;
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 many changes
yuki-kimoto authored on 2010-04-30
285
        $filter = $filters->{$filter} || $filters->{$default_filter};
286
        
Simplify key search
yuki-kimoto authored on 2010-02-11
287
        push @bind_values, 
many many changes
yuki-kimoto authored on 2010-04-30
288
             $filter ? $filter->($value)
Simplify key search
yuki-kimoto authored on 2010-02-11
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 = '';
456
    if (@where_keys) {
457
        $where_clause = 'where ';
458
        foreach my $where_key (@where_keys) {
many change
yuki-kimoto authored on 2010-02-11
459
            my $key_info = DBIx::Custom::KeyInfo->new($where_key);
460
            
461
            my $table_new = $key_info->table || $table;
462
            my $column = $table_new . '.' . $key_info->column
463
                         . '#@where';
464

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

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

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

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

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

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

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

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

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

            
663
=head1 NAME
664

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

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

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

            
671
=cut
672

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

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

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

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

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

            
710
=head2 user
711

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

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

            
724
=head2 data_source
725

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

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

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

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

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

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

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

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

            
749
=head2 port
750

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
873
Default value is 50
874

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

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

            
880
=head2 connect
881

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

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

            
886
=head2 disconnect
887

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

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

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

            
894
=head2 reconnect
895

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

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

            
900
=head2 connected
901

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
984
Create table
985

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

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

            
994
=head2 drop_table
995

            
996
Drop table
997

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1047
=head2 delete
1048

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1090
The following is some select samples
1091

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

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

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

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

            
1130
Prepare statement handle.
1131

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

            
1134
This method is same as DBI prepare method.
1135

            
1136
See also L<DBI>.
1137

            
1138
=head2 do
1139

            
1140
Execute SQL
1141

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

            
1145
Retrun value is affected rows count.
1146

            
1147
This method is same as DBI do method.
1148

            
1149
See also L<DBI>
1150

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

            
1153

            
1154

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1193
Copyright 2009 Yuki Kimoto, all rights reserved.
1194

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

            
1198
=cut