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

            
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
3
our $VERSION = '0.1615';
4

            
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6
use strict;
7
use warnings;
8

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
11
use Carp 'croak';
12
use DBI;
13
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
14
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
15
use DBIx::Custom::QueryBuilder;
update document
yuki-kimoto authored on 2010-05-27
16
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
17

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

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

            
cleanup
yuki-kimoto authored on 2010-04-28
29
__PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
30
__PACKAGE__->attr(query_builder  => sub {DBIx::Custom::QueryBuilder->new});
added cache_method attribute
yuki-kimoto authored on 2010-06-25
31

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

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

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

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

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

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

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

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

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

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
198
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
199
    my ($self, %args) = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
200
    
201
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
202
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
203
        croak qq{"$name" is invalid argument}
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
204
          unless $VALID_DELETE_ARGS{$name};
205
    }
206
    
207
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
208
    my $table            = $args{table} || '';
209
    my $where            = $args{where} || {};
add tests
yuki-kimoto authored on 2010-08-10
210
    my $append = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
211
    my $filter           = $args{filter};
212
    my $allow_delete_all = $args{allow_delete_all};
packaging one directory
yuki-kimoto authored on 2009-11-16
213
    
214
    # Where keys
removed register_format()
yuki-kimoto authored on 2010-05-26
215
    my @where_keys = keys %$where;
packaging one directory
yuki-kimoto authored on 2009-11-16
216
    
217
    # Not exists where keys
add tests
yuki-kimoto authored on 2010-08-10
218
    croak qq{"where" argument must be specified and } .
219
          qq{contains the pairs of column name and value}
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
220
      if !@where_keys && !$allow_delete_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
221
    
222
    # Where clause
223
    my $where_clause = '';
224
    if (@where_keys) {
225
        $where_clause = 'where ';
add tests
yuki-kimoto authored on 2010-08-10
226
        $where_clause .= "{= $_} and " for @where_keys;
packaging one directory
yuki-kimoto authored on 2009-11-16
227
        $where_clause =~ s/ and $//;
228
    }
229
    
add tests
yuki-kimoto authored on 2010-08-10
230
    # Source of SQL
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
231
    my $source = "delete from $table $where_clause";
add tests
yuki-kimoto authored on 2010-08-10
232
    $source .= " $append" if $append;
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, 
add tests
yuki-kimoto authored on 2010-08-10
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) {
add tests
yuki-kimoto authored on 2010-08-10
251
        croak qq{"$name" is invalid argument}
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
    
add tests
yuki-kimoto authored on 2010-08-10
264
    # Source of SQL
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
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
342
        my $builder = $self->query_builder;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
343
        
344
        # Create query
fixed Carp trast relationshi...
yuki-kimoto authored on 2010-08-12
345
        $query = $builder->build_query($source);
346

            
added cache_method attribute
yuki-kimoto authored on 2010-06-25
347
        # Cache query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
348
        $self->cache_method->($self, $source,
added cache_method attribute
yuki-kimoto authored on 2010-06-25
349
                             {sql     => $query->sql, 
350
                              columns => $query->columns})
351
          if $cache;
removed reconnect method
yuki-kimoto authored on 2010-05-28
352
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
353
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
354
    # Prepare statement handle
add tests
yuki-kimoto authored on 2010-08-10
355
    my $sth;
356
    eval { $sth = $self->dbh->prepare($query->{sql})};
357
    if ($@) {
358
        my $error = $@;
359
        $error =~ s/\s+at\s+.*?\s+line\s+\d+.*$//s;
360
        croak qq{$error. SQL: "$query->{sql}"};
361
    }
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
362
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
363
    # Set statement handle
364
    $query->sth($sth);
365
    
366
    return $query;
367
}
368

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
412
        return $result;
413
    }
414
    return $affected;
