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

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
3
our $VERSION = '0.1668';
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',
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
58
    reserved_word_quote => '',
update pod
Yuki Kimoto authored on 2011-03-13
59
    safety_character => '\w',
60
    stash => sub { {} }
fix tests
Yuki Kimoto authored on 2011-01-13
61
);
cleanup
yuki-kimoto authored on 2010-10-17
62

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

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

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

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

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

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

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

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

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

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

            
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
201
        # Bind
202
        my $columns = $query->columns;
203
        if (my $q = $self->reserved_word_quote) {
204
            foreach my $column (@$columns) {
205
                $column =~ s/$q//g;
206
            }
207
        }
208

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

            
update pod
Yuki Kimoto authored on 2011-03-13
231
sub dbh {
232
    my $self = shift;
233

            
234
    if (@_) {
235
        $self->{dbh} = $_[0];
236
        return $self;
237
    }
238
    else {
239
        my $pid = $$;
240
        if ($self->pid eq $pid) {
241
            return $self->{dbh};
242
        }
243
        else {
244
            # Create new connection in child process
245
            croak "Process is forked in transaction"
246
              unless $self->{dbh}->{AutoCommit};
247
            $self->pid($pid);
248
            $self->{dbh}->{InactiveDestroy} = 1;
249
            return $self->{dbh} = $self->_connect;
250
        }
251
    }
252
}
253

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

            
cleanup
yuki-kimoto authored on 2010-10-17
257
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
258
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
259

            
260
    # Quote for reserved word
261
    my $q = $self->reserved_word_quote;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
262
    
cleanup
Yuki Kimoto authored on 2011-03-09
263
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
264
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
265
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
266
          unless $DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
267
    }
268
    
269
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
270
    my $table = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
271
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
272
    my $where            = delete $args{where} || {};
273
    my $append           = delete $args{append};
274
    my $allow_delete_all = delete $args{allow_delete_all};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
275

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
276
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
277
    my $w = $self->_where($where);
278
    $where = $w->param;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
279
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
280
    # String where
281
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
282
    
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
283
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
284
      if $swhere eq '' && !$allow_delete_all;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
285

            
cleanup
Yuki Kimoto authored on 2011-01-27
286
    # SQL stack
287
    my @sql;
288

            
289
    # Delete
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
290
    push @sql, "delete from $q$table$q $swhere";
cleanup
Yuki Kimoto authored on 2011-01-27
291
    push @sql, $append if $append;
292
    
293
    my $sql = join(' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
294
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
295
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
296
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
297
    return $query if $args{query};
298
    
packaging one directory
yuki-kimoto authored on 2009-11-16
299
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
300
    my $ret_val = $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
301
        $query,
302
        param  => $where,
303
        table => $table,
304
        %args
305
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
306
    
307
    return $ret_val;
308
}
309

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

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

            
314
sub delete_at {
315
    my ($self, %args) = @_;
316
    
cleanup
Yuki Kimoto authored on 2011-03-09
317
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
318
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
319
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
320
          unless $DELETE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
321
    }
322
    
323
    # Primary key
324
    my $primary_keys = delete $args{primary_key};
325
    $primary_keys = [$primary_keys] unless ref $primary_keys;
326
    
327
    # Where clause
328
    my $where = {};
329
    if (exists $args{where}) {
330
        my $where_columns = delete $args{where};
331
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
332

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

            
added helper method
yuki-kimoto authored on 2010-10-17
352
sub DESTROY { }
353

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

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
356
sub create_model {
357
    my $self = shift;
358
    
359
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
360
    $args->{dbi} = $self;
361
    
362
    my $model_class = delete $args->{model_class} || 'DBIx::Custom::Model';
363
    my $model_name  = delete $args->{name};
364
    my $model_table = delete $args->{table};
365
    $model_name ||= $model_table;
366
    
367
    my $model = $model_class->new($args);
368
    $model->name($model_name) unless $model->name;
369
    $model->table($model_table) unless $model->table;
370
    
371
    # Apply filter
372
    croak "$model_class filter must be array reference"
373
      unless ref $model->filter eq 'ARRAY';
374
    $self->apply_filter($model->table, @{$model->filter});
375
    
376
    # Table - Model
377
    croak "Table name is duplicated"
378
      if exists $self->{_model_from}->{$model->table};
379
    $self->{_model_from}->{$model->table} = $model->name;
380

            
381
    # Table alias
382
    $self->{_table_alias} ||= {};
383
    $self->{_table_alias} = {%{$self->{_table_alias}}, %{$model->table_alias}};
384
    
385
    # Set model
386
    $self->model($model->name, $model);
387
    
create_model() return model
Yuki Kimoto authored on 2011-03-29
388
    return $self->model($model->name);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
389
}
390

            
391
sub each_column {
392
    my ($self, $cb) = @_;
393
    
394
    # Iterate all tables
395
    my $sth_tables = $self->dbh->table_info;
396
    while (my $table_info = $sth_tables->fetchrow_hashref) {
397
        
398
        # Table
399
        my $table = $table_info->{TABLE_NAME};
400
        
401
        # Iterate all columns
402
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
403
        while (my $column_info = $sth_columns->fetchrow_hashref) {
404
            my $column = $column_info->{COLUMN_NAME};
405
            $self->$cb($table, $column, $column_info);
406
        }
407
    }
408
}
409

            
cleanup
yuki-kimoto authored on 2010-10-17
410
sub execute{
411
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
412
    
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
413
    # Quote for reserved word
414
    my $q = $self->reserved_word_quote;
415
    
cleanup
Yuki Kimoto authored on 2011-03-09
416
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
417
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
418
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
419
          unless $EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
420
    }
421
    
cleanup
yuki-kimoto authored on 2010-10-17
422
    my $params = $args{param} || {};
packaging one directory
yuki-kimoto authored on 2009-11-16
423
    
cleanup
yuki-kimoto authored on 2010-10-17
424
    # First argument is the soruce of SQL
425
    $query = $self->create_query($query)
426
      unless ref $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
427
    
add table tag
Yuki Kimoto authored on 2011-02-09
428
    # Applied filter
cleanup
Yuki Kimoto authored on 2011-01-12
429
    my $filter = {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
430
    
add table tag
Yuki Kimoto authored on 2011-02-09
431
    my $tables = $query->tables;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
432
    if ($q) {
433
        foreach my $table (@$tables) {
434
            $table =~ s/$q//g;
435
        }
436
    }
add table tag
Yuki Kimoto authored on 2011-02-09
437
    my $arg_tables = $args{table} || [];
438
    $arg_tables = [$arg_tables]
439
      unless ref $arg_tables eq 'ARRAY';
440
    push @$tables, @$arg_tables;
cleanup
Yuki Kimoto authored on 2011-03-09
441

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

            
550
        return $result;
551
    }
552
    return $affected;
553
}
554

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

            
cleanup
yuki-kimoto authored on 2010-10-17
557
sub insert {
558
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
559
    
560
    # Quote for reserved word
561
    my $q = $self->reserved_word_quote;
cleanup
yuki-kimoto authored on 2010-10-17
562

            
cleanup
Yuki Kimoto authored on 2011-03-09
563
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
564
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
565
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
566
          unless $INSERT_ARGS{$name};
packaging one directory
yuki-kimoto authored on 2009-11-16
567
    }
568
    
cleanup
yuki-kimoto authored on 2010-10-17
569
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
570
    my $table  = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
571
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
572
    my $param  = delete $args{param} || {};
573
    my $append = delete $args{append} || '';
cleanup
yuki-kimoto authored on 2010-10-17
574
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
575
    # Columns
576
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
577
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
578
    foreach my $column (keys %$param) {
579
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
580
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
581
          $column = "$q$column$q";
582
          $column =~ s/\./$q.$q/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
583
        push @columns, $column;
584
    }
cleanup
yuki-kimoto authored on 2010-10-17
585
    
cleanup
Yuki Kimoto authored on 2011-01-27
586
    # SQL stack
587
    my @sql;
588
    
589
    # Insert
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
590
    push @sql, "insert into $q$table$q {insert_param ". join(' ', @columns) . '}';
cleanup
Yuki Kimoto authored on 2011-01-27
591
    push @sql, $append if $append;
592
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
593
    # SQL
cleanup
Yuki Kimoto authored on 2011-01-27
594
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
595
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
596
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
597
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
598
    return $query if $args{query};
599
    
