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

            
3
use strict;
4
use warnings;
5

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

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

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

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

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
26
__PACKAGE__->dual_attr('filters', default => sub { {} },
27
                                  inherit => 'hash_copy');
28
__PACKAGE__->register_filter(
29
    encode_utf8 => sub { encode('UTF-8', $_[0]) },
30
    decode_utf8 => sub { decode('UTF-8', $_[0]) }
31
);
packaging one directory
yuki-kimoto authored on 2009-11-16
32

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

            
36

            
packaging one directory
yuki-kimoto authored on 2009-11-16
37

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

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

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

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

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

            
107
sub disconnect {
108
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
109
    
packaging one directory
yuki-kimoto authored on 2009-11-16
110
    if ($self->connected) {
update document
yuki-kimoto authored on 2010-01-30
111
        
112
        # Disconnect
packaging one directory
yuki-kimoto authored on 2009-11-16
113
        $self->dbh->disconnect;
114
        delete $self->{dbh};
115
    }
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 reconnect {
121
    my $self = shift;
update document
yuki-kimoto authored on 2010-01-30
122
    
123
    # Reconnect
packaging one directory
yuki-kimoto authored on 2009-11-16
124
    $self->disconnect if $self->connected;
125
    $self->connect;
update document
yuki-kimoto authored on 2010-01-30
126
    
127
    return $self;
packaging one directory
yuki-kimoto authored on 2009-11-16
128
}
129

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

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

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
288
sub insert {
removed register_format()
yuki-kimoto authored on 2010-05-26
289
    my $self = shift;
cleanup insert
yuki-kimoto authored on 2010-04-28
290
    
291
    # Arguments
removed register_format()
yuki-kimoto authored on 2010-05-26
292
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
293

            
cleanup insert
yuki-kimoto authored on 2010-04-28
294
    # Check arguments
295
    foreach my $name (keys %$args) {
296
        croak "\"$name\" is invalid name"
297
          unless $VALID_INSERT_ARGS{$name};
298
    }
299
    
removed register_format()
yuki-kimoto authored on 2010-05-26
300
    # Arguments
301
    my $table  = $args->{table} || '';
302
    my $param  = $args->{param} || {};
303
    my $append = $args->{append} || '';
304
    my $filter = $args->{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
305
    
306
    # Insert keys
removed register_format()
yuki-kimoto authored on 2010-05-26
307
    my @insert_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
308
    
309
    # Not exists insert keys
310
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
311
      unless @insert_keys;
312
    
313
    # Templte for insert
314
    my $template = "insert into $table {insert " . join(' ', @insert_keys) . '}';
removed register_format()
yuki-kimoto authored on 2010-05-26
315
    $template .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
316
    
317
    # Execute query
removed register_format()
yuki-kimoto authored on 2010-05-26
318
    my $ret_val = $self->execute($template, param  => $param, 
319
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
320
    
321
    return $ret_val;
322
}
323

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

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

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

            
401
sub update_all {
removed register_format()
yuki-kimoto authored on 2010-05-26
402
    my $self = shift;;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
403
    
removed register_format()
yuki-kimoto authored on 2010-05-26
404
    # Arguments
405
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
406
        
refactoring select
yuki-kimoto authored on 2010-04-28
407
    # Allow all update
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
408
    $args->{allow_update_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
409
    
update document
yuki-kimoto authored on 2010-01-30
410
    # Update all rows
removed register_format()
yuki-kimoto authored on 2010-05-26
411
    return $self->update($args);
packaging one directory
yuki-kimoto authored on 2009-11-16
412
}
413

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
417
sub delete {
removed register_format()
yuki-kimoto authored on 2010-05-26
418
    my $self = shift;
419
    
420
    # Arguments
421
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
422
    
423
    # Check arguments
424
    foreach my $name (keys %$args) {
425
        croak "\"$name\" is invalid name"
426
          unless $VALID_DELETE_ARGS{$name};
427
    }
428
    
429
    # Arguments
removed register_format()
yuki-kimoto authored on 2010-05-26
430
    my $table            = $args->{table} || '';
431
    my $where            = $args->{where} || {};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
432
    my $append_statement = $args->{append};
removed register_format()
yuki-kimoto authored on 2010-05-26
433
    my $filter           = $args->{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
434
    my $allow_delete_all = $args->{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
435
    
436
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
437
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
438
    
439
    # Not exists where keys
440
    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
441
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
442
    
443
    # Where clause
444
    my $where_clause = '';
445
    if (@where_keys) {
446
        $where_clause = 'where ';
removed register_format()
yuki-kimoto authored on 2010-05-26
447
        foreach my $wkey (@where_keys) {
448
            $where_clause .= "{= $wkey} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
449
        }
450
        $where_clause =~ s/ and $//;
451
    }
452
    
453
    # Template for delete
454
    my $template = "delete from $table $where_clause";
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
455
    $template .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
456
    
457
    # Execute query
removed register_format()
yuki-kimoto authored on 2010-05-26
458
    my $ret_val = $self->execute($template, param  => $where, 
459
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
460
    
461
    return $ret_val;
462
}
463

            
464
sub delete_all {
removed register_format()
yuki-kimoto authored on 2010-05-26
465
    my $self = shift;
466
    
467
    # Arguments
468
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
469
    
refactoring select
yuki-kimoto authored on 2010-04-28
470
    # Allow all delete
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
471
    $args->{allow_delete_all} = 1;
insert, update, delete appnd...
yuki-kimoto authored on 2009-11-16
472
    
update document
yuki-kimoto authored on 2010-01-30
473
    # Delete all rows
removed register_format()
yuki-kimoto authored on 2010-05-26
474
    return $self->delete($args);
packaging one directory
yuki-kimoto authored on 2009-11-16
475
}
476

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

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

            
552
sub _add_query_cache {
553
    my ($class, $template, $query) = @_;
update document
yuki-kimoto authored on 2010-01-30
554
    
555
    # Query information
packaging one directory
yuki-kimoto authored on 2009-11-16
556
    my $query_cache_keys = $class->_query_cache_keys;
557
    my $query_caches     = $class->_query_caches;
558
    
update document
yuki-kimoto authored on 2010-01-30
559
    # Already cached
packaging one directory
yuki-kimoto authored on 2009-11-16
560
    return $class if $query_caches->{$template};
561
    
update document
yuki-kimoto authored on 2010-01-30
562
    # Cache
packaging one directory
yuki-kimoto authored on 2009-11-16
563
    $query_caches->{$template} = $query;
564
    push @$query_cache_keys, $template;
565
    
update document
yuki-kimoto authored on 2010-01-30
566
    # Check cache overflow
packaging one directory
yuki-kimoto authored on 2009-11-16
567
    my $overflow = @$query_cache_keys - $class->query_cache_max;
568
    for (my $i = 0; $i < $overflow; $i++) {
569
        my $template = shift @$query_cache_keys;
570
        delete $query_caches->{$template};
571
    }
572
    
573
    return $class;
574
}
575

            
576
=head1 NAME
577

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
582
Version 0.1501
catch up with Object::Simple...
yuki-kimoto authored on 2010-01-18
583

            
584
=cut
585

            
removed register_format()
yuki-kimoto authored on 2010-05-26
586
our $VERSION = '0.1501';
some changed
yuki-kimoto authored on 2010-05-02
587
$VERSION = eval $VERSION;
packaging one directory
yuki-kimoto authored on 2009-11-16
588

            
cleanup
yuki-kimoto authored on 2010-02-11
589
=head1 STATE
590

            
591
This module is not stable. Method name and functionality will be change.
592

            
version 0.0901
yuki-kimoto authored on 2009-12-17
593
=head1 SYNOPSYS
594
    
removed register_format()
yuki-kimoto authored on 2010-05-26
595
    # Connect
596
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
597
                                    user => 'ken', password => '!LFKD%$&');
some changed
yuki-kimoto authored on 2010-05-02
598
    
version 0.0901
yuki-kimoto authored on 2009-12-17
599
    # Insert 
removed register_format()
yuki-kimoto authored on 2010-05-26
600
    $dbi->insert(table  => 'books',
601
                 param  => {title => 'perl', author => 'Ken'}
602
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
603
    
604
    # Update 
removed register_format()
yuki-kimoto authored on 2010-05-26
605
    $dbi->update(table  => 'books', 
606
                 param  => {title => 'aaa', author => 'Ken'}, 
607
                 where  => {id => 5}
608
                 filter => {title => 'encode_utf8');
609
    
610
    # Update all
611
    $dbi->update_all(table  => 'books',
612
                     param  => {title => 'aaa'}
613
                     filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
614
    
615
    # Delete
removed register_format()
yuki-kimoto authored on 2010-05-26
616
    $dbi->delete(table  => 'books',
617
                 where  => {author => 'Ken'}
618
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
619
    
removed register_format()
yuki-kimoto authored on 2010-05-26
620
    # Delete all
621
    $dbi->delete_all(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
622
    
removed register_format()
yuki-kimoto authored on 2010-05-26
623
    # Select
624
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
625
    
removed register_format()
yuki-kimoto authored on 2010-05-26
626
    # Select(more complex)
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
627
    my $result = $dbi->select(
628
        'books',
629
        {
630
            columns => [qw/author title/],
631
            where   => {author => 'Ken'},
removed register_format()
yuki-kimoto authored on 2010-05-26
632
            append  => 'order by id limit 1',
633
            filter  => {tilte => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
634
        }
635
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
636

            
removed register_format()
yuki-kimoto authored on 2010-05-26
637
    # Execute SQL
638
    $dbi->execute("select title from books");
639
    
640
    # Execute SQL with parameters and filter
641
    $dbi->execute("select id from books where {= author} && {like title}",
642
                  param  => {author => 'ken', title => '%Perl%'},
643
                  filter => {tilte => 'encode_utf8'});
644
    
645
    # Default filter
646
    $dbi->default_query_filter('encode_utf8');
647
    $dbi->default_fetch_filter('decode_utf8');
648
    
649
    # Fetch
650
    while (my $row = $result->fetch) {
651
        # ...
652
    }
653
    
654
    # Fetch hash
655
    while (my $row = $result->fetch_hash) {
656
        
657
    }
658
    
659
    
update document
yuki-kimoto authored on 2010-01-30
660
=head1 ATTRIBUTES
packaging one directory
yuki-kimoto authored on 2009-11-16
661

            
662
=head2 user
663

            
update document
yuki-kimoto authored on 2010-01-30
664
Database user name
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
665
    
version 0.0901
yuki-kimoto authored on 2009-12-17
666
    $dbi  = $dbi->user('Ken');
667
    $user = $dbi->user;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
668
    
packaging one directory
yuki-kimoto authored on 2009-11-16
669
=head2 password
670

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

            
676
=head2 data_source
677

            
update document
yuki-kimoto authored on 2010-01-30
678
Database data source
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
679
    
version 0.0901
yuki-kimoto authored on 2009-12-17
680
    $dbi         = $dbi->data_source("dbi:mysql:dbname=$database");
681
    $data_source = $dbi->data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
682
    
version 0.0901
yuki-kimoto authored on 2009-12-17
683
If you know data source more, See also L<DBI>.
684

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

            
update document
yuki-kimoto authored on 2010-01-30
687
Database name
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->database('books');
690
    $database = $dbi->database;
packaging one directory
yuki-kimoto authored on 2009-11-16
691

            
add port and host method
yuki-kimoto authored on 2009-11-16
692
=head2 host
693

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

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

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

            
701
=head2 port
702

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

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

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

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

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

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

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

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

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

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

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
728
    $dbi     = $dbi->filters({filter1 => sub { }, filter2 => sub {}});
729
    $filters = $dbi->filters;
packaging one directory
yuki-kimoto authored on 2009-11-16
730
    
version 0.0901
yuki-kimoto authored on 2009-12-17
731
This method is generally used to get a filter.
732

            
733
    $filter = $dbi->filters->{encode_utf8};
734

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
739
Default query filter
packaging one directory
yuki-kimoto authored on 2009-11-16
740

            
removed register_format()
yuki-kimoto authored on 2010-05-26
741
    $dbi                  = $dbi->default_query_filter($default_query_filter);
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
742
    $default_query_filter = $dbi->default_query_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
743

            
removed register_format()
yuki-kimoto authored on 2010-05-26
744
Query filter example
cleanup
yuki-kimoto authored on 2010-04-28
745
    
some changed
yuki-kimoto authored on 2010-05-02
746
    $dbi->register_filter(encode_utf8 => sub {
cleanup
yuki-kimoto authored on 2010-04-28
747
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
748
        
cleanup
yuki-kimoto authored on 2010-04-28
749
        require Encode 'encode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
750
        
cleanup
yuki-kimoto authored on 2010-04-28
751
        return encode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
752
    });
cleanup
yuki-kimoto authored on 2010-04-28
753
    
rename bind_filter to query_...
yuki-kimoto authored on 2010-04-28
754
    $dbi->default_query_filter('encode_utf8')
packaging one directory
yuki-kimoto authored on 2009-11-16
755

            
version 0.0901
yuki-kimoto authored on 2009-12-17
756
Bind filter arguemts is
757

            
758
    1. $value : Value
removed register_format()
yuki-kimoto authored on 2010-05-26
759
    3. $dbi   : DBIx::Custom instance
version 0.0901
yuki-kimoto authored on 2009-12-17
760

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

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
768
Fetch filter example
packaging one directory
yuki-kimoto authored on 2009-11-16
769

            
some changed
yuki-kimoto authored on 2010-05-02
770
    $dbi->register_filter(decode_utf8 => sub {
cleanup
yuki-kimoto authored on 2010-04-28
771
        my $value = shift;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
772
        
cleanup
yuki-kimoto authored on 2010-04-28
773
        require Encode 'decode_utf8';
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
774
        
cleanup
yuki-kimoto authored on 2010-04-28
775
        return decode_utf8($value);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
776
    });
packaging one directory
yuki-kimoto authored on 2009-11-16
777

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
780
Fetching filter arguemts is
version 0.0901
yuki-kimoto authored on 2009-12-17
781

            
removed register_format()
yuki-kimoto authored on 2010-05-26
782
    1. Value
783
    2. DBIx::Custom instance
version 0.0901
yuki-kimoto authored on 2009-12-17
784

            
packaging one directory
yuki-kimoto authored on 2009-11-16
785
=head2 result_class
786

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
794
=head2 dbh
795

            
update document
yuki-kimoto authored on 2010-01-30
796
Database handle
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
797
    
version 0.0901
yuki-kimoto authored on 2009-12-17
798
    $dbi = $dbi->dbh($dbh);
799
    $dbh = $dbi->dbh;
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
800
    
801
=head2 query_cache_max
802

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

            
version 0.0901
yuki-kimoto authored on 2009-12-17
805
    $class           = DBIx::Custom->query_cache_max(50);
806
    $query_cache_max = DBIx::Custom->query_cache_max;
807

            
808
Default value is 50
809

            
update document
yuki-kimoto authored on 2010-01-30
810
=head1 METHODS
811

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
815
=head2 auto_commit
816

            
817
Set and Get auto commit
818

            
819
    $self        = $dbi->auto_commit($auto_commit);
820
    $auto_commit = $dbi->auto_commit;
821
    
packaging one directory
yuki-kimoto authored on 2009-11-16
822
=head2 connect
823

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

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

            
828
=head2 disconnect
829

            
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
830
Disconnect database
831

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

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

            
836
=head2 reconnect
837

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

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

            
842
=head2 connected
843

            
version 0.0901
yuki-kimoto authored on 2009-12-17
844
Check if database is connected.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
845
    
version 0.0901
yuki-kimoto authored on 2009-12-17
846
    $is_connected = $dbi->connected;
packaging one directory
yuki-kimoto authored on 2009-11-16
847
    
some changed
yuki-kimoto authored on 2010-05-02
848
=head2 register_filter
packaging one directory
yuki-kimoto authored on 2009-11-16
849

            
removed register_format()
yuki-kimoto authored on 2010-05-26
850
Resister filter
packaging one directory
yuki-kimoto authored on 2009-11-16
851
    
some changed
yuki-kimoto authored on 2010-05-02
852
    $dbi->register_filter($fname1 => $filter1, $fname => $filter2);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
853
    
removed register_format()
yuki-kimoto authored on 2010-05-26
854
register_filter example
version 0.0901
yuki-kimoto authored on 2009-12-17
855

            
some changed
yuki-kimoto authored on 2010-05-02
856
    $dbi->register_filter(
packaging one directory
yuki-kimoto authored on 2009-11-16
857
        encode_utf8 => sub {
removed register_format()
yuki-kimoto authored on 2010-05-26
858
            my $value = shift;
859
            
860
            require Encode;
861
            
862
            return Encode::encode('UTF-8', $value);
packaging one directory
yuki-kimoto authored on 2009-11-16
863
        },
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
864
        decode_utf8 => sub {
removed register_format()
yuki-kimoto authored on 2010-05-26
865
            my $value = shift;
866
            
867
            require Encode;
868
            
869
            return Encode::decode('UTF-8', $value)
packaging one directory
yuki-kimoto authored on 2009-11-16
870
        }
871
    );
872

            
version 0.0901
yuki-kimoto authored on 2009-12-17
873
=head2 create_query
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
874
    
version 0.0901
yuki-kimoto authored on 2009-12-17
875
Create Query object parsing SQL template
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
876

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
879
$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
880

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

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

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

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

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

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

            
rename query() to execute()
yuki-kimoto authored on 2010-05-01
893
    $result = $dbi->execute("select * from authors where {= name} and {= age}", 
removed register_format()
yuki-kimoto authored on 2010-05-26
894
                            {name => 'taro', age => 19});
packaging one directory
yuki-kimoto authored on 2009-11-16
895
    
896
    while (my @row = $result->fetch) {
897
        # do something
898
    }
899

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
904
=head2 insert
905

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
908
    $affected = $dbi->insert(table  => $table, 
909
                             param  => {%param},
910
                             append => $append,
911
                             filter => {%filter});
update document
yuki-kimoto authored on 2009-11-19
912

            
version 0.0901
yuki-kimoto authored on 2009-12-17
913
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
914
    
removed register_format()
yuki-kimoto authored on 2010-05-26
915
Example
version 0.0901
yuki-kimoto authored on 2009-12-17
916

            
removed register_format()
yuki-kimoto authored on 2010-05-26
917
    # insert
918
    $dbi->insert(table  => 'books', 
919
                 param  => {title => 'Perl', author => 'Taro'},
920
                 append => "some statement",
921
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
922

            
packaging one directory
yuki-kimoto authored on 2009-11-16
923
=head2 update
924

            
update document
yuki-kimoto authored on 2009-11-19
925
Update rows
926

            
removed register_format()
yuki-kimoto authored on 2010-05-26
927
    $affected = $dbi->update(table  => $table, 
928
                             param  => {%params},
929
                             where  => {%where},
930
                             append => $append,
931
                             filter => {%filter})
version 0.0901
yuki-kimoto authored on 2009-12-17
932

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
937
    #update
938
    $dbi->update(table  => 'books',
939
                 param  => {title => 'Perl', author => 'Taro'},
940
                 where  => {id => 5},
941
                 append => "some statement",
942
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
943

            
packaging one directory
yuki-kimoto authored on 2009-11-16
944
=head2 update_all
945

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
948
    $affected = $dbi->update_all(table  => $table, 
949
                                 param  => {%params},
950
                                 filter => {%filter},
951
                                 append => $append);
update document
yuki-kimoto authored on 2009-11-19
952

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
957
    # update_all
958
    $dbi->update_all(table  => 'books', 
959
                     param  => {author => 'taro'},
960
                     filter => {author => 'encode_utf8'});
packaging one directory
yuki-kimoto authored on 2009-11-16
961

            
962
=head2 delete
963

            
update document
yuki-kimoto authored on 2009-11-19
964
Delete rows
965

            
removed register_format()
yuki-kimoto authored on 2010-05-26
966
    # delete
967
    $affected = $dbi->delete(table  => $table,
968
                             where  => {%where},
969
                             append => $append
970
                             filter => {%filter});
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
971

            
version 0.0901
yuki-kimoto authored on 2009-12-17
972
Retrun value is affected rows count
packaging one directory
yuki-kimoto authored on 2009-11-16
973
    
removed register_format()
yuki-kimoto authored on 2010-05-26
974
Example
packaging one directory
yuki-kimoto authored on 2009-11-16
975

            
removed register_format()
yuki-kimoto authored on 2010-05-26
976
    # delete
977
    $dbi->delete(table  => 'books',
978
                 where  => {id => 5},
979
                 append => 'some statement',
980
                 filter => {id => 'encode_utf8');
version 0.0901
yuki-kimoto authored on 2009-12-17
981

            
packaging one directory
yuki-kimoto authored on 2009-11-16
982
=head2 delete_all
983

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

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
990
Example
991
    
992
    # delete_all
version 0.0901
yuki-kimoto authored on 2009-12-17
993
    $dbi->delete_all('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
994

            
995
=head2 select
996
    
update document
yuki-kimoto authored on 2009-11-19
997
Select rows
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
998

            
removed register_format()
yuki-kimoto authored on 2010-05-26
999
    $result = $dbi->select(table  => $table,
1000
                           column => [@column],
1001
                           where  => {%where},
1002
                           append => $append,
1003
                           filter => {%filter});
update document
yuki-kimoto authored on 2009-11-19
1004

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1009
    # select
update document
yuki-kimoto authored on 2009-11-19
1010
    $result = $dbi->select('books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1011
    
update document
yuki-kimoto authored on 2009-11-19
1012
    # select * from books where title = 'Perl';
1013
    $result = $dbi->select('books', {title => 1});
1014
    
1015
    # select title, author from books where id = 1 for update;
1016
    $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1017
        table  => 'books',
1018
        where  => ['title', 'author'],
1019
        where  => {id => 1},
1020
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1021
    );
1022

            
1023
You can join multi tables
1024
    
1025
    $result = $dbi->select(
1026
        ['table1', 'table2'],                # tables
1027
        ['table1.id as table1_id', 'title'], # columns (alias is ok)
1028
        {table1.id => 1},                    # where clase
1029
        "where table1.id = table2.id",       # join clause (must start 'where')
1030
    );
1031

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1034
By default, "AutoCommit" and "RaiseError" is true.
version 0.0901
yuki-kimoto authored on 2009-12-17
1035

            
packaging one directory
yuki-kimoto authored on 2009-11-16
1036
=head1 AUTHOR
1037

            
1038
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
1039

            
1040
Github L<http://github.com/yuki-kimoto>
1041

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

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

            
1046
Copyright 2009 Yuki Kimoto, all rights reserved.
1047

            
1048
This program is free software; you can redistribute it and/or modify it
1049
under the same terms as Perl itself.
1050

            
1051
=cut