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

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
3
our $VERSION = '0.1655';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
11
use Carp 'croak';
12
use DBI;
13
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
14
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
15
use DBIx::Custom::QueryBuilder;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
16
use DBIx::Custom::Where;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
17
use DBIx::Custom::Model;
cleanup
Yuki Kimoto authored on 2011-01-25
18
use DBIx::Custom::Tag;
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
19
use DBIx::Custom::Util;
update document
yuki-kimoto authored on 2010-05-27
20
use Encode qw/encode_utf8 decode_utf8/;
packaging one directory
yuki-kimoto authored on 2009-11-16
21

            
fix tests
Yuki Kimoto authored on 2011-01-13
22
__PACKAGE__->attr(
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
23
    [qw/data_source dbh password user/],
fix tests
Yuki Kimoto authored on 2011-01-13
24
    cache => 1,
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
25
    dbi_option => sub { {} },
26
    default_dbi_option => sub {{
27
        RaiseError => 1,
28
        PrintError => 0,
29
        AutoCommit => 1
30
    }},
add models() attribute
Yuki Kimoto authored on 2011-02-21
31
    models => sub { {} },
cleanup
Yuki Kimoto authored on 2011-01-23
32
    query_builder => sub { DBIx::Custom::QueryBuilder->new },
many changed
Yuki Kimoto authored on 2011-01-23
33
    result_class  => 'DBIx::Custom::Result',
update pod
Yuki Kimoto authored on 2011-02-07
34
    safety_column_name => sub { qr/^[\w\.]*$/ },
35
    stash => sub { {} }
many changed
Yuki Kimoto authored on 2011-01-23
36
);
37

            
38
__PACKAGE__->attr(
39
    cache_method => sub {
40
        sub {
41
            my $self = shift;
42
            
43
            $self->{_cached} ||= {};
44
            
45
            if (@_ > 1) {
46
                $self->{_cached}{$_[0]} = $_[1] 
47
            }
48
            else {
49
                return $self->{_cached}{$_[0]}
50
            }
51
        }
52
    }
53
);
54

            
55
__PACKAGE__->attr(
fix tests
Yuki Kimoto authored on 2011-01-13
56
    filters => sub {
57
        {
58
            encode_utf8 => sub { encode_utf8($_[0]) },
59
            decode_utf8 => sub { decode_utf8($_[0]) }
60
        }
many changed
Yuki Kimoto authored on 2011-01-23
61
    }
fix tests
Yuki Kimoto authored on 2011-01-13
62
);
cleanup
yuki-kimoto authored on 2010-10-17
63

            
added helper method
yuki-kimoto authored on 2010-10-17
64
our $AUTOLOAD;
65
sub AUTOLOAD {
66
    my $self = shift;
67

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
68
    # Method name
69
    my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
added helper method
yuki-kimoto authored on 2010-10-17
70

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
71
    # Method
72
    $self->{_methods} ||= {};
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
73
    if (my $method = $self->{_methods}->{$mname}) {
74
        return $self->$method(@_)
75
    }
76
    elsif ($self->dbh->can($mname)) {
77
        $self->dbh->$mname(@_);
78
    }
79
    else {
80
        croak qq/Can't locate object method "$mname" via "$package"/
81
    }
added helper method
yuki-kimoto authored on 2010-10-17
82
}
83

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
84
sub apply_filter {
many changed
Yuki Kimoto authored on 2011-01-23
85
    my ($self, $table, @cinfos) = @_;
86

            
87
    # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
88
    $self->{filter} ||= {};
many changed
Yuki Kimoto authored on 2011-01-23
89
    $self->{filter}{out} ||= {};
90
    $self->{filter}{in} ||= {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
91
    $self->{filter}{end} ||= {};
cleanup
Yuki Kimoto authored on 2010-12-22
92
    
many changed
Yuki Kimoto authored on 2011-01-23
93
    # Create filters
94
    my $usage = "Usage: \$dbi->apply_filter(" .
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
95
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
96
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
many changed
Yuki Kimoto authored on 2011-01-23
97

            
98
    for (my $i = 0; $i < @cinfos; $i += 2) {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
99
        
many changed
Yuki Kimoto authored on 2011-01-23
100
        # Column
101
        my $column = $cinfos[$i];
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
102
        
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
103
        if (ref $column eq 'ARRAY') {
104
            foreach my $c (@$column) {
105
                push @cinfos, $c, $cinfos[$i + 1];
106
            }
107
            next;
108
        }
109
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
110
        # Filter info
111
        my $finfo = $cinfos[$i + 1] || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
112
        croak "$usage (table: $table)" unless  ref $finfo eq 'HASH';
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
113
        foreach my $ftype (keys %$finfo) {
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
114
            croak "$usage (table: $table 2)" unless $ftype eq 'in' || $ftype eq 'out'
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
115
                             || $ftype eq 'end'; 
many changed
Yuki Kimoto authored on 2011-01-23
116
        }
117
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
118
        foreach my $way (qw/in out end/) {
119
            my $filter = $finfo->{$way};
cleanup
Yuki Kimoto authored on 2010-12-22
120
            
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
121
            # State
122
            my $state = !exists $finfo->{$way} ? 'not_exists'
123
                      : !defined $filter        ? 'not_defined'
124
                      : ref $filter eq 'CODE'   ? 'code'
125
                      : 'name';
126
            
127
            next if $state eq 'not_exists';
128
            
129
            # Check filter
130
            croak qq{Filter "$filter" is not registered}
131
              if  $state eq 'name'
132
               && ! exists $self->filters->{$filter};
133
            
134
            # Filter
135
            my $f = $state eq 'not_defined' ? undef
136
                  : $state eq 'code'        ? $filter
137
                  : $self->filters->{$filter};
138
            $self->{filter}{$way}{$table}{$column} = $f;
139
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
140
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
many changed
Yuki Kimoto authored on 2011-01-23
141
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
142
    }
143
    
many changed
Yuki Kimoto authored on 2011-01-23
144
    return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
145
}
146

            
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
147
sub method {
added helper method
yuki-kimoto authored on 2010-10-17
148
    my $self = shift;
149
    
150
    # Merge
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
151
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
152
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
added helper method
yuki-kimoto authored on 2010-10-17
153
    
154
    return $self;
155
}
156

            
packaging one directory
yuki-kimoto authored on 2009-11-16
157
sub connect {
cleanup
Yuki Kimoto authored on 2011-01-25
158
    my $self = ref $_[0] ? shift : shift->new(@_);;
removed register_format()
yuki-kimoto authored on 2010-05-26
159
    
many changed
Yuki Kimoto authored on 2011-01-23
160
    # Attributes
packaging one directory
yuki-kimoto authored on 2009-11-16
161
    my $data_source = $self->data_source;
many changed
Yuki Kimoto authored on 2011-01-23
162
    croak qq{"data_source" must be specified to connect()"}
check arguments of connect m...
Yuki Kimoto authored on 2010-12-20
163
      unless $data_source;
packaging one directory
yuki-kimoto authored on 2009-11-16
164
    my $user        = $self->user;
165
    my $password    = $self->password;
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
166
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
added dbi_options attribute
kimoto authored on 2010-12-20
167
    
update document
yuki-kimoto authored on 2010-01-30
168
    # Connect
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
169
    my $dbh = eval {DBI->connect(
packaging one directory
yuki-kimoto authored on 2009-11-16
170
        $data_source,
171
        $user,
172
        $password,
173
        {
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
174
            %{$self->default_dbi_option},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
175
            %$dbi_option
packaging one directory
yuki-kimoto authored on 2009-11-16
176
        }
177
    )};
178
    
update document
yuki-kimoto authored on 2010-01-30
179
    # Connect error
packaging one directory
yuki-kimoto authored on 2009-11-16
180
    croak $@ if $@;
181
    
update document
yuki-kimoto authored on 2010-01-30
182
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
183
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
184
    
packaging one directory
yuki-kimoto authored on 2009-11-16
185
    return $self;
186
}
187

            
cleanup
yuki-kimoto authored on 2010-10-17
188
sub create_query {
189
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
190
    
cleanup
yuki-kimoto authored on 2010-10-17
191
    # Cache
192
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
193
    
cleanup
yuki-kimoto authored on 2010-10-17
194
    # Create query
195
    my $query;
196
    if ($cache) {
197
        
198
        # Get query
199
        my $q = $self->cache_method->($self, $source);
200
        
201
        # Create query
add table tag
Yuki Kimoto authored on 2011-02-09
202
        if ($q) {
203
            $query = DBIx::Custom::Query->new($q);
204
            $query->filters($self->filters);
205
        }
cleanup
yuki-kimoto authored on 2010-10-17
206
    }
207
    
208
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
209

            
cleanup
yuki-kimoto authored on 2010-10-17
210
        # Create SQL object
211
        my $builder = $self->query_builder;
212
        
213
        # Create query
214
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
215

            
cleanup
yuki-kimoto authored on 2010-10-17
216
        # Cache query
217
        $self->cache_method->($self, $source,
218
                             {sql     => $query->sql, 
add table tag
Yuki Kimoto authored on 2011-02-09
219
                              columns => $query->columns,
220
                              tables  => $query->tables})
cleanup
yuki-kimoto authored on 2010-10-17
221
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
222
    }
223
    
cleanup
yuki-kimoto authored on 2010-10-17
224
    # Prepare statement handle
225
    my $sth;
226
    eval { $sth = $self->dbh->prepare($query->{sql})};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
227
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
228
    
cleanup
yuki-kimoto authored on 2010-10-17
229
    # Set statement handle
230
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
231
    
cleanup
Yuki Kimoto authored on 2011-02-09
232
    # Set filters
233
    $query->filters($self->filters);
234
    
cleanup
yuki-kimoto authored on 2010-10-17
235
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
236
}
237

            
cleanup
yuki-kimoto authored on 2010-10-17
238
our %VALID_DELETE_ARGS
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
239
  = map { $_ => 1 } qw/table where append filter allow_delete_all query/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
240

            
cleanup
yuki-kimoto authored on 2010-10-17
241
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
242
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
243
    
244
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
245
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
246
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
247
          unless $VALID_DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
248
    }
249
    
250
    # Arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
251
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
252
    croak qq{"table" option must be specified} unless $table;
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
253
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
254
    my $append           = $args{append};
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
255
    my $filter           = $args{filter};
cleanup
yuki-kimoto authored on 2010-10-17
256
    my $allow_delete_all = $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
257

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
258
    # Where
259
    my $w;
260
    if (ref $where eq 'HASH') {
261
        my $clause = ['and'];
262
        push @$clause, "{= $_}" for keys %$where;
263
        $w = $self->where;
264
        $w->clause($clause);
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
265
        $w->param($where);
packaging one directory
yuki-kimoto authored on 2009-11-16
266
    }
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
267
    elsif (ref $where eq 'DBIx::Custom::Where') {
268
        $w = $where;
269
        $where = $w->param;
270
    }    
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
271
    croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
272
      unless ref $w eq 'DBIx::Custom::Where';
273
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
274
    # String where
275
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
276
    
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
277
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
278
      if $swhere eq '' && !$allow_delete_all;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
279

            
cleanup
Yuki Kimoto authored on 2011-01-27
280
    # SQL stack
281
    my @sql;
282

            
283
    # Delete
284
    push @sql, "delete from $table $swhere";
285
    push @sql, $append if $append;
286
    
287
    my $sql = join(' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
288
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
289
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
290
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
291
    return $query if $args{query};
292
    
packaging one directory
yuki-kimoto authored on 2009-11-16
293
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
294
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
295
        $query, param  => $where, filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
296
        table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
297
    
298
    return $ret_val;
299
}
300

            
cleanup
yuki-kimoto authored on 2010-10-17
301
sub delete_all { shift->delete(allow_delete_all => 1, @_) }
packaging one directory
yuki-kimoto authored on 2009-11-16
302

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
303
our %VALID_DELETE_AT_ARGS
304
  = map { $_ => 1 } qw/table where append filter query
305
                       primary_key param/;
306

            
307
sub delete_at {
308
    my ($self, %args) = @_;
309
    
310
    # Check arguments
311
    foreach my $name (keys %args) {
312
        croak qq{"$name" is invalid argument}
313
          unless $VALID_DELETE_AT_ARGS{$name};
314
    }
315
    
316
    # Primary key
317
    my $primary_keys = delete $args{primary_key};
318
    $primary_keys = [$primary_keys] unless ref $primary_keys;
319
    
320
    # Where clause
321
    my $where = {};
322
    if (exists $args{where}) {
323
        my $where_columns = delete $args{where};
324
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
325

            
326
        croak qq{"where" must be constant value or array reference}
327
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
328
        
329
        for(my $i = 0; $i < @$primary_keys; $i ++) {
330
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
331
        }
332
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
333
    
334
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
335
        my $param = delete $args{param};
336
        
337
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
338
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
339
        }
340
    }
341
    
342
    return $self->delete(where => $where, %args);
343
}
344

            
added helper method
yuki-kimoto authored on 2010-10-17
345
sub DESTROY { }
346

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
347
our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter table/;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
348

            
cleanup
yuki-kimoto authored on 2010-10-17
349
sub execute{
350
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
351
    
352
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
353
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
354
        croak qq{"$name" is invalid argument}
cleanup
yuki-kimoto authored on 2010-10-17
355
          unless $VALID_EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
356
    }
