DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1153 lines | 26.495kb
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
        
Simplify key search
yuki-kimoto authored on 2010-02-11
237
        # Value
simplify filtering system
yuki-kimoto authored on 2010-05-01
238
        my $value = ref $params->{$column}
239
                  ? $params->{$column}->[$count->{$column} || 0]
240
                  : $params->{$column};
packaging one directory
yuki-kimoto authored on 2009-11-16
241
        
Simplify key search
yuki-kimoto authored on 2010-02-11
242
        # Filter
simplify filtering system
yuki-kimoto authored on 2010-05-01
243
        $filter ||= {};
many many changes
yuki-kimoto authored on 2010-04-30
244
        
simplify filtering system
yuki-kimoto authored on 2010-05-01
245
        # Filter name
246
        my $fname = $filter->{$column} || $self->default_query_filter || '';
247
        
248
        my $filters = $self->filters;
many change
yuki-kimoto authored on 2010-04-30
249
        push @bind_values, $filters->{$fname}
250
                         ? $filters->{$fname}->($value)
251
                         : $value;
simplify filtering system
yuki-kimoto authored on 2010-05-01
252
        
253
        # Count up 
254
        $count->{$column}++;
cleanup
yuki-kimoto authored on 2010-02-11
255
    }
256
    
Simplify key search
yuki-kimoto authored on 2010-02-11
257
    return \@bind_values;
