DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
2005 lines | 52.01kb
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
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
616
  = map { $_ => 1 } qw/table column where append relation filter query selection join/;
617

            
618
sub _need_tables {
619
    my ($self, $tree, $need_tables, $tables) = @_;
620
    
621
    foreach my $table (@$tables) {
622
        
623
        if ($tree->{$table}) {
624
            $need_tables->{$table} = 1;
625
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
626
        }
627
    }
628
}
refactoring select
yuki-kimoto authored on 2010-04-28
629

            
packaging one directory
yuki-kimoto authored on 2009-11-16
630
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
631
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
632
    
refactoring select
yuki-kimoto authored on 2010-04-28
633
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
634
    foreach my $name (keys %args) {
add tests
yuki-kimoto authored on 2010-08-10
635
        croak qq{"$name" is invalid argument}
refactoring select
yuki-kimoto authored on 2010-04-28
636
          unless $VALID_SELECT_ARGS{$name};
637
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
638
    
refactoring select
yuki-kimoto authored on 2010-04-28
639
    # Arguments
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
640
    my $table = $args{table};
641
    my $tables = ref $table eq 'ARRAY' ? $table
642
               : defined $table ? [$table]
643
               : [];
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
644
    my $columns   = $args{column} || [];
select method column option ...
Yuki Kimoto authored on 2011-02-22
645
    $columns = [$columns] unless ref $columns;
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
646
    my $selection = $args{selection} || '';
647
    my $where     = $args{where} || {};
648
    my $append    = $args{append};
649
    my $filter    = $args{filter};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
650
    my $join =     $args{join} || [];
651
    croak qq{"join" must be array reference}
652
      unless ref $join eq 'ARRAY';
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
653
    
cleanup
Yuki Kimoto authored on 2011-03-08
654
    my @join_tables;
655
    unshift @join_tables, $tables->[-1];
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
656
    
cleanup
Yuki Kimoto authored on 2011-03-08
657
    # Relation table(DEPRECATED!);
658
    $self->_add_relation_table($args{relation}, $tables);
packaging one directory
yuki-kimoto authored on 2009-11-16
659
    
cleanup
Yuki Kimoto authored on 2011-01-27
660
    # SQL stack
661
    my @sql;
662
    
663
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
664
    
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
665
    if ($selection) {
666
        push @sql, $selection;
cleanup
Yuki Kimoto authored on 2011-03-08
667
        push @join_tables, @{$self->_tables($selection)};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
668
    }
669
    else {
670
        # Column clause
671
        if (@$columns) {
672
            foreach my $column (@$columns) {
cleanup
Yuki Kimoto authored on 2011-03-08
673
                push @join_tables, @{$self->_tables($column)};
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
674
                push @sql, ($column, ',');
675
            }
676
            pop @sql if $sql[-1] eq ',';
677
        }
678
        else { push @sql, '*' }
679
        
680
        # Table
681
        croak qq{"table" option must be specified} unless @$tables;
682
        push @sql, 'from';
683
        foreach my $table (@$tables) {
684
            push @sql, ($table, ',');
packaging one directory
yuki-kimoto authored on 2009-11-16
685
        }
cleanup
Yuki Kimoto authored on 2011-01-27
686
        pop @sql if $sql[-1] eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
687
    }
688
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
689
    # Where
690
    my $w;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
691
    if (ref $where eq 'HASH') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
692
        my $clause = ['and'];
693
        push @$clause, "{= $_}" for keys %$where;
694
        $w = $self->where;
695
        $w->clause($clause);
696
        $w->param($where);
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
697
    }
698
    elsif (ref $where eq 'DBIx::Custom::Where') {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
699
        $w = $where;
700
        $where = $w->param;
packaging one directory
yuki-kimoto authored on 2009-11-16
701
    }
702
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
703
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
704
      unless ref $w eq 'DBIx::Custom::Where';
705
    
