DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1423 lines | 37.39kb
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
400
        return $result;
401
    }
402
    return $affected;
403
}
404

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

            
454
=head1 NAME
455

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

            
458
=cut
459

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

            
462
=head1 STABILITY
463

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

            
467
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
468

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

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

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

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

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
545
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
546

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

            
551
    # Get DBI object
552
    my $dbh = $dbi->dbh;
553

            
554
Fetch row.
555

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

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

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

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

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

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
592
C<connect()> method create a new L<DBIx::Custom>
593
object and 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
    use DBIx::Custom;
596
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
597
                                    user => 'ken', password => '!LFKD%$&');
598

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

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

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

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

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

            
update document
yuki-kimoto authored on 2010-08-07
616
=head3 insert()
617

            
updated document
yuki-kimoto authored on 2010-08-08
618
Execute insert statement.
619

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

            
623
The following SQL is executed.
624

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

            
627
The values of C<title> and C<author> is embedded into placeholders.
628

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

            
632
=head3 update()
633

            
updated document
yuki-kimoto authored on 2010-08-08
634
Execute update statement.
635

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

            
640
The following SQL is executed.
641

            
642
    update books set title = ?, author = ?;
643

            
644
The values of C<title> and C<author> is embedded into placeholders.
645

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

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

            
651
=head3 delete()
652

            
updated document
yuki-kimoto authored on 2010-08-08
653
Execute delete statement.
654

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

            
658
The following SQL is executed.
659

            
660
    delete from books where id = ?;
661

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

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

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

            
669
=head3 select()
670

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

            
673
    my $result = $dbi->select(table => 'books');
674

            
675
The following SQL is executed.
676

            
677
    select * from books;
678

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

            
682
    while (my $row = $result->fetch) {
683
        my $title  = $row->[0];
684
        my $author = $row->[1];
685
    }
686

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

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

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

            
697
The following SQL is executed.
698

            
699
    select author, title from books where author = ?;
700

            
701
the value of C<author> is embdded into placeholder.
702

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

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

            
711
The following SQL is executed.
712

            
713
    select books.name as book_name from books
714
    where books.id = rental.book_id;
715

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

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

            
726
The following SQL is executed.
727

            
728
    select * books where author = ? order by price limit 5;
729

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

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

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

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

            
746
Fetch only a first row into array.
747

            
748
    my $row = $result->fetch_first;
749

            
750
Fetch multiple rows into array of array.
751

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

            
762
    my $rows = $result->fetch_all;
763

            
764
Fetch row into hash.
765

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

            
773
Fetch only a first row into hash
774

            
775
    my $row = $result->fetch_hash_first;
776
    
777
Fetch multiple rows into array of hash
778

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

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

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

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

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

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

            
799
At frist, I show normal way of parameter binding.
800

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

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

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

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

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

            
822
    select * from books where {= author} and {like title}
823
      -> select * from books where author = ? and title like ?;
824

            
updated document
yuki-kimoto authored on 2010-08-08
825
The following tags is available.
826

            
827
=head1 Tags
828

            
829
The following tags is available.
830

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

            
847
See also L<DBIx::Custom::QueryBuilder>.
848

            
849
Default start tag is '{'. end tag is '}'.
850
You can change this tag.
851

            
852
    $dbi->query_builder->start_tag('|');
853
    $dbi->query_builder->end_tag('|');
update document
yuki-kimoto authored on 2010-08-07
854

            
855
=head2 5. Filtering
856

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

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

            
864
    $dbi->register_filter(
865
        to_upper_case => sub {
866
            my $value = shift;
867
            return uc $value;
868
        }
869
    );
870

            
871
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
872

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

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

            
881
you can also specify filter in suger methods, such as select(), update(), update_all,
882
delete(), delete_all(), select().
883

            
884
    $dbi->insert(table  => 'books',
885
                 param  => {title => 'perl', author => 'Ken'},
886
                 filter => {title => 'encode_utf8'});
887

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

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

            
898
    $dbi->default_bind_filter('encode_utf8');