packaging one directory
yuki-kimoto authored on 2009-11-16
600
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
601
    my $ret_val = $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
602
        $query,
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
603
        param  => $param,
cleanup
Yuki Kimoto authored on 2011-03-21
604
        table => $table,
605
        %args
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
606
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
607
    
608
    return $ret_val;
609
}
610

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

            
613
sub insert_at {
614
    my ($self, %args) = @_;
615
    
cleanup
Yuki Kimoto authored on 2011-03-09
616
    # Check argument names
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
617
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
618
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
619
          unless $INSERT_AT_ARGS{$name};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
620
    }
621
    
622
    # Primary key
623
    my $primary_keys = delete $args{primary_key};
624
    $primary_keys = [$primary_keys] unless ref $primary_keys;
625
    
626
    # Where clause
627
    my $where = {};
628
    my $param = {};
629
    
630
    if (exists $args{where}) {
631
        my $where_columns = delete $args{where};
632
        $where_columns = [$where_columns] unless ref $where_columns;
633

            
634
        croak qq{"where" must be constant value or array reference}
635
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
636
        
637
        for(my $i = 0; $i < @$primary_keys; $i ++) {
638
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
639
        }
640
    }
641
    
642
    if (exists $args{param}) {
643
        $param = delete $args{param};
644
        for(my $i = 0; $i < @$primary_keys; $i ++) {
645
             delete $param->{$primary_keys->[$i]};
646
        }
647
    }
648
    
649
    $param = {%$param, %$where};
650
    
651
    return $self->insert(param => $param, %args);
652
}
653

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
654
sub insert_param {
655
    my ($self, $param) = @_;
656
    
update pod
Yuki Kimoto authored on 2011-03-13
657
    # Insert parameter tag
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
658
    my @names;
659
    my @placeholders;
660
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
661
    my $safety = $self->safety_character;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
662
    my $q = $self->reserved_word_quote;
663
    
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
664
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
665
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
666
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
667
        
668
        my $c = "$q$column$q";
669
        $c =~ s/\./$q.$q/;
670
        
671
        push @names, $c;
672
        push @placeholders, "{? $c}";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
673
    }
674
    
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
675
    return '(' . join(', ', @names) . ') ' . 'values' .
676
           ' (' . join(', ', @placeholders) . ')';
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
677
}
678

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
679
sub include_model {
680
    my ($self, $name_space, $model_infos) = @_;
681
    
682
    $name_space ||= '';
683
    unless ($model_infos) {
684
        # Load name space module
685
        croak qq{"$name_space" is invalid class name}
686
          if $name_space =~ /[^\w:]/;
687
        eval "use $name_space";
688
        croak qq{Name space module "$name_space.pm" is needed. $@} if $@;
689
        
690
        # Search model modules
691
        my $path = $INC{"$name_space.pm"};
692
        $path =~ s/\.pm$//;
693
        opendir my $dh, $path
694
          or croak qq{Can't open directory "$path": $!};
695
        $model_infos = [];
696
        while (my $module = readdir $dh) {
697
            push @$model_infos, $module
698
              if $module =~ s/\.pm$//;
699
        }
700
        
701
        close $dh;
702
    }
703
    
704
    foreach my $model_info (@$model_infos) {
705
        
706
        # Model class, name, table
707
        my $model_class;
708
        my $model_name;
709
        my $model_table;
710
        if (ref $model_info eq 'HASH') {
711
            $model_class = $model_info->{class};
712
            $model_name  = $model_info->{name};
713
            $model_table = $model_info->{table};
714
            
715
            $model_name  ||= $model_class;
716
            $model_table ||= $model_name;
717
        }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
718
        else { $model_class = $model_name = $model_table = $model_info }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
719
        my $mclass = "${name_space}::$model_class";
720
        
721
        # Load
722
        croak qq{"$mclass" is invalid class name}
723
          if $mclass =~ /[^\w:]/;
724
        unless ($mclass->can('isa')) {
725
            eval "use $mclass";
726
            croak $@ if $@;
727
        }
728
        
729
        # Instantiate
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
730
        my $args = {};
731
        $args->{model_class} = $mclass if $mclass;
732
        $args->{name}        = $model_name if $model_name;
733
        $args->{table}       = $model_table if $model_table;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
734
        
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
735
        # Create model
736
        $self->create_model($args);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
737
    }
738
    
739
    return $self;
740
}
741

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
742
sub merge_param {
743
    my ($self, @params) = @_;
744
    
745
    my $param = {};
746
    
747
    foreach my $p (@params) {
748
        foreach my $column (keys %$p) {
749
            if (exists $param->{$column}) {
750
                $param->{$column} = [$param->{$column}]
751
                  unless ref $param->{$column} eq 'ARRAY';
752
                push @{$param->{$column}}, $p->{$column};
753
            }
754
            else {
755
                $param->{$column} = $p->{$column};
756
            }
757
        }
758
    }
759
    
760
    return $param;
761
}
762

            
cleanup
Yuki Kimoto authored on 2011-03-21
763
sub method {
764
    my $self = shift;
765
    
766
    # Merge
767
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
768
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
769
    
770
    return $self;
771
}
772

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
773
sub model {
774
    my ($self, $name, $model) = @_;
775
    
776
    # Set
777
    if ($model) {
778
        $self->models->{$name} = $model;
779
        return $self;
780
    }
781
    
782
    # Check model existance
783
    croak qq{Model "$name" is not included}
784
      unless $self->models->{$name};
785
    
786
    # Get
787
    return $self->models->{$name};
788
}
789

            
cleanup
Yuki Kimoto authored on 2011-03-21
790
sub mycolumn {
791
    my ($self, $table, $columns) = @_;
792
    
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
793
    my $q = $self->reserved_word_quote;
794
    
cleanup
Yuki Kimoto authored on 2011-03-21
795
    $columns ||= [];
796
    my @column;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
797
    push @column, "$q$table$q.$q$_$q as $q$_$q" for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
798
    
799
    return join (', ', @column);
800
}
801

            
added dbi_options attribute
kimoto authored on 2010-12-20
802
sub new {
803
    my $self = shift->SUPER::new(@_);
804
    
805
    # Check attribute names
806
    my @attrs = keys %$self;
807
    foreach my $attr (@attrs) {
808
        croak qq{"$attr" is invalid attribute name}
809
          unless $self->can($attr);
810
    }
cleanup
Yuki Kimoto authored on 2011-01-25
811

            
812
    $self->register_tag(
813
        '?'     => \&DBIx::Custom::Tag::placeholder,
814
        '='     => \&DBIx::Custom::Tag::equal,
815
        '<>'    => \&DBIx::Custom::Tag::not_equal,
816
        '>'     => \&DBIx::Custom::Tag::greater_than,
817
        '<'     => \&DBIx::Custom::Tag::lower_than,
818
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
819
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
820
        'like'  => \&DBIx::Custom::Tag::like,
821
        'in'    => \&DBIx::Custom::Tag::in,
822
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
823
        'update_param' => \&DBIx::Custom::Tag::update_param
824
    );
added dbi_options attribute
kimoto authored on 2010-12-20
825
    
826
    return $self;
827
}
828

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

            
cleanup
yuki-kimoto authored on 2010-10-17
831
sub register_filter {
832
    my $invocant = shift;
833
    
834
    # Register filter
835
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
836
    $invocant->filters({%{$invocant->filters}, %$filters});
837
    
838
    return $invocant;
839
}
packaging one directory
yuki-kimoto authored on 2009-11-16
840

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

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

            
packaging one directory
yuki-kimoto authored on 2009-11-16
846
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
847
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
848

            
849
    # Quote for reserved word
850
    my $q = $self->reserved_word_quote;
packaging one directory
yuki-kimoto authored on 2009-11-16
851
    
cleanup
Yuki Kimoto authored on 2011-03-09
852
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
853
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
854
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
855
          unless $SELECT_ARGS{$name};
refactoring select
yuki-kimoto authored on 2010-04-28
856
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
857
    
refactoring select
yuki-kimoto authored on 2010-04-28
858
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
859
    my $table = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
860
    my $tables = ref $table eq 'ARRAY' ? $table
861
               : defined $table ? [$table]
862
               : [];
cleanup
Yuki Kimoto authored on 2011-03-21
863
    my $columns   = delete $args{column};
864
    my $where     = delete $args{where} || {};
865
    my $append    = delete $args{append};
866
    my $join      = delete $args{join} || [];
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
867
    croak qq{"join" must be array reference}
868
      unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-03-21
869
    my $relation = delete $args{relation};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
870
    
cleanup
Yuki Kimoto authored on 2011-03-09
871
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
872
    $self->_add_relation_table($tables, $relation);
packaging one directory
yuki-kimoto authored on 2009-11-16
873
    
cleanup
Yuki Kimoto authored on 2011-01-27
874
    # SQL stack
875
    my @sql;
876
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
877
    
cleanup
Yuki Kimoto authored on 2011-03-30
878
    if ($columns) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
879

            
880
        $columns = [$columns] if ! ref $columns;
881
        
882
        if (ref $columns eq 'HASH') {
883
            # Find tables
884
            my $main_table;
885
            my %tables;
886
            if ($columns->{table}) {
887
                foreach my $table (@{$columns->{table}}) {
888
                    if (($table || '') eq $tables->[-1]) {
889
                        $main_table = $table;
890
                    }
891
                    else {
892
                        $tables{$table} = 1;
893
                    }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
894
                }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
895
            }
896
            elsif ($columns->{all}) {
897
                $main_table = $tables->[-1] || '';
898
                foreach my $j (@$join) {
899
                    my $tables = $self->_tables($j);
900
                    foreach my $table (@$tables) {
901
                        $tables{$table} = 1;
902
                    }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
903
                }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
904
                delete $tables{$main_table};
905
            }
906
            
907
            push @sql, $columns->{prepend} if $columns->{prepend};
908
            
909
            # Column clause of main table
910
            if ($main_table) {
cleanup
Yuki Kimoto authored on 2011-03-21
911
                push @sql, $self->model($main_table)->mycolumn;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
912
                push @sql, ',';
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
913
            }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
914
            
915
            # Column cluase of other tables
916
            foreach my $table (keys %tables) {
917
                unshift @$tables, $table;
cleanup
Yuki Kimoto authored on 2011-03-21
918
                push @sql, $self->model($table)->column($table);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
919
                push @sql, ',';
920
            }
921
            pop @sql if $sql[-1] eq ',';
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
922
        }
923
        else {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
924
            foreach my $column (@$columns) {
925
                unshift @$tables, @{$self->_tables($column)};
926
                push @sql, ($column, ',');
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
927
            }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
928
            pop @sql if $sql[-1] eq ',';
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
929
        }
930
    }