357
    
cleanup
yuki-kimoto authored on 2010-10-17
358
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
359
    
cleanup
yuki-kimoto authored on 2010-10-17
360
    # First argument is the soruce of SQL
361
    $query = $self->create_query($query)
362
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
363
    
add table tag
Yuki Kimoto authored on 2011-02-09
364
    # Applied filter
cleanup
Yuki Kimoto authored on 2011-01-12
365
    my $filter = {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
366
    
add table tag
Yuki Kimoto authored on 2011-02-09
367
    my $tables = $query->tables;
368
    my $arg_tables = $args{table} || [];
369
    $arg_tables = [$arg_tables]
370
      unless ref $arg_tables eq 'ARRAY';
371
    push @$tables, @$arg_tables;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
372
    
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
373
    foreach my $table (@$tables) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
374
        next unless $table;
cleanup
Yuki Kimoto authored on 2011-01-12
375
        $filter = {
376
            %$filter,
377
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
378
        }
379
    }
380
    
cleanup
Yuki Kimoto authored on 2011-01-12
381
    # Filter argument
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
382
    my $f = DBIx::Custom::Util::array_filter_to_hash($args{filter})
383
         || $query->filter || {};
cleanup
Yuki Kimoto authored on 2011-01-12
384
    foreach my $column (keys %$f) {
385
        my $fname = $f->{$column};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
386
        if (!defined $fname) {
cleanup
Yuki Kimoto authored on 2011-01-12
387
            $f->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
388
        }
389
        elsif (ref $fname ne 'CODE') {
many changed
Yuki Kimoto authored on 2011-01-23
390
          croak qq{Filter "$fname" is not registered"}
cleanup
Yuki Kimoto authored on 2010-12-21
391
            unless exists $self->filters->{$fname};
392
          
cleanup
Yuki Kimoto authored on 2011-01-12
393
          $f->{$column} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2010-12-21
394
        }
395
    }
cleanup
Yuki Kimoto authored on 2011-01-12
396
    $filter = {%$filter, %$f};
packaging one directory
yuki-kimoto authored on 2009-11-16
397
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
398
    # Bind
399
    my $bind = $self->_bind($params, $query->columns, $filter);
cleanup
yuki-kimoto authored on 2010-10-17
400
    
401
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
402
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
403
    my $affected;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
404
    eval {$affected = $sth->execute(@$bind)};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
405
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
406
    
407
    # Return resultset if select statement is executed
408
    if ($sth->{NUM_OF_FIELDS}) {
409
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
410
        # Result in and end filter
411
        my $in_filter  = {};
412
        my $end_filter = {};
cleanup
Yuki Kimoto authored on 2011-01-12
413
        foreach my $table (@$tables) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
414
            next unless $table;
cleanup
Yuki Kimoto authored on 2011-01-12
415
            $in_filter = {
416
                %$in_filter,
417
                %{$self->{filter}{in}{$table} || {}}
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
418
            };
419
            $end_filter = {
420
                %$end_filter,
421
                %{$self->{filter}{end}{$table} || {}}
422
            };
cleanup
Yuki Kimoto authored on 2011-01-12
423
        }
424
        
425
        # Result
426
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
427
            sth            => $sth,
428
            filters        => $self->filters,
429
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
430
            default_filter => $self->{default_in_filter},
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
431
            filter         => $in_filter || {},
432
            end_filter     => $end_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
433
        );
