DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1233 lines | 29.356kb
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;
11
use DBIx::Custom::SQL::Template;
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/]);
cleanup
yuki-kimoto authored on 2010-04-28
25
__PACKAGE__->attr([qw/default_bind_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');
31
__PACKAGE__->attr(sql_tmpl => sub { DBIx::Custom::SQL::Template->new });
packaging one directory
yuki-kimoto authored on 2009-11-16
32

            
33
sub add_filter {
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

            
43
sub add_format{
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
    my $table = '';
176
    if (ref $template eq 'ARRAY') {
Simplify key search
yuki-kimoto authored on 2010-02-11
177
        $table    = $template->[0] if $template->[0];
cleanup
yuki-kimoto authored on 2010-02-11
178
        $template = $template->[1];
179
    }
180
    
packaging one directory
yuki-kimoto authored on 2009-11-16
181
    # Create query from SQL template
version 0.0901
yuki-kimoto authored on 2009-12-17
182
    my $sql_tmpl = $self->sql_tmpl;
packaging one directory
yuki-kimoto authored on 2009-11-16
183
    
184
    # Try to get cached query
cleanup
yuki-kimoto authored on 2010-02-11
185
    my $cached_query = $class->_query_caches->{"$table$template"};
packaging one directory
yuki-kimoto authored on 2009-11-16
186
    
187
    # Create query
fix timeformat tests
yuki-kimoto authored on 2009-11-23
188
    my $query;
cleanup
yuki-kimoto authored on 2010-02-11
189
    if ($cached_query) {
190
        $query = DBIx::Custom::Query->new(
191
            sql       => $cached_query->sql,
192
            key_infos => $cached_query->key_infos
193
        );
fix timeformat tests
yuki-kimoto authored on 2009-11-23
194
    }
195
    else {
Simplify key search
yuki-kimoto authored on 2010-02-11
196
        $sql_tmpl->table($table);
197
        $query = eval{$sql_tmpl->create_query($template)};
packaging one directory
yuki-kimoto authored on 2009-11-16
198
        croak($@) if $@;
199
        
Simplify key search
yuki-kimoto authored on 2010-02-11
200
        $class->_add_query_cache("$table$template", $query);
packaging one directory
yuki-kimoto authored on 2009-11-16
201
    }
202
    
203
    # Connect if not
204
    $self->connect unless $self->connected;
205
    
206
    # Prepare statement handle
207
    my $sth = $self->prepare($query->{sql});
208
    
209
    # Set statement handle
210
    $query->sth($sth);
211
    
212
    # Set bind filter
cleanup
yuki-kimoto authored on 2010-04-28
213
    $query->bind_filter($self->default_bind_filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
214
    
215
    # Set fetch filter
cleanup
yuki-kimoto authored on 2010-04-28
216
    $query->fetch_filter($self->default_fetch_filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
217
    
218
    return $query;
219
}
220

            
version 0.0901
yuki-kimoto authored on 2009-12-17
221
sub query{
Simplify key search
yuki-kimoto authored on 2010-02-11
222
    my ($self, $query, $params, $query_edit_cb)  = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
223
    $params ||= {};
224
    
225
    # First argument is SQL template
Simplify key search
yuki-kimoto authored on 2010-02-11
226
    unless (ref $query eq 'DBIx::Custom::Query') {
227
        my $table;
228
        my $template;
229
        
230
        if (ref $query eq 'ARRAY') {
231
            $table    = $query->[0];
232
            $template = $query->[1];
233
        }
234
        else { $template = $query }
235
        
236
        $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
237
        $query_edit_cb->($query) if ref $query_edit_cb eq 'CODE';
238
    }
239
    
240
    # Create bind value
241
    my $bind_values = $self->_build_bind_values($query, $params);
242
    
243
    # Execute
version 0.0901
yuki-kimoto authored on 2009-12-17
244
    my $sth      = $query->sth;
245
    my $affected = eval{$sth->execute(@$bind_values)};
packaging one directory
yuki-kimoto authored on 2009-11-16
246
    
247
    # Execute error
248
    if (my $execute_error = $@) {
249
        require Data::Dumper;
250
        my $sql              = $query->{sql} || '';
251
        my $key_infos_dump   = Data::Dumper->Dump([$query->key_infos], ['*key_infos']);
252
        my $params_dump      = Data::Dumper->Dump([$params], ['*params']);
253
        
254
        croak("$execute_error" . 
255
              "<Your SQL>\n$sql\n" . 
256
              "<Your parameters>\n$params_dump");
257
    }
258
    
259
    # Return resultset if select statement is executed
260
    if ($sth->{NUM_OF_FIELDS}) {
261
        
262
        # Get result class
263
        my $result_class = $self->result_class;
264
        
265
        # Create result
266
        my $result = $result_class->new({
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
267
            _dbi             => $self,
packaging one directory
yuki-kimoto authored on 2009-11-16
268
            sth              => $sth,
269
            fetch_filter     => $query->fetch_filter,
270
        });
271
        return $result;
272
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
273
    return $affected;
packaging one directory
yuki-kimoto authored on 2009-11-16
274
}
275

            
276
sub _build_bind_values {
277
    my ($self, $query, $params) = @_;
Simplify key search
yuki-kimoto authored on 2010-02-11
278
    my $key_infos  = $query->key_infos;
279
    my $filter     = $query->bind_filter;
packaging one directory
yuki-kimoto authored on 2009-11-16
280
    
281
    # binding values
282
    my @bind_values;
283
    
Simplify key search
yuki-kimoto authored on 2010-02-11
284
    # Build bind values
packaging one directory
yuki-kimoto authored on 2009-11-16
285
    foreach my $key_info (@$key_infos) {
Simplify key search
yuki-kimoto authored on 2010-02-11
286
        my $table        = $key_info->table;
287
        my $column       = $key_info->column;
288
        my $id           = $key_info->id;
289
        my $pos          = $key_info->pos;
packaging one directory
yuki-kimoto authored on 2009-11-16
290
        
Simplify key search
yuki-kimoto authored on 2010-02-11
291
        # Value
many change
yuki-kimoto authored on 2010-02-11
292
        my $value = $id && defined $pos ? $params->{$id}->{$column}->[$pos]
293
                  : $id                 ? $params->{$id}->{$column}
294
                  : defined $pos        ? $params->{$column}->[$pos]
Simplify key search
yuki-kimoto authored on 2010-02-11
295
                  : $params->{$column};
packaging one directory
yuki-kimoto authored on 2009-11-16
296
        
Simplify key search
yuki-kimoto authored on 2010-02-11
297
        # Filter
298
        push @bind_values, 
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
299
             $filter ? $filter->($value, $table, $column, $self)
Simplify key search
yuki-kimoto authored on 2010-02-11
300
                     : $value;
cleanup
yuki-kimoto authored on 2010-02-11
301
    }
302
    
Simplify key search
yuki-kimoto authored on 2010-02-11
303
    return \@bind_values;
cleanup
yuki-kimoto authored on 2010-02-11
304
}
305

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
361
sub create_table {
362
    my ($self, $table, @column_definitions) = @_;
363
    
364
    # Create table
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
365
    my $sql = "create table $table (";
version 0.0901
yuki-kimoto authored on 2009-12-17
366
    
367
    # Column definitions
368
    foreach my $column_definition (@column_definitions) {
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
369
        $sql .= "$column_definition,";
version 0.0901
yuki-kimoto authored on 2009-12-17
370
    }
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
371
    $sql =~ s/,$//;
version 0.0901
yuki-kimoto authored on 2009-12-17
372
    
373
    # End
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
374
    $sql .= ");";
version 0.0901
yuki-kimoto authored on 2009-12-17
375
    
376
    # Do query
377
    return $self->do($sql);
378
}
379

            
380
sub drop_table {
381
    my ($self, $table) = @_;
382
    
383
    # Drop table
384
    my $sql = "drop table $table;";
385

            
386
    # Do query
387
    return $self->do($sql);
388
}
389

            
cleanup insert
yuki-kimoto authored on 2010-04-28
390
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/append query_edit_cb/;
391

            
packaging one directory
yuki-kimoto authored on 2009-11-16
392
sub insert {
cleanup insert
yuki-kimoto authored on 2010-04-28
393
    my ($self, $table, $insert_params, $args) = @_;
394
    
395
    # Table
396
    $table ||= '';
397
    
398
    # Insert params
399
    $insert_params ||= {};
400
    
401
    # Arguments
402
    $args ||= {};
403
    
404
    # Check arguments
405
    foreach my $name (keys %$args) {
406
        croak "\"$name\" is invalid name"
407
          unless $VALID_INSERT_ARGS{$name};
408
    }
409
    
410
    my $append_statement = $args->{append} || '';
411
    my $query_edit_cb    = $args->{query_edit_cb};
packaging one directory
yuki-kimoto authored on 2009-11-16
412
    
413
    # Insert keys
414
    my @insert_keys = keys %$insert_params;
415
    
416
    # Not exists insert keys
417
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
418
      unless @insert_keys;
419
    
420
    # Templte for insert
421
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
422
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
423
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
424
    my $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
425
    
426
    # Query edit callback must be code reference
427
    croak("Query edit callback must be code reference")
428
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
429
    
430
    # Query edit if need
431
    $query_edit_cb->($query) if $query_edit_cb;
432
    
433
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
434
    my $ret_val = $self->query($query, $insert_params);
packaging one directory
yuki-kimoto authored on 2009-11-16
435
    
436
    return $ret_val;
437
}
438

            
439
sub update {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
440
    my $self             = shift;
441
    my $table            = shift || '';
442
    my $update_params    = shift || {};
443
    my $where_params     = shift || {};
444
    my $append_statement = shift unless ref $_[0];
445
    my $query_edit_cb    = shift;
446
    my $options          = shift;
packaging one directory
yuki-kimoto authored on 2009-11-16
447
    
448
    # Update keys
449
    my @update_keys = keys %$update_params;
450
    
451
    # Not exists update kyes
452
    croak("Key-value pairs for update must be specified to 'update' second argument")
453
      unless @update_keys;
454
    
455
    # Where keys
456
    my @where_keys = keys %$where_params;
457
    
458
    # Not exists where keys
459
    croak("Key-value pairs for where clause must be specified to 'update' third argument")
460
      if !@where_keys && !$options->{allow_update_all};
461
    
462
    # Update clause
463
    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
464
    
465
    # Where clause
466
    my $where_clause = '';
467
    if (@where_keys) {
468
        $where_clause = 'where ';
469
        foreach my $where_key (@where_keys) {
many change
yuki-kimoto authored on 2010-02-11
470
            my $key_info = DBIx::Custom::KeyInfo->new($where_key);
471
            
472
            my $table_new = $key_info->table || $table;
473
            my $column = $table_new . '.' . $key_info->column
474
                         . '#@where';
475

            
476
            $where_clause .= "{= $column} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
477
        }
478
        $where_clause =~ s/ and $//;
479
    }
480
    
481
    # Template for update
482
    my $template = "update $table $update_clause $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
483
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
484
    
485
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
486
    my $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
487
    
488
    # Query edit callback must be code reference
489
    croak("Query edit callback must be code reference")
490
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
491
    
492
    # Query edit if need
493
    $query_edit_cb->($query) if $query_edit_cb;
494
    
495
    # Rearrange parammeters
many change
yuki-kimoto authored on 2010-02-11
496
    my $params = {%$update_params, '@where' => $where_params};
packaging one directory
yuki-kimoto authored on 2009-11-16
497
    
498
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
499
    my $ret_val = $self->query($query, $params);
packaging one directory
yuki-kimoto authored on 2009-11-16
500
    
501
    return $ret_val;
502
}
503

            
504
sub update_all {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
505
    my $self             = shift;
506
    my $table            = shift || '';
507
    my $update_params    = shift || {};
508
    my $append_statement = shift unless ref $_[0];
509
    my $query_edit_cb    = shift;
510
    my $options          = {allow_update_all => 1};
511
    
update document
yuki-kimoto authored on 2010-01-30
512
    # Update all rows
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
513
    return $self->update($table, $update_params, {}, $append_statement,
514
                         $query_edit_cb, $options);
packaging one directory
yuki-kimoto authored on 2009-11-16
515
}
516

            
517
sub delete {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
518
    my $self             = shift;
519
    my $table            = shift || '';
520
    my $where_params     = shift || {};
521
    my $append_statement = shift unless ref $_[0];
522
    my $query_edit_cb    = shift;
523
    my $options          = shift;
packaging one directory
yuki-kimoto authored on 2009-11-16
524
    
525
    # Where keys
526
    my @where_keys = keys %$where_params;
527
    
528
    # Not exists where keys
529
    croak("Key-value pairs for where clause must be specified to 'delete' second argument")
530
      if !@where_keys && !$options->{allow_delete_all};
531
    
532
    # Where clause
533
    my $where_clause = '';
534
    if (@where_keys) {
535
        $where_clause = 'where ';
536
        foreach my $where_key (@where_keys) {
537
            $where_clause .= "{= $where_key} and ";
538
        }
539
        $where_clause =~ s/ and $//;
540
    }
541
    
542
    # Template for delete
543
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
544
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
545
    
546
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
547
    my $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
548
    
549
    # Query edit callback must be code reference
550
    croak("Query edit callback must be code reference")
551
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
552
    
553
    # Query edit if need
554
    $query_edit_cb->($query) if $query_edit_cb;
555
    
556
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
557
    my $ret_val = $self->query($query, $where_params);
packaging one directory
yuki-kimoto authored on 2009-11-16
558
    
559
    return $ret_val;
560
}
561

            
562
sub delete_all {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
563
    my $self             = shift;
564
    my $table            = shift || '';
565
    my $append_statement = shift unless ref $_[0];
566
    my $query_edit_cb    = shift;
567
    my $options          = {allow_delete_all => 1};
568
    
update document
yuki-kimoto authored on 2010-01-30
569
    # Delete all rows
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
570
    return $self->delete($table, {}, $append_statement, $query_edit_cb,
571
                         $options);
packaging one directory
yuki-kimoto authored on 2009-11-16
572
}
573

            
574
sub _select_usage { return << 'EOS' }
575
Your select arguments is wrong.
576
select usage:
577
$dbi->select(
cleanup
yuki-kimoto authored on 2010-02-11
578
    $table,                # String or array ref
579
    [@$columns],           # Array reference. this can be ommited
580
    {%$where_params},      # Hash reference.  this can be ommited
581
    $append_statement,     # String.          this can be ommited
582
    $query_edit_callback   # Sub reference.   this can be ommited
packaging one directory
yuki-kimoto authored on 2009-11-16
583
);
584
EOS
585

            
586
sub select {
587
    my $self = shift;
588
    
589
    # Check argument
590
    croak($self->_select_usage) unless @_;
591
    
592
    # Arguments
593
    my $tables = shift || '';
594
    $tables    = [$tables] unless ref $tables;
595
    
596
    my $columns          = ref $_[0] eq 'ARRAY' ? shift : [];
597
    my $where_params     = ref $_[0] eq 'HASH'  ? shift : {};
598
    my $append_statement = $_[0] && !ref $_[0]  ? shift : '';
599
    my $query_edit_cb    = shift if ref $_[0] eq 'CODE';
600
    
601
    # Check rest argument
602
    croak($self->_select_usage) if @_;
603
    
604
    # SQL template for select statement
605
    my $template = 'select ';
606
    
607
    # Join column clause
608
    if (@$columns) {
609
        foreach my $column (@$columns) {
610
            $template .= "$column, ";
611
        }
612
        $template =~ s/, $/ /;
613
    }
614
    else {
615
        $template .= '* ';
616
    }
617
    
618
    # Join table
619
    $template .= 'from ';
620
    foreach my $table (@$tables) {
621
        $template .= "$table, ";
622
    }
623
    $template =~ s/, $/ /;
624
    
625
    # Where clause keys
626
    my @where_keys = keys %$where_params;
627
    
many change
yuki-kimoto authored on 2010-02-11
628
    my $where_params_new = {};
629
    
packaging one directory
yuki-kimoto authored on 2009-11-16
630
    # Join where clause
631
    if (@where_keys) {
632
        $template .= 'where ';
633
        foreach my $where_key (@where_keys) {
many change
yuki-kimoto authored on 2010-02-11
634
            my $key_info = DBIx::Custom::KeyInfo->new($where_key);
635
            
636
            my $table_new = $key_info->table || $tables->[0];
637
            my $column = $table_new . '.' . $key_info->column
638
                         . '#' . $table_new;
639
                      
640
            $template .= "{= $column} and ";
641
            
642
            $where_params_new->{$table_new} ||= {};
643
            $where_params_new->{$table_new}->{$key_info->column}
644
              = $where_params->{$where_key};
packaging one directory
yuki-kimoto authored on 2009-11-16
645
        }
646
    }
647
    $template =~ s/ and $//;
648
    
649
    # Append something to last of statement
650
    if ($append_statement =~ s/^where //) {
651
        if (@where_keys) {
652
            $template .= " and $append_statement";
653
        }
654
        else {
655
            $template .= " where $append_statement";
656
        }
657
    }
658
    else {
659
        $template .= " $append_statement";
660
    }
661
    
662
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
663
    my $query = $self->create_query([$tables->[0], $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
664
    
665
    # Query edit
666
    $query_edit_cb->($query) if $query_edit_cb;
667
    
668
    # Execute query
many change
yuki-kimoto authored on 2010-02-11
669
    my $result = $self->query($query, $where_params_new);
packaging one directory
yuki-kimoto authored on 2009-11-16
670
    
671
    return $result;
672
}
673

            
674
sub _add_query_cache {
675
    my ($class, $template, $query) = @_;
update document
yuki-kimoto authored on 2010-01-30
676
    
677
    # Query information
packaging one directory
yuki-kimoto authored on 2009-11-16
678
    my $query_cache_keys = $class->_query_cache_keys;
679
    my $query_caches     = $class->_query_caches;
680
    
update document
yuki-kimoto authored on 2010-01-30
681
    # Already cached
packaging one directory
yuki-kimoto authored on 2009-11-16
682
    return $class if $query_caches->{$template};
683
    
update document
yuki-kimoto authored on 2010-01-30
684
    # Cache
packaging one directory
yuki-kimoto authored on 2009-11-16
685
    $query_caches->{$template} = $query;
686
    push @$query_cache_keys, $template;
687
    
update document
yuki-kimoto authored on 2010-01-30
688
    # Check cache overflow
packaging one directory
yuki-kimoto authored on 2009-11-16
689
    my $overflow = @$query_cache_keys - $class->query_cache_max;
690
    for (my $i = 0; $i < $overflow; $i++) {
691
        my $template = shift @$query_cache_keys;
692
        delete $query_caches->{$template};
693
    }
694
    
695
    return $class;
696
}
697

            
698
=head1 NAME
699

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

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

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

            
706
=cut
707

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

            
cleanup
yuki-kimoto authored on 2010-02-11
710
=head1 STATE
711

            
712
This module is not stable. Method name and functionality will be change.
713

            
version 0.0901
yuki-kimoto authored on 2009-12-17
714
=head1 SYNOPSYS
715
    
716
    # New
717
    my $dbi = DBIx::Custom->new(data_source => "dbi:mysql:database=books"
718
                                user => 'ken', password => '!LFKD%$&');
719
    
720
    # Query
721
    $dbi->query("select title from books");
722
    
723
    # Query with parameters
724
    $dbi->query("select id from books where {= author} && {like title}",
725
                {author => 'ken', title => '%Perl%'});
726
    
727
    # Insert 
728
    $dbi->insert('books', {title => 'perl', author => 'Ken'});
729
    
730
    # Update 
731
    $dbi->update('books', {title => 'aaa', author => 'Ken'}, {id => 5});
732
    
733
    # Delete
734
    $dbi->delete('books', {author => 'Ken'});
735
    
736
    # Select
737
    $dbi->select('books');
738
    $dbi->select('books', {author => 'taro'}); 
739
    $dbi->select('books', [qw/author title/], {author => 'Ken'});
740
    $dbi->select('books', [qw/author title/], {author => 'Ken'},
741
                 'order by id limit 1');
packaging one directory
yuki-kimoto authored on 2009-11-16
742

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

            
745
=head2 user
746

            
update document
yuki-kimoto authored on 2010-01-30
747
Database user name
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
748
    
version 0.0901
yuki-kimoto authored on 2009-12-17
749
    $dbi  = $dbi->user('Ken');
750
    $user = $dbi->user;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
751
    
packaging one directory
yuki-kimoto authored on 2009-11-16
752
=head2 password
753

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

            
759
=head2 data_source
760

            
update document
yuki-kimoto authored on 2010-01-30
761
Database data source
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
762
    
version 0.0901
yuki-kimoto authored on 2009-12-17
763
    $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
764
    $data_source = $dbi->data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
765
    
version 0.0901
yuki-kimoto authored on 2009-12-17
766
If you know data source more, See also L<DBI>.
767

            
packaging one directory
yuki-kimoto authored on 2009-11-16
768
=head2 database
769

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

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

            
add port and host method
yuki-kimoto authored on 2009-11-16
775
=head2 host
776

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

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

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

            
784
=head2 port
785

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

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

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

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

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

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

            
update document
yuki-kimoto authored on 2010-01-30
800
SQL::Template object
packaging one directory
yuki-kimoto authored on 2009-11-16
801

            
version 0.0901
yuki-kimoto authored on 2009-12-17
802
    $dbi      = $dbi->sql_tmpl(DBIx::Cutom::SQL::Template->new);
803
    $sql_tmpl = $dbi->sql_tmpl;
packaging one directory
yuki-kimoto authored on 2009-11-16
804

            
version 0.0901
yuki-kimoto authored on 2009-12-17
805
See also L<DBIx::Custom::SQL::Template>.
packaging one directory
yuki-kimoto authored on 2009-11-16
806

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
811
    $dbi     = $dbi->filters({filter1 => sub { }, filter2 => sub {}});
812
    $filters = $dbi->filters;
packaging one directory
yuki-kimoto authored on 2009-11-16
813
    
version 0.0901
yuki-kimoto authored on 2009-12-17
814
This method is generally used to get a filter.
815

            
816
    $filter = $dbi->filters->{encode_utf8};
817

            
818
If you add filter, use add_filter method.
packaging one directory
yuki-kimoto authored on 2009-11-16
819

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

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

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

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

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

            
831
If you add format, use add_format method.
packaging one directory
yuki-kimoto authored on 2009-11-16
832

            
cleanup
yuki-kimoto authored on 2010-04-28
833
=head2 default_bind_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
834

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

            
cleanup
yuki-kimoto authored on 2010-04-28
837
    $dbi                 = $dbi->default_bind_filter($default_bind_filter);
838
    $default_bind_filter = $dbi->default_bind_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
839

            
version 0.0901
yuki-kimoto authored on 2009-12-17
840
The following is bind filter sample
cleanup
yuki-kimoto authored on 2010-04-28
841
    
842
    $dbi->add_filter(encode_utf8 => sub {
843
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
844
        
cleanup
yuki-kimoto authored on 2010-04-28
845
        require Encode 'encode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
846
        
cleanup
yuki-kimoto authored on 2010-04-28
847
        return encode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
848
    });
cleanup
yuki-kimoto authored on 2010-04-28
849
    
850
    $dbi->default_bind_filter('encode_utf8')
packaging one directory
yuki-kimoto authored on 2009-11-16
851

            
version 0.0901
yuki-kimoto authored on 2009-12-17
852
Bind filter arguemts is
853

            
854
    1. $value : Value
855
    2. $key   : Key
856
    3. $dbi   : DBIx::Custom object
857
    4. $infos : {table => $table, column => $column}
858

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-04-28
868
    $dbi->add_filter(decode_utf8 => sub {
869
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
870
        
cleanup
yuki-kimoto authored on 2010-04-28
871
        require Encode 'decode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
872
        
cleanup
yuki-kimoto authored on 2010-04-28
873
        return decode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
874
    });
packaging one directory
yuki-kimoto authored on 2009-11-16
875

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
878
Bind filter arguemts is
879

            
880
    1. $value : Value
881
    2. $key   : Key
882
    3. $dbi   : DBIx::Custom object
883
    4. $infos : {type => $table, sth => $sth, index => $index}
884

            
packaging one directory
yuki-kimoto authored on 2009-11-16
885
=head2 result_class
886

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
894
=head2 dbh
895

            
update document
yuki-kimoto authored on 2010-01-30
896
Database handle
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
897
    
version 0.0901
yuki-kimoto authored on 2009-12-17
898
    $dbi = $dbi->dbh($dbh);
899
    $dbh = $dbi->dbh;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
900
    
901
=head2 query_cache_max
902

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
905
    $class           = DBIx::Custom->query_cache_max(50);
906
    $query_cache_max = DBIx::Custom->query_cache_max;
907

            
908
Default value is 50
909

            
update document
yuki-kimoto authored on 2010-01-30
910
=head1 METHODS
911

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

            
915
=head2 connect
916

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

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

            
921
=head2 disconnect
922

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
923
Disconnect database
924

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

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

            
929
=head2 reconnect
930

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

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

            
935
=head2 connected
936

            
version 0.0901
yuki-kimoto authored on 2009-12-17
937
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
938
    
version 0.0901
yuki-kimoto authored on 2009-12-17
939
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
940
    
941
=head2 add_filter
942

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
943
Resist filter
packaging one directory
yuki-kimoto authored on 2009-11-16
944
    
version 0.0901
yuki-kimoto authored on 2009-12-17
945
    $dbi->add_filter($fname1 => $filter1, $fname => $filter2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
946
    
version 0.0901
yuki-kimoto authored on 2009-12-17
947
The following is add_filter sample
948

            
packaging one directory
yuki-kimoto authored on 2009-11-16
949
    $dbi->add_filter(
950
        encode_utf8 => sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
951
            my ($value, $key, $dbi, $infos) = @_;
952
            utf8::upgrade($value) unless Encode::is_utf8($value);
953
            return encode('UTF-8', $value);
packaging one directory
yuki-kimoto authored on 2009-11-16
954
        },
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
955
        decode_utf8 => sub {
956
            my ($value, $key, $dbi, $infos) = @_;
957
            return decode('UTF-8', $value)
packaging one directory
yuki-kimoto authored on 2009-11-16
958
        }
959
    );
960

            
961
=head2 add_format
962

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
965
    $dbi->add_format($fname1 => $format, $fname2 => $format2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
966
    
version 0.0901
yuki-kimoto authored on 2009-12-17
967
The following is add_format sample.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
968

            
version 0.0901
yuki-kimoto authored on 2009-12-17
969
    $dbi->add_format(date => '%Y:%m:%d', datetime => '%Y-%m-%d %H:%M:%S');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
970

            
version 0.0901
yuki-kimoto authored on 2009-12-17
971
=head2 create_query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
972
    
version 0.0901
yuki-kimoto authored on 2009-12-17
973
Create Query object parsing SQL template
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
974

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
981
If you know SQL template, see also L<DBIx::Custom::SQL::Template>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
982

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

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
991
    $result = $dbi->query("select * from authors where {= name} and {= age}", 
992
                          {author => 'taro', age => 19});
993
    
994
    while (my @row = $result->fetch) {
995
        # do something
996
    }
997

            
version 0.0901
yuki-kimoto authored on 2009-12-17
998
If you now syntax of template, See also L<DBIx::Custom::SQL::Template>
999

            
1000
Return value of query method is L<DBIx::Custom::Result> object
1001

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

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

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

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
1008
    $dbi->transaction->run(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1009
        my $dbi = shift;
1010
        
packaging one directory
yuki-kimoto authored on 2009-11-16
1011
        # do something
1012
    });
1013

            
1014
If transaction is success, commit is execute. 
1015
If tranzation is died, rollback is execute.
1016

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1017
=head2 create_table
1018

            
1019
Create table
1020

            
1021
    $dbi->create_table(
1022
        'books',
1023
        'name char(255)',
1024
        'age  int'
1025
    );
1026

            
1027
First argument is table name. Rest arguments is column definition.
1028

            
1029
=head2 drop_table
1030

            
1031
Drop table
1032

            
1033
    $dbi->drop_table('books');
1034

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1042
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
1043
    
version 0.0901
yuki-kimoto authored on 2009-12-17
1044
The following is insert sample.
1045

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1048
You can add statement.
1049

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1052
=head2 update
1053

            
update document
yuki-kimoto authored on 2009-11-19
1054
Update rows
1055

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1065
You can add statement.
1066

            
1067
    $dbi->update('books', {title => 'Perl', author => 'Taro'},
1068
                 {id => 5}, "some statement");
1069

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1070
=head2 update_all
1071

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

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

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

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

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

            
1082
=head2 delete
1083

            
update document
yuki-kimoto authored on 2009-11-19
1084
Delete rows
1085

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1089
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
1090
    
version 0.0901
yuki-kimoto authored on 2009-12-17
1091
The following is delete sample.
1092

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1095
You can add statement.
1096

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1099
=head2 delete_all
1100

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

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

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

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

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

            
1111
=head2 select
1112
    
update document
yuki-kimoto authored on 2009-11-19
1113
Select rows
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1114

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1115
    $resut = $dbi->select(
packaging one directory
yuki-kimoto authored on 2009-11-16
1116
        $table,                # must be string or array;
version 0.0901
yuki-kimoto authored on 2009-12-17
1117
        \@$columns,            # must be array reference. this can be ommited
1118
        \%$where_params,       # must be hash reference.  this can be ommited
1119
        $append_statement,     # must be string.          this can be ommited
1120
        $query_edit_callback   # must be code reference.  this can be ommited
packaging one directory
yuki-kimoto authored on 2009-11-16
1121
    );
update document
yuki-kimoto authored on 2009-11-19
1122

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

            
1125
The following is some select samples
1126

            
1127
    # select * from books;
1128
    $result = $dbi->select('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1129
    
update document
yuki-kimoto authored on 2009-11-19
1130
    # select * from books where title = 'Perl';
1131
    $result = $dbi->select('books', {title => 1});
1132
    
1133
    # select title, author from books where id = 1 for update;
1134
    $result = $dbi->select(
1135
        'books',              # table
1136
        ['title', 'author'],  # columns
1137
        {id => 1},            # where clause
1138
        'for update',         # append statement
1139
    );
1140

            
1141
You can join multi tables
1142
    
1143
    $result = $dbi->select(
1144
        ['table1', 'table2'],                # tables
1145
        ['table1.id as table1_id', 'title'], # columns (alias is ok)
1146
        {table1.id => 1},                    # where clase
1147
        "where table1.id = table2.id",       # join clause (must start 'where')
1148
    );
1149

            
1150
You can also edit query
1151
        
packaging one directory
yuki-kimoto authored on 2009-11-16
1152
    $dbi->select(
update document
yuki-kimoto authored on 2009-11-19
1153
        'books',
1154
        # column, where clause, append statement,
packaging one directory
yuki-kimoto authored on 2009-11-16
1155
        sub {
1156
            my $query = shift;
1157
            $query->bind_filter(sub {
1158
                # ...
1159
            });
1160
        }
update document
yuki-kimoto authored on 2009-11-19
1161
    }
1162

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1163
=head2 prepare
1164

            
1165
Prepare statement handle.
1166

            
1167
    $sth = $dbi->prepare('select * from books;');
1168

            
1169
This method is same as DBI prepare method.
1170

            
1171
See also L<DBI>.
1172

            
1173
=head2 do
1174

            
1175
Execute SQL
1176

            
1177
    $affected = $dbi->do('insert into books (title, author) values (?, ?)',
1178
                        'Perl', 'taro');
1179

            
1180
Retrun value is affected rows count.
1181

            
1182
This method is same as DBI do method.
1183

            
1184
See also L<DBI>
1185

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

            
1188

            
1189

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

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

            
1195
    1. AutoCommit is true
1196
    2. RaiseError is true
1197

            
1198
By default, Both AutoCommit and RaiseError is true.
1199
You must not change these mode not to damage your data.
1200

            
1201
If you change these mode, 
1202
you cannot get correct error message, 
1203
or run_transaction may fail.
1204

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

            
1207
DBIx::Custom is customizable DBI.
1208
You can inherit DBIx::Custom and custumize attributes.
1209

            
1210
    package DBIx::Custom::Yours;
1211
    use base DBIx::Custom;
1212
    
1213
    my $class = __PACKAGE__;
1214
    
1215
    $class->user('your_name');
1216
    $class->password('your_password');
1217

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1218
=head1 AUTHOR
1219

            
1220
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
1221

            
1222
Github L<http://github.com/yuki-kimoto>
1223

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

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

            
1228
Copyright 2009 Yuki Kimoto, all rights reserved.
1229

            
1230
This program is free software; you can redistribute it and/or modify it
1231
under the same terms as Perl itself.
1232

            
1233
=cut