931
    
932
    # "*" is default
933
    else { push @sql, '*' }
934
    
935
    # Table
cleanup
Yuki Kimoto authored on 2011-03-30
936
    push @sql, 'from';
937
    if ($relation) {
938
        my $found = {};
939
        foreach my $table (@$tables) {
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
940
            push @sql, ("$q$table$q", ',') unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
941
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
942
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
943
    }
cleanup
Yuki Kimoto authored on 2011-03-30
944
    else {
945
        my $main_table = $tables->[-1] || '';
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
946
        push @sql, "$q$main_table$q";
cleanup
Yuki Kimoto authored on 2011-03-30
947
    }
948
    pop @sql if ($sql[-1] || '') eq ',';
packaging one directory
yuki-kimoto authored on 2009-11-16
949
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
950
    # Main table
951
    croak "Not found table name" unless $tables->[-1];
952
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
953
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
954
    my $w = $self->_where($where);
955
    $where = $w->param;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
956
    
957
    # String where
958
    my $swhere = "$w";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
959
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
960
    # Add table names in where clause
961
    unshift @$tables, @{$self->_tables($swhere)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
962
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
963
    # Push join
964
    $self->_push_join(\@sql, $join, $tables);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
965
    
cleanup
Yuki Kimoto authored on 2011-03-09
966
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-01-27
967
    push @sql, $swhere;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
968
    
cleanup
Yuki Kimoto authored on 2011-03-08
969
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
970
    $self->_push_relation(\@sql, $tables, $relation, $swhere eq '' ? 1 : 0);
cleanup
Yuki Kimoto authored on 2011-03-08
971
    
cleanup
Yuki Kimoto authored on 2011-01-27
972
    # Append statement
973
    push @sql, $append if $append;
974
    
975
    # SQL
976
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
977
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
978
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
979
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
980
    return $query if $args{query};
981
    
packaging one directory
yuki-kimoto authored on 2009-11-16
982
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
983
    my $result = $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
984
        $query,
985
        param  => $where, 
986
        table => $tables,
987
        %args
988
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
989
    
990
    return $result;
991
}
992

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

            
995
sub select_at {
996
    my ($self, %args) = @_;
997
    
cleanup
Yuki Kimoto authored on 2011-03-09
998
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
999
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
1000
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
1001
          unless $SELECT_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1002
    }
1003
    
1004
    # Primary key
1005
    my $primary_keys = delete $args{primary_key};
1006
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1007
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
1008
    # Table
1009
    croak qq{"table" option must be specified} unless $args{table};
1010
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
1011
    
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1012
    # Where clause
1013
    my $where = {};
1014
    if (exists $args{where}) {
1015
        my $where_columns = delete $args{where};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1016
        
1017
        croak qq{"where" must be constant value or array reference}
1018
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
1019
        
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1020
        $where_columns = [$where_columns] unless ref $where_columns;
1021
        
1022
        for(my $i = 0; $i < @$primary_keys; $i ++) {
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
1023
           $where->{$table . '.' . $primary_keys->[$i]} = $where_columns->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1024
        }
1025
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1026
    
1027
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1028
        my $param = delete $args{param};
1029
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1030
             delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1031
        }
1032
    }
1033
    
1034
    return $self->select(where => $where, %args);
1035
}
1036

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1037
sub setup_model {
1038
    my $self = shift;
1039
    
1040
    $self->each_column(
1041
        sub {
1042
            my ($self, $table, $column, $column_info) = @_;
1043
            
1044
            if (my $model = $self->models->{$table}) {
1045
                push @{$model->columns}, $column;
1046
            }
1047
        }
1048
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
1049
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1050
}
1051

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

            
1055
sub update {
1056
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1057

            
1058
    # Quote for reserved word
1059
    my $q = $self->reserved_word_quote;
version 0.0901
yuki-kimoto authored on 2009-12-17
1060
    
cleanup
Yuki Kimoto authored on 2011-03-09
1061
    # Check argument names
cleanup
yuki-kimoto authored on 2010-10-17
1062
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
1063
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
1064
          unless $UPDATE_ARGS{$name};
removed reconnect method
yuki-kimoto authored on 2010-05-28
1065
    }
added cache_method attribute
yuki-kimoto authored on 2010-06-25
1066
    
cleanup
yuki-kimoto authored on 2010-10-17
1067
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
1068
    my $table = delete $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
1069
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
1070
    my $param            = delete $args{param} || {};
1071
    my $where            = delete $args{where} || {};
1072
    my $append           = delete $args{append} || '';
1073
    my $allow_update_all = delete $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
1074
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1075
    # Columns
1076
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1077
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1078
    foreach my $column (keys %$param) {
1079
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1080
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1081
          $column = "$q$column$q";
1082
          $column =~ s/\./$q.$q/;
1083
        push @columns, "$column";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1084
    }
1085
        
cleanup
yuki-kimoto authored on 2010-10-17
1086
    # Update clause
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1087
    my $update_clause = '{update_param ' . join(' ', @columns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1088

            
1089
    # Where
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1090
    my $w = $self->_where($where);
1091
    $where = $w->param;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1092
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1093
    # String where
1094
    my $swhere = "$w";
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1095
    
1096
    croak qq{"where" must be specified}
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1097
      if "$swhere" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1098
    
cleanup
Yuki Kimoto authored on 2011-01-27
1099
    # SQL stack
1100
    my @sql;
1101
    
1102
    # Update
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1103
    push @sql, "update $q$table$q $update_clause $swhere";
cleanup
Yuki Kimoto authored on 2011-01-27
1104
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1105
    
cleanup
yuki-kimoto authored on 2010-10-17
1106
    # Rearrange parameters
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1107
    foreach my $wkey (keys %$where) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1108
        
cleanup
yuki-kimoto authored on 2010-10-17
1109
        if (exists $param->{$wkey}) {
1110
            $param->{$wkey} = [$param->{$wkey}]
1111
              unless ref $param->{$wkey} eq 'ARRAY';
1112
            
1113
            push @{$param->{$wkey}}, $where->{$wkey};
1114
        }
1115
        else {
1116
            $param->{$wkey} = $where->{$wkey};
1117
        }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1118
    }
cleanup
yuki-kimoto authored on 2010-10-17
1119
    
cleanup
Yuki Kimoto authored on 2011-01-27
1120
    # SQL
1121
    my $sql = join(' ', @sql);
1122
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1123
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
1124
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
1125
    return $query if $args{query};
1126
    
cleanup
yuki-kimoto authored on 2010-10-17
1127
    # Execute query
cleanup
Yuki Kimoto authored on 2011-03-21
1128
    my $ret_val = $self->execute(
1129
        $query,
1130
        param  => $param, 
1131
        table => $table,
1132
        %args
1133
    );
cleanup
yuki-kimoto authored on 2010-10-17
1134
    
1135
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1136
}
1137

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

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

            
1142
sub update_at {
1143
    my ($self, %args) = @_;
1144
    
cleanup
Yuki Kimoto authored on 2011-03-09
1145
    # Check argument names
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1146
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-03-09
1147
        croak qq{Argument "$name" is invalid name}
cleanup
Yuki Kimoto authored on 2011-03-21
1148
          unless $UPDATE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1149
    }