cleanup
yuki-kimoto authored on 2010-02-11
258
}
259

            
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
260
sub run_transaction {
261
    my ($self, $transaction) = @_;
262
    
263
    # Shorcut
many change
yuki-kimoto authored on 2010-02-11
264
    return unless $self;
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
265
    
266
    # Check auto commit
267
    croak("AutoCommit must be true before transaction start")
many change
yuki-kimoto authored on 2010-02-11
268
      unless $self->_auto_commit;
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
269
    
270
    # Auto commit off
many change
yuki-kimoto authored on 2010-02-11
271
    $self->_auto_commit(0);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
272
    
273
    # Run transaction
274
    eval {$transaction->()};
275
    
276
    # Tranzaction error
277
    my $transaction_error = $@;
278
    
279
    # Tranzaction is failed.
280
    if ($transaction_error) {
281
        # Rollback
many change
yuki-kimoto authored on 2010-02-11
282
        eval{$self->dbh->rollback};
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
283
        
284
        # Rollback error
285
        my $rollback_error = $@;
286
        
287
        # Auto commit on
many change
yuki-kimoto authored on 2010-02-11
288
        $self->_auto_commit(1);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
289
        
290
        if ($rollback_error) {
291
            # Rollback is failed
292
            croak("${transaction_error}Rollback is failed : $rollback_error");
293
        }
294
        else {
295
            # Rollback is success
296
            croak("${transaction_error}Rollback is success");
297
        }
298
    }
299
    # Tranzaction is success
300
    else {
301
        # Commit
many change
yuki-kimoto authored on 2010-02-11
302
        eval{$self->dbh->commit};
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
303
        my $commit_error = $@;
304
        
305
        # Auto commit on
many change
yuki-kimoto authored on 2010-02-11
306
        $self->_auto_commit(1);
remove DBIx::Custom::Transac...
yuki-kimoto authored on 2010-02-11
307
        
308
        # Commit is failed
309
        croak($commit_error) if $commit_error;
310
    }
311
}
312

            
version 0.0901
yuki-kimoto authored on 2009-12-17
313
sub create_table {
314
    my ($self, $table, @column_definitions) = @_;
315
    
316
    # Create table
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
317
    my $sql = "create table $table (";
version 0.0901
yuki-kimoto authored on 2009-12-17
318
    
319
    # Column definitions
320
    foreach my $column_definition (@column_definitions) {
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
321
        $sql .= "$column_definition,";
version 0.0901
yuki-kimoto authored on 2009-12-17
322
    }
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
323
    $sql =~ s/,$//;
version 0.0901
yuki-kimoto authored on 2009-12-17
324
    
325
    # End
rename fetch_first to fetch_...
yuki-kimoto authored on 2010-01-29
326
    $sql .= ");";
version 0.0901
yuki-kimoto authored on 2009-12-17
327
    
add all tests
yuki-kimoto authored on 2010-05-01
328
    # Connect
329
    $self->connect unless $self->connected;
330
    
version 0.0901
yuki-kimoto authored on 2009-12-17
331
    # Do query
add all tests
yuki-kimoto authored on 2010-05-01
332
    return $self->dbh->do($sql);
version 0.0901
yuki-kimoto authored on 2009-12-17
333
}
334

            
335
sub drop_table {
336
    my ($self, $table) = @_;
337
    
338
    # Drop table
339
    my $sql = "drop table $table;";
340

            
add all tests
yuki-kimoto authored on 2010-05-01
341
    # Connect
342
    $self->connect unless $self->connected;
343

            
version 0.0901
yuki-kimoto authored on 2009-12-17
344
    # Do query
add all tests
yuki-kimoto authored on 2010-05-01
345
    return $self->dbh->do($sql);
version 0.0901
yuki-kimoto authored on 2009-12-17
346
}
347

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
350
sub insert {
cleanup insert
yuki-kimoto authored on 2010-04-28
351
    my ($self, $table, $insert_params, $args) = @_;
352
    
353
    # Table
354
    $table ||= '';
355
    
356
    # Insert params
357
    $insert_params ||= {};
358
    
359
    # Arguments
360
    $args ||= {};
361
    
362
    # Check arguments
363
    foreach my $name (keys %$args) {
364
        croak "\"$name\" is invalid name"
365
          unless $VALID_INSERT_ARGS{$name};
366
    }
367
    
368
    my $append_statement = $args->{append} || '';
many many changes
yuki-kimoto authored on 2010-04-30
369
    my $filter           = $args->{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
370
    
371
    # Insert keys
372
    my @insert_keys = keys %$insert_params;
373
    
374
    # Not exists insert keys
375
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
376
      unless @insert_keys;
377
    
378
    # Templte for insert
379
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
380
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
381
    
382
    # Execute query
rename query() to execute()
yuki-kimoto authored on 2010-05-01
383
    my $ret_val = $self->execute($template, $insert_params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
384
    
385
    return $ret_val;
386
}
387

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

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

            
460
sub update_all {
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
461
    my ($self, $table, $update_params, $args) = @_;
462
    
refactoring select
yuki-kimoto authored on 2010-04-28
463
    # Allow all update
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
464
    $args ||= {};
465
    $args->{allow_update_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
466
    
update document
yuki-kimoto authored on 2010-01-30
467
    # Update all rows
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
468
    return $self->update($table, $update_params, $args);
packaging one directory
yuki-kimoto authored on 2009-11-16
469
}
470

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
474
sub delete {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
475
    my ($self, $table, $args) = @_;
476
    
477
    # Table
478
    $table            ||= '';
479

            
480
    # Check arguments
481
    foreach my $name (keys %$args) {
482
        croak "\"$name\" is invalid name"
483
          unless $VALID_DELETE_ARGS{$name};
484
    }
485
    
486
    # Arguments
487
    my $where_params     = $args->{where} || {};
488
    my $append_statement = $args->{append};
many many changes
yuki-kimoto authored on 2010-04-30
489
    my $filter    = $args->{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
490
    my $allow_delete_all = $args->{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
491
    
492
    # Where keys
493
    my @where_keys = keys %$where_params;
494
    
495
    # Not exists where keys
496
    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
497
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
498
    
499
    # Where clause
500
    my $where_clause = '';
501
    if (@where_keys) {
502
        $where_clause = 'where ';
503
        foreach my $where_key (@where_keys) {
504
            $where_clause .= "{= $where_key} and ";
505
        }
506
        $where_clause =~ s/ and $//;
507
    }
508
    
509
    # Template for delete
510
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
511
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
512
    
513
    # Execute query
rename query() to execute()
yuki-kimoto authored on 2010-05-01
514
    my $ret_val = $self->execute($template, $where_params, {filter => $filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
515
    
516
    return $ret_val;
517
}
518

            
519
sub delete_all {
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
520
    my ($self, $table, $args) = @_;
521
    
refactoring select
yuki-kimoto authored on 2010-04-28
522
    # Allow all delete
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
523
    $args ||= {};
524
    $args->{allow_delete_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
525
    
update document
yuki-kimoto authored on 2010-01-30
526
    # Delete all rows
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
527
    return $self->delete($table, $args);
packaging one directory
yuki-kimoto authored on 2009-11-16
528
}
529

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

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

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

            
628
=head1 NAME
629

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

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

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

            
636
=cut
637

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

            
cleanup
yuki-kimoto authored on 2010-02-11
641
=head1 STATE
642

            
643
This module is not stable. Method name and functionality will be change.
644

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

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

            
692
=head2 user
693

            
update document
yuki-kimoto authored on 2010-01-30
694
Database user name
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
695
    
version 0.0901
yuki-kimoto authored on 2009-12-17
696
    $dbi  = $dbi->user('Ken');
697
    $user = $dbi->user;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
698
    
packaging one directory
yuki-kimoto authored on 2009-11-16
699
=head2 password
700

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

            
706
=head2 data_source
707

            
update document
yuki-kimoto authored on 2010-01-30
708
Database data source
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
709
    
version 0.0901
yuki-kimoto authored on 2009-12-17
710
    $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
711
    $data_source = $dbi->data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
712
    
version 0.0901
yuki-kimoto authored on 2009-12-17
713
If you know data source more, See also L<DBI>.
714

            
packaging one directory
yuki-kimoto authored on 2009-11-16
715
=head2 database
716

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

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

            
add port and host method
yuki-kimoto authored on 2009-11-16
722
=head2 host
723

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

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

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

            
731
=head2 port
732

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

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

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

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

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

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

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

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
758
    $dbi     = $dbi->filters({filter1 => sub { }, filter2 => sub {}});
759
    $filters = $dbi->filters;
packaging one directory
yuki-kimoto authored on 2009-11-16
760
    
version 0.0901
yuki-kimoto authored on 2009-12-17
761
This method is generally used to get a filter.
762

            
763
    $filter = $dbi->filters->{encode_utf8};
764

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

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

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

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

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

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

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

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
787
The following is bind filter example
cleanup
yuki-kimoto authored on 2010-04-28
788
    
some changed
yuki-kimoto authored on 2010-05-02
789
    $dbi->register_filter(encode_utf8 => sub {
cleanup
yuki-kimoto authored on 2010-04-28
790
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
791
        
cleanup
yuki-kimoto authored on 2010-04-28
792
        require Encode 'encode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
793
        
cleanup
yuki-kimoto authored on 2010-04-28
794
        return encode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
795
    });
cleanup
yuki-kimoto authored on 2010-04-28
796
    
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
797
    $dbi->default_query_filter('encode_utf8')
packaging one directory
yuki-kimoto authored on 2009-11-16
798

            
version 0.0901
yuki-kimoto authored on 2009-12-17
799
Bind filter arguemts is
800

            
801
    1. $value : Value
802
    2. $key   : Key
803
    3. $dbi   : DBIx::Custom object
804
    4. $infos : {table => $table, column => $column}
805

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

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

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

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

            
some changed
yuki-kimoto authored on 2010-05-02
815
    $dbi->register_filter(decode_utf8 => sub {
cleanup
yuki-kimoto authored on 2010-04-28
816
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
817
        
cleanup
yuki-kimoto authored on 2010-04-28
818
        require Encode 'decode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
819
        
cleanup
yuki-kimoto authored on 2010-04-28
820
        return decode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
821
    });
packaging one directory
yuki-kimoto authored on 2009-11-16
822

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
825
Bind filter arguemts is
826

            
827
    1. $value : Value
828
    2. $key   : Key
829
    3. $dbi   : DBIx::Custom object
830
    4. $infos : {type => $table, sth => $sth, index => $index}
831

            
packaging one directory
yuki-kimoto authored on 2009-11-16
832
=head2 result_class
833

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
841
=head2 dbh
842

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

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

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

            
855
Default value is 50
856

            
update document
yuki-kimoto authored on 2010-01-30
857
=head1 METHODS
858

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

            
862
=head2 connect
863

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

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

            
868
=head2 disconnect
869

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
870
Disconnect database
871

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

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

            
876
=head2 reconnect
877

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

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

            
882
=head2 connected
883

            
version 0.0901
yuki-kimoto authored on 2009-12-17
884
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
885
    
version 0.0901
yuki-kimoto authored on 2009-12-17
886
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
887
    
some changed
yuki-kimoto authored on 2010-05-02
888
=head2 register_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
889

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
890
Resist filter
packaging one directory
yuki-kimoto authored on 2009-11-16
891
    
some changed
yuki-kimoto authored on 2010-05-02
892
    $dbi->register_filter($fname1 => $filter1, $fname => $filter2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
893
    
some changed
yuki-kimoto authored on 2010-05-02
894
The following is register_filter example
version 0.0901
yuki-kimoto authored on 2009-12-17
895

            
some changed
yuki-kimoto authored on 2010-05-02
896
    $dbi->register_filter(
packaging one directory
yuki-kimoto authored on 2009-11-16
897
        encode_utf8 => sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
898
            my ($value, $key, $dbi, $infos) = @_;
899
            utf8::upgrade($value) unless Encode::is_utf8($value);
900
            return encode('UTF-8', $value);
packaging one directory
yuki-kimoto authored on 2009-11-16
901
        },
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
902
        decode_utf8 => sub {
903
            my ($value, $key, $dbi, $infos) = @_;
904
            return decode('UTF-8', $value)
packaging one directory
yuki-kimoto authored on 2009-11-16
905
        }
906
    );
907

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

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

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

            
some changed
yuki-kimoto authored on 2010-05-02
916
    $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
917

            
version 0.0901
yuki-kimoto authored on 2009-12-17
918
=head2 create_query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
919
    
version 0.0901
yuki-kimoto authored on 2009-12-17
920
Create Query object parsing SQL template
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
921

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

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

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

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

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

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
938
    $result = $dbi->execute("select * from authors where {= name} and {= age}", 
packaging one directory
yuki-kimoto authored on 2009-11-16
939
                          {author => 'taro', age => 19});
940
    
941
    while (my @row = $result->fetch) {
942
        # do something
943
    }
944

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

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

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

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

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
953
    $dbi->transaction->run(sub {
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
954
        my $dbi = shift;
955
        
packaging one directory
yuki-kimoto authored on 2009-11-16
956
        # do something
957
    });
958

            
959
If transaction is success, commit is execute. 
960
If tranzation is died, rollback is execute.
961

            
version 0.0901
yuki-kimoto authored on 2009-12-17
962
=head2 create_table
963

            
964
Create table
965

            
966
    $dbi->create_table(
967
        'books',
968
        'name char(255)',
969
        'age  int'
970
    );
971

            
972
First argument is table name. Rest arguments is column definition.
973

            
974
=head2 drop_table
975

            
976
Drop table
977

            
978
    $dbi->drop_table('books');
979

            
packaging one directory
yuki-kimoto authored on 2009-11-16
980
=head2 insert
981

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
993
You can add statement.
994

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
997
=head2 update
998

            
update document
yuki-kimoto authored on 2009-11-19
999
Update rows
1000

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1010
You can add statement.
1011

            
1012
    $dbi->update('books', {title => 'Perl', author => 'Taro'},
1013
                 {id => 5}, "some statement");
1014

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1015
=head2 update_all
1016

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

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

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

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

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

            
1027
=head2 delete
1028

            
update document
yuki-kimoto authored on 2009-11-19
1029
Delete rows
1030

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1040
You can add statement.
1041

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1044
=head2 delete_all
1045

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

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

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

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

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

            
1056
=head2 select
1057
    
update document
yuki-kimoto authored on 2009-11-19
1058
Select rows
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1059

            
version 0.0901
yuki-kimoto authored on 2009-12-17
1060
    $resut = $dbi->select(
packaging one directory
yuki-kimoto authored on 2009-11-16
1061
        $table,                # must be string or array;
version 0.0901
yuki-kimoto authored on 2009-12-17
1062
        \@$columns,            # must be array reference. this can be ommited
1063
        \%$where_params,       # must be hash reference.  this can be ommited
1064
        $append_statement,     # must be string.          this can be ommited
1065
        $query_edit_callback   # must be code reference.  this can be ommited
packaging one directory
yuki-kimoto authored on 2009-11-16
1066
    );
update document
yuki-kimoto authored on 2009-11-19
1067

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

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

            
1072
    # select * from books;
1073
    $result = $dbi->select('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1074
    
update document
yuki-kimoto authored on 2009-11-19
1075
    # select * from books where title = 'Perl';
1076
    $result = $dbi->select('books', {title => 1});
1077
    
1078
    # select title, author from books where id = 1 for update;
1079
    $result = $dbi->select(
1080
        'books',              # table
1081
        ['title', 'author'],  # columns
1082
        {id => 1},            # where clause
1083
        'for update',         # append statement
1084
    );
1085

            
1086
You can join multi tables
1087
    
1088
    $result = $dbi->select(
1089
        ['table1', 'table2'],                # tables
1090
        ['table1.id as table1_id', 'title'], # columns (alias is ok)
1091
        {table1.id => 1},                    # where clase
1092
        "where table1.id = table2.id",       # join clause (must start 'where')
1093
    );
1094

            
1095
You can also edit query
1096
        
packaging one directory
yuki-kimoto authored on 2009-11-16
1097
    $dbi->select(
update document
yuki-kimoto authored on 2009-11-19
1098
        'books',
1099
        # column, where clause, append statement,
packaging one directory
yuki-kimoto authored on 2009-11-16
1100
        sub {
1101
            my $query = shift;
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
1102
            $query->query_filter(sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
1103
                # ...
1104
            });
1105
        }
update document
yuki-kimoto authored on 2009-11-19
1106
    }
1107

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

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

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

            
1115
    1. AutoCommit is true
1116
    2. RaiseError is true
1117

            
1118
By default, Both AutoCommit and RaiseError is true.
1119
You must not change these mode not to damage your data.
1120

            
1121
If you change these mode, 
1122
you cannot get correct error message, 
1123
or run_transaction may fail.
1124

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

            
1127
DBIx::Custom is customizable DBI.
1128
You can inherit DBIx::Custom and custumize attributes.
1129

            
1130
    package DBIx::Custom::Yours;
1131
    use base DBIx::Custom;
1132
    
1133
    my $class = __PACKAGE__;
1134
    
1135
    $class->user('your_name');
1136
    $class->password('your_password');
1137

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1138
=head1 AUTHOR
1139

            
1140
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
1141

            
1142
Github L<http://github.com/yuki-kimoto>
1143

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

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

            
1148
Copyright 2009 Yuki Kimoto, all rights reserved.
1149

            
1150
This program is free software; you can redistribute it and/or modify it
1151
under the same terms as Perl itself.
1152

            
1153
=cut