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

            
cleanup
Yuki Kimoto authored on 2011-03-30
3
our $VERSION = '0.1667';
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

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
22
our @COMMON_ARGS = qw/table query filter type/;
cleanup
Yuki Kimoto authored on 2011-03-21
23

            
fix tests
Yuki Kimoto authored on 2011-01-13
24
__PACKAGE__->attr(
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
25
    [qw/data_source password pid user/],
removed from cache() and cac...
Yuki Kimoto authored on 2011-03-29
26
    cache => 0,
many changed
Yuki Kimoto authored on 2011-01-23
27
    cache_method => sub {
28
        sub {
29
            my $self = shift;
30
            
31
            $self->{_cached} ||= {};
32
            
33
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-03-13
34
                $self->{_cached}{$_[0]} = $_[1];
many changed
Yuki Kimoto authored on 2011-01-23
35
            }
36
            else {
update pod
Yuki Kimoto authored on 2011-03-13
37
                return $self->{_cached}{$_[0]};
many changed
Yuki Kimoto authored on 2011-01-23
38
            }
39
        }
update pod
Yuki Kimoto authored on 2011-03-13
40
    },
41
    dbi_option => sub { {} },
42
    default_dbi_option => sub {
43
        {
44
            RaiseError => 1,
45
            PrintError => 0,
46
            AutoCommit => 1
47
        }
48
    },
fix tests
Yuki Kimoto authored on 2011-01-13
49
    filters => sub {
50
        {
51
            encode_utf8 => sub { encode_utf8($_[0]) },
52
            decode_utf8 => sub { decode_utf8($_[0]) }
53
        }
update pod
Yuki Kimoto authored on 2011-03-13
54
    },
55
    models => sub { {} },
56
    query_builder => sub { DBIx::Custom::QueryBuilder->new },
57
    result_class  => 'DBIx::Custom::Result',
58
    safety_character => '\w',
59
    stash => sub { {} }
fix tests
Yuki Kimoto authored on 2011-01-13
60
);
cleanup
yuki-kimoto authored on 2010-10-17
61

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
145
sub column {
146
    my ($self, $table, $columns) = @_;
added helper method
yuki-kimoto authored on 2010-10-17
147
    
cleanup
Yuki Kimoto authored on 2011-03-21
148
    $columns ||= [];
added helper method
yuki-kimoto authored on 2010-10-17
149
    
cleanup
Yuki Kimoto authored on 2011-03-21
150
    my @column;
151
    push @column, "$table.$_ as ${table}__$_" for @$columns;
152
    
153
    return join (', ', @column);
added helper method
yuki-kimoto authored on 2010-10-17
154
}
155

            
packaging one directory
yuki-kimoto authored on 2009-11-16
156
sub connect {
cleanup
Yuki Kimoto authored on 2011-01-25
157
    my $self = ref $_[0] ? shift : shift->new(@_);;
removed register_format()
yuki-kimoto authored on 2010-05-26
158
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
159
    my $dbh = $self->_connect;
packaging one directory
yuki-kimoto authored on 2009-11-16
160
    
update document
yuki-kimoto authored on 2010-01-30
161
    # Database handle
packaging one directory
yuki-kimoto authored on 2009-11-16
162
    $self->dbh($dbh);
update document
yuki-kimoto authored on 2010-01-30
163
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
164
    # Process ID
165
    $self->pid($$);
166
    
packaging one directory
yuki-kimoto authored on 2009-11-16
167
    return $self;
168
}
169

            
cleanup
yuki-kimoto authored on 2010-10-17
170
sub create_query {
171
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
172
    
cleanup
yuki-kimoto authored on 2010-10-17
173
    # Cache
174
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
175
    
cleanup
yuki-kimoto authored on 2010-10-17
176
    # Create query
177
    my $query;
178
    if ($cache) {
179
        
180
        # Get query
181
        my $q = $self->cache_method->($self, $source);
182
        
183
        # Create query
add table tag
Yuki Kimoto authored on 2011-02-09
184
        if ($q) {
185
            $query = DBIx::Custom::Query->new($q);
186
            $query->filters($self->filters);
187
        }
cleanup
yuki-kimoto authored on 2010-10-17
188
    }
189
    
190
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
191

            
cleanup
yuki-kimoto authored on 2010-10-17
192
        # Create SQL object
193
        my $builder = $self->query_builder;
194
        
195
        # Create query
196
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
197

            
cleanup
yuki-kimoto authored on 2010-10-17
198
        # Cache query
199
        $self->cache_method->($self, $source,
200
                             {sql     => $query->sql, 
add table tag
Yuki Kimoto authored on 2011-02-09
201
                              columns => $query->columns,
202
                              tables  => $query->tables})
cleanup
yuki-kimoto authored on 2010-10-17
203
          if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
204
    }
205
    
cleanup
yuki-kimoto authored on 2010-10-17
206
    # Prepare statement handle
207
    my $sth;
208
    eval { $sth = $self->dbh->prepare($query->{sql})};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
209
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
210
    
cleanup
yuki-kimoto authored on 2010-10-17
211
    # Set statement handle
212
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
213
    
cleanup
Yuki Kimoto authored on 2011-02-09
214
    # Set filters
215
    $query->filters($self->filters);
216
    
cleanup
yuki-kimoto authored on 2010-10-17
217
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
218
}
219

            
update pod
Yuki Kimoto authored on 2011-03-13
220
sub dbh {
221
    my $self = shift;
222

            
223
    if (@_) {
224
        $self->{dbh} = $_[0];
225
        return $self;
226
    }
227
    else {
228
        my $pid = $$;
229
        if ($self->pid eq $pid) {
230
            return $self->{dbh};
231
        }
232
        else {
233
            # Create new connection in child process
234
            croak "Process is forked in transaction"
235
              unless $self->{dbh}->{AutoCommit};
236
            $self->pid($pid);
237
            $self->{dbh}->{InactiveDestroy} = 1;
238
            return $self->{dbh} = $self->_connect;
239
        }
240
    }
241
}
242

            
cleanup
Yuki Kimoto authored on 2011-03-21
243
our %DELETE_ARGS
cleanup
Yuki Kimoto authored on 2011-03-21
244
  = map { $_ => 1 } @COMMON_ARGS, qw/where append allow_delete_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
245

            
cleanup
yuki-kimoto authored on 2010-10-17
246
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
247
    my ($self, %args) = @_;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
248
    
cleanup
Yuki Kimoto authored on 2011-03-09
249
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
250
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
251
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
252
          unless $DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
253
    }
254
    
255
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
256
    my $table = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
257
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
258
    my $where            = delete $args{where} || {};
259
    my $append           = delete $args{append};
260
    my $allow_delete_all = delete $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
261

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
262
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
263
    my $w = $self->_where($where);
264
    $where = $w->param;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
265
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
266
    # String where
267
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
268
    
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
269
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
270
      if $swhere eq '' && !$allow_delete_all;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
271

            
cleanup
Yuki Kimoto authored on 2011-01-27
272
    # SQL stack
273
    my @sql;