1150
    
1151
    # Primary key
1152
    my $primary_keys = delete $args{primary_key};
1153
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1154
    
1155
    # Where clause
1156
    my $where = {};
1157
    my $param = {};
1158
    
1159
    if (exists $args{where}) {
1160
        my $where_columns = delete $args{where};
1161
        $where_columns = [$where_columns] unless ref $where_columns;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1162

            
1163
        croak qq{"where" must be constant value or array reference}
1164
          unless !ref $where_columns || ref $where_columns eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1165
        
1166
        for(my $i = 0; $i < @$primary_keys; $i ++) {
1167
           $where->{$primary_keys->[$i]} = $where_columns->[$i];
1168
        }
1169
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1170
    
1171
    if (exists $args{param}) {
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1172
        $param = delete $args{param};
1173
        for(my $i = 0; $i < @$primary_keys; $i ++) {
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1174
            delete $param->{$primary_keys->[$i]};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1175
        }
1176
    }
1177
    
1178
    return $self->update(where => $where, param => $param, %args);
1179
}
1180

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1181
sub update_param {
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1182
    my ($self, $param, $opt) = @_;
1183
    
1184
    # Insert parameter tag
1185
    my @params;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1186
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1187
    my $safety = $self->safety_character;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1188
    my $q = $self->reserved_word_quote;
1189
    
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1190
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1191
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1192
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1193
        
1194
        my $c = "$q$column$q";
1195
        $c =~ s/\./$q.$q/;
1196
        
1197
        push @params, "$c = {? $c}";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1198
    }
1199
    
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1200
    my $clause;
1201
    $clause .= 'set ' unless $opt->{no_set};
1202
    $clause .= join(', ', @params);
1203
    
1204
    return $clause;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1205
}
1206

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

            
1210
    return DBIx::Custom::Where->new(
1211
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1212
        safety_character => $self->safety_character,
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1213
        reserved_word_quote => $self->reserved_word_quote,
cleanup
Yuki Kimoto authored on 2011-03-09
1214
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1215
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1216
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1217

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1218
sub _bind {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1219
    my ($self, $params, $columns, $filter, $type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1220
    
cleanup
Yuki Kimoto authored on 2011-01-12
1221
    # bind values
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1222
    my $bind = [];
add tests
yuki-kimoto authored on 2010-08-08
1223
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
1224
    # Build bind values
1225
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1226
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1227
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1228
        
1229
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1230
        my $value;
1231
        if(ref $params->{$column} eq 'ARRAY') {
1232
            my $i = $count->{$column} || 0;
1233
            $i += $not_exists->{$column} || 0;
1234
            my $found;
1235
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1236
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1237
                    $not_exists->{$column}++;
1238
                }
1239
                else  {
1240
                    $value = $params->{$column}->[$k];
1241
                    $found = 1;
1242
                    last
1243
                }
1244
            }
1245
            next unless $found;
1246
        }
1247
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1248
        
cleanup
Yuki Kimoto authored on 2011-01-12
1249
        # Filter
1250
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1251
        
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1252
        # Type
1253
        push @$bind, {
1254
            value => $f ? $f->($value) : $value,
1255
            type => $type->{$column}
1256
        };
removed reconnect method
yuki-kimoto authored on 2010-05-28
1257
        
1258
        # Count up 
1259
        $count->{$column}++;
1260
    }
1261
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1262
    return $bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1263
}
1264

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1265
sub _connect {
1266
    my $self = shift;
1267
    
1268
    # Attributes
1269
    my $data_source = $self->data_source;
1270
    croak qq{"data_source" must be specified to connect()"}
1271
      unless $data_source;
1272
    my $user        = $self->user;
1273
    my $password    = $self->password;
1274
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
1275
    
1276
    # Connect
1277
    my $dbh = eval {DBI->connect(
1278
        $data_source,
1279
        $user,
1280
        $password,
1281
        {
1282
            %{$self->default_dbi_option},
1283
            %$dbi_option
1284
        }
1285
    )};
1286
    
1287
    # Connect error
1288
    croak $@ if $@;
1289
    
1290
    return $dbh;
1291
}
1292

            
cleanup
yuki-kimoto authored on 2010-10-17
1293
sub _croak {
1294
    my ($self, $error, $append) = @_;
1295
    $append ||= "";
1296
    
1297
    # Verbose
1298
    if ($Carp::Verbose) { croak $error }
1299
    
1300
    # Not verbose
1301
    else {
1302
        
1303
        # Remove line and module infromation
1304
        my $at_pos = rindex($error, ' at ');
1305
        $error = substr($error, 0, $at_pos);
1306
        $error =~ s/\s+$//;
1307
        
1308
        croak "$error$append";
1309
    }
1310
}
1311

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1312
sub _need_tables {
1313
    my ($self, $tree, $need_tables, $tables) = @_;
1314
    
1315
    foreach my $table (@$tables) {
1316
        
1317
        if ($tree->{$table}) {
1318
            $need_tables->{$table} = 1;
1319
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1320
        }
1321
    }
1322
}
1323

            
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1324
sub _tables {
1325
    my ($self, $source) = @_;
1326
    
1327
    my $tables = [];
1328
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1329
    my $safety_character = $self->safety_character;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1330
    my $q = $self->reserved_word_quote;
1331
    my $q_re = quotemeta($q);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1332
    
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1333
    my $table_re = $q ? qr/\b$q_re?([$safety_character]+)$q_re?\./
1334
                      : qr/\b([$safety_character]+)\./;
1335
    while ($source =~ /$table_re/g) {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1336
        push @$tables, $1;
1337
    }
1338
    
1339
    return $tables;
1340
}
1341

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1342
sub _push_join {
1343
    my ($self, $sql, $join, $join_tables) = @_;
1344
    
1345
    return unless @$join;
1346
    
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1347
    my $q = $self->reserved_word_quote;
1348
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1349
    my $tree = {};
1350
    
1351
    for (my $i = 0; $i < @$join; $i++) {
1352
        
1353
        my $join_clause = $join->[$i];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1354
        my $q_re = quotemeta($q);
1355
        my $join_re = $q ? qr/\s$q_re?([^\.\s$q_re]+?)$q_re?\..+\s$q_re?([^\.\s$q_re]+?)$q_re?\..+?$/
1356
                         : qr/\s([^\.\s]+?)\..+\s([^\.\s]+?)\..+?$/;
1357
        if ($join_clause =~ $join_re) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1358
            
1359
            my $table1 = $1;
1360
            my $table2 = $2;
1361
            
1362
            croak qq{right side table of "$join_clause" must be uniq}
1363
              if exists $tree->{$table2};
1364
            
1365
            $tree->{$table2}
1366
              = {position => $i, parent => $table1, join => $join_clause};
1367
        }
1368
        else {
1369
            croak qq{join "$join_clause" must be two table name};
1370
        }
1371
    }
1372
    
1373
    my $need_tables = {};
1374
    $self->_need_tables($tree, $need_tables, $join_tables);
1375
    
1376
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-03-08
1377

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1378
    foreach my $need_table (@need_tables) {
1379
        push @$sql, $tree->{$need_table}{join};
1380
    }
1381
}
cleanup
Yuki Kimoto authored on 2011-03-08
1382

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1383
sub _where {
1384
    my ($self, $where) = @_;
1385
    
1386
    my $w;
1387
    if (ref $where eq 'HASH') {
1388
        my $clause = ['and'];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1389
        my $q = $self->reserved_word_quote;
1390
        foreach my $column (keys %$where) {
1391
            $column = "$q$column$q";
1392
            $column =~ s/\./$q.$q/;
1393
            push @$clause, "{= $column}" for keys %$where;
1394
        }
1395
        
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1396
        $w = $self->where(clause => $clause, param => $where);
1397
    }
1398
    elsif (ref $where eq 'DBIx::Custom::Where') {
1399
        $w = $where;
1400
    }
1401
    elsif (ref $where eq 'ARRAY') {
1402
        $w = $self->where(
1403
            clause => $where->[0],
1404
            param  => $where->[1]
1405
        );
1406
    }
1407
    
1408
    croak qq{"where" must be hash reference or DBIx::Custom::Where object} .
1409
          qq{or array reference, which contains where clause and paramter}
1410
      unless ref $w eq 'DBIx::Custom::Where';
1411
    
1412
    return $w;
1413
}
1414

            
cleanup
Yuki Kimoto authored on 2011-01-25
1415
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1416
__PACKAGE__->attr(
1417
    dbi_options => sub { {} },
1418
    filter_check  => 1
1419
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1420

            
cleanup
Yuki Kimoto authored on 2011-01-25
1421
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1422
sub default_bind_filter {
1423
    my $self = shift;
1424
    
1425
    if (@_) {
1426
        my $fname = $_[0];
1427
        
1428
        if (@_ && !$fname) {
1429
            $self->{default_out_filter} = undef;
1430
        }
1431
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1432
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1433
              unless exists $self->filters->{$fname};
1434
        
1435
            $self->{default_out_filter} = $self->filters->{$fname};
1436
        }
1437
        return $self;
1438
    }
1439
    
1440
    return $self->{default_out_filter};
1441
}
1442

            
cleanup
Yuki Kimoto authored on 2011-01-25
1443
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1444
sub default_fetch_filter {
1445
    my $self = shift;
1446
    
1447
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1448
        my $fname = $_[0];
1449

            
cleanup
Yuki Kimoto authored on 2011-01-12
1450
        if (@_ && !$fname) {
1451
            $self->{default_in_filter} = undef;
1452
        }
1453
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1454
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1455
              unless exists $self->filters->{$fname};
1456
        
1457
            $self->{default_in_filter} = $self->filters->{$fname};
1458
        }
