DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1478 lines | 38.982kb
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;
add tests
yuki-kimoto authored on 2010-08-08
413

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

            
add tests
yuki-kimoto authored on 2010-08-08
465
sub _check_filter {
466
    my ($self, $filters, $filter, $default_filter, $params) = @_;
467
    
468
    # Filter name not exists
469
    foreach my $fname (values %$filter) {
470
        croak qq{Bind filter "$fname" is not registered}
471
          unless exists $filters->{$fname};
472
    }
473
    
474
    # Default filter name not exists
475
    croak qq{Default bind filter "$default_filter" is not registered}
476
      if $default_filter && ! exists $filters->{$default_filter};
477
    
478
    # Column name not exists
479
    foreach my $column (keys %$filter) {
480
        
481
        croak qq{Column name "$column" in bind filter is not found in paramters}
482
          unless exists $params->{$column};
483
    }
484
}
485

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
486
=head1 NAME
487

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

            
490
=cut
491

            
updated document
yuki-kimoto authored on 2010-08-08
492
our $VERSION = '0.1610';
removed reconnect method
yuki-kimoto authored on 2010-05-28
493

            
494
=head1 STABILITY
495

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

            
499
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
500

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
509
    # Insert 
510
    $dbi->insert(table  => 'books',
511
                 param  => {title => 'perl', author => 'Ken'},
512
                 filter => {title => 'encode_utf8'});
513
    
514
    # Update 
515
    $dbi->update(table  => 'books', 
516
                 param  => {title => 'aaa', author => 'Ken'}, 
517
                 where  => {id => 5},
518
                 filter => {title => 'encode_utf8'});
519
    
520
    # Update all
521
    $dbi->update_all(table  => 'books',
522
                     param  => {title => 'aaa'},
523
                     filter => {title => 'encode_utf8'});
524
    
525
    # Delete
526
    $dbi->delete(table  => 'books',
527
                 where  => {author => 'Ken'},
528
                 filter => {title => 'encode_utf8'});
529
    
530
    # Delete all
531
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
532

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
535
    # Select
536
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
537
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
538
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
539
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
540
        table  => 'books',
541
        column => [qw/author title/],
542
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
543
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
544
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
545
    );
added commit method
yuki-kimoto authored on 2010-05-27
546
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
547
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
548
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
549
        table    => ['books', 'rental'],
550
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
551
        relation => {'books.id' => 'rental.book_id'}
552
    );
updated document
yuki-kimoto authored on 2010-08-08
553
    
554
    # Select, more flexible where
555
    my $result = $dbi->select(
556
        table  => 'books',
557
        where  => ['{= author} and {like title}', 
558
                   {author => 'Ken', title => '%Perl%'}]
559
    );
cleanup
yuki-kimoto authored on 2010-08-05
560

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
563
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
564
    $dbi->execute("select title from books");
565
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
566
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
567
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
568
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
569
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
570

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

            
updated document
yuki-kimoto authored on 2010-08-08
577
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
578

            
removed register_format()
yuki-kimoto authored on 2010-05-26
579
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
580
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
581
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
582

            
583
    # Get DBI object
584
    my $dbh = $dbi->dbh;
585

            
586
Fetch row.
587

            
removed register_format()
yuki-kimoto authored on 2010-05-26
588
    # Fetch
589
    while (my $row = $result->fetch) {
590
        # ...
591
    }
592
    
593
    # Fetch hash
594
    while (my $row = $result->fetch_hash) {
595
        
596
    }
597
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
598
=head1 DESCRIPTION
599

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

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

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

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

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

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

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

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

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

            
633
    use DBIx::Custom::SQLite;
634
    my $dbi = DBIx::Custom->connect(database => 'books');
635
    
updated document
yuki-kimoto authored on 2010-08-08
636
If database is  MySQL, use L<DBIx::Costom::MySQL>.
update document
yuki-kimoto authored on 2010-08-07
637

            
638
    use DBIx::Custom::MySQL;
639
    my $dbi = DBIx::Custom->connect(database => 'books',
640
                                    user => 'ken', password => '!LFKD%$&');
641

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

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

            
update document
yuki-kimoto authored on 2010-08-07
648
=head3 insert()
649

            
updated document
yuki-kimoto authored on 2010-08-08
650
Execute insert statement.
651

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

            
655
The following SQL is executed.
656

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

            
659
The values of C<title> and C<author> is embedded into placeholders.
660

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

            
664
=head3 update()
665

            
updated document
yuki-kimoto authored on 2010-08-08
666
Execute update statement.
667

            
update document
yuki-kimoto authored on 2010-08-07
668
    $dbi->update(table  => 'books', 
669
                 param  => {title => 'aaa', author => 'Ken'}, 
670
                 where  => {id => 5});
