DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
1962 lines | 50.935kb
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
    
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
541
    # Insert paramter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
542
    my @tag;
543
    push @tag, '{insert_param';
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
544
    my $safety = $self->safety_column_name;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
545
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
546
        croak qq{"$column" is not safety column name}
547
          unless $column =~ /$safety/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
548
        push @tag, $column;
549
    }
550
    push @tag, '}';
551
    
552
    return join ' ', @tag;
553
}
554

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1058
sub update_param {
1059
    my ($self, $param) = @_;
1060
    
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1061
    # Update parameter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1062
    my @tag;
1063
    push @tag, '{update_param';
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1064
    my $safety = $self->safety_column_name;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1065
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1066
        croak qq{"$column" is not safety column name}
1067
          unless $column =~ /$safety/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1068
        push @tag, $column;
1069
    }
1070
    push @tag, '}';
1071
    
1072
    return join ' ', @tag;
1073
}
1074

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

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1218
=head1 NAME
1219

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

            
1222
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1223

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

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

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

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

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

            
1277
    # Get DBI object
1278
    my $dbh = $dbi->dbh;
1279

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

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

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

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

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

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

            
1314
=head1 GUIDE
1315

            
1316
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
1317

            
1318
=head1 EXAMPLES
1319

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1352
DBI options.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1353

            
1354
Each option specified can ovewrite C<default_dbi_option>.
1355

            
1356
C<connect()> method use this value to connect the database.
1357

            
1358

            
1359
=head2 C<default_dbi_option>
1360

            
1361
    my $default_dbi_option = $dbi->default_dbi_option;
1362
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1363

            
1364
DBI default options.
1365

            
1366
    RaiseError => 1,
1367
    PrintError => 0,
1368
    AutoCommit => 1,
1369

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

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

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

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

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1379
Filters
1380

            
1381
=head2 C<(experimental) models>
1382

            
1383
    my $models = $dbi->models;
1384
    $dbi       = $dbi->models(\%models);
1385

            
1386
Models
1387

            
cleanup
yuki-kimoto authored on 2010-10-17
1388
=head2 C<password>
1389

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

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

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

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

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

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

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

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

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

            
1415
    my $safety_column_name = $self->safety_column_name;
1416
    $dbi                   = $self->safety_column_name($name);
1417

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

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

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

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

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

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

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

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

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

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

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

            
1457
You can use three name as column name.
1458

            
1459
    1. column        : author
1460
    2. table.column  : book.author
1461
    3. table__column : book__author
1462

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1525
To delete row by using primary key, use C<delete_at()>
1526

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

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

            
1536
You can also write arguments like this.
1537

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1544
=head2 C<insert>
1545

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

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

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

            
1566
To insert row by using primary key, use C<insert_at()>
1567

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

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

            
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1580
=head2 C<(experimental) insert_param>
1581

            
1582
    my $insert_param = $dbi->insert_param({title => 'a', age => 2});
1583

            
1584
Create insert parameter tag.
1585

            
1586
    {title => 'a', age => 2}   ->   {insert_param title age}
1587

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1590
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1591
        sub {
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1592
            my ($self, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1593
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1594
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
1595
            
1596
            if ($type eq 'DATE') {
1597
                # ...
1598
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1599
        }
1600
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1601
Get column informations from database.
1602
Argument is callback.
1603
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1604
Callback receive four arguments, dbi object, table name,
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1605
column name and column information.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1606

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1609
    $dbi->include_model(
1610
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1611
            'book', 'person', 'company'
1612
        ]
1613
    );
1614

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

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

            
1621
    $dbi->include_model('MyModel');
1622

            
1623
Note that in this case name spece module is needed.
1624

            
1625
    # MyModel.pm
1626
    package MyModel;
1627
    
1628
    use base 'DBIx::Custom::Model';
1629

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1632
    MyModel::book
1633
    MyModel::person
1634
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1635

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1643
    $dbi->include_model(
1644
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1645
            {'book' => 'Book'},
1646
            {'person' => 'Person'}
1647
        ]
1648
    );
1649

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

            
1652
    $dbi->method(
1653
        update_or_insert => sub {
1654
            my $self = shift;
1655
            # do something
1656
        },
1657
        find_or_create   => sub {
1658
            my $self = shift;
1659
            # do something
1660
        }
1661
    );
1662

            
1663
Register method. These method is called from L<DBIx::Custom> object directory.
1664

            
1665
    $dbi->update_or_insert;
1666
    $dbi->find_or_create;
1667

            
1668
=head2 C<new>
1669

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

            
1673
Create a new L<DBIx::Custom> object.
1674

            
1675
=head2 C<(experimental) not_exists>
1676

            
1677
    my $not_exists = $dbi->not_exists;
1678

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1681
=head2 C<register_filter>
1682

            
1683
    $dbi->register_filter(%filters);
1684
    $dbi->register_filter(\%filters);
1685
    
