DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1438 lines | 37.868kb
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;
cleanup
yuki-kimoto authored on 2010-02-11
11
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
12
use DBIx::Custom::QueryBuilder;
update document
yuki-kimoto authored on 2010-05-27
13
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
14

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

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

            
cleanup
yuki-kimoto authored on 2010-04-28
26
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
27
__PACKAGE__->attr(sql_builder  => sub {DBIx::Custom::QueryBuilder->new});
added cache_method attribute
yuki-kimoto authored on 2010-06-25
28

            
add cache attribute
yuki-kimoto authored on 2010-06-14
29
__PACKAGE__->attr(cache => 1);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
30
__PACKAGE__->attr(cache_method => sub {
31
    sub {
32
        my $self = shift;
33
        
34
        $self->{_cached} ||= {};
35
        
36
        if (@_ > 1) {
37
            $self->{_cached}{$_[0]} = $_[1] 
38
        }
39
        else {
40
            return $self->{_cached}{$_[0]}
41
        }
42
    }
43
});
removed register_format()
yuki-kimoto authored on 2010-05-26
44

            
added check_filter attribute
yuki-kimoto authored on 2010-08-08
45
__PACKAGE__->attr(filter_check => 1);
46

            
packaging one directory
yuki-kimoto authored on 2009-11-16
47
sub connect {
removed register_format()
yuki-kimoto authored on 2010-05-26
48
    my $proto = shift;
49
    
50
    # Create
51
    my $self = ref $proto ? $proto : $proto->new(@_);
update document
yuki-kimoto authored on 2010-01-30
52
    
53
    # Information
packaging one directory
yuki-kimoto authored on 2009-11-16
54
    my $data_source = $self->data_source;
55
    my $user        = $self->user;
56
    my $password    = $self->password;
57
    
update document
yuki-kimoto authored on 2010-01-30
58
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
59
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
60
        $data_source,
61
        $user,
62
        $password,
63
        {
64
            RaiseError => 1,
65
            PrintError => 0,
66
            AutoCommit => 1,
67
        }
68
    )};
69
    
update document
yuki-kimoto authored on 2010-01-30
70
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
71
    croak $@ if $@;
72
    
update document
yuki-kimoto authored on 2010-01-30
73
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
74
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
75
    
packaging one directory
yuki-kimoto authored on 2009-11-16
76
    return $self;
