DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
942 lines | 22.85kb
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');
version 0.0901
yuki-kimoto authored on 2009-12-17
16

            
cleanup
yuki-kimoto authored on 2010-04-28
17
__PACKAGE__->attr([qw/user password data_source/]);
update document
yuki-kimoto authored on 2010-05-27
18
__PACKAGE__->attr([qw/default_query_filter default_fetch_filter/]);
packaging one directory
yuki-kimoto authored on 2009-11-16
19

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
refactoring select
yuki-kimoto authored on 2010-04-28
459
our %VALID_SELECT_ARGS
removed register_format()
yuki-kimoto authored on 2010-05-26
460
  = map { $_ => 1 } qw/table column where append filter/;
refactoring select
yuki-kimoto authored on 2010-04-28
461

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

            
534
=head1 NAME
535

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

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

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

            
542
=cut
543

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

            
cleanup
yuki-kimoto authored on 2010-02-11
547
=head1 STATE
548

            
549
This module is not stable. Method name and functionality will be change.
550

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

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

            
620
=head2 user
621

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

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

            
634
=head2 data_source
635

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
680
=head2 result_class
681

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
689
=head2 dbh
690

            
update document
yuki-kimoto authored on 2010-05-27
691
Database handle.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
692
    
version 0.0901
yuki-kimoto authored on 2009-12-17
693
    $dbi = $dbi->dbh($dbh);
694
    $dbh = $dbi->dbh;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
695
    
update document
yuki-kimoto authored on 2010-01-30
696
=head1 METHODS
697

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
701
=head2 auto_commit
702

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

            
update document
yuki-kimoto authored on 2010-05-27
705
    $self        = $dbi->auto_commit(1);
removed register_format()
yuki-kimoto authored on 2010-05-26
706
    $auto_commit = $dbi->auto_commit;
update document
yuki-kimoto authored on 2010-05-27
707

            
708
This is equal to
709

            
710
    $dbi->dbh->{AutoCommit} = 1;
711
    $auto_commit = $dbi->dbh->{AutoCommit};
712

            
packaging one directory
yuki-kimoto authored on 2009-11-16
713
=head2 connect
714

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

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

            
723
=head2 disconnect
724

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

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

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

            
731
=head2 reconnect
732

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

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

            
737
=head2 connected
738

            
version 0.0901
yuki-kimoto authored on 2009-12-17
739
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
740
    
version 0.0901
yuki-kimoto authored on 2009-12-17
741
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
742
    
some changed
yuki-kimoto authored on 2010-05-02
743
=head2 register_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
744

            
update document
yuki-kimoto authored on 2010-05-27
745
Resister filter.
packaging one directory
yuki-kimoto authored on 2009-11-16
746
    
update document
yuki-kimoto authored on 2010-05-27
747
    $dbi->register_filter(%filters);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
748
    
update document
yuki-kimoto authored on 2010-05-27
749
Example.
version 0.0901
yuki-kimoto authored on 2009-12-17
750

            
some changed
yuki-kimoto authored on 2010-05-02
751
    $dbi->register_filter(
packaging one directory
yuki-kimoto authored on 2009-11-16
752
        encode_utf8 => sub {
removed register_format()
yuki-kimoto authored on 2010-05-26
753
            my $value = shift;
754
            
755
            require Encode;
756
            
757
            return Encode::encode('UTF-8', $value);
packaging one directory
yuki-kimoto authored on 2009-11-16
758
        },
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
759
        decode_utf8 => sub {
removed register_format()
yuki-kimoto authored on 2010-05-26
760
            my $value = shift;
761
            
762
            require Encode;
763
            
764
            return Encode::decode('UTF-8', $value)
packaging one directory
yuki-kimoto authored on 2009-11-16
765
        }
766
    );
767

            
version 0.0901
yuki-kimoto authored on 2009-12-17
768
=head2 create_query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
769
    
update document
yuki-kimoto authored on 2010-05-27
770
Create Query instance parsing SQL template
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
771

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
774
$query is <DBIx::Query> instance. This is executed by query method as the following
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
775

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

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

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

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
788
    $result = $dbi->execute("select * from authors where {= name} and {= age}", 
removed register_format()
yuki-kimoto authored on 2010-05-26
789
                            {name => 'taro', age => 19});
packaging one directory
yuki-kimoto authored on 2009-11-16
790
    
791
    while (my @row = $result->fetch) {
792
        # do something
793
    }
794

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
797
execute() return L<DBIx::Custom::Result> instance
version 0.0901
yuki-kimoto authored on 2009-12-17
798

            
packaging one directory
yuki-kimoto authored on 2009-11-16
799
=head2 insert
800

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
803
    $affected = $dbi->insert(table  => $table, 
804
                             param  => {%param},
805
                             append => $append,
806
                             filter => {%filter});
