DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1185 lines | 28.152kb
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';
packaging one directory
yuki-kimoto authored on 2009-11-16
7
use Carp 'croak';
8
use DBI;
9
use DBIx::Custom::Result;
10
use DBIx::Custom::SQL::Template;
remove run_transaction().
yuki-kimoto authored on 2010-01-30
11
use DBIx::Custom::Transaction;
cleanup
yuki-kimoto authored on 2010-02-11
12
use DBIx::Custom::Query;
packaging one directory
yuki-kimoto authored on 2009-11-16
13

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
288
sub _build_bind_values {
289
    my ($self, $query, $params) = @_;
Simplify key search
yuki-kimoto authored on 2010-02-11
290
    my $key_infos  = $query->key_infos;
291
    my $filter     = $query->bind_filter;
packaging one directory
yuki-kimoto authored on 2009-11-16
292
    
293
    # binding values
294
    my @bind_values;
295
    
Simplify key search
yuki-kimoto authored on 2010-02-11
296
    # Build bind values
packaging one directory
yuki-kimoto authored on 2009-11-16
297
    foreach my $key_info (@$key_infos) {
Simplify key search
yuki-kimoto authored on 2010-02-11
298
        my $table        = $key_info->table;
299
        my $column       = $key_info->column;
300
        my $id           = $key_info->id;
301
        my $pos          = $key_info->pos;
packaging one directory
yuki-kimoto authored on 2009-11-16
302
        
Simplify key search
yuki-kimoto authored on 2010-02-11
303
        # Value
304
        my $value = $id && $pos ? $params->{$id}->{$column}->[$pos]
305
                  : $id         ? $params->{$id}->{$column}
306
                  : $pos        ? $params->{$column}->[$pos]
307
                  : $params->{$column};
packaging one directory
yuki-kimoto authored on 2009-11-16
308
        
Simplify key search
yuki-kimoto authored on 2010-02-11
309
        # Filter
310
        push @bind_values, 
311
             $filter ? $filter->($value, $table, $column, $self->filters)
312
                     : $value;
cleanup
yuki-kimoto authored on 2010-02-11
313
    }
314
    
Simplify key search
yuki-kimoto authored on 2010-02-11
315
    return \@bind_values;
cleanup
yuki-kimoto authored on 2010-02-11
316
}
317

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
320
sub create_table {
321
    my ($self, $table, @column_definitions) = @_;
322
    
323
    # Create table
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
324
    my $sql = "create table $table (";
version 0.0901
yuki-kimoto authored on 2009-12-17
325
    
326
    # Column definitions
327
    foreach my $column_definition (@column_definitions) {
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
328
        $sql .= "$column_definition,";
version 0.0901
yuki-kimoto authored on 2009-12-17
329
    }
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
330
    $sql =~ s/,$//;
version 0.0901
yuki-kimoto authored on 2009-12-17
331
    
332
    # End
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
333
    $sql .= ");";
version 0.0901
yuki-kimoto authored on 2009-12-17
334
    
335
    # Do query
336
    return $self->do($sql);
337
}
338

            
339
sub drop_table {
340
    my ($self, $table) = @_;
341
    
342
    # Drop table
343
    my $sql = "drop table $table;";
344

            
345
    # Do query
346
    return $self->do($sql);
347
}
348

            
packaging one directory
yuki-kimoto authored on 2009-11-16
349
sub insert {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
350
    my $self             = shift;
351
    my $table            = shift || '';
352
    my $insert_params    = shift || {};
353
    my $append_statement = shift unless ref $_[0];
354
    my $query_edit_cb    = shift;
packaging one directory
yuki-kimoto authored on 2009-11-16
355
    
356
    # Insert keys
357
    my @insert_keys = keys %$insert_params;
358
    
359
    # Not exists insert keys
360
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
361
      unless @insert_keys;
362
    
363
    # Templte for insert
364
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
365
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
366
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
367
    my $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
368
    
369
    # Query edit callback must be code reference
370
    croak("Query edit callback must be code reference")
371
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
372
    
373
    # Query edit if need
374
    $query_edit_cb->($query) if $query_edit_cb;
375
    
376
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
377
    my $ret_val = $self->query($query, $insert_params);
packaging one directory
yuki-kimoto authored on 2009-11-16
378
    
379
    return $ret_val;
380
}
381

            
382
sub update {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
383
    my $self             = shift;
384
    my $table            = shift || '';
385
    my $update_params    = shift || {};
386
    my $where_params     = shift || {};
387
    my $append_statement = shift unless ref $_[0];
388
    my $query_edit_cb    = shift;
389
    my $options          = shift;
packaging one directory
yuki-kimoto authored on 2009-11-16
390
    
391
    # Update keys
392
    my @update_keys = keys %$update_params;
393
    
394
    # Not exists update kyes
395
    croak("Key-value pairs for update must be specified to 'update' second argument")
396
      unless @update_keys;
397
    
398
    # Where keys
399
    my @where_keys = keys %$where_params;
400
    
401
    # Not exists where keys
402
    croak("Key-value pairs for where clause must be specified to 'update' third argument")
403
      if !@where_keys && !$options->{allow_update_all};
404
    
405
    # Update clause
406
    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
407
    
408
    # Where clause
409
    my $where_clause = '';
410
    if (@where_keys) {
411
        $where_clause = 'where ';
412
        foreach my $where_key (@where_keys) {
413
            $where_clause .= "{= $where_key} and ";
414
        }
415
        $where_clause =~ s/ and $//;
416
    }
417
    
418
    # Template for update
419
    my $template = "update $table $update_clause $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
420
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
421
    
422
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
423
    my $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
424
    
425
    # Query edit callback must be code reference
426
    croak("Query edit callback must be code reference")
427
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
428
    
429
    # Query edit if need
430
    $query_edit_cb->($query) if $query_edit_cb;
431
    
432
    # Rearrange parammeters
add DBIx::Custom::Column
yuki-kimoto authored on 2010-02-11
433
    my $params = {%$update_params, %$where_params};
packaging one directory
yuki-kimoto authored on 2009-11-16
434
    
435
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
436
    my $ret_val = $self->query($query, $params);
packaging one directory
yuki-kimoto authored on 2009-11-16
437
    
438
    return $ret_val;
439
}
440

            
441
sub update_all {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
442
    my $self             = shift;
443
    my $table            = shift || '';
444
    my $update_params    = shift || {};
445
    my $append_statement = shift unless ref $_[0];
446
    my $query_edit_cb    = shift;
447
    my $options          = {allow_update_all => 1};
448
    
update document
yuki-kimoto authored on 2010-01-30
449
    # Update all rows
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
450
    return $self->update($table, $update_params, {}, $append_statement,
451
                         $query_edit_cb, $options);
packaging one directory
yuki-kimoto authored on 2009-11-16
452
}
453

            
454
sub delete {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
455
    my $self             = shift;
456
    my $table            = shift || '';
457
    my $where_params     = shift || {};
458
    my $append_statement = shift unless ref $_[0];
459
    my $query_edit_cb    = shift;
460
    my $options          = shift;
packaging one directory
yuki-kimoto authored on 2009-11-16
461
    
462
    # Where keys
463
    my @where_keys = keys %$where_params;
464
    
465
    # Not exists where keys
466
    croak("Key-value pairs for where clause must be specified to 'delete' second argument")
467
      if !@where_keys && !$options->{allow_delete_all};
468
    
469
    # Where clause
470
    my $where_clause = '';
471
    if (@where_keys) {
472
        $where_clause = 'where ';
473
        foreach my $where_key (@where_keys) {
474
            $where_clause .= "{= $where_key} and ";
475
        }
476
        $where_clause =~ s/ and $//;
477
    }
478
    
479
    # Template for delete
480
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
481
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
482
    
483
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
484
    my $query = $self->create_query([$table, $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
485
    
486
    # Query edit callback must be code reference
487
    croak("Query edit callback must be code reference")
488
      if $query_edit_cb && ref $query_edit_cb ne 'CODE';
489
    
490
    # Query edit if need
491
    $query_edit_cb->($query) if $query_edit_cb;
492
    
493
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
494
    my $ret_val = $self->query($query, $where_params);
packaging one directory
yuki-kimoto authored on 2009-11-16
495
    
496
    return $ret_val;
497
}
498

            
499
sub delete_all {
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
500
    my $self             = shift;
501
    my $table            = shift || '';
502
    my $append_statement = shift unless ref $_[0];
503
    my $query_edit_cb    = shift;
504
    my $options          = {allow_delete_all => 1};
505
    
update document
yuki-kimoto authored on 2010-01-30
506
    # Delete all rows
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
507
    return $self->delete($table, {}, $append_statement, $query_edit_cb,
508
                         $options);
packaging one directory
yuki-kimoto authored on 2009-11-16
509
}
510

            
511
sub _select_usage { return << 'EOS' }
512
Your select arguments is wrong.
513
select usage:
514
$dbi->select(
cleanup
yuki-kimoto authored on 2010-02-11
515
    $table,                # String or array ref
516
    [@$columns],           # Array reference. this can be ommited
517
    {%$where_params},      # Hash reference.  this can be ommited
518
    $append_statement,     # String.          this can be ommited
519
    $query_edit_callback   # Sub reference.   this can be ommited
packaging one directory
yuki-kimoto authored on 2009-11-16
520
);
521
EOS
522

            
523
sub select {
524
    my $self = shift;
525
    
526
    # Check argument
527
    croak($self->_select_usage) unless @_;
528
    
529
    # Arguments
530
    my $tables = shift || '';
531
    $tables    = [$tables] unless ref $tables;
532
    
533
    my $columns          = ref $_[0] eq 'ARRAY' ? shift : [];
534
    my $where_params     = ref $_[0] eq 'HASH'  ? shift : {};
535
    my $append_statement = $_[0] && !ref $_[0]  ? shift : '';
536
    my $query_edit_cb    = shift if ref $_[0] eq 'CODE';
537
    
538
    # Check rest argument
539
    croak($self->_select_usage) if @_;
540
    
541
    # SQL template for select statement
542
    my $template = 'select ';
543
    
544
    # Join column clause
545
    if (@$columns) {
546
        foreach my $column (@$columns) {
547
            $template .= "$column, ";
548
        }
549
        $template =~ s/, $/ /;
550
    }
551
    else {
552
        $template .= '* ';
553
    }
554
    
555
    # Join table
556
    $template .= 'from ';
557
    foreach my $table (@$tables) {
558
        $template .= "$table, ";
559
    }
560
    $template =~ s/, $/ /;
561
    
562
    # Where clause keys
563
    my @where_keys = keys %$where_params;
564
    
565
    # Join where clause
566
    if (@where_keys) {
567
        $template .= 'where ';
568
        foreach my $where_key (@where_keys) {
569
            $template .= "{= $where_key} and ";
570
        }
571
    }
572
    $template =~ s/ and $//;
573
    
574
    # Append something to last of statement
575
    if ($append_statement =~ s/^where //) {
576
        if (@where_keys) {
577
            $template .= " and $append_statement";
578
        }
579
        else {
580
            $template .= " where $append_statement";
581
        }
582
    }
583
    else {
584
        $template .= " $append_statement";
585
    }
586
    
587
    # Create query
Simplify key search
yuki-kimoto authored on 2010-02-11
588
    my $query = $self->create_query([$tables->[0], $template]);
packaging one directory
yuki-kimoto authored on 2009-11-16
589
    
590
    # Query edit
591
    $query_edit_cb->($query) if $query_edit_cb;
592
    
593
    # Execute query
version 0.0901
yuki-kimoto authored on 2009-12-17
594
    my $result = $self->query($query, $where_params);
packaging one directory
yuki-kimoto authored on 2009-11-16
595
    
596
    return $result;
597
}
598

            
599
sub _add_query_cache {
600
    my ($class, $template, $query) = @_;
update document
yuki-kimoto authored on 2010-01-30
601
    
602
    # Query information
packaging one directory
yuki-kimoto authored on 2009-11-16
603
    my $query_cache_keys = $class->_query_cache_keys;
604
    my $query_caches     = $class->_query_caches;
605
    
update document
yuki-kimoto authored on 2010-01-30
606
    # Already cached
packaging one directory
yuki-kimoto authored on 2009-11-16
607
    return $class if $query_caches->{$template};
608
    
update document
yuki-kimoto authored on 2010-01-30
609
    # Cache
packaging one directory
yuki-kimoto authored on 2009-11-16
610
    $query_caches->{$template} = $query;
611
    push @$query_cache_keys, $template;
612
    
update document
yuki-kimoto authored on 2010-01-30
613
    # Check cache overflow
packaging one directory
yuki-kimoto authored on 2009-11-16
614
    my $overflow = @$query_cache_keys - $class->query_cache_max;
615
    for (my $i = 0; $i < $overflow; $i++) {
616
        my $template = shift @$query_cache_keys;
617
        delete $query_caches->{$template};
618
    }
619
    
620
    return $class;
621
}
622

            
623
sub filter_off {
624
    my $self = shift;
625
    
update document
yuki-kimoto authored on 2010-01-30
626
    # Filter off
packaging one directory
yuki-kimoto authored on 2009-11-16
627
    $self->bind_filter(undef);
628
    $self->fetch_filter(undef);
629
    
630
    return $self;
631
}
632

            
633
=head1 NAME
634

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

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

            
update document
yuki-kimoto authored on 2010-01-30
639
Version 0.1101
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
640

            
641
=cut
642

            
update document
yuki-kimoto authored on 2010-01-30
643
our $VERSION = '0.1101';
packaging one directory
yuki-kimoto authored on 2009-11-16
644

            
cleanup
yuki-kimoto authored on 2010-02-11
645
=head1 STATE
646

            
647
This module is not stable. Method name and functionality will be change.
648

            
version 0.0901
yuki-kimoto authored on 2009-12-17
649
=head1 SYNOPSYS
650
    
651
    # New
652
    my $dbi = DBIx::Custom->new(data_source => "dbi:mysql:database=books"
653
                                user => 'ken', password => '!LFKD%$&');
654
    
655
    # Query
656
    $dbi->query("select title from books");
657
    
658
    # Query with parameters
659
    $dbi->query("select id from books where {= author} && {like title}",
660
                {author => 'ken', title => '%Perl%'});
661
    
662
    # Insert 
663
    $dbi->insert('books', {title => 'perl', author => 'Ken'});
664
    
665
    # Update 
666
    $dbi->update('books', {title => 'aaa', author => 'Ken'}, {id => 5});
667
    
668
    # Delete
669
    $dbi->delete('books', {author => 'Ken'});
670
    
671
    # Select
672
    $dbi->select('books');
673
    $dbi->select('books', {author => 'taro'}); 
674
    $dbi->select('books', [qw/author title/], {author => 'Ken'});
675
    $dbi->select('books', [qw/author title/], {author => 'Ken'},
676
                 'order by id limit 1');
packaging one directory
yuki-kimoto authored on 2009-11-16
677

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

            
680
=head2 user
681

            
update document
yuki-kimoto authored on 2010-01-30
682
Database user name
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
683
    
version 0.0901
yuki-kimoto authored on 2009-12-17
684
    $dbi  = $dbi->user('Ken');
685
    $user = $dbi->user;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
686
    
packaging one directory
yuki-kimoto authored on 2009-11-16
687
=head2 password
688

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

            
694
=head2 data_source
695

            
update document
yuki-kimoto authored on 2010-01-30
696
Database data source
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
697
    
version 0.0901
yuki-kimoto authored on 2009-12-17
698
    $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
699
    $data_source = $dbi->data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
700
    
version 0.0901
yuki-kimoto authored on 2009-12-17
701
If you know data source more, See also L<DBI>.
702

            
packaging one directory
yuki-kimoto authored on 2009-11-16
703
=head2 database
704

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

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

            
add port and host method
yuki-kimoto authored on 2009-11-16
710
=head2 host
711

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

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

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

            
719
=head2 port
720

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

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

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

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

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

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

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

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
746
    $dbi     = $dbi->filters({filter1 => sub { }, filter2 => sub {}});
747
    $filters = $dbi->filters;
packaging one directory
yuki-kimoto authored on 2009-11-16
748
    
version 0.0901
yuki-kimoto authored on 2009-12-17
749
This method is generally used to get a filter.
750

            
751
    $filter = $dbi->filters->{encode_utf8};
752

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

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

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

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

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

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

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

            
768
=head2 bind_filter
769

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

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

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

            
777
    $dbi->bind_filter(sub {
778
        my ($value, $key, $dbi, $infos) = @_;
779
        
780
        # edit $value
781
        
782
        return $value;
783
    });
packaging one directory
yuki-kimoto authored on 2009-11-16
784

            
version 0.0901
yuki-kimoto authored on 2009-12-17
785
Bind filter arguemts is
786

            
787
    1. $value : Value
788
    2. $key   : Key
789
    3. $dbi   : DBIx::Custom object
790
    4. $infos : {table => $table, column => $column}
791

            
packaging one directory
yuki-kimoto authored on 2009-11-16
792
=head2 fetch_filter
793

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

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

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

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
801
    $dbi->fetch_filter(sub {
802
        my ($value, $key, $dbi, $infos) = @_;
803
        
804
        # edit $value
805
        
806
        return $value;
807
    });
packaging one directory
yuki-kimoto authored on 2009-11-16
808

            
version 0.0901
yuki-kimoto authored on 2009-12-17
809
Bind filter arguemts is
810

            
811
    1. $value : Value
812
    2. $key   : Key
813
    3. $dbi   : DBIx::Custom object
814
    4. $infos : {type => $table, sth => $sth, index => $index}
815

            
packaging one directory
yuki-kimoto authored on 2009-11-16
816
=head2 no_bind_filters
817

            
update document
yuki-kimoto authored on 2010-01-30
818
Key list which dose not have to bind filtering
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
819
    
version 0.0901
yuki-kimoto authored on 2009-12-17
820
    $dbi             = $dbi->no_bind_filters(qw/title author/);
821
    $no_bind_filters = $dbi->no_bind_filters;
packaging one directory
yuki-kimoto authored on 2009-11-16
822

            
823
=head2 no_fetch_filters
824

            
update document
yuki-kimoto authored on 2010-01-30
825
Key list which dose not have to fetch filtering
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
826

            
version 0.0901
yuki-kimoto authored on 2009-12-17
827
    $dbi              = $dbi->no_fetch_filters(qw/title author/);
828
    $no_fetch_filters = $dbi->no_fetch_filters;
packaging one directory
yuki-kimoto authored on 2009-11-16
829

            
830
=head2 result_class
831

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
839
=head2 dbh
840

            
update document
yuki-kimoto authored on 2010-01-30
841
Database handle
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
842
    
version 0.0901
yuki-kimoto authored on 2009-12-17
843
    $dbi = $dbi->dbh($dbh);
844
    $dbh = $dbi->dbh;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
845
    
846
=head2 query_cache_max
847

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
850
    $class           = DBIx::Custom->query_cache_max(50);
851
    $query_cache_max = DBIx::Custom->query_cache_max;
852

            
853
Default value is 50
854

            
update document
yuki-kimoto authored on 2010-01-30
855
=head1 METHODS
856

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

            
860
=head2 connect
861

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

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

            
866
=head2 disconnect
867

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
868
Disconnect database
869

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

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

            
874
=head2 reconnect
875

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

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

            
880
=head2 connected
881

            
version 0.0901
yuki-kimoto authored on 2009-12-17
882
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
883
    
version 0.0901
yuki-kimoto authored on 2009-12-17
884
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
885
    
886
=head2 filter_off
887

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
888
bind_filter and fitch_filter off
889
    
version 0.0901
yuki-kimoto authored on 2009-12-17
890
    $dbi->filter_off
packaging one directory
yuki-kimoto authored on 2009-11-16
891
    
version 0.0901
yuki-kimoto authored on 2009-12-17
892
This method is equeal to
packaging one directory
yuki-kimoto authored on 2009-11-16
893
    
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
894
    $dbi->bind_filter(undef);
895
    $dbi->fetch_filter(undef);
packaging one directory
yuki-kimoto authored on 2009-11-16
896

            
897
=head2 add_filter
898

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
899
Resist filter
packaging one directory
yuki-kimoto authored on 2009-11-16
900
    
version 0.0901
yuki-kimoto authored on 2009-12-17
901
    $dbi->add_filter($fname1 => $filter1, $fname => $filter2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
902
    
version 0.0901
yuki-kimoto authored on 2009-12-17
903
The following is add_filter sample
904

            
packaging one directory
yuki-kimoto authored on 2009-11-16
905
    $dbi->add_filter(
906
        encode_utf8 => sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
907
            my ($value, $key, $dbi, $infos) = @_;
908
            utf8::upgrade($value) unless Encode::is_utf8($value);
909
            return encode('UTF-8', $value);
packaging one directory
yuki-kimoto authored on 2009-11-16
910
        },
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
911
        decode_utf8 => sub {
912
            my ($value, $key, $dbi, $infos) = @_;
913
            return decode('UTF-8', $value)
packaging one directory
yuki-kimoto authored on 2009-11-16
914
        }
915
    );
916

            
917
=head2 add_format
918

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
927
=head2 create_query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
928
    
version 0.0901
yuki-kimoto authored on 2009-12-17
929
Create Query object parsing SQL template
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
930

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

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

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

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

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

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
947
    $result = $dbi->query("select * from authors where {= name} and {= age}", 
948
                          {author => 'taro', age => 19});
949
    
950
    while (my @row = $result->fetch) {
951
        # do something
952
    }
953

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

            
956
Return value of query method is L<DBIx::Custom::Result> object
957

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

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

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

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
964
    $dbi->transaction->run(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
965
        my $dbi = shift;
966
        
packaging one directory
yuki-kimoto authored on 2009-11-16
967
        # do something
968
    });
969

            
970
If transaction is success, commit is execute. 
971
If tranzation is died, rollback is execute.
972

            
version 0.0901
yuki-kimoto authored on 2009-12-17
973
=head2 create_table
974

            
975
Create table
976

            
977
    $dbi->create_table(
978
        'books',
979
        'name char(255)',
980
        'age  int'
981
    );
982

            
983
First argument is table name. Rest arguments is column definition.
984

            
985
=head2 drop_table
986

            
987
Drop table
988

            
989
    $dbi->drop_table('books');
990

            
packaging one directory
yuki-kimoto authored on 2009-11-16
991
=head2 insert
992

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
998
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
999
    
version 0.0901
yuki-kimoto authored on 2009-12-17
1000
The following is insert sample.
1001

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1004
You can add statement.
1005

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1008
=head2 update
1009

            
update document
yuki-kimoto authored on 2009-11-19
1010
Update rows
1011

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1021
You can add statement.
1022

            
1023
    $dbi->update('books', {title => 'Perl', author => 'Taro'},
1024
                 {id => 5}, "some statement");
1025

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1026
=head2 update_all
1027

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

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

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

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

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

            
1038
=head2 delete
1039

            
update document
yuki-kimoto authored on 2009-11-19
1040
Delete rows
1041

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

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

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1055
=head2 delete_all
1056

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

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

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

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

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

            
1067
=head2 select
1068
    
update document
yuki-kimoto authored on 2009-11-19
1069
Select rows
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1070

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

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

            
1081
The following is some select samples
1082

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

            
1097
You can join multi tables
1098
    
1099
    $result = $dbi->select(
1100
        ['table1', 'table2'],                # tables
1101
        ['table1.id as table1_id', 'title'], # columns (alias is ok)
1102
        {table1.id => 1},                    # where clase
1103
        "where table1.id = table2.id",       # join clause (must start 'where')
1104
    );
1105

            
1106
You can also edit query
1107
        
packaging one directory
yuki-kimoto authored on 2009-11-16
1108
    $dbi->select(
update document
yuki-kimoto authored on 2009-11-19
1109
        'books',
1110
        # column, where clause, append statement,
packaging one directory
yuki-kimoto authored on 2009-11-16
1111
        sub {
1112
            my $query = shift;
1113
            $query->bind_filter(sub {
1114
                # ...
1115
            });
1116
        }
update document
yuki-kimoto authored on 2009-11-19
1117
    }
1118

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1119
=head2 prepare
1120

            
1121
Prepare statement handle.
1122

            
1123
    $sth = $dbi->prepare('select * from books;');
1124

            
1125
This method is same as DBI prepare method.
1126

            
1127
See also L<DBI>.
1128

            
1129
=head2 do
1130

            
1131
Execute SQL
1132

            
1133
    $affected = $dbi->do('insert into books (title, author) values (?, ?)',
1134
                        'Perl', 'taro');
1135

            
1136
Retrun value is affected rows count.
1137

            
1138
This method is same as DBI do method.
1139

            
1140
See also L<DBI>
1141

            
1142
=head1 DBIx::Custom default configuration
packaging one directory
yuki-kimoto authored on 2009-11-16
1143

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

            
1147
    1. AutoCommit is true
1148
    2. RaiseError is true
1149

            
1150
By default, Both AutoCommit and RaiseError is true.
1151
You must not change these mode not to damage your data.
1152

            
1153
If you change these mode, 
1154
you cannot get correct error message, 
1155
or run_transaction may fail.
1156

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

            
1159
DBIx::Custom is customizable DBI.
1160
You can inherit DBIx::Custom and custumize attributes.
1161

            
1162
    package DBIx::Custom::Yours;
1163
    use base DBIx::Custom;
1164
    
1165
    my $class = __PACKAGE__;
1166
    
1167
    $class->user('your_name');
1168
    $class->password('your_password');
1169

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1170
=head1 AUTHOR
1171

            
1172
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
1173

            
1174
Github L<http://github.com/yuki-kimoto>
1175

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

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

            
1180
Copyright 2009 Yuki Kimoto, all rights reserved.
1181

            
1182
This program is free software; you can redistribute it and/or modify it
1183
under the same terms as Perl itself.
1184

            
1185
=cut