415
}
416

            
417
sub _build_bind_values {
418
    my ($self, $query, $params, $filter) = @_;
419
    
420
    # binding values
421
    my @bind_values;
add tests
yuki-kimoto authored on 2010-08-08
422

            
423
    # Filter
424
    $filter ||= {};
425
    
426
    # Parameter
427
    $params ||= {};
428
    
429
    # Check filter
430
    $self->_check_filter($self->filters, $filter,
431
                         $self->default_bind_filter, $params)
432
      if $self->filter_check;
removed reconnect method
yuki-kimoto authored on 2010-05-28
433
    
434
    # Build bind values
435
    my $count = {};
436
    foreach my $column (@{$query->columns}) {
437
        
438
        # Value
439
        my $value = ref $params->{$column} eq 'ARRAY'
440
                  ? $params->{$column}->[$count->{$column} || 0]
441
                  : $params->{$column};
442
        
add tests
yuki-kimoto authored on 2010-08-10
443
        # Filtering
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
444
        my $fname = $filter->{$column} || $self->default_bind_filter || '';
add tests
yuki-kimoto authored on 2010-08-10
445
        my $filter_func = $fname ? $self->filters->{$fname} : undef;
removed reconnect method
yuki-kimoto authored on 2010-05-28
446
        push @bind_values, $filter_func
447
                         ? $filter_func->($value)
448
                         : $value;
449
        
450
        # Count up 
451
        $count->{$column}++;
452
    }
453
    
454
    return \@bind_values;
455
}
456

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

            
added register_method() meth...
yuki-kimoto authored on 2010-08-10
478
__PACKAGE__->attr('methods' => sub { {} });
479

            
480
sub register_method {
481
    my $self = shift;
482
    
483
    # Register method
484
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
485
    $self->methods({%{$self->methods}, %$methods});
486
    
487
    return $self;
488
}
489

            
490
our $AUTOLOAD;
491
sub AUTOLOAD {
492
    my $self = shift;
493
    my $method = $AUTOLOAD;
494
    $method =~ s/.*:://;
495
   
496
   return if $method eq 'DESTROY';
497
   
498
   croak qq{Method "$method" is not registered"}
499
     unless $self->methods->{$method};
500
   
501
   return $self->methods->{$method}->($self, @_);
502
}
503

            
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
504
1;
505

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
506
=head1 NAME
507

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

            
510
=head1 STABILITY
511

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

            
515
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
516

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
517
Connect to the database.
518
    
519
    use DBIx::Custom;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
520
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
removed reconnect method
yuki-kimoto authored on 2010-05-28
521
                                    user => 'ken', password => '!LFKD%$&');
cleanup
yuki-kimoto authored on 2010-08-05
522

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
525
    # Insert 
526
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
527
                 param  => {title => 'Perl', author => 'Ken'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
528
                 filter => {title => 'encode_utf8'});
529
    
530
    # Update 
531
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
532
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
533
                 where  => {id => 5},
534
                 filter => {title => 'encode_utf8'});
535
    
536
    # Update all
537
    $dbi->update_all(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
538
                     param  => {title => 'Perl'},
removed reconnect method
yuki-kimoto authored on 2010-05-28
539
                     filter => {title => 'encode_utf8'});
540
    
541
    # Delete
542
    $dbi->delete(table  => 'books',
543
                 where  => {author => 'Ken'},
544
                 filter => {title => 'encode_utf8'});
545
    
546
    # Delete all
547
    $dbi->delete_all(table => 'books');
cleanup
yuki-kimoto authored on 2010-08-05
548

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
551
    # Select
552
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
553
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
554
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
555
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
556
        table  => 'books',
557
        column => [qw/author title/],
558
        where  => {author => 'Ken'},
updated document
yuki-kimoto authored on 2010-08-08
559
        append => 'order by id limit 5',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
560
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
561
    );
added commit method
yuki-kimoto authored on 2010-05-27
562
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
563
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
564
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
565
        table    => ['books', 'rental'],
566
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
567
        relation => {'books.id' => 'rental.book_id'}
568
    );
updated document
yuki-kimoto authored on 2010-08-08
569
    
570
    # Select, more flexible where
571
    my $result = $dbi->select(
572
        table  => 'books',
573
        where  => ['{= author} and {like title}', 
574
                   {author => 'Ken', title => '%Perl%'}]
575
    );
cleanup
yuki-kimoto authored on 2010-08-05
576

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
579
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
580
    $dbi->execute("select title from books");
581
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
582
    # Execute SQL with hash binding and filtering
updated document
yuki-kimoto authored on 2010-08-08
583
    $dbi->execute("select id from books where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
584
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
585
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
586

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

            
updated document
yuki-kimoto authored on 2010-08-08
593
Other features.
cleanup
yuki-kimoto authored on 2010-08-05
594

            
removed register_format()
yuki-kimoto authored on 2010-05-26
595
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
596
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
597
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
598

            
599
    # Get DBI object
600
    my $dbh = $dbi->dbh;
601

            
602
Fetch row.
603

            
removed register_format()
yuki-kimoto authored on 2010-05-26
604
    # Fetch
605
    while (my $row = $result->fetch) {
606
        # ...
607
    }
608
    
609
    # Fetch hash
610
    while (my $row = $result->fetch_hash) {
611
        
612
    }
613
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
614
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
615

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

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

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

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

            
updated document
yuki-kimoto authored on 2010-08-08
632
L<DBIx::Custom> respects SQL. SQL is very complex and not beautiful,
633
but de-facto standard,
634
so all people learing database know it.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
635
If you already know SQL,
636
you learn a little thing to use L<DBIx::Custom>.
removed reconnect method
yuki-kimoto authored on 2010-05-28
637

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
638
=head2 2. Connect to the database
removed reconnect method
yuki-kimoto authored on 2010-05-28
639

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
643
    use DBIx::Custom;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
644
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
645
                                    user => 'ken', password => '!LFKD%$&');
