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

            
3
use strict;
4
use warnings;
5

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
454
=head1 NAME
455

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

            
458
=cut
459

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

            
462
=head1 STABILITY
463

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

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

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

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

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

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

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

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

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

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

            
538
More features.
539

            
removed register_format()
yuki-kimoto authored on 2010-05-26
540
    # Default filter
renamed default_query_filter...
yuki-kimoto authored on 2010-08-03
541
    $dbi->default_bind_filter('encode_utf8');
removed register_format()
yuki-kimoto authored on 2010-05-26
542
    $dbi->default_fetch_filter('decode_utf8');
cleanup
yuki-kimoto authored on 2010-08-05
543

            
544
    # Get DBI object
545
    my $dbh = $dbi->dbh;
546

            
547
Fetch row.
548

            
removed register_format()
yuki-kimoto authored on 2010-05-26
549
    # Fetch
550
    while (my $row = $result->fetch) {
551
        # ...
552
    }
553
    
554
    # Fetch hash
555
    while (my $row = $result->fetch_hash) {
556
        
557
    }
558
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
559

            
560
=head1 DESCRIPTION
561

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

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

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

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

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

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

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

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

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

            
594
    use DBIx::Custom::SQLite;
595
    my $dbi = DBIx::Custom->connect(database => 'books');
596
    
597
If database is  MySQL, use L<DBIx::Costm::MySQL>.
598

            
599
    use DBIx::Custom::MySQL;
600
    my $dbi = DBIx::Custom->connect(database => 'books',
601
                                    user => 'ken', password => '!LFKD%$&');
602

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

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

            
update document
yuki-kimoto authored on 2010-08-07
609
=head3 insert()
610

            
611
    $dbi->insert(table  => 'books',
612
                 param  => {title => 'perl', author => 'Ken'});
613

            
614
The following SQL is executed.
615

            
616
    insert into (title, author) values (?, ?)
617

            
618
The values of C<title> and C<author> is embedded into placeholders.
619

            
620
C<append> and C<filter> argument can be specified if you need.
621

            
622
=head3 update()
623

            
624
    $dbi->update(table  => 'books', 
625
                 param  => {title => 'aaa', author => 'Ken'}, 
626
                 where  => {id => 5});
627

            
628
The following SQL is executed.
629

            
630
    update books set title = ?, author = ?;
631

            
632
The values of C<title> and C<author> is embedded into placeholders.
633

            
634
If you want to update all rows, use C<update_all()> method instead.
635

            
636
C<append> and C<filter> argument can be specified if you need.
637

            
638
=head3 delete()
639

            
640
    $dbi->delete(table  => 'books',
641
                 where  => {author => 'Ken'});
642

            
643
The following SQL is executed.
644

            
645
    delete from books where id = ?;
646

            
647
The value of C<id> is embedded into a placehodler.
648

            
649
C<append> and C<filter> argument can be specified if you need.
650

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

            
653
=head3 select()
654

            
655
Specify only table:
656

            
657
    my $result = $dbi->select(table => 'books');
658

            
659
The following SQL is executed.
660

            
661
    select * from books;
662

            
663
the result of C<select()> method is L<DBIx::Custom::Result> object.
664
use C<fetch()> method to fetch a row.
665

            
666
    while (my $row = $result->fetch) {
667
        my $title  = $row->[0];
668
        my $author = $row->[1];
669
    }
670

            
671
L<DBIx::Custom::Result> has various methods to fetch row.
672
See "3. Result of select statement".
673

            
674
Specify C<column> and C<where> arguments:
675

            
676
    my $result = $dbi->select(
677
        table  => 'books',
678
        column => [qw/author title/],
679
        where  => {author => 'Ken'});
680

            
681
The following SQL is executed.
682

            
683
    select author, title from books where author = ?;
684

            
685
the value of C<author> is embdded into placeholder.
686

            
687
If C<relation> argument is specifed, you can join tables.
688

            
689
    my $result = $dbi->select(
690
        table    => ['books', 'rental'],
691
        column   => ['books.name as book_name']
692
        relation => {'books.id' => 'rental.book_id'}
693
    );
694

            
695
The following SQL is executed.
696

            
697
    select books.name as book_name from books
698
    where books.id = rental.book_id;
699

            
700
C<append> argument add a string to the end of SQL statement.
701
It is useful to add "order by" or "limit" cluase.
702

            
703
    # Select, more complex
704
    my $result = $dbi->select(
705
        table  => 'books',
706
        where  => {author => 'Ken'},
707
        append => 'order by price limit 5',
708
    );
709

            
710
The following SQL is executed.
711

            
712
    select * books where author = ? order by price limit 5;