434

            
435
        return $result;
436
    }
437
    return $affected;
438
}
439

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
440
our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
441
                                            filter query/;
cleanup
yuki-kimoto authored on 2010-10-17
442
sub insert {
443
    my ($self, %args) = @_;
444

            
445
    # Check arguments
446
    foreach my $name (keys %args) {
447
        croak qq{"$name" is invalid argument}
448
          unless $VALID_INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
449
    }
450
    
cleanup
yuki-kimoto authored on 2010-10-17
451
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
452
    my $table  = $args{table};
453
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
454
    my $param  = $args{param} || {};
455
    my $append = $args{append} || '';
456
    my $filter = $args{filter};
457
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
458
    # Columns
459
    my @columns;
460
    my $safety = $self->safety_column_name;
461
    foreach my $column (keys %$param) {
462
        croak qq{"$column" is not safety column name}
463
          unless $column =~ /$safety/;
464
        push @columns, $column;
465
    }
cleanup
yuki-kimoto authored on 2010-10-17
466
    
cleanup
Yuki Kimoto authored on 2011-01-27
467
    # SQL stack
468
    my @sql;
469
    
470
    # Insert
471
    push @sql, "insert into $table {insert_param ". join(' ', @columns) . '}';
472
    push @sql, $append if $append;
473
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
474
    # SQL
cleanup
Yuki Kimoto authored on 2011-01-27
475
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
476
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
477
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
478
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
479
    return $query if $args{query};
480
    
packaging one directory
yuki-kimoto authored on 2009-11-16
481
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
482
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
483
        $query,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
484
        param  => $param,
485
        filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
486
        table => $table
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
487
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
488
    
489
    return $ret_val;
490
}
491

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
492
our %VALID_INSERT_AT_ARGS
493
  = map { $_ => 1 } qw/table param
494
                       where append filter query
495
                       primary_key param/;
496

            
497
sub insert_at {
498
    my ($self, %args) = @_;
499
    
500
    # Check arguments
501
    foreach my $name (keys %args) {
502
        croak qq{"$name" is invalid argument}
503
          unless $VALID_INSERT_AT_ARGS{$name};
504
    }
505
    
506
    # Primary key
507
    my $primary_keys = delete $args{primary_key};
508
    $primary_keys = [$primary_keys] unless ref $primary_keys;
509
    
510
    # Where clause
511
    my $where = {};
512
    my $param = {};
513
    
514
    if (exists $args{where}) {
515
        my $where_columns = delete $args{where};
516
        $where_columns = [$where_columns] unless ref $where_columns;
517

            
518
        croak qq{"where" must be constant value or array reference}
519
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
520
        
521
        for(my $i = 0; $i < @$primary_keys; $i ++) {
522
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
523
        }
524
    }
525
    
526
    if (exists $args{param}) {
527
        $param = delete $args{param};
528
        for(my $i = 0; $i < @$primary_keys; $i ++) {
529
             delete $param->{$primary_keys->[$i]};
530
        }
531
    }
532
    
533
    $param = {%$param, %$where};
534
    
535
    return $self->insert(param => $param, %args);
536
}
537

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
538
sub insert_param {
539
    my ($self, $param) = @_;
540
    
541
    my @tag;
542
    
543
    push @tag, '{insert_param';
544
    
545
    foreach my $column (keys %$param) {
546
        push @tag, $column;
547
    }
548
    
549
    push @tag, '}';
550
    
551
    return join ' ', @tag;
552
}
553

            
pod fix
Yuki Kimoto authored on 2011-01-21
554
sub each_column {
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
555
    my ($self, $cb) = @_;
556
    
557
    # Iterate all tables
558
    my $sth_tables = $self->dbh->table_info;
559
    while (my $table_info = $sth_tables->fetchrow_hashref) {
560
        
561
        # Table
562
        my $table = $table_info->{TABLE_NAME};
563
        
564
        # Iterate all columns
565
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
566
        while (my $column_info = $sth_columns->fetchrow_hashref) {
567
            my $column = $column_info->{COLUMN_NAME};
removed experimental txn_sco...
Yuki Kimoto authored on 2011-01-24
568
            $self->$cb($table, $column, $column_info);
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
569
        }
570
    }
571
}
572

            
added dbi_options attribute
kimoto authored on 2010-12-20
573
sub new {
574
    my $self = shift->SUPER::new(@_);
575
    
576
    # Check attribute names
577
    my @attrs = keys %$self;
578
    foreach my $attr (@attrs) {
579
        croak qq{"$attr" is invalid attribute name}
580
          unless $self->can($attr);
581
    }
cleanup
Yuki Kimoto authored on 2011-01-25
582

            
583
    $self->register_tag(
584
        '?'     => \&DBIx::Custom::Tag::placeholder,
585
        '='     => \&DBIx::Custom::Tag::equal,
586
        '<>'    => \&DBIx::Custom::Tag::not_equal,
587
        '>'     => \&DBIx::Custom::Tag::greater_than,
588
        '<'     => \&DBIx::Custom::Tag::lower_than,
589
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
590
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
591
        'like'  => \&DBIx::Custom::Tag::like,
592
        'in'    => \&DBIx::Custom::Tag::in,
593
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
594
        'update_param' => \&DBIx::Custom::Tag::update_param
595
    );
added dbi_options attribute
kimoto authored on 2010-12-20
596
    
597
    return $self;
598
}
599

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
600
sub not_exists { bless {}, 'DBIx::Custom::NotExists' }
601

            
cleanup
yuki-kimoto authored on 2010-10-17
602
sub register_filter {
603
    my $invocant = shift;
604
    
605
    # Register filter
606
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
607
    $invocant->filters({%{$invocant->filters}, %$filters});
608
    
609
    return $invocant;
610
}
packaging one directory
yuki-kimoto authored on 2009-11-16
611

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
612
sub register_tag { shift->query_builder->register_tag(@_) }
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
613

            
refactoring select
yuki-kimoto authored on 2010-04-28
614
our %VALID_SELECT_ARGS
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
615
  = map { $_ => 1 } qw/table column where append relation filter query selection left_join/;
refactoring select
yuki-kimoto authored on 2010-04-28
616

            
packaging one directory
yuki-kimoto authored on 2009-11-16
617
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
618
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
619
    
refactoring select
yuki-kimoto authored on 2010-04-28
620
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
621
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
622
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
623
          unless $VALID_SELECT_ARGS{$name};
624
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
625
    
refactoring select
yuki-kimoto authored on 2010-04-28
626
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
627
    my $table = $args{table};
628
    my $tables = ref $table eq 'ARRAY' ? $table
629
               : defined $table ? [$table]
630
               : [];
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
631
    my $columns   = $args{column} || [];
select method column option ...
Yuki Kimoto authored on 2011-02-22
632
    $columns = [$columns] unless ref $columns;
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
633
    my $selection = $args{selection} || '';
634
    my $where     = $args{where} || {};
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
635
    my $relation  = $args{relation} || {};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
636
    my $append    = $args{append};
637
    my $filter    = $args{filter};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
638
    my $left_join = $args{left_join} || [];
639
    
640
    my @left_join_tables;
641
    unshift @left_join_tables, $tables->[-1];
642
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
643
    # Relation
644
    if (!$selection && keys %$relation) {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
645
        foreach my $rcolumn (keys %$relation) {
646
            my $table1 = (split (/\./, $rcolumn))[0];
647
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
648
            
649
            my $table1_exists;
650
            my $table2_exists;
651
            foreach my $table (@$tables) {
652
                $table1_exists = 1 if $table eq $table1;
653
                $table2_exists = 1 if $table eq $table2;
654
            }
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
655
            unshift @$tables, $table1 unless $table1_exists;
656
            unshift @$tables, $table2 unless $table2_exists;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
657
        }
658
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
659
    
cleanup
Yuki Kimoto authored on 2011-01-27
660
    # SQL stack
661
    my @sql;
662
    
663
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
664
    
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
665
    if ($selection) {
666
        push @sql, $selection;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
667
        push @left_join_tables, @{$self->_tables($selection)};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
668
    }
669
    else {
670
        # Column clause
671
        if (@$columns) {
672
            foreach my $column (@$columns) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
673
                push @left_join_tables, @{$self->_tables($column)};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
674
                push @sql, ($column, ',');
675
            }
676
            pop @sql if $sql[-1] eq ',';
677
        }
678
        else { push @sql, '*' }
679
        
680
        # Table
681
        croak qq{"table" option must be specified} unless @$tables;
682
        push @sql, 'from';
683
        foreach my $table (@$tables) {
684
            push @sql, ($table, ',');
packaging one directory
yuki-kimoto authored on 2009-11-16
685
        }
cleanup
Yuki Kimoto authored on 2011-01-27
686
        pop @sql if $sql[-1] eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
687
    }