274

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
298
our %DELETE_AT_ARGS = (%DELETE_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
299

            
300
sub delete_at {
301
    my ($self, %args) = @_;
302
    
cleanup
Yuki Kimoto authored on 2011-03-09
303
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
304
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
305
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
306
          unless $DELETE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
307
    }
308
    
309
    # Primary key
310
    my $primary_keys = delete $args{primary_key};
311
    $primary_keys = [$primary_keys] unless ref $primary_keys;
312
    
313
    # Where clause
314
    my $where = {};
315
    if (exists $args{where}) {
316
        my $where_columns = delete $args{where};
317
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
318

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

            
added helper method
yuki-kimoto authored on 2010-10-17
338
sub DESTROY { }
339

            
cleanup
Yuki Kimoto authored on 2011-03-21
340
our %EXECUTE_ARGS = map { $_ => 1 } @COMMON_ARGS, 'param';
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
341

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
342
sub create_model {
343
    my $self = shift;
344
    
345
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
346
    $args->{dbi} = $self;
347
    
348
    my $model_class = delete $args->{model_class} || 'DBIx::Custom::Model';
349
    my $model_name  = delete $args->{name};
350
    my $model_table = delete $args->{table};
351
    $model_name ||= $model_table;
352
    
353
    my $model = $model_class->new($args);
354
    $model->name($model_name) unless $model->name;
355
    $model->table($model_table) unless $model->table;
356
    
357
    # Apply filter
358
    croak "$model_class filter must be array reference"
359
      unless ref $model->filter eq 'ARRAY';
360
    $self->apply_filter($model->table, @{$model->filter});
361
    
362
    # Table - Model
363
    croak "Table name is duplicated"
364
      if exists $self->{_model_from}->{$model->table};
365
    $self->{_model_from}->{$model->table} = $model->name;
366

            
367
    # Table alias
368
    $self->{_table_alias} ||= {};
369
    $self->{_table_alias} = {%{$self->{_table_alias}}, %{$model->table_alias}};
370
    
371
    # Set model
372
    $self->model($model->name, $model);
373
    
create_model() return model
Yuki Kimoto authored on 2011-03-29
374
    return $self->model($model->name);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
375
}
376

            
377
sub each_column {
378
    my ($self, $cb) = @_;
379
    
380
    # Iterate all tables
381
    my $sth_tables = $self->dbh->table_info;
382
    while (my $table_info = $sth_tables->fetchrow_hashref) {
383
        
384
        # Table
385
        my $table = $table_info->{TABLE_NAME};
386
        
387
        # Iterate all columns
388
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
389
        while (my $column_info = $sth_columns->fetchrow_hashref) {
390
            my $column = $column_info->{COLUMN_NAME};
391
            $self->$cb($table, $column, $column_info);
392
        }
393
    }
394
}
395

            
cleanup
yuki-kimoto authored on 2010-10-17
396
sub execute{
397
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
398
    
cleanup
Yuki Kimoto authored on 2011-03-09
399
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
400
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
401
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
402
          unless $EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
403
    }
404
    
cleanup
yuki-kimoto authored on 2010-10-17
405
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
406
    
cleanup
yuki-kimoto authored on 2010-10-17
407
    # First argument is the soruce of SQL
408
    $query = $self->create_query($query)
409
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
410
    
add table tag
Yuki Kimoto authored on 2011-02-09
411
    # Applied filter
cleanup
Yuki Kimoto authored on 2011-01-12
412
    my $filter = {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
413
    
add table tag
Yuki Kimoto authored on 2011-02-09
414
    my $tables = $query->tables;
415
    my $arg_tables = $args{table} || [];
416
    $arg_tables = [$arg_tables]
417
      unless ref $arg_tables eq 'ARRAY';
418
    push @$tables, @$arg_tables;
cleanup
Yuki Kimoto authored on 2011-03-09
419

            
420
    # Organize tables
421
    my %table_set = map {defined $_ ? ($_ => 1) : ()} @$tables;
422
    my $main_table = pop @$tables;
423
    delete $table_set{$main_table} if $main_table;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
424
    foreach my $table (keys %table_set) {
425
        push @$tables, $table;
426
        
427
        if (my $dist = $self->{_table_alias}->{$table}) {
428
            $self->{filter} ||= {};
429
            
430
            unless ($self->{filter}{out}{$table}) {
431
                $self->{filter}{out} ||= {};
432
                $self->{filter}{in}  ||= {};
433
                $self->{filter}{end} ||= {};
434
                
435
                foreach my $type (qw/out in end/) {
436
                    
437
                    foreach my $filter_name (keys %{$self->{filter}{$type}{$dist} || {}}) {
438
                        my $filter_name_alias = $filter_name;
439
                        $filter_name_alias =~ s/^$dist\./$table\./;
440
                        $filter_name_alias =~ s/^${dist}__/${table}__/; 
441
                        
442
                        $self->{filter}{$type}{$table}{$filter_name_alias}
443
                          = $self->{filter}{$type}{$dist}{$filter_name}
444
                    }
445
                }
446
            }
447
        }
448
    }
449
    
cleanup
Yuki Kimoto authored on 2011-03-09
450
    $tables = [keys %table_set];
451
    push @$tables, $main_table if $main_table;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
452
    
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
453
    foreach my $table (@$tables) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
454
        next unless $table;
cleanup
Yuki Kimoto authored on 2011-01-12
455
        $filter = {
456
            %$filter,
457
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
458
        }
459
    }
460
    
cleanup
Yuki Kimoto authored on 2011-01-12
461
    # Filter argument
cleanup
Yuki Kimoto authored on 2011-03-21
462
    my $f = DBIx::Custom::Util::array_to_hash($args{filter})
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
463
         || $query->filter || {};
cleanup
Yuki Kimoto authored on 2011-01-12
464
    foreach my $column (keys %$f) {
465
        my $fname = $f->{$column};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
466
        if (!defined $fname) {
cleanup
Yuki Kimoto authored on 2011-01-12
467
            $f->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
468
        }
469
        elsif (ref $fname ne 'CODE') {
many changed
Yuki Kimoto authored on 2011-01-23
470
          croak qq{Filter "$fname" is not registered"}
cleanup
Yuki Kimoto authored on 2010-12-21
471
            unless exists $self->filters->{$fname};
472
          
cleanup
Yuki Kimoto authored on 2011-01-12
473
          $f->{$column} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2010-12-21
474
        }
475
    }
cleanup
Yuki Kimoto authored on 2011-01-12
476
    $filter = {%$filter, %$f};
packaging one directory
yuki-kimoto authored on 2009-11-16
477
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
478
    # Type
479
    my $type = DBIx::Custom::Util::array_to_hash($args{type});
480
    
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
481
    # Bind
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
482
    my $bind = $self->_bind($params, $query->columns, $filter, $type);
cleanup
yuki-kimoto authored on 2010-10-17
483
    
484
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
485
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
486
    my $affected;
cleanup
Yuki Kimoto authored on 2011-03-21
487
    eval {
488
        for (my $i = 0; $i < @$bind; $i++) {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
489
            if (my $type = $bind->[$i]->{type}) {
490
                $sth->bind_param($i + 1, $bind->[$i]->{value}, $type);
491
            }
492
            else {
493
                $sth->bind_param($i + 1, $bind->[$i]->{value});
494
            }
cleanup
Yuki Kimoto authored on 2011-03-21
495
        }
496
        $affected = $sth->execute;
497
    };
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
498
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
499
    
500
    # Return resultset if select statement is executed
501
    if ($sth->{NUM_OF_FIELDS}) {
502
        
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
503
        # Result in and end filter
504
        my $in_filter  = {};
505
        my $end_filter = {};
cleanup
Yuki Kimoto authored on 2011-01-12
506
        foreach my $table (@$tables) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
507
            next unless $table;
cleanup
Yuki Kimoto authored on 2011-01-12
508
            $in_filter = {
509
                %$in_filter,
510
                %{$self->{filter}{in}{$table} || {}}
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
511
            };
512
            $end_filter = {
513
                %$end_filter,
514
                %{$self->{filter}{end}{$table} || {}}
515
            };
cleanup
Yuki Kimoto authored on 2011-01-12
516
        }
517
        
518
        # Result
519
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
520
            sth            => $sth,
521
            filters        => $self->filters,
522
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
523
            default_filter => $self->{default_in_filter},
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
524
            filter         => $in_filter || {},
525
            end_filter     => $end_filter || {}
cleanup
yuki-kimoto authored on 2010-10-17
526
        );
527

            
528
        return $result;
529
    }
530
    return $affected;
531
}
532

            
cleanup
Yuki Kimoto authored on 2011-03-21
533
our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param append/;
update pod
Yuki Kimoto authored on 2011-03-13
534

            
cleanup
yuki-kimoto authored on 2010-10-17
535
sub insert {
536
    my ($self, %args) = @_;
537

            
cleanup
Yuki Kimoto authored on 2011-03-09
538
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
539
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
540
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
541
          unless $INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
542
    }
543
    
cleanup
yuki-kimoto authored on 2010-10-17
544
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
545
    my $table  = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
546
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
547
    my $param  = delete $args{param} || {};
548
    my $append = delete $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
549
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
550
    # Columns
551
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
552
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
553
    foreach my $column (keys %$param) {
554
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
555
          unless $column =~ /^[$safety\.]+$/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
556
        push @columns, $column;
557
    }
cleanup
yuki-kimoto authored on 2010-10-17
558
    
cleanup
Yuki Kimoto authored on 2011-01-27
559
    # SQL stack
560
    my @sql;
561
    
562
    # Insert
563
    push @sql, "insert into $table {insert_param ". join(' ', @columns) . '}';
564
    push @sql, $append if $append;
565
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
566
    # SQL
cleanup
Yuki Kimoto authored on 2011-01-27
567
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
568
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
569
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
570
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
571
    return $query if $args{query};
572
    
packaging one directory
yuki-kimoto authored on 2009-11-16
573
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
574
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
575
        $query,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
576
        param  => $param,
cleanup
Yuki Kimoto authored on 2011-03-21
577
        table => $table,
578
        %args
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
579
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
580
    
581
    return $ret_val;
582
}
583

            
cleanup
Yuki Kimoto authored on 2011-03-21
584
our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1);
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
585

            
586
sub insert_at {
587
    my ($self, %args) = @_;
588
    
cleanup
Yuki Kimoto authored on 2011-03-09
589
    # Check argument names
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
590
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
591
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
592
          unless $INSERT_AT_ARGS{$name};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
593
    }
594
    
595
    # Primary key
596
    my $primary_keys = delete $args{primary_key};
597
    $primary_keys = [$primary_keys] unless ref $primary_keys;
598
    
599
    # Where clause
600
    my $where = {};
601
    my $param = {};
602
    
603
    if (exists $args{where}) {
604
        my $where_columns = delete $args{where};
605
        $where_columns = [$where_columns] unless ref $where_columns;
606

            
607
        croak qq{"where" must be constant value or array reference}
608
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
609
        
610
        for(my $i = 0; $i < @$primary_keys; $i ++) {
611
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
612
        }
613
    }
614
    
615
    if (exists $args{param}) {
616
        $param = delete $args{param};
617
        for(my $i = 0; $i < @$primary_keys; $i ++) {
618
             delete $param->{$primary_keys->[$i]};
619
        }
620
    }
621
    
622
    $param = {%$param, %$where};
623
    
624
    return $self->insert(param => $param, %args);
625
}
626

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
627
sub insert_param {
628
    my ($self, $param) = @_;
629
    
update pod
Yuki Kimoto authored on 2011-03-13
630
    # Insert parameter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
631
    my @tag;
632
    push @tag, '{insert_param';
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
633
    my $safety = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
634
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
635
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
636
          unless $column =~ /^[$safety\.]+$/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
637
        push @tag, $column;
638
    }
639
    push @tag, '}';
640
    
641
    return join ' ', @tag;