899

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

            
909
This is same as the following one.
910

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

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

            
919
    my $result = $dbi->select(table => 'books');
920
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
921

            
922
you can specify C<default_fetch_filter()>.
923

            
924
    $dbi->default_fetch_filter('decode_utf8');
925

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

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

            
936
This is same as the following one.
937

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

            
updated document
yuki-kimoto authored on 2010-08-08
944
=head2 6. Performance
945

            
946
If you execute insert statement by using select() method,
947
you sometimes can't meet performance requirment.
948

            
949
C<insert()> method is a little slow because SQL statement and statement handle
950
is created every time.
951

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

            
963
Execute query repeatedly
964
    
965
    my $inputs = [
966
        {title => 'Perl',      author => 'Ken'},
967
        {title => 'Good days', author => 'Mike'}
968
    ];
969
    
970
    foreach my $input (@$inputs) {
971
        $dbi->execute($query, $input);
972
    }
973

            
974
This is faster than C<insert()> and C<update()> method.
975

            
976
C<execute()> method cache the parsing result of SQL soruce.
977
Default to 1
978

            
979
    $dbi->cache(1);
980

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

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

            
1009
=head2 7. More features
1010

            
1011
=head3 Get DBI object
1012

            
1013
You can get L<DBI> object and call any method of L<DBI>.
1014

            
1015
    $dbi->dbh->begin_work;
1016
    $dbi->dbh->commit;
1017
    $dbi->dbh->rollback;
1018

            
1019
=head3 Change Result class
1020

            
1021
You can change Result class if you need.
1022

            
1023
    package Your::Result;
1024
    use base 'DBIx::Custom::Result';
1025
    
1026
    sub some_method { ... }
1027

            
1028
    1;
1029
    
1030
    package main;
1031
    
1032
    use Your::Result;
1033
    
1034
    my $dbi = DBIx::Custom->connect(...);
1035
    $dbi->result_class('Your::Result');
1036

            
1037
=head3 Custamize SQL builder object
1038

            
1039
You can custamize SQL builder object
1040

            
1041
    my $dbi = DBIx::Custom->connect(...);
1042
    $dbi->query_builder->start_tag('|');
1043
    $dbi->query_builder->end_tag('|');
1044
    $dbi->query_builder->register_tag_processor(
1045
        name => sub {
1046
           ...
1047
        }
1048
    );
1049

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1122
=head2 C<cache>
1123

            
1124
    my $cache = $dbi->cache;
1125
    $dbi      = $dbi->cache(1);
1126

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

            
1130
=head2 C<cache_method>
1131

            
1132
    $dbi          = $dbi->cache_method(\&cache_method);
1133
    $cache_method = $dbi->cache_method
