DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1168 lines | 26.962kb
cleanup
yuki-kimoto authored on 2009-12-22
1
package DBIx::Custom;
2

            
3
use strict;
4
use warnings;
5

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
6
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
7

            
packaging one directory
yuki-kimoto authored on 2009-11-16
8
use Carp 'croak';
9
use DBI;
10
use DBIx::Custom::Result;
many many changes
yuki-kimoto authored on 2010-04-30
11
use DBIx::Custom::SQLTemplate;
cleanup
yuki-kimoto authored on 2010-02-11
12
use DBIx::Custom::Query;
packaging one directory
yuki-kimoto authored on 2009-11-16
13

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
131
sub create_query {
132
    my ($self, $template) = @_;
cleanup
yuki-kimoto authored on 2010-02-11
133
    
packaging one directory
yuki-kimoto authored on 2009-11-16
134
    my $class = ref $self;
135
    
cleanup
yuki-kimoto authored on 2010-02-11
136
    if (ref $template eq 'ARRAY') {
137
        $template = $template->[1];
138
    }
139
    
packaging one directory
yuki-kimoto authored on 2009-11-16
140
    # Create query from SQL template
version 0.0901
yuki-kimoto authored on 2009-12-17
141
    my $sql_tmpl = $self->sql_tmpl;
packaging one directory
yuki-kimoto authored on 2009-11-16
142
    
143
    # Try to get cached query
many many changes
yuki-kimoto authored on 2010-04-30
144
    my $cached_query = $class->_query_caches->{"$template"};
packaging one directory
yuki-kimoto authored on 2009-11-16
145
    
146
    # Create query
fix timeformat tests
yuki-kimoto authored on 2009-11-23
147
    my $query;
cleanup
yuki-kimoto authored on 2010-02-11
148
    if ($cached_query) {
149
        $query = DBIx::Custom::Query->new(
150
            sql       => $cached_query->sql,
simplify filtering system
yuki-kimoto authored on 2010-05-01
151
            columns => $cached_query->columns
cleanup
yuki-kimoto authored on 2010-02-11
152
        );
fix timeformat tests
yuki-kimoto authored on 2009-11-23
153
    }
154
    else {
Simplify key search
yuki-kimoto authored on 2010-02-11
155
        $query = eval{$sql_tmpl->create_query($template)};
packaging one directory
yuki-kimoto authored on 2009-11-16
156
        croak($@) if $@;
157
        
many many changes
yuki-kimoto authored on 2010-04-30
158
        $class->_add_query_cache("$template", $query);
packaging one directory
yuki-kimoto authored on 2009-11-16
159
    }
160
    
161
    # Connect if not
162
    $self->connect unless $self->connected;
163
    
164
    # Prepare statement handle
add all tests
yuki-kimoto authored on 2010-05-01
165
    my $sth = $self->dbh->prepare($query->{sql});
packaging one directory
yuki-kimoto authored on 2009-11-16
166
    
167
    # Set statement handle
168
    $query->sth($sth);
169
    
170
    return $query;
171
}
172

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
173
sub execute{
many many changes
yuki-kimoto authored on 2010-04-30
174
    my ($self, $query, $params, $args)  = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
175
    $params ||= {};
176
    
177
    # First argument is SQL template
Simplify key search
yuki-kimoto authored on 2010-02-11
178
    unless (ref $query eq 'DBIx::Custom::Query') {
179
        my $template;
180
        
181
        if (ref $query eq 'ARRAY') {
many many changes
yuki-kimoto authored on 2010-04-30
182
            $template = $query->[0];
Simplify key search
yuki-kimoto authored on 2010-02-11
183
        }
184
        else { $template = $query }
185
        
many many changes
yuki-kimoto authored on 2010-04-30
186
        $query = $self->create_query($template);
packaging one directory
yuki-kimoto authored on 2009-11-16
187
    }
many many changes
yuki-kimoto authored on 2010-04-30
188

            
many change
yuki-kimoto authored on 2010-04-30
189
    # Filter
190
    my $filter = $args->{filter} || $query->filter || {};
191

            
packaging one directory
yuki-kimoto authored on 2009-11-16
192
    # Create bind value
many many changes
yuki-kimoto authored on 2010-04-30
193
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
194
    
195
    # Execute
version 0.0901
yuki-kimoto authored on 2009-12-17
196
    my $sth      = $query->sth;
197
    my $affected = eval{$sth->execute(@$bind_values)};
packaging one directory
yuki-kimoto authored on 2009-11-16
198
    
199
    # Execute error
200
    if (my $execute_error = $@) {
201
        require Data::Dumper;
202
        my $sql              = $query->{sql} || '';
203
        my $params_dump      = Data::Dumper->Dump([$params], ['*params']);
204
        
205
        croak("$execute_error" . 
206
              "<Your SQL>\n$sql\n" . 
207
              "<Your parameters>\n$params_dump");
208
    }
209
    
210
    # Return resultset if select statement is executed
211
    if ($sth->{NUM_OF_FIELDS}) {
212
        
213
        # Get result class
214
        my $result_class = $self->result_class;
215
        
216
        # Create result
217
        my $result = $result_class->new({
many many changes
yuki-kimoto authored on 2010-04-30
218
            sth             => $sth,
219
            default_filter  => $self->default_fetch_filter,
220
            filters         => $self->filters
packaging one directory
yuki-kimoto authored on 2009-11-16
221
        });
222
        return $result;
223
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
224
    return $affected;
packaging one directory
yuki-kimoto authored on 2009-11-16
225
}
226

            
227
sub _build_bind_values {
many many changes
yuki-kimoto authored on 2010-04-30
228
    my ($self, $query, $params, $filter) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
229
    
230
    # binding values
231
    my @bind_values;
232
    
Simplify key search
yuki-kimoto authored on 2010-02-11
233
    # Build bind values
simplify filtering system
yuki-kimoto authored on 2010-05-01
234
    my $count = {};
235
    foreach my $column (@{$query->columns}) {
packaging one directory
yuki-kimoto authored on 2009-11-16
236
        
add query filter error check
yuki-kimoto authored on 2010-05-14
237
        croak "\"$column\" is not exists in params"
238
          unless exists $params->{$column};
239
        
Simplify key search
yuki-kimoto authored on 2010-02-11
240
        # Value
add query filter error check
yuki-kimoto authored on 2010-05-14
241
        my $value = ref $params->{$column} eq 'ARRAY'
simplify filtering system
yuki-kimoto authored on 2010-05-01
242
                  ? $params->{$column}->[$count->{$column} || 0]
243
                  : $params->{$column};
packaging one directory
yuki-kimoto authored on 2009-11-16
244
        
Simplify key search
yuki-kimoto authored on 2010-02-11
245
        # Filter
simplify filtering system
yuki-kimoto authored on 2010-05-01
246
        $filter ||= {};
many many changes
yuki-kimoto authored on 2010-04-30
247
        
simplify filtering system
yuki-kimoto authored on 2010-05-01
248
        # Filter name
249
        my $fname = $filter->{$column} || $self->default_query_filter || '';
250
        
add query filter error check
yuki-kimoto authored on 2010-05-14
251
        my $filter_func;
252
        if ($fname) {
253
            
254
            if (ref $fname eq 'CODE') {
255
                $filter_func = $fname;
256
            }
257
            else {
258
                my $filters = $self->filters;
259
                croak "Not exists filter \"$fname\"" unless exists $filters->{$fname};
260
                $filter_func = $filters->{$fname};
261
            }            
262
        }
263
        
264
        push @bind_values, $filter_func
265
                         ? $filter_func->($value)
many change
yuki-kimoto authored on 2010-04-30
266
                         : $value;
simplify filtering system
yuki-kimoto authored on 2010-05-01
267
        
268
        # Count up 
269
        $count->{$column}++;
cleanup
yuki-kimoto authored on 2010-02-11
270
    }
271
    
Simplify key search
yuki-kimoto authored on 2010-02-11
272
    return \@bind_values;
cleanup
yuki-kimoto authored on 2010-02-11
273
}
274

            
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
275
sub run_transaction {
276
    my ($self, $transaction) = @_;
277
    
278
    # Shorcut
many change
yuki-kimoto authored on 2010-02-11
279
    return unless $self;
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
280
    
281
    # Check auto commit
282
    croak("AutoCommit must be true before transaction start")
many change
yuki-kimoto authored on 2010-02-11
283
      unless $self->_auto_commit;
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
284
    
285
    # Auto commit off
many change
yuki-kimoto authored on 2010-02-11
286
    $self->_auto_commit(0);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
287
    
288
    # Run transaction
289
    eval {$transaction->()};
290
    
291
    # Tranzaction error
292
    my $transaction_error = $@;
293
    
294
    # Tranzaction is failed.
295
    if ($transaction_error) {
296
        # Rollback
many change
yuki-kimoto authored on 2010-02-11
297
        eval{$self->dbh->rollback};
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
298
        
299
        # Rollback error
300
        my $rollback_error = $@;
301
        
302
        # Auto commit on
many change
yuki-kimoto authored on 2010-02-11
303
        $self->_auto_commit(1);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
304
        
305
        if ($rollback_error) {
306
            # Rollback is failed
307
            croak("${transaction_error}Rollback is failed : $rollback_error");
308
        }
309
        else {
310
            # Rollback is success
311
            croak("${transaction_error}Rollback is success");
312
        }
313
    }
314
    # Tranzaction is success
315
    else {
316
        # Commit
many change
yuki-kimoto authored on 2010-02-11
317
        eval{$self->dbh->commit};
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
318
        my $commit_error = $@;
319
        
320
        # Auto commit on
many change
yuki-kimoto authored on 2010-02-11
321
        $self->_auto_commit(1);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
322
        
323
        # Commit is failed
324
        croak($commit_error) if $commit_error;
325
    }
326
}
327

            
version 0.0901
yuki-kimoto authored on 2009-12-17
328
sub create_table {
329
    my ($self, $table, @column_definitions) = @_;
330
    
331
    # Create table
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
332
    my $sql = "create table $table (";
version 0.0901
yuki-kimoto authored on 2009-12-17
333
    
334
    # Column definitions
335
    foreach my $column_definition (@column_definitions) {
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
336
        $sql .= "$column_definition,";
version 0.0901
yuki-kimoto authored on 2009-12-17
337
    }
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
338
    $sql =~ s/,$//;
version 0.0901
yuki-kimoto authored on 2009-12-17
339
    
340
    # End
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
341
    $sql .= ");";
version 0.0901
yuki-kimoto authored on 2009-12-17
342
    
add all tests
yuki-kimoto authored on 2010-05-01
343
    # Connect
344
    $self->connect unless $self->connected;
345
    
version 0.0901
yuki-kimoto authored on 2009-12-17
346
    # Do query
add all tests
yuki-kimoto authored on 2010-05-01
347
    return $self->dbh->do($sql);
version 0.0901
yuki-kimoto authored on 2009-12-17
348
}
349

            
350
sub drop_table {
351
    my ($self, $table) = @_;
352
    
353
    # Drop table
354
    my $sql = "drop table $table;";
355

            
add all tests
yuki-kimoto authored on 2010-05-01
356
    # Connect
357
    $self->connect unless $self->connected;
358

            
version 0.0901
yuki-kimoto authored on 2009-12-17
359
    # Do query
add all tests
yuki-kimoto authored on 2010-05-01
360
    return $self->dbh->do($sql);
version 0.0901
yuki-kimoto authored on 2009-12-17
361
}
362

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
365
sub insert {
cleanup insert
yuki-kimoto authored on 2010-04-28
366
    my ($self, $table, $insert_params, $args) = @_;
367
    
368
    # Table
369
    $table ||= '';
370
    
371
    # Insert params
372
    $insert_params ||= {};
373
    
374
    # Arguments
375
    $args ||= {};
376
    
377
    # Check arguments
378
    foreach my $name (keys %$args) {
379
        croak "\"$name\" is invalid name"
380
          unless $VALID_INSERT_ARGS{$name};
381
    }
382
    
383
    my $append_statement = $args->{append} || '';
many many changes
yuki-kimoto authored on 2010-04-30
384
    my $filter           = $args->{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
385
    
386
    # Insert keys
387
    my @insert_keys = keys %$insert_params;
388
    
389
    # Not exists insert keys
390
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
391
      unless @insert_keys;
392
    
393
    # Templte for insert
394
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
395
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
396
    
397
    # Execute query
rename query() to execute()
yuki-kimoto authored on 2010-05-01
398
    my $ret_val = $self->execute($template, $insert_params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
399
    
400
    return $ret_val;
401
}
402

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

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

            
475
sub update_all {
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
476
    my ($self, $table, $update_params, $args) = @_;
477
    
refactoring select
yuki-kimoto authored on 2010-04-28
478
    # Allow all update
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
479
    $args ||= {};
480
    $args->{allow_update_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
481
    
update document
yuki-kimoto authored on 2010-01-30
482
    # Update all rows
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
483
    return $self->update($table, $update_params, $args);
packaging one directory
yuki-kimoto authored on 2009-11-16
484
}
485

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
489
sub delete {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
490
    my ($self, $table, $args) = @_;
491
    
492
    # Table
493
    $table            ||= '';
494

            
495
    # Check arguments
496
    foreach my $name (keys %$args) {
497
        croak "\"$name\" is invalid name"
498
          unless $VALID_DELETE_ARGS{$name};
499
    }
500
    
501
    # Arguments
502
    my $where_params     = $args->{where} || {};
503
    my $append_statement = $args->{append};
many many changes
yuki-kimoto authored on 2010-04-30
504
    my $filter    = $args->{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
505
    my $allow_delete_all = $args->{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
506
    
507
    # Where keys
508
    my @where_keys = keys %$where_params;
509
    
510
    # Not exists where keys
511
    croak("Key-value pairs for where clause must be specified to 'delete' second argument")
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
512
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
513
    
514
    # Where clause
515
    my $where_clause = '';
516
    if (@where_keys) {
517
        $where_clause = 'where ';
518
        foreach my $where_key (@where_keys) {
519
            $where_clause .= "{= $where_key} and ";
520
        }
521
        $where_clause =~ s/ and $//;
522
    }
523
    
524
    # Template for delete
525
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
526
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
527
    
528
    # Execute query
rename query() to execute()
yuki-kimoto authored on 2010-05-01
529
    my $ret_val = $self->execute($template, $where_params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
530
    
531
    return $ret_val;
532
}
533

            
534
sub delete_all {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
535
    my ($self, $table, $args) = @_;
536
    
refactoring select
yuki-kimoto authored on 2010-04-28
537
    # Allow all delete
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
538
    $args ||= {};
539
    $args->{allow_delete_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
540
    
update document
yuki-kimoto authored on 2010-01-30
541
    # Delete all rows
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
542
    return $self->delete($table, $args);
packaging one directory
yuki-kimoto authored on 2009-11-16
543
}
544

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
548
sub select {
refactoring select
yuki-kimoto authored on 2010-04-28
549
    my ($self, $tables, $args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
550
    
refactoring select
yuki-kimoto authored on 2010-04-28
551
    # Table
552
    $tables ||= '';
553
    $tables = [$tables] unless ref $tables;
packaging one directory
yuki-kimoto authored on 2009-11-16
554
    
refactoring select
yuki-kimoto authored on 2010-04-28
555
    # Check arguments
556
    foreach my $name (keys %$args) {
557
        croak "\"$name\" is invalid name"
558
          unless $VALID_SELECT_ARGS{$name};
559
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
560
    
refactoring select
yuki-kimoto authored on 2010-04-28
561
    # Arguments
562
    my $columns          = $args->{columns} || [];
563
    my $where_params     = $args->{where} || {};
564
    my $append_statement = $args->{append} || '';
many many changes
yuki-kimoto authored on 2010-04-30
565
    my $filter    = $args->{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
566
    
567
    # SQL template for select statement
568
    my $template = 'select ';
569
    
570
    # Join column clause
571
    if (@$columns) {
572
        foreach my $column (@$columns) {
573
            $template .= "$column, ";
574
        }
575
        $template =~ s/, $/ /;
576
    }
577
    else {
578
        $template .= '* ';
579
    }
580
    
581
    # Join table
582
    $template .= 'from ';
583
    foreach my $table (@$tables) {
584
        $template .= "$table, ";
585
    }
586
    $template =~ s/, $/ /;
587
    
588
    # Where clause keys
589
    my @where_keys = keys %$where_params;
590
    
591
    # Join where clause
592
    if (@where_keys) {
593
        $template .= 'where ';
594
        foreach my $where_key (@where_keys) {
compile success
yuki-kimoto authored on 2010-05-01
595
            $template .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
596
        }
597
    }
598
    $template =~ s/ and $//;
599
    
600
    # Append something to last of statement
601
    if ($append_statement =~ s/^where //) {
602
        if (@where_keys) {
603
            $template .= " and $append_statement";
604
        }
605
        else {
606
            $template .= " where $append_statement";
607
        }
608
    }
609
    else {
610
        $template .= " $append_statement";
611
    }
612
    
613
    # Execute query
rename query() to execute()
yuki-kimoto authored on 2010-05-01
614
    my $result = $self->execute($template, $where_params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
615
    
616
    return $result;
617
}
618

            
619
sub _add_query_cache {
620
    my ($class, $template, $query) = @_;
update document
yuki-kimoto authored on 2010-01-30
621
    
622
    # Query information
packaging one directory
yuki-kimoto authored on 2009-11-16
623
    my $query_cache_keys = $class->_query_cache_keys;
624
    my $query_caches     = $class->_query_caches;
625
    
update document
yuki-kimoto authored on 2010-01-30
626
    # Already cached
packaging one directory
yuki-kimoto authored on 2009-11-16
627
    return $class if $query_caches->{$template};
628
    
update document
yuki-kimoto authored on 2010-01-30
629
    # Cache
packaging one directory
yuki-kimoto authored on 2009-11-16
630
    $query_caches->{$template} = $query;
631
    push @$query_cache_keys, $template;
632
    
update document
yuki-kimoto authored on 2010-01-30
633
    # Check cache overflow
packaging one directory
yuki-kimoto authored on 2009-11-16
634
    my $overflow = @$query_cache_keys - $class->query_cache_max;
635
    for (my $i = 0; $i < $overflow; $i++) {
636
        my $template = shift @$query_cache_keys;
637
        delete $query_caches->{$template};
638
    }
639
    
640
    return $class;
641
}
642

            
643
=head1 NAME
644

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
645
DBIx::Custom - DBI with hash bind and filtering system 
packaging one directory
yuki-kimoto authored on 2009-11-16
646

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

            
some changed
yuki-kimoto authored on 2010-05-02
649
Version 0.1402
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
650

            
651
=cut
652

            
some changed
yuki-kimoto authored on 2010-05-02
653
our $VERSION = '0.1402';
654
$VERSION = eval $VERSION;
packaging one directory
yuki-kimoto authored on 2009-11-16
655

            
cleanup
yuki-kimoto authored on 2010-02-11
656
=head1 STATE
657

            
658
This module is not stable. Method name and functionality will be change.
659

            
version 0.0901
yuki-kimoto authored on 2009-12-17
660
=head1 SYNOPSYS
661
    
662
    # New
663
    my $dbi = DBIx::Custom->new(data_source => "dbi:mysql:database=books"
664
                                user => 'ken', password => '!LFKD%$&');
665
    
666
    # Query
rename query() to execute()
yuki-kimoto authored on 2010-05-01
667
    $dbi->execute("select title from books");
version 0.0901
yuki-kimoto authored on 2009-12-17
668
    
669
    # Query with parameters
rename query() to execute()
yuki-kimoto authored on 2010-05-01
670
    $dbi->execute("select id from books where {= author} && {like title}",
version 0.0901
yuki-kimoto authored on 2009-12-17
671
                {author => 'ken', title => '%Perl%'});
672
    
some changed
yuki-kimoto authored on 2010-05-02
673
    
674
    
version 0.0901
yuki-kimoto authored on 2009-12-17
675
    # Insert 
676
    $dbi->insert('books', {title => 'perl', author => 'Ken'});
677
    
678
    # Update 
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
679
    $dbi->update('books', {title => 'aaa', author => 'Ken'}, {where => {id => 5}});
version 0.0901
yuki-kimoto authored on 2009-12-17
680
    
681
    # Delete
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
682
    $dbi->delete('books', {where => {author => 'Ken'}});
version 0.0901
yuki-kimoto authored on 2009-12-17
683
    
684
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
685
    my $result = $dbi->select('books');
686
    my $result = $dbi->select('books', {where => {author => 'taro'}}); 
687
    
688
    my $result = $dbi->select(
689
       'books', 
690
       {
691
           columns => [qw/author title/],
692
           where   => {author => 'Ken'}
693
        }
694
    );
695
    
696
    my $result = $dbi->select(
697
        'books',
698
        {
699
            columns => [qw/author title/],
700
            where   => {author => 'Ken'},
701
            append  => 'order by id limit 1'
702
        }
703
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
704

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

            
707
=head2 user
708

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

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

            
721
=head2 data_source
722

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

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

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

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

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

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

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

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

            
746
=head2 port
747

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

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

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

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

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

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

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

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

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

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

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

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

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

            
some changed
yuki-kimoto authored on 2010-05-02
780
If you add filter, use register_filter method.
packaging one directory
yuki-kimoto authored on 2009-11-16
781

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

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

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

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

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

            
some changed
yuki-kimoto authored on 2010-05-02
793
If you add format, use register_format method.
packaging one directory
yuki-kimoto authored on 2009-11-16
794

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
802
The following is bind filter example
cleanup
yuki-kimoto authored on 2010-04-28
803
    
some changed
yuki-kimoto authored on 2010-05-02
804
    $dbi->register_filter(encode_utf8 => sub {
cleanup
yuki-kimoto authored on 2010-04-28
805
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
806
        
cleanup
yuki-kimoto authored on 2010-04-28
807
        require Encode 'encode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
808
        
cleanup
yuki-kimoto authored on 2010-04-28
809
        return encode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
810
    });
cleanup
yuki-kimoto authored on 2010-04-28
811
    
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
812
    $dbi->default_query_filter('encode_utf8')
packaging one directory
yuki-kimoto authored on 2009-11-16
813

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

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

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
828
The following is fetch filter example
packaging one directory
yuki-kimoto authored on 2009-11-16
829

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

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

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

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

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

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

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

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

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

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

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

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

            
870
Default value is 50
871

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

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

            
877
=head2 connect
878

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

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

            
883
=head2 disconnect
884

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

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

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

            
891
=head2 reconnect
892

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

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

            
897
=head2 connected
898

            
version 0.0901
yuki-kimoto authored on 2009-12-17
899
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
900
    
version 0.0901
yuki-kimoto authored on 2009-12-17
901
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
902
    
some changed
yuki-kimoto authored on 2010-05-02
903
=head2 register_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
904

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
905
Resist filter
packaging one directory
yuki-kimoto authored on 2009-11-16
906
    
some changed
yuki-kimoto authored on 2010-05-02
907
    $dbi->register_filter($fname1 => $filter1, $fname => $filter2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
908
    
some changed
yuki-kimoto authored on 2010-05-02
909
The following is register_filter example
version 0.0901
yuki-kimoto authored on 2009-12-17
910

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

            
some changed
yuki-kimoto authored on 2010-05-02
923
=head2 register_format
packaging one directory
yuki-kimoto authored on 2009-11-16
924

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

            
some changed
yuki-kimoto authored on 2010-05-02
927
    $dbi->register_format($fname1 => $format, $fname2 => $format2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
928
    
some changed
yuki-kimoto authored on 2010-05-02
929
The following is register_format example.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
930

            
some changed
yuki-kimoto authored on 2010-05-02
931
    $dbi->register_format(date => '%Y:%m:%d', datetime => '%Y-%m-%d %H:%M:%S');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
932

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
939
$query is <DBIx::Query> object. This is executed by query method as the following
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
940

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
941
    $dbi->execute($query, $params);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
942

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
945
=head2 execute
packaging one directory
yuki-kimoto authored on 2009-11-16
946

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
949
    $result = $dbi->execute($template, $params);
packaging one directory
yuki-kimoto authored on 2009-11-16
950

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
951
The following is query example
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
952

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
962
execute() return L<DBIx::Custom::Result> object
packaging one directory
yuki-kimoto authored on 2009-11-16
963

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

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

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

            
974
If transaction is success, commit is execute. 
975
If tranzation is died, rollback is execute.
976

            
version 0.0901
yuki-kimoto authored on 2009-12-17
977
=head2 create_table
978

            
979
Create table
980

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

            
987
First argument is table name. Rest arguments is column definition.
988

            
989
=head2 drop_table
990

            
991
Drop table
992

            
993
    $dbi->drop_table('books');
994

            
packaging one directory
yuki-kimoto authored on 2009-11-16
995
=head2 insert
996

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1002
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
1003
    
rename query() to execute()
yuki-kimoto authored on 2010-05-01
1004
The following is insert example.
version 0.0901
yuki-kimoto authored on 2009-12-17
1005

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1008
You can add statement.
1009

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1012
=head2 update
1013

            
update document
yuki-kimoto authored on 2009-11-19
1014
Update rows
1015

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
1021
The following is update example.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1022

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1025
You can add statement.
1026

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1030
=head2 update_all
1031

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
1038
The following is update_all example.
update document
yuki-kimoto authored on 2009-11-19
1039

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

            
1042
=head2 delete
1043

            
update document
yuki-kimoto authored on 2009-11-19
1044
Delete rows
1045

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1049
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
1050
    
rename query() to execute()
yuki-kimoto authored on 2010-05-01
1051
The following is delete example.
version 0.0901
yuki-kimoto authored on 2009-12-17
1052

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1055
You can add statement.
1056

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1059
=head2 delete_all
1060

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
1067
The following is delete_all example.
version 0.0901
yuki-kimoto authored on 2009-12-17
1068

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

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
1085
The following is some select examples
update document
yuki-kimoto authored on 2009-11-19
1086

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

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

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

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

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

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

            
1130
    1. AutoCommit is true
1131
    2. RaiseError is true
1132

            
1133
By default, Both AutoCommit and RaiseError is true.
1134
You must not change these mode not to damage your data.
1135

            
1136
If you change these mode, 
1137
you cannot get correct error message, 
1138
or run_transaction may fail.
1139

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

            
1142
DBIx::Custom is customizable DBI.
1143
You can inherit DBIx::Custom and custumize attributes.
1144

            
1145
    package DBIx::Custom::Yours;
1146
    use base DBIx::Custom;
1147
    
1148
    my $class = __PACKAGE__;
1149
    
1150
    $class->user('your_name');
1151
    $class->password('your_password');
1152

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1153
=head1 AUTHOR
1154

            
1155
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
1156

            
1157
Github L<http://github.com/yuki-kimoto>
1158

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

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

            
1163
Copyright 2009 Yuki Kimoto, all rights reserved.
1164

            
1165
This program is free software; you can redistribute it and/or modify it
1166
under the same terms as Perl itself.
1167

            
1168
=cut