DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
904 lines | 22.778kb
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;
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
11
use DBIx::Custom::QueryBuilder;
cleanup
yuki-kimoto authored on 2010-02-11
12
use DBIx::Custom::Query;
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
    
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
80
    # Add filter
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) {
cleanup insert
yuki-kimoto authored on 2010-04-28
94
        croak "\"$name\" is invalid name"
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
    # Not exists insert keys
108
    croak("Key-value pairs for insert must be specified to 'insert' second argument")
109
      unless @insert_keys;
110
    
111
    # Templte for insert
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
112
    my $source = "insert into $table {insert "
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
113
                   . join(' ', @insert_keys) . '}';
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
114
    $source .= " $append" if $append;
packaging one directory
yuki-kimoto authored on 2009-11-16
115
    
116
    # Execute query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
117
    my $ret_val = $self->execute($source, param  => $param, 
removed register_format()
yuki-kimoto authored on 2010-05-26
118
                                            filter => $filter);
packaging one directory
yuki-kimoto authored on 2009-11-16
119
    
120
    return $ret_val;
121
}
122

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

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

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

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

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

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

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

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

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

            
340
        # Create SQL object
341
        my $builder = $self->sql_builder;
added cache_method attribute
yuki-kimoto authored on 2010-06-25
342
        
343
        # Create query
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
344
        $query = eval{$builder->build_query($source)};
removed reconnect method
yuki-kimoto authored on 2010-05-28
345
        croak($@) if $@;
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
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
355
    my $sth = eval {$self->dbh->prepare($query->{sql})};
356
    croak $@ if $@;
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
357
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
358
    # Set statement handle
359
    $query->sth($sth);
360
    
361
    return $query;
362
}
363

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

            
366
sub execute{
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
367
    my ($self, $query, %args)  = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
368
    
369
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
370
    foreach my $name (keys %args) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
371
        croak "\"$name\" is invalid name"
372
          unless $VALID_EXECUTE_ARGS{$name};
373
    }
374
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
375
    my $params = $args{param} || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
376
    
377
    # First argument is SQL template
378
    unless (ref $query eq 'DBIx::Custom::Query') {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
379
        my $source;
removed reconnect method
yuki-kimoto authored on 2010-05-28
380
        
381
        if (ref $query eq 'ARRAY') {
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
382
            $source = $query->[0];
removed reconnect method
yuki-kimoto authored on 2010-05-28
383
        }
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
384
        else { $source = $query }
removed reconnect method
yuki-kimoto authored on 2010-05-28
385
        
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
386
        $query = $self->build_query($source);
removed reconnect method
yuki-kimoto authored on 2010-05-28
387
    }
388
    
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
389
    my $filter = $args{filter} || $query->filter || {};
removed reconnect method
yuki-kimoto authored on 2010-05-28
390
    
391
    # Create bind value
392
    my $bind_values = $self->_build_bind_values($query, $params, $filter);
393
    
394
    # Execute
395
    my $sth      = $query->sth;
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
396
    my $affected = eval {$sth->execute(@$bind_values)};
397
    croak $@ if $@;
removed reconnect method
yuki-kimoto authored on 2010-05-28
398
    
399
    # Return resultset if select statement is executed
400
    if ($sth->{NUM_OF_FIELDS}) {
401
        
402
        # Create result
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
403
        my $result = $self->result_class->new(
404
            sth            => $sth,
405
            default_filter => $self->default_fetch_filter,
406
            filters        => $self->filters
407
        );
408

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
409
        return $result;
410
    }
411
    return $affected;
412
}
413

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

            
463
=head1 NAME
464

            
465
DBIx::Custom - DBI with hash parameter binding and filtering system
466

            
467
=cut
468

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
469
our $VERSION = '0.1604';
removed reconnect method
yuki-kimoto authored on 2010-05-28
470

            
471
=head1 STABILITY
472

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
473
This module is not stable. Method name and implementations will be changed.
removed reconnect method
yuki-kimoto authored on 2010-05-28
474

            
475
=head1 SYNOPSYS
476
    
477
    # Connect
478
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=books",
479
                                    user => 'ken', password => '!LFKD%$&');
480
    # Insert 
481
    $dbi->insert(table  => 'books',
482
                 param  => {title => 'perl', author => 'Ken'},
483
                 filter => {title => 'encode_utf8'});
484
    
485
    # Update 
