DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
962 lines | 23.221kb
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;
update document
yuki-kimoto authored on 2010-05-27
13
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
14

            
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
15
__PACKAGE__->attr('dbh');
cleanup
yuki-kimoto authored on 2010-04-28
16
__PACKAGE__->attr([qw/user password data_source/]);
update document
yuki-kimoto authored on 2010-05-27
17
__PACKAGE__->attr([qw/default_query_filter default_fetch_filter/]);
packaging one directory
yuki-kimoto authored on 2009-11-16
18

            
removed register_format()
yuki-kimoto authored on 2010-05-26
19
__PACKAGE__->dual_attr('filters', default => sub { {} },
20
                                  inherit => 'hash_copy');
21
__PACKAGE__->register_filter(
update document
yuki-kimoto authored on 2010-05-27
22
    encode_utf8 => sub { encode_utf8($_[0]) },
23
    decode_utf8 => sub { decode_utf8($_[0]) }
removed register_format()
yuki-kimoto authored on 2010-05-26
24
);
packaging one directory
yuki-kimoto authored on 2009-11-16
25

            
cleanup
yuki-kimoto authored on 2010-04-28
26
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
removed register_format()
yuki-kimoto authored on 2010-05-26
27
__PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new });
28

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
39
sub auto_commit {
packaging one directory
yuki-kimoto authored on 2009-11-16
40
    my $self = shift;
41
    
42
    if (@_) {
update document
yuki-kimoto authored on 2010-01-30
43
        
44
        # Set AutoCommit
packaging one directory
yuki-kimoto authored on 2009-11-16
45
        $self->dbh->{AutoCommit} = $_[0];
update document
yuki-kimoto authored on 2010-01-30
46
        
packaging one directory
yuki-kimoto authored on 2009-11-16
47
        return $self;
48
    }
49
    return $self->dbh->{AutoCommit};
50
}
51

            
added commit method
yuki-kimoto authored on 2010-05-27
52
sub commit   { shift->dbh->commit }
53
sub rollback { shift->dbh->rollback }
54

            
packaging one directory
yuki-kimoto authored on 2009-11-16
55
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
56
    my $proto = shift;
57
    
58
    # Create
59
    my $self = ref $proto ? $proto : $proto->new(@_);
update document
yuki-kimoto authored on 2010-01-30
60
    
61
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
62
    my $data_source = $self->data_source;
63
    my $user        = $self->user;
64
    my $password    = $self->password;
65
    
update document
yuki-kimoto authored on 2010-01-30
66
    # Connect
packaging one directory
yuki-kimoto authored on 2009-11-16
67
    my $dbh = eval{DBI->connect(
68
        $data_source,
69
        $user,
70
        $password,
71
        {
72
            RaiseError => 1,
73
            PrintError => 0,
74
            AutoCommit => 1,
75
        }
76
    )};
77
    
update document
yuki-kimoto authored on 2010-01-30
78
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
79
    croak $@ if $@;
80
    
update document
yuki-kimoto authored on 2010-01-30
81
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
82
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
83
    
packaging one directory
yuki-kimoto authored on 2009-11-16
84
    return $self;