646

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
647
If database is SQLite, use L<DBIx::Custom::SQLite> instead.
648
you connect database easily.
update document
yuki-kimoto authored on 2010-08-07
649

            
650
    use DBIx::Custom::SQLite;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
651
    my $dbi = DBIx::Custom::SQLite->connect(database => 'dbname');
update document
yuki-kimoto authored on 2010-08-07
652
    
cleanup
yuki-kimoto authored on 2010-08-09
653
If database is  MySQL, use L<DBIx::Custom::MySQL>.
update document
yuki-kimoto authored on 2010-08-07
654

            
655
    use DBIx::Custom::MySQL;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
656
    my $dbi = DBIx::Custom::MySQL->connect(
657
        database => 'dbname',
658
        user     => 'ken',
659
        password => '!LFKD%$&'
660
    );
update document
yuki-kimoto authored on 2010-08-07
661

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
662
=head2 3. Suger methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
663

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

            
update document
yuki-kimoto authored on 2010-08-07
668
=head3 insert()
669

            
updated document
yuki-kimoto authored on 2010-08-08
670
Execute insert statement.
671

            
update document
yuki-kimoto authored on 2010-08-07
672
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
673
                 param  => {title => 'Perl', author => 'Ken'});
update document
yuki-kimoto authored on 2010-08-07
674

            
675
The following SQL is executed.
676

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
679
The values of C<title> and C<author> is embedded into the placeholders.
update document
yuki-kimoto authored on 2010-08-07
680

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
681
C<append> and C<filter> argument can be specified.
682
See also "METHODS" section.
update document
yuki-kimoto authored on 2010-08-07
683

            
684
=head3 update()
685

            
updated document
yuki-kimoto authored on 2010-08-08
686
Execute update statement.
687

            
update document
yuki-kimoto authored on 2010-08-07
688
    $dbi->update(table  => 'books', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
689
                 param  => {title => 'Perl', author => 'Ken'}, 
update document
yuki-kimoto authored on 2010-08-07
690
                 where  => {id => 5});
691

            
692
The following SQL is executed.
693

            
694
    update books set title = ?, author = ?;
695

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
696
The values of C<title> and C<author> is embedded into the placeholders.
update document
yuki-kimoto authored on 2010-08-07
697

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
698
C<append> and C<filter> argument can be specified.
699
See also "METHOD" section.
update document
yuki-kimoto authored on 2010-08-07
700

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
701
If you want to update all rows, use C<update_all()> method.
update document
yuki-kimoto authored on 2010-08-07
702

            
703
=head3 delete()
704

            
updated document
yuki-kimoto authored on 2010-08-08
705
Execute delete statement.
706

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

            
710
The following SQL is executed.
711

            
712
    delete from books where id = ?;
713

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
716
C<append> and C<filter> argument can be specified.
717
see also "METHODS" section.
update document
yuki-kimoto authored on 2010-08-07
718

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
719
If you want to delete all rows, use C<delete_all()> method.
update document
yuki-kimoto authored on 2010-08-07
720

            
721
=head3 select()
722

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

            
725
    my $result = $dbi->select(table => 'books');
726

            
727
The following SQL is executed.
728

            
729
    select * from books;
730

            
731
the result of C<select()> method is L<DBIx::Custom::Result> object.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
732
You can fetch a row by C<fetch()> method.
update document
yuki-kimoto authored on 2010-08-07
733

            
734
    while (my $row = $result->fetch) {
735
        my $title  = $row->[0];
736
        my $author = $row->[1];
737
    }
738

            
739
L<DBIx::Custom::Result> has various methods to fetch row.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
740
See "4. Fetch row".
update document
yuki-kimoto authored on 2010-08-07
741

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
742
C<column> and C<where> arguments specified.
update document
yuki-kimoto authored on 2010-08-07
743

            
744
    my $result = $dbi->select(
745
        table  => 'books',
746
        column => [qw/author title/],
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
747
        where  => {author => 'Ken'}
748
    );
update document
yuki-kimoto authored on 2010-08-07
749

            
750
The following SQL is executed.
751

            
752
    select author, title from books where author = ?;
753

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
754
the value of C<author> is embdded into the placeholder.
update document
yuki-kimoto authored on 2010-08-07
755

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

            
758
    my $result = $dbi->select(
759
        table    => ['books', 'rental'],
760
        column   => ['books.name as book_name']
761
        relation => {'books.id' => 'rental.book_id'}
762
    );
763

            
764
The following SQL is executed.
765

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
766
    select books.name as book_name from books, rental
update document
yuki-kimoto authored on 2010-08-07
767
    where books.id = rental.book_id;
768

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
769
If you want to add some string to the end of SQL statement,
770
use C<append> argument.
update document
yuki-kimoto authored on 2010-08-07
771

            
772
    my $result = $dbi->select(
773
        table  => 'books',
774
        where  => {author => 'Ken'},
775
        append => 'order by price limit 5',
776
    );