1134

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

            
1137
B<Example:>
1138

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1171
    $dbi->insert(table  => $table, 
1172
                 param  => \%param,
1173
                 append => $append,
1174
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1175

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1176
Execute insert statement.
1177
C<insert> method have C<table>, C<param>, C<append>
1178
and C<filter> arguments.
1179
C<table> is a table name.
1180
C<param> is column-value pairs. this must be hash reference.
1181
C<append> is a string added at the end of the SQL statement.
1182
C<filter> is filters when parameter binding is executed.
1183
This is overwrites C<default_bind_filter>.
1184
Return value of C<insert> is the count of affected rows.
1185

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1188
    $dbi->insert(table  => 'books', 
1189
                 param  => {title => 'Perl', author => 'Taro'},
1190
                 append => "some statement",
1191
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
1192

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1195
    $dbi->update(table  => $table, 
1196
                 param  => \%params,
1197
                 where  => \%where,
1198
                 append => $append,
1199
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
1200

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1201
Execute update statement.
1202
C<update> method have C<table>, C<param>, C<where>, C<append>
1203
and C<filter> arguments.
1204
C<table> is a table name.
1205
C<param> is column-value pairs. this must be hash reference.
1206
C<where> is where clause. this must be hash reference.
1207
C<append> is a string added at the end of the SQL statement.
1208
C<filter> is filters when parameter binding is executed.
1209
This is overwrites C<default_bind_filter>.
1210
Return value of C<update> is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
1211

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1222
    $dbi->update_all(table  => $table, 
1223
                     param  => \%params,
1224
                     filter => \%filter,
1225
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1226

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1240
    $dbi->delete(table  => $table,
1241
                 where  => \%where,
1242
                 append => $append,
1243
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1244

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1245
Execute delete statement.
1246
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1247
C<table> is a table name.
1248
C<where> is where clause. this must be hash reference.
1249
C<append> is a string added at the end of the SQL statement.
1250
C<filter> is filters when parameter binding is executed.
1251
Return value of C<delete> is the count of affected rows.
1252

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

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

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

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1273
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1274
    
cleanup
yuki-kimoto authored on 2010-08-05
1275
    my $result = $dbi->select(table    => $table,
1276
                              column   => [@column],
1277
                              where    => \%where,
1278
                              append   => $append,
1279
                              relation => \%relation,
1280
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1281

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1282
Execute select statement.
1283
C<select> method have C<table>, C<column>, C<where>, C<append>
1284
C<relation> and C<filter> arguments.
1285
C<table> is a table name.
1286
C<where> is where clause. this must be hash reference
1287
or a string containing such tags as "{= title} or {= author}".
1288
C<append> is a string added at the end of the SQL statement.
1289
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1290

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1293
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1294
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1295
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1296
    # select * from books where title = ?;
1297
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1298
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1299
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1300
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1301
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1302
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1303
        where  => {id => 1},
1304
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1305
    );
1306
    
added commit method
yuki-kimoto authored on 2010-05-27
1307
    # select books.name as book_name from books, rental 
1308
    # where books.id = rental.book_id;
1309
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1310
        table    => ['books', 'rental'],
1311
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1312
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1313
    );
1314

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1334
    my $result = $dbi->execute("select * from authors where {= name} and {= age}", 
removed reconnect method
yuki-kimoto authored on 2010-05-28
1335
                            param => {name => 'taro', age => 19});
1336
    
1337
    while (my $row = $result->fetch) {
1338
        # do something
1339
    }
1340

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

            
1343
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1344
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1345
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1346
Register filter. Registered filters is available in the following methods
1347
or arguments.
1348

            
1349
=over 4
1350

            
1351
=item *
1352

            
1353
C<default_bind_filter()>
1354

            
1355
=item *
1356

            
1357
C<default_fetch_filter()>
1358

            
1359
=item *
1360

            
1361
C<filter> argument of C<insert()>, C<update()>,
1362
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>,
1363
C<execute> method.
1364

            
1365
=item *
1366

            
1367
C<DBIx::Custom::Query::default_filter()>
1368

            
1369
=item *
1370

            
1371
C<DBIx::Csutom::Query::filter()>
1372

            
1373
=item *
1374

            
1375
C<DBIx::Custom::Result::default_filter()>
1376

            
1377
=item *
1378

            
1379
C<DBIx::Custom::Result::filter()>
1380

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1385
    $dbi->register_filter(
1386
        encode_utf8 => sub {
1387
            my $value = shift;
1388
            
1389
            require Encode;
1390
            
1391
            return Encode::encode('UTF-8', $value);
1392
        },
1393
        decode_utf8 => sub {
1394
            my $value = shift;
1395
            
1396
            require Encode;
1397
            
1398
            return Encode::decode('UTF-8', $value)
1399
        }
1400
    );
1401

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

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

            
1406
C<< <kimoto.yuki at gmail.com> >>
1407

            
1408
L<http://github.com/yuki-kimoto/DBIx-Custom>
1409

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1410
=head1 AUTHOR
1411

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

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

            
1416
Copyright 2009 Yuki Kimoto, all rights reserved.
1417

            
1418
This program is free software; you can redistribute it and/or modify it
1419
under the same terms as Perl itself.
1420

            
1421
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1422

            
1423