688
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
689
    # Where
690
    my $w;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
691
    if (ref $where eq 'HASH') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
692
        my $clause = ['and'];
693
        push @$clause, "{= $_}" for keys %$where;
694
        $w = $self->where;
695
        $w->clause($clause);
696
        $w->param($where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
697
    }
698
    elsif (ref $where eq 'DBIx::Custom::Where') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
699
        $w = $where;
700
        $where = $w->param;
packaging one directory
yuki-kimoto authored on 2009-11-16
701
    }
702
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
703
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
704
      unless ref $w eq 'DBIx::Custom::Where';
705
    
706
    # String where
707
    my $swhere = "$w";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
708
    
709
    # Table name in Where
710
    unshift @left_join_tables, @{$self->_tables($swhere)};
711
    
712
    # Left join
713
    if (@$left_join) {
714
        for (my $i = 0; $i < @$left_join; $i += 2) {
715
            my $column1 = $left_join->[$i];
716
            my $column2 = $left_join->[$i + 1];
717
            
718
            my $table1 = (split (/\./, $column1))[0];
719
            my $table2 = (split (/\./, $column2))[0];
720
            
721
            my $table1_exists;
722
            my $table2_exists;
723
            
724
            foreach my $table (@left_join_tables) {
725
                $table1_exists = 1 if $table eq $table1;
726
                $table2_exists = 1 if $table eq $table2;
727
            }
728
            
729
            if ($table1_exists && $table2_exists) {
730
                push @sql, "left outer join $table2 on $column1 = $column2";
731
            }
732
        }
733
    }
734
    
735
    # Add where
cleanup
Yuki Kimoto authored on 2011-01-27
736
    push @sql, $swhere;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
737
    
added commit method
yuki-kimoto authored on 2010-05-27
738
    # Relation
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
739
    if (!$selection && keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-01-27
740
        push @sql, $swhere eq '' ? 'where' : 'and';
741
        foreach my $rcolumn (keys %$relation) {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-21
742
            my $table1 = (split (/\./, $rcolumn))[0];
743
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
744
            push @$tables, ($table1, $table2);
745
            
cleanup
Yuki Kimoto authored on 2011-01-27
746
            push @sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
packaging one directory
yuki-kimoto authored on 2009-11-16
747
        }
748
    }
cleanup
Yuki Kimoto authored on 2011-01-27
749
    pop @sql if $sql[-1] eq 'and';
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
750
        
cleanup
Yuki Kimoto authored on 2011-01-27
751
    # Append statement
752
    push @sql, $append if $append;
753
    
754
    # SQL
755
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
756
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
757
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
758
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
759
    return $query if $args{query};
760
    
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
761
    unshift @$tables, @left_join_tables;
762
    
packaging one directory
yuki-kimoto authored on 2009-11-16
763
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
764
    my $result = $self->execute(
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
765
        $query, param  => $where, filter => $filter,
766
        table => $tables);
packaging one directory
yuki-kimoto authored on 2009-11-16
767
    
768
    return $result;
769
}
770

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
771
our %VALID_SELECT_AT_ARGS
772
  = map { $_ => 1 } qw/table column where append relation filter query selection
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
773
                       param primary_key left_join/;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
774

            
775
sub select_at {
776
    my ($self, %args) = @_;
777
    
778
    # Check arguments
779
    foreach my $name (keys %args) {
780
        croak qq{"$name" is invalid argument}
781
          unless $VALID_SELECT_AT_ARGS{$name};
782
    }
783
    
784
    # Primary key
785
    my $primary_keys = delete $args{primary_key};
786
    $primary_keys = [$primary_keys] unless ref $primary_keys;
787
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
788
    # Table
789
    croak qq{"table" option must be specified} unless $args{table};
790
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
791
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
792
    # Where clause
793
    my $where = {};
794
    if (exists $args{where}) {
795
        my $where_columns = delete $args{where};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
796
        
797
        croak qq{"where" must be constant value or array reference}
798
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
799
        
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
800
        $where_columns = [$where_columns] unless ref $where_columns;
801
        
802
        for(my $i = 0; $i < @$primary_keys; $i ++) {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
803
           $where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
804
        }
805
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
806
    
807
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
808
        my $param = delete $args{param};
809
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
810
             delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
811
        }
812
    }
813
    
814
    return $self->select(where => $where, %args);