713

            
714
C<filter> argument can be specified if you need.
715

            
716
=head2 3. Result of select statement
717

            
718
C<select> method reurn L<DBIx::Custom::Result> object.
719
Using various methods, you can fetch row.
720

            
721
Fetch row into array.
722
    
723
    while (my $row = $result->fetch) {
724
        my $author = $row->[0];
725
        my $title  = $row->[1];
726
        
727
    }
728

            
729
Fetch only a first row into array.
730

            
731
    my $row = $result->fetch_first;
732

            
733
Fetch multiple rows into array of array.
734

            
735
    while (my $rows = $result->fetch_multi(5)) {
736
        my $first_author  = $rows->[0][0];
737
        my $first_title   = $rows->[0][1];
738
        my $second_author = $rows->[1][0];
739
        my $second_value  = $rows->[1][1];
740
    
741
    }
742
    
743
Fetch all rows into array of array.
744

            
745
    my $rows = $result->fetch_all;
746

            
747
Fetch row into hash.
748

            
749
    # Fetch a row into hash
750
    while (my $row = $result->fetch_hash) {
751
        my $title  = $row->{title};
752
        my $author = $row->{author};
753
        
754
    }
755

            
756
Fetch only a first row into hash
757

            
758
    my $row = $result->fetch_hash_first;
759
    
760
Fetch multiple rows into array of hash
761

            
762
    while (my $rows = $result->fetch_hash_multi(5)) {
763
        my $first_title   = $rows->[0]{title};
764
        my $first_author  = $rows->[0]{author};
765
        my $second_title  = $rows->[1]{title};
766
        my $second_author = $rows->[1]{author};
767
    
768
    }
769
    
770
Fetch all rows into array of hash
771

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

            
update document
yuki-kimoto authored on 2010-08-07
774
If you want to access row statement handle of L<DBI>, use sth() attribute.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
775

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

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

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

            
782
At frist, I show normal way of parameter binding.
783

            
784
    use DBI;
785
    my $dbh = DBI->connect(...);
786
    my $sth = $dbh->prepare(
787
        "select * from books where author = ? and title like ?;"
788
    );
789
    $sth->execute('Ken', '%Perl%');
790

            
791
This is very good way because database system enable SQL caching,
792
and parameter is quoted automatically.
793

            
794
L<DBIx::Custom>hash parameter binding system improve normal parameter binding to
795
specify hash parameter.
796

            
797
    my $result = $dbi->execute(
798
        "select * from books where {= author} and {like title};"
799
        param => {author => 'Ken', title => '%Perl%'}
800
    );
801

            
802
This is same as the normal way, execpt that the parameter is hash.
803
{= author} is called C<tag>. tag is expand to placeholder internally.
804

            
805
    select * from books where {= author} and {like title}
806
      -> select * from books where author = ? and title like ?;
807

            
808
See L<DBIx::Custom::QueryBuilder> to know all tags.
809

            
810
=head2 5. Filtering
811

            
812
Usually, Perl string is kept as internal string.
813
If you want to save the string to database, You must encode the string.
814
Filtering system help you to convert a data to another data
815
when you save to the data and get the data form database.
816

            
817
If you want to register filter, use register_filter() method.
818

            
819
    $dbi->register_filter(
820
        to_upper_case => sub {
821
            my $value = shift;
822
            return uc $value;
823
        }
824
    );
825

            
826
C<encode_utf8> and C<decode_utf8> filter is registerd by default.
827

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

            
830
    my $result = $dbi->execute(
831
        "select * from books where {= author} and {like title};"
832
        param => {author => 'Ken', title => '%Perl%'});
833
        filter => {author => 'to_upper_case, title => 'encode_utf8'}
834
    );
835

            
836
you can also specify filter in suger methods, such as select(), update(), update_all,
837
delete(), delete_all(), select().
838

            
839
    $dbi->insert(table  => 'books',
840
                 param  => {title => 'perl', author => 'Ken'},
841
                 filter => {title => 'encode_utf8'});
842

            
843
    my $result = $dbi->select(
844
        table  => 'books',
845
        column => [qw/author title/],
846
        where  => {author => 'Ken'},
847
        append => 'order by id limit 1',
848
        filter => {title => 'encode_utf8'}
849
    );
850

            
851
Filter work to each parmeter, but you prepare default filter for all parameters.
852
you can use C<default_bind_filter()> attribute.
853

            
854
    $dbi->default_bind_filter('encode_utf8');
855

            
856
C<filter()> argument overwrites the filter specified by C<default_bind_filter()>.
857
    
858
    $dbi->default_bind_filter('encode_utf8');
