DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1237 lines | 29.374kb
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

            
23
__PACKAGE__->dual_attr([qw/user password data_source/], inherit => 'scalar_copy');
24
__PACKAGE__->dual_attr([qw/database host port/],        inherit => 'scalar_copy');
25
__PACKAGE__->dual_attr([qw/bind_filter fetch_filter/],  inherit => 'scalar_copy');
packaging one directory
yuki-kimoto authored on 2009-11-16
26

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
27
__PACKAGE__->dual_attr([qw/options 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

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
30
__PACKAGE__->dual_attr('result_class', default => 'DBIx::Custom::Result',
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-22
31
                                       inherit => 'scalar_copy');
packaging one directory
yuki-kimoto authored on 2009-11-16
32

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
33
__PACKAGE__->dual_attr('sql_tmpl', default => sub {DBIx::Custom::SQL::Template->new},
cleanup
yuki-kimoto authored on 2010-01-21
34
                                   inherit   => sub {$_[0] ? $_[0]->clone : undef});
packaging one directory
yuki-kimoto authored on 2009-11-16
35

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

            
46
sub add_format{
47
    my $invocant = shift;
48
    
update document
yuki-kimoto authored on 2010-01-30
49
    # Add format
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
50
    my $formats = ref $_[0] eq 'HASH' ? $_[0] : {@_};
51
    $invocant->formats({%{$invocant->formats}, %$formats});
52

            
packaging one directory
yuki-kimoto authored on 2009-11-16
53
    return $invocant;
54
}
55

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
383
sub drop_table {
384
    my ($self, $table) = @_;
385
    
386
    # Drop table
387
    my $sql = "drop table $table;";
388

            
389
    # Do query
390
    return $self->do($sql);
391
}
392

            
packaging one directory
yuki-kimoto authored on 2009-11-16
393
sub insert {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
394
    my $self             = shift;
395
    my $table            = shift || '';
396
    my $insert_params    = shift || {};
397
    my $append_statement = shift unless ref $_[0];
398
    my $query_edit_cb    = shift;
packaging one directory
yuki-kimoto authored on 2009-11-16
399
    
400
    # Insert keys
401
    my @insert_keys = keys %$insert_params;
402
    
403
    # Not exists insert keys
404
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
405
      unless @insert_keys;
406
    
407
    # Templte for insert
408
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
409
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
410
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
411
    my $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
412
    
413
    # Query edit callback must be code reference
414
    croak("Query edit callback must be code reference")
415
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
416
    
417
    # Query edit if need
418
    $query_edit_cb->($query) if $query_edit_cb;
419
    
420
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
421
    my $ret_val = $self->query($query, $insert_params);
packaging one directory
yuki-kimoto authored on 2009-11-16
422
    
423
    return $ret_val;
424
}
425

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

            
463
            $where_clause .= "{= $column} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
464
        }
465
        $where_clause =~ s/ and $//;
466
    }
467
    
468
    # Template for update
469
    my $template = "update $table $update_clause $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
470
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
471
    
472
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
473
    my $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
474
    
475
    # Query edit callback must be code reference
476
    croak("Query edit callback must be code reference")
477
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
478
    
479
    # Query edit if need
480
    $query_edit_cb->($query) if $query_edit_cb;
481
    
482
    # Rearrange parammeters
many change
yuki-kimoto authored on 2010-02-11
483
    my $params = {%$update_params, '@where' => $where_params};
packaging one directory
yuki-kimoto authored on 2009-11-16
484
    
485
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
486
    my $ret_val = $self->query($query, $params);
packaging one directory
yuki-kimoto authored on 2009-11-16
487
    
488
    return $ret_val;