642
}
643

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
644
sub include_model {
645
    my ($self, $name_space, $model_infos) = @_;
646
    
647
    $name_space ||= '';
648
    unless ($model_infos) {
649
        # Load name space module
650
        croak qq{"$name_space" is invalid class name}
651
          if $name_space =~ /[^\w:]/;
652
        eval "use $name_space";
653
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
654
        
655
        # Search model modules
656
        my $path = $INC{"$name_space.pm"};
657
        $path =~ s/\.pm$//;
658
        opendir my $dh, $path
659
          or croak qq{Can't open directory "$path": $!};
660
        $model_infos = [];
661
        while (my $module = readdir $dh) {
662
            push @$model_infos, $module
663
              if $module =~ s/\.pm$//;
664
        }
665
        
666
        close $dh;
667
    }
668
    
669
    foreach my $model_info (@$model_infos) {
670
        
671
        # Model class, name, table
672
        my $model_class;
673
        my $model_name;
674
        my $model_table;
675
        if (ref $model_info eq 'HASH') {
676
            $model_class = $model_info->{class};
677
            $model_name  = $model_info->{name};
678
            $model_table = $model_info->{table};
679
            
680
            $model_name  ||= $model_class;
681
            $model_table ||= $model_name;
682
        }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
683
        else { $model_class = $model_name = $model_table = $model_info }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
684
        my $mclass = "${name_space}::$model_class";
685
        
686
        # Load
687
        croak qq{"$mclass" is invalid class name}
688
          if $mclass =~ /[^\w:]/;
689
        unless ($mclass->can('isa')) {
690
            eval "use $mclass";
691
            croak $@ if $@;
692
        }
693
        
694
        # Instantiate
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
695
        my $args = {};
696
        $args->{model_class} = $mclass if $mclass;
697
        $args->{name}        = $model_name if $model_name;
698
        $args->{table}       = $model_table if $model_table;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
699
        
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
700
        # Create model
701
        $self->create_model($args);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
702
    }
703
    
704
    return $self;
705
}
706

            
cleanup
Yuki Kimoto authored on 2011-03-21
707
sub method {
708
    my $self = shift;
709
    
710
    # Merge
711
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
712
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
713
    
714
    return $self;
715
}
716

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
717
sub model {
718
    my ($self, $name, $model) = @_;
719
    
720
    # Set
721
    if ($model) {
722
        $self->models->{$name} = $model;
723
        return $self;
724
    }
725
    
726
    # Check model existance
727
    croak qq{Model "$name" is not included}
728
      unless $self->models->{$name};
729
    
730
    # Get
731
    return $self->models->{$name};
732
}
733

            
cleanup
Yuki Kimoto authored on 2011-03-21
734
sub mycolumn {
735
    my ($self, $table, $columns) = @_;
736
    
737
    $columns ||= [];
738
    my @column;
739
    push @column, "$table.$_ as $_" for @$columns;
740
    
741
    return join (', ', @column);
742
}
743

            
added dbi_options attribute
kimoto authored on 2010-12-20
744
sub new {
745
    my $self = shift->SUPER::new(@_);
746
    
747
    # Check attribute names
748
    my @attrs = keys %$self;
749
    foreach my $attr (@attrs) {
750
        croak qq{"$attr" is invalid attribute name}
751
          unless $self->can($attr);
752
    }
cleanup
Yuki Kimoto authored on 2011-01-25
753

            
754
    $self->register_tag(
755
        '?'     => \&DBIx::Custom::Tag::placeholder,
756
        '='     => \&DBIx::Custom::Tag::equal,
757
        '<>'    => \&DBIx::Custom::Tag::not_equal,
758
        '>'     => \&DBIx::Custom::Tag::greater_than,
759
        '<'     => \&DBIx::Custom::Tag::lower_than,
760
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
761
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
762
        'like'  => \&DBIx::Custom::Tag::like,
763
        'in'    => \&DBIx::Custom::Tag::in,
764
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
765
        'update_param' => \&DBIx::Custom::Tag::update_param
766
    );
added dbi_options attribute
kimoto authored on 2010-12-20
767
    
768
    return $self;
769
}
770

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

            
cleanup
yuki-kimoto authored on 2010-10-17
773
sub register_filter {
774
    my $invocant = shift;
775
    
776
    # Register filter
777
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
778
    $invocant->filters({%{$invocant->filters}, %$filters});
779
    
780
    return $invocant;
781
}
packaging one directory
yuki-kimoto authored on 2009-11-16
782

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
785
our %SELECT_ARGS
cleanup
Yuki Kimoto authored on 2011-03-30
786
  = map { $_ => 1 } @COMMON_ARGS, qw/column where append relation join/;
refactoring select
yuki-kimoto authored on 2010-04-28
787

            
packaging one directory
yuki-kimoto authored on 2009-11-16
788
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
789
    my ($self, %args) = @_;
packaging one directory
yuki-kimoto authored on 2009-11-16
790
    
cleanup
Yuki Kimoto authored on 2011-03-09
791
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
792
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
793
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
794
          unless $SELECT_ARGS{$name};
refactoring select
yuki-kimoto authored on 2010-04-28
795
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
796
    
refactoring select
yuki-kimoto authored on 2010-04-28
797
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
798
    my $table = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
799
    my $tables = ref $table eq 'ARRAY' ? $table
800
               : defined $table ? [$table]
801
               : [];
cleanup
Yuki Kimoto authored on 2011-03-21
802
    my $columns   = delete $args{column};
803
    my $where     = delete $args{where} || {};
804
    my $append    = delete $args{append};
805
    my $join      = delete $args{join} || [];
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
806
    croak qq{"join" must be array reference}
807
      unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-03-21
808
    my $relation = delete $args{relation};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
809
    
cleanup
Yuki Kimoto authored on 2011-03-09
810
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
811
    $self->_add_relation_table($tables, $relation);
packaging one directory
yuki-kimoto authored on 2009-11-16
812
    
cleanup
Yuki Kimoto authored on 2011-01-27
813
    # SQL stack
814
    my @sql;
815
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
816
    
cleanup
Yuki Kimoto authored on 2011-03-30
817
    if ($columns) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
818

            
819
        $columns = [$columns] if ! ref $columns;
820
        
821
        if (ref $columns eq 'HASH') {
822
            # Find tables
823
            my $main_table;
824
            my %tables;
825
            if ($columns->{table}) {
826
                foreach my $table (@{$columns->{table}}) {
827
                    if (($table || '') eq $tables->[-1]) {
828
                        $main_table = $table;
829
                    }
830
                    else {
831
                        $tables{$table} = 1;
832
                    }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
833
                }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
834
            }
835
            elsif ($columns->{all}) {
836
                $main_table = $tables->[-1] || '';
837
                foreach my $j (@$join) {
838
                    my $tables = $self->_tables($j);
839
                    foreach my $table (@$tables) {
840
                        $tables{$table} = 1;
841
                    }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
842
                }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
843
                delete $tables{$main_table};
844
            }
845
            
846
            push @sql, $columns->{prepend} if $columns->{prepend};
847
            
848
            # Column clause of main table
849
            if ($main_table) {
cleanup
Yuki Kimoto authored on 2011-03-21
850
                push @sql, $self->model($main_table)->mycolumn;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
851
                push @sql, ',';
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
852
            }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
853
            
854
            # Column cluase of other tables
855
            foreach my $table (keys %tables) {
856
                unshift @$tables, $table;
cleanup
Yuki Kimoto authored on 2011-03-21
857
                push @sql, $self->model($table)->column($table);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
858
                push @sql, ',';
859
            }
860
            pop @sql if $sql[-1] eq ',';
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
861
        }
862
        else {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
863
            foreach my $column (@$columns) {
864
                unshift @$tables, @{$self->_tables($column)};
865
                push @sql, ($column, ',');
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
866
            }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
867
            pop @sql if $sql[-1] eq ',';
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
868
        }
869
    }
870
    
871
    # "*" is default
872
    else { push @sql, '*' }
873
    
874
    # Table
cleanup
Yuki Kimoto authored on 2011-03-30
875
    push @sql, 'from';
876
    if ($relation) {
877
        my $found = {};
878
        foreach my $table (@$tables) {
879
            push @sql, ($table, ',') unless $found->{$table};
880
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
881
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
882
    }
cleanup
Yuki Kimoto authored on 2011-03-30
883
    else {
884
        my $main_table = $tables->[-1] || '';
885
        push @sql, $main_table;
886
    }
887
    pop @sql if ($sql[-1] || '') eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
888
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
889
    # Main table
890
    croak "Not found table name" unless $tables->[-1];
891
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
892
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
893
    my $w = $self->_where($where);
894
    $where = $w->param;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
895
    
896
    # String where
897
    my $swhere = "$w";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
898
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
899
    # Add table names in where clause
900
    unshift @$tables, @{$self->_tables($swhere)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
901
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
902
    # Push join
903
    $self->_push_join(\@sql, $join, $tables);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
904
    
cleanup
Yuki Kimoto authored on 2011-03-09
905
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-01-27
906
    push @sql, $swhere;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
907
    
cleanup
Yuki Kimoto authored on 2011-03-08
908
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
909
    $self->_push_relation(\@sql, $tables, $relation, $swhere eq '' ? 1 : 0);
cleanup
Yuki Kimoto authored on 2011-03-08
910
    
cleanup
Yuki Kimoto authored on 2011-01-27
911
    # Append statement
912
    push @sql, $append if $append;
913
    
914
    # SQL
915
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
916
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
917
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
918
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
919
    return $query if $args{query};
920
    
packaging one directory
yuki-kimoto authored on 2009-11-16
921
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
922
    my $result = $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
923
        $query,
924
        param  => $where, 
925
        table => $tables,
926
        %args
927
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
928
    
929
    return $result;
930
}
931

            
cleanup
Yuki Kimoto authored on 2011-03-21
932
our %SELECT_AT_ARGS = (%SELECT_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
933

            
934
sub select_at {
935
    my ($self, %args) = @_;
936
    
cleanup
Yuki Kimoto authored on 2011-03-09
937
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
938
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
939
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
940
          unless $SELECT_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
941
    }
942
    
943
    # Primary key
944
    my $primary_keys = delete $args{primary_key};
945
    $primary_keys = [$primary_keys] unless ref $primary_keys;
946
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
947
    # Table
948
    croak qq{"table" option must be specified} unless $args{table};
949
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
950
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
951
    # Where clause
952
    my $where = {};
953
    if (exists $args{where}) {
954
        my $where_columns = delete $args{where};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
955
        
956
        croak qq{"where" must be constant value or array reference}
957
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
958
        
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
959
        $where_columns = [$where_columns] unless ref $where_columns;
960
        
961
        for(my $i = 0; $i < @$primary_keys; $i ++) {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
962
           $where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
963
        }
964
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
965
    
966
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
967
        my $param = delete $args{param};
968
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
969
             delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
970
        }
971
    }
972
    
973
    return $self->select(where => $where, %args);
974
}
975

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
976
sub setup_model {
977
    my $self = shift;
978
    
979
    $self->each_column(
980
        sub {
981
            my ($self, $table, $column, $column_info) = @_;
982
            
983
            if (my $model = $self->models->{$table}) {
984
                push @{$model->columns}, $column;
985
            }
986
        }
987
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
988
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
989
}
990

            
cleanup
Yuki Kimoto authored on 2011-03-21
991
our %UPDATE_ARGS
992
  = map { $_ => 1 } @COMMON_ARGS, qw/param where append allow_update_all/;
cleanup
yuki-kimoto authored on 2010-10-17
993

            
994
sub update {
995
    my ($self, %args) = @_;
version 0.0901
yuki-kimoto authored on 2009-12-17
996
    
cleanup
Yuki Kimoto authored on 2011-03-09
997
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
998
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
999
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
1000
          unless $UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
1001
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1002
    
cleanup
yuki-kimoto authored on 2010-10-17
1003
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
1004
    my $table = delete $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
1005
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
1006
    my $param            = delete $args{param} || {};
1007
    my $where            = delete $args{where} || {};
1008
    my $append           = delete $args{append} || '';
1009
    my $allow_update_all = delete $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
1010
    
cleanup
yuki-kimoto authored on 2010-10-17
1011
    # Update keys
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1012
    my @clumns = keys %$param;
1013

            
1014
    # Columns
1015
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1016
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1017
    foreach my $column (keys %$param) {
1018
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1019
          unless $column =~ /^[$safety\.]+$/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1020
        push @columns, $column;
1021
    }
1022
        
cleanup
yuki-kimoto authored on 2010-10-17
1023
    # Update clause
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1024
    my $update_clause = '{update_param ' . join(' ', @clumns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1025

            
1026
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1027
    my $w = $self->_where($where);
1028
    $where = $w->param;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1029
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1030
    # String where
1031
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1032
    
1033
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1034
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1035
    
cleanup
Yuki Kimoto authored on 2011-01-27
1036
    # SQL stack
1037
    my @sql;
1038
    
1039
    # Update
1040
    push @sql, "update $table $update_clause $swhere";
1041
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1042
    
cleanup
yuki-kimoto authored on 2010-10-17
1043
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1044
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1045
        
cleanup
yuki-kimoto authored on 2010-10-17
1046
        if (exists $param->{$wkey}) {
1047
            $param->{$wkey} = [$param->{$wkey}]
1048
              unless ref $param->{$wkey} eq 'ARRAY';
1049
            
1050
            push @{$param->{$wkey}}, $where->{$wkey};
1051
        }
1052
        else {
1053
            $param->{$wkey} = $where->{$wkey};
1054
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1055
    }
cleanup
yuki-kimoto authored on 2010-10-17
1056
    
cleanup
Yuki Kimoto authored on 2011-01-27
1057
    # SQL
1058
    my $sql = join(' ', @sql);
1059
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1060
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
1061
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1062
    return $query if $args{query};
1063
    
cleanup
yuki-kimoto authored on 2010-10-17
1064
    # Execute query
cleanup
Yuki Kimoto authored on 2011-03-21
1065
    my $ret_val = $self->execute(
1066
        $query,
1067
        param  => $param, 
1068
        table => $table,
1069
        %args
1070
    );
cleanup
yuki-kimoto authored on 2010-10-17
1071
    
1072
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1073
}
1074

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
1077
our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1078

            
1079
sub update_at {
1080
    my ($self, %args) = @_;
1081
    
cleanup
Yuki Kimoto authored on 2011-03-09
1082
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1083
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
1084
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
1085
          unless $UPDATE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1086
    }
1087
    
1088
    # Primary key
1089
    my $primary_keys = delete $args{primary_key};
1090
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1091
    
1092
    # Where clause
1093
    my $where = {};
1094
    my $param = {};
1095
    
1096
    if (exists $args{where}) {
1097
        my $where_columns = delete $args{where};
1098
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1099

            
1100
        croak qq{"where" must be constant value or array reference}
1101
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1102
        
1103
        for(my $i = 0; $i < @$primary_keys; $i ++) {
1104
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
1105
        }
1106
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1107
    
1108
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1109
        $param = delete $args{param};
1110
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1111
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1112
        }
1113
    }
1114
    
1115
    return $self->update(where => $where, param => $param, %args);
1116
}
1117

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1118
sub update_param {
1119
    my ($self, $param) = @_;
1120
    
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1121
    # Update parameter tag
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1122
    my @tag;
1123
    push @tag, '{update_param';
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1124
    my $safety = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1125
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1126
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1127
          unless $column =~ /^[$safety\.]+$/;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1128
        push @tag, $column;
1129
    }