777

            
778
The following SQL is executed.
779

            
780
    select * books where author = ? order by price limit 5;
781

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
782
C<filter> argument can be specified.
783
see also "METHODS" section.
update document
yuki-kimoto authored on 2010-08-07
784

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
785
=head2 4. Fetch row
update document
yuki-kimoto authored on 2010-08-07
786

            
updated document
yuki-kimoto authored on 2010-08-08
787
C<select()> method return L<DBIx::Custom::Result> object.
788
You can fetch row by various methods.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
789
Note that in this section, array means array reference,
790
and hash meanse hash reference.
update document
yuki-kimoto authored on 2010-08-07
791

            
792
Fetch row into array.
793
    
794
    while (my $row = $result->fetch) {
795
        my $author = $row->[0];
796
        my $title  = $row->[1];
797
        
798
    }
799

            
800
Fetch only a first row into array.
801

            
802
    my $row = $result->fetch_first;
803

            
804
Fetch multiple rows into array of array.
805

            
806
    while (my $rows = $result->fetch_multi(5)) {
807
        my $first_author  = $rows->[0][0];
808
        my $first_title   = $rows->[0][1];
809
        my $second_author = $rows->[1][0];
810
        my $second_value  = $rows->[1][1];
811
    
812
    }
813
    
814
Fetch all rows into array of array.
815

            
816
    my $rows = $result->fetch_all;
817

            
818
Fetch row into hash.
819

            
820
    # Fetch a row into hash
821
    while (my $row = $result->fetch_hash) {
822
        my $title  = $row->{title};
823
        my $author = $row->{author};
824
        
825
    }
826

            
827
Fetch only a first row into hash
828

            
829
    my $row = $result->fetch_hash_first;
830
    
831
Fetch multiple rows into array of hash
832

            
833
    while (my $rows = $result->fetch_hash_multi(5)) {
834
        my $first_title   = $rows->[0]{title};
835
        my $first_author  = $rows->[0]{author};
836
        my $second_title  = $rows->[1]{title};
837
        my $second_author = $rows->[1]{author};
838
    
839
    }
840
    
841
Fetch all rows into array of hash
842

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

            
cleanup
yuki-kimoto authored on 2010-08-09
845
If you want to access statement handle of L<DBI>, use C<sth> attribute.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
846

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
849
=head2 5. Hash parameter binding
removed reconnect method
yuki-kimoto authored on 2010-05-28
850

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
853
At frist, I show normal parameter binding.
update document
yuki-kimoto authored on 2010-08-07
854

            
855
    use DBI;
856
    my $dbh = DBI->connect(...);
857
    my $sth = $dbh->prepare(
858
        "select * from books where author = ? and title like ?;"
859
    );
860
    $sth->execute('Ken', '%Perl%');
861

            
updated document
yuki-kimoto authored on 2010-08-08
862
This is very good way because database system can enable SQL caching,
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
863
and parameter is quoted automatically. this is secure.
update document
yuki-kimoto authored on 2010-08-07
864

            
updated document
yuki-kimoto authored on 2010-08-08
865
L<DBIx::Custom> hash parameter binding system improve
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
866
normal parameter binding to use hash parameter.
update document
yuki-kimoto authored on 2010-08-07
867

            
868
    my $result = $dbi->execute(
869
        "select * from books where {= author} and {like title};"
870
        param => {author => 'Ken', title => '%Perl%'}
871
    );
872

            
873
This is same as the normal way, execpt that the parameter is hash.
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
874
{= author} and {like title} is called C<tag>.
875
tag is expand to placeholder string internally.
update document
yuki-kimoto authored on 2010-08-07
876

            
877
    select * from books where {= author} and {like title}
878
      -> select * from books where author = ? and title like ?;
879

            
updated document
yuki-kimoto authored on 2010-08-08
880
The following tags is available.
881

            
882
    [TAG]                       [REPLACED]
883
    {? NAME}               ->   ?
884
    {= NAME}               ->   NAME = ?
885
    {<> NAME}              ->   NAME <> ?
886
    
887
    {< NAME}               ->   NAME < ?
888
    {> NAME}               ->   NAME > ?
889
    {>= NAME}              ->   NAME >= ?
890
    {<= NAME}              ->   NAME <= ?
891
    
892
    {like NAME}            ->   NAME like ?
893
    {in NAME COUNT}        ->   NAME in [?, ?, ..]
894
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
895
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
896
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
updated document
yuki-kimoto authored on 2010-08-08
897

            
898
See also L<DBIx::Custom::QueryBuilder>.
899

            
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
900
C<{> and C<}> is reserved. If you use these charactors,
901
you must escape them using '\'. Note that '\' is
902
already perl escaped charactor, so you must write '\\'. 
updated document
yuki-kimoto authored on 2010-08-08
903

            
removed DBIx::Custom::Query ...
yuki-kimoto authored on 2010-08-12
904
    'select * from books \\{ something statement \\}'
