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

            
3
use strict;
4
use warnings;
5

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
393
        return $result;
394
    }
395
    return $affected;
396
}
397

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

            
447
=head1 NAME
448

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

            
451
=cut
452

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
453
our $VERSION = '0.1606';
removed reconnect method
yuki-kimoto authored on 2010-05-28
454

            
455
=head1 STABILITY
456

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

            
460
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
461

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
496
    # Select
497
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
498
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
499
    # Select, more complex
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
500
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
501
        table  => 'books',
502
        column => [qw/author title/],
503
        where  => {author => 'Ken'},
504
        append => 'order by id limit 1',
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
505
        filter => {title => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
506
    );
added commit method
yuki-kimoto authored on 2010-05-27
507
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
508
    # Select, join table
added commit method
yuki-kimoto authored on 2010-05-27
509
    my $result = $dbi->select(
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
510
        table    => ['books', 'rental'],
511
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
512
        relation => {'books.id' => 'rental.book_id'}
513
    );
cleanup
yuki-kimoto authored on 2010-08-05
514

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
517
    # Execute SQL
removed register_format()
yuki-kimoto authored on 2010-05-26
518
    $dbi->execute("select title from books");
519
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
520
    # Execute SQL with hash binding and filtering
removed register_format()
yuki-kimoto authored on 2010-05-26
521
    $dbi->execute("select id from books where {= author} && {like title}",
522
                  param  => {author => 'ken', title => '%Perl%'},
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
523
                  filter => {title => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
524

            
525
    # Create query and execute it
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
526
    my $query = $dbi->create_query(
removed reconnect method
yuki-kimoto authored on 2010-05-28
527
        "select id from books where {= author} && {like title}"
528
    );
529
    $dbi->execute($query, param => {author => 'ken', title => '%Perl%'})
cleanup
yuki-kimoto authored on 2010-08-05
530

            
531
More features.
532

            
removed register_format()
yuki-kimoto authored on 2010-05-26
533
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
534
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
535
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
536

            
537
    # Get DBI object
538
    my $dbh = $dbi->dbh;
539

            
540
Fetch row.
541

            
removed register_format()
yuki-kimoto authored on 2010-05-26
542
    # Fetch
543
    while (my $row = $result->fetch) {
544
        # ...
545
    }
546
    
547
    # Fetch hash
548
    while (my $row = $result->fetch_hash) {
549
        
550
    }
551
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
552

            
553
=head1 DESCRIPTION
554

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
560
This module is not O/R mapper. O/R mapper is useful,
561
but you must learn many syntax of the O/R mapper,
562
which is almost another language
563
Create SQL statement is offten not effcient and damage SQL performance.
564
so you have to execute raw SQL in the end.
removed reconnect method
yuki-kimoto authored on 2010-05-28
565

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
566
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper.
567
L<DBIx::Custom> provide flexible hash parameter binding adn filtering system,
568
and suger method, such as C<select()>, C<update()>, C<delete()>, C<select()>
569
to execute a query easily.
removed reconnect method
yuki-kimoto authored on 2010-05-28
570

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
571
L<DBIx::Custom> respects SQL. SQL is not beautiful, but de-facto standard,
572
so all people learing database system know it.
573
If you know SQL statement,
574
you learn a little thing about L<DBIx::Custom> to do your works.
removed reconnect method
yuki-kimoto authored on 2010-05-28
575

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
576
=head2 2. Basic usage
removed reconnect method
yuki-kimoto authored on 2010-05-28
577

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
578
=head3 connect to the database
removed reconnect method
yuki-kimoto authored on 2010-05-28
579

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

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

            
587
=head3 Suger methods
588

            
589
L<DBIx::Custom> has suger methods, such as C<insert()>, C<update()>,
590
C<delete()> and C<select()>. If you want to do simple works,
591
You don't have to create SQL statement.
592

            
593
B<Execute insert statement:>
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
594

            
595

            
596

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
597

            
598

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
671
=head2 C<cache>
672

            
673
    my $cache = $dbi->cache;
674
    $dbi      = $dbi->cache(1);
675

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

            
679
=head2 C<cache_method>
680

            
681
    $dbi          = $dbi->cache_method(\&cache_method);
682
    $cache_method = $dbi->cache_method
683

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

            
686
B<Example:>
687

            
688
    $dbi->cache_method(
689
        sub {
690
            my $self = shift;
691
            
692
            $self->{_cached} ||= {};
693
            
694
            if (@_ > 1) {
695
                $self->{_cached}{$_[0]} = $_[1] 
696
            }
697
            else {
698
                return $self->{_cached}{$_[0]}
699
            }
700
        }
701
    );
added commit method
yuki-kimoto authored on 2010-05-27
702

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
720
    $dbi->insert(table  => $table, 
721
                 param  => \%param,
722
                 append => $append,
723
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
724

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
725
Execute insert statement.
726
C<insert> method have C<table>, C<param>, C<append>
727
and C<filter> arguments.
728
C<table> is a table name.
729
C<param> is column-value pairs. this must be hash reference.
730
C<append> is a string added at the end of the SQL statement.
731
C<filter> is filters when parameter binding is executed.
732
This is overwrites C<default_bind_filter>.
733
Return value of C<insert> is the count of affected rows.
734

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
737
    $dbi->insert(table  => 'books', 
738
                 param  => {title => 'Perl', author => 'Taro'},
739
                 append => "some statement",
740
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
741

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

            
cleanup
yuki-kimoto authored on 2010-08-05
744
    $dbi->update(table  => $table, 
745
                 param  => \%params,
746
                 where  => \%where,
747
                 append => $append,
748
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
749

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
750
Execute update statement.
751
C<update> method have C<table>, C<param>, C<where>, C<append>
752
and C<filter> arguments.
753
C<table> is a table name.
754
C<param> is column-value pairs. this must be hash reference.
755
C<where> is where clause. this must be hash reference.
756
C<append> is a string added at the end of the SQL statement.
757
C<filter> is filters when parameter binding is executed.
758
This is overwrites C<default_bind_filter>.
759
Return value of C<update> is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
760

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
771
    $dbi->update_all(table  => $table, 
772
                     param  => \%params,
773
                     filter => \%filter,
774
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
775

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
789
    $dbi->delete(table  => $table,
790
                 where  => \%where,
791
                 append => $append,
792
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
793

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
794
Execute delete statement.
795
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
796
C<table> is a table name.
797
C<where> is where clause. this must be hash reference.
798
C<append> is a string added at the end of the SQL statement.
799
C<filter> is filters when parameter binding is executed.
800
Return value of C<delete> is the count of affected rows.
801

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

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

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

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
822
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
823
    
cleanup
yuki-kimoto authored on 2010-08-05
824
    my $result = $dbi->select(table    => $table,
825
                              column   => [@column],
826
                              where    => \%where,
827
                              append   => $append,
828
                              relation => \%relation,
829
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
830

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
831
Execute select statement.
832
C<select> method have C<table>, C<column>, C<where>, C<append>
833
C<relation> and C<filter> arguments.
834
C<table> is a table name.
835
C<where> is where clause. this must be hash reference
836
or a string containing such tags as "{= title} or {= author}".
837
C<append> is a string added at the end of the SQL statement.
838
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
839

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

            
added commit method
yuki-kimoto authored on 2010-05-27
842
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
843
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
844
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
845
    # select * from books where title = ?;
846
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
847
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
848
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
849
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
850
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
851
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
852
        where  => {id => 1},
853
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
854
    );
855
    
added commit method
yuki-kimoto authored on 2010-05-27
856
    # select books.name as book_name from books, rental 
857
    # where books.id = rental.book_id;
858
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
859
        table    => ['books', 'rental'],
860
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
861
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
862
    );
863

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
883
    my $result = $dbi->execute("select * from authors where {= name} and {= age}", 
removed reconnect method
yuki-kimoto authored on 2010-05-28
884
                            param => {name => 'taro', age => 19});
885
    
886
    while (my $row = $result->fetch) {
887
        # do something
888
    }
889

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

            
892
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
893
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
894
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
895
Register filter. Registered filters is available in the following methods
896
or arguments.
897

            
898
=over 4
899

            
900
=item *
901

            
902
C<default_bind_filter()>
903

            
904
=item *
905

            
906
C<default_fetch_filter()>
907

            
908
=item *
909

            
910
C<filter> argument of C<insert()>, C<update()>,
911
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>,
912
C<execute> method.
913

            
914
=item *
915

            
916
C<DBIx::Custom::Query::default_filter()>
917

            
918
=item *
919

            
920
C<DBIx::Csutom::Query::filter()>
921

            
922
=item *
923

            
924
C<DBIx::Custom::Result::default_filter()>
925

            
926
=item *
927

            
928
C<DBIx::Custom::Result::filter()>
929

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
934
    $dbi->register_filter(
935
        encode_utf8 => sub {
936
            my $value = shift;
937
            
938
            require Encode;
939
            
940
            return Encode::encode('UTF-8', $value);
941
        },
942
        decode_utf8 => sub {
943
            my $value = shift;
944
            
945
            require Encode;
946
            
947
            return Encode::decode('UTF-8', $value)
948
        }
949
    );
950

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

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

            
955
C<< <kimoto.yuki at gmail.com> >>
956

            
957
L<http://github.com/yuki-kimoto/DBIx-Custom>
958

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
959
=head1 AUTHOR
960

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

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

            
965
Copyright 2009 Yuki Kimoto, all rights reserved.
966

            
967
This program is free software; you can redistribute it and/or modify it
968
under the same terms as Perl itself.
969

            
970
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
971

            
972