1130
    push @tag, '}';
1131
    
1132
    return join ' ', @tag;
1133
}
1134

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

            
1138
    return DBIx::Custom::Where->new(
1139
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1140
        safety_character => $self->safety_character,
cleanup
Yuki Kimoto authored on 2011-03-09
1141
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1142
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1143
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1144

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1145
sub _bind {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1146
    my ($self, $params, $columns, $filter, $type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1147
    
cleanup
Yuki Kimoto authored on 2011-01-12
1148
    # bind values
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1149
    my $bind = [];
add tests
yuki-kimoto authored on 2010-08-08
1150
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1151
    # Build bind values
1152
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1153
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1154
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1155
        
1156
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1157
        my $value;
1158
        if(ref $params->{$column} eq 'ARRAY') {
1159
            my $i = $count->{$column} || 0;
1160
            $i += $not_exists->{$column} || 0;
1161
            my $found;
1162
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1163
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1164
                    $not_exists->{$column}++;
1165
                }
1166
                else  {
1167
                    $value = $params->{$column}->[$k];
1168
                    $found = 1;
1169
                    last
1170
                }
1171
            }
1172
            next unless $found;
1173
        }
1174
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1175
        
cleanup
Yuki Kimoto authored on 2011-01-12
1176
        # Filter
1177
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1178
        
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1179
        # Type
1180
        push @$bind, {
1181
            value => $f ? $f->($value) : $value,
1182
            type => $type->{$column}
1183
        };
removed reconnect method
yuki-kimoto authored on 2010-05-28
1184
        
1185
        # Count up 
1186
        $count->{$column}++;
1187
    }
1188
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1189
    return $bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1190
}
1191

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1192
sub _connect {
1193
    my $self = shift;
1194
    
1195
    # Attributes
1196
    my $data_source = $self->data_source;
1197
    croak qq{"data_source" must be specified to connect()"}
1198
      unless $data_source;
1199
    my $user        = $self->user;
1200
    my $password    = $self->password;
1201
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
1202
    
1203
    # Connect
1204
    my $dbh = eval {DBI->connect(
1205
        $data_source,
1206
        $user,
1207
        $password,
1208
        {
1209
            %{$self->default_dbi_option},
1210
            %$dbi_option
1211
        }
1212
    )};
1213
    
1214
    # Connect error
1215
    croak $@ if $@;
1216
    
1217
    return $dbh;
1218
}
1219

            
cleanup
yuki-kimoto authored on 2010-10-17
1220
sub _croak {
1221
    my ($self, $error, $append) = @_;
1222
    $append ||= "";
1223
    
1224
    # Verbose
1225
    if ($Carp::Verbose) { croak $error }
1226
    
1227
    # Not verbose
1228
    else {
1229
        
1230
        # Remove line and module infromation
1231
        my $at_pos = rindex($error, ' at ');
1232
        $error = substr($error, 0, $at_pos);
1233
        $error =~ s/\s+$//;
1234
        
1235
        croak "$error$append";
1236
    }
1237
}
1238

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1239
sub _need_tables {
1240
    my ($self, $tree, $need_tables, $tables) = @_;
1241
    
1242
    foreach my $table (@$tables) {
1243
        
1244
        if ($tree->{$table}) {
1245
            $need_tables->{$table} = 1;
1246
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1247
        }
1248
    }
1249
}
1250

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1251
sub _tables {
1252
    my ($self, $source) = @_;
1253
    
1254
    my $tables = [];
1255
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1256
    my $safety_character = $self->safety_character;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1257
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1258
    while ($source =~ /\b($safety_character+)\./g) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1259
        push @$tables, $1;
1260
    }
1261
    
1262
    return $tables;
1263
}
1264

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1265
sub _push_join {
1266
    my ($self, $sql, $join, $join_tables) = @_;
1267
    
1268
    return unless @$join;
1269
    
1270
    my $tree = {};
1271
    
1272
    for (my $i = 0; $i < @$join; $i++) {
1273
        
1274
        my $join_clause = $join->[$i];
1275
        
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-11
1276
        if ($join_clause =~ /\s([^\.\s]+?)\..+\s([^\.\s]+?)\..+?$/) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1277
            
1278
            my $table1 = $1;
1279
            my $table2 = $2;
1280
            
1281
            croak qq{right side table of "$join_clause" must be uniq}
1282
              if exists $tree->{$table2};
1283
            
1284
            $tree->{$table2}
1285
              = {position => $i, parent => $table1, join => $join_clause};
1286
        }
1287
        else {
1288
            croak qq{join "$join_clause" must be two table name};
1289
        }
1290
    }
1291
    
1292
    my $need_tables = {};
1293
    $self->_need_tables($tree, $need_tables, $join_tables);
1294
    
1295
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-03-08
1296

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1297
    foreach my $need_table (@need_tables) {
1298
        push @$sql, $tree->{$need_table}{join};
1299
    }
1300
}
cleanup
Yuki Kimoto authored on 2011-03-08
1301

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1302
sub _where {
1303
    my ($self, $where) = @_;
1304
    
1305
    my $w;
1306
    if (ref $where eq 'HASH') {
1307
        my $clause = ['and'];
1308
        push @$clause, "{= $_}" for keys %$where;
1309
        $w = $self->where(clause => $clause, param => $where);
1310
    }
1311
    elsif (ref $where eq 'DBIx::Custom::Where') {
1312
        $w = $where;
1313
    }
1314
    elsif (ref $where eq 'ARRAY') {
1315
        $w = $self->where(
1316
            clause => $where->[0],
1317
            param  => $where->[1]
1318
        );
1319
    }
1320
    
1321
    croak qq{"where" must be hash reference or DBIx::Custom::Where object} .
1322
          qq{or array reference, which contains where clause and paramter}
1323
      unless ref $w eq 'DBIx::Custom::Where';
1324
    
1325
    return $w;
1326
}
1327

            
cleanup
Yuki Kimoto authored on 2011-01-25
1328
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1329
__PACKAGE__->attr(
1330
    dbi_options => sub { {} },
1331
    filter_check  => 1
1332
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1333

            
cleanup
Yuki Kimoto authored on 2011-01-25
1334
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1335
sub default_bind_filter {
1336
    my $self = shift;
1337
    
1338
    if (@_) {
1339
        my $fname = $_[0];
1340
        
1341
        if (@_ && !$fname) {
1342
            $self->{default_out_filter} = undef;
1343
        }
1344
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1345
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1346
              unless exists $self->filters->{$fname};
1347
        
1348
            $self->{default_out_filter} = $self->filters->{$fname};
1349
        }
1350
        return $self;
1351
    }
1352
    
1353
    return $self->{default_out_filter};
1354
}
1355

            
cleanup
Yuki Kimoto authored on 2011-01-25
1356
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1357
sub default_fetch_filter {
1358
    my $self = shift;
1359
    
1360
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1361
        my $fname = $_[0];
1362

            
cleanup
Yuki Kimoto authored on 2011-01-12
1363
        if (@_ && !$fname) {
1364
            $self->{default_in_filter} = undef;
1365
        }
1366
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1367
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1368
              unless exists $self->filters->{$fname};
1369
        
1370
            $self->{default_in_filter} = $self->filters->{$fname};
1371
        }
1372
        
1373
        return $self;
1374
    }
1375
    
many changed
Yuki Kimoto authored on 2011-01-23
1376
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1377
}
1378

            
cleanup
Yuki Kimoto authored on 2011-01-25
1379
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1380
sub register_tag_processor {
1381
    return shift->query_builder->register_tag_processor(@_);
1382
}
1383

            
cleanup
Yuki Kimoto authored on 2011-03-08
1384
# DEPRECATED!
1385
sub _push_relation {
1386
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1387
    
1388
    if (keys %{$relation || {}}) {
1389
        push @$sql, $need_where ? 'where' : 'and';
1390
        foreach my $rcolumn (keys %$relation) {
1391
            my $table1 = (split (/\./, $rcolumn))[0];
1392
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1393
            push @$tables, ($table1, $table2);
1394
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1395
        }
1396
    }
1397
    pop @$sql if $sql->[-1] eq 'and';    
1398
}
1399

            
1400
# DEPRECATED!
1401
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1402
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1403
    
1404
    if (keys %{$relation || {}}) {
1405
        foreach my $rcolumn (keys %$relation) {
1406
            my $table1 = (split (/\./, $rcolumn))[0];
1407
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1408
            my $table1_exists;
1409
            my $table2_exists;
1410
            foreach my $table (@$tables) {
1411
                $table1_exists = 1 if $table eq $table1;
1412
                $table2_exists = 1 if $table eq $table2;
1413
            }
1414
            unshift @$tables, $table1 unless $table1_exists;
1415
            unshift @$tables, $table2 unless $table2_exists;
1416
        }
1417
    }
1418
}
1419

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1422
=head1 NAME
1423

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1424
DBIx::Custom - Useful database access, respecting SQL!
removed reconnect method
yuki-kimoto authored on 2010-05-28
1425

            
1426
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1427

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1428
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1429
    
1430
    # Connect
1431
    my $dbi = DBIx::Custom->connect(
1432
        data_source => "dbi:mysql:database=dbname",
1433
        user => 'ken',
1434
        password => '!LFKD%$&',
1435
        dbi_option => {mysql_enable_utf8 => 1}
1436
    );