77
}
78

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
79
sub register_filter {
80
    my $invocant = shift;
update document
yuki-kimoto authored on 2010-01-30
81
    
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
82
    # Register filter
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
83
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
84
    $invocant->filters({%{$invocant->filters}, %$filters});
update document
yuki-kimoto authored on 2010-01-30
85
    
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
86
    return $invocant;
packaging one directory
yuki-kimoto authored on 2009-11-16
87
}
88

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
91
sub insert {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
92
    my ($self, %args) = @_;
removed register_format()
yuki-kimoto authored on 2010-05-26
93

            
cleanup insert
yuki-kimoto authored on 2010-04-28
94
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
95
    foreach my $name (keys %args) {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
96
        croak qq{"$name" is invalid name}
cleanup insert
yuki-kimoto authored on 2010-04-28
97
          unless $VALID_INSERT_ARGS{$name};
98
    }
99
    
removed register_format()
yuki-kimoto authored on 2010-05-26
100
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
101
    my $table  = $args{table} || '';
102
    my $param  = $args{param} || {};
103
    my $append = $args{append} || '';
104
    my $filter = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
105
    
106
    # Insert keys
removed register_format()
yuki-kimoto authored on 2010-05-26
107
    my @insert_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
108
    
109
    # Templte for insert
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
110
    my $source = "insert into $table {insert "
update document
yuki-kimoto authored on 2010-08-07
111
               . join(' ', @insert_keys) . '}';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
112
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
113
    
114
    # Execute query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
115
    my $ret_val = $self->execute($source, param  => $param, 
removed register_format()
yuki-kimoto authored on 2010-05-26
116
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
117
    
118
    return $ret_val;
119
}
120

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
124
sub update {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
125
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
126
    
127
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
128
    foreach my $name (keys %args) {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
129
        croak qq{"$name" is invalid name}
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
130
          unless $VALID_UPDATE_ARGS{$name};
131
    }
132
    
133
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
134
    my $table            = $args{table} || '';
135
    my $param            = $args{param} || {};
136
    my $where            = $args{where} || {};
137
    my $append_statement = $args{append} || '';
138
    my $filter           = $args{filter};
139
    my $allow_update_all = $args{allow_update_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
140
    
141
    # Update keys
removed register_format()
yuki-kimoto authored on 2010-05-26
142
    my @update_keys = keys %$param;
packaging one directory
yuki-kimoto authored on 2009-11-16
143
    
144
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
145
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
146
    
147
    # Not exists where keys
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
148
    croak qq{"where" must contain column-value pair}
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
149
      if !@where_keys && !$allow_update_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
150
    
151
    # Update clause
152
    my $update_clause = '{update ' . join(' ', @update_keys) . '}';
153
    
154
    # Where clause
155
    my $where_clause = '';
simplify filtering system
yuki-kimoto authored on 2010-05-01
156
    my $new_where = {};
many change
yuki-kimoto authored on 2010-04-30
157
    
packaging one directory
yuki-kimoto authored on 2009-11-16
158
    if (@where_keys) {
159
        $where_clause = 'where ';
160
        foreach my $where_key (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
161
            
162
            $where_clause .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
163
        }
164
        $where_clause =~ s/ and $//;
165
    }
166
    
167
    # Template for update
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
168
    my $source = "update $table $update_clause $where_clause";
169
    $source .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
170
    
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
171
    # Rearrange parameters
removed register_format()
yuki-kimoto authored on 2010-05-26
172
    foreach my $wkey (@where_keys) {
simplify filtering system
yuki-kimoto authored on 2010-05-01
173
        
removed register_format()
yuki-kimoto authored on 2010-05-26
174
        if (exists $param->{$wkey}) {
175
            $param->{$wkey} = [$param->{$wkey}]
176
              unless ref $param->{$wkey} eq 'ARRAY';
simplify filtering system
yuki-kimoto authored on 2010-05-01
177
            
removed register_format()
yuki-kimoto authored on 2010-05-26
178
            push @{$param->{$wkey}}, $where->{$wkey};
simplify filtering system
yuki-kimoto authored on 2010-05-01
179
        }
add tests
yuki-kimoto authored on 2010-05-01
180
        else {
removed register_format()
yuki-kimoto authored on 2010-05-26
181
            $param->{$wkey} = $where->{$wkey};
add tests
yuki-kimoto authored on 2010-05-01
182
        }
simplify filtering system
yuki-kimoto authored on 2010-05-01
183
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
184
    
185
    # Execute query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
186
    my $ret_val = $self->execute($source, param  => $param, 
removed register_format()
yuki-kimoto authored on 2010-05-26
187
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
188
    
189
    return $ret_val;
190
}
191

            
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
192
sub update_all { shift->update(allow_update_all => 1, @_) };
packaging one directory
yuki-kimoto authored on 2009-11-16
193

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
197
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
198
    my ($self, %args) = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
199
    
200
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
201
    foreach my $name (keys %args) {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
202
        croak qq{"$name" is invalid name}
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
203
          unless $VALID_DELETE_ARGS{$name};
204
    }
205
    
206
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
207
    my $table            = $args{table} || '';
208
    my $where            = $args{where} || {};
209
    my $append_statement = $args{append};
210
    my $filter           = $args{filter};
211
    my $allow_delete_all = $args{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
212
    
213
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
214
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
215
    
216
    # Not exists where keys
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
217
    croak qq{Key-value pairs for where clause must be specified to "delete" second argument}
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
218
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
219
    
220
    # Where clause
221
    my $where_clause = '';
222
    if (@where_keys) {
223
        $where_clause = 'where ';
removed register_format()
yuki-kimoto authored on 2010-05-26
224
        foreach my $wkey (@where_keys) {
225
            $where_clause .= "{= $wkey} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
226
        }
227
        $where_clause =~ s/ and $//;
228
    }
229
    
230
    # Template for delete
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
231
    my $source = "delete from $table $where_clause";
232
    $source .= " $append_statement" if $append_statement;
packaging one directory
yuki-kimoto authored on 2009-11-16
233
    
234
    # Execute query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
235
    my $ret_val = $self->execute($source, param  => $where, 
removed register_format()
yuki-kimoto authored on 2010-05-26
236
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
237
    
238
    return $ret_val;
239
}
240

            
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
241
sub delete_all { shift->delete(allow_delete_all => 1, @_) }
packaging one directory
yuki-kimoto authored on 2009-11-16
242

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
246
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
247
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
248
    
refactoring select
yuki-kimoto authored on 2010-04-28
249
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
250
    foreach my $name (keys %args) {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
251
        croak qq{"$name" is invalid name}
refactoring select
yuki-kimoto authored on 2010-04-28
252
          unless $VALID_SELECT_ARGS{$name};
253
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
254
    
refactoring select
yuki-kimoto authored on 2010-04-28
255
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
256
    my $tables = $args{table} || [];
removed register_format()
yuki-kimoto authored on 2010-05-26
257
    $tables = [$tables] unless ref $tables eq 'ARRAY';
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
258
    my $columns  = $args{column} || [];
update document
yuki-kimoto authored on 2010-08-07
259
    my $where    = $args{where};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
260
    my $relation = $args{relation};
261
    my $append   = $args{append};
262
    my $filter   = $args{filter};
packaging one directory
yuki-kimoto authored on 2009-11-16
263
    
264
    # SQL template for select statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
265
    my $source = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
266
    
added commit method
yuki-kimoto authored on 2010-05-27
267
    # Column clause
packaging one directory
yuki-kimoto authored on 2009-11-16
268
    if (@$columns) {
269
        foreach my $column (@$columns) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
270
            $source .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
271
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
272
        $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
273
    }
274
    else {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
275
        $source .= '* ';
packaging one directory
yuki-kimoto authored on 2009-11-16
276
    }
277
    
added commit method
yuki-kimoto authored on 2010-05-27
278
    # Table
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
279
    $source .= 'from ';
packaging one directory
yuki-kimoto authored on 2009-11-16
280
    foreach my $table (@$tables) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
281
        $source .= "$table, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
282
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
283
    $source =~ s/, $/ /;
packaging one directory
yuki-kimoto authored on 2009-11-16
284
    
added commit method
yuki-kimoto authored on 2010-05-27
285
    # Where clause
update document
yuki-kimoto authored on 2010-08-07
286
    my $param;
287
    if (ref $where eq 'HASH') {
288
        $param = $where;
289
        $source .= 'where (';
290
        foreach my $where_key (keys %$where) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
291
            $source .= "{= $where_key} and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
292
        }
update document
yuki-kimoto authored on 2010-08-07
293
        $source =~ s/ and $//;
294
        $source .= ') ';
295
    }
296
    elsif (ref $where eq 'ARRAY') {
297
        my$where_str = $where->[0] || '';
298
        $param = $where->[1];
299
        
300
        $source .= "where ($where_str) ";
packaging one directory
yuki-kimoto authored on 2009-11-16
301
    }
302
    
added commit method
yuki-kimoto authored on 2010-05-27
303
    # Relation
304
    if ($relation) {
update document
yuki-kimoto authored on 2010-08-07
305
        $source .= $where ? "and " : "where ";
added commit method
yuki-kimoto authored on 2010-05-27
306
        foreach my $rkey (keys %$relation) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
307
            $source .= "$rkey = " . $relation->{$rkey} . " and ";
packaging one directory
yuki-kimoto authored on 2009-11-16
308
        }
309
    }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
310
    $source =~ s/ and $//;
added commit method
yuki-kimoto authored on 2010-05-27
311
    
312
    # Append some statement
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
313
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
314
    
315
    # Execute query
update document
yuki-kimoto authored on 2010-08-07
316
    my $result = $self->execute($source, param  => $param, 
317
                                         filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
318
    
319
    return $result;
320
}
321

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
322
sub create_query {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
323
    my ($self, $source) = @_;
removed register_format()
yuki-kimoto authored on 2010-05-26
324
    
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
325
    # Cache
added cache_method attribute
yuki-kimoto authored on 2010-06-25
326
    my $cache = $self->cache;
version 0.0901
yuki-kimoto authored on 2009-12-17
327
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
328
    # Create query
329
    my $query;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
330
    if ($cache) {
331
        
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
332
        # Get query
333
        my $q = $self->cache_method->($self, $source);
added cache_method attribute
yuki-kimoto authored on 2010-06-25
334
        
335
        # Create query
336
        $query = DBIx::Custom::Query->new($q) if $q;
removed reconnect method
yuki-kimoto authored on 2010-05-28
337
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
338
    
339
    unless ($query) {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
340

            
341
        # Create SQL object
342
        my $builder = $self->sql_builder;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
343
        
344
        # Create query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
345
        $query = eval{$builder->build_query($source)};
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
346
        croak $@ if $@;
removed reconnect method
yuki-kimoto authored on 2010-05-28
347
        
added cache_method attribute
yuki-kimoto authored on 2010-06-25
348
        # Cache query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
349
        $self->cache_method->($self, $source,
added cache_method attribute
yuki-kimoto authored on 2010-06-25
350
                             {sql     => $query->sql, 
351
                              columns => $query->columns})
352
          if $cache;
removed reconnect method
yuki-kimoto authored on 2010-05-28
353
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
354
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
355
    # Prepare statement handle
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
356
    my $sth = eval {$self->dbh->prepare($query->{sql})};
update document
yuki-kimoto authored on 2010-08-07
357
    croak qq{$@ SQL: "$query->{sql}"} if $@;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
358
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
359
    # Set statement handle
360
    $query->sth($sth);
361
    
362
    return $query;
363
}
364

            
365
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter/;
366

            
367
sub execute{
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
368
    my ($self, $query, %args)  = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
369
    
370
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
371
    foreach my $name (keys %args) {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
372
        croak qq{"$name" is invalid name}
removed reconnect method
yuki-kimoto authored on 2010-05-28
373
          unless $VALID_EXECUTE_ARGS{$name};
374
    }
375
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
376
    my $params = $args{param} || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
377
    
378
    # First argument is SQL template
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
379
    $query = $self->create_query($query)
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
380
      unless ref $query;
removed reconnect method
yuki-kimoto authored on 2010-05-28
381
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
382
    my $filter = $args{filter} || $query->filter || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
383
    
384
    # Create bind value
385
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
386
    
387
    # Execute
388
    my $sth      = $query->sth;
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
389
    my $affected = eval {$sth->execute(@$bind_values)};
390
    croak $@ if $@;
removed reconnect method
yuki-kimoto authored on 2010-05-28
391
    
392
    # Return resultset if select statement is executed
393
    if ($sth->{NUM_OF_FIELDS}) {
394
        
395
        # Create result
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
396
        my $result = $self->result_class->new(
397
            sth            => $sth,
398
            default_filter => $self->default_fetch_filter,
added check_filter attribute
yuki-kimoto authored on 2010-08-08
399
            filters        => $self->filters,
400
            filter_check   => $self->filter_check
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
401
        );
402

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
403
        return $result;
404
    }
405
    return $affected;
406
}
407

            
408
sub _build_bind_values {
409
    my ($self, $query, $params, $filter) = @_;
410
    
411
    # binding values
412
    my @bind_values;
413
    
414
    # Build bind values
415
    my $count = {};
416
    foreach my $column (@{$query->columns}) {
417
        
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
418
        croak qq{"$column" is not exists in params}
removed reconnect method
yuki-kimoto authored on 2010-05-28
419
          unless exists $params->{$column};
420
        
421
        # Value
422
        my $value = ref $params->{$column} eq 'ARRAY'
423
                  ? $params->{$column}->[$count->{$column} || 0]
424
                  : $params->{$column};
425
        
426
        # Filter
427
        $filter ||= {};
428
        
429
        # Filter name
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
430
        my $fname = $filter->{$column} || $self->default_bind_filter || '';
removed reconnect method
yuki-kimoto authored on 2010-05-28
431
        
432
        my $filter_func;
433
        if ($fname) {
434
            
435
            if (ref $fname eq 'CODE') {
436
                $filter_func = $fname;
437
            }
438
            else {
439
                my $filters = $self->filters;
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
440
                croak qq{Not exists filter "$fname"}
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
441
                  unless exists $filters->{$fname};
removed reconnect method
yuki-kimoto authored on 2010-05-28
442
                $filter_func = $filters->{$fname};
443
            }            
444
        }
445
        
446
        push @bind_values, $filter_func
447
                         ? $filter_func->($value)
448
                         : $value;
449
        
450
        # Count up 
451
        $count->{$column}++;
452
    }
453
    
454
    return \@bind_values;
455
}
456

            
457
=head1 NAME
458

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
459
DBIx::Custom - DBI interface, having hash parameter binding and filtering system
removed reconnect method
yuki-kimoto authored on 2010-05-28
460

            
461
=cut
462

            
updated document
yuki-kimoto authored on 2010-08-08
463
our $VERSION = '0.1609';
removed reconnect method
yuki-kimoto authored on 2010-05-28
464

            
465
=head1 STABILITY
466

            
updated document
yuki-kimoto authored on 2010-08-08
467
B<This module is not stable>.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
468
Method name and implementations will be changed for a while.
removed reconnect method
yuki-kimoto authored on 2010-05-28
469

            
470
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
471

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
472
Connect to the database.
473
    
474
    use DBIx::Custom;
removed reconnect method
yuki-kimoto authored on 2010-05-28
475
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
476
                                    user => 'ken', password => '!LFKD%$&');
cleanup
yuki-kimoto authored on 2010-08-05
477

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
478
Insert, update, and delete
cleanup
yuki-kimoto authored on 2010-08-05
479

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
480
    # Insert 
481
    $dbi->insert(table  => 'books',
482
                 param  => {title => 'perl', author => 'Ken'},
483
                 filter => {title => 'encode_utf8'});
484
    
485
    # Update 
486
    $dbi->update(table  => 'books', 
487
                 param  => {title => 'aaa', author => 'Ken'}, 
488
                 where  => {id => 5},
489
                 filter => {title => 'encode_utf8'});
490
    
491
    # Update all
492
    $dbi->update_all(table  => 'books',
493
                     param  => {title => 'aaa'},
494
                     filter => {title => 'encode_utf8'});
495
    
496
    # Delete
497
    $dbi->delete(table  => 'books',
498
                 where  => {author => 'Ken'},
499
                 filter => {title => 'encode_utf8'});
500
    
501
    # Delete all
502
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
503

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
504
Select
cleanup
yuki-kimoto authored on 2010-08-05
505

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
506
    # Select
507
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
508
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
509
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
510
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
511
        table  => 'books',
512
        column => [qw/author title/],
513
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
514
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
515
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
516
    );
added commit method
yuki-kimoto authored on 2010-05-27
517
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
518
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
519
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
520
        table    => ['books', 'rental'],
521
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
522
        relation => {'books.id' => 'rental.book_id'}
523
    );
updated document
yuki-kimoto authored on 2010-08-08
524
    
525
    # Select, more flexible where
526
    my $result = $dbi->select(
527
        table  => 'books',
528
        where  => ['{= author} and {like title}', 
529
                   {author => 'Ken', title => '%Perl%'}]
530
    );
cleanup
yuki-kimoto authored on 2010-08-05
531

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
532
Execute SQL
cleanup
yuki-kimoto authored on 2010-08-05
533

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
534
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
535
    $dbi->execute("select title from books");
536
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
537
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
538
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
539
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
540
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
541

            
542
    # Create query and execute it
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
543
    my $query = $dbi->create_query(
updated document
yuki-kimoto authored on 2010-08-08
544
        "select id from books where {= author} and {like title}"
removed reconnect method
yuki-kimoto authored on 2010-05-28
545
    );
updated document
yuki-kimoto authored on 2010-08-08
546
    $dbi->execute($query, param => {author => 'Ken', title => '%Perl%'})
cleanup
yuki-kimoto authored on 2010-08-05
547

            
updated document
yuki-kimoto authored on 2010-08-08
548
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
549

            
removed register_format()
yuki-kimoto authored on 2010-05-26
550
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
551
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
552
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
553

            
554
    # Get DBI object
555
    my $dbh = $dbi->dbh;
556

            
557
Fetch row.
558

            
removed register_format()
yuki-kimoto authored on 2010-05-26
559
    # Fetch
560
    while (my $row = $result->fetch) {
561
        # ...
562
    }
563
    
564
    # Fetch hash
565
    while (my $row = $result->fetch_hash) {
566
        
567
    }
568
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
569
=head1 DESCRIPTION
570

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
571
=head2 1. Features
removed reconnect method
yuki-kimoto authored on 2010-05-28
572

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
573
L<DBIx::Custom> is one of L<DBI> interface modules,
574
such as L<DBIx::Class>, L<DBIx::Simple>.
removed reconnect method
yuki-kimoto authored on 2010-05-28
575

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
576
This module is not O/R mapper. O/R mapper is useful,
577
but you must learn many syntax of the O/R mapper,
updated document
yuki-kimoto authored on 2010-08-08
578
which is almost another language.
579
Created SQL statement is offten not effcient and damage SQL performance.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
580
so you have to execute raw SQL in the end.
removed reconnect method
yuki-kimoto authored on 2010-05-28
581

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
582
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper.
updated document
yuki-kimoto authored on 2010-08-08
583
L<DBIx::Custom> provide flexible hash parameter binding and filtering system,
584
and suger methods, such as C<select()>, C<update()>, C<delete()>, C<select()>
585
to execute SQL easily.
removed reconnect method
yuki-kimoto authored on 2010-05-28
586

            
updated document
yuki-kimoto authored on 2010-08-08
587
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful,
588
but de-facto standard,
589
so all people learing database know it.
590
If you know SQL,
591
you learn a little thing to do your works, using L<DBIx::Custom>
removed reconnect method
yuki-kimoto authored on 2010-05-28
592

            
update document
yuki-kimoto authored on 2010-08-07
593
=head2 1. Connect to the database
removed reconnect method
yuki-kimoto authored on 2010-05-28
594

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
595
C<connect()> method create a new L<DBIx::Custom>
596
object and connect to the database.
removed reconnect method
yuki-kimoto authored on 2010-05-28
597

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
598
    use DBIx::Custom;
599
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
600
                                    user => 'ken', password => '!LFKD%$&');
601

            
update document
yuki-kimoto authored on 2010-08-07
602
If database is SQLite, use L<DBIx::Custom::SQLite>. you connect database easy way.
603

            
604
    use DBIx::Custom::SQLite;
605
    my $dbi = DBIx::Custom->connect(database => 'books');
606
    
updated document
yuki-kimoto authored on 2010-08-08
607
If database is  MySQL, use L<DBIx::Costom::MySQL>.
update document
yuki-kimoto authored on 2010-08-07
608

            
609
    use DBIx::Custom::MySQL;
610
    my $dbi = DBIx::Custom->connect(database => 'books',
611
                                    user => 'ken', password => '!LFKD%$&');
612

            
613
=head2 2. Suger methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
614

            
615
L<DBIx::Custom> has suger methods, such as C<insert()>, C<update()>,
updated document
yuki-kimoto authored on 2010-08-08
616
C<delete()> and C<select()>. If you want to do small works,
617
You don't have to create SQL statements.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
618

            
update document
yuki-kimoto authored on 2010-08-07
619
=head3 insert()
620

            
updated document
yuki-kimoto authored on 2010-08-08
621
Execute insert statement.
622

            
update document
yuki-kimoto authored on 2010-08-07
623
    $dbi->insert(table  => 'books',
624
                 param  => {title => 'perl', author => 'Ken'});
625

            
626
The following SQL is executed.
627

            
updated document
yuki-kimoto authored on 2010-08-08
628
    insert into (title, author) values (?, ?);
update document
yuki-kimoto authored on 2010-08-07
629

            
630
The values of C<title> and C<author> is embedded into placeholders.
631

            
updated document
yuki-kimoto authored on 2010-08-08
632
C<append> and C<filter> argument can be specified
633
to C<insert()> method if you need.
update document
yuki-kimoto authored on 2010-08-07
634

            
635
=head3 update()
636

            
updated document
yuki-kimoto authored on 2010-08-08
637
Execute update statement.
638

            
update document
yuki-kimoto authored on 2010-08-07
639
    $dbi->update(table  => 'books', 
640
                 param  => {title => 'aaa', author => 'Ken'}, 
641
                 where  => {id => 5});
642

            
643
The following SQL is executed.
644

            
645
    update books set title = ?, author = ?;
646

            
647
The values of C<title> and C<author> is embedded into placeholders.
648

            
updated document
yuki-kimoto authored on 2010-08-08
649
C<append> and C<filter> argument can be specified
650
to C<update()> method if you need.
update document
yuki-kimoto authored on 2010-08-07
651

            
updated document
yuki-kimoto authored on 2010-08-08
652
If you want to update all rows, use C<update_all()> method instead.
update document
yuki-kimoto authored on 2010-08-07
653

            
654
=head3 delete()
655

            
updated document
yuki-kimoto authored on 2010-08-08
656
Execute delete statement.
657

            
update document
yuki-kimoto authored on 2010-08-07
658
    $dbi->delete(table  => 'books',
659
                 where  => {author => 'Ken'});
660

            
661
The following SQL is executed.
662

            
663
    delete from books where id = ?;
664

            
updated document
yuki-kimoto authored on 2010-08-08
665
The value of C<id> is embedded into the placehodler.
update document
yuki-kimoto authored on 2010-08-07
666

            
updated document
yuki-kimoto authored on 2010-08-08
667
C<append> and C<filter> argument can be specified
668
to C<delete()> method if you need.
update document
yuki-kimoto authored on 2010-08-07
669

            
670
If you want to delete all rows, use C<delete_all()> method instead.
671

            
672
=head3 select()
673

            
updated document
yuki-kimoto authored on 2010-08-08
674
Execute select statement, only C<table> argument specified :
update document
yuki-kimoto authored on 2010-08-07
675

            
676
    my $result = $dbi->select(table => 'books');
677

            
678
The following SQL is executed.
679

            
680
    select * from books;
681

            
682
the result of C<select()> method is L<DBIx::Custom::Result> object.
updated document
yuki-kimoto authored on 2010-08-08
683
You can fetch row.
update document
yuki-kimoto authored on 2010-08-07
684

            
685
    while (my $row = $result->fetch) {
686
        my $title  = $row->[0];
687
        my $author = $row->[1];
688
    }
689

            
690
L<DBIx::Custom::Result> has various methods to fetch row.
updated document
yuki-kimoto authored on 2010-08-08
691
See "3. Fetch row".
update document
yuki-kimoto authored on 2010-08-07
692

            
updated document
yuki-kimoto authored on 2010-08-08
693
Specify C<column> and C<where> arguments.
update document
yuki-kimoto authored on 2010-08-07
694

            
695
    my $result = $dbi->select(
696
        table  => 'books',
697
        column => [qw/author title/],
698
        where  => {author => 'Ken'});
699

            
700
The following SQL is executed.
701

            
702
    select author, title from books where author = ?;
703

            
704
the value of C<author> is embdded into placeholder.
705

            
updated document
yuki-kimoto authored on 2010-08-08
706
If you want to join tables, specify C<relation> argument. 
update document
yuki-kimoto authored on 2010-08-07
707

            
708
    my $result = $dbi->select(
709
        table    => ['books', 'rental'],
710
        column   => ['books.name as book_name']
711
        relation => {'books.id' => 'rental.book_id'}
712
    );
713

            
714
The following SQL is executed.
715

            
716
    select books.name as book_name from books
717
    where books.id = rental.book_id;
718

            
719
C<append> argument add a string to the end of SQL statement.
updated document
yuki-kimoto authored on 2010-08-08
720
You can add "order by" or "limit" cluase.
update document
yuki-kimoto authored on 2010-08-07
721

            
722
    # Select, more complex
723
    my $result = $dbi->select(
724
        table  => 'books',
725
        where  => {author => 'Ken'},
726
        append => 'order by price limit 5',
727
    );
728

            
729
The following SQL is executed.
730

            
731
    select * books where author = ? order by price limit 5;
732

            
updated document
yuki-kimoto authored on 2010-08-08
733
C<filter> argument can be specified to filter parameters
734
if you need.
update document
yuki-kimoto authored on 2010-08-07
735

            
updated document
yuki-kimoto authored on 2010-08-08
736
=head2 3. Fetch row
update document
yuki-kimoto authored on 2010-08-07
737

            
updated document
yuki-kimoto authored on 2010-08-08
738
C<select()> method return L<DBIx::Custom::Result> object.
739
You can fetch row by various methods.
update document
yuki-kimoto authored on 2010-08-07
740

            
741
Fetch row into array.
742
    
743
    while (my $row = $result->fetch) {
744
        my $author = $row->[0];
745
        my $title  = $row->[1];
746
        
747
    }
748

            
749
Fetch only a first row into array.
750

            
751
    my $row = $result->fetch_first;
752

            
753
Fetch multiple rows into array of array.
754

            
755
    while (my $rows = $result->fetch_multi(5)) {
756
        my $first_author  = $rows->[0][0];
757
        my $first_title   = $rows->[0][1];
758
        my $second_author = $rows->[1][0];
759
        my $second_value  = $rows->[1][1];
760
    
761
    }
762
    
763
Fetch all rows into array of array.
764

            
765
    my $rows = $result->fetch_all;
766

            
767
Fetch row into hash.
768

            
769
    # Fetch a row into hash
770
    while (my $row = $result->fetch_hash) {
771
        my $title  = $row->{title};
772
        my $author = $row->{author};
773
        
774
    }
775

            
776
Fetch only a first row into hash
777

            
778
    my $row = $result->fetch_hash_first;
779
    
780
Fetch multiple rows into array of hash
781

            
782
    while (my $rows = $result->fetch_hash_multi(5)) {
783
        my $first_title   = $rows->[0]{title};
784
        my $first_author  = $rows->[0]{author};
785
        my $second_title  = $rows->[1]{title};
786
        my $second_author = $rows->[1]{author};
787
    
788
    }
789
    
790
Fetch all rows into array of hash
791

            
792
    my $rows = $result->fetch_hash_all;
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
793

            
updated document
yuki-kimoto authored on 2010-08-08
794
If you want to access raw statement handle of L<DBI>, use C<sth()> attribute.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
795

            
update document
yuki-kimoto authored on 2010-08-07
796
    my $sth = $result->sth;
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
797

            
update document
yuki-kimoto authored on 2010-08-07
798
=head2 4. Hash parameter binding
removed reconnect method
yuki-kimoto authored on 2010-05-28
799

            
update document
yuki-kimoto authored on 2010-08-07
800
L<DBIx::Custom> provides hash parameter binding.
801

            
802
At frist, I show normal way of parameter binding.
803

            
804
    use DBI;
805
    my $dbh = DBI->connect(...);
806
    my $sth = $dbh->prepare(
807
        "select * from books where author = ? and title like ?;"
808
    );
809
    $sth->execute('Ken', '%Perl%');
810

            
updated document
yuki-kimoto authored on 2010-08-08
811
This is very good way because database system can enable SQL caching,
812
and parameter is quoted automatically, it is secure.
update document
yuki-kimoto authored on 2010-08-07
813

            
updated document
yuki-kimoto authored on 2010-08-08
814
L<DBIx::Custom> hash parameter binding system improve
815
normal parameter binding way to specify hash parameter.
update document
yuki-kimoto authored on 2010-08-07
816

            
817
    my $result = $dbi->execute(
818
        "select * from books where {= author} and {like title};"
819
        param => {author => 'Ken', title => '%Perl%'}
820
    );
821

            
822
This is same as the normal way, execpt that the parameter is hash.
updated document
yuki-kimoto authored on 2010-08-08
823
{= author} is called C<tag>. tag is expand to placeholder string internally.
update document
yuki-kimoto authored on 2010-08-07
824

            
825
    select * from books where {= author} and {like title}
826
      -> select * from books where author = ? and title like ?;
827

            
updated document
yuki-kimoto authored on 2010-08-08
828
The following tags is available.
829

            
830
=head1 Tags
831

            
832
The following tags is available.
833

            
834
    [TAG]                       [REPLACED]
835
    {? NAME}               ->   ?
836
    {= NAME}               ->   NAME = ?
837
    {<> NAME}              ->   NAME <> ?
838
    
839
    {< NAME}               ->   NAME < ?
840
    {> NAME}               ->   NAME > ?
841
    {>= NAME}              ->   NAME >= ?
842
    {<= NAME}              ->   NAME <= ?
843
    
844
    {like NAME}            ->   NAME like ?
845
    {in NAME COUNT}        ->   NAME in [?, ?, ..]
846
    
847
    {insert NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
848
    {update NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
849

            
850
See also L<DBIx::Custom::QueryBuilder>.
851

            
852
Default start tag is '{'. end tag is '}'.
853
You can change this tag.
854

            
855
    $dbi->query_builder->start_tag('|');
856
    $dbi->query_builder->end_tag('|');
update document
yuki-kimoto authored on 2010-08-07
857

            
858
=head2 5. Filtering
859

            
860
Usually, Perl string is kept as internal string.
861
If you want to save the string to database, You must encode the string.
862
Filtering system help you to convert a data to another data
863
when you save to the data and get the data form database.
864

            
updated document
yuki-kimoto authored on 2010-08-08
865
If you want to register filter, use C<register_filter()> method.
update document
yuki-kimoto authored on 2010-08-07
866

            
867
    $dbi->register_filter(
868
        to_upper_case => sub {
869
            my $value = shift;
870
            return uc $value;
871
        }
872
    );
873

            
874
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
875

            
876
You can specify these filters to C<filter> argument of C<execute()> method.
877

            
878
    my $result = $dbi->execute(
879
        "select * from books where {= author} and {like title};"
updated document
yuki-kimoto authored on 2010-08-08
880
        param  => {author => 'Ken', title => '%Perl%'},
update document
yuki-kimoto authored on 2010-08-07
881
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
882
    );
883

            
884
you can also specify filter in suger methods, such as select(), update(), update_all,
885
delete(), delete_all(), select().
886

            
887
    $dbi->insert(table  => 'books',
888
                 param  => {title => 'perl', author => 'Ken'},
889
                 filter => {title => 'encode_utf8'});
890

            
891
    my $result = $dbi->select(
892
        table  => 'books',
893
        column => [qw/author title/],
894
        where  => {author => 'Ken'},
895
        append => 'order by id limit 1',
896
        filter => {title => 'encode_utf8'}
897
    );
898

            
updated document
yuki-kimoto authored on 2010-08-08
899
Filter works each parmeter, but you prepare default filter for all parameters.
update document
yuki-kimoto authored on 2010-08-07
900

            
901
    $dbi->default_bind_filter('encode_utf8');
902

            
903
C<filter()> argument overwrites the filter specified by C<default_bind_filter()>.
904
    
905
    $dbi->default_bind_filter('encode_utf8');
906
    $dbi->insert(
907
        table  => 'books',
908
        param  => {title => 'perl', author => 'Ken', price => 1000},
909
        filter => {author => 'to_upper_case', price => undef}
910
    );
911

            
912
This is same as the following one.
913

            
914
    $dbi->insert(
915
        table  => 'books',
916
        param  => {title => 'perl', author => 'Ken', price => 1000},
917
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
918
    );
919

            
updated document
yuki-kimoto authored on 2010-08-08
920
You can also specify filter when the row is fetched. This is reverse of bind filter.
update document
yuki-kimoto authored on 2010-08-07
921

            
922
    my $result = $dbi->select(table => 'books');
923
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
924

            
925
you can specify C<default_fetch_filter()>.
926

            
927
    $dbi->default_fetch_filter('decode_utf8');
928

            
updated document
yuki-kimoto authored on 2010-08-08
929
C<DBIx::Custom::Result::filter()> overwrites the filter specified
update document
yuki-kimoto authored on 2010-08-07
930
by C<default_fetch_filter()>
931

            
932
    $dbi->default_fetch_filter('decode_utf8');
933
    my $result = $dbi->select(
934
        table => 'books',
935
        columns => ['title', 'author', 'price']
936
    );
937
    $result->filter({author => 'to_upper_case', price => undef});
938

            
939
This is same as the following one.
940

            
941
    my $result = $dbi->select(
942
        table => 'books',
943
        columns => ['title', 'author', 'price']
944
    );
945
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
946

            
added check_filter attribute
yuki-kimoto authored on 2010-08-08
947
In fetch filter, column name must be lower case even if column conatain upper case charactor. This is requirment not to depend database systems.
948

            
updated document
yuki-kimoto authored on 2010-08-08
949
=head2 6. Performance
950

            
951
If you execute insert statement by using select() method,
952
you sometimes can't meet performance requirment.
953

            
954
C<insert()> method is a little slow because SQL statement and statement handle
955
is created every time.
956

            
957
In that case, you can prepare a query by C<create_query()> method.
958
    
959
    my $query = $dbi->create_query(
960
        "insert into books {insert title author};"
961
    );
962
    
963
    # (In the case of update statement)
964
    my $query = $dbi->create_query(
965
        "update books {update author};";
966
    );
967

            
968
Execute query repeatedly
969
    
970
    my $inputs = [
971
        {title => 'Perl',      author => 'Ken'},
972
        {title => 'Good days', author => 'Mike'}
973
    ];
974
    
975
    foreach my $input (@$inputs) {
976
        $dbi->execute($query, $input);
977
    }
978

            
979
This is faster than C<insert()> and C<update()> method.
980

            
981
C<execute()> method cache the parsing result of SQL soruce.
982
Default to 1
983

            
984
    $dbi->cache(1);
985

            
986
Caching is on memory, but you can change this by C<cache_method()>.
987
First argument is L<DBIx::Custom> object.
988
Second argument is SQL source,
989
such as "select * from books where {=title} and {=author};";
990
Third argument is parsed result, such as
991
{sql => "select * from books where title = ? and author = ?",
992
 columns => ['title', 'author']}, this is hash reference.
993
If argument is more than two, this is called ti set cache.
994
otherwise, called to get cache.
995

            
996
    $dbi->cache_mehod(sub {
997
        sub {
998
            my $self = shift;
999
            
1000
            $self->{_cached} ||= {};
1001
            
1002
            # Set cache
1003
            if (@_ > 1) {
1004
                $self->{_cached}{$_[0]} = $_[1] 
1005
            }
1006
            
1007
            # Get cache
1008
            else {
1009
                return $self->{_cached}{$_[0]}
1010
            }
1011
        }
1012
    });
1013

            
1014
=head2 7. More features
1015

            
1016
=head3 Get DBI object
1017

            
1018
You can get L<DBI> object and call any method of L<DBI>.
1019

            
1020
    $dbi->dbh->begin_work;
1021
    $dbi->dbh->commit;
1022
    $dbi->dbh->rollback;
1023

            
1024
=head3 Change Result class
1025

            
1026
You can change Result class if you need.
1027

            
1028
    package Your::Result;
1029
    use base 'DBIx::Custom::Result';
1030
    
1031
    sub some_method { ... }
1032

            
1033
    1;
1034
    
1035
    package main;
1036
    
1037
    use Your::Result;
1038
    
1039
    my $dbi = DBIx::Custom->connect(...);
1040
    $dbi->result_class('Your::Result');
1041

            
1042
=head3 Custamize SQL builder object
1043

            
1044
You can custamize SQL builder object
1045

            
1046
    my $dbi = DBIx::Custom->connect(...);
1047
    $dbi->query_builder->start_tag('|');
1048
    $dbi->query_builder->end_tag('|');
1049
    $dbi->query_builder->register_tag_processor(
1050
        name => sub {
1051
           ...
1052
        }
1053
    );
1054

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1057
=head2 C<user>
packaging one directory
yuki-kimoto authored on 2009-11-16
1058

            
cleanup
yuki-kimoto authored on 2010-08-03
1059
    my $user = $dbi->user;
1060
    $dbi     = $dbi->user('Ken');
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1061

            
cleanup
yuki-kimoto authored on 2010-08-05
1062
User name.
1063
C<connect()> method use this value to connect the database.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1064
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1065
=head2 C<password>
packaging one directory
yuki-kimoto authored on 2009-11-16
1066

            
cleanup
yuki-kimoto authored on 2010-08-03
1067
    my $password = $dbi->password;
1068
    $dbi         = $dbi->password('lkj&le`@s');
packaging one directory
yuki-kimoto authored on 2009-11-16
1069

            
cleanup
yuki-kimoto authored on 2010-08-05
1070
Password.
1071
C<connect()> method use this value to connect the database.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1072

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1073
=head2 C<data_source>
packaging one directory
yuki-kimoto authored on 2009-11-16
1074

            
cleanup
yuki-kimoto authored on 2010-08-03
1075
    my $data_source = $dbi->data_source;
cleanup
yuki-kimoto authored on 2010-08-05
1076
    $dbi            = $dbi->data_source("DBI:mysql:database=dbname");
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1077

            
cleanup
yuki-kimoto authored on 2010-08-05
1078
Data source.
1079
C<connect()> method use this value to connect the database.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1080

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1081
=head2 C<dbh>
packaging one directory
yuki-kimoto authored on 2009-11-16
1082

            
cleanup
yuki-kimoto authored on 2010-08-03
1083
    my $dbh = $dbi->dbh;
1084
    $dbi    = $dbi->dbh($dbh);
packaging one directory
yuki-kimoto authored on 2009-11-16
1085

            
cleanup
yuki-kimoto authored on 2010-08-05
1086
L<DBI> object. You can call all methods of L<DBI>.
packaging one directory
yuki-kimoto authored on 2009-11-16
1087

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1088
=head2 C<filters>
packaging one directory
yuki-kimoto authored on 2009-11-16
1089

            
cleanup
yuki-kimoto authored on 2010-08-03
1090
    my $filters = $dbi->filters;
cleanup
yuki-kimoto authored on 2010-08-03
1091
    $dbi        = $dbi->filters(\%filters);
version 0.0901
yuki-kimoto authored on 2009-12-17
1092

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1093
Filter functions.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1094
"encode_utf8" and "decode_utf8" is registered by default.
version 0.0901
yuki-kimoto authored on 2009-12-17
1095

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
1096
=head2 C<default_bind_filter>
packaging one directory
yuki-kimoto authored on 2009-11-16
1097

            
cleanup
yuki-kimoto authored on 2010-08-03
1098
    my $default_bind_filter = $dbi->default_bind_filter
1099
    $dbi                    = $dbi->default_bind_filter('encode_utf8');
packaging one directory
yuki-kimoto authored on 2009-11-16
1100

            
cleanup
yuki-kimoto authored on 2010-08-05
1101
Default filter when parameter binding is executed.
packaging one directory
yuki-kimoto authored on 2009-11-16
1102

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1103
=head2 C<default_fetch_filter>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1104

            
cleanup
yuki-kimoto authored on 2010-08-03
1105
    my $default_fetch_filter = $dbi->default_fetch_filter;
1106
    $dbi                     = $dbi->default_fetch_filter('decode_utf8');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1107

            
cleanup
yuki-kimoto authored on 2010-08-05
1108
Default filter when row is fetched.
packaging one directory
yuki-kimoto authored on 2009-11-16
1109

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1110
=head2 C<result_class>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1111

            
cleanup
yuki-kimoto authored on 2010-08-03
1112
    my $result_class = $dbi->result_class;
1113
    $dbi             = $dbi->result_class('DBIx::Custom::Result');
packaging one directory
yuki-kimoto authored on 2009-11-16
1114

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1115
Result class for select statement.
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
1116
Default to L<DBIx::Custom::Result>.
update document
yuki-kimoto authored on 2010-01-30
1117

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
1118
=head2 C<sql_builder>
added commit method
yuki-kimoto authored on 2010-05-27
1119

            
cleanup
yuki-kimoto authored on 2010-08-03
1120
    my $sql_class = $dbi->sql_builder;
1121
    $dbi          = $dbi->sql_builder(DBIx::Custom::QueryBuilder->new);
added commit method
yuki-kimoto authored on 2010-05-27
1122

            
cleanup
yuki-kimoto authored on 2010-08-03
1123
SQL builder. sql_builder must be 
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1124
the instance of L<DBIx::Custom::QueryBuilder> subclass.
1125
Default to L<DBIx::Custom::QueryBuilder> object.
cleanup
yuki-kimoto authored on 2010-08-05
1126

            
1127
=head2 C<cache>
1128

            
1129
    my $cache = $dbi->cache;
1130
    $dbi      = $dbi->cache(1);
1131

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1132
Enable parsed L<DBIx::Custom::Query> object caching.
cleanup
yuki-kimoto authored on 2010-08-05
1133
Default to 1.
1134

            
1135
=head2 C<cache_method>
1136

            
1137
    $dbi          = $dbi->cache_method(\&cache_method);
1138
    $cache_method = $dbi->cache_method
1139

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1140
Method to set and get caches.
cleanup
yuki-kimoto authored on 2010-08-05
1141

            
1142
B<Example:>
1143

            
1144
    $dbi->cache_method(
1145
        sub {
1146
            my $self = shift;
1147
            
1148
            $self->{_cached} ||= {};
1149
            
1150
            if (@_ > 1) {
1151
                $self->{_cached}{$_[0]} = $_[1] 
1152
            }
1153
            else {
1154
                return $self->{_cached}{$_[0]}
1155
            }
1156
        }
1157
    );
added commit method
yuki-kimoto authored on 2010-05-27
1158

            
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1159
=head2 C<filter_check>
1160

            
1161
    my $filter_check = $dbi->filter_check;
1162
    $dbi             = $dbi->filter_check(0);
1163

            
1164
Enable filter check. 
1165
Default to 1.
1166
This check maybe damege performance.
1167
If you require performance, set C<filter_check> to 0.
1168

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1169
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1170

            
cleanup
yuki-kimoto authored on 2010-08-05
1171
L<DBIx::Custom> inherits all methods from L<Object::Simple>
1172
and implements the following new ones.
added commit method
yuki-kimoto authored on 2010-05-27
1173

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1174
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
1175

            
cleanup
yuki-kimoto authored on 2010-08-05
1176
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
update document
yuki-kimoto authored on 2010-05-27
1177
                                    user => 'ken', password => '!LFKD%$&');
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1178

            
cleanup
yuki-kimoto authored on 2010-08-05
1179
Create a new L<DBIx::Custom> object and connect to the database.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1180
L<DBIx::Custom> is a wrapper of L<DBI>.
1181
C<AutoCommit> and C<RaiseError> option is true, 
1182
and C<PrintError> option is false by default. 
packaging one directory
yuki-kimoto authored on 2009-11-16
1183

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1184
=head2 C<insert>
packaging one directory
yuki-kimoto authored on 2009-11-16
1185

            
cleanup
yuki-kimoto authored on 2010-08-05
1186
    $dbi->insert(table  => $table, 
1187
                 param  => \%param,
1188
                 append => $append,
1189
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1190

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1191
Execute insert statement.
1192
C<insert> method have C<table>, C<param>, C<append>
1193
and C<filter> arguments.
1194
C<table> is a table name.
1195
C<param> is column-value pairs. this must be hash reference.
1196
C<append> is a string added at the end of the SQL statement.
1197
C<filter> is filters when parameter binding is executed.
1198
This is overwrites C<default_bind_filter>.
1199
Return value of C<insert> is the count of affected rows.
1200

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1201
B<Example:>
version 0.0901
yuki-kimoto authored on 2009-12-17
1202

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1203
    $dbi->insert(table  => 'books', 
1204
                 param  => {title => 'Perl', author => 'Taro'},
1205
                 append => "some statement",
1206
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
1207

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1208
=head2 C<update>
packaging one directory
yuki-kimoto authored on 2009-11-16
1209

            
cleanup
yuki-kimoto authored on 2010-08-05
1210
    $dbi->update(table  => $table, 
1211
                 param  => \%params,
1212
                 where  => \%where,
1213
                 append => $append,
1214
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
1215

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1216
Execute update statement.
1217
C<update> method have C<table>, C<param>, C<where>, C<append>
1218
and C<filter> arguments.
1219
C<table> is a table name.
1220
C<param> is column-value pairs. this must be hash reference.
1221
C<where> is where clause. this must be hash reference.
1222
C<append> is a string added at the end of the SQL statement.
1223
C<filter> is filters when parameter binding is executed.
1224
This is overwrites C<default_bind_filter>.
1225
Return value of C<update> is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
1226

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1227
B<Example:>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1228

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1229
    $dbi->update(table  => 'books',
1230
                 param  => {title => 'Perl', author => 'Taro'},
1231
                 where  => {id => 5},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1232
                 append => "for update",
added commit method
yuki-kimoto authored on 2010-05-27
1233
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1234

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1235
=head2 C<update_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
1236

            
cleanup
yuki-kimoto authored on 2010-08-05
1237
    $dbi->update_all(table  => $table, 
1238
                     param  => \%params,
1239
                     filter => \%filter,
1240
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1241

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1242
Execute update statement to update all rows.
1243
Arguments is same as C<update> method,
1244
except that C<update_all> don't have C<where> argument.
1245
Return value of C<update_all> is the count of affected rows.
version 0.0901
yuki-kimoto authored on 2009-12-17
1246

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1247
B<Example:>
update document
yuki-kimoto authored on 2009-11-19
1248

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1249
    $dbi->update_all(table  => 'books', 
1250
                     param  => {author => 'taro'},
1251
                     filter => {author => 'encode_utf8'});
packaging one directory
yuki-kimoto authored on 2009-11-16
1252

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1253
=head2 C<delete>
packaging one directory
yuki-kimoto authored on 2009-11-16
1254

            
cleanup
yuki-kimoto authored on 2010-08-05
1255
    $dbi->delete(table  => $table,
1256
                 where  => \%where,
1257
                 append => $append,
1258
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1259

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1260
Execute delete statement.
1261
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1262
C<table> is a table name.
1263
C<where> is where clause. this must be hash reference.
1264
C<append> is a string added at the end of the SQL statement.
1265
C<filter> is filters when parameter binding is executed.
1266
Return value of C<delete> is the count of affected rows.
1267

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1268
B<Example:>
packaging one directory
yuki-kimoto authored on 2009-11-16
1269

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1270
    $dbi->delete(table  => 'books',
1271
                 where  => {id => 5},
1272
                 append => 'some statement',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1273
                 filter => {id => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1274

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1275
=head2 C<delete_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
1276

            
cleanup
yuki-kimoto authored on 2010-08-05
1277
    $dbi->delete_all(table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
1278

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1279
Execute delete statement to delete all rows.
1280
Arguments is same as C<delete> method,
1281
except that C<delete_all> don't have C<where> argument.
1282
Return value of C<delete_all> is the count of affected rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1283

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1284
B<Example:>
removed register_format()
yuki-kimoto authored on 2010-05-26
1285
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1286
    $dbi->delete_all(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1287

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1288
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1289
    
cleanup
yuki-kimoto authored on 2010-08-05
1290
    my $result = $dbi->select(table    => $table,
1291
                              column   => [@column],
1292
                              where    => \%where,
1293
                              append   => $append,
1294
                              relation => \%relation,
1295
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1296

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1297
Execute select statement.
1298
C<select> method have C<table>, C<column>, C<where>, C<append>
1299
C<relation> and C<filter> arguments.
1300
C<table> is a table name.
1301
C<where> is where clause. this must be hash reference
1302
or a string containing such tags as "{= title} or {= author}".
1303
C<append> is a string added at the end of the SQL statement.
1304
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1305

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1306
B<Example:>
update document
yuki-kimoto authored on 2009-11-19
1307

            
added commit method
yuki-kimoto authored on 2010-05-27
1308
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1309
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1310
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1311
    # select * from books where title = ?;
1312
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1313
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1314
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1315
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1316
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1317
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1318
        where  => {id => 1},
1319
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1320
    );
1321
    
added commit method
yuki-kimoto authored on 2010-05-27
1322
    # select books.name as book_name from books, rental 
1323
    # where books.id = rental.book_id;
1324
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1325
        table    => ['books', 'rental'],
1326
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1327
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1328
    );
1329

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1330
=head2 C<create_query>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1331
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1332
    my $query = $dbi->create_query(
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1333
        "select * from authors where {= name} and {= age};"
1334
    );
1335

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1336
Create the instance of L<DBIx::Custom::Query> from SQL source.
packaging one directory
yuki-kimoto authored on 2009-11-16
1337

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1338
=head2 C<execute>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1339

            
cleanup
yuki-kimoto authored on 2010-08-05
1340
    my $result = $dbi->execute($query,  param => $params, filter => \%filter);
1341
    my $result = $dbi->execute($source, param => $params, filter => \%filter);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1342

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1343
Execute query or SQL source. Query is L<DBIx::Csutom::Query> object.
1344
Return value is L<DBIx::Custom::Result> in select statement,
1345
or the count of affected rows in insert, update, delete statement.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1346

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1347
B<Example:>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1348

            
cleanup
yuki-kimoto authored on 2010-08-05
1349
    my $result = $dbi->execute("select * from authors where {= name} and {= age}", 
removed reconnect method
yuki-kimoto authored on 2010-05-28
1350
                            param => {name => 'taro', age => 19});
1351
    
1352
    while (my $row = $result->fetch) {
1353
        # do something
1354
    }
1355

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1356
=head2 C<register_filter>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1357

            
1358
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1359
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1360
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1361
Register filter. Registered filters is available in the following methods
1362
or arguments.
1363

            
1364
=over 4
1365

            
1366
=item *
1367

            
1368
C<default_bind_filter()>
1369

            
1370
=item *
1371

            
1372
C<default_fetch_filter()>
1373

            
1374
=item *
1375

            
1376
C<filter> argument of C<insert()>, C<update()>,
1377
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>,
1378
C<execute> method.
1379

            
1380
=item *
1381

            
1382
C<DBIx::Custom::Query::default_filter()>
1383

            
1384
=item *
1385

            
1386
C<DBIx::Csutom::Query::filter()>
1387

            
1388
=item *
1389

            
1390
C<DBIx::Custom::Result::default_filter()>
1391

            
1392
=item *
1393

            
1394
C<DBIx::Custom::Result::filter()>
1395

            
1396
=back
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1397

            
1398
B<Example:>
packaging one directory
yuki-kimoto authored on 2009-11-16
1399

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1400
    $dbi->register_filter(
1401
        encode_utf8 => sub {
1402
            my $value = shift;
1403
            
1404
            require Encode;
1405
            
1406
            return Encode::encode('UTF-8', $value);
1407
        },
1408
        decode_utf8 => sub {
1409
            my $value = shift;
1410
            
1411
            require Encode;
1412
            
1413
            return Encode::decode('UTF-8', $value)
1414
        }
1415
    );
1416

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1417
=head1 BUGS
1418

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1419
Please tell me bugs if found.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1420

            
1421
C<< <kimoto.yuki at gmail.com> >>
1422

            
1423
L<http://github.com/yuki-kimoto/DBIx-Custom>
1424

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1425
=head1 AUTHOR
1426

            
1427
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
version 0.0901
yuki-kimoto authored on 2009-12-17
1428

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

            
1431
Copyright 2009 Yuki Kimoto, all rights reserved.
1432

            
1433
This program is free software; you can redistribute it and/or modify it
1434
under the same terms as Perl itself.
1435

            
1436
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1437

            
1438