859
    $dbi->insert(
860
        table  => 'books',
861
        param  => {title => 'perl', author => 'Ken', price => 1000},
862
        filter => {author => 'to_upper_case', price => undef}
863
    );
864

            
865
This is same as the following one.
866

            
867
    $dbi->insert(
868
        table  => 'books',
869
        param  => {title => 'perl', author => 'Ken', price => 1000},
870
        filter => {title => 'encode_uft8' author => 'to_upper_case'}
871
    );
872

            
873
You can also specify filter when the row is fetching. This is reverse of bindig filter.
874

            
875
    my $result = $dbi->select(table => 'books');
876
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
877

            
878
you can specify C<default_fetch_filter()>.
879

            
880
    $dbi->default_fetch_filter('decode_utf8');
881

            
882
C<DBIx::Custom::Result::filter> overwrites the filter specified
883
by C<default_fetch_filter()>
884

            
885
    $dbi->default_fetch_filter('decode_utf8');
886
    my $result = $dbi->select(
887
        table => 'books',
888
        columns => ['title', 'author', 'price']
889
    );
890
    $result->filter({author => 'to_upper_case', price => undef});
891

            
892
This is same as the following one.
893

            
894
    my $result = $dbi->select(
895
        table => 'books',
896
        columns => ['title', 'author', 'price']
897
    );
898
    $result->filter({title => 'decode_utf8', author => 'to_upper_case'});
removed reconnect method
yuki-kimoto authored on 2010-05-28
899

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
972
=head2 C<cache>
973

            
974
    my $cache = $dbi->cache;
975
    $dbi      = $dbi->cache(1);
976

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

            
980
=head2 C<cache_method>
981

            
982
    $dbi          = $dbi->cache_method(\&cache_method);
983
    $cache_method = $dbi->cache_method
984

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

            
987
B<Example:>
988

            
989
    $dbi->cache_method(
990
        sub {
991
            my $self = shift;
992
            
993
            $self->{_cached} ||= {};
994
            
995
            if (@_ > 1) {
996
                $self->{_cached}{$_[0]} = $_[1] 
997
            }
998
            else {
999
                return $self->{_cached}{$_[0]}
1000
            }
1001
        }
1002
    );