cleanup
yuki-kimoto authored on 2010-08-05
1437

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1438
    # Insert 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1439
    $dbi->insert(
1440
        table  => 'book',
1441
        param  => {title => 'Perl', author => 'Ken'}
1442
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1443
    
1444
    # Update 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1445
    $dbi->update(
1446
        table  => 'book', 
1447
        param  => {title => 'Perl', author => 'Ken'}, 
1448
        where  => {id => 5},
1449
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1450
    
1451
    # Delete
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1452
    $dbi->delete(
1453
        table  => 'book',
1454
        where  => {author => 'Ken'},
1455
    );
cleanup
yuki-kimoto authored on 2010-08-05
1456

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1457
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
1458
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1459
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
1460
        where  => {author => 'Ken'},
added commit method
yuki-kimoto authored on 2010-05-27
1461
    );
cleanup
yuki-kimoto authored on 2010-08-05
1462

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1463
    # Select, more complex
1464
    my $result = $dbi->select(
1465
        table  => 'book',
1466
        column => [
1467
            'book.author as book__author',
1468
            'company.name as company__name'
1469
        ],
1470
        where  => {'book.author' => 'Ken'},
1471
        join => ['left outer join company on book.company_id = company.id'],
1472
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1473
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1474
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1475
    # Fetch
1476
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1477
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1478
    }
1479
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1480
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1481
    while (my $row = $result->fetch_hash) {
1482
        
1483
    }
1484
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1485
    # Execute SQL with parameter.
1486
    $dbi->execute(
1487
        "select id from book where {= author} and {like title}",
1488
        param  => {author => 'ken', title => '%Perl%'}
1489
    );
1490
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1491
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1492

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1493
L<DBIx::Custom> is L<DBI> wrapper module.
1494

            
1495
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1496

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1497
=over 4
removed reconnect method
yuki-kimoto authored on 2010-05-28
1498

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1499
=item *
removed reconnect method
yuki-kimoto authored on 2010-05-28
1500

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1501
There are many basic methods to execute various queries.
1502
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1503
C<delete_all()>, C<select()>,
1504
C<insert_at()>, C<update_at()>, 
1505
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1506

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1507
=item *
1508

            
1509
Filter when data is send or receive.
1510

            
1511
=item *
1512

            
1513
Data filtering system
1514

            
1515
=item *
1516

            
1517
Model support.
1518

            
1519
=item *
1520

            
1521
Generate where clause dinamically.
1522

            
1523
=item *
1524

            
1525
Generate join clause dinamically.
1526

            
1527
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1528

            
1529
=head1 GUIDE
1530

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1531
L<DBIx::Custom::Guide> - L<DBIx::Custom> Guide
pod fix
Yuki Kimoto authored on 2011-01-21
1532

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1533
=head1 Wiki
pod fix
Yuki Kimoto authored on 2011-01-21
1534

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1535
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki>
updated document
yuki-kimoto authored on 2010-08-08
1536

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

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1544
Data source, used when C<connect()> is executed.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1545

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

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1548
    my $dbi_option = $dbi->dbi_option;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1549
    $dbi           = $dbi->dbi_option($dbi_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1550

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1551
L<DBI> option, used when C<connect()> is executed.
1552
Each value in option override the value of C<default_dbi_option>.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1553

            
1554
=head2 C<default_dbi_option>
1555

            
1556
    my $default_dbi_option = $dbi->default_dbi_option;
1557
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1558

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1559
L<DBI> default option, used when C<connect()> is executed,
1560
default to the following values.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1561

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1562
    {
1563
        RaiseError => 1,
1564
        PrintError => 0,
1565
        AutoCommit => 1,
1566
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1567

            
update pod
Yuki Kimoto authored on 2011-03-13
1568
You should not change C<AutoCommit> value directly,
1569
the value is used to check if the process is in transaction.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1570

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1576
Filters, registered by C<register_filter()>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
1577

            
update pod
Yuki Kimoto authored on 2011-03-13
1578
=head2 C<models> EXPERIMENTAL
add models() attribute
Yuki Kimoto authored on 2011-02-21
1579

            
1580
    my $models = $dbi->models;
1581
    $dbi       = $dbi->models(\%models);
1582

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1583
Models, included by C<include_model()>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
1584

            
cleanup
yuki-kimoto authored on 2010-10-17
1585
=head2 C<password>
1586

            
1587
    my $password = $dbi->password;
1588
    $dbi         = $dbi->password('lkj&le`@s');
1589

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1590
Password, used when C<connect()> is executed.
update document
yuki-kimoto authored on 2010-01-30
1591

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1597
Query builder, default to L<DBIx::Custom::QueryBuilder> object.
cleanup
yuki-kimoto authored on 2010-08-05
1598

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1604
Result class, default to L<DBIx::Custom::Result>.
cleanup
yuki-kimoto authored on 2010-08-05
1605

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1606
=head2 C<safety_character>
update pod
Yuki Kimoto authored on 2011-01-27
1607

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1608
    my $safety_character = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-03-10
1609
    $dbi                 = $self->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
1610

            
update pod
Yuki Kimoto authored on 2011-03-13
1611
Regex of safety character for table and column name, default to '\w'.
cleanup
Yuki Kimoto authored on 2011-03-10
1612
Note that you don't have to specify like '[\w]'.
update pod
Yuki Kimoto authored on 2011-01-27
1613

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1616
    my $user = $dbi->user;
1617
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1618

            
cleanup
Yuki Kimoto authored on 2011-03-10
1619
User name, used when C<connect()> is executed.
update pod
Yuki Kimoto authored on 2011-01-27
1620

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1623
L<DBIx::Custom> inherits all methods from L<Object::Simple>
cleanup
Yuki Kimoto authored on 2011-03-10
1624
and use all methods of L<DBI>
cleanup
yuki-kimoto authored on 2010-10-17
1625
and implements the following new ones.
added check_filter attribute
yuki-kimoto authored on 2010-08-08
1626

            
update pod
Yuki Kimoto authored on 2011-03-13
1627
=head2 C<apply_filter> EXPERIMENTAL
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1628

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1629
    $dbi->apply_filter(
cleanup
Yuki Kimoto authored on 2011-03-10
1630
        'book',
update pod
Yuki Kimoto authored on 2011-03-13
1631
        'issue_date' => {
1632
            out => 'tp_to_date',
1633
            in  => 'date_to_tp',
1634
            end => 'tp_to_displaydate'
1635
        },
1636
        'write_date' => {
1637
            out => 'tp_to_date',
1638
            in  => 'date_to_tp',
1639
            end => 'tp_to_displaydate'
1640
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1641
    );
1642

            
update pod
Yuki Kimoto authored on 2011-03-13
1643
Apply filter to columns.
1644
C<out> filter is executed before data is send to database.
1645
C<in> filter is executed after a row is fetch.
1646
C<end> filter is execute after C<in> filter is executed.
1647

            
1648
Filter is applied to the follwoing tree column name pattern.
cleanup
Yuki Kimoto authored on 2010-12-21
1649

            
update pod
Yuki Kimoto authored on 2011-03-13
1650
       PETTERN         EXAMPLE
1651
    1. Column        : author
1652
    2. Table.Column  : book.author
1653
    3. Table__Column : book__author
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1654

            
update pod
Yuki Kimoto authored on 2011-03-13
1655
If column name is duplicate with other table,
1656
Main filter specified by C<table> option is used.
1657

            
1658
You can set multiple filters at once.
1659

            
1660
    $dbi->apply_filter(
1661
        'book',
1662
        [qw/issue_date write_date/] => {
1663
            out => 'tp_to_date',
1664
            in  => 'date_to_tp',
1665
            end => 'tp_to_displaydate'
1666
        }
1667
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1668

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1671
    my $dbi = DBIx::Custom->connect(
1672
        data_source => "dbi:mysql:database=dbname",
1673
        user => 'ken',
1674
        password => '!LFKD%$&',
1675
        dbi_option => {mysql_enable_utf8 => 1}
1676
    );
1677

            
1678
Connect to the database and create a new L<DBIx::Custom> object.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1679

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1680
L<DBIx::Custom> is a wrapper of L<DBI>.
cleanup
yuki-kimoto authored on 2010-08-09
1681
C<AutoCommit> and C<RaiseError> options are true, 
update pod
Yuki Kimoto authored on 2011-03-13
1682
and C<PrintError> option is false by default.
packaging one directory
yuki-kimoto authored on 2009-11-16
1683

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1684
=head2 create_model
1685

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
1686
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1687
        table => 'book',
1688
        primary_key => 'id',
1689
        join => [
1690
            'inner join company on book.comparny_id = company.id'
1691
        ],
1692
        filter => [
1693
            publish_date => {
1694
                out => 'tp_to_date',
1695
                in => 'date_to_tp',
1696
                end => 'tp_to_displaydate'
1697
            }
1698
        ]
1699
    );
1700

            
1701
Create L<DBIx::Custom::Model> object and initialize model.
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
1702
the module is also used from model() method.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1703

            
1704
   $dbi->model('book')->select(...);
1705

            
cleanup
yuki-kimoto authored on 2010-10-17
1706
=head2 C<create_query>
1707
    
1708
    my $query = $dbi->create_query(
update pod
Yuki Kimoto authored on 2011-03-13
1709
        "insert into book {insert_param title author};";
cleanup
yuki-kimoto authored on 2010-10-17
1710
    );
update document
yuki-kimoto authored on 2009-11-19
1711

            
update pod
Yuki Kimoto authored on 2011-03-13
1712
Create L<DBIx::Custom::Query> object.
1713

            
cleanup
yuki-kimoto authored on 2010-10-17
1714
If you want to get high performance,
update pod
Yuki Kimoto authored on 2011-03-13
1715
create L<DBIx::Custom::Query> object and execute the query by C<execute()>
1716
instead of other methods, such as C<insert>, C<update>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1717

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

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1720
=head2 C<dbh>
1721

            
1722
    my $dbh = $dbi->dbh;
1723
    $dbi    = $dbi->dbh($dbh);
1724

            
1725
Get and set database handle of L<DBI>.
1726

            
update pod
Yuki Kimoto authored on 2011-03-13
1727
If process is spawn by forking, new connection is created automatically.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1728

            
1729
=head2 C<each_column>
1730

            
1731
    $dbi->each_column(
1732
        sub {
1733
            my ($dbi, $table, $column, $column_info) = @_;
1734
            
1735
            my $type = $column_info->{TYPE_NAME};
1736
            
1737
            if ($type eq 'DATE') {
1738
                # ...
1739
            }
1740
        }
1741
    );
1742

            
1743
Iterate all column informations of all table from database.
1744
Argument is callback when one column is found.
1745
Callback receive four arguments, dbi object, table name,
1746
column name and column information.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1747

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1750
    my $result = $dbi->execute(
1751
        "select * from book where {= title} and {like author}",
1752
        param => {title => 'Perl', author => '%Ken%'}
1753
    );
1754

            
1755
Execute SQL, containing tags.
1756
Return value is L<DBIx::Custom::Result> in select statement, or
1757
the count of affected rows in insert, update, delete statement.
1758

            
1759
Tag is turned into the statement containing place holder
1760
before SQL is executed.
1761

            
1762
    select * from where title = ? and author like ?;
1763

            
1764
See also L<Tags/Tags>.
1765

            
1766
The following opitons are currently available.
1767

            
1768
=over 4
1769

            
1770
=item C<filter>
1771

            
1772
Filter, executed before data is send to database. This is array reference.
1773
Filter value is code reference or
1774
filter name registerd by C<register_filter()>.
1775

            
1776
    # Basic
1777
    $dbi->execute(
1778
        $sql,
1779
        filter => [
1780
            title  => sub { uc $_[0] }
1781
            author => sub { uc $_[0] }
1782
        ]
1783
    );
1784
    
1785
    # At once
1786
    $dbi->execute(
1787
        $sql,
1788
        filter => [
1789
            [qw/title author/]  => sub { uc $_[0] }
1790
        ]
1791
    );
1792
    
1793
    # Filter name
1794
    $dbi->execute(
1795
        $sql,
1796
        filter => [
1797
            title  => 'upper_case',
1798
            author => 'upper_case'
1799
        ]
1800
    );
1801

            
1802
These filters are added to the C<out> filters, set by C<apply_filter()>.
update document
yuki-kimoto authored on 2009-11-19
1803

            
update pod
Yuki Kimoto authored on 2011-03-13
1804
=back
version 0.0901
yuki-kimoto authored on 2009-12-17
1805

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1808
    $dbi->delete(table => 'book', where => {title => 'Perl'});
1809

            
1810
Delete statement.
1811

            
1812
The following opitons are currently available.
1813

            
update pod
Yuki Kimoto authored on 2011-03-13
1814
=over 4
1815

            
update pod
Yuki Kimoto authored on 2011-03-13
1816
=item C<table>
1817

            
1818
Table name.
1819

            
1820
    $dbi->delete(table => 'book');
1821

            
1822
=item C<where>
1823

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1824
Where clause. This is hash reference or L<DBIx::Custom::Where> object
1825
or array refrence, which contains where clause and paramter.
update pod
Yuki Kimoto authored on 2011-03-13
1826
    
1827
    # Hash reference
1828
    $dbi->delete(where => {title => 'Perl'});
1829
    
1830
    # DBIx::Custom::Where object
1831
    my $where = $dbi->where(
1832
        clause => ['and', '{= author}', '{like title}'],
1833
        param  => {author => 'Ken', title => '%Perl%'}
1834
    );
1835
    $dbi->delete(where => $where);
1836

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1837
    # Array refrendce (where clause and parameter)
1838
    $dbi->delete(where =>
1839
        [
1840
            ['and', '{= author}', '{like title}'],
1841
            {author => 'Ken', title => '%Perl%'}
1842
        ]
1843
    );
1844
    
update pod
Yuki Kimoto authored on 2011-03-13
1845
=item C<append>
1846

            
1847
Append statement to last of SQL. This is string.
1848

            
1849
    $dbi->delete(append => 'order by title');
1850

            
1851
=item C<filter>
1852

            
1853
Filter, executed before data is send to database. This is array reference.
1854
Filter value is code reference or
1855
filter name registerd by C<register_filter()>.
1856

            
1857
    # Basic
1858
    $dbi->delete(
1859
        filter => [
1860
            title  => sub { uc $_[0] }
1861
            author => sub { uc $_[0] }
1862
        ]
1863
    );
1864
    
1865
    # At once
1866
    $dbi->delete(
1867
        filter => [
1868
            [qw/title author/]  => sub { uc $_[0] }
1869
        ]
1870
    );
1871
    
1872
    # Filter name
1873
    $dbi->delete(
1874
        filter => [
1875
            title  => 'upper_case',
1876
            author => 'upper_case'
1877
        ]
1878
    );
1879

            
1880
These filters are added to the C<out> filters, set by C<apply_filter()>.
1881

            
cleanup
Yuki Kimoto authored on 2011-03-21
1882
=head2 C<column> EXPERIMENTAL
1883

            
1884
    my $column = $self->column(book => ['author', 'title']);
1885

            
1886
Create column clause. The follwoing column clause is created.
1887

            
1888
    book.author as book__author,
1889
    book.title as book__title
1890

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1891
=item C<query>
update pod
Yuki Kimoto authored on 2011-03-13
1892

            
1893
Get L<DBIx::Custom::Query> object instead of executing SQL.
1894
This is true or false value.
1895

            
1896
    my $query = $dbi->delete(query => 1);
1897

            
1898
You can check SQL.
1899

            
1900
    my $sql = $query->sql;
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1901

            
update pod
Yuki Kimoto authored on 2011-03-13
1902
=back
1903

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1908
Delete statement to delete all rows.
1909
Options is same as C<delete()>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1910

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1911
=head2 C<delete_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1912

            
update pod
Yuki Kimoto authored on 2011-03-13
1913
Delete statement, using primary key.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1914

            
1915
    $dbi->delete_at(
1916
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1917
        primary_key => 'id',
1918
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1919
    );
1920

            
update pod
Yuki Kimoto authored on 2011-03-13
1921
This method is same as C<delete()> exept that
1922
C<primary_key> is specified and C<where> is constant value or array refrence.
1923
all option of C<delete()> is available.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1924

            
update pod
Yuki Kimoto authored on 2011-03-13
1925
=over 4
1926

            
1927
=item C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1928

            
update pod
Yuki Kimoto authored on 2011-03-13
1929
Primary key. This is constant value or array reference.
1930
    
1931
    # Constant value
1932
    $dbi->delete(primary_key => 'id');
1933

            
1934
    # Array reference
1935
    $dbi->delete(primary_key => ['id1', 'id2' ]);
1936

            
1937
This is used to create where clause.
1938

            
update pod
Yuki Kimoto authored on 2011-03-13
1939
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
1940

            
1941
Where clause, created from primary key information.
1942
This is constant value or array reference.
1943

            
1944
    # Constant value
1945
    $dbi->delete(where => 5);
1946

            
1947
    # Array reference
1948
    $dbi->delete(where => [3, 5]);
1949

            
1950
In first examle, the following SQL is created.
1951

            
1952
    delete from book where id = ?;
1953

            
1954
Place holder is set to 5.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1955

            
update pod
Yuki Kimoto authored on 2011-03-13
1956
=back
1957

            
cleanup
yuki-kimoto authored on 2010-10-17
1958
=head2 C<insert>
1959

            
update pod
Yuki Kimoto authored on 2011-03-13
1960
    $dbi->insert(
1961
        table  => 'book', 
1962
        param  => {title => 'Perl', author => 'Ken'}
1963
    );
1964

            
1965
Insert statement.
1966

            
1967
The following opitons are currently available.
1968

            
update pod
Yuki Kimoto authored on 2011-03-13
1969
=over 4
1970

            
update pod
Yuki Kimoto authored on 2011-03-13
1971
=item C<table>
1972

            
1973
Table name.
1974

            
1975
    $dbi->insert(table => 'book');
1976

            
1977
=item C<param>
1978

            
1979
Insert data. This is hash reference.
1980

            
1981
    $dbi->insert(param => {title => 'Perl'});
1982

            
1983
=item C<append>
1984

            
1985
Append statement to last of SQL. This is string.
1986

            
1987
    $dbi->insert(append => 'order by title');
1988

            
1989
=item C<filter>
1990

            
1991
Filter, executed before data is send to database. This is array reference.
1992
Filter value is code reference or
1993
filter name registerd by C<register_filter()>.
1994

            
1995
    # Basic
1996
    $dbi->insert(
1997
        filter => [
1998
            title  => sub { uc $_[0] }
1999
            author => sub { uc $_[0] }
2000
        ]
2001
    );
2002
    
2003
    # At once
2004
    $dbi->insert(
2005
        filter => [
2006
            [qw/title author/]  => sub { uc $_[0] }
2007
        ]
2008
    );
2009
    
2010
    # Filter name
2011
    $dbi->insert(
2012
        filter => [
2013
            title  => 'upper_case',
2014
            author => 'upper_case'
2015
        ]
2016
    );
2017

            
2018
These filters are added to the C<out> filters, set by C<apply_filter()>.
2019

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2020
=item C<query>
update pod
Yuki Kimoto authored on 2011-03-13
2021

            
2022
Get L<DBIx::Custom::Query> object instead of executing SQL.
2023
This is true or false value.
2024

            
2025
    my $query = $dbi->insert(query => 1);
cleanup
yuki-kimoto authored on 2010-10-17
2026

            
update pod
Yuki Kimoto authored on 2011-03-13
2027
You can check SQL.
cleanup
yuki-kimoto authored on 2010-10-17
2028

            
update pod
Yuki Kimoto authored on 2011-03-13
2029
    my $sql = $query->sql;
2030

            
update pod
Yuki Kimoto authored on 2011-03-13
2031
=back
2032

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2033
=head2 C<insert_at()>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2034

            
update pod
Yuki Kimoto authored on 2011-03-13
2035
Insert statement, using primary key.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2036

            
2037
    $dbi->insert_at(
2038
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2039
        primary_key => 'id',
2040
        where => '5',
2041
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2042
    );
2043

            
update pod
Yuki Kimoto authored on 2011-03-13
2044
This method is same as C<insert()> exept that
2045
C<primary_key> is specified and C<where> is constant value or array refrence.
2046
all option of C<insert()> is available.
2047

            
update pod
Yuki Kimoto authored on 2011-03-13
2048
=over 4
2049

            
2050
=item C<primary_key>
update pod
Yuki Kimoto authored on 2011-03-13
2051

            
2052
Primary key. This is constant value or array reference.
2053
    
2054
    # Constant value
2055
    $dbi->insert(primary_key => 'id');
2056

            
2057
    # Array reference
2058
    $dbi->insert(primary_key => ['id1', 'id2' ]);
2059

            
2060
This is used to create parts of insert data.
2061

            
update pod
Yuki Kimoto authored on 2011-03-13
2062
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
2063

            
2064
Parts of Insert data, create from primary key information.
2065
This is constant value or array reference.
2066

            
2067
    # Constant value
2068
    $dbi->insert(where => 5);
2069

            
2070
    # Array reference
2071
    $dbi->insert(where => [3, 5]);
2072

            
2073
In first examle, the following SQL is created.
2074

            
2075
    insert into book (id, title) values (?, ?);
2076

            
2077
Place holders are set to 5 and 'Perl'.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2078

            
update pod
Yuki Kimoto authored on 2011-03-13
2079
=back
2080

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2081
=head2 C<insert_param>
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2082

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

            
2085
Create insert parameter tag.
2086

            
update pod
Yuki Kimoto authored on 2011-03-13
2087
    {insert_param title age}
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2088

            
update pod
Yuki Kimoto authored on 2011-03-13
2089
=head2 C<include_model> EXPERIMENTAL
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2090

            
update pod
Yuki Kimoto authored on 2011-03-13
2091
    $dbi->include_model('MyModel');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2092

            
update pod
Yuki Kimoto authored on 2011-03-13
2093
Include models from specified namespace,
2094
the following layout is needed to include models.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2095

            
update pod
Yuki Kimoto authored on 2011-03-13
2096
    lib / MyModel.pm
2097
        / MyModel / book.pm
2098
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2099

            
update pod
Yuki Kimoto authored on 2011-03-13
2100
Name space module, extending L<DBIx::Custom::Model>.
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2101

            
update pod
Yuki Kimoto authored on 2011-03-13
2102
B<MyModel.pm>
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2103

            
2104
    package MyModel;
2105
    
2106
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-03-13
2107
    
2108
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2109

            
update pod
Yuki Kimoto authored on 2011-03-13
2110
Model modules, extending name space module.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2111

            
update pod
Yuki Kimoto authored on 2011-03-13
2112
B<MyModel/book.pm>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2113

            
update pod
Yuki Kimoto authored on 2011-03-13
2114
    package MyModel::book;
2115
    
2116
    use base 'MyModel';
2117
    
2118
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2119

            
update pod
Yuki Kimoto authored on 2011-03-13
2120
B<MyModel/company.pm>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2121

            
update pod
Yuki Kimoto authored on 2011-03-13
2122
    package MyModel::company;
2123
    
2124
    use base 'MyModel';
2125
    
2126
    1;
2127
    
2128
MyModel::book and MyModel::company is included by C<include_model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2129

            
update pod
Yuki Kimoto authored on 2011-03-13
2130
You can get model object by C<model()>.
2131

            
2132
    my $book_model    = $dbi->model('book');
2133
    my $company_model = $dbi->model('company');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2134

            
update pod
Yuki Kimoto authored on 2011-03-13
2135
See L<DBIx::Custom::Model> to know model features.
2136

            
2137
=head2 C<method> EXPERIMENTAL
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2138

            
2139
    $dbi->method(
2140
        update_or_insert => sub {
2141
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2142
            
2143
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2144
        },
2145
        find_or_create   => sub {
2146
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2147
            
2148
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2149
        }
2150
    );
2151

            
update pod
Yuki Kimoto authored on 2011-03-13
2152
Register method. These method is called directly from L<DBIx::Custom> object.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2153

            
2154
    $dbi->update_or_insert;
2155
    $dbi->find_or_create;
2156

            
update pod
Yuki Kimoto authored on 2011-03-13
2157
=head2 C<model> EXPERIMENTAL
2158

            
2159
    $dbi->model('book')->method(
2160
        insert => sub { ... },
2161
        update => sub { ... }
2162
    );
2163
    
2164
    my $model = $dbi->model('book');
2165

            
2166
Set and get a L<DBIx::Custom::Model> object,
2167

            
cleanup
Yuki Kimoto authored on 2011-03-21
2168
=head2 C<mycolumn> EXPERIMENTAL
2169

            
2170
    my $column = $self->mycolumn(book => ['author', 'title']);
2171

            
2172
Create column clause for myself. The follwoing column clause is created.
2173

            
2174
    book.author as author,
2175
    book.title as title
2176

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2177
=head2 C<new>
2178

            
update pod
Yuki Kimoto authored on 2011-03-13
2179
    my $dbi = DBIx::Custom->new(
2180
        data_source => "dbi:mysql:database=dbname",
2181
        user => 'ken',
2182
        password => '!LFKD%$&',
2183
        dbi_option => {mysql_enable_utf8 => 1}
2184
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2185

            
2186
Create a new L<DBIx::Custom> object.
2187

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2188
=head2 C<not_exists>
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2189

            
2190
    my $not_exists = $dbi->not_exists;
2191

            
update pod
Yuki Kimoto authored on 2011-03-13
2192
DBIx::Custom::NotExists object, indicating the column is not exists.
2193
This is used by C<clause> of L<DBIx::Custom::Where> .
experimental extended select...
Yuki Kimoto authored on 2011-01-17
2194

            
cleanup
yuki-kimoto authored on 2010-10-17
2195
=head2 C<register_filter>
2196

            
update pod
Yuki Kimoto authored on 2011-03-13
2197
    $dbi->register_filter(
2198
        # Time::Piece object to database DATE format
2199
        tp_to_date => sub {
2200
            my $tp = shift;
2201
            return $tp->strftime('%Y-%m-%d');
2202
        },
2203
        # database DATE format to Time::Piece object
2204
        date_to_tp => sub {
2205
           my $date = shift;
2206
           return Time::Piece->strptime($date, '%Y-%m-%d');
2207
        }
2208
    );
cleanup
yuki-kimoto authored on 2010-10-17
2209
    
update pod
Yuki Kimoto authored on 2011-03-13
2210
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2211

            
update pod
Yuki Kimoto authored on 2011-03-13
2212
=head2 C<register_tag>
cleanup
yuki-kimoto authored on 2010-10-17
2213

            
update pod
Yuki Kimoto authored on 2011-03-13
2214
    $dbi->register_tag(
2215
        update => sub {
2216
            my @columns = @_;
2217
            
2218
            # Update parameters
2219
            my $s = 'set ';
2220
            $s .= "$_ = ?, " for @columns;
2221
            $s =~ s/, $//;
2222
            
2223
            return [$s, \@columns];
2224
        }
2225
    );
cleanup
yuki-kimoto authored on 2010-10-17
2226

            
update pod
Yuki Kimoto authored on 2011-03-13
2227
Register tag, used by C<execute()>.
cleanup
yuki-kimoto authored on 2010-10-17
2228

            
update pod
Yuki Kimoto authored on 2011-03-13
2229
See also L<Tags/Tags> about tag registered by default.
cleanup
yuki-kimoto authored on 2010-10-17
2230

            
update pod
Yuki Kimoto authored on 2011-03-13
2231
Tag parser receive arguments specified in tag.
2232
In the following tag, 'title' and 'author' is parser arguments
cleanup
yuki-kimoto authored on 2010-10-17
2233

            
update pod
Yuki Kimoto authored on 2011-03-13
2234
    {update_param title author} 
cleanup
yuki-kimoto authored on 2010-10-17
2235

            
update pod
Yuki Kimoto authored on 2011-03-13
2236
Tag parser must return array refrence,
2237
first element is the result statement, 
2238
second element is column names corresponding to place holders.
cleanup
yuki-kimoto authored on 2010-10-17
2239

            
update pod
Yuki Kimoto authored on 2011-03-13
2240
In this example, result statement is 
cleanup
yuki-kimoto authored on 2010-10-17
2241

            
update pod
Yuki Kimoto authored on 2011-03-13
2242
    set title = ?, author = ?
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2243

            
update pod
Yuki Kimoto authored on 2011-03-13
2244
Column names is
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2245

            
update pod
Yuki Kimoto authored on 2011-03-13
2246
    ['title', 'author']
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2247

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2248
=head2 C<select>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2249

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2250
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2251
        table  => 'book',
2252
        column => ['author', 'title'],
2253
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2254
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2255
    
update pod
Yuki Kimoto authored on 2011-03-12
2256
Select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2257

            
2258
The following opitons are currently available.
2259

            
2260
=over 4
2261

            
2262
=item C<table>
2263

            
2264
Table name.
2265

            
update pod
Yuki Kimoto authored on 2011-03-12
2266
    $dbi->select(table => 'book');
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2267

            
2268
=item C<column>
2269

            
2270
Column clause. This is array reference or constant value.
2271

            
2272
    # Hash refernce
2273
    $dbi->select(column => ['author', 'title']);
2274
    
2275
    # Constant value
2276
    $dbi->select(column => 'author');
2277

            
2278
Default is '*' unless C<column> is specified.
2279

            
2280
    # Default
2281
    $dbi->select(column => '*');
2282

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
2283
You can use hash option in C<column>
2284

            
2285
=over 4
2286

            
2287
=item all EXPERIMENTAL
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2288

            
update pod
Yuki Kimoto authored on 2011-03-12
2289
Colum clause, contains all columns of joined table. This is true or false value
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2290

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
2291
    $dbi->select(column => {all => 1});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2292

            
2293
If main table is C<book> and joined table is C<company>,
2294
This create the following column clause.
2295

            
2296
    book.author as author
2297
    book.company_id as company_id
2298
    company.id as company__id
2299
    company.name as company__name
2300

            
2301
Columns of main table is consist of only column name,
2302
Columns of joined table is consist of table and column name joined C<__>.
2303

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
2304
Note that this option is failed unless modles is included and
2305
C<columns> attribute is set.
update pod
Yuki Kimoto authored on 2011-03-12
2306

            
2307
    # Generally do the following way before using all_column option
2308
    $dbi->include_model('MyModel')->setup_model;
2309

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
2310
=item table EXPERIMENTAL
2311

            
2312
You can also specify table names by C<table> option
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
2313

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
2314
    $dbi->select(column => {table => ['book', 'company']});
2315

            
2316
=item prepend EXPERIMENTAL
2317

            
2318
You can add before created statement
2319

            
2320
    $dbi->select(column => {prepend => 'SOME', all => 1});
2321

            
2322
=back
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
2323

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2324
=item C<where>
2325

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2326
Where clause. This is hash reference or L<DBIx::Custom::Where> object,
2327
or array refrence, which contains where clause and paramter.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2328
    
2329
    # Hash reference
update pod
Yuki Kimoto authored on 2011-03-12
2330
    $dbi->select(where => {author => 'Ken', 'title' => 'Perl'});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2331
    
update pod
Yuki Kimoto authored on 2011-03-12
2332
    # DBIx::Custom::Where object
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2333
    my $where = $dbi->where(
2334
        clause => ['and', '{= author}', '{like title}'],
2335
        param  => {author => 'Ken', title => '%Perl%'}
2336
    );
update pod
Yuki Kimoto authored on 2011-03-12
2337
    $dbi->select(where => $where);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2338

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2339
    # Array refrendce (where clause and parameter)
2340
    $dbi->select(where =>
2341
        [
2342
            ['and', '{= author}', '{like title}'],
2343
            {author => 'Ken', title => '%Perl%'}
2344
        ]
2345
    );
2346
    
update pod
Yuki Kimoto authored on 2011-03-13
2347
=item C<join> EXPERIMENTAL
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2348

            
update pod
Yuki Kimoto authored on 2011-03-12
2349
Join clause used in need. This is array reference.
2350

            
2351
    $dbi->select(join =>
2352
        [
2353
            'left outer join company on book.company_id = company_id',
2354
            'left outer join location on company.location_id = location.id'
2355
        ]
2356
    );
2357

            
2358
If column cluase or where clause contain table name like "company.name",
2359
needed join clause is used automatically.
2360

            
2361
    $dbi->select(
2362
        table => 'book',
2363
        column => ['company.location_id as company__location_id'],
2364
        where => {'company.name' => 'Orange'},
2365
        join => [
2366
            'left outer join company on book.company_id = company.id',
2367
            'left outer join location on company.location_id = location.id'
2368
        ]
2369
    );
2370

            
2371
In above select, the following SQL is created.
2372

            
2373
    select company.location_id as company__location_id
2374
    from book
2375
      left outer join company on book.company_id = company.id
2376
    where company.name = Orange
2377

            
2378
=item C<append>
2379

            
update pod
Yuki Kimoto authored on 2011-03-13
2380
Append statement to last of SQL. This is string.
update pod
Yuki Kimoto authored on 2011-03-12
2381

            
2382
    $dbi->select(append => 'order by title');
2383

            
2384
=item C<filter>
2385

            
update pod
Yuki Kimoto authored on 2011-03-13
2386
Filter, executed before data is send to database. This is array reference.
2387
Filter value is code reference or
update pod
Yuki Kimoto authored on 2011-03-12
2388
filter name registerd by C<register_filter()>.
2389

            
2390
    # Basic
2391
    $dbi->select(
2392
        filter => [
2393
            title  => sub { uc $_[0] }
2394
            author => sub { uc $_[0] }
2395
        ]
2396
    );
2397
    
2398
    # At once
2399
    $dbi->select(
2400
        filter => [
2401
            [qw/title author/]  => sub { uc $_[0] }
2402
        ]
2403
    );
2404
    
2405
    # Filter name
2406
    $dbi->select(
2407
        filter => [
2408
            title  => 'upper_case',
2409
            author => 'upper_case'
2410
        ]
2411
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
2412

            
update pod
Yuki Kimoto authored on 2011-03-13
2413
These filters are added to the C<out> filters, set by C<apply_filter()>.
update document
yuki-kimoto authored on 2009-11-19
2414

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2415
=item C<query>
cleanup
yuki-kimoto authored on 2010-08-09
2416

            
update pod
Yuki Kimoto authored on 2011-03-12
2417
Get L<DBIx::Custom::Query> object instead of executing SQL.
2418
This is true or false value.
2419

            
update pod
Yuki Kimoto authored on 2011-03-13
2420
    my $query = $dbi->select(query => 1);
update pod
Yuki Kimoto authored on 2011-03-12
2421

            
update pod
Yuki Kimoto authored on 2011-03-13
2422
You can check SQL.
update pod
Yuki Kimoto authored on 2011-03-12
2423

            
2424
    my $sql = $query->sql;
2425

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
2426
=item C<type> EXPERIMENTAL
2427

            
2428
Specify database data type.
2429

            
2430
    $dbi->select(type => [image => DBI::SQL_BLOB]);
2431
    $dbi->select(type => [[qw/image audio/] => DBI::SQL_BLOB]);
2432

            
2433
This is used to bind paramter by C<bind_param()> of statment handle.
2434

            
2435
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2436

            
update pod
Yuki Kimoto authored on 2011-03-12
2437
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2438

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2439
=head2 C<select_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2440

            
update pod
Yuki Kimoto authored on 2011-03-12
2441
Select statement, using primary key.
2442

            
2443
    $dbi->select_at(
2444
        table => 'book',
2445
        primary_key => 'id',
2446
        where => '5'
2447
    );
2448

            
update pod
Yuki Kimoto authored on 2011-03-13
2449
This method is same as C<select()> exept that
2450
C<primary_key> is specified and C<where> is constant value or array refrence.
update pod
Yuki Kimoto authored on 2011-03-12
2451
all option of C<select()> is available.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2452

            
update pod
Yuki Kimoto authored on 2011-03-13
2453
=over 4
2454

            
2455
=item C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2456

            
update pod
Yuki Kimoto authored on 2011-03-12
2457
Primary key. This is constant value or array reference.
2458
    
2459
    # Constant value
2460
    $dbi->select(primary_key => 'id');
2461

            
2462
    # Array reference
2463
    $dbi->select(primary_key => ['id1', 'id2' ]);
2464

            
update pod
Yuki Kimoto authored on 2011-03-13
2465
This is used to create where clause.
2466

            
update pod
Yuki Kimoto authored on 2011-03-13
2467
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-12
2468

            
update pod
Yuki Kimoto authored on 2011-03-13
2469
Where clause, created from primary key information.
update pod
Yuki Kimoto authored on 2011-03-12
2470
This is constant value or array reference.
2471

            
2472
    # Constant value
2473
    $dbi->select(where => 5);
2474

            
2475
    # Array reference
2476
    $dbi->select(where => [3, 5]);
2477

            
2478
In first examle, the following SQL is created.
2479

            
2480
    select * from book where id = ?
2481

            
2482
Place holder is set to 5.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2483

            
update pod
Yuki Kimoto authored on 2011-03-13
2484
=back
2485

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2488
    $dbi->update(
2489
        table  => 'book',
2490
        param  => {title => 'Perl'},
2491
        where  => {id => 4}
2492
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2493

            
update pod
Yuki Kimoto authored on 2011-03-13
2494
Update statement.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2495

            
update pod
Yuki Kimoto authored on 2011-03-13
2496
The following opitons are currently available.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2497

            
update pod
Yuki Kimoto authored on 2011-03-13
2498
=over 4
2499

            
update pod
Yuki Kimoto authored on 2011-03-13
2500
=item C<table>
2501

            
update pod
Yuki Kimoto authored on 2011-03-13
2502
Table name.
2503

            
2504
    $dbi->update(table => 'book');
2505

            
2506
=item C<param>
2507

            
2508
Update data. This is hash reference.
2509

            
2510
    $dbi->update(param => {title => 'Perl'});
2511

            
2512
=item C<where>
2513

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2514
Where clause. This is hash reference or L<DBIx::Custom::Where> object
2515
or array refrence.
update pod
Yuki Kimoto authored on 2011-03-13
2516
    
2517
    # Hash reference
2518
    $dbi->update(where => {author => 'Ken', 'title' => 'Perl'});
2519
    
2520
    # DBIx::Custom::Where object
2521
    my $where = $dbi->where(
2522
        clause => ['and', '{= author}', '{like title}'],
2523
        param  => {author => 'Ken', title => '%Perl%'}
2524
    );
2525
    $dbi->update(where => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2526
    
2527
    # Array refrendce (where clause and parameter)
2528
    $dbi->update(where =>
2529
        [
2530
            ['and', '{= author}', '{like title}'],
2531
            {author => 'Ken', title => '%Perl%'}
2532
        ]
2533
    );
update pod
Yuki Kimoto authored on 2011-03-13
2534

            
2535
=item C<append>
2536

            
2537
Append statement to last of SQL. This is string.
2538

            
2539
    $dbi->update(append => 'order by title');
2540

            
2541
=item C<filter>
2542

            
2543
Filter, executed before data is send to database. This is array reference.
2544
Filter value is code reference or
2545
filter name registerd by C<register_filter()>.
2546

            
2547
    # Basic
2548
    $dbi->update(
2549
        filter => [
2550
            title  => sub { uc $_[0] }
2551
            author => sub { uc $_[0] }
2552
        ]
2553
    );
2554
    
2555
    # At once
2556
    $dbi->update(
2557
        filter => [
2558
            [qw/title author/]  => sub { uc $_[0] }
2559
        ]
2560
    );
2561
    
2562
    # Filter name
2563
    $dbi->update(
2564
        filter => [
2565
            title  => 'upper_case',
2566
            author => 'upper_case'
2567
        ]
2568
    );
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2569

            
update pod
Yuki Kimoto authored on 2011-03-13
2570
These filters are added to the C<out> filters, set by C<apply_filter()>.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2571

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2572
=item C<query>
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2573

            
update pod
Yuki Kimoto authored on 2011-03-13
2574
Get L<DBIx::Custom::Query> object instead of executing SQL.
2575
This is true or false value.
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2576

            
update pod
Yuki Kimoto authored on 2011-03-13
2577
    my $query = $dbi->update(query => 1);
2578

            
2579
You can check SQL.
2580

            
2581
    my $sql = $query->sql;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2582

            
update pod
Yuki Kimoto authored on 2011-03-13
2583
=back
2584

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2587
    $dbi->update_all(table => 'book', param => {title => 'Perl'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
2588

            
update pod
Yuki Kimoto authored on 2011-03-13
2589
Update statement to update all rows.
2590
Options is same as C<update()>.
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2591

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2592
=head2 C<update_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2593

            
update pod
Yuki Kimoto authored on 2011-03-13
2594
Update statement, using primary key.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2595

            
2596
    $dbi->update_at(
2597
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2598
        primary_key => 'id',
2599
        where => '5',
2600
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2601
    );
2602

            
update pod
Yuki Kimoto authored on 2011-03-13
2603
This method is same as C<update()> exept that
2604
C<primary_key> is specified and C<where> is constant value or array refrence.
2605
all option of C<update()> is available.
2606

            
update pod
Yuki Kimoto authored on 2011-03-13
2607
=over 4
2608

            
2609
=item C<primary_key>
update pod
Yuki Kimoto authored on 2011-03-13
2610

            
2611
Primary key. This is constant value or array reference.
2612
    
2613
    # Constant value
2614
    $dbi->update(primary_key => 'id');
2615

            
2616
    # Array reference
2617
    $dbi->update(primary_key => ['id1', 'id2' ]);
2618

            
2619
This is used to create where clause.
2620

            
update pod
Yuki Kimoto authored on 2011-03-13
2621
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
2622

            
2623
Where clause, created from primary key information.
2624
This is constant value or array reference.
2625

            
2626
    # Constant value
2627
    $dbi->update(where => 5);
2628

            
2629
    # Array reference
2630
    $dbi->update(where => [3, 5]);
2631

            
2632
In first examle, the following SQL is created.
2633

            
2634
    update book set title = ? where id = ?
2635

            
2636
Place holders are set to 'Perl' and 5.
2637

            
update pod
Yuki Kimoto authored on 2011-03-13
2638
=back
2639

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2640
=head2 C<update_param>
update pod
Yuki Kimoto authored on 2011-03-13
2641

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

            
2644
Create update parameter tag.
2645

            
2646
    {update_param title age}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2647

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2648
=head2 C<where>
fix tests
Yuki Kimoto authored on 2011-01-18
2649

            
cleanup
Yuki Kimoto authored on 2011-03-09
2650
    my $where = $dbi->where(
2651
        clause => ['and', '{= title}', '{= author}'],
2652
        param => {title => 'Perl', author => 'Ken'}
2653
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2654

            
2655
Create a new L<DBIx::Custom::Where> object.
2656

            
update pod
Yuki Kimoto authored on 2011-03-13
2657
=head2 C<setup_model> EXPERIMENTAL
cleanup
Yuki Kimoto authored on 2011-01-12
2658

            
update pod
Yuki Kimoto authored on 2011-03-13
2659
    $dbi->setup_model;
cleanup
Yuki Kimoto authored on 2011-01-12
2660

            
update pod
Yuki Kimoto authored on 2011-03-13
2661
Setup all model objects.
update pod
Yuki Kimoto authored on 2011-03-13
2662
C<columns> of model object is automatically set, parsing database information.
cleanup
Yuki Kimoto authored on 2011-01-12
2663

            
cleanup
Yuki Kimoto authored on 2011-01-25
2664
=head1 Tags
2665

            
2666
The following tags is available.
2667

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2668
=head2 C<table>
add table tag
Yuki Kimoto authored on 2011-02-09
2669

            
2670
Table tag
2671

            
2672
    {table TABLE}    ->    TABLE
2673

            
update pod
Yuki Kimoto authored on 2011-03-13
2674
This is used to tell C<execute()> what table is needed .
add table tag
Yuki Kimoto authored on 2011-02-09
2675

            
cleanup
Yuki Kimoto authored on 2011-01-25
2676
=head2 C<?>
2677

            
2678
Placeholder tag.
2679

            
2680
    {? NAME}    ->   ?
2681

            
2682
=head2 C<=>
2683

            
2684
Equal tag.
2685

            
2686
    {= NAME}    ->   NAME = ?
2687

            
2688
=head2 C<E<lt>E<gt>>
2689

            
2690
Not equal tag.
2691

            
2692
    {<> NAME}   ->   NAME <> ?
2693

            
2694
=head2 C<E<lt>>
2695

            
2696
Lower than tag
2697

            
2698
    {< NAME}    ->   NAME < ?
2699

            
2700
=head2 C<E<gt>>
2701

            
2702
Greater than tag
2703

            
2704
    {> NAME}    ->   NAME > ?
2705

            
2706
=head2 C<E<gt>=>
2707

            
2708
Greater than or equal tag
2709

            
2710
    {>= NAME}   ->   NAME >= ?
2711

            
2712
=head2 C<E<lt>=>
2713

            
2714
Lower than or equal tag
2715

            
2716
    {<= NAME}   ->   NAME <= ?
2717

            
2718
=head2 C<like>
2719

            
2720
Like tag
2721

            
2722
    {like NAME}   ->   NAME like ?
2723

            
2724
=head2 C<in>
2725

            
2726
In tag.
2727

            
2728
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2729

            
2730
=head2 C<insert_param>
2731

            
2732
Insert parameter tag.
2733

            
2734
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2735

            
2736
=head2 C<update_param>
2737

            
2738
Updata parameter tag.
2739

            
2740
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2741

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2744
L<DBIx::Custom> is stable. APIs keep backword compatible
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2745
except EXPERIMENTAL one in the feature.
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
2746

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

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

            
2751
C<< <kimoto.yuki at gmail.com> >>
2752

            
2753
L<http://github.com/yuki-kimoto/DBIx-Custom>
2754

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2755
=head1 AUTHOR
2756

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

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

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

            
2763
This program is free software; you can redistribute it and/or modify it
2764
under the same terms as Perl itself.
2765

            
2766
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2767

            
2768