706
    # String where
707
    my $swhere = "$w";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
708
    
709
    # Table name in Where
cleanup
Yuki Kimoto authored on 2011-03-08
710
    unshift @join_tables, @{$self->_tables($swhere)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
711
    
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
712
    # Join
713
    if (@$join) {
714
        my $tree = {};
715
        
716
        for (my $i = 0; $i < @$join; $i++) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
717
            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
718
            my $join_clause = $join->[$i];
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
719
            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
720
            if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\./) {
721
                
722
                my $table1 = $1;
723
                my $table2 = $2;
724
                
725
                croak qq{right side table of "$join_clause" must be uniq}
726
                  if exists $tree->{$table2};
727
                
728
                $tree->{$table2}
729
                  = {position => $i, parent => $table1, join => $join_clause};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
730
            }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
731
            else {
732
                croak qq{join "$join_clause" must be two table name};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
733
            }
734
        }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
735
        
736
        my $need_tables = {};
737
        $self->_need_tables($tree, $need_tables, \@join_tables);
738
        
739
        
740
        my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
741

            
742
        foreach my $need_table (@need_tables) {
743
            push @sql, $tree->{$need_table}{join};
744
        }
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
745
    }
746
    
747
    # Add where
cleanup
Yuki Kimoto authored on 2011-01-27
748
    push @sql, $swhere;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
749
    
cleanup
Yuki Kimoto authored on 2011-03-08
750
    # Relation(DEPRECATED!);
751
    $self->_push_relation(\@sql, $tables, $args{relation}, $swhere eq '' ? 1 : 0);
752
    
cleanup
Yuki Kimoto authored on 2011-01-27
753
    # Append statement
754
    push @sql, $append if $append;
755
    
756
    # SQL
757
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
758
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
759
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
760
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
761
    return $query if $args{query};
762
    
cleanup
Yuki Kimoto authored on 2011-03-08
763
    unshift @$tables, @join_tables;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
764
    
packaging one directory
yuki-kimoto authored on 2009-11-16
765
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
766
    my $result = $self->execute(
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
767
        $query, param  => $where, filter => $filter,
768
        table => $tables);
packaging one directory
yuki-kimoto authored on 2009-11-16
769
    
770
    return $result;