1459
        
1460
        return $self;
1461
    }
1462
    
many changed
Yuki Kimoto authored on 2011-01-23
1463
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1464
}
1465

            
cleanup
Yuki Kimoto authored on 2011-01-25
1466
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1467
sub register_tag_processor {
1468
    return shift->query_builder->register_tag_processor(@_);
1469
}
1470

            
cleanup
Yuki Kimoto authored on 2011-03-08
1471
# DEPRECATED!
1472
sub _push_relation {
1473
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1474
    
1475
    if (keys %{$relation || {}}) {
1476
        push @$sql, $need_where ? 'where' : 'and';
1477
        foreach my $rcolumn (keys %$relation) {
1478
            my $table1 = (split (/\./, $rcolumn))[0];
1479
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1480
            push @$tables, ($table1, $table2);
1481
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1482
        }
1483
    }
1484
    pop @$sql if $sql->[-1] eq 'and';    
1485
}
1486

            
1487
# DEPRECATED!
1488
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1489
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1490
    
1491
    if (keys %{$relation || {}}) {
1492
        foreach my $rcolumn (keys %$relation) {
1493
            my $table1 = (split (/\./, $rcolumn))[0];
1494
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1495
            my $table1_exists;
1496
            my $table2_exists;
1497
            foreach my $table (@$tables) {
1498
                $table1_exists = 1 if $table eq $table1;
1499
                $table2_exists = 1 if $table eq $table2;
1500
            }
1501
            unshift @$tables, $table1 unless $table1_exists;
1502
            unshift @$tables, $table2 unless $table2_exists;
1503
        }
1504
    }
1505
}
1506

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1509
=head1 NAME
1510

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

            
1513
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1514

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1515
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1516
    
1517
    # Connect
1518
    my $dbi = DBIx::Custom->connect(
1519
        data_source => "dbi:mysql:database=dbname",
1520
        user => 'ken',
1521
        password => '!LFKD%$&',
1522
        dbi_option => {mysql_enable_utf8 => 1}
1523
    );