489
}
490

            
491
sub update_all {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
492
    my $self             = shift;
493
    my $table            = shift || '';
494
    my $update_params    = shift || {};
495
    my $append_statement = shift unless ref $_[0];
496
    my $query_edit_cb    = shift;
497
    my $options          = {allow_update_all => 1};
498
    
update document
yuki-kimoto authored on 2010-01-30
499
    # Update all rows
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
500
    return $self->update($table, $update_params, {}, $append_statement,
501
                         $query_edit_cb, $options);
packaging one directory
yuki-kimoto authored on 2009-11-16
502
}
503

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

            
549
sub delete_all {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
550
    my $self             = shift;
551
    my $table            = shift || '';
552
    my $append_statement = shift unless ref $_[0];
553
    my $query_edit_cb    = shift;
554
    my $options          = {allow_delete_all => 1};
555
    
update document
yuki-kimoto authored on 2010-01-30
556
    # Delete all rows
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
557
    return $self->delete($table, {}, $append_statement, $query_edit_cb,
558
                         $options);
packaging one directory
yuki-kimoto authored on 2009-11-16
559
}
560

            
561
sub _select_usage { return << 'EOS' }
562
Your select arguments is wrong.
563
select usage:
564
$dbi->select(
cleanup
yuki-kimoto authored on 2010-02-11
565
    $table,                # String or array ref
566
    [@$columns],           # Array reference. this can be ommited
567
    {%$where_params},      # Hash reference.  this can be ommited
568
    $append_statement,     # String.          this can be ommited
569
    $query_edit_callback   # Sub reference.   this can be ommited
packaging one directory
yuki-kimoto authored on 2009-11-16
570
);
571
EOS
572

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

            
661
sub _add_query_cache {
662
    my ($class, $template, $query) = @_;
update document
yuki-kimoto authored on 2010-01-30
663
    
664
    # Query information
packaging one directory
yuki-kimoto authored on 2009-11-16
665
    my $query_cache_keys = $class->_query_cache_keys;
666
    my $query_caches     = $class->_query_caches;
667
    
update document
yuki-kimoto authored on 2010-01-30
668
    # Already cached
packaging one directory
yuki-kimoto authored on 2009-11-16
669
    return $class if $query_caches->{$template};
670
    
update document
yuki-kimoto authored on 2010-01-30
671
    # Cache
packaging one directory
yuki-kimoto authored on 2009-11-16
672
    $query_caches->{$template} = $query;
673
    push @$query_cache_keys, $template;
674
    
update document
yuki-kimoto authored on 2010-01-30
675
    # Check cache overflow
packaging one directory
yuki-kimoto authored on 2009-11-16
676
    my $overflow = @$query_cache_keys - $class->query_cache_max;
677
    for (my $i = 0; $i < $overflow; $i++) {
678
        my $template = shift @$query_cache_keys;
679
        delete $query_caches->{$template};
680
    }
681
    
682
    return $class;
683
}
684

            
685
sub filter_off {
686
    my $self = shift;
687
    
update document
yuki-kimoto authored on 2010-01-30
688
    # Filter off
packaging one directory
yuki-kimoto authored on 2009-11-16
689
    $self->bind_filter(undef);
690
    $self->fetch_filter(undef);
691
    
692
    return $self;
693
}
694

            
695
=head1 NAME
696

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

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

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

            
703
=cut
704

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

            
cleanup
yuki-kimoto authored on 2010-02-11
707
=head1 STATE
708

            
709
This module is not stable. Method name and functionality will be change.
710

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

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

            
742
=head2 user
743

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

            
update document
yuki-kimoto authored on 2010-01-30
751
Database password
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->password('lkj&le`@s');
754
    $password = $dbi->password;
packaging one directory
yuki-kimoto authored on 2009-11-16
755

            
756
=head2 data_source
757

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
765
=head2 database
766

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

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

            
add port and host method
yuki-kimoto authored on 2009-11-16
772
=head2 host
773

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

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

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

            
781
=head2 port
782

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

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

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

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

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

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

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

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

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

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

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

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

            
813
    $filter = $dbi->filters->{encode_utf8};
814

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

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

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

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

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

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

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

            
830
=head2 bind_filter
831

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
834
    $dbi         = $dbi->bind_filter($bind_filter);
835
    $bind_filter = $dbi->bind_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
836

            
version 0.0901
yuki-kimoto authored on 2009-12-17
837
The following is bind filter sample
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
838

            
839
    $dbi->bind_filter(sub {
840
        my ($value, $key, $dbi, $infos) = @_;
841
        
842
        # edit $value
843
        
844
        return $value;
845
    });
packaging one directory
yuki-kimoto authored on 2009-11-16
846

            
version 0.0901
yuki-kimoto authored on 2009-12-17
847
Bind filter arguemts is
848

            
849
    1. $value : Value
850
    2. $key   : Key
851
    3. $dbi   : DBIx::Custom object
852
    4. $infos : {table => $table, column => $column}
853

            
packaging one directory
yuki-kimoto authored on 2009-11-16
854
=head2 fetch_filter
855

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
858
    $dbi          = $dbi->fetch_filter($fetch_filter);
859
    $fetch_filter = $dbi->fetch_filter;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
860

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

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
863
    $dbi->fetch_filter(sub {
864
        my ($value, $key, $dbi, $infos) = @_;
865
        
866
        # edit $value
867
        
868
        return $value;
869
    });
packaging one directory
yuki-kimoto authored on 2009-11-16
870

            
version 0.0901
yuki-kimoto authored on 2009-12-17
871
Bind filter arguemts is
872

            
873
    1. $value : Value
874
    2. $key   : Key
875
    3. $dbi   : DBIx::Custom object
876
    4. $infos : {type => $table, sth => $sth, index => $index}
877

            
packaging one directory
yuki-kimoto authored on 2009-11-16
878
=head2 result_class
879

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
887
=head2 dbh
888

            
update document
yuki-kimoto authored on 2010-01-30
889
Database handle
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
890
    
version 0.0901
yuki-kimoto authored on 2009-12-17
891
    $dbi = $dbi->dbh($dbh);
892
    $dbh = $dbi->dbh;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
893
    
894
=head2 query_cache_max
895

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
898
    $class           = DBIx::Custom->query_cache_max(50);
899
    $query_cache_max = DBIx::Custom->query_cache_max;