771
}
772

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-08
1161

            
1162

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-08
1219
# DEPRECATED!
1220
sub _push_relation {
1221
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1222
    
1223
    if (keys %{$relation || {}}) {
1224
        push @$sql, $need_where ? 'where' : 'and';
1225
        foreach my $rcolumn (keys %$relation) {
1226
            my $table1 = (split (/\./, $rcolumn))[0];
1227
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1228
            push @$tables, ($table1, $table2);
1229
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1230
        }
1231
    }
1232
    pop @$sql if $sql->[-1] eq 'and';    
1233
}
1234

            
1235
# DEPRECATED!
1236
sub _add_relation_table {
1237
    my ($self, $relation, $tables) = @_;
1238
    
1239
    if (keys %{$relation || {}}) {
1240
        foreach my $rcolumn (keys %$relation) {
1241
            my $table1 = (split (/\./, $rcolumn))[0];
1242
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1243
            my $table1_exists;
1244
            my $table2_exists;
1245
            foreach my $table (@$tables) {
1246
                $table1_exists = 1 if $table eq $table1;
1247
                $table2_exists = 1 if $table eq $table2;
1248
            }
1249
            unshift @$tables, $table1 unless $table1_exists;
1250
            unshift @$tables, $table2 unless $table2_exists;
1251
        }
1252
    }
1253
}
1254

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1257
=head1 NAME
1258

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

            
1261
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1262

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1268
    # Insert 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1269
    $dbi->insert(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1270
                 param  => {title => 'Perl', author => 'Ken'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1271
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1272
    
1273
    # Update 
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1274
    $dbi->update(table  => 'book', 
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1275
                 param  => {title => 'Perl', author => 'Ken'}, 
removed reconnect method
yuki-kimoto authored on 2010-05-28
1276
                 where  => {id => 5},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1277
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1278
    
1279
    # Update all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1280
    $dbi->update_all(table  => 'book',
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1281
                     param  => {title => 'Perl'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1282
                     filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1283
    
1284
    # Delete
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1285
    $dbi->delete(table  => 'book',
removed reconnect method
yuki-kimoto authored on 2010-05-28
1286
                 where  => {author => 'Ken'},
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1287
                 filter => [title => 'to_something']);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1288
    
1289
    # Delete all
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1290
    $dbi->delete_all(table => 'book');
cleanup
yuki-kimoto authored on 2010-08-05
1291

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1292
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
1293
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1294
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
1295
        column => [qw/author title/],
1296
        where  => {author => 'Ken'},
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1297
        relation => {'book.id' => 'rental.book_id'},
updated document
yuki-kimoto authored on 2010-08-08
1298
        append => 'order by id limit 5',
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1299
        filter => [title => 'to_something']
added commit method
yuki-kimoto authored on 2010-05-27
1300
    );
cleanup
yuki-kimoto authored on 2010-08-05
1301

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

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

            
1316
    # Get DBI object
1317
    my $dbh = $dbi->dbh;
1318

            
removed register_format()
yuki-kimoto authored on 2010-05-26
1319
    # Fetch
1320
    while (my $row = $result->fetch) {
1321
        # ...
1322
    }
1323
    
1324
    # Fetch hash
1325
    while (my $row = $result->fetch_hash) {
1326
        
1327
    }
1328
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1329
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1330

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

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

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

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

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

            
1353
=head1 GUIDE
1354

            
1355
L<DBIx::Custom::Guide> - L<DBIx::Custom> complete guide
1356

            
1357
=head1 EXAMPLES
1358

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1391
DBI options.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1392

            
1393
Each option specified can ovewrite C<default_dbi_option>.
1394

            
1395
C<connect()> method use this value to connect the database.
1396

            
1397

            
1398
=head2 C<default_dbi_option>
1399

            
1400
    my $default_dbi_option = $dbi->default_dbi_option;
1401
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1402

            
1403
DBI default options.
1404

            
1405
    RaiseError => 1,
1406
    PrintError => 0,
1407
    AutoCommit => 1,
1408

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

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

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

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

            
add models() attribute
Yuki Kimoto authored on 2011-02-21
1418
Filters
1419

            
1420
=head2 C<(experimental) models>
1421

            
1422
    my $models = $dbi->models;
1423
    $dbi       = $dbi->models(\%models);
1424

            
1425
Models
1426

            
cleanup
yuki-kimoto authored on 2010-10-17
1427
=head2 C<password>
1428

            
1429
    my $password = $dbi->password;
1430
    $dbi         = $dbi->password('lkj&le`@s');
1431

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

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

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

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

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

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

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

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

            
1454
    my $safety_column_name = $self->safety_column_name;
1455
    $dbi                   = $self->safety_column_name($name);
1456

            
1457
Safety column name regex.
1458
Default is qr/^[\w\.]*$/
1459

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1462
    my $user = $dbi->user;
1463
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1464

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1476
    $dbi->apply_filter(
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1477
        $table,
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1478
        $column1 => {in => $infilter1, out => $outfilter1, end => $endfilter1}
1479
        $column2 => {in => $infilter2, out => $outfilter2, end =. $endfilter2}
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1480
        ...,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1481
    );
1482

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

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

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

            
1496
You can use three name as column name.
1497

            
1498
    1. column        : author
1499
    2. table.column  : book.author
1500
    3. table__column : book__author
1501

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
1564
To delete row by using primary key, use C<delete_at()>
1565

            
1566
    $dbi->delete_at(
1567
        table => 'book',
1568
        primary_key => ['id'],
1569
        where => ['123']
1570
    );
1571

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

            
1575
You can also write arguments like this.
1576

            
1577
    $dbi->delete_at(
1578
        table => 'book',
1579
        primary_key => ['id'],
1580
        param => {id => '123'}
1581
    );
1582

            
cleanup
yuki-kimoto authored on 2010-10-17
1583
=head2 C<insert>
1584

            
1585
    $dbi->insert(table  => $table, 
1586
                 param  => \%param,
1587
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1588
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1589
                 query  => 1);
cleanup
yuki-kimoto authored on 2010-10-17
1590

            
1591
Execute insert statement.
1592
C<insert> method have C<table>, C<param>, C<append>
1593
and C<filter> arguments.
1594
C<table> is a table name.
1595
C<param> is the pairs of column name value. this must be hash reference.
1596
C<append> is a string added at the end of the SQL statement.
1597
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1598
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1599
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1600
This is overwrites C<default_bind_filter>.
1601
Return value of C<insert()> is the count of affected rows.
1602

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

            
1605
To insert row by using primary key, use C<insert_at()>
1606

            
1607
    $dbi->insert_at(
1608
        table => 'book',
1609
        primary_key => ['id'],
1610
        where => ['123'],
1611
        param => {name => 'Ken'}
1612
    );
1613

            
1614
In this example, row which id column is 123 is inserted.
1615
NOTE that you must pass array reference as C<where>.
1616
If C<param> contains primary key,
1617
the key and value is delete from C<param>.
1618

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

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

            
1623
Create insert parameter tag.
1624

            
1625
    {title => 'a', age => 2}   ->   {insert_param title age}
1626

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

            
pod fix
Yuki Kimoto authored on 2011-01-21
1629
    $dbi->each_column(
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1630
        sub {
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1631
            my ($self, $table, $column, $column_info) = @_;
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1632
            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1633
            my $type = $column_info->{TYPE_NAME};
pod fix
Yuki Kimoto authored on 2011-01-21
1634
            
1635
            if ($type eq 'DATE') {
1636
                # ...
1637
            }
added experimental iterate_a...
Yuki Kimoto authored on 2010-12-22
1638
        }
1639
    );
pod fix
Yuki Kimoto authored on 2011-01-21
1640
Get column informations from database.
1641
Argument is callback.
1642
You can do anything in callback.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1643
Callback receive four arguments, dbi object, table name,
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1644
column name and column information.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1645

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1648
    $dbi->include_model(
1649
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1650
            'book', 'person', 'company'
1651
        ]
1652
    );
1653

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

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

            
1660
    $dbi->include_model('MyModel');
1661

            
1662
Note that in this case name spece module is needed.
1663

            
1664
    # MyModel.pm
1665
    package MyModel;
1666
    
1667
    use base 'DBIx::Custom::Model';
1668

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1671
    MyModel::book
1672
    MyModel::person
1673
    MyModel::company
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1674

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

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

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1682
    $dbi->include_model(
1683
        'MyModel' => [
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
1684
            {'book' => 'Book'},
1685
            {'person' => 'Person'}
1686
        ]
1687
    );
1688

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

            
1691
    $dbi->method(
1692
        update_or_insert => sub {
1693
            my $self = shift;
1694
            # do something
1695
        },
1696
        find_or_create   => sub {
1697
            my $self = shift;
1698
            # do something
1699
        }
1700
    );
1701

            
1702
Register method. These method is called from L<DBIx::Custom> object directory.
1703

            
1704
    $dbi->update_or_insert;
1705
    $dbi->find_or_create;
1706

            
1707
=head2 C<new>
1708

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

            
1712
Create a new L<DBIx::Custom> object.
1713

            
1714
=head2 C<(experimental) not_exists>
1715

            
1716
    my $not_exists = $dbi->not_exists;
1717

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1720
=head2 C<register_filter>
1721

            
1722
    $dbi->register_filter(%filters);
1723
    $dbi->register_filter(\%filters);
1724
    
1725
Register filter. Registered filters is available in the following attributes
1726
or arguments.
1727

            
1728
=over 4
1729

            
1730
=item *
1731

            
1732
C<filter> argument of C<insert()>, C<update()>,
1733
C<update_all()>, C<delete()>, C<delete_all()>, C<select()>
1734
methods
1735

            
1736
=item *
1737

            
1738
C<execute()> method
1739

            
1740
=item *
1741

            
1742
C<default_filter> and C<filter> of C<DBIx::Custom::Query>
1743

            
1744
=item *
1745

            
1746
C<default_filter> and C<filter> of C<DBIx::Custom::Result>
1747

            
1748
=back
1749

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1752
    $dbi->register_tag(
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1753
        limit => sub {
1754
            ...;
1755
        }
1756
    );
1757

            
cleanup
Yuki Kimoto authored on 2011-01-25
1758
Register tag.
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
1759

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

            
1762
    $dbi->rollback;
1763

            
1764
Rollback transaction.
1765
This is same as L<DBI>'s C<rollback>.
1766

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1767
=head2 C<select>
packaging one directory
yuki-kimoto authored on 2009-11-16
1768
    
select method column option ...
Yuki Kimoto authored on 2011-02-22
1769
    my $result = $dbi->select(
1770
        table     => $table,
1771
        column    => [@column],
1772
        where     => \%where,
1773
        append    => $append,
1774
        relation  => \%relation,
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
1775
        join => ['left outer join company on book.company_id = company.id']
select method column option ...
Yuki Kimoto authored on 2011-02-22
1776
        filter    => \%filter,
1777
        query     => 1,
1778
        selection => $selection
1779
    );
update document
yuki-kimoto authored on 2009-11-19
1780

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1781
Execute select statement.
cleanup
yuki-kimoto authored on 2010-08-09
1782
C<select> method have C<table>, C<column>, C<where>, C<append>,
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1783
C<relation> and C<filter> arguments.
1784
C<table> is a table name.
select method column option ...
Yuki Kimoto authored on 2011-02-22
1785
C<column> is column names. this is array reference or string.
cleanup
yuki-kimoto authored on 2010-08-09
1786
C<where> is where clause. this is normally hash reference.
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1787
C<append> is a string added at the end of the SQL statement.
1788
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1789
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1790
default to 0. This is experimental.
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
1791
C<selection> is string of column name and tables. This is experimental
1792

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

            
cleanup
yuki-kimoto authored on 2010-08-09
1796
First element is a string. it contains tags,
1797
such as "{= title} or {like author}".
1798
Second element is paramters.
1799

            
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
1800
C<join> is join clause after from clause.
cleanup
Yuki Kimoto authored on 2011-03-08
1801
This is experimental.
1802

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1814
    $dbi->update(table  => $table, 
1815
                 param  => \%params,
1816
                 where  => \%where,
1817
                 append => $append,
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
1818
                 filter => \@filter,
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1819
                 query  => 1)
removed reconnect method
yuki-kimoto authored on 2010-05-28
1820

            
cleanup
yuki-kimoto authored on 2010-10-17
1821
Execute update statement.
1822
C<update> method have C<table>, C<param>, C<where>, C<append>
1823
and C<filter> arguments.
1824
C<table> is a table name.
1825
C<param> is column-value pairs. this must be hash reference.
1826
C<where> is where clause. this must be hash reference.
1827
C<append> is a string added at the end of the SQL statement.
1828
C<filter> is filters when parameter binding is executed.
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1829
C<query> is if you don't execute sql and get L<DBIx::Custom::Query> object as return value.
1830
default to 0. This is experimental.
cleanup
yuki-kimoto authored on 2010-10-17
1831
This is overwrites C<default_bind_filter>.
1832
Return value of C<update()> is the count of affected rows.
removed reconnect method
yuki-kimoto authored on 2010-05-28
1833

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

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

            
1838
Create update parameter tag.
1839

            
1840
    {title => 'a', age => 2}   ->   {update_param title age}
1841

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

            
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1844
    $dbi->model('book')->method(
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1845
        insert => sub { ... },
1846
        update => sub { ... }
1847
    );
1848
    
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
1849
    my $model = $dbi->model('book');
remove DBIx::Custom::Model
Yuki Kimoto authored on 2011-01-12
1850

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

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

            
1855
    $dbi->setup_model;
1856

            
1857
Setup all model objects.
1858
C<columns> and C<primary_key> is automatically set.
1859

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

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

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

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

            
1874
To update row by using primary key, use C<update_at()>
1875

            
1876
    $dbi->update_at(
1877
        table => 'book',
1878
        primary_key => ['id'],
1879
        where => ['123'],
1880
        param => {name => 'Ken'}
1881
    );
1882

            
1883
In this example, row which id column is 123 is updated.
1884
NOTE that you must pass array reference as C<where>.
1885
If C<param> contains primary key,
1886
the key and value is delete from C<param>.
1887

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

            
1890
    my $where = $dbi->where;
1891

            
1892
Create a new L<DBIx::Custom::Where> object.
1893

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

            
1896
    $dbi          = $dbi->cache_method(\&cache_method);
1897
    $cache_method = $dbi->cache_method
1898

            
1899
Method to set and get caches.
1900

            
cleanup
Yuki Kimoto authored on 2011-01-25
1901
=head1 Tags
1902

            
1903
The following tags is available.
1904

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

            
1907
Table tag
1908

            
1909
    {table TABLE}    ->    TABLE
1910

            
1911
This is used to teach what is applied table to C<execute()>.
1912

            
cleanup
Yuki Kimoto authored on 2011-01-25
1913
=head2 C<?>
1914

            
1915
Placeholder tag.
1916

            
1917
    {? NAME}    ->   ?
1918

            
1919
=head2 C<=>
1920

            
1921
Equal tag.
1922

            
1923
    {= NAME}    ->   NAME = ?
1924

            
1925
=head2 C<E<lt>E<gt>>
1926

            
1927
Not equal tag.
1928

            
1929
    {<> NAME}   ->   NAME <> ?
1930

            
1931
=head2 C<E<lt>>
1932

            
1933
Lower than tag
1934

            
1935
    {< NAME}    ->   NAME < ?
1936

            
1937
=head2 C<E<gt>>
1938

            
1939
Greater than tag
1940

            
1941
    {> NAME}    ->   NAME > ?
1942

            
1943
=head2 C<E<gt>=>
1944

            
1945
Greater than or equal tag
1946

            
1947
    {>= NAME}   ->   NAME >= ?
1948

            
1949
=head2 C<E<lt>=>
1950

            
1951
Lower than or equal tag
1952

            
1953
    {<= NAME}   ->   NAME <= ?
1954

            
1955
=head2 C<like>
1956

            
1957
Like tag
1958

            
1959
    {like NAME}   ->   NAME like ?
1960

            
1961
=head2 C<in>
1962

            
1963
In tag.
1964

            
1965
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
1966

            
1967
=head2 C<insert_param>
1968

            
1969
Insert parameter tag.
1970

            
1971
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
1972

            
1973
=head2 C<update_param>
1974

            
1975
Updata parameter tag.
1976

            
1977
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
1978

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

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

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

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

            
1988
C<< <kimoto.yuki at gmail.com> >>
1989

            
1990
L<http://github.com/yuki-kimoto/DBIx-Custom>
1991

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1992
=head1 AUTHOR
1993

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

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

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

            
2000
This program is free software; you can redistribute it and/or modify it
2001
under the same terms as Perl itself.
2002

            
2003
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2004

            
2005