815
}
816

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
817
sub model {
818
    my ($self, $name, $model) = @_;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
819
    
820
    # Set
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
821
    if ($model) {
add models() attribute
Yuki Kimoto authored on 2011-02-21
822
        $self->models->{$name} = $model;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
823
        return $self;
824
    }
825
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
826
    # Check model existance
827
    croak qq{Model "$name" is not included}
add models() attribute
Yuki Kimoto authored on 2011-02-21
828
      unless $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
829
    
830
    # Get
add models() attribute
Yuki Kimoto authored on 2011-02-21
831
    return $self->models->{$name};
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
832
}
833

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
834
sub include_model {
835
    my ($self, $name_space, $model_infos) = @_;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
836
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
837
    $name_space ||= '';
838
    unless ($model_infos) {
839
        # Load name space module
840
        croak qq{"$name_space" is invalid class name}
841
          if $name_space =~ /[^\w:]/;
842
        eval "use $name_space";
843
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
844
        
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
845
        # Search model modules
846
        my $path = $INC{"$name_space.pm"};
847
        $path =~ s/\.pm$//;
848
        opendir my $dh, $path
849
          or croak qq{Can't open directory "$path": $!};
850
        $model_infos = [];
851
        while (my $module = readdir $dh) {
852
            push @$model_infos, $module
853
              if $module =~ s/\.pm$//;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
854
        }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
855
        
856
        close $dh;
857
    }
858
    
859
    foreach my $model_info (@$model_infos) {
860
        
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
861
        # Model class, name, table
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
862
        my $model_class;
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
863
        my $model_name;
864
        my $model_table;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
865
        if (ref $model_info eq 'HASH') {
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
866
            $model_class = $model_info->{class};
867
            $model_name  = $model_info->{name};
868
            $model_table = $model_info->{table};
869
            
870
            $model_name  ||= $model_class;
871
            $model_table ||= $model_name;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
872
        }
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
873
        else { $model_class =$model_name = $model_table = $model_info }
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
874
        my $mclass = "${name_space}::$model_class";
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
875
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
876
        # Load
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
877
        croak qq{"$mclass" is invalid class name}
878
          if $mclass =~ /[^\w:]/;
879
        unless ($mclass->can('isa')) {
880
            eval "use $mclass";
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
881
            croak $@ if $@;
882
        }
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
883
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
884
        # Instantiate
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
885
        my $model = $mclass->new(dbi => $self);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
886
        $model->name($model_name) unless $model->name;
887
        $model->table($model_table) unless $model->table;
removed experimental DBIx::C...
Yuki Kimoto authored on 2011-01-28
888
        
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
889
        # Set
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
890
        $self->model($model->name, $model);
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-24
891
        
892
        # Apply filter
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
893
        croak "${name_space}::$model_class filter must be array reference"
894
          unless ref $model->filter eq 'ARRAY';
895
        $self->apply_filter($model->table, @{$model->filter});
table object call dbi object...
Yuki Kimoto authored on 2011-01-25
896
    }
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
897
    return $self;
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
898
}
899

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
900
sub setup_model {
901
    my $self = shift;
902
    
903
    $self->each_column(
904
        sub {
905
            my ($self, $table, $column, $column_info) = @_;
906
            
907
            if (my $model = $self->models->{$table}) {
908
                push @{$model->columns}, $column;
909
            }
910
        }
911
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
912
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
913
}
914

            
cleanup
yuki-kimoto authored on 2010-10-17
915
our %VALID_UPDATE_ARGS
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
916
  = map { $_ => 1 } qw/table param
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
917
                       where append filter allow_update_all query/;
cleanup
yuki-kimoto authored on 2010-10-17
918

            
919
sub update {
920
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
921
    
cleanup
yuki-kimoto authored on 2010-10-17
922
    # Check arguments
923
    foreach my $name (keys %args) {
924
        croak qq{"$name" is invalid argument}
925
          unless $VALID_UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
926
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
927
    
cleanup
yuki-kimoto authored on 2010-10-17
928
    # Arguments
929
    my $table            = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
930
    croak qq{"table" option must be specified} unless $table;
cleanup
yuki-kimoto authored on 2010-10-17
931
    my $param            = $args{param} || {};
932
    my $where            = $args{where} || {};
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
933
    my $append           = $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
934
    my $filter           = $args{filter};
935
    my $allow_update_all = $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
936
    
cleanup
yuki-kimoto authored on 2010-10-17
937
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
938
    my @clumns = keys %$param;
939

            
940
    # Columns
941
    my @columns;
942
    my $safety = $self->safety_column_name;
943
    foreach my $column (keys %$param) {
944
        croak qq{"$column" is not safety column name}
945
          unless $column =~ /$safety/;
946
        push @columns, $column;
947
    }
948
        
cleanup
yuki-kimoto authored on 2010-10-17
949
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
950
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
951

            
952
    # Where
953
    my $w;
954
    if (ref $where eq 'HASH') {
955
        my $clause = ['and'];
956
        push @$clause, "{= $_}" for keys %$where;
957
        $w = $self->where;
958
        $w->clause($clause);
959
        $w->param($where);
960
    }
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
961
    elsif (ref $where eq 'DBIx::Custom::Where') {
962
        $w = $where;
963
        $where = $w->param;
964
    }  
removed experimental registe...
yuki-kimoto authored on 2010-08-24
965
    
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
966
    croak qq{"where" must be hash refernce or DBIx::Custom::Where object}
967
      unless ref $w eq 'DBIx::Custom::Where';
removed reconnect method
yuki-kimoto authored on 2010-05-28
968
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
969
    # String where
970
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
971
    
972
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
973
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
974
    
cleanup
Yuki Kimoto authored on 2011-01-27
975
    # SQL stack
976
    my @sql;
977
    
978
    # Update
979
    push @sql, "update $table $update_clause $swhere";
980
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
981
    
cleanup
yuki-kimoto authored on 2010-10-17
982
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
983
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
984
        
cleanup
yuki-kimoto authored on 2010-10-17
985
        if (exists $param->{$wkey}) {
986
            $param->{$wkey} = [$param->{$wkey}]
987
              unless ref $param->{$wkey} eq 'ARRAY';
988
            
989
            push @{$param->{$wkey}}, $where->{$wkey};
990
        }
991
        else {
992
            $param->{$wkey} = $where->{$wkey};
993
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
994
    }
cleanup
yuki-kimoto authored on 2010-10-17
995
    
cleanup
Yuki Kimoto authored on 2011-01-27
996
    # SQL
997
    my $sql = join(' ', @sql);
998
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
999
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
1000
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1001
    return $query if $args{query};
1002
    
cleanup
yuki-kimoto authored on 2010-10-17
1003
    # Execute query
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1004
    my $ret_val = $self->execute($query, param  => $param, 
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1005
                                 filter => $filter,
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1006
                                 table => $table);
cleanup
yuki-kimoto authored on 2010-10-17
1007
    
1008
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1009
}
1010

            
cleanup
yuki-kimoto authored on 2010-10-17
1011
sub update_all { shift->update(allow_update_all => 1, @_) };
1012

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1013
our %VALID_UPDATE_AT_ARGS
1014
  = map { $_ => 1 } qw/table param
1015
                       where append filter query
1016
                       primary_key param/;
1017

            
1018
sub update_at {
1019
    my ($self, %args) = @_;
1020
    
1021
    # Check arguments
1022
    foreach my $name (keys %args) {
1023
        croak qq{"$name" is invalid argument}
1024
          unless $VALID_UPDATE_AT_ARGS{$name};
1025
    }
1026
    
1027
    # Primary key
1028
    my $primary_keys = delete $args{primary_key};
1029
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1030
    
1031
    # Where clause
1032
    my $where = {};
1033
    my $param = {};
1034
    
1035
    if (exists $args{where}) {
1036
        my $where_columns = delete $args{where};
1037
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1038

            
1039
        croak qq{"where" must be constant value or array reference}
1040
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1041
        
1042
        for(my $i = 0; $i < @$primary_keys; $i ++) {
1043
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
1044
        }
1045
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1046
    
1047
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1048
        $param = delete $args{param};
1049
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1050
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1051
        }
1052
    }
1053
    
1054
    return $self->update(where => $where, param => $param, %args);
1055
}
1056

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1057
sub update_param {
1058
    my ($self, $param) = @_;
1059
    
1060
    my @tag;
1061
    
1062
    push @tag, '{update_param';
1063
    
1064
    foreach my $column (keys %$param) {
1065
        push @tag, $column;
1066
    }
1067
    
1068
    push @tag, '}';
1069
    
1070
    return join ' ', @tag;
1071
}
1072

            
cleanup
Yuki Kimoto authored on 2011-01-25
1073
sub where {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1074
    my $self = shift;
1075

            
1076
    return DBIx::Custom::Where->new(
1077
        query_builder => $self->query_builder,
1078
        safety_column_name => $self->safety_column_name
1079
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1080
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1081

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1082
sub _bind {
cleanup
Yuki Kimoto authored on 2011-01-12
1083
    my ($self, $params, $columns, $filter) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1084
    
cleanup
Yuki Kimoto authored on 2011-01-12
1085
    # bind values
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1086
    my @bind;
add tests
yuki-kimoto authored on 2010-08-08
1087
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1088
    # Build bind values
1089
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1090
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1091
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1092
        
1093
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1094
        my $value;
1095
        if(ref $params->{$column} eq 'ARRAY') {
1096
            my $i = $count->{$column} || 0;
1097
            $i += $not_exists->{$column} || 0;
1098
            my $found;
1099
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1100
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1101
                    $not_exists->{$column}++;
1102
                }
1103
                else  {
1104
                    $value = $params->{$column}->[$k];
1105
                    $found = 1;
1106
                    last
1107
                }
1108
            }
1109
            next unless $found;
1110
        }
1111
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1112
        
cleanup
Yuki Kimoto authored on 2011-01-12
1113
        # Filter
1114
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1115
        
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1116
        push @bind, $f ? $f->($value) : $value;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1117
        
1118
        # Count up 
1119
        $count->{$column}++;
1120
    }
1121
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1122
    return \@bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1123
}
1124

            
cleanup
yuki-kimoto authored on 2010-10-17
1125
sub _croak {
1126
    my ($self, $error, $append) = @_;
1127
    $append ||= "";
1128
    
1129
    # Verbose
1130
    if ($Carp::Verbose) { croak $error }
1131
    
1132
    # Not verbose
1133
    else {
1134
        
1135
        # Remove line and module infromation
1136
        my $at_pos = rindex($error, ' at ');
1137
        $error = substr($error, 0, $at_pos);
1138
        $error =~ s/\s+$//;
1139
        
1140
        croak "$error$append";
1141
    }
1142
}
1143

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1144
sub _tables {
1145
    my ($self, $source) = @_;
1146
    
1147
    my $tables = [];
1148
    
1149
    my $safety_name = $self->safety_column_name;
1150
    
1151
    while ($source =~ /\b(\w+)\./g) {
1152
        push @$tables, $1;
1153
    }
1154
    
1155
    return $tables;
1156
}
1157

            
cleanup
Yuki Kimoto authored on 2011-01-25
1158
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1159
__PACKAGE__->attr(
1160
    dbi_options => sub { {} },
1161
    filter_check  => 1
1162
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1163

            
cleanup
Yuki Kimoto authored on 2011-01-25
1164
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1165
sub default_bind_filter {
1166
    my $self = shift;
1167
    
1168
    if (@_) {
1169
        my $fname = $_[0];
1170
        
1171
        if (@_ && !$fname) {
1172
            $self->{default_out_filter} = undef;
1173
        }
1174
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1175
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1176
              unless exists $self->filters->{$fname};
1177
        
1178
            $self->{default_out_filter} = $self->filters->{$fname};
1179
        }
1180
        return $self;
1181
    }
1182
    
1183
    return $self->{default_out_filter};
1184
}
1185

            
cleanup
Yuki Kimoto authored on 2011-01-25
1186
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1187
sub default_fetch_filter {
1188
    my $self = shift;
1189
    
1190
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1191
        my $fname = $_[0];
1192

            
cleanup
Yuki Kimoto authored on 2011-01-12
1193
        if (@_ && !$fname) {
1194
            $self->{default_in_filter} = undef;
1195
        }
1196
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1197
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1198
              unless exists $self->filters->{$fname};
1199
        
1200
            $self->{default_in_filter} = $self->filters->{$fname};
1201
        }
1202
        
1203
        return $self;
1204
    }