486
    $dbi->update(table  => 'books', 
487
                 param  => {title => 'aaa', author => 'Ken'}, 
488
                 where  => {id => 5},
489
                 filter => {title => 'encode_utf8'});
490
    
491
    # Update all
492
    $dbi->update_all(table  => 'books',
493
                     param  => {title => 'aaa'},
494
                     filter => {title => 'encode_utf8'});
495
    
496
    # Delete
497
    $dbi->delete(table  => 'books',
498
                 where  => {author => 'Ken'},
499
                 filter => {title => 'encode_utf8'});
500
    
501
    # Delete all
502
    $dbi->delete_all(table => 'books');
503
    
504
    # Select
505
    my $result = $dbi->select(table => 'books');
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
506
    
removed register_format()
yuki-kimoto authored on 2010-05-26
507
    # Select(more complex)
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
508
    my $result = $dbi->select(
update document
yuki-kimoto authored on 2010-05-27
509
        table  => 'books',
510
        column => [qw/author title/],
511
        where  => {author => 'Ken'},
512
        append => 'order by id limit 1',
513
        filter => {tilte => 'encode_utf8'}
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
514
    );
added commit method
yuki-kimoto authored on 2010-05-27
515
    
516
    # Select(Join table)
517
    my $result = $dbi->select(
518
        table => ['books', 'rental'],
519
        column => ['books.name as book_name']
520
        relation => {'books.id' => 'rental.book_id'}
521
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
522
    
removed register_format()
yuki-kimoto authored on 2010-05-26
523
    # Execute SQL
524
    $dbi->execute("select title from books");
525
    
526
    # Execute SQL with parameters and filter
527
    $dbi->execute("select id from books where {= author} && {like title}",
528
                  param  => {author => 'ken', title => '%Perl%'},
529
                  filter => {tilte => 'encode_utf8'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
530

            
531
    # Create query and execute it
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
532
    my $query = $dbi->build_query(
removed reconnect method
yuki-kimoto authored on 2010-05-28
533
        "select id from books where {= author} && {like title}"
534
    );
535
    $dbi->execute($query, param => {author => 'ken', title => '%Perl%'})
removed register_format()
yuki-kimoto authored on 2010-05-26
536
    
537
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
538
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
539
    $dbi->default_fetch_filter('decode_utf8');
540
    
541
    # Fetch
542
    while (my $row = $result->fetch) {
543
        # ...
544
    }
545
    
546
    # Fetch hash
547
    while (my $row = $result->fetch_hash) {
548
        
549
    }
550
    
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
551
    # Get DBI object
update document
yuki-kimoto authored on 2010-05-27
552
    my $dbh = $dbi->dbh;
removed reconnect method
yuki-kimoto authored on 2010-05-28
553

            
554
=head1 DESCRIPTION
555

            
556
L<DBIx::Custom> is useful L<DBI> extention.
557
This module have hash parameter binding and filtering system.
558

            
559
Normally, binding parameter is array.
560
L<DBIx::Custom> enable you to pass binding parameter as hash.
561

            
562
This module also provide filtering system.
563
You can filter the binding parameter
564
or the value of fetching row.
565

            
566
And have useful method such as insert(), update(), delete(), and select().
567

            
568
=head2 Features
569

            
570
=over 4
571

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
572
=item *
removed reconnect method
yuki-kimoto authored on 2010-05-28
573

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
574
Hash parameter binding.
removed reconnect method
yuki-kimoto authored on 2010-05-28
575

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
576
=item *
577

            
578
Value filtering.
579

            
580
=item *
581

            
582
Provide suger methods, such as insert(), update(), delete(), and select().
removed reconnect method
yuki-kimoto authored on 2010-05-28
583

            
584
=back
585

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

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

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

            
593
Database user name.
594
This is used for connect().
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
595
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
596
=head2 C<password>
packaging one directory
yuki-kimoto authored on 2009-11-16
597

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

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
601
Database password.
602
This is used for connect().
603

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

            
cleanup
yuki-kimoto authored on 2010-08-03
606
    my $data_source = $dbi->data_source;
607
    $dbi            = $dbi->data_source("dbi:mysql:dbname=$database");
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
608

            
609
Database data source.
610
This is used for connect().
611

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

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

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
617
Database handle. This is a L<DBI> object.
618
You can call all methods of L<DBI>
packaging one directory
yuki-kimoto authored on 2009-11-16
619

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
620
    my $sth    = $dbi->dbh->prepare("...");
621
    my $errstr = $dbi->dbh->errstr;
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
622
    $dbi->dbh->begin_work;
623
    $dbi->dbh->commit;
624
    $dbi->dbh->rollback;
removed reconnect method
yuki-kimoto authored on 2010-05-28
625
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
626
=head2 C<filters>
packaging one directory
yuki-kimoto authored on 2009-11-16
627

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

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
631
Filter functions.
632
By default, "encode_utf8" and "decode_utf8" is registered.
version 0.0901
yuki-kimoto authored on 2009-12-17
633

            
update document
yuki-kimoto authored on 2010-05-27
634
    $encode_utf8 = $dbi->filters->{encode_utf8};
635
    $decode_utf8 = $dbi->filters->{decode_utf8};
packaging one directory
yuki-kimoto authored on 2009-11-16
636

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

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

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
642
Default filter for value binding
packaging one directory
yuki-kimoto authored on 2009-11-16
643

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

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

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
649
Default filter for fetching.
packaging one directory
yuki-kimoto authored on 2009-11-16
650

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-03
664
SQL builder. sql_builder must be 
665
the instance of L<DBIx::Custom::QueryBuilder> subclass
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
666
Default to DBIx::Custom::QueryBuilder.
added commit method
yuki-kimoto authored on 2010-05-27
667

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
670
This class is L<Object::Simple> subclass.
671
You can use all methods of L<Object::Simple>
added commit method
yuki-kimoto authored on 2010-05-27
672

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

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

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
678
Connect to database.
update document
yuki-kimoto authored on 2010-05-27
679
"AutoCommit" and "RaiseError" option is true, 
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
680
and "PrintError" option is false by default.
packaging one directory
yuki-kimoto authored on 2009-11-16
681

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
684
    $affected = $dbi->insert(table  => $table, 
cleanup
yuki-kimoto authored on 2010-08-03
685
                             param  => \%param,
removed register_format()
yuki-kimoto authored on 2010-05-26
686
                             append => $append,
cleanup
yuki-kimoto authored on 2010-08-03
687
                             filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
688

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
689
Insert row.
690
Retrun value is the count of affected rows.
packaging one directory
yuki-kimoto authored on 2009-11-16
691
    
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
692
B<Example:>
version 0.0901
yuki-kimoto authored on 2009-12-17
693

            
removed register_format()
yuki-kimoto authored on 2010-05-26
694
    $dbi->insert(table  => 'books', 
695
                 param  => {title => 'Perl', author => 'Taro'},
696
                 append => "some statement",
697
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
698

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
701
    $affected = $dbi->update(table  => $table, 
cleanup
yuki-kimoto authored on 2010-08-03
702
                             param  => \%params,
703
                             where  => \%where,
removed register_format()
yuki-kimoto authored on 2010-05-26
704
                             append => $append,
cleanup
yuki-kimoto authored on 2010-08-03
705
                             filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
706

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
707
Update rows.
708
Retrun value is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
709

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
712
    $dbi->update(table  => 'books',
713
                 param  => {title => 'Perl', author => 'Taro'},
714
                 where  => {id => 5},
715
                 append => "some statement",
added commit method
yuki-kimoto authored on 2010-05-27
716
                 filter => {title => 'encode_utf8'});
version 0.0901
yuki-kimoto authored on 2009-12-17
717

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

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

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
725
Update all rows.
726
Retrun value is the count of affected rows.
version 0.0901
yuki-kimoto authored on 2009-12-17
727

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

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
736
    $affected = $dbi->delete(table  => $table,
cleanup
yuki-kimoto authored on 2010-08-03
737
                             where  => \%where,
added commit method
yuki-kimoto authored on 2010-05-27
738
                             append => $append,
cleanup
yuki-kimoto authored on 2010-08-03
739
                             filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
740

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
741
Delete rows.
742
Retrun value is the count of affected rows.
packaging one directory
yuki-kimoto authored on 2009-11-16
743
    
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
744
B<Example:>
packaging one directory
yuki-kimoto authored on 2009-11-16
745

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

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
753
    $affected = $dbi->delete_all(table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
754

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
755
Delete all rows.
756
Retrun value is the count of affected rows.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
757

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
762
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
763
    
added commit method
yuki-kimoto authored on 2010-05-27
764
    $result = $dbi->select(table    => $table,
765
                           column   => [@column],
cleanup
yuki-kimoto authored on 2010-08-03
766
                           where    => \%where,
added commit method
yuki-kimoto authored on 2010-05-27
767
                           append   => $append,
cleanup
yuki-kimoto authored on 2010-08-03
768
                           relation => \%relation,
769
                           filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
770

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
771
Select rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
772
Return value is the instance of L<DBIx::Custom::Result>.
update document
yuki-kimoto authored on 2009-11-19
773

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

            
added commit method
yuki-kimoto authored on 2010-05-27
776
    # select * from books;
removed reconnect method
yuki-kimoto authored on 2010-05-28
777
    $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
778
    
update document
yuki-kimoto authored on 2009-11-19
779
    # select * from books where title = 'Perl';
removed reconnect method
yuki-kimoto authored on 2010-05-28
780
    $result = $dbi->select(table => 'books', where => {title => 1});
update document
yuki-kimoto authored on 2009-11-19
781
    
782
    # select title, author from books where id = 1 for update;
783
    $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
784
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
785
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
786
        where  => {id => 1},
787
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
788
    );
789
    
added commit method
yuki-kimoto authored on 2010-05-27
790
    # select books.name as book_name from books, rental 
791
    # where books.id = rental.book_id;
792
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
793
        table    => ['books', 'rental'],
794
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
795
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
796
    );
797

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
798
=head2 C<build_query>
removed reconnect method
yuki-kimoto authored on 2010-05-28
799
    
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
800
    my $query = $dbi->build_query(
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
801
        "select * from authors where {= name} and {= age};"
802
    );
803

            
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
804
Build the instance of L<DBIx::Custom::Query>
805
using L<DBIx::Custom::QueryBuilder>.
packaging one directory
yuki-kimoto authored on 2009-11-16
806

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

            
cleanup
yuki-kimoto authored on 2010-08-03
809
    $result = $dbi->execute($query,  param => $params, filter => \%filter);
810
    $result = $dbi->execute($source, param => $params, filter => \%filter);
removed reconnect method
yuki-kimoto authored on 2010-05-28
811

            
812
Execute the instace of L<DBIx::Custom::Query> or
813
the string written by SQL template.
814
Return value is the instance of L<DBIx::Custom::Result>.
815

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

            
818
    $result = $dbi->execute("select * from authors where {= name} and {= age}", 
819
                            param => {name => 'taro', age => 19});
820
    
821
    while (my $row = $result->fetch) {
822
        # do something
823
    }
824

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

            
827
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
828
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
829
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
830
Resister filter.
831

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
834
    $dbi->register_filter(
835
        encode_utf8 => sub {
836
            my $value = shift;
837
            
838
            require Encode;
839
            
840
            return Encode::encode('UTF-8', $value);
841
        },
842
        decode_utf8 => sub {
843
            my $value = shift;
844
            
845
            require Encode;
846
            
847
            return Encode::decode('UTF-8', $value)
848
        }
849
    );
850

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
851
=head2 C<cache>
added cache_method attribute
yuki-kimoto authored on 2010-06-25
852

            
853
    $dbi   = $dbi->cache(1);
854
    $cache = $dbi->cache;
855

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
856
Cache the result of parsing SQL template.
added cache_method attribute
yuki-kimoto authored on 2010-06-25
857
Default to 1.
858

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
859
=head2 C<cache_method>
860

            
861
    $dbi          = $dbi->cache_method(\&cache_method);
862
    $cache_method = $dbi->cache_method
added cache_method attribute
yuki-kimoto authored on 2010-06-25
863

            
864
Method for cache.
865

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
866
B<Example:>
added cache_method attribute
yuki-kimoto authored on 2010-06-25
867

            
868
    $dbi->cache_method(
869
        sub {
870
            my $self = shift;
871
            
872
            $self->{_cached} ||= {};
873
            
874
            if (@_ > 1) {
875
                $self->{_cached}{$_[0]} = $_[1] 
876
            }
877
            else {
878
                return $self->{_cached}{$_[0]}
879
            }
880
        }
881
    );
882

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

            
885
Please tell me bugs.
886

            
887
C<< <kimoto.yuki at gmail.com> >>
888

            
889
L<http://github.com/yuki-kimoto/DBIx-Custom>
890

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
891
=head1 AUTHOR
892

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

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

            
897
Copyright 2009 Yuki Kimoto, all rights reserved.
898

            
899
This program is free software; you can redistribute it and/or modify it
900
under the same terms as Perl itself.
901

            
902
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
903

            
904