update document
yuki-kimoto authored on 2010-08-07
905

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
906
=head2 6. Filtering
update document
yuki-kimoto authored on 2010-08-07
907

            
908
Usually, Perl string is kept as internal string.
909
If you want to save the string to database, You must encode the string.
910
Filtering system help you to convert a data to another data
911
when you save to the data and get the data form database.
912

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

            
915
    $dbi->register_filter(
916
        to_upper_case => sub {
917
            my $value = shift;
918
            return uc $value;
919
        }
920
    );
921

            
922
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
923

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

            
926
    my $result = $dbi->execute(
927
        "select * from books where {= author} and {like title};"
updated document
yuki-kimoto authored on 2010-08-08
928
        param  => {author => 'Ken', title => '%Perl%'},
update document
yuki-kimoto authored on 2010-08-07
929
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
930
    );
931

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
932
C<filter> argument can be specified to suger methods, such as
cleanup
yuki-kimoto authored on 2010-08-09
933
C<insert()>, C<update()>, C<update_all()>,
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
934
C<delete()>, C<delete_all()>, C<select()>.
update document
yuki-kimoto authored on 2010-08-07
935

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
936
    # insert(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
937
    $dbi->insert(table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
938
                 param  => {title => 'Perl', author => 'Ken'},
update document
yuki-kimoto authored on 2010-08-07
939
                 filter => {title => 'encode_utf8'});
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
940
    
941
    # select(), having filter argument
update document
yuki-kimoto authored on 2010-08-07
942
    my $result = $dbi->select(
943
        table  => 'books',
944
        column => [qw/author title/],
945
        where  => {author => 'Ken'},
946
        append => 'order by id limit 1',
947
        filter => {title => 'encode_utf8'}
948
    );
949

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

            
952
    $dbi->default_bind_filter('encode_utf8');
953

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
954
C<filter()> argument overwrites this default filter.
update document
yuki-kimoto authored on 2010-08-07
955
    
956
    $dbi->default_bind_filter('encode_utf8');
957
    $dbi->insert(
958
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
959
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
960
        filter => {author => 'to_upper_case', price => undef}
961
    );
962

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
963
This is same as the following example.
update document
yuki-kimoto authored on 2010-08-07
964

            
965
    $dbi->insert(
966
        table  => 'books',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
967
        param  => {title => 'Perl', author => 'Ken', price => 1000},
update document
yuki-kimoto authored on 2010-08-07
968
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
969
    );
970

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

            
973
    my $result = $dbi->select(table => 'books');
974
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
975

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
976
Filter works each column value, but you prepare a default filter
977
for all clumn value.
update document
yuki-kimoto authored on 2010-08-07
978

            
979
    $dbi->default_fetch_filter('decode_utf8');
980

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
981
C<filter()> method of L<DBIx::Custom::Result>
982
overwrites this default filter.
update document
yuki-kimoto authored on 2010-08-07
983

            
984
    $dbi->default_fetch_filter('decode_utf8');
985
    my $result = $dbi->select(
986
        table => 'books',
987
        columns => ['title', 'author', 'price']
988
    );
989
    $result->filter({author => 'to_upper_case', price => undef});
990

            
991
This is same as the following one.
992

            
993
    my $result = $dbi->select(
994
        table => 'books',
995
        columns => ['title', 'author', 'price']
996
    );
997
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
998

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
999
Note that in fetch filter, column names must be lower case
1000
even if the column name conatains upper case charactors.
1001
This is requirment not to depend database systems.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1002

            
cleanup
yuki-kimoto authored on 2010-08-09
1003
=head2 7. Get high performance
updated document
yuki-kimoto authored on 2010-08-08
1004

            
updated document
yuki-kimoto authored on 2010-08-08
1005
=head3 Disable filter checking
1006

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1007
Filter checking is executed by default.
1008
This is done to check right filter name is specified,
1009
but sometimes damage performance.
updated document
yuki-kimoto authored on 2010-08-08
1010

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1011
If you disable this filter checking,
1012
Set C<filter_check> attribute to 0.
updated document
yuki-kimoto authored on 2010-08-08
1013

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1014
    $dbi->filter_check(0);
updated document
yuki-kimoto authored on 2010-08-08
1015

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1016
=head3 Use execute() method instead suger methods
1017

            
1018
If you execute insert statement by C<insert()> method,
1019
you sometimes can't get required performance.
updated document
yuki-kimoto authored on 2010-08-08
1020

            
1021
C<insert()> method is a little slow because SQL statement and statement handle
1022
is created every time.
1023

            
1024
In that case, you can prepare a query by C<create_query()> method.
1025
    
1026
    my $query = $dbi->create_query(
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1027
        "insert into books {insert_param title author};"
updated document
yuki-kimoto authored on 2010-08-08
1028
    );