671

            
672
The following SQL is executed.
673

            
674
    update books set title = ?, author = ?;
675

            
676
The values of C<title> and C<author> is embedded into placeholders.
677

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

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

            
683
=head3 delete()
684

            
updated document
yuki-kimoto authored on 2010-08-08
685
Execute delete statement.
686

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

            
690
The following SQL is executed.
691

            
692
    delete from books where id = ?;
693

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

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

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

            
701
=head3 select()
702

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

            
705
    my $result = $dbi->select(table => 'books');
706

            
707
The following SQL is executed.
708

            
709
    select * from books;
710

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

            
714
    while (my $row = $result->fetch) {
715
        my $title  = $row->[0];
716
        my $author = $row->[1];
717
    }
718

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

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

            
724
    my $result = $dbi->select(
725
        table  => 'books',
726
        column => [qw/author title/],
727
        where  => {author => 'Ken'});
728

            
729
The following SQL is executed.
730

            
731
    select author, title from books where author = ?;
732

            
733
the value of C<author> is embdded into placeholder.
734

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

            
737
    my $result = $dbi->select(
738
        table    => ['books', 'rental'],
739
        column   => ['books.name as book_name']
740
        relation => {'books.id' => 'rental.book_id'}
741
    );
742

            
743
The following SQL is executed.
744

            
745
    select books.name as book_name from books
746
    where books.id = rental.book_id;
747

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

            
751
    # Select, more complex
752
    my $result = $dbi->select(
753
        table  => 'books',
754
        where  => {author => 'Ken'},
755
        append => 'order by price limit 5',
756
    );
757

            
758
The following SQL is executed.
759

            
760
    select * books where author = ? order by price limit 5;
761

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

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

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

            
770
Fetch row into array.
771
    
772
    while (my $row = $result->fetch) {
773
        my $author = $row->[0];
774
        my $title  = $row->[1];
775
        
776
    }
777

            
778
Fetch only a first row into array.
779

            
780
    my $row = $result->fetch_first;
781

            
782
Fetch multiple rows into array of array.
783

            
784
    while (my $rows = $result->fetch_multi(5)) {
785
        my $first_author  = $rows->[0][0];
786
        my $first_title   = $rows->[0][1];
787
        my $second_author = $rows->[1][0];
788
        my $second_value  = $rows->[1][1];
789
    
790
    }
791
    
792
Fetch all rows into array of array.
793

            
794
    my $rows = $result->fetch_all;
795

            
796
Fetch row into hash.
797

            
798
    # Fetch a row into hash
799
    while (my $row = $result->fetch_hash) {
800
        my $title  = $row->{title};
801
        my $author = $row->{author};
802
        
803
    }
804

            
805
Fetch only a first row into hash
806

            
807
    my $row = $result->fetch_hash_first;
808
    
809
Fetch multiple rows into array of hash
810

            
811
    while (my $rows = $result->fetch_hash_multi(5)) {
812
        my $first_title   = $rows->[0]{title};
813
        my $first_author  = $rows->[0]{author};
814
        my $second_title  = $rows->[1]{title};
815
        my $second_author = $rows->[1]{author};
816
    
817
    }
818
    
819
Fetch all rows into array of hash
820

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

            
updated document
yuki-kimoto authored on 2010-08-08
823
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
824

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

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

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

            
831
At frist, I show normal way of parameter binding.
832

            
833
    use DBI;
834
    my $dbh = DBI->connect(...);
835
    my $sth = $dbh->prepare(
836
        "select * from books where author = ? and title like ?;"
837
    );
838
    $sth->execute('Ken', '%Perl%');
839

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

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

            
846
    my $result = $dbi->execute(
847
        "select * from books where {= author} and {like title};"
848
        param => {author => 'Ken', title => '%Perl%'}
849
    );
850

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

            
854
    select * from books where {= author} and {like title}
855
      -> select * from books where author = ? and title like ?;
856

            
updated document
yuki-kimoto authored on 2010-08-08
857
The following tags is available.
858

            
859
=head1 Tags
860

            
861
The following tags is available.
862

            
863
    [TAG]                       [REPLACED]
864
    {? NAME}               ->   ?
865
    {= NAME}               ->   NAME = ?
866
    {<> NAME}              ->   NAME <> ?
867
    
868
    {< NAME}               ->   NAME < ?
869
    {> NAME}               ->   NAME > ?
870
    {>= NAME}              ->   NAME >= ?
871
    {<= NAME}              ->   NAME <= ?