1205
    
many changed
Yuki Kimoto authored on 2011-01-23
1206
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1207
}
1208

            
cleanup
Yuki Kimoto authored on 2011-01-25
1209
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1210
sub register_tag_processor {
1211
    return shift->query_builder->register_tag_processor(@_);
1212
}
1213

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1216
=head1 NAME
1217

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

            
1220
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1221

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1222
    use DBIx::Custom;
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1223
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1224
                                    user => 'ken', password => '!LFKD%$&',
1225
                                    dbi_option => {mysql_enable_utf8 => 1});
cleanup
yuki-kimoto authored on 2010-08-05
1226

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1227
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1228
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1229
                 param  => {title => 'Perl', author => 'Ken'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1230
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1231
    
1232
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1233
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1234
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
1235
                 where  => {id => 5},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1236
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1237
    
1238
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1239
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1240
                     param  => {title => 'Perl'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1241
                     filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1242
    
1243
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1244
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1245
                 where  => {author => 'Ken'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1246
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1247
    
1248
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1249
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
1250

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1251
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
1252
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1253
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
1254
        column => [qw/author title/],
1255
        where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1256
        relation => {'book.id' => 'rental.book_id'},
updated document
yuki-kimoto authored on 2010-08-08
1257
        append => 'order by id limit 5',
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1258
        filter => [title => 'to_something']
added commit method
yuki-kimoto authored on 2010-05-27
1259
    );
cleanup
yuki-kimoto authored on 2010-08-05
1260

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1261
    # Execute SQL
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1262
    $dbi->execute("select title from book");
removed register_format()
yuki-kimoto authored on 2010-05-26
1263
    
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1264
    # Execute SQL with hash binding and filtering
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1265
    $dbi->execute("select id from book where {= author} and {like title}",
removed register_format()
yuki-kimoto authored on 2010-05-26
1266
                  param  => {author => 'ken', title => '%Perl%'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1267
                  filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1268

            
1269
    # Create query and execute it
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1270
    my $query = $dbi->create_query(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1271
        "select id from book where {= author} and {like title}"
removed reconnect method
yuki-kimoto authored on 2010-05-28
1272
    );
updated document
yuki-kimoto authored on 2010-08-08
1273
    $dbi->execute($query, param => {author => 'Ken', title => '%Perl%'})
cleanup
yuki-kimoto authored on 2010-08-05
1274

            
1275
    # Get DBI object
1276
    my $dbh = $dbi->dbh;
1277

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1278
    # Fetch
1279
    while (my $row = $result->fetch) {
1280
        # ...
1281
    }
1282
    
1283
    # Fetch hash
1284
    while (my $row = $result->fetch_hash) {
1285
        
1286
    }
1287
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1288
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1289

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1299
L<DBIx::Custom> is middle area between L<DBI> and O/R mapper.
updated document
yuki-kimoto authored on 2010-08-08
1300
L<DBIx::Custom> provide flexible hash parameter binding and filtering system,
added experimental expand me...
yuki-kimoto authored on 2010-10-20
1301
and suger methods, such as C<insert()>, C<update()>, C<delete()>, C<select()>
updated document
yuki-kimoto authored on 2010-08-08
1302
to execute SQL easily.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1303

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1310
See L<DBIx::Custom::Guide> for more details.
1311

            
1312
=head1 GUIDE
1313

            
1314
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
1315

            
1316
=head1 EXAMPLES
1317

            
1318
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki> - Many useful examples
updated document
yuki-kimoto authored on 2010-08-08
1319

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1322
=head2 C<cache>
packaging one directory
yuki-kimoto authored on 2009-11-16
1323

            
cleanup
yuki-kimoto authored on 2010-10-17
1324
    my $cache = $dbi->cache;
1325
    $dbi      = $dbi->cache(1);
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1326

            
cleanup
yuki-kimoto authored on 2010-10-17
1327
Enable parsed L<DBIx::Custom::Query> object caching.
1328
Default to 1.
packaging one directory
yuki-kimoto authored on 2009-11-16
1329

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

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

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

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

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

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

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1345
=head2 C<dbi_option>
added dbi_options attribute
kimoto authored on 2010-12-20
1346

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1347
    my $dbi_option = $dbi->dbi_option;
1348
    $dbi            = $dbi->dbi_option($dbi_option);
added dbi_options attribute
kimoto authored on 2010-12-20
1349

            
1350
DBI options.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1351

            
1352
Each option specified can ovewrite C<default_dbi_option>.
1353

            
1354
C<connect()> method use this value to connect the database.
1355

            
1356

            
1357
=head2 C<default_dbi_option>
1358

            
1359
    my $default_dbi_option = $dbi->default_dbi_option;
1360
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1361

            
1362
DBI default options.
1363

            
1364
    RaiseError => 1,
1365
    PrintError => 0,
1366
    AutoCommit => 1,
1367

            
added dbi_options attribute
kimoto authored on 2010-12-20
1368
C<connect()> method use this value to connect the database.
1369

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1372
=head2 C<filters>
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1373

            
cleanup
yuki-kimoto authored on 2010-10-17
1374
    my $filters = $dbi->filters;
1375
    $dbi        = $dbi->filters(\%filters);
packaging one directory
yuki-kimoto authored on 2009-11-16
1376

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1377
Filters
1378

            
1379
=head2 C<(experimental) models>
1380

            
1381
    my $models = $dbi->models;
1382
    $dbi       = $dbi->models(\%models);
1383

            
1384
Models
1385

            
cleanup
yuki-kimoto authored on 2010-10-17
1386
=head2 C<password>
1387

            
1388
    my $password = $dbi->password;
1389
    $dbi         = $dbi->password('lkj&le`@s');
1390

            
1391
Password.
1392
C<connect()> method use this value to connect the database.
update document
yuki-kimoto authored on 2010-01-30
1393

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1403
=head2 C<result_class>
cleanup
yuki-kimoto authored on 2010-08-05
1404

            
cleanup
yuki-kimoto authored on 2010-10-17
1405
    my $result_class = $dbi->result_class;
1406
    $dbi             = $dbi->result_class('DBIx::Custom::Result');
cleanup
yuki-kimoto authored on 2010-08-05
1407

            
cleanup
yuki-kimoto authored on 2010-10-17
1408
Result class for select statement.
1409
Default to L<DBIx::Custom::Result>.
cleanup
yuki-kimoto authored on 2010-08-05
1410

            
update pod
Yuki Kimoto authored on 2011-01-27
1411
=head2 C<(experimental) safety_column_name>
1412

            
1413
    my $safety_column_name = $self->safety_column_name;
1414
    $dbi                   = $self->safety_column_name($name);
1415

            
1416
Safety column name regex.
1417
Default is qr/^[\w\.]*$/
1418

            
cleanup
yuki-kimoto authored on 2010-10-17
1419
=head2 C<user>
cleanup
yuki-kimoto authored on 2010-08-05
1420

            
cleanup
yuki-kimoto authored on 2010-10-17
1421
    my $user = $dbi->user;
1422
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1423

            
cleanup
yuki-kimoto authored on 2010-10-17
1424
User name.
1425
C<connect()> method use this value to connect the database.
update pod
Yuki Kimoto authored on 2011-01-27
1426

            
cleanup
yuki-kimoto authored on 2010-10-17
1427
=head1 METHODS
added commit method
yuki-kimoto authored on 2010-05-27
1428

            
cleanup
yuki-kimoto authored on 2010-10-17
1429
L<DBIx::Custom> inherits all methods from L<Object::Simple>
autoload DBI method
Yuki Kimoto authored on 2011-01-26
1430
and use all method of L<DBI>
cleanup
yuki-kimoto authored on 2010-10-17
1431
and implements the following new ones.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1432

            
cleanup
Yuki Kimoto authored on 2011-01-25
1433
=head2 C<(experimental) apply_filter>
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1434

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1435
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1436
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1437
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1438
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1439
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1440
    );
1441

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1442
C<apply_filter> is automatically filter for columns of table.
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1443
This have effect C<insert>, C<update>, C<delete>. C<select>
cleanup
Yuki Kimoto authored on 2010-12-21
1444
and L<DBIx::Custom::Result> object. but this has'nt C<execute> method.
1445

            
cleanup
Yuki Kimoto authored on 2011-01-12
1446
If you want to have effect C<execute()> method, use C<table>
cleanup
Yuki Kimoto authored on 2010-12-21
1447
arguments.
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1448

            
cleanup
Yuki Kimoto authored on 2010-12-21
1449
    $result = $dbi->execute(
1450
        "select * from table1 where {= key1} and {= key2};",
1451
         param => {key1 => 1, key2 => 2},
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1452
         table => ['table1']
cleanup
Yuki Kimoto authored on 2010-12-21
1453
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1454

            
1455
You can use three name as column name.
1456

            
1457
    1. column        : author
1458
    2. table.column  : book.author
1459
    3. table__column : book__author
1460

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1471
=head2 C<create_query>
1472
    
1473
    my $query = $dbi->create_query(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1474
        "select * from book where {= author} and {like title};"
cleanup
yuki-kimoto authored on 2010-10-17
1475
    );
update document
yuki-kimoto authored on 2009-11-19
1476

            
cleanup
yuki-kimoto authored on 2010-10-17
1477
Create the instance of L<DBIx::Custom::Query> from the source of SQL.
1478
If you want to get high performance,
1479
use C<create_query()> method and execute it by C<execute()> method
1480
instead of suger methods.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1481

            
cleanup
yuki-kimoto authored on 2010-10-17
1482
    $dbi->execute($query, {author => 'Ken', title => '%Perl%'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1483

            
cleanup
yuki-kimoto authored on 2010-10-17
1484
=head2 C<execute>
packaging one directory
yuki-kimoto authored on 2009-11-16
1485

            
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1486
    my $result = $dbi->execute($query,  param => $params, filter => \@filter);
1487
    my $result = $dbi->execute($source, param => $params, filter => \@filter);
update document
yuki-kimoto authored on 2009-11-19
1488

            
cleanup
yuki-kimoto authored on 2010-10-17
1489
Execute query or the source of SQL.
1490
Query is L<DBIx::Custom::Query> object.
1491
Return value is L<DBIx::Custom::Result> if select statement is executed,
1492
or the count of affected rows if insert, update, delete statement is executed.
version 0.0901
yuki-kimoto authored on 2009-12-17
1493

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

            
cleanup
yuki-kimoto authored on 2010-08-05
1496
    $dbi->delete(table  => $table,
1497
                 where  => \%where,
1498
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1499
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1500
                 query  => 1);
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1501

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1502
Execute delete statement.
1503
C<delete> method have C<table>, C<where>, C<append>, and C<filter> arguments.
1504
C<table> is a table name.
1505
C<where> is where clause. this must be hash reference.
1506
C<append> is a string added at the end of the SQL statement.
1507
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1508
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1509
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-08-09
1510
Return value of C<delete()> is the count of affected rows.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1511

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

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

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

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1521
=head3 C<(experimental) delete_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1522

            
1523
To delete row by using primary key, use C<delete_at()>
1524

            
1525
    $dbi->delete_at(
1526
        table => 'book',
1527
        primary_key => ['id'],
1528
        where => ['123']
1529
    );
1530

            
1531
In this example, row which id column is 123 is deleted.
1532
NOTE that you must pass array reference as C<where>.
1533

            
1534
You can also write arguments like this.
1535

            
1536
    $dbi->delete_at(
1537
        table => 'book',
1538
        primary_key => ['id'],
1539
        param => {id => '123'}
1540
    );
1541

            
cleanup
yuki-kimoto authored on 2010-10-17
1542
=head2 C<insert>
1543

            
1544
    $dbi->insert(table  => $table, 
1545
                 param  => \%param,
1546
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1547
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1548
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1549

            
1550
Execute insert statement.
1551
C<insert> method have C<table>, C<param>, C<append>
1552
and C<filter> arguments.
1553
C<table> is a table name.
1554
C<param> is the pairs of column name value. this must be hash reference.
1555
C<append> is a string added at the end of the SQL statement.
1556
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1557
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1558
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1559
This is overwrites C<default_bind_filter>.
1560
Return value of C<insert()> is the count of affected rows.
1561

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1562
=head3 C<(experimental) insert_at()>
1563

            
1564
To insert row by using primary key, use C<insert_at()>
1565

            
1566
    $dbi->insert_at(
1567
        table => 'book',
1568
        primary_key => ['id'],
1569
        where => ['123'],
1570
        param => {name => 'Ken'}
1571
    );
1572

            
1573
In this example, row which id column is 123 is inserted.
1574
NOTE that you must pass array reference as C<where>.
1575
If C<param> contains primary key,
1576
the key and value is delete from C<param>.
1577

            
pod fix
Yuki Kimoto authored on 2011-01-21
1578
=head2 C<(experimental) each_column>
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1579

            
pod fix
Yuki Kimoto authored on 2011-01-21
1580
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1581
        sub {
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1582
            my ($self, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1583
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1584
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
1585
            
1586
            if ($type eq 'DATE') {
1587
                # ...
1588
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1589
        }
1590
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1591
Get column informations from database.
1592
Argument is callback.
1593
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1594
Callback receive four arguments, dbi object, table name,
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1595
column name and column information.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1596

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1597
=head2 C<(experimental) include_model>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1598

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1599
    $dbi->include_model(
1600
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1601
            'book', 'person', 'company'
1602
        ]
1603
    );
1604

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1605
Include models. First argument is name space.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1606
Second argument is array reference of class base names.
1607

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1608
If you don't specify second argument, All models under name space is
1609
included.
1610

            
1611
    $dbi->include_model('MyModel');
1612

            
1613
Note that in this case name spece module is needed.
1614

            
1615
    # MyModel.pm
1616
    package MyModel;
1617
    
1618
    use base 'DBIx::Custom::Model';
1619

            
1620
The following model is instantiated and included.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1621

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1622
    MyModel::book
1623
    MyModel::person
1624
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1625

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1626
You can get these instance by C<model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1627

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1628
    my $book_model = $dbi->model('book');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1629

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1630
If you want to other name as model class,
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1631
you can do like this.
1632

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1633
    $dbi->include_model(
1634
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1635
            {'book' => 'Book'},
1636
            {'person' => 'Person'}
1637
        ]
1638
    );
1639

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1640
=head2 C<(experimental) method>
1641

            
1642
    $dbi->method(
1643
        update_or_insert => sub {
1644
            my $self = shift;
1645
            # do something
1646
        },
1647
        find_or_create   => sub {
1648
            my $self = shift;
1649
            # do something
1650
        }
1651
    );
1652

            
1653
Register method. These method is called from L<DBIx::Custom> object directory.
1654

            
1655
    $dbi->update_or_insert;
1656
    $dbi->find_or_create;
1657

            
1658
=head2 C<new>
1659

            
1660
    my $dbi = DBIx::Custom->connect(data_source => "dbi:mysql:database=dbname",
1661
                                    user => 'ken', password => '!LFKD%$&');
1662

            
1663
Create a new L<DBIx::Custom> object.
1664

            
1665
=head2 C<(experimental) not_exists>
1666

            
1667
    my $not_exists = $dbi->not_exists;
1668

            
1669
Get DBIx::Custom::NotExists object.
experimental extended select...
Yuki Kimoto authored on 2011-01-17
1670

            
cleanup
yuki-kimoto authored on 2010-10-17
1671
=head2 C<register_filter>
1672

            
1673
    $dbi->register_filter(%filters);
1674
    $dbi->register_filter(\%filters);
1675
    
1676
Register filter. Registered filters is available in the following attributes
1677
or arguments.
1678

            
1679
=over 4
1680

            
1681
=item *
1682

            
1683
C<filter> argument of C<insert()>, C<update()>,
1684
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1685
methods
1686

            
1687
=item *
1688

            
1689
C<execute()> method
1690

            
1691
=item *
1692

            
1693
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1694

            
1695
=item *
1696

            
1697
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1698

            
1699
=back
1700

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1701
=head2 C<register_tag>
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1702

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1703
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1704
        limit => sub {
1705
            ...;
1706
        }
1707
    );
1708

            
cleanup
Yuki Kimoto authored on 2011-01-25
1709
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1710

            
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1711
=head2 C<rollback>
cleanup
yuki-kimoto authored on 2010-10-17
1712

            
1713
    $dbi->rollback;
1714

            
1715
Rollback transaction.
1716
This is same as L<DBI>'s C<rollback>.
1717

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1718
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1719
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
1720
    my $result = $dbi->select(
1721
        table     => $table,
1722
        column    => [@column],
1723
        where     => \%where,
1724
        append    => $append,
1725
        relation  => \%relation,
1726
        filter    => \%filter,
1727
        query     => 1,
1728
        selection => $selection
1729
    );
update document
yuki-kimoto authored on 2009-11-19
1730

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1731
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1732
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1733
C<relation> and C<filter> arguments.
1734
C<table> is a table name.
select method column option ...
Yuki Kimoto authored on 2011-02-22
1735
C<column> is column names. this is array reference or string.
cleanup
yuki-kimoto authored on 2010-08-09
1736
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1737
C<append> is a string added at the end of the SQL statement.
1738
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1739
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1740
default to 0. This is experimental.
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1741
C<selection> is string of column name and tables. This is experimental
1742

            
1743
    selection => 'name, location.name as location_name ' .
1744
                 'from company inner join location'
update document
yuki-kimoto authored on 2009-11-19
1745

            
cleanup
yuki-kimoto authored on 2010-08-09
1746
First element is a string. it contains tags,
1747
such as "{= title} or {like author}".
1748
Second element is paramters.
1749

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1750
=head3 C<(experimental) select_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1751

            
1752
To select row by using primary key, use C<select_at()>.
1753

            
1754
    $dbi->select_at(table => 'book', primary_key => ['id'], where => ['123']);
1755

            
1756
In this example, row which id colunm is 123 is selected.
1757
NOTE that you must pass array reference as C<where>.
1758

            
cleanup
yuki-kimoto authored on 2010-10-17
1759
=head2 C<update>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1760

            
cleanup
yuki-kimoto authored on 2010-10-17
1761
    $dbi->update(table  => $table, 
1762
                 param  => \%params,
1763
                 where  => \%where,
1764
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1765
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1766
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1767

            
cleanup
yuki-kimoto authored on 2010-10-17
1768
Execute update statement.
1769
C<update> method have C<table>, C<param>, C<where>, C<append>
1770
and C<filter> arguments.
1771
C<table> is a table name.
1772
C<param> is column-value pairs. this must be hash reference.
1773
C<where> is where clause. this must be hash reference.
1774
C<append> is a string added at the end of the SQL statement.
1775
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1776
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1777
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1778
This is overwrites C<default_bind_filter>.
1779
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1780

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1781
=head2 C<(experimental) model>
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1782

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1783
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1784
        insert => sub { ... },
1785
        update => sub { ... }
1786
    );
1787
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1788
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1789

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1790
Set and get a L<DBIx::Custom::Model> object,
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1791

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1792
=head2 C<(experimental) setup_model>
1793

            
1794
    $dbi->setup_model;
1795

            
1796
Setup all model objects.
1797
C<columns> and C<primary_key> is automatically set.
1798

            
cleanup
yuki-kimoto authored on 2010-10-17
1799
=head2 C<update_all>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1800

            
cleanup
yuki-kimoto authored on 2010-10-17
1801
    $dbi->update_all(table  => $table, 
1802
                     param  => \%params,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1803
                     filter => \@filter,
cleanup
yuki-kimoto authored on 2010-10-17
1804
                     append => $append);
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1805

            
cleanup
yuki-kimoto authored on 2010-10-17
1806
Execute update statement to update all rows.
1807
Arguments is same as C<update> method,
1808
except that C<update_all> don't have C<where> argument.
1809
Return value of C<update_all()> is the count of affected rows.
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1810

            
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
1811
=head3 C<(experimental) update_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1812

            
1813
To update row by using primary key, use C<update_at()>
1814

            
1815
    $dbi->update_at(
1816
        table => 'book',
1817
        primary_key => ['id'],
1818
        where => ['123'],
1819
        param => {name => 'Ken'}
1820
    );
1821

            
1822
In this example, row which id column is 123 is updated.
1823
NOTE that you must pass array reference as C<where>.
1824
If C<param> contains primary key,
1825
the key and value is delete from C<param>.
1826

            
fix tests
Yuki Kimoto authored on 2011-01-18
1827
=head2 C<(experimental) where>
1828

            
1829
    my $where = $dbi->where;
1830

            
1831
Create a new L<DBIx::Custom::Where> object.
1832

            
cleanup
Yuki Kimoto authored on 2011-01-25
1833
=head2 C<cache_method>
cleanup
Yuki Kimoto authored on 2011-01-12
1834

            
1835
    $dbi          = $dbi->cache_method(\&cache_method);
1836
    $cache_method = $dbi->cache_method
1837

            
1838
Method to set and get caches.
1839

            
cleanup
Yuki Kimoto authored on 2011-01-25
1840
=head1 Tags
1841

            
1842
The following tags is available.
1843

            
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1844
=head2 C<(experimental) table>
add table tag
Yuki Kimoto authored on 2011-02-09
1845

            
1846
Table tag
1847

            
1848
    {table TABLE}    ->    TABLE
1849

            
1850
This is used to teach what is applied table to C<execute()>.
1851

            
cleanup
Yuki Kimoto authored on 2011-01-25
1852
=head2 C<?>
1853

            
1854
Placeholder tag.
1855

            
1856
    {? NAME}    ->   ?
1857

            
1858
=head2 C<=>
1859

            
1860
Equal tag.
1861

            
1862
    {= NAME}    ->   NAME = ?
1863

            
1864
=head2 C<E<lt>E<gt>>
1865

            
1866
Not equal tag.
1867

            
1868
    {<> NAME}   ->   NAME <> ?
1869

            
1870
=head2 C<E<lt>>
1871

            
1872
Lower than tag
1873

            
1874
    {< NAME}    ->   NAME < ?
1875

            
1876
=head2 C<E<gt>>
1877

            
1878
Greater than tag
1879

            
1880
    {> NAME}    ->   NAME > ?
1881

            
1882
=head2 C<E<gt>=>
1883

            
1884
Greater than or equal tag
1885

            
1886
    {>= NAME}   ->   NAME >= ?
1887

            
1888
=head2 C<E<lt>=>
1889

            
1890
Lower than or equal tag
1891

            
1892
    {<= NAME}   ->   NAME <= ?
1893

            
1894
=head2 C<like>
1895

            
1896
Like tag
1897

            
1898
    {like NAME}   ->   NAME like ?
1899

            
1900
=head2 C<in>
1901

            
1902
In tag.
1903

            
1904
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1905

            
1906
=head2 C<insert_param>
1907

            
1908
Insert parameter tag.
1909

            
1910
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1911

            
1912
=head2 C<update_param>
1913

            
1914
Updata parameter tag.
1915

            
1916
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1917

            
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
1918
=head1 STABILITY
1919

            
cleanup
Yuki Kimoto authored on 2011-01-25
1920
L<DBIx::Custom> is stable. APIs keep backword compatible
1921
except experimental one in the feature.
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
1922

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

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

            
1927
C<< <kimoto.yuki at gmail.com> >>
1928

            
1929
L<http://github.com/yuki-kimoto/DBIx-Custom>
1930

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1931
=head1 AUTHOR
1932

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
1937
Copyright 2009-2011 Yuki Kimoto, all rights reserved.
packaging one directory
yuki-kimoto authored on 2009-11-16
1938

            
1939
This program is free software; you can redistribute it and/or modify it
1940
under the same terms as Perl itself.
1941

            
1942
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1943

            
1944