cleanup
yuki-kimoto authored on 2010-08-05
1524

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1525
    # Insert 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1526
    $dbi->insert(
1527
        table  => 'book',
1528
        param  => {title => 'Perl', author => 'Ken'}
1529
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1530
    
1531
    # Update 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1532
    $dbi->update(
1533
        table  => 'book', 
1534
        param  => {title => 'Perl', author => 'Ken'}, 
1535
        where  => {id => 5},
1536
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1537
    
1538
    # Delete
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1539
    $dbi->delete(
1540
        table  => 'book',
1541
        where  => {author => 'Ken'},
1542
    );
cleanup
yuki-kimoto authored on 2010-08-05
1543

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1550
    # Select, more complex
1551
    my $result = $dbi->select(
1552
        table  => 'book',
1553
        column => [
1554
            'book.author as book__author',
1555
            'company.name as company__name'
1556
        ],
1557
        where  => {'book.author' => 'Ken'},
1558
        join => ['left outer join company on book.company_id = company.id'],
1559
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1560
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1561
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1562
    # Fetch
1563
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1564
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1565
    }
1566
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1567
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1568
    while (my $row = $result->fetch_hash) {
1569
        
1570
    }
1571
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1572
    # Execute SQL with parameter.
1573
    $dbi->execute(
1574
        "select id from book where {= author} and {like title}",
1575
        param  => {author => 'ken', title => '%Perl%'}
1576
    );
1577
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1578
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1579

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

            
1582
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1583

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1588
There are many basic methods to execute various queries.
1589
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1590
C<delete_all()>, C<select()>,
1591
C<insert_at()>, C<update_at()>, 
1592
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1593

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1594
=item *
1595

            
1596
Filter when data is send or receive.
1597

            
1598
=item *
1599

            
1600
Data filtering system
1601

            
1602
=item *
1603

            
1604
Model support.
1605

            
1606
=item *
1607

            
1608
Generate where clause dinamically.
1609

            
1610
=item *
1611

            
1612
Generate join clause dinamically.
1613

            
1614
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1615

            
1616
=head1 GUIDE
1617

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

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

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

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

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

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

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

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

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

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

            
1641
=head2 C<default_dbi_option>
1642

            
1643
    my $default_dbi_option = $dbi->default_dbi_option;
1644
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1645

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1649
    {
1650
        RaiseError => 1,
1651
        PrintError => 0,
1652
        AutoCommit => 1,
1653
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1654

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

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

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

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

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

            
1667
    my $models = $dbi->models;
1668
    $dbi       = $dbi->models(\%models);
1669

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1672
=head2 C<password>
1673

            
1674
    my $password = $dbi->password;
1675
    $dbi         = $dbi->password('lkj&le`@s');
1676

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

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

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

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

            
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1686
=head2 C<reserved_word_quote> EXPERIMENTAL
1687

            
1688
     my reserved_word_quote = $dbi->reserved_word_quote;
1689
     $dbi                   = $dbi->reserved_word_quote('"');
1690

            
1691
Quote for reserved word, default to empty string.
1692

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1710
    my $user = $dbi->user;
1711
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1712

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

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

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

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

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1723
    $dbi->apply_filter(
cleanup
Yuki Kimoto authored on 2011-03-10
1724
        'book',
update pod
Yuki Kimoto authored on 2011-03-13
1725
        'issue_date' => {
1726
            out => 'tp_to_date',
1727
            in  => 'date_to_tp',
1728
            end => 'tp_to_displaydate'
1729
        },
1730
        'write_date' => {
1731
            out => 'tp_to_date',
1732
            in  => 'date_to_tp',
1733
            end => 'tp_to_displaydate'
1734
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1735
    );
1736

            
update pod
Yuki Kimoto authored on 2011-03-13
1737
Apply filter to columns.
1738
C<out> filter is executed before data is send to database.
1739
C<in> filter is executed after a row is fetch.
1740
C<end> filter is execute after C<in> filter is executed.
1741

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1744
       PETTERN         EXAMPLE
1745
    1. Column        : author
1746
    2. Table.Column  : book.author
1747
    3. Table__Column : book__author
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1748

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

            
1752
You can set multiple filters at once.
1753

            
1754
    $dbi->apply_filter(
1755
        'book',
1756
        [qw/issue_date write_date/] => {
1757
            out => 'tp_to_date',
1758
            in  => 'date_to_tp',
1759
            end => 'tp_to_displaydate'
1760
        }
1761
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1762

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1765
    my $dbi = DBIx::Custom->connect(
1766
        data_source => "dbi:mysql:database=dbname",
1767
        user => 'ken',
1768
        password => '!LFKD%$&',
1769
        dbi_option => {mysql_enable_utf8 => 1}
1770
    );
1771

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

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

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
1780
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1781
        table => 'book',
1782
        primary_key => 'id',
1783
        join => [
1784
            'inner join company on book.comparny_id = company.id'
1785
        ],
1786
        filter => [
1787
            publish_date => {
1788
                out => 'tp_to_date',
1789
                in => 'date_to_tp',
1790
                end => 'tp_to_displaydate'
1791
            }
1792
        ]
1793
    );
1794

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

            
1798
   $dbi->model('book')->select(...);
1799

            
cleanup
yuki-kimoto authored on 2010-10-17
1800
=head2 C<create_query>
1801
    
1802
    my $query = $dbi->create_query(
update pod
Yuki Kimoto authored on 2011-03-13
1803
        "insert into book {insert_param title author};";
cleanup
yuki-kimoto authored on 2010-10-17
1804
    );
update document
yuki-kimoto authored on 2009-11-19
1805

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

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

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

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

            
1816
    my $dbh = $dbi->dbh;
1817
    $dbi    = $dbi->dbh($dbh);
1818

            
1819
Get and set database handle of L<DBI>.
1820

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

            
1823
=head2 C<each_column>
1824

            
1825
    $dbi->each_column(
1826
        sub {
1827
            my ($dbi, $table, $column, $column_info) = @_;
1828
            
1829
            my $type = $column_info->{TYPE_NAME};
1830
            
1831
            if ($type eq 'DATE') {
1832
                # ...
1833
            }
1834
        }
1835
    );
1836

            
1837
Iterate all column informations of all table from database.
1838
Argument is callback when one column is found.
1839
Callback receive four arguments, dbi object, table name,
1840
column name and column information.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1841

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1844
    my $result = $dbi->execute(
1845
        "select * from book where {= title} and {like author}",
1846
        param => {title => 'Perl', author => '%Ken%'}
1847
    );
1848

            
1849
Execute SQL, containing tags.
1850
Return value is L<DBIx::Custom::Result> in select statement, or
1851
the count of affected rows in insert, update, delete statement.
1852

            
1853
Tag is turned into the statement containing place holder
1854
before SQL is executed.
1855

            
1856
    select * from where title = ? and author like ?;
1857

            
1858
See also L<Tags/Tags>.
1859

            
1860
The following opitons are currently available.
1861

            
1862
=over 4
1863

            
1864
=item C<filter>
1865

            
1866
Filter, executed before data is send to database. This is array reference.
1867
Filter value is code reference or
1868
filter name registerd by C<register_filter()>.
1869

            
1870
    # Basic
1871
    $dbi->execute(
1872
        $sql,
1873
        filter => [
1874
            title  => sub { uc $_[0] }
1875
            author => sub { uc $_[0] }
1876
        ]
1877
    );
1878
    
1879
    # At once
1880
    $dbi->execute(
1881
        $sql,
1882
        filter => [
1883
            [qw/title author/]  => sub { uc $_[0] }
1884
        ]
1885
    );
1886
    
1887
    # Filter name
1888
    $dbi->execute(
1889
        $sql,
1890
        filter => [
1891
            title  => 'upper_case',
1892
            author => 'upper_case'
1893
        ]
1894
    );
1895

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

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

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

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

            
1904
Delete statement.
1905

            
1906
The following opitons are currently available.
1907

            
update pod
Yuki Kimoto authored on 2011-03-13
1908
=over 4
1909

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

            
1912
Table name.
1913

            
1914
    $dbi->delete(table => 'book');
1915

            
1916
=item C<where>
1917

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1918
Where clause. This is hash reference or L<DBIx::Custom::Where> object
1919
or array refrence, which contains where clause and paramter.
update pod
Yuki Kimoto authored on 2011-03-13
1920
    
1921
    # Hash reference
1922
    $dbi->delete(where => {title => 'Perl'});
1923
    
1924
    # DBIx::Custom::Where object
1925
    my $where = $dbi->where(
1926
        clause => ['and', '{= author}', '{like title}'],
1927
        param  => {author => 'Ken', title => '%Perl%'}
1928
    );
1929
    $dbi->delete(where => $where);
1930

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1931
    # Array refrendce (where clause and parameter)
1932
    $dbi->delete(where =>
1933
        [
1934
            ['and', '{= author}', '{like title}'],
1935
            {author => 'Ken', title => '%Perl%'}
1936
        ]
1937
    );
1938
    
update pod
Yuki Kimoto authored on 2011-03-13
1939
=item C<append>
1940

            
1941
Append statement to last of SQL. This is string.
1942

            
1943
    $dbi->delete(append => 'order by title');
1944

            
1945
=item C<filter>
1946

            
1947
Filter, executed before data is send to database. This is array reference.
1948
Filter value is code reference or
1949
filter name registerd by C<register_filter()>.
1950

            
1951
    # Basic
1952
    $dbi->delete(
1953
        filter => [
1954
            title  => sub { uc $_[0] }
1955
            author => sub { uc $_[0] }
1956
        ]
1957
    );
1958
    
1959
    # At once
1960
    $dbi->delete(
1961
        filter => [
1962
            [qw/title author/]  => sub { uc $_[0] }
1963
        ]
1964
    );
1965
    
1966
    # Filter name
1967
    $dbi->delete(
1968
        filter => [
1969
            title  => 'upper_case',
1970
            author => 'upper_case'
1971
        ]
1972
    );
1973

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

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

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

            
1980
Create column clause. The follwoing column clause is created.
1981

            
1982
    book.author as book__author,
1983
    book.title as book__title
1984

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

            
1987
Get L<DBIx::Custom::Query> object instead of executing SQL.
1988
This is true or false value.
1989

            
1990
    my $query = $dbi->delete(query => 1);
1991

            
1992
You can check SQL.
1993

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1996
=back
1997

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

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

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

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

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

            
2009
    $dbi->delete_at(
2010
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2011
        primary_key => 'id',
2012
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2013
    );
2014

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2019
=over 4
2020

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2023
Primary key. This is constant value or array reference.
2024
    
2025
    # Constant value
2026
    $dbi->delete(primary_key => 'id');
2027

            
2028
    # Array reference
2029
    $dbi->delete(primary_key => ['id1', 'id2' ]);
2030

            
2031
This is used to create where clause.
2032

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

            
2035
Where clause, created from primary key information.
2036
This is constant value or array reference.
2037

            
2038
    # Constant value
2039
    $dbi->delete(where => 5);
2040

            
2041
    # Array reference
2042
    $dbi->delete(where => [3, 5]);
2043

            
2044
In first examle, the following SQL is created.
2045

            
2046
    delete from book where id = ?;
2047

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2050
=back
2051

            
cleanup
yuki-kimoto authored on 2010-10-17
2052
=head2 C<insert>
2053

            
update pod
Yuki Kimoto authored on 2011-03-13
2054
    $dbi->insert(
2055
        table  => 'book', 
2056
        param  => {title => 'Perl', author => 'Ken'}
2057
    );
2058

            
2059
Insert statement.
2060

            
2061
The following opitons are currently available.
2062

            
update pod
Yuki Kimoto authored on 2011-03-13
2063
=over 4
2064

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

            
2067
Table name.
2068

            
2069
    $dbi->insert(table => 'book');
2070

            
2071
=item C<param>
2072

            
2073
Insert data. This is hash reference.
2074

            
2075
    $dbi->insert(param => {title => 'Perl'});
2076

            
2077
=item C<append>
2078

            
2079
Append statement to last of SQL. This is string.
2080

            
2081
    $dbi->insert(append => 'order by title');
2082

            
2083
=item C<filter>
2084

            
2085
Filter, executed before data is send to database. This is array reference.
2086
Filter value is code reference or
2087
filter name registerd by C<register_filter()>.
2088

            
2089
    # Basic
2090
    $dbi->insert(
2091
        filter => [
2092
            title  => sub { uc $_[0] }
2093
            author => sub { uc $_[0] }
2094
        ]
2095
    );
2096
    
2097
    # At once
2098
    $dbi->insert(
2099
        filter => [
2100
            [qw/title author/]  => sub { uc $_[0] }
2101
        ]
2102
    );
2103
    
2104
    # Filter name
2105
    $dbi->insert(
2106
        filter => [
2107
            title  => 'upper_case',
2108
            author => 'upper_case'
2109
        ]
2110
    );
2111

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

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

            
2116
Get L<DBIx::Custom::Query> object instead of executing SQL.
2117
This is true or false value.
2118

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2125
=back
2126

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

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

            
2131
    $dbi->insert_at(
2132
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2133
        primary_key => 'id',
2134
        where => '5',
2135
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2136
    );
2137

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2142
=over 4
2143

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

            
2146
Primary key. This is constant value or array reference.
2147
    
2148
    # Constant value
2149
    $dbi->insert(primary_key => 'id');
2150

            
2151
    # Array reference
2152
    $dbi->insert(primary_key => ['id1', 'id2' ]);
2153

            
2154
This is used to create parts of insert data.
2155

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

            
2158
Parts of Insert data, create from primary key information.
2159
This is constant value or array reference.
2160

            
2161
    # Constant value
2162
    $dbi->insert(where => 5);
2163

            
2164
    # Array reference
2165
    $dbi->insert(where => [3, 5]);
2166

            
2167
In first examle, the following SQL is created.
2168

            
2169
    insert into book (id, title) values (?, ?);
2170

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2173
=back
2174

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

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

            
2179
Create insert parameter tag.
2180

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2181
    (title, author) values ({? title}, {? author});
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2182

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2190
    lib / MyModel.pm
2191
        / MyModel / book.pm
2192
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2193

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

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

            
2198
    package MyModel;
2199
    
2200
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-03-13
2201
    
2202
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2203

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2208
    package MyModel::book;
2209
    
2210
    use base 'MyModel';
2211
    
2212
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2213

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2216
    package MyModel::company;
2217
    
2218
    use base 'MyModel';
2219
    
2220
    1;
2221
    
2222
MyModel::book and MyModel::company is included by C<include_model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2223

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

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

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

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2231
=head2 C<merge_param> EXPERIMENTAL
2232

            
2233
    my $param = $dbi->merge_param({key1 => 1}, {key1 => 1, key2 => 2});
2234

            
2235
Merge paramters.
2236

            
2237
$param:
2238

            
2239
    {key1 => [1, 1], key2 => 2}
2240

            
update pod
Yuki Kimoto authored on 2011-03-13
2241
=head2 C<method> EXPERIMENTAL
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2242

            
2243
    $dbi->method(
2244
        update_or_insert => sub {
2245
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2246
            
2247
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2248
        },
2249
        find_or_create   => sub {
2250
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2251
            
2252
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2253
        }
2254
    );
2255

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

            
2258
    $dbi->update_or_insert;
2259
    $dbi->find_or_create;
2260

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

            
2263
    $dbi->model('book')->method(
2264
        insert => sub { ... },
2265
        update => sub { ... }
2266
    );
2267
    
2268
    my $model = $dbi->model('book');
2269

            
2270
Set and get a L<DBIx::Custom::Model> object,
2271

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

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

            
2276
Create column clause for myself. The follwoing column clause is created.
2277

            
2278
    book.author as author,
2279
    book.title as title
2280

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2283
    my $dbi = DBIx::Custom->new(
2284
        data_source => "dbi:mysql:database=dbname",
2285
        user => 'ken',
2286
        password => '!LFKD%$&',
2287
        dbi_option => {mysql_enable_utf8 => 1}
2288
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2289

            
2290
Create a new L<DBIx::Custom> object.
2291

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

            
2294
    my $not_exists = $dbi->not_exists;
2295

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2299
=head2 C<register_filter>
2300

            
update pod
Yuki Kimoto authored on 2011-03-13
2301
    $dbi->register_filter(
2302
        # Time::Piece object to database DATE format
2303
        tp_to_date => sub {
2304
            my $tp = shift;
2305
            return $tp->strftime('%Y-%m-%d');
2306
        },
2307
        # database DATE format to Time::Piece object
2308
        date_to_tp => sub {
2309
           my $date = shift;
2310
           return Time::Piece->strptime($date, '%Y-%m-%d');
2311
        }
2312
    );
cleanup
yuki-kimoto authored on 2010-10-17
2313
    
update pod
Yuki Kimoto authored on 2011-03-13
2314
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2315

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2318
    $dbi->register_tag(
2319
        update => sub {
2320
            my @columns = @_;
2321
            
2322
            # Update parameters
2323
            my $s = 'set ';
2324
            $s .= "$_ = ?, " for @columns;
2325
            $s =~ s/, $//;
2326
            
2327
            return [$s, \@columns];
2328
        }
2329
    );
cleanup
yuki-kimoto authored on 2010-10-17
2330

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

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

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

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

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

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

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

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

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

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2354
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2355
        table  => 'book',
2356
        column => ['author', 'title'],
2357
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2358
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2359
    
update pod
Yuki Kimoto authored on 2011-03-12
2360
Select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2361

            
2362
The following opitons are currently available.
2363

            
2364
=over 4
2365

            
2366
=item C<table>
2367

            
2368
Table name.
2369

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

            
2372
=item C<column>
2373

            
2374
Column clause. This is array reference or constant value.
2375

            
2376
    # Hash refernce
2377
    $dbi->select(column => ['author', 'title']);
2378
    
2379
    # Constant value
2380
    $dbi->select(column => 'author');
2381

            
2382
Default is '*' unless C<column> is specified.
2383

            
2384
    # Default
2385
    $dbi->select(column => '*');
2386

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

            
2389
=over 4
2390

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

            
update pod
Yuki Kimoto authored on 2011-03-12
2393
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
2394

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

            
2397
If main table is C<book> and joined table is C<company>,
2398
This create the following column clause.
2399

            
2400
    book.author as author
2401
    book.company_id as company_id
2402
    company.id as company__id
2403
    company.name as company__name
2404

            
2405
Columns of main table is consist of only column name,
2406
Columns of joined table is consist of table and column name joined C<__>.
2407

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

            
2411
    # Generally do the following way before using all_column option
2412
    $dbi->include_model('MyModel')->setup_model;
2413

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

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

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

            
2420
=item prepend EXPERIMENTAL
2421

            
2422
You can add before created statement
2423

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

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

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

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2430
Where clause. This is hash reference or L<DBIx::Custom::Where> object,
2431
or array refrence, which contains where clause and paramter.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2432
    
2433
    # Hash reference
update pod
Yuki Kimoto authored on 2011-03-12
2434
    $dbi->select(where => {author => 'Ken', 'title' => 'Perl'});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2435
    
update pod
Yuki Kimoto authored on 2011-03-12
2436
    # DBIx::Custom::Where object
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2437
    my $where = $dbi->where(
2438
        clause => ['and', '{= author}', '{like title}'],
2439
        param  => {author => 'Ken', title => '%Perl%'}
2440
    );
update pod
Yuki Kimoto authored on 2011-03-12
2441
    $dbi->select(where => $where);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2442

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2443
    # Array refrendce (where clause and parameter)
2444
    $dbi->select(where =>
2445
        [
2446
            ['and', '{= author}', '{like title}'],
2447
            {author => 'Ken', title => '%Perl%'}
2448
        ]
2449
    );
2450
    
update pod
Yuki Kimoto authored on 2011-03-13
2451
=item C<join> EXPERIMENTAL
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2452

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

            
2455
    $dbi->select(join =>
2456
        [
2457
            'left outer join company on book.company_id = company_id',
2458
            'left outer join location on company.location_id = location.id'
2459
        ]
2460
    );
2461

            
2462
If column cluase or where clause contain table name like "company.name",
2463
needed join clause is used automatically.
2464

            
2465
    $dbi->select(
2466
        table => 'book',
2467
        column => ['company.location_id as company__location_id'],
2468
        where => {'company.name' => 'Orange'},
2469
        join => [
2470
            'left outer join company on book.company_id = company.id',
2471
            'left outer join location on company.location_id = location.id'
2472
        ]
2473
    );
2474

            
2475
In above select, the following SQL is created.
2476

            
2477
    select company.location_id as company__location_id
2478
    from book
2479
      left outer join company on book.company_id = company.id
2480
    where company.name = Orange
2481

            
2482
=item C<append>
2483

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

            
2486
    $dbi->select(append => 'order by title');
2487

            
2488
=item C<filter>
2489

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

            
2494
    # Basic
2495
    $dbi->select(
2496
        filter => [
2497
            title  => sub { uc $_[0] }
2498
            author => sub { uc $_[0] }
2499
        ]
2500
    );
2501
    
2502
    # At once
2503
    $dbi->select(
2504
        filter => [
2505
            [qw/title author/]  => sub { uc $_[0] }
2506
        ]
2507
    );
2508
    
2509
    # Filter name
2510
    $dbi->select(
2511
        filter => [
2512
            title  => 'upper_case',
2513
            author => 'upper_case'
2514
        ]
2515
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
2516

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

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

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

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

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

            
2528
    my $sql = $query->sql;
2529

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

            
2532
Specify database data type.
2533

            
2534
    $dbi->select(type => [image => DBI::SQL_BLOB]);
2535
    $dbi->select(type => [[qw/image audio/] => DBI::SQL_BLOB]);
2536

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

            
2539
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2540

            
update pod
Yuki Kimoto authored on 2011-03-12
2541
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2542

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

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

            
2547
    $dbi->select_at(
2548
        table => 'book',
2549
        primary_key => 'id',
2550
        where => '5'
2551
    );
2552

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2557
=over 4
2558

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

            
update pod
Yuki Kimoto authored on 2011-03-12
2561
Primary key. This is constant value or array reference.
2562
    
2563
    # Constant value
2564
    $dbi->select(primary_key => 'id');
2565

            
2566
    # Array reference
2567
    $dbi->select(primary_key => ['id1', 'id2' ]);
2568

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

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

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

            
2576
    # Constant value
2577
    $dbi->select(where => 5);
2578

            
2579
    # Array reference
2580
    $dbi->select(where => [3, 5]);
2581

            
2582
In first examle, the following SQL is created.
2583

            
2584
    select * from book where id = ?
2585

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2588
=back
2589

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2592
    $dbi->update(
2593
        table  => 'book',
2594
        param  => {title => 'Perl'},
2595
        where  => {id => 4}
2596
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2597

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2602
=over 4
2603

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2606
Table name.
2607

            
2608
    $dbi->update(table => 'book');
2609

            
2610
=item C<param>
2611

            
2612
Update data. This is hash reference.
2613

            
2614
    $dbi->update(param => {title => 'Perl'});
2615

            
2616
=item C<where>
2617

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2618
Where clause. This is hash reference or L<DBIx::Custom::Where> object
2619
or array refrence.
update pod
Yuki Kimoto authored on 2011-03-13
2620
    
2621
    # Hash reference
2622
    $dbi->update(where => {author => 'Ken', 'title' => 'Perl'});
2623
    
2624
    # DBIx::Custom::Where object
2625
    my $where = $dbi->where(
2626
        clause => ['and', '{= author}', '{like title}'],
2627
        param  => {author => 'Ken', title => '%Perl%'}
2628
    );
2629
    $dbi->update(where => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2630
    
2631
    # Array refrendce (where clause and parameter)
2632
    $dbi->update(where =>
2633
        [
2634
            ['and', '{= author}', '{like title}'],
2635
            {author => 'Ken', title => '%Perl%'}
2636
        ]
2637
    );
update pod
Yuki Kimoto authored on 2011-03-13
2638

            
2639
=item C<append>
2640

            
2641
Append statement to last of SQL. This is string.
2642

            
2643
    $dbi->update(append => 'order by title');
2644

            
2645
=item C<filter>
2646

            
2647
Filter, executed before data is send to database. This is array reference.
2648
Filter value is code reference or
2649
filter name registerd by C<register_filter()>.
2650

            
2651
    # Basic
2652
    $dbi->update(
2653
        filter => [
2654
            title  => sub { uc $_[0] }
2655
            author => sub { uc $_[0] }
2656
        ]
2657
    );
2658
    
2659
    # At once
2660
    $dbi->update(
2661
        filter => [
2662
            [qw/title author/]  => sub { uc $_[0] }
2663
        ]
2664
    );
2665
    
2666
    # Filter name
2667
    $dbi->update(
2668
        filter => [
2669
            title  => 'upper_case',
2670
            author => 'upper_case'
2671
        ]
2672
    );
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2673

            
update pod
Yuki Kimoto authored on 2011-03-13
2674
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
2675

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

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

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

            
2683
You can check SQL.
2684

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2687
=back
2688

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

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

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

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

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

            
2700
    $dbi->update_at(
2701
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2702
        primary_key => 'id',
2703
        where => '5',
2704
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2705
    );
2706

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2711
=over 4
2712

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

            
2715
Primary key. This is constant value or array reference.
2716
    
2717
    # Constant value
2718
    $dbi->update(primary_key => 'id');
2719

            
2720
    # Array reference
2721
    $dbi->update(primary_key => ['id1', 'id2' ]);
2722

            
2723
This is used to create where clause.
2724

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

            
2727
Where clause, created from primary key information.
2728
This is constant value or array reference.
2729

            
2730
    # Constant value
2731
    $dbi->update(where => 5);
2732

            
2733
    # Array reference
2734
    $dbi->update(where => [3, 5]);
2735

            
2736
In first examle, the following SQL is created.
2737

            
2738
    update book set title = ? where id = ?
2739

            
2740
Place holders are set to 'Perl' and 5.
2741

            
update pod
Yuki Kimoto authored on 2011-03-13
2742
=back
2743

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

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

            
2748
Create update parameter tag.
2749

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2750
    set title = {? title}, author = {? author}
2751

            
2752
You can create tag without 'set ' by C<no_set> option. This option is EXPERIMENTAL.
2753

            
2754
    my $update_param = $dbi->update_param(
2755
        {title => 'a', age => 2}
2756
        {no_set => 1}
2757
    );
2758

            
2759
    title = {? title}, author = {? author}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2760

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

            
cleanup
Yuki Kimoto authored on 2011-03-09
2763
    my $where = $dbi->where(
2764
        clause => ['and', '{= title}', '{= author}'],
2765
        param => {title => 'Perl', author => 'Ken'}
2766
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2767

            
2768
Create a new L<DBIx::Custom::Where> object.
2769

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2777
=head1 Tags
2778

            
2779
The following tags is available.
2780

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

            
2783
Table tag
2784

            
2785
    {table TABLE}    ->    TABLE
2786

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2789
=head2 C<?>
2790

            
2791
Placeholder tag.
2792

            
2793
    {? NAME}    ->   ?
2794

            
2795
=head2 C<=>
2796

            
2797
Equal tag.
2798

            
2799
    {= NAME}    ->   NAME = ?
2800

            
2801
=head2 C<E<lt>E<gt>>
2802

            
2803
Not equal tag.
2804

            
2805
    {<> NAME}   ->   NAME <> ?
2806

            
2807
=head2 C<E<lt>>
2808

            
2809
Lower than tag
2810

            
2811
    {< NAME}    ->   NAME < ?
2812

            
2813
=head2 C<E<gt>>
2814

            
2815
Greater than tag
2816

            
2817
    {> NAME}    ->   NAME > ?
2818

            
2819
=head2 C<E<gt>=>
2820

            
2821
Greater than or equal tag
2822

            
2823
    {>= NAME}   ->   NAME >= ?
2824

            
2825
=head2 C<E<lt>=>
2826

            
2827
Lower than or equal tag
2828

            
2829
    {<= NAME}   ->   NAME <= ?
2830

            
2831
=head2 C<like>
2832

            
2833
Like tag
2834

            
2835
    {like NAME}   ->   NAME like ?
2836

            
2837
=head2 C<in>
2838

            
2839
In tag.
2840

            
2841
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2842

            
2843
=head2 C<insert_param>
2844

            
2845
Insert parameter tag.
2846

            
2847
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2848

            
2849
=head2 C<update_param>
2850

            
2851
Updata parameter tag.
2852

            
2853
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2854

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

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

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

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

            
2864
C<< <kimoto.yuki at gmail.com> >>
2865

            
2866
L<http://github.com/yuki-kimoto/DBIx-Custom>
2867

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2868
=head1 AUTHOR
2869

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

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

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

            
2876
This program is free software; you can redistribute it and/or modify it
2877
under the same terms as Perl itself.
2878

            
2879
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2880

            
2881