872
    
873
    {like NAME}            ->   NAME like ?
874
    {in NAME COUNT}        ->   NAME in [?, ?, ..]
875
    
876
    {insert NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
877
    {update NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
878

            
879
See also L<DBIx::Custom::QueryBuilder>.
880

            
881
Default start tag is '{'. end tag is '}'.
882
You can change this tag.
883

            
884
    $dbi->query_builder->start_tag('|');
885
    $dbi->query_builder->end_tag('|');
update document
yuki-kimoto authored on 2010-08-07
886

            
887
=head2 5. Filtering
888

            
889
Usually, Perl string is kept as internal string.
890
If you want to save the string to database, You must encode the string.
891
Filtering system help you to convert a data to another data
892
when you save to the data and get the data form database.
893

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

            
896
    $dbi->register_filter(
897
        to_upper_case => sub {
898
            my $value = shift;
899
            return uc $value;
900
        }
901
    );
902

            
903
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
904

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

            
907
    my $result = $dbi->execute(
908
        "select * from books where {= author} and {like title};"
updated document
yuki-kimoto authored on 2010-08-08
909
        param  => {author => 'Ken', title => '%Perl%'},
update document
yuki-kimoto authored on 2010-08-07
910
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
911
    );
912

            
913
you can also specify filter in suger methods, such as select(), update(), update_all,
914
delete(), delete_all(), select().
915

            
916
    $dbi->insert(table  => 'books',
917
                 param  => {title => 'perl', author => 'Ken'},
918
                 filter => {title => 'encode_utf8'});
919

            
920
    my $result = $dbi->select(
921
        table  => 'books',
922
        column => [qw/author title/],
923
        where  => {author => 'Ken'},
924
        append => 'order by id limit 1',
925
        filter => {title => 'encode_utf8'}
926
    );
927

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

            
930
    $dbi->default_bind_filter('encode_utf8');
931

            
932
C<filter()> argument overwrites the filter specified by C<default_bind_filter()>.
933
    
934
    $dbi->default_bind_filter('encode_utf8');
935
    $dbi->insert(
936
        table  => 'books',
937
        param  => {title => 'perl', author => 'Ken', price => 1000},
938
        filter => {author => 'to_upper_case', price => undef}
939
    );
940

            
941
This is same as the following one.
942

            
943
    $dbi->insert(
944
        table  => 'books',
945
        param  => {title => 'perl', author => 'Ken', price => 1000},
946
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
947
    );
948

            
updated document
yuki-kimoto authored on 2010-08-08
949
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
950

            
951
    my $result = $dbi->select(table => 'books');
952
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
953

            
954
you can specify C<default_fetch_filter()>.
955

            
956
    $dbi->default_fetch_filter('decode_utf8');
957

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

            
961
    $dbi->default_fetch_filter('decode_utf8');
962
    my $result = $dbi->select(
963
        table => 'books',
964
        columns => ['title', 'author', 'price']
965
    );
966
    $result->filter({author => 'to_upper_case', price => undef});
967

            
968
This is same as the following one.
969

            
970
    my $result = $dbi->select(
971
        table => 'books',
972
        columns => ['title', 'author', 'price']
973
    );
974
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
975

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

            
updated document
yuki-kimoto authored on 2010-08-08
978
=head2 6. Performance
979

            
updated document
yuki-kimoto authored on 2010-08-08
980
=head3 Disable filter checking
981

            
982
C<filter_check> is 1 by defaut. This is useful in debug.
983

            
984
This filter check maybe damege performance.
985
If you require performance, set C<filter_check> to 0.
986

            
987
=head3 Using execute() method instead suger methods
988

            
updated document
yuki-kimoto authored on 2010-08-08
989
If you execute insert statement by using select() method,
990
you sometimes can't meet performance requirment.
991

            
992
C<insert()> method is a little slow because SQL statement and statement handle
993
is created every time.
994

            
995
In that case, you can prepare a query by C<create_query()> method.
996
    
997
    my $query = $dbi->create_query(
998
        "insert into books {insert title author};"
999
    );
1000
    
1001
    # (In the case of update statement)
1002
    my $query = $dbi->create_query(
1003
        "update books {update author};";
1004
    );
1005

            
1006
Execute query repeatedly
1007
    
1008
    my $inputs = [
1009
        {title => 'Perl',      author => 'Ken'},
1010
        {title => 'Good days', author => 'Mike'}
1011
    ];
1012
    
1013
    foreach my $input (@$inputs) {
1014
        $dbi->execute($query, $input);
1015
    }
1016

            
1017
This is faster than C<insert()> and C<update()> method.
1018

            
updated document
yuki-kimoto authored on 2010-08-08
1019
=head2 caching
1020

            
updated document
yuki-kimoto authored on 2010-08-08
1021
C<execute()> method cache the parsing result of SQL soruce.
1022
Default to 1
1023

            
1024
    $dbi->cache(1);
1025

            
1026
Caching is on memory, but you can change this by C<cache_method()>.
1027
First argument is L<DBIx::Custom> object.
1028
Second argument is SQL source,
1029
such as "select * from books where {=title} and {=author};";
1030
Third argument is parsed result, such as
1031
{sql => "select * from books where title = ? and author = ?",
1032
 columns => ['title', 'author']}, this is hash reference.
1033
If argument is more than two, this is called ti set cache.
1034
otherwise, called to get cache.
1035

            
1036
    $dbi->cache_mehod(sub {
1037
        sub {
1038
            my $self = shift;
1039
            
1040
            $self->{_cached} ||= {};
1041
            
1042
            # Set cache
1043
            if (@_ > 1) {
1044
                $self->{_cached}{$_[0]} = $_[1] 
1045
            }
1046
            
1047
            # Get cache
1048
            else {
1049
                return $self->{_cached}{$_[0]}
1050
            }
1051
        }
1052
    });
1053

            
1054
=head2 7. More features
1055

            
1056
=head3 Get DBI object
1057

            
1058
You can get L<DBI> object and call any method of L<DBI>.
1059

            
1060
    $dbi->dbh->begin_work;
1061
    $dbi->dbh->commit;
1062
    $dbi->dbh->rollback;
1063

            
1064
=head3 Change Result class
1065

            
1066
You can change Result class if you need.
1067

            
1068
    package Your::Result;
1069
    use base 'DBIx::Custom::Result';
1070
    
1071
    sub some_method { ... }
1072

            
1073
    1;
1074
    
1075
    package main;
1076
    
1077
    use Your::Result;
1078
    
1079
    my $dbi = DBIx::Custom->connect(...);
1080
    $dbi->result_class('Your::Result');
1081

            
1082
=head3 Custamize SQL builder object
1083

            
1084
You can custamize SQL builder object
1085

            
1086
    my $dbi = DBIx::Custom->connect(...);
1087
    $dbi->query_builder->start_tag('|');
1088
    $dbi->query_builder->end_tag('|');
1089
    $dbi->query_builder->register_tag_processor(
1090
        name => sub {
1091
           ...
1092
        }
1093
    );
1094

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1167
=head2 C<cache>
1168

            
1169
    my $cache = $dbi->cache;
1170
    $dbi      = $dbi->cache(1);
1171

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

            
1175
=head2 C<cache_method>
1176

            
1177
    $dbi          = $dbi->cache_method(\&cache_method);
1178
    $cache_method = $dbi->cache_method
1179

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

            
1182
B<Example:>
1183

            
1184
    $dbi->cache_method(
1185
        sub {
1186
            my $self = shift;
1187
            
1188
            $self->{_cached} ||= {};
1189
            
1190
            if (@_ > 1) {
1191
                $self->{_cached}{$_[0]} = $_[1] 
1192
            }
1193
            else {
1194
                return $self->{_cached}{$_[0]}
1195
            }
1196
        }
1197
    );
added commit method
yuki-kimoto authored on 2010-05-27
1198

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

            
1201
    my $filter_check = $dbi->filter_check;
1202
    $dbi             = $dbi->filter_check(0);
1203

            
1204
Enable filter check. 
1205
Default to 1.
1206
This check maybe damege performance.
1207
If you require performance, set C<filter_check> to 0.
1208

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1226
    $dbi->insert(table  => $table, 
1227
                 param  => \%param,
1228
                 append => $append,
1229
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1230

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1231
Execute insert statement.
1232
C<insert> method have C<table>, C<param>, C<append>
1233
and C<filter> arguments.
1234
C<table> is a table name.
1235
C<param> is column-value pairs. this must be hash reference.
1236
C<append> is a string added at the end of the SQL statement.
1237
C<filter> is filters when parameter binding is executed.
1238
This is overwrites C<default_bind_filter>.
1239
Return value of C<insert> is the count of affected rows.
1240

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1243
    $dbi->insert(table  => 'books', 
1244
                 param  => {title => 'Perl', author => 'Taro'},
1245
                 append => "some statement",
1246
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
1247

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1250
    $dbi->update(table  => $table, 
1251
                 param  => \%params,
1252
                 where  => \%where,
1253
                 append => $append,
1254
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
1255

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1256
Execute update statement.
1257
C<update> method have C<table>, C<param>, C<where>, C<append>
1258
and C<filter> arguments.
1259
C<table> is a table name.
1260
C<param> is column-value pairs. this must be hash reference.
1261
C<where> is where clause. this must be hash reference.
1262
C<append> is a string added at the end of the SQL statement.
1263
C<filter> is filters when parameter binding is executed.
1264
This is overwrites C<default_bind_filter>.
1265
Return value of C<update> is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
1266

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1269
    $dbi->update(table  => 'books',
1270
                 param  => {title => 'Perl', author => 'Taro'},
1271
                 where  => {id => 5},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1272
                 append => "for update",
added commit method
yuki-kimoto authored on 2010-05-27
1273
                 filter => {title => '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<update_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
1276

            
cleanup
yuki-kimoto authored on 2010-08-05
1277
    $dbi->update_all(table  => $table, 
1278
                     param  => \%params,
1279
                     filter => \%filter,
1280
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1281

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1295
    $dbi->delete(table  => $table,
1296
                 where  => \%where,
1297
                 append => $append,
1298
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1299

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1300
Execute delete statement.
1301
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1302
C<table> is a table name.
1303
C<where> is where clause. this must be hash reference.
1304
C<append> is a string added at the end of the SQL statement.
1305
C<filter> is filters when parameter binding is executed.
1306
Return value of C<delete> is the count of affected rows.
1307

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

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

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

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1328
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1329
    
cleanup
yuki-kimoto authored on 2010-08-05
1330
    my $result = $dbi->select(table    => $table,
1331
                              column   => [@column],
1332
                              where    => \%where,
1333
                              append   => $append,
1334
                              relation => \%relation,
1335
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1336

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1337
Execute select statement.
1338
C<select> method have C<table>, C<column>, C<where>, C<append>
1339
C<relation> and C<filter> arguments.
1340
C<table> is a table name.
1341
C<where> is where clause. this must be hash reference
1342
or a string containing such tags as "{= title} or {= author}".
1343
C<append> is a string added at the end of the SQL statement.
1344
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1345

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1348
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1349
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1350
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1351
    # select * from books where title = ?;
1352
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1353
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1354
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1355
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1356
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1357
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1358
        where  => {id => 1},
1359
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1360
    );
1361
    
added commit method
yuki-kimoto authored on 2010-05-27
1362
    # select books.name as book_name from books, rental 
1363
    # where books.id = rental.book_id;
1364
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1365
        table    => ['books', 'rental'],
1366
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1367
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1368
    );
1369

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1389
    my $result = $dbi->execute("select * from authors where {= name} and {= age}", 
removed reconnect method
yuki-kimoto authored on 2010-05-28
1390
                            param => {name => 'taro', age => 19});
1391
    
1392
    while (my $row = $result->fetch) {
1393
        # do something
1394
    }
1395

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

            
1398
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1399
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1400
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1401
Register filter. Registered filters is available in the following methods
1402
or arguments.
1403

            
1404
=over 4
1405

            
1406
=item *
1407

            
1408
C<default_bind_filter()>
1409

            
1410
=item *
1411

            
1412
C<default_fetch_filter()>
1413

            
1414
=item *
1415

            
1416
C<filter> argument of C<insert()>, C<update()>,
1417
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>,
1418
C<execute> method.
1419

            
1420
=item *
1421

            
1422
C<DBIx::Custom::Query::default_filter()>
1423

            
1424
=item *
1425

            
1426
C<DBIx::Csutom::Query::filter()>
1427

            
1428
=item *
1429

            
1430
C<DBIx::Custom::Result::default_filter()>
1431

            
1432
=item *
1433

            
1434
C<DBIx::Custom::Result::filter()>
1435

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1440
    $dbi->register_filter(
1441
        encode_utf8 => sub {
1442
            my $value = shift;
1443
            
1444
            require Encode;
1445
            
1446
            return Encode::encode('UTF-8', $value);
1447
        },
1448
        decode_utf8 => sub {
1449
            my $value = shift;
1450
            
1451
            require Encode;
1452
            
1453
            return Encode::decode('UTF-8', $value)
1454
        }
1455
    );
1456

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

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

            
1461
C<< <kimoto.yuki at gmail.com> >>
1462

            
1463
L<http://github.com/yuki-kimoto/DBIx-Custom>
1464

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1465
=head1 AUTHOR
1466

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

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

            
1471
Copyright 2009 Yuki Kimoto, all rights reserved.
1472

            
1473
This program is free software; you can redistribute it and/or modify it
1474
under the same terms as Perl itself.
1475

            
1476
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1477

            
1478