1686
Register filter. Registered filters is available in the following attributes
1687
or arguments.
1688

            
1689
=over 4
1690

            
1691
=item *
1692

            
1693
C<filter> argument of C<insert()>, C<update()>,
1694
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1695
methods
1696

            
1697
=item *
1698

            
1699
C<execute()> method
1700

            
1701
=item *
1702

            
1703
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1704

            
1705
=item *
1706

            
1707
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1708

            
1709
=back
1710

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1713
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1714
        limit => sub {
1715
            ...;
1716
        }
1717
    );
1718

            
cleanup
Yuki Kimoto authored on 2011-01-25
1719
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1720

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

            
1723
    $dbi->rollback;
1724

            
1725
Rollback transaction.
1726
This is same as L<DBI>'s C<rollback>.
1727

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1728
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1729
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
1730
    my $result = $dbi->select(
1731
        table     => $table,
1732
        column    => [@column],
1733
        where     => \%where,
1734
        append    => $append,
1735
        relation  => \%relation,
1736
        filter    => \%filter,
1737
        query     => 1,
1738
        selection => $selection
1739
    );
update document
yuki-kimoto authored on 2009-11-19
1740

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

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1756
First element is a string. it contains tags,
1757
such as "{= title} or {like author}".
1758
Second element is paramters.
1759

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1771
    $dbi->update(table  => $table, 
1772
                 param  => \%params,
1773
                 where  => \%where,
1774
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1775
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1776
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1777

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

            
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1791
=head2 C<(experimental) update_param>
1792

            
1793
    my $update_param = $dbi->update_param({title => 'a', age => 2});
1794

            
1795
Create update parameter tag.
1796

            
1797
    {title => 'a', age => 2}   ->   {update_param title age}
1798

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1801
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1802
        insert => sub { ... },
1803
        update => sub { ... }
1804
    );
1805
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1806
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1807

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

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

            
1812
    $dbi->setup_model;
1813

            
1814
Setup all model objects.
1815
C<columns> and C<primary_key> is automatically set.
1816

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

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

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

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

            
1831
To update row by using primary key, use C<update_at()>
1832

            
1833
    $dbi->update_at(
1834
        table => 'book',
1835
        primary_key => ['id'],
1836
        where => ['123'],
1837
        param => {name => 'Ken'}
1838
    );
1839

            
1840
In this example, row which id column is 123 is updated.
1841
NOTE that you must pass array reference as C<where>.
1842
If C<param> contains primary key,
1843
the key and value is delete from C<param>.
1844

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

            
1847
    my $where = $dbi->where;
1848

            
1849
Create a new L<DBIx::Custom::Where> object.
1850

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

            
1853
    $dbi          = $dbi->cache_method(\&cache_method);
1854
    $cache_method = $dbi->cache_method
1855

            
1856
Method to set and get caches.
1857

            
cleanup
Yuki Kimoto authored on 2011-01-25
1858
=head1 Tags
1859

            
1860
The following tags is available.
1861

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

            
1864
Table tag
1865

            
1866
    {table TABLE}    ->    TABLE
1867

            
1868
This is used to teach what is applied table to C<execute()>.
1869

            
cleanup
Yuki Kimoto authored on 2011-01-25
1870
=head2 C<?>
1871

            
1872
Placeholder tag.
1873

            
1874
    {? NAME}    ->   ?
1875

            
1876
=head2 C<=>
1877

            
1878
Equal tag.
1879

            
1880
    {= NAME}    ->   NAME = ?
1881

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

            
1884
Not equal tag.
1885

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

            
1888
=head2 C<E<lt>>
1889

            
1890
Lower than tag
1891

            
1892
    {< NAME}    ->   NAME < ?
1893

            
1894
=head2 C<E<gt>>
1895

            
1896
Greater than tag
1897

            
1898
    {> NAME}    ->   NAME > ?
1899

            
1900
=head2 C<E<gt>=>
1901

            
1902
Greater than or equal tag
1903

            
1904
    {>= NAME}   ->   NAME >= ?
1905

            
1906
=head2 C<E<lt>=>
1907

            
1908
Lower than or equal tag
1909

            
1910
    {<= NAME}   ->   NAME <= ?
1911

            
1912
=head2 C<like>
1913

            
1914
Like tag
1915

            
1916
    {like NAME}   ->   NAME like ?
1917

            
1918
=head2 C<in>
1919

            
1920
In tag.
1921

            
1922
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1923

            
1924
=head2 C<insert_param>
1925

            
1926
Insert parameter tag.
1927

            
1928
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1929

            
1930
=head2 C<update_param>
1931

            
1932
Updata parameter tag.
1933

            
1934
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1935

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

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

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

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

            
1945
C<< <kimoto.yuki at gmail.com> >>
1946

            
1947
L<http://github.com/yuki-kimoto/DBIx-Custom>
1948

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1949
=head1 AUTHOR
1950

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

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

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

            
1957
This program is free software; you can redistribute it and/or modify it
1958
under the same terms as Perl itself.
1959

            
1960
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1961

            
1962