added commit method
yuki-kimoto authored on 2010-05-27
1003

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1021
    $dbi->insert(table  => $table, 
1022
                 param  => \%param,
1023
                 append => $append,
1024
                 filter => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1025

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1026
Execute insert statement.
1027
C<insert> method have C<table>, C<param>, C<append>
1028
and C<filter> arguments.
1029
C<table> is a table name.
1030
C<param> is column-value pairs. this must be hash reference.
1031
C<append> is a string added at the end of the SQL statement.
1032
C<filter> is filters when parameter binding is executed.
1033
This is overwrites C<default_bind_filter>.
1034
Return value of C<insert> is the count of affected rows.
1035

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

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1038
    $dbi->insert(table  => 'books', 
1039
                 param  => {title => 'Perl', author => 'Taro'},
1040
                 append => "some statement",
1041
                 filter => {title => 'encode_utf8'})
version 0.0901
yuki-kimoto authored on 2009-12-17
1042

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1045
    $dbi->update(table  => $table, 
1046
                 param  => \%params,
1047
                 where  => \%where,
1048
                 append => $append,
1049
                 filter => \%filter)
version 0.0901
yuki-kimoto authored on 2009-12-17
1050

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1051
Execute update statement.
1052
C<update> method have C<table>, C<param>, C<where>, C<append>
1053
and C<filter> arguments.
1054
C<table> is a table name.
1055
C<param> is column-value pairs. this must be hash reference.
1056
C<where> is where clause. this must be hash reference.
1057
C<append> is a string added at the end of the SQL statement.
1058
C<filter> is filters when parameter binding is executed.
1059
This is overwrites C<default_bind_filter>.
1060
Return value of C<update> is the count of affected rows.
update document
yuki-kimoto authored on 2009-11-19
1061

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1072
    $dbi->update_all(table  => $table, 
1073
                     param  => \%params,
1074
                     filter => \%filter,
1075
                     append => $append);
update document
yuki-kimoto authored on 2009-11-19
1076

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1090
    $dbi->delete(table  => $table,
1091
                 where  => \%where,
1092
                 append => $append,
1093
                 filter => \%filter);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1094

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1095
Execute delete statement.
1096
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1097
C<table> is a table name.
1098
C<where> is where clause. this must be hash reference.
1099
C<append> is a string added at the end of the SQL statement.
1100
C<filter> is filters when parameter binding is executed.
1101
Return value of C<delete> is the count of affected rows.
1102

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

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

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

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

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

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1123
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1124
    
cleanup
yuki-kimoto authored on 2010-08-05
1125
    my $result = $dbi->select(table    => $table,
1126
                              column   => [@column],
1127
                              where    => \%where,
1128
                              append   => $append,
1129
                              relation => \%relation,
1130
                              filter   => \%filter);
update document
yuki-kimoto authored on 2009-11-19
1131

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1132
Execute select statement.
1133
C<select> method have C<table>, C<column>, C<where>, C<append>
1134
C<relation> and C<filter> arguments.
1135
C<table> is a table name.
1136
C<where> is where clause. this must be hash reference
1137
or a string containing such tags as "{= title} or {= author}".
1138
C<append> is a string added at the end of the SQL statement.
1139
C<filter> is filters when parameter binding is executed.
update document
yuki-kimoto authored on 2009-11-19
1140

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

            
added commit method
yuki-kimoto authored on 2010-05-27
1143
    # select * from books;
cleanup
yuki-kimoto authored on 2010-08-05
1144
    my $result = $dbi->select(table => 'books');
packaging one directory
yuki-kimoto authored on 2009-11-16
1145
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1146
    # select * from books where title = ?;
1147
    my $result = $dbi->select(table => 'books', where => {title => 'Perl'});
update document
yuki-kimoto authored on 2009-11-19
1148
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1149
    # select title, author from books where id = ? for update;
cleanup
yuki-kimoto authored on 2010-08-05
1150
    my $result = $dbi->select(
removed register_format()
yuki-kimoto authored on 2010-05-26
1151
        table  => 'books',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1152
        column => ['title', 'author'],
removed register_format()
yuki-kimoto authored on 2010-05-26
1153
        where  => {id => 1},
1154
        appned => 'for update'
update document
yuki-kimoto authored on 2009-11-19
1155
    );
1156
    
added commit method
yuki-kimoto authored on 2010-05-27
1157
    # select books.name as book_name from books, rental 
1158
    # where books.id = rental.book_id;
1159
    my $result = $dbi->select(
removed reconnect method
yuki-kimoto authored on 2010-05-28
1160
        table    => ['books', 'rental'],
1161
        column   => ['books.name as book_name']
added commit method
yuki-kimoto authored on 2010-05-27
1162
        relation => {'books.id' => 'rental.book_id'}
update document
yuki-kimoto authored on 2009-11-19
1163
    );
1164

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1184
    my $result = $dbi->execute("select * from authors where {= name} and {= age}", 
removed reconnect method
yuki-kimoto authored on 2010-05-28
1185
                            param => {name => 'taro', age => 19});
1186
    
1187
    while (my $row = $result->fetch) {
1188
        # do something
1189
    }
1190

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

            
1193
    $dbi->register_filter(%filters);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1194
    $dbi->register_filter(\%filters);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1195
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1196
Register filter. Registered filters is available in the following methods
1197
or arguments.
1198

            
1199
=over 4
1200

            
1201
=item *
1202

            
1203
C<default_bind_filter()>
1204

            
1205
=item *
1206

            
1207
C<default_fetch_filter()>
1208

            
1209
=item *
1210

            
1211
C<filter> argument of C<insert()>, C<update()>,
1212
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>,
1213
C<execute> method.
1214

            
1215
=item *
1216

            
1217
C<DBIx::Custom::Query::default_filter()>
1218

            
1219
=item *
1220

            
1221
C<DBIx::Csutom::Query::filter()>
1222

            
1223
=item *
1224

            
1225
C<DBIx::Custom::Result::default_filter()>
1226

            
1227
=item *
1228

            
1229
C<DBIx::Custom::Result::filter()>
1230

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1235
    $dbi->register_filter(
1236
        encode_utf8 => sub {
1237
            my $value = shift;
1238
            
1239
            require Encode;
1240
            
1241
            return Encode::encode('UTF-8', $value);
1242
        },
1243
        decode_utf8 => sub {
1244
            my $value = shift;
1245
            
1246
            require Encode;
1247
            
1248
            return Encode::decode('UTF-8', $value)
1249
        }
1250
    );
1251

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

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

            
1256
C<< <kimoto.yuki at gmail.com> >>
1257

            
1258
L<http://github.com/yuki-kimoto/DBIx-Custom>
1259

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1260
=head1 AUTHOR
1261

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

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

            
1266
Copyright 2009 Yuki Kimoto, all rights reserved.
1267

            
1268
This program is free software; you can redistribute it and/or modify it
1269
under the same terms as Perl itself.
1270

            
1271
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1272

            
1273