900

            
901
Default value is 50
902

            
update document
yuki-kimoto authored on 2010-01-30
903
=head1 METHODS
904

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

            
908
=head2 connect
909

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

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

            
914
=head2 disconnect
915

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
916
Disconnect database
917

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

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

            
922
=head2 reconnect
923

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

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

            
928
=head2 connected
929

            
version 0.0901
yuki-kimoto authored on 2009-12-17
930
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
931
    
version 0.0901
yuki-kimoto authored on 2009-12-17
932
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
933
    
934
=head2 filter_off
935

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
936
bind_filter and fitch_filter off
937
    
version 0.0901
yuki-kimoto authored on 2009-12-17
938
    $dbi->filter_off
packaging one directory
yuki-kimoto authored on 2009-11-16
939
    
version 0.0901
yuki-kimoto authored on 2009-12-17
940
This method is equeal to
packaging one directory
yuki-kimoto authored on 2009-11-16
941
    
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
942
    $dbi->bind_filter(undef);
943
    $dbi->fetch_filter(undef);
packaging one directory
yuki-kimoto authored on 2009-11-16
944

            
945
=head2 add_filter
946

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

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

            
965
=head2 add_format
966

            
version 0.0901
yuki-kimoto authored on 2009-12-17
967
Add format
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($fname1 => $format, $fname2 => $format2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
970
    
version 0.0901
yuki-kimoto authored on 2009-12-17
971
The following is add_format sample.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
972

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
975
=head2 create_query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
976
    
version 0.0901
yuki-kimoto authored on 2009-12-17
977
Create Query object parsing SQL template
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
978

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

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

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

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

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

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

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

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

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

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

            
1004
Return value of query method is L<DBIx::Custom::Result> object
1005

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

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

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

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

            
1018
If transaction is success, commit is execute. 
1019
If tranzation is died, rollback is execute.
1020

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1021
=head2 create_table
1022

            
1023
Create table
1024

            
1025
    $dbi->create_table(
1026
        'books',
1027
        'name char(255)',
1028
        'age  int'
1029
    );
1030

            
1031
First argument is table name. Rest arguments is column definition.
1032

            
1033
=head2 drop_table
1034

            
1035
Drop table
1036

            
1037
    $dbi->drop_table('books');
1038

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1039
=head2 insert
1040

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1046
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
1047
    
version 0.0901
yuki-kimoto authored on 2009-12-17
1048
The following is insert sample.
1049

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1052
You can add statement.
1053

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1056
=head2 update
1057

            
update document
yuki-kimoto authored on 2009-11-19
1058
Update rows
1059

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1069
You can add statement.
1070

            
1071
    $dbi->update('books', {title => 'Perl', author => 'Taro'},
1072
                 {id => 5}, "some statement");
1073

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1074
=head2 update_all
1075

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

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

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

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

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

            
1086
=head2 delete
1087

            
update document
yuki-kimoto authored on 2009-11-19
1088
Delete rows
1089

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1093
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
1094
    
version 0.0901
yuki-kimoto authored on 2009-12-17
1095
The following is delete sample.
1096

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1099
You can add statement.
1100

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1103
=head2 delete_all
1104

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

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

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

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

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

            
1115
=head2 select
1116
    
update document
yuki-kimoto authored on 2009-11-19
1117
Select rows
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1118

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

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

            
1129
The following is some select samples
1130

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1167
=head2 prepare
1168

            
1169
Prepare statement handle.
1170

            
1171
    $sth = $dbi->prepare('select * from books;');
1172

            
1173
This method is same as DBI prepare method.
1174

            
1175
See also L<DBI>.
1176

            
1177
=head2 do
1178

            
1179
Execute SQL
1180

            
1181
    $affected = $dbi->do('insert into books (title, author) values (?, ?)',
1182
                        'Perl', 'taro');
1183

            
1184
Retrun value is affected rows count.
1185

            
1186
This method is same as DBI do method.
1187

            
1188
See also L<DBI>
1189

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

            
1192

            
1193

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

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

            
1199
    1. AutoCommit is true
1200
    2. RaiseError is true
1201

            
1202
By default, Both AutoCommit and RaiseError is true.
1203
You must not change these mode not to damage your data.
1204

            
1205
If you change these mode, 
1206
you cannot get correct error message, 
1207
or run_transaction may fail.
1208

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

            
1211
DBIx::Custom is customizable DBI.
1212
You can inherit DBIx::Custom and custumize attributes.
1213

            
1214
    package DBIx::Custom::Yours;
1215
    use base DBIx::Custom;
1216
    
1217
    my $class = __PACKAGE__;
1218
    
1219
    $class->user('your_name');
1220
    $class->password('your_password');
1221

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1222
=head1 AUTHOR
1223

            
1224
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
1225

            
1226
Github L<http://github.com/yuki-kimoto>
1227

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

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

            
1232
Copyright 2009 Yuki Kimoto, all rights reserved.
1233

            
1234
This program is free software; you can redistribute it and/or modify it
1235
under the same terms as Perl itself.
1236

            
1237
=cut