85
}
86

            
87
sub DESTROY {
88
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
89
    
90
    # Disconnect
packaging one directory
yuki-kimoto authored on 2009-11-16
91
    $self->disconnect if $self->connected;
92
}
93

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

            
96
sub disconnect {
97
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
98
    
packaging one directory
yuki-kimoto authored on 2009-11-16
99
    if ($self->connected) {
update document
yuki-kimoto authored on 2010-01-30
100
        
101
        # Disconnect
packaging one directory
yuki-kimoto authored on 2009-11-16
102
        $self->dbh->disconnect;
103
        delete $self->{dbh};
104
    }
update document
yuki-kimoto authored on 2010-01-30
105
    
106
    return $self;
packaging one directory
yuki-kimoto authored on 2009-11-16
107
}
108

            
109
sub reconnect {
110
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
111
    
112
    # Reconnect
packaging one directory
yuki-kimoto authored on 2009-11-16
113
    $self->disconnect if $self->connected;
114
    $self->connect;
update document
yuki-kimoto authored on 2010-01-30
115
    
116
    return $self;
packaging one directory
yuki-kimoto authored on 2009-11-16
117
}
118

            
119
sub create_query {
120
    my ($self, $template) = @_;
cleanup
yuki-kimoto authored on 2010-02-11
121
    
packaging one directory
yuki-kimoto authored on 2009-11-16
122
    # Create query from SQL template
removed register_format()
yuki-kimoto authored on 2010-05-26
123
    my $sql_template = $self->sql_template;
packaging one directory
yuki-kimoto authored on 2009-11-16
124
    
update document
yuki-kimoto authored on 2010-05-27
125
    # Get cached query
added commit method
yuki-kimoto authored on 2010-05-27
126
    my $cache = $self->{_cache}->{$template};
packaging one directory
yuki-kimoto authored on 2009-11-16
127
    
128
    # Create query
fix timeformat tests
yuki-kimoto authored on 2009-11-23
129
    my $query;
update document
yuki-kimoto authored on 2010-05-27
130
    if ($cache) {
cleanup
yuki-kimoto authored on 2010-02-11
131
        $query = DBIx::Custom::Query->new(
update document
yuki-kimoto authored on 2010-05-27
132
            sql       => $cache->sql,
133
            columns   => $cache->columns
cleanup
yuki-kimoto authored on 2010-02-11
134
        );
fix timeformat tests
yuki-kimoto authored on 2009-11-23
135
    }
136
    else {
removed register_format()
yuki-kimoto authored on 2010-05-26
137
        $query = eval{$sql_template->create_query($template)};
packaging one directory
yuki-kimoto authored on 2009-11-16
138
        croak($@) if $@;
139
        
update document
yuki-kimoto authored on 2010-05-27
140
        $self->{_cache}->{$template} = $query
added commit method
yuki-kimoto authored on 2010-05-27
141
          unless $self->{_cache}->{$template};
packaging one directory
yuki-kimoto authored on 2009-11-16
142
    }
143
    
144
    # Prepare statement handle
add all tests
yuki-kimoto authored on 2010-05-01
145
    my $sth = $self->dbh->prepare($query->{sql});
packaging one directory
yuki-kimoto authored on 2009-11-16
146
    
147
    # Set statement handle
148
    $query->sth($sth);
149
    
150
    return $query;
151
}
152

            
removed register_format()
yuki-kimoto authored on 2010-05-26
153
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
154

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
155
sub execute{
removed register_format()
yuki-kimoto authored on 2010-05-26
156
    my $self  = shift;
157
    my $query = shift;
158
    
159
    # Arguments
160
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
161
    
162
    # Check arguments
163
    foreach my $name (keys %$args) {
164
        croak "\"$name\" is invalid name"
165
          unless $VALID_EXECUTE_ARGS{$name};
166
    }
167
    
168
    my $params = $args->{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
169
    
170
    # First argument is SQL template
Simplify key search
yuki-kimoto authored on 2010-02-11
171
    unless (ref $query eq 'DBIx::Custom::Query') {
172
        my $template;
173
        
174
        if (ref $query eq 'ARRAY') {
many many changes
yuki-kimoto authored on 2010-04-30
175
            $template = $query->[0];
Simplify key search
yuki-kimoto authored on 2010-02-11
176
        }
177
        else { $template = $query }
178
        
many many changes
yuki-kimoto authored on 2010-04-30
179
        $query = $self->create_query($template);
packaging one directory
yuki-kimoto authored on 2009-11-16
180
    }
removed register_format()
yuki-kimoto authored on 2010-05-26
181
    
many change
yuki-kimoto authored on 2010-04-30
182
    my $filter = $args->{filter} || $query->filter || {};
removed register_format()
yuki-kimoto authored on 2010-05-26
183
    
packaging one directory
yuki-kimoto authored on 2009-11-16
184
    # Create bind value
many many changes
yuki-kimoto authored on 2010-04-30
185
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
186
    
187
    # Execute
version 0.0901
yuki-kimoto authored on 2009-12-17
188
    my $sth      = $query->sth;
189
    my $affected = eval{$sth->execute(@$bind_values)};
packaging one directory
yuki-kimoto authored on 2009-11-16
190
    
191
    # Execute error
192
    if (my $execute_error = $@) {
193
        require Data::Dumper;
194
        my $sql              = $query->{sql} || '';
195
        my $params_dump      = Data::Dumper->Dump([$params], ['*params']);
196
        
197
        croak("$execute_error" . 
198
              "<Your SQL>\n$sql\n" . 
199
              "<Your parameters>\n$params_dump");
200
    }
201
    
202
    # Return resultset if select statement is executed
203
    if ($sth->{NUM_OF_FIELDS}) {
204
        
205
        # Get result class
206
        my $result_class = $self->result_class;
207
        
208
        # Create result
209
        my $result = $result_class->new({
many many changes
yuki-kimoto authored on 2010-04-30
210
            sth             => $sth,
211
            default_filter  => $self->default_fetch_filter,
212
            filters         => $self->filters
packaging one directory
yuki-kimoto authored on 2009-11-16
213
        });
214
        return $result;
215
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
216
    return $affected;
packaging one directory
yuki-kimoto authored on 2009-11-16
217
}
218

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
267
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append filter/;
cleanup insert
yuki-kimoto authored on 2010-04-28
268

            
packaging one directory
yuki-kimoto authored on 2009-11-16
269
sub insert {
removed register_format()
yuki-kimoto authored on 2010-05-26
270
    my $self = shift;
cleanup insert
yuki-kimoto authored on 2010-04-28
271
    
272
    # Arguments
removed register_format()
yuki-kimoto authored on 2010-05-26
273
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
274

            
cleanup insert
yuki-kimoto authored on 2010-04-28
275
    # Check arguments
276
    foreach my $name (keys %$args) {
277
        croak "\"$name\" is invalid name"
278
          unless $VALID_INSERT_ARGS{$name};
279
    }
280
    
removed register_format()
yuki-kimoto authored on 2010-05-26
281
    # Arguments
282
    my $table  = $args->{table} || '';
283
    my $param  = $args->{param} || {};
284
    my $append = $args->{append} || '';
285
    my $filter = $args->{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
286
    
287
    # Insert keys
removed register_format()
yuki-kimoto authored on 2010-05-26
288
    my @insert_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
289
    
290
    # Not exists insert keys
291
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
292
      unless @insert_keys;
293
    
294
    # Templte for insert
295
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
removed register_format()
yuki-kimoto authored on 2010-05-26
296
    $template .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
297
    
298
    # Execute query
removed register_format()
yuki-kimoto authored on 2010-05-26
299
    my $ret_val = $self->execute($template, param  => $param, 
300
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
301
    
302
    return $ret_val;
303
}
304

            
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
305
our %VALID_UPDATE_ARGS
removed register_format()
yuki-kimoto authored on 2010-05-26
306
  = map { $_ => 1 } qw/table param where append filter allow_update_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
307

            
packaging one directory
yuki-kimoto authored on 2009-11-16
308
sub update {
removed register_format()
yuki-kimoto authored on 2010-05-26
309
    my $self = shift;
310

            
311
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
312
    
313
    # Check arguments
314
    foreach my $name (keys %$args) {
315
        croak "\"$name\" is invalid name"
316
          unless $VALID_UPDATE_ARGS{$name};
317
    }
318
    
319
    # Arguments
removed register_format()
yuki-kimoto authored on 2010-05-26
320
    my $table            = $args->{table} || '';
321
    my $param            = $args->{param} || {};
322
    my $where            = $args->{where} || {};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
323
    my $append_statement = $args->{append} || '';
many many changes
yuki-kimoto authored on 2010-04-30
324
    my $filter           = $args->{filter};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
325
    my $allow_update_all = $args->{allow_update_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
326
    
327
    # Update keys
removed register_format()
yuki-kimoto authored on 2010-05-26
328
    my @update_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
329
    
330
    # Not exists update kyes
331
    croak("Key-value pairs for update must be specified to 'update' second argument")
332
      unless @update_keys;
333
    
334
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
335
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
336
    
337
    # Not exists where keys
338
    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
339
      if !@where_keys && !$allow_update_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
340
    
341
    # Update clause
342
    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
343
    
344
    # Where clause
345
    my $where_clause = '';
simplify filtering system
yuki-kimoto authored on 2010-05-01
346
    my $new_where = {};
many change
yuki-kimoto authored on 2010-04-30
347
    
packaging one directory
yuki-kimoto authored on 2009-11-16
348
    if (@where_keys) {
349
        $where_clause = 'where ';
350
        foreach my $where_key (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
351
            
352
            $where_clause .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
353
        }
354
        $where_clause =~ s/ and $//;
355
    }
356
    
357
    # Template for update
358
    my $template = "update $table $update_clause $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
359
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
360
    
361
    # Rearrange parammeters
removed register_format()
yuki-kimoto authored on 2010-05-26
362
    foreach my $wkey (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
363
        
removed register_format()
yuki-kimoto authored on 2010-05-26
364
        if (exists $param->{$wkey}) {
365
            $param->{$wkey} = [$param->{$wkey}]
366
              unless ref $param->{$wkey} eq 'ARRAY';
simplify filtering system
yuki-kimoto authored on 2010-05-01
367
            
removed register_format()
yuki-kimoto authored on 2010-05-26
368
            push @{$param->{$wkey}}, $where->{$wkey};
simplify filtering system
yuki-kimoto authored on 2010-05-01
369
        }
add tests
yuki-kimoto authored on 2010-05-01
370
        else {
removed register_format()
yuki-kimoto authored on 2010-05-26
371
            $param->{$wkey} = $where->{$wkey};
add tests
yuki-kimoto authored on 2010-05-01
372
        }
simplify filtering system
yuki-kimoto authored on 2010-05-01
373
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
374
    
375
    # Execute query
removed register_format()
yuki-kimoto authored on 2010-05-26
376
    my $ret_val = $self->execute($template, param  => $param, 
377
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
378
    
379
    return $ret_val;
380
}
381

            
382
sub update_all {
removed register_format()
yuki-kimoto authored on 2010-05-26
383
    my $self = shift;;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
384
    
removed register_format()
yuki-kimoto authored on 2010-05-26
385
    # Arguments
386
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
387
        
refactoring select
yuki-kimoto authored on 2010-04-28
388
    # Allow all update
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
389
    $args->{allow_update_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
390
    
update document
yuki-kimoto authored on 2010-01-30
391
    # Update all rows
removed register_format()
yuki-kimoto authored on 2010-05-26
392
    return $self->update($args);
packaging one directory
yuki-kimoto authored on 2009-11-16
393
}
394

            
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
395
our %VALID_DELETE_ARGS
removed register_format()
yuki-kimoto authored on 2010-05-26
396
  = map { $_ => 1 } qw/table where append filter allow_delete_all/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
397

            
packaging one directory
yuki-kimoto authored on 2009-11-16
398
sub delete {
removed register_format()
yuki-kimoto authored on 2010-05-26
399
    my $self = shift;
400
    
401
    # Arguments
402
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
403
    
404
    # Check arguments
405
    foreach my $name (keys %$args) {
406
        croak "\"$name\" is invalid name"
407
          unless $VALID_DELETE_ARGS{$name};
408
    }
409
    
410
    # Arguments
removed register_format()
yuki-kimoto authored on 2010-05-26
411
    my $table            = $args->{table} || '';
412
    my $where            = $args->{where} || {};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
413
    my $append_statement = $args->{append};
removed register_format()
yuki-kimoto authored on 2010-05-26
414
    my $filter           = $args->{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
415
    my $allow_delete_all = $args->{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
416
    
417
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
418
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
419
    
420
    # Not exists where keys
421
    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
422
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
423
    
424
    # Where clause
425
    my $where_clause = '';
426
    if (@where_keys) {
427
        $where_clause = 'where ';
removed register_format()
yuki-kimoto authored on 2010-05-26
428
        foreach my $wkey (@where_keys) {
429
            $where_clause .= "{= $wkey} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
430
        }
431
        $where_clause =~ s/ and $//;
432
    }
433
    
434
    # Template for delete
435
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
436
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
437
    
438
    # Execute query
removed register_format()
yuki-kimoto authored on 2010-05-26
439
    my $ret_val = $self->execute($template, param  => $where, 
440
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
441
    
442
    return $ret_val;
443
}
444

            
445
sub delete_all {
removed register_format()
yuki-kimoto authored on 2010-05-26
446
    my $self = shift;
447
    
448
    # Arguments
449
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
450
    
refactoring select
yuki-kimoto authored on 2010-04-28
451
    # Allow all delete
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
452
    $args->{allow_delete_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
453
    
update document
yuki-kimoto authored on 2010-01-30
454
    # Delete all rows
removed register_format()
yuki-kimoto authored on 2010-05-26
455
    return $self->delete($args);
packaging one directory
yuki-kimoto authored on 2009-11-16
456
}
457

            
refactoring select
yuki-kimoto authored on 2010-04-28
458
our %VALID_SELECT_ARGS
added commit method
yuki-kimoto authored on 2010-05-27
459
  = map { $_ => 1 } qw/table column where append relation filter param/;
refactoring select
yuki-kimoto authored on 2010-04-28
460

            
packaging one directory
yuki-kimoto authored on 2009-11-16
461
sub select {
removed register_format()
yuki-kimoto authored on 2010-05-26
462
    my $self = shift;;
packaging one directory
yuki-kimoto authored on 2009-11-16
463
    
removed register_format()
yuki-kimoto authored on 2010-05-26
464
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
packaging one directory
yuki-kimoto authored on 2009-11-16
465
    
refactoring select
yuki-kimoto authored on 2010-04-28
466
    # Check arguments
467
    foreach my $name (keys %$args) {
468
        croak "\"$name\" is invalid name"
469
          unless $VALID_SELECT_ARGS{$name};
470
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
471
    
refactoring select
yuki-kimoto authored on 2010-04-28
472
    # Arguments
removed register_format()
yuki-kimoto authored on 2010-05-26
473
    my $tables = $args->{table} || [];
474
    $tables = [$tables] unless ref $tables eq 'ARRAY';
added commit method
yuki-kimoto authored on 2010-05-27
475
    my $columns  = $args->{column} || [];
476
    my $where    = $args->{where} || {};
477
    my $relation = $args->{relation};
478
    my $append   = $args->{append};
479
    my $filter   = $args->{filter};
480
    my $param    = $args->{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
481
    
482
    # SQL template for select statement
483
    my $template = 'select ';
484
    
added commit method
yuki-kimoto authored on 2010-05-27
485
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
486
    if (@$columns) {
487
        foreach my $column (@$columns) {
488
            $template .= "$column, ";
489
        }
490
        $template =~ s/, $/ /;
491
    }
492
    else {
493
        $template .= '* ';
494
    }
495
    
added commit method
yuki-kimoto authored on 2010-05-27
496
    # Table
packaging one directory
yuki-kimoto authored on 2009-11-16
497
    $template .= 'from ';
498
    foreach my $table (@$tables) {
499
        $template .= "$table, ";
500
    }
501
    $template =~ s/, $/ /;
502
    
added commit method
yuki-kimoto authored on 2010-05-27
503
    # Where clause
504
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
505
    if (@where_keys) {
506
        $template .= 'where ';
507
        foreach my $where_key (@where_keys) {
compile success
yuki-kimoto authored on 2010-05-01
508
            $template .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
509
        }
510
    }
511
    $template =~ s/ and $//;
512
    
added commit method
yuki-kimoto authored on 2010-05-27
513
    # Relation
514
    if ($relation) {
515
        $template .= @where_keys ? "and " : "where ";
516
        foreach my $rkey (keys %$relation) {
517
            $template .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
518
        }
519
    }
added commit method
yuki-kimoto authored on 2010-05-27
520
    $template =~ s/ and $//;
521
    
522
    # Append some statement
523
    $template .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
524
    
525
    # Execute query
added commit method
yuki-kimoto authored on 2010-05-27
526
    my $result = $self->execute($template, param  => $where, 
removed register_format()
yuki-kimoto authored on 2010-05-26
527
                                           filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
528
    
529
    return $result;
530
}
531

            
532
=head1 NAME
533

            
update document
yuki-kimoto authored on 2010-05-27
534
DBIx::Custom - DBI with hash parameter binding and filtering system
packaging one directory
yuki-kimoto authored on 2009-11-16
535

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

            
update document
yuki-kimoto authored on 2010-05-27
538
Version 0.1502
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
539

            
540
=cut
541

            
update document
yuki-kimoto authored on 2010-05-27
542
our $VERSION = '0.1502';
some changed
yuki-kimoto authored on 2010-05-02
543
$VERSION = eval $VERSION;
packaging one directory
yuki-kimoto authored on 2009-11-16
544

            
cleanup
yuki-kimoto authored on 2010-02-11
545
=head1 STATE
546

            
547
This module is not stable. Method name and functionality will be change.
548

            
version 0.0901
yuki-kimoto authored on 2009-12-17
549
=head1 SYNOPSYS
550
    
removed register_format()
yuki-kimoto authored on 2010-05-26
551
    # Connect
552
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
553
                                    user => 'ken', password => '!LFKD%$&');
some changed
yuki-kimoto authored on 2010-05-02
554
    
version 0.0901
yuki-kimoto authored on 2009-12-17
555
    # Insert 
removed register_format()
yuki-kimoto authored on 2010-05-26
556
    $dbi->insert(table  => 'books',
update document
yuki-kimoto authored on 2010-05-27
557
                 param  => {title => 'perl', author => 'Ken'},
removed register_format()
yuki-kimoto authored on 2010-05-26
558
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
559
    
560
    # Update 
removed register_format()
yuki-kimoto authored on 2010-05-26
561
    $dbi->update(table  => 'books', 
562
                 param  => {title => 'aaa', author => 'Ken'}, 
update document
yuki-kimoto authored on 2010-05-27
563
                 where  => {id => 5},
564
                 filter => {title => 'encode_utf8'});
removed register_format()
yuki-kimoto authored on 2010-05-26
565
    
566
    # Update all
567
    $dbi->update_all(table  => 'books',
update document
yuki-kimoto authored on 2010-05-27
568
                     param  => {title => 'aaa'},
removed register_format()
yuki-kimoto authored on 2010-05-26
569
                     filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
570
    
571
    # Delete
removed register_format()
yuki-kimoto authored on 2010-05-26
572
    $dbi->delete(table  => 'books',
update document
yuki-kimoto authored on 2010-05-27
573
                 where  => {author => 'Ken'},
removed register_format()
yuki-kimoto authored on 2010-05-26
574
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
575
    
removed register_format()
yuki-kimoto authored on 2010-05-26
576
    # Delete all
577
    $dbi->delete_all(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
578
    
removed register_format()
yuki-kimoto authored on 2010-05-26
579
    # Select
580
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
581
    
removed register_format()
yuki-kimoto authored on 2010-05-26
582
    # Select(more complex)
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
583
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
584
        table  => 'books',
585
        column => [qw/author title/],
586
        where  => {author => 'Ken'},
587
        append => 'order by id limit 1',
588
        filter => {tilte => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
589
    );
added commit method
yuki-kimoto authored on 2010-05-27
590
    
591
    # Select(Join table)
592
    my $result = $dbi->select(
593
        table => ['books', 'rental'],
594
        column => ['books.name as book_name']
595
        relation => {'books.id' => 'rental.book_id'}
596
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
597

            
removed register_format()
yuki-kimoto authored on 2010-05-26
598
    # Execute SQL
599
    $dbi->execute("select title from books");
600
    
601
    # Execute SQL with parameters and filter
602
    $dbi->execute("select id from books where {= author} && {like title}",
603
                  param  => {author => 'ken', title => '%Perl%'},
604
                  filter => {tilte => 'encode_utf8'});
605
    
606
    # Default filter
607
    $dbi->default_query_filter('encode_utf8');
608
    $dbi->default_fetch_filter('decode_utf8');
609
    
610
    # Fetch
611
    while (my $row = $result->fetch) {
612
        # ...
613
    }
614
    
615
    # Fetch hash
616
    while (my $row = $result->fetch_hash) {
617
        
618
    }
619
    
update document
yuki-kimoto authored on 2010-05-27
620
    # DBI instance
621
    my $dbh = $dbi->dbh;
removed register_format()
yuki-kimoto authored on 2010-05-26
622
    
update document
yuki-kimoto authored on 2010-01-30
623
=head1 ATTRIBUTES
packaging one directory
yuki-kimoto authored on 2009-11-16
624

            
625
=head2 user
626

            
update document
yuki-kimoto authored on 2010-05-27
627
Database user name.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
628
    
version 0.0901
yuki-kimoto authored on 2009-12-17
629
    $dbi  = $dbi->user('Ken');
630
    $user = $dbi->user;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
631
    
packaging one directory
yuki-kimoto authored on 2009-11-16
632
=head2 password
633

            
update document
yuki-kimoto authored on 2010-05-27
634
Database password.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
635
    
version 0.0901
yuki-kimoto authored on 2009-12-17
636
    $dbi      = $dbi->password('lkj&le`@s');
637
    $password = $dbi->password;
packaging one directory
yuki-kimoto authored on 2009-11-16
638

            
639
=head2 data_source
640

            
update document
yuki-kimoto authored on 2010-05-27
641
Database data source.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
642
    
version 0.0901
yuki-kimoto authored on 2009-12-17
643
    $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
644
    $data_source = $dbi->data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
645
    
version 0.0901
yuki-kimoto authored on 2009-12-17
646
If you know data source more, See also L<DBI>.
647

            
removed register_format()
yuki-kimoto authored on 2010-05-26
648
=head2 sql_template
packaging one directory
yuki-kimoto authored on 2009-11-16
649

            
update document
yuki-kimoto authored on 2010-05-27
650
SQLTemplate instance. sql_template attribute must be 
651
the instance of L<DBIx::Cutom::SQLTemplate> subclass.
packaging one directory
yuki-kimoto authored on 2009-11-16
652

            
removed register_format()
yuki-kimoto authored on 2010-05-26
653
    $dbi          = $dbi->sql_template(DBIx::Cutom::SQLTemplate->new);
654
    $sql_template = $dbi->sql_template;
packaging one directory
yuki-kimoto authored on 2009-11-16
655

            
update document
yuki-kimoto authored on 2010-05-27
656
the instance of DBIx::Cutom::SQLTemplate is set to 
657
this attribute by default.
packaging one directory
yuki-kimoto authored on 2009-11-16
658

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

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

            
update document
yuki-kimoto authored on 2010-05-27
663
    $dbi     = $dbi->filters({%filters});
version 0.0901
yuki-kimoto authored on 2009-12-17
664
    $filters = $dbi->filters;
665

            
update document
yuki-kimoto authored on 2010-05-27
666
encode_utf8 and decode_utf8 is set to this attribute by default.
version 0.0901
yuki-kimoto authored on 2009-12-17
667

            
update document
yuki-kimoto authored on 2010-05-27
668
    $encode_utf8 = $dbi->filters->{encode_utf8};
669
    $decode_utf8 = $dbi->filters->{decode_utf8};
packaging one directory
yuki-kimoto authored on 2009-11-16
670

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

            
update document
yuki-kimoto authored on 2010-05-27
673
Default query filter.
packaging one directory
yuki-kimoto authored on 2009-11-16
674

            
update document
yuki-kimoto authored on 2010-05-27
675
    $dbi                  = $dbi->default_query_filter('encode_utf8');
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
676
    $default_query_filter = $dbi->default_query_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
677

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

            
update document
yuki-kimoto authored on 2010-05-27
680
Fetching filter.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
681

            
update document
yuki-kimoto authored on 2010-05-27
682
    $dbi                  = $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-04-28
683
    $default_fetch_filter = $dbi->default_fetch_filter;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
684

            
packaging one directory
yuki-kimoto authored on 2009-11-16
685
=head2 result_class
686

            
update document
yuki-kimoto authored on 2010-05-27
687
Result class.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
688

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

            
update document
yuki-kimoto authored on 2010-05-27
692
L<DBIx::Custom::Result> is set to this attribute by default.
update document
yuki-kimoto authored on 2010-01-30
693

            
packaging one directory
yuki-kimoto authored on 2009-11-16
694
=head2 dbh
695

            
update document
yuki-kimoto authored on 2010-05-27
696
Database handle.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
697
    
version 0.0901
yuki-kimoto authored on 2009-12-17
698
    $dbi = $dbi->dbh($dbh);
699
    $dbh = $dbi->dbh;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
700
    
update document
yuki-kimoto authored on 2010-01-30
701
=head1 METHODS
702

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
706
=head2 auto_commit
707

            
update document
yuki-kimoto authored on 2010-05-27
708
Auto commit.
removed register_format()
yuki-kimoto authored on 2010-05-26
709

            
update document
yuki-kimoto authored on 2010-05-27
710
    $self        = $dbi->auto_commit(1);
removed register_format()
yuki-kimoto authored on 2010-05-26
711
    $auto_commit = $dbi->auto_commit;
update document
yuki-kimoto authored on 2010-05-27
712

            
713
This is equal to
714

            
715
    $dbi->dbh->{AutoCommit} = 1;
716
    $auto_commit = $dbi->dbh->{AutoCommit};
717

            
added commit method
yuki-kimoto authored on 2010-05-27
718
=head2 commit
719

            
720
Commit.
721

            
722
    $dbi->commit;
723

            
724
This is equal to
725

            
726
    $dbi->dbh->commit;
727

            
728
=head2 rollback
729

            
730
Rollback.
731

            
732
    $dbi->rollback
733

            
734
This is equal to
735

            
736
    $dbi->dbh->rollback;
737

            
packaging one directory
yuki-kimoto authored on 2009-11-16
738
=head2 connect
739

            
update document
yuki-kimoto authored on 2010-05-27
740
Connect to database.
741
    
742
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
743
                                    user => 'ken', password => '!LFKD%$&');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
744

            
update document
yuki-kimoto authored on 2010-05-27
745
"AutoCommit" and "RaiseError" option is true, 
746
and "PrintError" option is false by dfault.
packaging one directory
yuki-kimoto authored on 2009-11-16
747

            
748
=head2 disconnect
749

            
update document
yuki-kimoto authored on 2010-05-27
750
Disconnect database.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
751

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

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

            
756
=head2 reconnect
757

            
update document
yuki-kimoto authored on 2010-05-27
758
Reconnect to database.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
759

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

            
762
=head2 connected
763

            
version 0.0901
yuki-kimoto authored on 2009-12-17
764
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
765
    
version 0.0901
yuki-kimoto authored on 2009-12-17
766
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
767
    
some changed
yuki-kimoto authored on 2010-05-02
768
=head2 register_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
769

            
update document
yuki-kimoto authored on 2010-05-27
770
Resister filter.
packaging one directory
yuki-kimoto authored on 2009-11-16
771
    
update document
yuki-kimoto authored on 2010-05-27
772
    $dbi->register_filter(%filters);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
773
    
update document
yuki-kimoto authored on 2010-05-27
774
Example.
version 0.0901
yuki-kimoto authored on 2009-12-17
775

            
some changed
yuki-kimoto authored on 2010-05-02
776
    $dbi->register_filter(
packaging one directory
yuki-kimoto authored on 2009-11-16
777
        encode_utf8 => sub {
removed register_format()
yuki-kimoto authored on 2010-05-26
778
            my $value = shift;
779
            
780
            require Encode;
781
            
782
            return Encode::encode('UTF-8', $value);
packaging one directory
yuki-kimoto authored on 2009-11-16
783
        },
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
784
        decode_utf8 => sub {
removed register_format()
yuki-kimoto authored on 2010-05-26
785
            my $value = shift;
786
            
787
            require Encode;
788
            
789
            return Encode::decode('UTF-8', $value)
packaging one directory
yuki-kimoto authored on 2009-11-16
790
        }
791
    );
792

            
version 0.0901
yuki-kimoto authored on 2009-12-17
793
=head2 create_query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
794
    
added commit method
yuki-kimoto authored on 2010-05-27
795
Create the instance of L<DBIx::Custom::Query>. 
796
This receive the string written by SQL template.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
797

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

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

            
added commit method
yuki-kimoto authored on 2010-05-27
802
Execute the query or the string written by SQL template.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
803

            
added commit method
yuki-kimoto authored on 2010-05-27
804
    $result = $dbi->execute($query,    param => $params, filter => {%filter});
805
    $result = $dbi->execute($template, param => $params, filter => {%filter});
packaging one directory
yuki-kimoto authored on 2009-11-16
806

            
added commit method
yuki-kimoto authored on 2010-05-27
807
Example.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
808

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
809
    $result = $dbi->execute("select * from authors where {= name} and {= age}", 
removed register_format()
yuki-kimoto authored on 2010-05-26
810
                            {name => 'taro', age => 19});
packaging one directory
yuki-kimoto authored on 2009-11-16
811
    
812
    while (my @row = $result->fetch) {
813
        # do something
814
    }
815

            
added commit method
yuki-kimoto authored on 2010-05-27
816
See also L<DBIx::Custom::SQLTemplate>.
version 0.0901
yuki-kimoto authored on 2009-12-17
817

            
added commit method
yuki-kimoto authored on 2010-05-27
818
Returned value L<DBIx::Custom::Result> instance.
version 0.0901
yuki-kimoto authored on 2009-12-17
819

            
packaging one directory
yuki-kimoto authored on 2009-11-16
820
=head2 insert
821

            
added commit method
yuki-kimoto authored on 2010-05-27
822
Insert row.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
823

            
removed register_format()
yuki-kimoto authored on 2010-05-26
824
    $affected = $dbi->insert(table  => $table, 
825
                             param  => {%param},
826
                             append => $append,
827
                             filter => {%filter});
update document
yuki-kimoto authored on 2009-11-19
828

            
added commit method
yuki-kimoto authored on 2010-05-27
829
Retruned value is affected rows count.
packaging one directory
yuki-kimoto authored on 2009-11-16
830
    
added commit method
yuki-kimoto authored on 2010-05-27
831
Example.
version 0.0901
yuki-kimoto authored on 2009-12-17
832

            
removed register_format()
yuki-kimoto authored on 2010-05-26
833
    # insert
834
    $dbi->insert(table  => 'books', 
835
                 param  => {title => 'Perl', author => 'Taro'},
836
                 append => "some statement",
837
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
838

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

            
added commit method
yuki-kimoto authored on 2010-05-27
841
Update rows.
update document
yuki-kimoto authored on 2009-11-19
842

            
removed register_format()
yuki-kimoto authored on 2010-05-26
843
    $affected = $dbi->update(table  => $table, 
844
                             param  => {%params},
845
                             where  => {%where},
846
                             append => $append,
847
                             filter => {%filter})
version 0.0901
yuki-kimoto authored on 2009-12-17
848

            
added commit method
yuki-kimoto authored on 2010-05-27
849
Retruned value is affected rows count
update document
yuki-kimoto authored on 2009-11-19
850

            
added commit method
yuki-kimoto authored on 2010-05-27
851
Example.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
852

            
removed register_format()
yuki-kimoto authored on 2010-05-26
853
    #update
854
    $dbi->update(table  => 'books',
855
                 param  => {title => 'Perl', author => 'Taro'},
856
                 where  => {id => 5},
857
                 append => "some statement",
added commit method
yuki-kimoto authored on 2010-05-27
858
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
859

            
packaging one directory
yuki-kimoto authored on 2009-11-16
860
=head2 update_all
861

            
added commit method
yuki-kimoto authored on 2010-05-27
862
Update all rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
863

            
removed register_format()
yuki-kimoto authored on 2010-05-26
864
    $affected = $dbi->update_all(table  => $table, 
865
                                 param  => {%params},
866
                                 filter => {%filter},
867
                                 append => $append);
update document
yuki-kimoto authored on 2009-11-19
868

            
added commit method
yuki-kimoto authored on 2010-05-27
869
Retruned value is affected rows count.
version 0.0901
yuki-kimoto authored on 2009-12-17
870

            
added commit method
yuki-kimoto authored on 2010-05-27
871
Example.
update document
yuki-kimoto authored on 2009-11-19
872

            
removed register_format()
yuki-kimoto authored on 2010-05-26
873
    # update_all
874
    $dbi->update_all(table  => 'books', 
875
                     param  => {author => 'taro'},
876
                     filter => {author => 'encode_utf8'});
packaging one directory
yuki-kimoto authored on 2009-11-16
877

            
878
=head2 delete
879

            
added commit method
yuki-kimoto authored on 2010-05-27
880
Delete rows.
update document
yuki-kimoto authored on 2009-11-19
881

            
removed register_format()
yuki-kimoto authored on 2010-05-26
882
    $affected = $dbi->delete(table  => $table,
883
                             where  => {%where},
added commit method
yuki-kimoto authored on 2010-05-27
884
                             append => $append,
removed register_format()
yuki-kimoto authored on 2010-05-26
885
                             filter => {%filter});
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
886

            
version 0.0901
yuki-kimoto authored on 2009-12-17
887
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
888
    
added commit method
yuki-kimoto authored on 2010-05-27
889
Example.
packaging one directory
yuki-kimoto authored on 2009-11-16
890

            
removed register_format()
yuki-kimoto authored on 2010-05-26
891
    # delete
892
    $dbi->delete(table  => 'books',
893
                 where  => {id => 5},
894
                 append => 'some statement',
895
                 filter => {id => 'encode_utf8');
version 0.0901
yuki-kimoto authored on 2009-12-17
896

            
packaging one directory
yuki-kimoto authored on 2009-11-16
897
=head2 delete_all
898

            
added commit method
yuki-kimoto authored on 2010-05-27
899
Delete all rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
900

            
removed register_format()
yuki-kimoto authored on 2010-05-26
901
    $affected = $dbi->delete_all(table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
902

            
added commit method
yuki-kimoto authored on 2010-05-27
903
Retruned value is affected rows count.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
904

            
removed register_format()
yuki-kimoto authored on 2010-05-26
905
Example
906
    
907
    # delete_all
version 0.0901
yuki-kimoto authored on 2009-12-17
908
    $dbi->delete_all('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
909

            
910
=head2 select
911
    
added commit method
yuki-kimoto authored on 2010-05-27
912
Select rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
913

            
added commit method
yuki-kimoto authored on 2010-05-27
914
    $result = $dbi->select(table    => $table,
915
                           column   => [@column],
916
                           where    => {%where},
917
                           append   => $append,
918
                           relation => {%relation}
919
                           filter   => {%filter});
update document
yuki-kimoto authored on 2009-11-19
920

            
added commit method
yuki-kimoto authored on 2010-05-27
921
Returned value is L<DBIx::Custom::Result> instance.
update document
yuki-kimoto authored on 2009-11-19
922

            
added commit method
yuki-kimoto authored on 2010-05-27
923
Example.
update document
yuki-kimoto authored on 2009-11-19
924

            
added commit method
yuki-kimoto authored on 2010-05-27
925
    # select * from books;
update document
yuki-kimoto authored on 2009-11-19
926
    $result = $dbi->select('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
927
    
update document
yuki-kimoto authored on 2009-11-19
928
    # select * from books where title = 'Perl';
929
    $result = $dbi->select('books', {title => 1});
930
    
931
    # select title, author from books where id = 1 for update;
932
    $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
933
        table  => 'books',
added commit method
yuki-kimoto authored on 2010-05-27
934
        column  => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
935
        where  => {id => 1},
936
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
937
    );
938
    
added commit method
yuki-kimoto authored on 2010-05-27
939
    # select books.name as book_name from books, rental 
940
    # where books.id = rental.book_id;
941
    my $result = $dbi->select(
942
        table => ['books', 'rental'],
943
        column => ['books.name as book_name']
944
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
945
    );
946

            
packaging one directory
yuki-kimoto authored on 2009-11-16
947
=head1 AUTHOR
948

            
949
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
950

            
951
Github L<http://github.com/yuki-kimoto>
952

            
added commit method
yuki-kimoto authored on 2010-05-27
953
I develope this module on L<http://github.com/yuki-kimoto/DBIx-Custom>
version 0.0901
yuki-kimoto authored on 2009-12-17
954

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

            
957
Copyright 2009 Yuki Kimoto, all rights reserved.
958

            
959
This program is free software; you can redistribute it and/or modify it
960
under the same terms as Perl itself.
961

            
962
=cut