update document
yuki-kimoto authored on 2009-11-19
807

            
version 0.0901
yuki-kimoto authored on 2009-12-17
808
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
809
    
removed register_format()
yuki-kimoto authored on 2010-05-26
810
Example
version 0.0901
yuki-kimoto authored on 2009-12-17
811

            
removed register_format()
yuki-kimoto authored on 2010-05-26
812
    # insert
813
    $dbi->insert(table  => 'books', 
814
                 param  => {title => 'Perl', author => 'Taro'},
815
                 append => "some statement",
816
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
817

            
packaging one directory
yuki-kimoto authored on 2009-11-16
818
=head2 update
819

            
update document
yuki-kimoto authored on 2009-11-19
820
Update rows
821

            
removed register_format()
yuki-kimoto authored on 2010-05-26
822
    $affected = $dbi->update(table  => $table, 
823
                             param  => {%params},
824
                             where  => {%where},
825
                             append => $append,
826
                             filter => {%filter})
version 0.0901
yuki-kimoto authored on 2009-12-17
827

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
830
Example
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
831

            
removed register_format()
yuki-kimoto authored on 2010-05-26
832
    #update
833
    $dbi->update(table  => 'books',
834
                 param  => {title => 'Perl', author => 'Taro'},
835
                 where  => {id => 5},
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_all
840

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
843
    $affected = $dbi->update_all(table  => $table, 
844
                                 param  => {%params},
845
                                 filter => {%filter},
846
                                 append => $append);
update document
yuki-kimoto authored on 2009-11-19
847

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
850
Example
update document
yuki-kimoto authored on 2009-11-19
851

            
removed register_format()
yuki-kimoto authored on 2010-05-26
852
    # update_all
853
    $dbi->update_all(table  => 'books', 
854
                     param  => {author => 'taro'},
855
                     filter => {author => 'encode_utf8'});
packaging one directory
yuki-kimoto authored on 2009-11-16
856

            
857
=head2 delete
858

            
update document
yuki-kimoto authored on 2009-11-19
859
Delete rows
860

            
removed register_format()
yuki-kimoto authored on 2010-05-26
861
    # delete
862
    $affected = $dbi->delete(table  => $table,
863
                             where  => {%where},
864
                             append => $append
865
                             filter => {%filter});
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
866

            
version 0.0901
yuki-kimoto authored on 2009-12-17
867
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
868
    
removed register_format()
yuki-kimoto authored on 2010-05-26
869
Example
packaging one directory
yuki-kimoto authored on 2009-11-16
870

            
removed register_format()
yuki-kimoto authored on 2010-05-26
871
    # delete
872
    $dbi->delete(table  => 'books',
873
                 where  => {id => 5},
874
                 append => 'some statement',
875
                 filter => {id => 'encode_utf8');
version 0.0901
yuki-kimoto authored on 2009-12-17
876

            
packaging one directory
yuki-kimoto authored on 2009-11-16
877
=head2 delete_all
878

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

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
885
Example
886
    
887
    # delete_all
version 0.0901
yuki-kimoto authored on 2009-12-17
888
    $dbi->delete_all('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
889

            
890
=head2 select
891
    
update document
yuki-kimoto authored on 2009-11-19
892
Select rows
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
893

            
removed register_format()
yuki-kimoto authored on 2010-05-26
894
    $result = $dbi->select(table  => $table,
895
                           column => [@column],
896
                           where  => {%where},
897
                           append => $append,
898
                           filter => {%filter});
update document
yuki-kimoto authored on 2009-11-19
899

            
removed register_format()
yuki-kimoto authored on 2010-05-26
900
$reslt is L<DBIx::Custom::Result> instance
update document
yuki-kimoto authored on 2009-11-19
901

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
904
    # select
update document
yuki-kimoto authored on 2009-11-19
905
    $result = $dbi->select('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
906
    
update document
yuki-kimoto authored on 2009-11-19
907
    # select * from books where title = 'Perl';
908
    $result = $dbi->select('books', {title => 1});
909
    
910
    # select title, author from books where id = 1 for update;
911
    $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
912
        table  => 'books',
913
        where  => ['title', 'author'],
914
        where  => {id => 1},
915
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
916
    );
917

            
918
You can join multi tables
919
    
920
    $result = $dbi->select(
921
        ['table1', 'table2'],                # tables
922
        ['table1.id as table1_id', 'title'], # columns (alias is ok)
923
        {table1.id => 1},                    # where clase
924
        "where table1.id = table2.id",       # join clause (must start 'where')
925
    );
926

            
packaging one directory
yuki-kimoto authored on 2009-11-16
927
=head1 AUTHOR
928

            
929
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
930

            
931
Github L<http://github.com/yuki-kimoto>
932

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

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

            
937
Copyright 2009 Yuki Kimoto, all rights reserved.
938

            
939
This program is free software; you can redistribute it and/or modify it
940
under the same terms as Perl itself.
941

            
942
=cut