cleanup
yuki-kimoto authored on 2010-08-09
1029

            
1030
Return value of C<create_query()> is L<DBIx::Custom::Query> object.
1031
This keep the information of SQL and column names.
1032

            
1033
    {
1034
        sql     => 'insert into books (title, author) values (?, ?);',
1035
        columns => ['title', 'author']
1036
    }
1037

            
1038
Execute query repeatedly.
updated document
yuki-kimoto authored on 2010-08-08
1039
    
1040
    my $inputs = [
1041
        {title => 'Perl',      author => 'Ken'},
1042
        {title => 'Good days', author => 'Mike'}
1043
    ];
1044
    
1045
    foreach my $input (@$inputs) {
1046
        $dbi->execute($query, $input);
1047
    }
1048

            
cleanup
yuki-kimoto authored on 2010-08-09
1049
This is faster than C<insert()> method.
updated document
yuki-kimoto authored on 2010-08-08
1050

            
cleanup
yuki-kimoto authored on 2010-08-09
1051
=head3 caching
updated document
yuki-kimoto authored on 2010-08-08
1052

            
cleanup
yuki-kimoto authored on 2010-08-09
1053
C<execute()> method caches the parsed result of the source of SQL.
updated document
yuki-kimoto authored on 2010-08-08
1054
Default to 1
1055

            
1056
    $dbi->cache(1);
1057

            
1058
Caching is on memory, but you can change this by C<cache_method()>.
1059
First argument is L<DBIx::Custom> object.
cleanup
yuki-kimoto authored on 2010-08-09
1060
Second argument is a source of SQL,
1061
such as "select * from books where {= title} and {= author};";
updated document
yuki-kimoto authored on 2010-08-08
1062
Third argument is parsed result, such as
1063
{sql => "select * from books where title = ? and author = ?",
1064
 columns => ['title', 'author']}, this is hash reference.
cleanup
yuki-kimoto authored on 2010-08-09
1065
If arguments is more than two, this method is called to set cache.
1066
If not, this method is called to get cache.
updated document
yuki-kimoto authored on 2010-08-08
1067

            
cleanup
yuki-kimoto authored on 2010-08-09
1068
    $dbi->cache_method(sub {
updated document
yuki-kimoto authored on 2010-08-08
1069
        sub {
1070
            my $self = shift;
1071
            
1072
            $self->{_cached} ||= {};
1073
            
1074
            # Set cache
1075
            if (@_ > 1) {
1076
                $self->{_cached}{$_[0]} = $_[1] 
1077
            }
1078
            
1079
            # Get cache
1080
            else {
1081
                return $self->{_cached}{$_[0]}
1082
            }
1083
        }
1084
    });
1085

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1086
=head2 8. More features
updated document
yuki-kimoto authored on 2010-08-08
1087

            
1088
=head3 Get DBI object
1089

            
1090
You can get L<DBI> object and call any method of L<DBI>.
1091

            
1092
    $dbi->dbh->begin_work;
1093
    $dbi->dbh->commit;
1094
    $dbi->dbh->rollback;
1095

            
1096
=head3 Change Result class
1097

            
1098
You can change Result class if you need.
1099

            
1100
    package Your::Result;
1101
    use base 'DBIx::Custom::Result';
1102
    
1103
    sub some_method { ... }
1104

            
1105
    1;
1106
    
1107
    package main;
1108
    
1109
    use Your::Result;
1110
    
1111
    my $dbi = DBIx::Custom->connect(...);
1112
    $dbi->result_class('Your::Result');
1113

            
1114
=head3 Custamize SQL builder object
1115

            
1116
You can custamize SQL builder object
1117

            
1118
    my $dbi = DBIx::Custom->connect(...);
1119
    $dbi->query_builder->register_tag_processor(
1120
        name => sub {
1121
           ...
1122
        }
1123
    );
1124

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1188
=head2 C<query_builder>
added commit method
yuki-kimoto authored on 2010-05-27
1189

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1190
    my $sql_class = $dbi->query_builder;
1191
    $dbi          = $dbi->query_builder(DBIx::Custom::QueryBuilder->new);
added commit method
yuki-kimoto authored on 2010-05-27
1192

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1193
SQL builder. C<query_builder()> must be 
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1194
the instance of L<DBIx::Custom::QueryBuilder> subclass.
1195
Default to L<DBIx::Custom::QueryBuilder> object.
cleanup
yuki-kimoto authored on 2010-08-05
1196

            
1197
=head2 C<cache>
1198

            
1199
    my $cache = $dbi->cache;
1200
    $dbi      = $dbi->cache(1);
1201

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

            
1205
=head2 C<cache_method>
1206

            
1207
    $dbi          = $dbi->cache_method(\&cache_method);
1208
    $cache_method = $dbi->cache_method
1209

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

            
1212
B<Example:>
1213

            
1214
    $dbi->cache_method(
1215
        sub {
1216
            my $self = shift;
1217
            
1218
            $self->{_cached} ||= {};
1219
            
1220
            if (@_ > 1) {
1221
                $self->{_cached}{$_[0]} = $_[1] 
1222
            }
1223
            else {
1224
                return $self->{_cached}{$_[0]}
1225
            }
1226
        }
1227
    );
added commit method
yuki-kimoto authored on 2010-05-27
1228

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

            
1231
    my $filter_check = $dbi->filter_check;
1232
    $dbi             = $dbi->filter_check(0);
1233

            
1234
Enable filter check. 
1235
Default to 1.
1236
This check maybe damege performance.
cleanup
yuki-kimoto authored on 2010-08-09
1237
If you require performance, set C<filter_check> attribute to 0.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1238

            
added register_method() meth...
yuki-kimoto authored on 2010-08-10
1239
=head2 C<(experimental) methods>
1240

            
1241
    my $methods = $dbi->methods;
1242
    $dbi        = $dbi->methods(\%methods);
1243

            
1244
Additional methods.
1245

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1256
Create a new L<DBIx::Custom> object and connect to the database.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1257
L<DBIx::Custom> is a wrapper of L<DBI>.
cleanup
yuki-kimoto authored on 2010-08-09
1258
C<AutoCommit> and C<RaiseError> options are true, 
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1259
and C<PrintError> option is false by default. 
packaging one directory
yuki-kimoto authored on 2009-11-16
1260

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1263
    $dbi->insert(table  => $table, 
1264
                 param  => \%param,
1265
                 append => $append,
1266
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1267

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1268
Execute insert statement.
1269
C<insert> method have C<table>, C<param>, C<append>
1270
and C<filter> arguments.
1271
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1272
C<param> is the pairs of column name value. this must be hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1273
C<append> is a string added at the end of the SQL statement.
1274
C<filter> is filters when parameter binding is executed.
1275
This is overwrites C<default_bind_filter>.
cleanup
yuki-kimoto authored on 2010-08-09
1276
Return value of C<insert()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1277

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1280
    $dbi->insert(table  => 'books', 
1281
                 param  => {title => 'Perl', author => 'Taro'},
1282
                 append => "some statement",
1283
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
1284

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1287
    $dbi->update(table  => $table, 
1288
                 param  => \%params,
1289
                 where  => \%where,
1290
                 append => $append,
1291
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
1292

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1293
Execute update statement.
1294
C<update> method have C<table>, C<param>, C<where>, C<append>
1295
and C<filter> arguments.
1296
C<table> is a table name.
1297
C<param> is column-value pairs. this must be hash reference.
1298
C<where> is where clause. this must be hash reference.
1299
C<append> is a string added at the end of the SQL statement.
1300
C<filter> is filters when parameter binding is executed.
1301
This is overwrites C<default_bind_filter>.
cleanup
yuki-kimoto authored on 2010-08-09
1302
Return value of C<update()> is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
1303

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1306
    $dbi->update(table  => 'books',
1307
                 param  => {title => 'Perl', author => 'Taro'},
1308
                 where  => {id => 5},
cleanup
yuki-kimoto authored on 2010-08-09
1309
                 append => "some statement",
added commit method
yuki-kimoto authored on 2010-05-27
1310
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1311

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1314
    $dbi->update_all(table  => $table, 
1315
                     param  => \%params,
1316
                     filter => \%filter,
1317
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1318

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1319
Execute update statement to update all rows.
1320
Arguments is same as C<update> method,
1321
except that C<update_all> don't have C<where> argument.
cleanup
yuki-kimoto authored on 2010-08-09
1322
Return value of C<update_all()> is the count of affected rows.
version 0.0901
yuki-kimoto authored on 2009-12-17
1323

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1332
    $dbi->delete(table  => $table,
1333
                 where  => \%where,
1334
                 append => $append,
1335
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1336

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1337
Execute delete statement.
1338
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1339
C<table> is a table name.
1340
C<where> is where clause. this must be hash reference.
1341
C<append> is a string added at the end of the SQL statement.
1342
C<filter> is filters when parameter binding is executed.
cleanup
yuki-kimoto authored on 2010-08-09
1343
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1344

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

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

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1356
Execute delete statement to delete all rows.
1357
Arguments is same as C<delete> method,
1358
except that C<delete_all> don't have C<where> argument.
cleanup
yuki-kimoto authored on 2010-08-09
1359
Return value of C<delete_all()> is the count of affected rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1360

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1365
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1366
    
cleanup
yuki-kimoto authored on 2010-08-05
1367
    my $result = $dbi->select(table    => $table,
1368
                              column   => [@column],
1369
                              where    => \%where,
1370
                              append   => $append,
1371
                              relation => \%relation,
1372
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1373

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1374
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1375
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1376
C<relation> and C<filter> arguments.
1377
C<table> is a table name.
cleanup
yuki-kimoto authored on 2010-08-09
1378
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1379
C<append> is a string added at the end of the SQL statement.
1380
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1381

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1384
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1385
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1386
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1387
    # select * from books where title = ?;
1388
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1389
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1390
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1391
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1392
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1393
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1394
        where  => {id => 1},
1395
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1396
    );
1397
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1398
    # select books.name as book_name from books, rental
added commit method
yuki-kimoto authored on 2010-05-27
1399
    # where books.id = rental.book_id;
1400
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1401
        table    => ['books', 'rental'],
1402
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1403
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1404
    );
1405

            
cleanup
yuki-kimoto authored on 2010-08-09
1406
If you use more complex condition,
1407
you can specify a array reference to C<where> argument.
1408

            
1409
    my $result = $dbi->select(
1410
        table  => 'books',
1411
        column => ['title', 'author'],
1412
        where  => ['{= title} or {like author}',
1413
                   {title => '%Perl%', author => 'Ken'}]
1414
    );
1415

            
1416
First element is a string. it contains tags,
1417
such as "{= title} or {like author}".
1418
Second element is paramters.
1419

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1420
=head2 C<create_query>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1421
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1422
    my $query = $dbi->create_query(
cleanup
yuki-kimoto authored on 2010-08-09
1423
        "select * from books where {= author} and {like title};"
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1424
    );
1425

            
cleanup
yuki-kimoto authored on 2010-08-09
1426
Create the instance of L<DBIx::Custom::Query> from the source of SQL.
1427
If you want to get high performance,
1428
use C<create_query()> method and execute it by C<execute()> method
1429
instead of suger methods.
1430

            
1431
    $dbi->execute($query, {author => 'Ken', title => '%Perl%'});
packaging one directory
yuki-kimoto authored on 2009-11-16
1432

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

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1438
Execute query or the source of SQL.
1439
Query is L<DBIx::Custom::Query> object.
1440
Return value is L<DBIx::Custom::Result> if select statement is executed,
1441
or the count of affected rows if insert, update, delete statement is executed.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1442

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1445
    my $result = $dbi->execute(
1446
        "select * from books where {= author} and {like title}", 
1447
        param => {author => 'Ken', title => '%Perl%'}
1448
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1449
    
1450
    while (my $row = $result->fetch) {
cleanup
yuki-kimoto authored on 2010-08-09
1451
        my $author = $row->[0];
1452
        my $title  = $row->[1];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1453
    }
1454

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

            
1457
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1458
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1459
    
cleanup
yuki-kimoto authored on 2010-08-09
1460
Register filter. Registered filters is available in the following attributes
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1461
or arguments.
1462

            
1463
=over 4
1464

            
1465
=item *
1466

            
cleanup
yuki-kimoto authored on 2010-08-09
1467
C<default_bind_filter>, C<default_fetch_filter>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1468

            
1469
=item *
1470

            
1471
C<filter> argument of C<insert()>, C<update()>,
cleanup
yuki-kimoto authored on 2010-08-09
1472
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1473
methods
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1474

            
1475
=item *
1476

            
cleanup
yuki-kimoto authored on 2010-08-09
1477
C<execute()> method
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1478

            
1479
=item *
1480

            
cleanup
yuki-kimoto authored on 2010-08-09
1481
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1482

            
1483
=item *
1484

            
cleanup
yuki-kimoto authored on 2010-08-09
1485
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1486

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1491
    $dbi->register_filter(
1492
        encode_utf8 => sub {
1493
            my $value = shift;
1494
            
1495
            require Encode;
1496
            
1497
            return Encode::encode('UTF-8', $value);
1498
        },
1499
        decode_utf8 => sub {
1500
            my $value = shift;
1501
            
1502
            require Encode;
1503
            
1504
            return Encode::decode('UTF-8', $value)
1505
        }
1506
    );
1507

            
added register_method() meth...
yuki-kimoto authored on 2010-08-10
1508
=head2 C<(experimental) register_method>
1509

            
1510
    $dbi->register_method(
1511
        begin_work => sub { shift->dbh->begin_work },
1512
        commit     => sub { shift->dbh->commit },
1513
        rollback   => sub { shift->dbh->rollback}
1514
    );
1515

            
1516
Register methods to the object. You can call these methods
1517
from the object.
1518

            
1519
    $dbi->begin_work;
1520
    $dbi->commit;
1521
    $dbi->rollback;
1522

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

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

            
1527
C<< <kimoto.yuki at gmail.com> >>
1528

            
1529
L<http://github.com/yuki-kimoto/DBIx-Custom>
1530

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1531
=head1 AUTHOR
1532

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

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

            
1537
Copyright 2009 Yuki Kimoto, all rights reserved.
1538

            
1539
This program is free software; you can redistribute it and/or modify it
1540
under the same terms as Perl itself.
1541

            
1542
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1543

            
1544