DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
3553 lines | 92.623kb
cleanup
yuki-kimoto authored on 2009-12-22
1
package DBIx::Custom;
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
2
use Object::Simple -base;
cleanup
yuki-kimoto authored on 2009-12-22
3

            
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
4
our $VERSION = '0.1738';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6

            
packaging one directory
yuki-kimoto authored on 2009-11-16
7
use Carp 'croak';
8
use DBI;
9
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
10
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
11
use DBIx::Custom::QueryBuilder;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
12
use DBIx::Custom::Where;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
13
use DBIx::Custom::Model;
cleanup
Yuki Kimoto authored on 2011-01-25
14
use DBIx::Custom::Tag;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
15
use DBIx::Custom::Order;
cleanup
Yuki Kimoto authored on 2011-04-25
16
use DBIx::Custom::Util qw/_array_to_hash _subname/;
added tests
Yuki Kimoto authored on 2011-08-26
17
use DBIx::Custom::Mapper;
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
18
use DBIx::Custom::NotExists;
improved debug message
Yuki Kimoto authored on 2011-05-23
19
use Encode qw/encode encode_utf8 decode_utf8/;
cleanup
Yuki Kimoto authored on 2011-08-13
20
use Scalar::Util qw/weaken/;
packaging one directory
yuki-kimoto authored on 2009-11-16
21

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
22
has [qw/connector dsn password quote user exclude_table user_table_info
23
        user_column_info/],
removed from cache() and cac...
Yuki Kimoto authored on 2011-03-29
24
    cache => 0,
many changed
Yuki Kimoto authored on 2011-01-23
25
    cache_method => sub {
26
        sub {
27
            my $self = shift;
28
            
29
            $self->{_cached} ||= {};
30
            
31
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-03-13
32
                $self->{_cached}{$_[0]} = $_[1];
many changed
Yuki Kimoto authored on 2011-01-23
33
            }
34
            else {
update pod
Yuki Kimoto authored on 2011-03-13
35
                return $self->{_cached}{$_[0]};
many changed
Yuki Kimoto authored on 2011-01-23
36
            }
37
        }
update pod
Yuki Kimoto authored on 2011-03-13
38
    },
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
39
    option => sub { {} },
40
    default_option => sub {
update pod
Yuki Kimoto authored on 2011-03-13
41
        {
42
            RaiseError => 1,
43
            PrintError => 0,
44
            AutoCommit => 1
45
        }
46
    },
fix tests
Yuki Kimoto authored on 2011-01-13
47
    filters => sub {
48
        {
49
            encode_utf8 => sub { encode_utf8($_[0]) },
50
            decode_utf8 => sub { decode_utf8($_[0]) }
51
        }
update pod
Yuki Kimoto authored on 2011-03-13
52
    },
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
53
    last_sql => '',
update pod
Yuki Kimoto authored on 2011-03-13
54
    models => sub { {} },
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
55
    now => sub {
56
        sub {
57
            my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
58
            $mon++;
59
            $year += 1900;
60
            return sprintf("%04d-%02d-%02d %02d:%02d:%02d");
61
        }
62
    },
cleanup
Yuki Kimoto authored on 2011-08-13
63
    query_builder => sub {
64
        my $self = shift;
65
        my $builder = DBIx::Custom::QueryBuilder->new(dbi => $self);
66
        weaken $builder->{dbi};
67
        return $builder;
68
    },
update pod
Yuki Kimoto authored on 2011-03-13
69
    result_class  => 'DBIx::Custom::Result',
70
    safety_character => '\w',
cleanup test
Yuki Kimoto authored on 2011-08-10
71
    separator => '.',
added tag_parse attribute
Yuki Kimoto authored on 2011-06-28
72
    stash => sub { {} },
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
73
    tag_parse => 1;
cleanup
yuki-kimoto authored on 2010-10-17
74

            
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
75
sub available_datatype {
test cleanup
Yuki Kimoto authored on 2011-08-10
76
    my $self = shift;
77
    
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
78
    my $data_types = '';
cleanup
Yuki Kimoto authored on 2011-10-21
79
    for my $i (-1000 .. 1000) {
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
80
         my $type_info = $self->dbh->type_info($i);
81
         my $data_type = $type_info->{DATA_TYPE};
82
         my $type_name = $type_info->{TYPE_NAME};
83
         $data_types .= "$data_type ($type_name)\n"
84
           if defined $data_type;
85
    }
86
    return "Data Type maybe equal to Type Name" unless $data_types;
87
    $data_types = "Data Type (Type name)\n" . $data_types;
88
    return $data_types;
89
}
90

            
91
sub available_typename {
92
    my $self = shift;
93
    
94
    # Type Names
95
    my $type_names = {};
96
    $self->each_column(sub {
97
        my ($self, $table, $column, $column_info) = @_;
98
        $type_names->{$column_info->{TYPE_NAME}} = 1
99
          if $column_info->{TYPE_NAME};
100
    });
101
    my @output = sort keys %$type_names;
102
    unshift @output, "Type Name";
103
    return join "\n", @output;
test cleanup
Yuki Kimoto authored on 2011-08-10
104
}
105

            
added helper method
yuki-kimoto authored on 2010-10-17
106
our $AUTOLOAD;
107
sub AUTOLOAD {
108
    my $self = shift;
109

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
113
    # Call method
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
114
    $self->{_methods} ||= {};
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
115
    if (my $method = $self->{_methods}->{$mname}) {
116
        return $self->$method(@_)
117
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
118
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
119
        $self->dbh->$dbh_method(@_);
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
120
    }
121
    else {
cleanup
Yuki Kimoto authored on 2011-04-25
122
        croak qq{Can't locate object method "$mname" via "$package" }
123
            . _subname;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
124
    }
added helper method
yuki-kimoto authored on 2010-10-17
125
}
126

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
127
sub assign_clause {
updated pod
Yuki Kimoto authored on 2011-09-02
128
    my ($self, $param, $opts) = @_;
129
    
130
    my $wrap = $opts->{wrap} || {};
micro optimization
Yuki Kimoto authored on 2011-10-23
131
    my $safety = $self->{safety_character} || $self->safety_character;
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
132
    my $qp = $self->_q('');
133
    my $q = substr($qp, 0, 1) || '';
134
    my $p = substr($qp, 1, 1) || '';
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
135
    
micro optimization
Yuki Kimoto authored on 2011-10-23
136
    # Check unsafety keys
137
    unless ((join('', keys %$param) || '') =~ /^[$safety\.]+$/) {
138
        for my $column (keys %$param) {
139
            croak qq{"$column" is not safety column name } . _subname
140
              unless $column =~ /^[$safety\.]+$/;
141
        }
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
142
    }
143
    
micro optimization
Yuki Kimoto authored on 2011-10-23
144
    # Assign clause (performance is important)
micro optimization
Yuki Kimoto authored on 2011-10-23
145
    join(
146
      ', ',
147
      map {
148
          ref $param->{$_} eq 'SCALAR' ? "$q$_$p = " . ${$param->{$_}}
149
          : $wrap->{$_} ? "$q$_$p = " . $wrap->{$_}->(":$_")
150
          : "$q$_$p = :$_";
151
      } sort keys %$param
152
    );
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
153
}
154

            
cleanup
Yuki Kimoto authored on 2011-03-21
155
sub column {
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
156
    my $self = shift;
157
    my $option = pop if ref $_[-1] eq 'HASH';
158
    my $real_table = shift;
159
    my $columns = shift;
160
    my $table = $option->{alias} || $real_table;
161
    
162
    # Columns
163
    unless ($columns) {
164
        $columns ||= $self->model($real_table)->columns;
165
    }
added helper method
yuki-kimoto authored on 2010-10-17
166
    
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
167
    # Separator
168
    my $separator = $self->separator;
169
    
cleanup
Yuki Kimoto authored on 2011-04-02
170
    # Column clause
cleanup
Yuki Kimoto authored on 2011-03-21
171
    my @column;
cleanup
Yuki Kimoto authored on 2011-04-02
172
    $columns ||= [];
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
173
    push @column, $self->_q($table) . "." . $self->_q($_) .
174
      " as " . $self->_q("${table}${separator}$_")
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
175
      for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
176
    
177
    return join (', ', @column);
added helper method
yuki-kimoto authored on 2010-10-17
178
}
179

            
packaging one directory
yuki-kimoto authored on 2009-11-16
180
sub connect {
cleanup
Yuki Kimoto authored on 2011-08-16
181
    my $self = ref $_[0] ? shift : shift->new(@_);
182
    
183
    my $connector = $self->connector;
184
    
185
    if (!ref $connector && $connector) {
186
        require DBIx::Connector;
187
        
188
        my $dsn = $self->dsn;
189
        my $user = $self->user;
190
        my $password = $self->password;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
191
        my $option = $self->_option;
cleanup
Yuki Kimoto authored on 2011-08-16
192
        my $connector = DBIx::Connector->new($dsn, $user, $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
193
          {%{$self->default_option} , %$option});
cleanup
Yuki Kimoto authored on 2011-08-16
194
        $self->connector($connector);
195
    }
removed register_format()
yuki-kimoto authored on 2010-05-26
196
    
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
197
    # Connect
198
    $self->dbh;
update document
yuki-kimoto authored on 2010-01-30
199
    
packaging one directory
yuki-kimoto authored on 2009-11-16
200
    return $self;
201
}
202

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
203
sub count { shift->select(column => 'count(*)', @_)->fetch_first->[0] }
204

            
update pod
Yuki Kimoto authored on 2011-03-13
205
sub dbh {
206
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-04-02
207
    
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
208
    # Set
209
    if (@_) {
210
        $self->{dbh} = $_[0];
211
        
212
        return $self;
213
    }
214
    
215
    # Get
216
    else {
217
        # From Connction manager
218
        if (my $connector = $self->connector) {
cleanup
Yuki Kimoto authored on 2011-04-25
219
            croak "connector must have dbh() method " . _subname
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
220
              unless ref $connector && $connector->can('dbh');
221
              
set reserved_word_quote auto...
Yuki Kimoto authored on 2011-06-08
222
            $self->{dbh} = $connector->dbh;
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
223
        }
224
        
set reserved_word_quote auto...
Yuki Kimoto authored on 2011-06-08
225
        # Connect
226
        $self->{dbh} ||= $self->_connect;
227
        
228
        # Quote
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
229
        if (!defined $self->reserved_word_quote && !defined $self->quote) {
prepare oracle test
Yuki Kimoto authored on 2011-08-15
230
            my $driver = $self->_driver;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
231
            my $quote =  $driver eq 'odbc' ? '[]'
232
                       : $driver eq 'ado' ? '[]'
233
                       : $driver eq 'mysql' ? '`'
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
234
                       : '"';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
235
            $self->quote($quote);
set reserved_word_quote auto...
Yuki Kimoto authored on 2011-06-08
236
        }
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
237
        
set reserved_word_quote auto...
Yuki Kimoto authored on 2011-06-08
238
        return $self->{dbh};
update pod
Yuki Kimoto authored on 2011-03-13
239
    }
240
}
241

            
cleanup
Yuki Kimoto authored on 2011-10-21
242
sub delete {
243
    my ($self, %opt) = @_;
244
    warn "delete method where_param option is DEPRECATED!"
245
      if $opt{where_param};
246
    
cleanup
Yuki Kimoto authored on 2011-10-21
247
    # Don't allow delete all rows
cleanup
Yuki Kimoto authored on 2011-10-21
248
    croak qq{delete method where or id option must be specified } . _subname
249
      if !$opt{where} && !defined $opt{id} && !$opt{allow_delete_all};
250
    
251
    # Where
cleanup
Yuki Kimoto authored on 2011-10-25
252
    my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
cleanup
Yuki Kimoto authored on 2011-10-25
253
      delete $opt{id}, $opt{primary_key}, $opt{table});
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
254

            
cleanup
Yuki Kimoto authored on 2011-04-02
255
    # Delete statement
cleanup
Yuki Kimoto authored on 2011-10-21
256
    my $sql = "delete ";
cleanup
Yuki Kimoto authored on 2011-10-21
257
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-25
258
    $sql .= "from " . $self->_q($opt{table}) . " $w->{clause} ";
packaging one directory
yuki-kimoto authored on 2009-11-16
259
    
260
    # Execute query
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
261
    $opt{statement} = 'delete';
cleanup
Yuki Kimoto authored on 2011-10-25
262
    $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
263
}
264

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

            
added memory leak check test
Yuki Kimoto authored on 2011-08-15
267
sub DESTROY {}
added helper method
yuki-kimoto authored on 2010-10-17
268

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
269
sub create_model {
270
    my $self = shift;
271
    
cleanup
Yuki Kimoto authored on 2011-10-21
272
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
273
    my $opt = ref $_[0] eq 'HASH' ? $_[0] : {@_};
274
    $opt->{dbi} = $self;
275
    my $model_class = delete $opt->{model_class} || 'DBIx::Custom::Model';
276
    my $model_name  = delete $opt->{name};
277
    my $model_table = delete $opt->{table};
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
278
    $model_name ||= $model_table;
279
    
cleanup
Yuki Kimoto authored on 2011-04-02
280
    # Create model
cleanup
Yuki Kimoto authored on 2011-10-21
281
    my $model = $model_class->new($opt);
cleanup
Yuki Kimoto authored on 2011-08-13
282
    weaken $model->{dbi};
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
283
    $model->name($model_name) unless $model->name;
284
    $model->table($model_table) unless $model->table;
285
    
micro optimization
Yuki Kimoto authored on 2011-07-30
286
    # Apply filter(DEPRECATED logic)
287
    if ($model->{filter}) {
288
        my $filter = ref $model->filter eq 'HASH'
289
                   ? [%{$model->filter}]
290
                   : $model->filter;
291
        $filter ||= [];
292
        warn "DBIx::Custom::Model filter method is DEPRECATED!"
293
          if @$filter;
294
        $self->_apply_filter($model->table, @$filter);
295
    }
296
    
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
297
    # Set model
298
    $self->model($model->name, $model);
299
    
create_model() return model
Yuki Kimoto authored on 2011-03-29
300
    return $self->model($model->name);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
301
}
302

            
303
sub each_column {
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
304
    my ($self, $cb, %options) = @_;
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
305

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
306
    my $user_column_info = $self->user_column_info;
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
307
    
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
308
    if ($user_column_info) {
309
        $self->$cb($_->{table}, $_->{column}, $_->{info}) for @$user_column_info;
310
    }
311
    else {
312
    
313
        my $re = $self->exclude_table || $options{exclude_table};
314
        # Tables
315
        my %tables;
316
        $self->each_table(sub { $tables{$_[1]}++ });
added SQL Server test
Yuki Kimoto authored on 2011-08-14
317

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
318
        # Iterate all tables
319
        my @tables = sort keys %tables;
320
        for (my $i = 0; $i < @tables; $i++) {
321
            my $table = $tables[$i];
322
            
323
            # Iterate all columns
324
            my $sth_columns;
325
            eval {$sth_columns = $self->dbh->column_info(undef, undef, $table, '%')};
326
            next if $@;
327
            while (my $column_info = $sth_columns->fetchrow_hashref) {
328
                my $column = $column_info->{COLUMN_NAME};
329
                $self->$cb($table, $column, $column_info);
330
            }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
331
        }
332
    }
333
}
334

            
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
335
sub each_table {
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
336
    my ($self, $cb, %option) = @_;
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
337
    
cleanup test
Yuki Kimoto authored on 2011-08-16
338
    my $user_table_infos = $self->user_table_info;
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
339
    
added test
Yuki Kimoto authored on 2011-08-16
340
    # Iterate tables
341
    if ($user_table_infos) {
342
        $self->$cb($_->{table}, $_->{info}) for @$user_table_infos;
343
    }
344
    else {
345
        my $re = $self->exclude_table || $option{exclude};
346
        my $sth_tables = $self->dbh->table_info;
347
        while (my $table_info = $sth_tables->fetchrow_hashref) {
348
            
349
            # Table
350
            my $table = $table_info->{TABLE_NAME};
351
            next if defined $re && $table =~ /$re/;
352
            $self->$cb($table, $table_info);
353
        }
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
354
    }
355
}
356

            
cleanup
Yuki Kimoto authored on 2011-04-02
357
sub execute {
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
358
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-10-20
359
    my $sql = shift;
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
360

            
361
    # Options
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
362
    my $param;
363
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
364
    my %opt = @_;
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
365
    warn "sqlfilter option is DEPRECATED" if $opt{sqlfilter};
366
    $param ||= $opt{param} || {};
cleanup
Yuki Kimoto authored on 2011-10-21
367
    my $tables = $opt{table} || [];
cleanup
Yuki Kimoto authored on 2011-04-02
368
    $tables = [$tables] unless ref $tables eq 'ARRAY';
micro optimization
Yuki Kimoto authored on 2011-10-23
369
    my $filter = ref $opt{filter} eq 'ARRAY' ?
370
      _array_to_hash($opt{filter}) : $opt{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
371
    
micro optimization and
Yuki Kimoto authored on 2011-10-25
372
    # Merge second parameter
373
    my @cleanup;
374
    if (ref $param eq 'ARRAY') {
375
        my $param2 = $param->[1];
376
        $param = $param->[0];
377
        for my $column (keys %$param2) {
378
            if (!exists $param->{$column}) {
379
                $param->{$column} = $param2->{$column};
380
                push @cleanup, $column;
381
            }
382
            else {
383
                delete $param->{$_} for @cleanup;
384
                @cleanup = ();
385
                $param = $self->merge_param($param, $param2);
386
                last;
387
            }
388
        }
389
    }
390
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
391
    # Append
392
    $sql .= $opt{append} if defined $opt{append} && !ref $sql;
393
    
394
    # Query
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
395
    my $query;
micro optimization and
Yuki Kimoto authored on 2011-10-25
396
    if (ref $sql) {
397
        $query = $sql;
398
        warn "execute method receiving query object as first parameter is DEPRECATED!" .
399
             "because this is very buggy.";
400
    }
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
401
    else {
402
        $query = $opt{reuse}->{$sql} if $opt{reuse};
403
        $query = $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter})
404
          unless $query;
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
405
        $query->statement($opt{statement} || '');
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
406
        $opt{reuse}->{$sql} = $query if $opt{reuse};
407
    }
408
        
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
409
    # Save query
micro optimization
Yuki Kimoto authored on 2011-10-23
410
    $self->{last_sql} = $query->{sql};
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
411

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
412
    # Return query
413
    return $query if $opt{query};
micro optimization
Yuki Kimoto authored on 2011-07-30
414
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
415
    # Merge query filter(DEPRECATED!)
DBIx::Custom::Query filter m...
Yuki Kimoto authored on 2011-07-30
416
    $filter ||= $query->{filter} || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
417
    
cleanup
Yuki Kimoto authored on 2011-04-02
418
    # Tables
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
419
    unshift @$tables, @{$query->{tables} || []};
micro optimization
Yuki Kimoto authored on 2011-07-30
420
    my $main_table = @{$tables}[-1];
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
421

            
422
    # Merge id to parameter
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
423
    if (defined $opt{id}) {
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
424
        croak "execute id option must be specified with primary_key option"
425
          unless $opt{primary_key};
426
        $opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
427
        $opt{id} = [$opt{id}] unless ref $opt{id};
428
        my $statement = $query->statement;
429
        for (my $i = 0; $i < @{$opt{primary_key}}; $i++) {
430
           my $key = $opt{primary_key}->[$i];
431
           $key = "$main_table.$key" if $statement eq 'update' ||
432
             $statement eq 'delete' || $statement eq 'select';
433
           next if exists $param->{$key};
434
           $param->{$key} = $opt{id}->[$i];
micro optimization and
Yuki Kimoto authored on 2011-10-25
435
           push @cleanup, $key;1
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
436
        }
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
437
    }
micro optimization
Yuki Kimoto authored on 2011-07-30
438
    
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
439
    # Created time and updated time
440
    if (defined $opt{created_at} || defined $opt{updated_at}) {
441
        my $now = $self->now->();
442
        if (defined $opt{create_at}) {
443
            $param->{$opt{created_at}} = $now;
444
            push @cleanup, $opt{created_at};
445
        }
446
        if (defined $opt{updated_at}) {
447
            $param->{$opt{updated_at}} = $now;
448
            push @cleanup, $opt{updated_at};
449
        }
450
    }
451
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
452
    # Cleanup tables(DEPRECATED!)
micro optimization
Yuki Kimoto authored on 2011-07-30
453
    $tables = $self->_remove_duplicate_table($tables, $main_table)
454
      if @$tables > 1;
cleanup
Yuki Kimoto authored on 2011-04-02
455
    
added type_rule into logic
Yuki Kimoto authored on 2011-06-09
456
    # Type rule
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
457
    my $type_filters = {};
micro optimization
Yuki Kimoto authored on 2011-10-23
458
    if ($self->{_type_rule_is_called}) {
459
        unless ($opt{type_rule_off}) {
460
            my $type_rule_off_parts = {
461
                1 => $opt{type_rule1_off},
462
                2 => $opt{type_rule2_off}
463
            };
464
            for my $i (1, 2) {
465
                unless ($type_rule_off_parts->{$i}) {
466
                    $type_filters->{$i} = {};
467
                    my $table_alias = $opt{table_alias} || {};
468
                    for my $alias (keys %$table_alias) {
469
                        my $table = $table_alias->{$alias};
470
                        
471
                        for my $column (keys %{$self->{"_into$i"}{key}{$table} || {}}) {
472
                            $type_filters->{$i}->{"$alias.$column"} = $self->{"_into$i"}{key}{$table}{$column};
473
                        }
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
474
                    }
micro optimization
Yuki Kimoto authored on 2011-10-23
475
                    $type_filters->{$i} = {%{$type_filters->{$i}}, %{$self->{"_into$i"}{key}{$main_table} || {}}}
476
                      if $main_table;
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
477
                }
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
478
            }
added type_rule into logic
Yuki Kimoto authored on 2011-06-09
479
        }
480
    }
cleanup
Yuki Kimoto authored on 2011-04-02
481
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
482
    # Applied filter(DEPRECATED!)
micro optimization
Yuki Kimoto authored on 2011-07-30
483
    if ($self->{filter}{on}) {
484
        my $applied_filter = {};
cleanup
Yuki Kimoto authored on 2011-10-21
485
        for my $table (@$tables) {
micro optimization
Yuki Kimoto authored on 2011-07-30
486
            $applied_filter = {
487
                %$applied_filter,
488
                %{$self->{filter}{out}->{$table} || {}}
489
            }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
490
        }
micro optimization
Yuki Kimoto authored on 2011-07-30
491
        $filter = {%$applied_filter, %$filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
492
    }
493
    
cleanup
Yuki Kimoto authored on 2011-04-02
494
    # Replace filter name to code
cleanup
Yuki Kimoto authored on 2011-10-21
495
    for my $column (keys %$filter) {
cleanup
Yuki Kimoto authored on 2011-04-02
496
        my $name = $filter->{$column};
497
        if (!defined $name) {
498
            $filter->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
499
        }
cleanup
Yuki Kimoto authored on 2011-04-02
500
        elsif (ref $name ne 'CODE') {
cleanup
Yuki Kimoto authored on 2011-04-25
501
          croak qq{Filter "$name" is not registered" } . _subname
cleanup
Yuki Kimoto authored on 2011-04-02
502
            unless exists $self->filters->{$name};
503
          $filter->{$column} = $self->filters->{$name};
cleanup
Yuki Kimoto authored on 2010-12-21
504
        }
505
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
506
    
cleanup
Yuki Kimoto authored on 2011-04-02
507
    # Create bind values
micro optimization
Yuki Kimoto authored on 2011-10-23
508
    my ($bind, $bind_types) = $self->_create_bind_values($param, $query->columns,
509
      $filter, $type_filters, $opt{bind_type} || $opt{type} || {});
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
510

            
cleanup
yuki-kimoto authored on 2010-10-17
511
    # Execute
micro optimization
Yuki Kimoto authored on 2011-10-23
512
    my $sth = $query->{sth};
cleanup
yuki-kimoto authored on 2010-10-17
513
    my $affected;
cleanup
Yuki Kimoto authored on 2011-03-21
514
    eval {
micro optimization
Yuki Kimoto authored on 2011-10-23
515
        if ($opt{bind_type} || $opt{type}) {
516
            $sth->bind_param($_ + 1, $bind->[$_],
517
                $bind_types->[$_] ? $bind_types->[$_] : ())
518
              for (0 .. @$bind - 1);
519
            $affected = $sth->execute;
520
        }
521
        else {
522
            $affected = $sth->execute(@$bind);
523
        }
cleanup
Yuki Kimoto authored on 2011-03-21
524
    };
improved error messages
Yuki Kimoto authored on 2011-04-18
525
    
micro optimization
Yuki Kimoto authored on 2011-07-30
526
    $self->_croak($@, qq{. Following SQL is executed.\n}
527
      . qq{$query->{sql}\n} . _subname) if $@;
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
528

            
529
    # Remove id from parameter
530
    delete $param->{$_} for @cleanup;
micro optimization and
Yuki Kimoto authored on 2011-10-25
531

            
cleanup
yuki-kimoto authored on 2010-10-17
532
    
improved debug message
Yuki Kimoto authored on 2011-05-23
533
    # DEBUG message
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
534
    if ($ENV{DBIX_CUSTOM_DEBUG}) {
535
        warn "SQL:\n" . $query->sql . "\n";
improved debug message
Yuki Kimoto authored on 2011-05-23
536
        my @output;
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
537
        for my $value (@$bind) {
improved debug message
Yuki Kimoto authored on 2011-05-23
538
            $value = 'undef' unless defined $value;
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
539
            $value = encode($ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8', $value)
improved debug message
Yuki Kimoto authored on 2011-05-23
540
              if utf8::is_utf8($value);
541
            push @output, $value;
542
        }
fixed DEBUG messsage bug
Yuki Kimoto authored on 2011-10-24
543
        warn "Bind values: " . join(', ', @output) . "\n\n";
improved debug message
Yuki Kimoto authored on 2011-05-23
544
    }
added environment variable D...
Yuki Kimoto authored on 2011-04-02
545
    
micro optimization
Yuki Kimoto authored on 2011-10-23
546
    # Not select statement
547
    return $affected unless $sth->{NUM_OF_FIELDS};
548

            
549
    # Filter(DEPRECATED!)
550
    my $infilter = {};
551
    if ($self->{filter}{on}) {
552
        $infilter->{in}  = {};
553
        $infilter->{end} = {};
554
        push @$tables, $main_table if $main_table;
555
        for my $table (@$tables) {
556
            for my $way (qw/in end/) {
557
                $infilter->{$way} = {%{$infilter->{$way}},
558
                  %{$self->{filter}{$way}{$table} || {}}};
cleanup
Yuki Kimoto authored on 2011-04-02
559
            }
cleanup
Yuki Kimoto authored on 2011-01-12
560
        }
cleanup
yuki-kimoto authored on 2010-10-17
561
    }
micro optimization
Yuki Kimoto authored on 2011-10-23
562
    
563
    # Result
564
    my $result = $self->result_class->new(
565
        sth => $sth,
566
        dbi => $self,
567
        default_filter => $self->{default_in_filter},
568
        filter => $infilter->{in} || {},
569
        end_filter => $infilter->{end} || {},
570
        type_rule => {
571
            from1 => $self->type_rule->{from1},
572
            from2 => $self->type_rule->{from2}
573
        },
574
    );
575
    $result;
cleanup
yuki-kimoto authored on 2010-10-17
576
}
577

            
added test
Yuki Kimoto authored on 2011-08-16
578
sub get_table_info {
cleanup
Yuki Kimoto authored on 2011-10-21
579
    my ($self, %opt) = @_;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
580
    
cleanup
Yuki Kimoto authored on 2011-10-21
581
    my $exclude = delete $opt{exclude};
582
    croak qq/"$_" is wrong option/ for keys %opt;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
583
    
added test
Yuki Kimoto authored on 2011-08-16
584
    my $table_info = [];
585
    $self->each_table(
586
        sub { push @$table_info, {table => $_[1], info => $_[2] } },
587
        exclude => $exclude
588
    );
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
589
    
cleanup test
Yuki Kimoto authored on 2011-08-16
590
    return [sort {$a->{table} cmp $b->{table} } @$table_info];
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
591
}
592

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
593
sub get_column_info {
cleanup
Yuki Kimoto authored on 2011-10-21
594
    my ($self, %opt) = @_;
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
595
    
cleanup
Yuki Kimoto authored on 2011-10-21
596
    my $exclude_table = delete $opt{exclude_table};
597
    croak qq/"$_" is wrong option/ for keys %opt;
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
598
    
599
    my $column_info = [];
600
    $self->each_column(
601
        sub { push @$column_info, {table => $_[1], column => $_[2], info => $_[3] } },
602
        exclude_table => $exclude_table
603
    );
604
    
605
    return [
606
      sort {$a->{table} cmp $b->{table} || $a->{column} cmp $b->{column} }
cleanup
Yuki Kimoto authored on 2011-08-16
607
        @$column_info];
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
608
}
609

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
610
sub helper {
611
    my $self = shift;
612
    
613
    # Register method
614
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
615
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
616
    
617
    return $self;
618
}
619

            
cleanup
yuki-kimoto authored on 2010-10-17
620
sub insert {
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
621
    my $self = shift;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
622
    
cleanup
Yuki Kimoto authored on 2011-10-21
623
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
624
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
625
    my %opt = @_;
micro optimization
Yuki Kimoto authored on 2011-10-23
626
    warn "insert method param option is DEPRECATED!" if $opt{param};
cleanup
Yuki Kimoto authored on 2011-10-21
627
    $param ||= delete $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
628
    
629
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
630
    if ($opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
631
        warn "insert timestamp option is DEPRECATED! use created_at with now attribute";
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
632
        my $columns = $insert_timestamp->[0];
633
        $columns = [$columns] unless ref $columns eq 'ARRAY';
634
        my $value = $insert_timestamp->[1];
635
        $value = $value->() if ref $value eq 'CODE';
636
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
637
    }
cleanup
Yuki Kimoto authored on 2011-10-21
638
    
639
    # Merge id to parameter
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
640
    my @cleanup;
micro optimization and
Yuki Kimoto authored on 2011-10-25
641
    my $id_param = {};
micro optimization
Yuki Kimoto authored on 2011-10-23
642
    if (defined $opt{id}) {
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
643
        croak "insert id option must be specified with primary_key option"
micro optimization
Yuki Kimoto authored on 2011-10-23
644
          unless $opt{primary_key};
645
        $opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
646
        $opt{id} = [$opt{id}] unless ref $opt{id};
647
        for (my $i = 0; $i < @{$opt{primary_key}}; $i++) {
648
           my $key = $opt{primary_key}->[$i];
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
649
           next if exists $param->{$key};
micro optimization
Yuki Kimoto authored on 2011-10-23
650
           $param->{$key} = $opt{id}->[$i];
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
651
           push @cleanup, $key;
micro optimization
Yuki Kimoto authored on 2011-10-23
652
        }
653
    }
cleanup
Yuki Kimoto authored on 2011-10-21
654
    
cleanup
Yuki Kimoto authored on 2011-04-02
655
    # Insert statement
cleanup
Yuki Kimoto authored on 2011-10-21
656
    my $sql = "insert ";
cleanup
Yuki Kimoto authored on 2011-10-21
657
    $sql .= "$opt{prefix} " if defined $opt{prefix};
658
    $sql .= "into " . $self->_q($opt{table}) . " "
659
      . $self->values_clause($param, {wrap => $opt{wrap}}) . " ";
micro optimization
Yuki Kimoto authored on 2011-10-23
660

            
661
    # Remove id from parameter
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
662
    delete $param->{$_} for @cleanup;
packaging one directory
yuki-kimoto authored on 2009-11-16
663
    
664
    # Execute query
micro optimization
Yuki Kimoto authored on 2011-10-23
665
    $opt{statement} = 'insert';
micro optimization
Yuki Kimoto authored on 2011-10-23
666
    $self->execute($sql, $param, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
667
}
668

            
micro optimization
Yuki Kimoto authored on 2011-10-23
669
sub _merge_id_to_param {
670
    my ($self, $id, $primary_keys, $param) = @_;
671
    
672
    # Create parameter
673
    $id = [$id] unless ref $id;
674
    
675
    return $param;
676
}
677

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
678
sub insert_timestamp {
679
    my $self = shift;
680
    
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
681
    warn "insert_timestamp method is DEPRECATED! use now attribute";
682
    
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
683
    if (@_) {
684
        $self->{insert_timestamp} = [@_];
685
        
686
        return $self;
687
    }
688
    return $self->{insert_timestamp};
689
}
690

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
691
sub include_model {
692
    my ($self, $name_space, $model_infos) = @_;
693
    
cleanup
Yuki Kimoto authored on 2011-04-02
694
    # Name space
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
695
    $name_space ||= '';
cleanup
Yuki Kimoto authored on 2011-04-02
696
    
697
    # Get Model infomations
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
698
    unless ($model_infos) {
cleanup
Yuki Kimoto authored on 2011-04-02
699

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
700
        # Load name space module
cleanup
Yuki Kimoto authored on 2011-04-25
701
        croak qq{"$name_space" is invalid class name } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
702
          if $name_space =~ /[^\w:]/;
703
        eval "use $name_space";
cleanup
Yuki Kimoto authored on 2011-04-25
704
        croak qq{Name space module "$name_space.pm" is needed. $@ }
705
            . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
706
          if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
707
        
708
        # Search model modules
709
        my $path = $INC{"$name_space.pm"};
710
        $path =~ s/\.pm$//;
711
        opendir my $dh, $path
cleanup
Yuki Kimoto authored on 2011-04-25
712
          or croak qq{Can't open directory "$path": $! } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
713
        $model_infos = [];
714
        while (my $module = readdir $dh) {
715
            push @$model_infos, $module
716
              if $module =~ s/\.pm$//;
717
        }
718
        close $dh;
719
    }
720
    
cleanup
Yuki Kimoto authored on 2011-04-02
721
    # Include models
cleanup
Yuki Kimoto authored on 2011-10-21
722
    for my $model_info (@$model_infos) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
723
        
cleanup
Yuki Kimoto authored on 2011-04-02
724
        # Load model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
725
        my $model_class;
726
        my $model_name;
727
        my $model_table;
728
        if (ref $model_info eq 'HASH') {
729
            $model_class = $model_info->{class};
730
            $model_name  = $model_info->{name};
731
            $model_table = $model_info->{table};
732
            
733
            $model_name  ||= $model_class;
734
            $model_table ||= $model_name;
735
        }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
736
        else { $model_class = $model_name = $model_table = $model_info }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
737
        my $mclass = "${name_space}::$model_class";
cleanup
Yuki Kimoto authored on 2011-04-25
738
        croak qq{"$mclass" is invalid class name } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
739
          if $mclass =~ /[^\w:]/;
740
        unless ($mclass->can('isa')) {
741
            eval "use $mclass";
cleanup
Yuki Kimoto authored on 2011-04-25
742
            croak "$@ " . _subname if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
743
        }
744
        
cleanup
Yuki Kimoto authored on 2011-04-02
745
        # Create model
cleanup
Yuki Kimoto authored on 2011-10-21
746
        my $opt = {};
747
        $opt->{model_class} = $mclass if $mclass;
748
        $opt->{name}        = $model_name if $model_name;
749
        $opt->{table}       = $model_table if $model_table;
750
        $self->create_model($opt);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
751
    }
752
    
753
    return $self;
754
}
755

            
added EXPERIMENTAL like_valu...
Yuki Kimoto authored on 2011-09-16
756
sub like_value { sub { "%$_[0]%" } }
757

            
added DBIx::Custom::Mapper
Yuki Kimoto authored on 2011-08-26
758
sub mapper {
759
    my $self = shift;
760
    return DBIx::Custom::Mapper->new(@_);
761
}
762

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
763
sub merge_param {
764
    my ($self, @params) = @_;
765
    
cleanup
Yuki Kimoto authored on 2011-04-02
766
    # Merge parameters
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
767
    my $merge = {};
cleanup
Yuki Kimoto authored on 2011-10-21
768
    for my $param (@params) {
769
        for my $column (keys %$param) {
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
770
            my $param_is_array = ref $param->{$column} eq 'ARRAY' ? 1 : 0;
771
            
772
            if (exists $merge->{$column}) {
773
                $merge->{$column} = [$merge->{$column}]
774
                  unless ref $merge->{$column} eq 'ARRAY';
775
                push @{$merge->{$column}},
776
                  ref $param->{$column} ? @{$param->{$column}} : $param->{$column};
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
777
            }
778
            else {
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
779
                $merge->{$column} = $param->{$column};
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
780
            }
781
        }
782
    }
783
    
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
784
    return $merge;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
785
}
786

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
787
sub model {
788
    my ($self, $name, $model) = @_;
789
    
cleanup
Yuki Kimoto authored on 2011-04-02
790
    # Set model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
791
    if ($model) {
792
        $self->models->{$name} = $model;
793
        return $self;
794
    }
795
    
796
    # Check model existance
cleanup
Yuki Kimoto authored on 2011-04-25
797
    croak qq{Model "$name" is not included } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
798
      unless $self->models->{$name};
799
    
cleanup
Yuki Kimoto authored on 2011-04-02
800
    # Get model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
801
    return $self->models->{$name};
802
}
803

            
cleanup
Yuki Kimoto authored on 2011-03-21
804
sub mycolumn {
805
    my ($self, $table, $columns) = @_;
806
    
cleanup
Yuki Kimoto authored on 2011-04-02
807
    # Create column clause
808
    my @column;
cleanup
Yuki Kimoto authored on 2011-03-21
809
    $columns ||= [];
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
810
    push @column, $self->_q($table) . "." . $self->_q($_) .
811
      " as " . $self->_q($_)
812
      for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
813
    
814
    return join (', ', @column);
815
}
816

            
added dbi_options attribute
kimoto authored on 2010-12-20
817
sub new {
818
    my $self = shift->SUPER::new(@_);
819
    
cleanup
Yuki Kimoto authored on 2011-04-02
820
    # Check attributes
added dbi_options attribute
kimoto authored on 2010-12-20
821
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
822
    for my $attr (@attrs) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
823
        croak qq{Invalid attribute: "$attr" } . _subname
added dbi_options attribute
kimoto authored on 2010-12-20
824
          unless $self->can($attr);
825
    }
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
826

            
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
827
    # DEPRECATED
cleanup
Yuki Kimoto authored on 2011-08-13
828
    $self->{_tags} = {
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
829
        '?'     => \&DBIx::Custom::Tag::placeholder,
830
        '='     => \&DBIx::Custom::Tag::equal,
831
        '<>'    => \&DBIx::Custom::Tag::not_equal,
832
        '>'     => \&DBIx::Custom::Tag::greater_than,
833
        '<'     => \&DBIx::Custom::Tag::lower_than,
834
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
835
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
836
        'like'  => \&DBIx::Custom::Tag::like,
837
        'in'    => \&DBIx::Custom::Tag::in,
838
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
839
        'update_param' => \&DBIx::Custom::Tag::update_param
cleanup
Yuki Kimoto authored on 2011-08-13
840
    };
841
    
842
    return $self;
843
}
844

            
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
845
sub not_exists { DBIx::Custom::NotExists->singleton }
cleanup
Yuki Kimoto authored on 2011-08-13
846

            
847
sub order {
848
    my $self = shift;
849
    return DBIx::Custom::Order->new(dbi => $self, @_);
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
850
}
851

            
cleanup
yuki-kimoto authored on 2010-10-17
852
sub register_filter {
cleanup
Yuki Kimoto authored on 2011-04-02
853
    my $self = shift;
cleanup
yuki-kimoto authored on 2010-10-17
854
    
855
    # Register filter
856
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
cleanup
Yuki Kimoto authored on 2011-04-02
857
    $self->filters({%{$self->filters}, %$filters});
cleanup
yuki-kimoto authored on 2010-10-17
858
    
cleanup
Yuki Kimoto authored on 2011-04-02
859
    return $self;
cleanup
yuki-kimoto authored on 2010-10-17
860
}
packaging one directory
yuki-kimoto authored on 2009-11-16
861

            
862
sub select {
cleanup
Yuki Kimoto authored on 2011-10-21
863
    my ($self, %opt) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
864

            
cleanup
Yuki Kimoto authored on 2011-10-21
865
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
866
    my $tables = ref $opt{table} eq 'ARRAY' ? $opt{table}
867
               : defined $opt{table} ? [$opt{table}]
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
868
               : [];
cleanup
Yuki Kimoto authored on 2011-10-21
869
    $opt{table} = $tables;
cleanup
Yuki Kimoto authored on 2011-10-21
870
    my $where_param = $opt{where_param} || delete $opt{param} || {};
select method where_param op...
Yuki Kimoto authored on 2011-10-25
871
    warn "select method where_param option is DEPRECATED!"
872
      if $opt{where_param};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
873
    
cleanup
Yuki Kimoto authored on 2011-03-09
874
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-10-21
875
    if ($opt{relation}) {
876
        warn "select() relation option is DEPRECATED!";
877
        $self->_add_relation_table($tables, $opt{relation});
878
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
879
    
cleanup
Yuki Kimoto authored on 2011-04-02
880
    # Select statement
micro optimization
Yuki Kimoto authored on 2011-09-30
881
    my $sql = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
882
    
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
883
    # Prefix
cleanup
Yuki Kimoto authored on 2011-10-21
884
    $sql .= "$opt{prefix} " if defined $opt{prefix};
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
885
    
cleanup
Yuki Kimoto authored on 2011-10-21
886
    # Column
cleanup
Yuki Kimoto authored on 2011-10-21
887
    if (defined $opt{column}) {
888
        my $columns
889
          = ref $opt{column} eq 'ARRAY' ? $opt{column} : [$opt{column}];
cleanup
Yuki Kimoto authored on 2011-10-21
890
        for my $column (@$columns) {
- select() column option can...
Yuki Kimoto authored on 2011-06-08
891
            if (ref $column eq 'HASH') {
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
892
                $column = $self->column(%$column) if ref $column eq 'HASH';
- select() column option can...
Yuki Kimoto authored on 2011-06-08
893
            }
894
            elsif (ref $column eq 'ARRAY') {
- select method column optio...
Yuki Kimoto authored on 2011-07-11
895
                if (@$column == 3 && $column->[1] eq 'as') {
896
                    warn "[COLUMN, as => ALIAS] is DEPRECATED! use [COLUMN => ALIAS]";
897
                    splice @$column, 1, 1;
898
                }
899
                
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
900
                $column = join(' ', $column->[0], 'as', $self->_q($column->[1]));
- select() column option can...
Yuki Kimoto authored on 2011-06-08
901
            }
cleanup
Yuki Kimoto authored on 2011-04-02
902
            unshift @$tables, @{$self->_search_tables($column)};
micro optimization
Yuki Kimoto authored on 2011-09-30
903
            $sql .= "$column, ";
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
904
        }
micro optimization
Yuki Kimoto authored on 2011-09-30
905
        $sql =~ s/, $/ /;
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
906
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
907
    else { $sql .= '* ' }
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
908
    
909
    # Table
micro optimization
Yuki Kimoto authored on 2011-09-30
910
    $sql .= 'from ';
cleanup
Yuki Kimoto authored on 2011-10-21
911
    if ($opt{relation}) {
cleanup
Yuki Kimoto authored on 2011-03-30
912
        my $found = {};
cleanup
Yuki Kimoto authored on 2011-10-21
913
        for my $table (@$tables) {
micro optimization
Yuki Kimoto authored on 2011-09-30
914
            $sql .= $self->_q($table) . ', ' unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
915
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
916
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
917
    }
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
918
    else { $sql .= $self->_q($tables->[-1] || '') . ' ' }
micro optimization
Yuki Kimoto authored on 2011-09-30
919
    $sql =~ s/, $/ /;
cleanup
Yuki Kimoto authored on 2011-10-21
920
    croak "select method table option must be specified " . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
921
      unless $tables->[-1];
cleanup
Yuki Kimoto authored on 2011-04-01
922

            
cleanup
Yuki Kimoto authored on 2011-04-02
923
    # Add tables in parameter
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
924
    unshift @$tables,
925
            @{$self->_search_tables(join(' ', keys %$where_param) || '')};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
926
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
927
    # Where
cleanup
Yuki Kimoto authored on 2011-10-25
928
    my $w = $self->_where_clause_and_param($opt{where}, $where_param,
929
      delete $opt{id}, $opt{primary_key}, $tables->[-1]);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
930
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
931
    # Add table names in where clause
cleanup
Yuki Kimoto authored on 2011-10-21
932
    unshift @$tables, @{$self->_search_tables($w->{clause})};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
933
    
cleanup
Yuki Kimoto authored on 2011-10-21
934
    # Join statement
cleanup
Yuki Kimoto authored on 2011-10-21
935
    $self->_push_join(\$sql, $opt{join}, $tables) if defined $opt{join};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
936
    
cleanup
Yuki Kimoto authored on 2011-03-09
937
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-10-21
938
    $sql .= "$w->{clause} ";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
939
    
cleanup
Yuki Kimoto authored on 2011-03-08
940
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-10-21
941
    $self->_push_relation(\$sql, $tables, $opt{relation}, $w->{clause} eq '' ? 1 : 0)
cleanup
Yuki Kimoto authored on 2011-10-21
942
      if $opt{relation};
cleanup
Yuki Kimoto authored on 2011-03-08
943
    
packaging one directory
yuki-kimoto authored on 2009-11-16
944
    # Execute query
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
945
    $opt{statement} = 'select';
cleanup
Yuki Kimoto authored on 2011-10-21
946
    my $result = $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
947
    
micro optimization
Yuki Kimoto authored on 2011-10-23
948
    $result;
packaging one directory
yuki-kimoto authored on 2009-11-16
949
}
950

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
951
sub setup_model {
952
    my $self = shift;
953
    
cleanup
Yuki Kimoto authored on 2011-04-02
954
    # Setup model
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
955
    $self->each_column(
956
        sub {
957
            my ($self, $table, $column, $column_info) = @_;
958
            if (my $model = $self->models->{$table}) {
959
                push @{$model->columns}, $column;
960
            }
961
        }
962
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
963
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
964
}
965

            
update pod
Yuki Kimoto authored on 2011-08-10
966
sub show_datatype {
967
    my ($self, $table) = @_;
968
    croak "Table name must be specified" unless defined $table;
969
    print "$table\n";
970
    
971
    my $result = $self->select(table => $table, where => "'0' <> '0'");
972
    my $sth = $result->sth;
973

            
974
    my $columns = $sth->{NAME};
975
    my $data_types = $sth->{TYPE};
976
    
977
    for (my $i = 0; $i < @$columns; $i++) {
978
        my $column = $columns->[$i];
979
        my $data_type = $data_types->[$i];
980
        print "$column: $data_type\n";
981
    }
982
}
983

            
984
sub show_typename {
985
    my ($self, $t) = @_;
986
    croak "Table name must be specified" unless defined $t;
987
    print "$t\n";
988
    
989
    $self->each_column(sub {
990
        my ($self, $table, $column, $infos) = @_;
991
        return unless $table eq $t;
992
        my $typename = $infos->{TYPE_NAME};
993
        print "$column: $typename\n";
994
    });
995
    
996
    return $self;
997
}
998

            
test cleanup
Yuki Kimoto authored on 2011-08-15
999
sub show_tables {
1000
    my $self = shift;
1001
    
1002
    my %tables;
1003
    $self->each_table(sub { $tables{$_[1]}++ });
1004
    print join("\n", sort keys %tables) . "\n";
1005
    return $self;
1006
}
1007

            
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1008
sub type_rule {
1009
    my $self = shift;
micro optimization
Yuki Kimoto authored on 2011-10-23
1010

            
1011
    $self->{_type_rule_is_called} = 1;
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1012
    
1013
    if (@_) {
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
1014
        my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
1015
        
1016
        # Into
cleanup
Yuki Kimoto authored on 2011-10-21
1017
        for my $i (1 .. 2) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1018
            my $into = "into$i";
cleanup
Yuki Kimoto authored on 2011-08-16
1019
            my $exists_into = exists $type_rule->{$into};
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1020
            $type_rule->{$into} = _array_to_hash($type_rule->{$into});
1021
            $self->{type_rule} = $type_rule;
1022
            $self->{"_$into"} = {};
cleanup
Yuki Kimoto authored on 2011-10-21
1023
            for my $type_name (keys %{$type_rule->{$into} || {}}) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1024
                croak qq{type name of $into section must be lower case}
1025
                  if $type_name =~ /[A-Z]/;
1026
            }
cleanup
Yuki Kimoto authored on 2011-08-16
1027
            
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1028
            $self->each_column(sub {
1029
                my ($dbi, $table, $column, $column_info) = @_;
1030
                
1031
                my $type_name = lc $column_info->{TYPE_NAME};
1032
                if ($type_rule->{$into} &&
1033
                    (my $filter = $type_rule->{$into}->{$type_name}))
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
1034
                {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1035
                    return unless exists $type_rule->{$into}->{$type_name};
1036
                    if  (defined $filter && ref $filter ne 'CODE') 
1037
                    {
1038
                        my $fname = $filter;
1039
                        croak qq{Filter "$fname" is not registered" } . _subname
1040
                          unless exists $self->filters->{$fname};
1041
                        
1042
                        $filter = $self->filters->{$fname};
1043
                    }
1044

            
micro optimization
Yuki Kimoto authored on 2011-07-30
1045
                    $self->{"_$into"}{key}{$table}{$column} = $filter;
1046
                    $self->{"_$into"}{dot}{"$table.$column"} = $filter;
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1047
                }
1048
            });
1049
        }
1050

            
1051
        # From
cleanup
Yuki Kimoto authored on 2011-10-21
1052
        for my $i (1 .. 2) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1053
            $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
cleanup
Yuki Kimoto authored on 2011-10-21
1054
            for my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1055
                croak qq{data type of from$i section must be lower case or number}
1056
                  if $data_type =~ /[A-Z]/;
1057
                my $fname = $type_rule->{"from$i"}{$data_type};
1058
                if (defined $fname && ref $fname ne 'CODE') {
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
1059
                    croak qq{Filter "$fname" is not registered" } . _subname
1060
                      unless exists $self->filters->{$fname};
1061
                    
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1062
                    $type_rule->{"from$i"}{$data_type} = $self->filters->{$fname};
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
1063
                }
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
1064
            }
1065
        }
1066
        
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1067
        return $self;
1068
    }
1069
    
1070
    return $self->{type_rule} || {};
1071
}
1072

            
cleanup
yuki-kimoto authored on 2010-10-17
1073
sub update {
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
1074
    my $self = shift;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1075

            
cleanup
Yuki Kimoto authored on 2011-10-21
1076
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1077
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
1078
    my %opt = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
1079
    warn "update param option is DEPRECATED!" if $opt{param};
cleanup
Yuki Kimoto authored on 2011-10-21
1080
    warn "update method where_param option is DEPRECATED!"
1081
      if $opt{where_param};
cleanup
Yuki Kimoto authored on 2011-10-21
1082
    $param ||= $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1083
    
cleanup
Yuki Kimoto authored on 2011-10-21
1084
    # Don't allow update all rows
1085
    croak qq{update method where option must be specified } . _subname
1086
      if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all};
1087
    
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1088
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
1089
    if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
1090
        warn "update timestamp option is DEPRECATED! use updated_at and now method";
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1091
        my $columns = $update_timestamp->[0];
1092
        $columns = [$columns] unless ref $columns eq 'ARRAY';
1093
        my $value = $update_timestamp->[1];
1094
        $value = $value->() if ref $value eq 'CODE';
1095
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1096
    }
1097

            
cleanup
Yuki Kimoto authored on 2011-10-21
1098
    # Assign clause
cleanup
Yuki Kimoto authored on 2011-10-21
1099
    my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
cleanup
Yuki Kimoto authored on 2011-10-21
1100
    
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1101
    # Where
cleanup
Yuki Kimoto authored on 2011-10-25
1102
    my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
1103
      delete $opt{id}, $opt{primary_key}, $opt{table});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1104
    
cleanup
Yuki Kimoto authored on 2011-04-02
1105
    # Update statement
cleanup
Yuki Kimoto authored on 2011-10-21
1106
    my $sql = "update ";
1107
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-21
1108
    $sql .= $self->_q($opt{table}) . " set $assign_clause $w->{clause} ";
cleanup
Yuki Kimoto authored on 2011-01-27
1109
    
cleanup
yuki-kimoto authored on 2010-10-17
1110
    # Execute query
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
1111
    $opt{statement} = 'update';
micro optimization and
Yuki Kimoto authored on 2011-10-25
1112
    $self->execute($sql, [$param, $w->{param}], %opt);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1113
}
1114

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1117
sub update_or_insert {
1118
    my $self = shift;
1119

            
cleanup
Yuki Kimoto authored on 2011-10-21
1120
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1121
    my $param  = shift;
1122
    my %opt = @_;
1123
    my $id = $opt{id};
1124
    my $primary_key = $opt{primary_key};
1125
    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
1126
    croak "update_or_insert method need primary_key option " .
1127
          "when id is specified" . _subname
1128
      if defined $id && !defined $primary_key;
1129
    my $table  = $opt{table};
1130
    croak qq{"table" option must be specified } . _subname
1131
      unless defined $table;
1132
    my $select_option = $opt{select_option};
1133
    
1134
    my $rows = $self->select(table => $table, id => $id,
1135
        primary_key => $primary_key, %$select_option)->all;
1136
    
1137
    croak "selected row count must be one or zero" . _subname
1138
      if @$rows > 1;
1139
    
1140
    my $row = $rows->[0];
1141
    my @opt = (table => $table);
1142
    push @opt, id => $id, primary_key => $primary_key if defined $id;
1143
    push @opt, %opt;
1144
    
1145
    if ($row) {
1146
        return $self->update($param, @opt);
1147
    }
1148
    else {
1149
        return $self->insert($param, @opt);
1150
    }
1151
}
1152

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1153
sub update_timestamp {
1154
    my $self = shift;
1155
    
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
1156
    warn "update_timestamp method is DEPRECATED! use now method";
1157
    
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1158
    if (@_) {
1159
        $self->{update_timestamp} = [@_];
1160
        
1161
        return $self;
1162
    }
1163
    return $self->{update_timestamp};
1164
}
1165

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1166
sub values_clause {
1167
    my ($self, $param, $opts) = @_;
1168
    
1169
    my $wrap = $opts->{wrap} || {};
1170
    
1171
    # Create insert parameter tag
micro optimization
Yuki Kimoto authored on 2011-10-23
1172
    my $safety = $self->{safety_character} || $self->safety_character;
improved performance
Yuki Kimoto authored on 2011-10-22
1173
    my $qp = $self->_q('');
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
1174
    my $q = substr($qp, 0, 1) || '';
1175
    my $p = substr($qp, 1, 1) || '';
improved performance
Yuki Kimoto authored on 2011-10-22
1176
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1177
    # Check unsafety keys
1178
    unless ((join('', keys %$param) || '') =~ /^[$safety\.]+$/) {
1179
        for my $column (keys %$param) {
1180
            croak qq{"$column" is not safety column name } . _subname
1181
              unless $column =~ /^[$safety\.]+$/;
1182
        }
1183
    }
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1184
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1185
    # values clause(performance is important)
micro optimization
Yuki Kimoto authored on 2011-10-23
1186
    '(' .
1187
    join(
1188
      ', ',
1189
      map { "$q$_$p" } sort keys %$param
1190
    ) .
1191
    ') values (' .
1192
    join(
1193
      ', ',
1194
      map {
1195
          ref $param->{$_} eq 'SCALAR' ? ${$param->{$_}} :
1196
          $wrap->{$_} ? $wrap->{$_}->(":$_") :
1197
          ":$_";
1198
      } sort keys %$param
1199
    ) .
1200
    ')'
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1201
}
1202

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
1203
sub where { DBIx::Custom::Where->new(dbi => shift, @_) }
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1204

            
updated pod
Yuki Kimoto authored on 2011-06-21
1205
sub _create_query {
cleanup
Yuki Kimoto authored on 2011-06-13
1206
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
1207
    my ($self, $source, $after_build_sql) = @_;
cleanup
Yuki Kimoto authored on 2011-06-13
1208
    
updated pod
Yuki Kimoto authored on 2011-06-21
1209
    # Cache
1210
    my $cache = $self->cache;
1211
    
1212
    # Query
1213
    my $query;
1214
    
1215
    # Get cached query
1216
    if ($cache) {
cleanup
Yuki Kimoto authored on 2011-06-13
1217
        
updated pod
Yuki Kimoto authored on 2011-06-21
1218
        # Get query
1219
        my $q = $self->cache_method->($self, $source);
cleanup
Yuki Kimoto authored on 2011-06-13
1220
        
updated pod
Yuki Kimoto authored on 2011-06-21
1221
        # Create query
1222
        if ($q) {
1223
            $query = DBIx::Custom::Query->new($q);
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1224
            $query->{filters} = $self->filters;
cleanup
Yuki Kimoto authored on 2011-06-13
1225
        }
updated pod
Yuki Kimoto authored on 2011-06-21
1226
    }
1227
    
1228
    # Create query
1229
    unless ($query) {
1230

            
1231
        # Create query
1232
        my $builder = $self->query_builder;
1233
        $query = $builder->build_query($source);
1234

            
1235
        # Remove reserved word quote
1236
        if (my $q = $self->_quote) {
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1237
            $q = quotemeta($q);
1238
            $_ =~ s/[$q]//g for @{$query->columns}
cleanup
Yuki Kimoto authored on 2011-06-13
1239
        }
updated pod
Yuki Kimoto authored on 2011-06-21
1240

            
1241
        # Save query to cache
1242
        $self->cache_method->(
1243
            $self, $source,
1244
            {
1245
                sql     => $query->sql, 
1246
                columns => $query->columns,
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
1247
                tables  => $query->{tables} || []
updated pod
Yuki Kimoto authored on 2011-06-21
1248
            }
1249
        ) if $cache;
cleanup
Yuki Kimoto authored on 2011-06-13
1250
    }
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1251

            
1252
    # Filter SQL
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1253
    if ($after_build_sql) {
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1254
        my $sql = $query->sql;
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1255
        $sql = $after_build_sql->($sql);
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1256
        $query->sql($sql);
1257
    }
1258
        
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
1259
    # Save sql
1260
    $self->last_sql($query->sql);
1261
    
updated pod
Yuki Kimoto authored on 2011-06-21
1262
    # Prepare statement handle
1263
    my $sth;
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
1264
    eval { $sth = $self->dbh->prepare($query->{sql}) };
updated pod
Yuki Kimoto authored on 2011-06-21
1265
    
1266
    if ($@) {
1267
        $self->_croak($@, qq{. Following SQL is executed.\n}
1268
                        . qq{$query->{sql}\n} . _subname);
1269
    }
1270
    
1271
    # Set statement handle
1272
    $query->sth($sth);
1273
    
1274
    # Set filters
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1275
    $query->{filters} = $self->filters;
updated pod
Yuki Kimoto authored on 2011-06-21
1276
    
1277
    return $query;
cleanup
Yuki Kimoto authored on 2011-06-13
1278
}
1279

            
cleanup
Yuki Kimoto authored on 2011-04-02
1280
sub _create_bind_values {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1281
    my ($self, $params, $columns, $filter, $type_filters, $bind_type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1282
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1283
    $bind_type = _array_to_hash($bind_type) if ref $bind_type eq 'ARRAY';
1284
    
cleanup
Yuki Kimoto authored on 2011-04-02
1285
    # Create bind values
micro optimization
Yuki Kimoto authored on 2011-10-23
1286
    my @bind;
1287
    my @types;
1288
    my %count;
1289
    my %not_exists;
cleanup
Yuki Kimoto authored on 2011-10-21
1290
    for my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1291
        
micro optimization
Yuki Kimoto authored on 2011-10-23
1292
        # Bind value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1293
        if(ref $params->{$column} eq 'ARRAY') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1294
            my $i = $count{$column} || 0;
1295
            $i += $not_exists{$column} || 0;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1296
            my $found;
1297
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1298
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1299
                    $not_exists{$column}++;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1300
                }
1301
                else  {
micro optimization
Yuki Kimoto authored on 2011-10-23
1302
                    push @bind, $params->{$column}->[$k];
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1303
                    $found = 1;
1304
                    last
1305
                }
1306
            }
1307
            next unless $found;
1308
        }
micro optimization
Yuki Kimoto authored on 2011-10-23
1309
        else { push @bind, $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1310
        
cleanup
Yuki Kimoto authored on 2011-01-12
1311
        # Filter
micro optimization
Yuki Kimoto authored on 2011-10-23
1312
        if (my $f = $filter->{$column} || $self->{default_out_filter} || '') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1313
            $bind[-1] = $f->($bind[-1]);
micro optimization
Yuki Kimoto authored on 2011-10-23
1314
        }
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
1315
        
1316
        # Type rule
micro optimization
Yuki Kimoto authored on 2011-10-23
1317
        if ($self->{_type_rule_is_called}) {
1318
            my $tf1 = $self->{"_into1"}->{dot}->{$column}
1319
              || $type_filters->{1}->{$column};
micro optimization
Yuki Kimoto authored on 2011-10-23
1320
            $bind[-1] = $tf1->($bind[-1]) if $tf1;
micro optimization
Yuki Kimoto authored on 2011-10-23
1321
            my $tf2 = $self->{"_into2"}->{dot}->{$column}
1322
              || $type_filters->{2}->{$column};
micro optimization
Yuki Kimoto authored on 2011-10-23
1323
            $bind[-1] = $tf2->($bind[-1]) if $tf2;
micro optimization
Yuki Kimoto authored on 2011-10-23
1324
        }
micro optimization
Yuki Kimoto authored on 2011-10-22
1325
       
micro optimization
Yuki Kimoto authored on 2011-10-23
1326
        # Bind types
micro optimization
Yuki Kimoto authored on 2011-10-23
1327
        push @types, $bind_type->{$column};
removed reconnect method
yuki-kimoto authored on 2010-05-28
1328
        
1329
        # Count up 
micro optimization
Yuki Kimoto authored on 2011-10-23
1330
        $count{$column}++;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1331
    }
1332
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1333
    return (\@bind, \@types);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1334
}
1335

            
cleanup
Yuki Kimoto authored on 2011-10-21
1336
sub _id_to_param {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1337
    my ($self, $id, $primary_keys, $table) = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
1338
    
1339
    # Check primary key
cleanup
Yuki Kimoto authored on 2011-10-21
1340
    croak "primary_key option " .
1341
          "must be specified with id option " . _subname
1342
      unless defined $primary_keys;
1343
    $primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY';
improved error messages
Yuki Kimoto authored on 2011-04-18
1344
    
cleanup
Yuki Kimoto authored on 2011-06-08
1345
    # Create parameter
1346
    my $param = {};
fixed small insert, update, ...
Yuki Kimoto authored on 2011-06-21
1347
    if (defined $id) {
cleanup
Yuki Kimoto authored on 2011-06-08
1348
        $id = [$id] unless ref $id;
1349
        croak qq{"id" must be constant value or array reference}
improved error messages
Yuki Kimoto authored on 2011-04-18
1350
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1351
          unless !ref $id || ref $id eq 'ARRAY';
1352
        croak qq{"id" must contain values same count as primary key}
improved error messages
Yuki Kimoto authored on 2011-04-18
1353
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1354
          unless @$primary_keys eq @$id;
improved error messages
Yuki Kimoto authored on 2011-04-18
1355
        for(my $i = 0; $i < @$primary_keys; $i ++) {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1356
           my $key = $primary_keys->[$i];
1357
           $key = "$table." . $key if $table;
1358
           $param->{$key} = $id->[$i];
improved error messages
Yuki Kimoto authored on 2011-04-18
1359
        }
1360
    }
1361
    
cleanup
Yuki Kimoto authored on 2011-06-08
1362
    return $param;
improved error messages
Yuki Kimoto authored on 2011-04-18
1363
}
1364

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1365
sub _connect {
1366
    my $self = shift;
1367
    
1368
    # Attributes
added warnings
Yuki Kimoto authored on 2011-06-07
1369
    my $dsn = $self->data_source;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1370
    warn "data_source is DEPRECATED!\n"
fixed bug that data_source D...
Yuki Kimoto authored on 2011-06-13
1371
      if $dsn;
added warnings
Yuki Kimoto authored on 2011-06-07
1372
    $dsn ||= $self->dsn;
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1373
    croak qq{"dsn" must be specified } . _subname
1374
      unless $dsn;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1375
    my $user        = $self->user;
1376
    my $password    = $self->password;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1377
    my $option = $self->_option;
1378
    $option = {%{$self->default_option}, %$option};
cleanup
Yuki Kimoto authored on 2011-08-16
1379
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1380
    # Connect
cleanup
Yuki Kimoto authored on 2011-08-16
1381
    my $dbh;
1382
    eval {
1383
        $dbh = DBI->connect(
1384
            $dsn,
1385
            $user,
1386
            $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1387
            $option
cleanup
Yuki Kimoto authored on 2011-08-16
1388
        );
1389
    };
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1390
    
1391
    # Connect error
cleanup
Yuki Kimoto authored on 2011-04-25
1392
    croak "$@ " . _subname if $@;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1393
    
1394
    return $dbh;
1395
}
1396

            
cleanup
yuki-kimoto authored on 2010-10-17
1397
sub _croak {
1398
    my ($self, $error, $append) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
1399
    
1400
    # Append
cleanup
yuki-kimoto authored on 2010-10-17
1401
    $append ||= "";
1402
    
1403
    # Verbose
1404
    if ($Carp::Verbose) { croak $error }
1405
    
1406
    # Not verbose
1407
    else {
1408
        
1409
        # Remove line and module infromation
1410
        my $at_pos = rindex($error, ' at ');
1411
        $error = substr($error, 0, $at_pos);
1412
        $error =~ s/\s+$//;
1413
        croak "$error$append";
1414
    }
1415
}
1416

            
prepare oracle test
Yuki Kimoto authored on 2011-08-15
1417
sub _driver { lc shift->{dbh}->{Driver}->{Name} }
1418

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1419
sub _need_tables {
1420
    my ($self, $tree, $need_tables, $tables) = @_;
1421
    
cleanup
Yuki Kimoto authored on 2011-04-02
1422
    # Get needed tables
cleanup
Yuki Kimoto authored on 2011-10-21
1423
    for my $table (@$tables) {
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1424
        if ($tree->{$table}) {
1425
            $need_tables->{$table} = 1;
1426
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1427
        }
1428
    }
1429
}
1430

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1431
sub _option {
1432
    my $self = shift;
1433
    my $option = {%{$self->dbi_options}, %{$self->dbi_option}, %{$self->option}};
1434
    warn "dbi_options is DEPRECATED! use option instead\n"
1435
      if keys %{$self->dbi_options};
1436
    warn "dbi_option is DEPRECATED! use option instead\n"
1437
      if keys %{$self->dbi_option};
1438
    return $option;
1439
}
1440

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1441
sub _push_join {
1442
    my ($self, $sql, $join, $join_tables) = @_;
1443
    
cleanup
Yuki Kimoto authored on 2011-10-21
1444
    $join = [$join] unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-10-21
1445
    
cleanup
Yuki Kimoto authored on 2011-04-02
1446
    # No join
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1447
    return unless @$join;
1448
    
cleanup
Yuki Kimoto authored on 2011-04-02
1449
    # Push join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1450
    my $tree = {};
1451
    for (my $i = 0; $i < @$join; $i++) {
1452
        
cleanup
Yuki Kimoto authored on 2011-07-28
1453
        # Arrange
added join new syntax
Yuki Kimoto authored on 2011-07-28
1454
        my $join_clause;;
1455
        my $option;
1456
        if (ref $join->[$i] eq 'HASH') {
1457
            $join_clause = $join->[$i]->{clause};
1458
            $option = {table => $join->[$i]->{table}};
1459
        }
1460
        else {
1461
            $join_clause = $join->[$i];
1462
            $option = {};
1463
        };
cleanup
Yuki Kimoto authored on 2011-07-28
1464

            
1465
        # Find tables in join clause
added join new syntax
Yuki Kimoto authored on 2011-07-28
1466
        my $table1;
1467
        my $table2;
1468
        if (my $table = $option->{table}) {
1469
            $table1 = $table->[0];
1470
            $table2 = $table->[1];
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1471
        }
cleanup
Yuki Kimoto authored on 2011-07-28
1472
        else {
1473
            my $q = $self->_quote;
1474
            my $j_clause = (split /\s+on\s+/, $join_clause)[-1];
1475
            $j_clause =~ s/'.+?'//g;
1476
            my $q_re = quotemeta($q);
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1477
            $j_clause =~ s/[$q_re]//g;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1478
            
1479
            my @j_clauses = reverse split /\s(and|on)\s/, $j_clause;
cleanup
Yuki Kimoto authored on 2011-07-28
1480
            my $c = $self->safety_character;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1481
            my $join_re = qr/($c+)\.$c+[^$c].*?($c+)\.$c+/sm;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1482
            for my $clause (@j_clauses) {
1483
                if ($clause =~ $join_re) {
1484
                    $table1 = $1;
1485
                    $table2 = $2;
1486
                    last;
1487
                }                
cleanup
Yuki Kimoto authored on 2011-07-28
1488
            }
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1489
        }
added join new syntax
Yuki Kimoto authored on 2011-07-28
1490
        croak qq{join clause must have two table name after "on" keyword. } .
1491
              qq{"$join_clause" is passed }  . _subname
1492
          unless defined $table1 && defined $table2;
1493
        croak qq{right side table of "$join_clause" must be unique }
1494
            . _subname
1495
          if exists $tree->{$table2};
1496
        croak qq{Same table "$table1" is specified} . _subname
1497
          if $table1 eq $table2;
1498
        $tree->{$table2}
1499
          = {position => $i, parent => $table1, join => $join_clause};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1500
    }
1501
    
cleanup
Yuki Kimoto authored on 2011-04-02
1502
    # Search need tables
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1503
    my $need_tables = {};
1504
    $self->_need_tables($tree, $need_tables, $join_tables);
cleanup
Yuki Kimoto authored on 2011-10-21
1505
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} }
1506
      keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1507
    
1508
    # Add join clause
cleanup
Yuki Kimoto authored on 2011-10-21
1509
    $$sql .= $tree->{$_}{join} . ' ' for @need_tables;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1510
}
cleanup
Yuki Kimoto authored on 2011-03-08
1511

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1512
sub _quote {
1513
    my $self = shift;
micro optimization
Yuki Kimoto authored on 2011-10-22
1514
    return $self->{reserved_word_quote} || $self->quote || '';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1515
}
1516

            
cleanup
Yuki Kimoto authored on 2011-07-29
1517
sub _q {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1518
    my ($self, $value, $quotemeta) = @_;
cleanup
Yuki Kimoto authored on 2011-07-29
1519
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1520
    my $quote = $self->{reserved_word_quote}
1521
      || $self->{quote} || $self->quote || '';
1522
    return "$quote$value$quote"
1523
      if !$quotemeta && ($quote eq '`' || $quote eq '"');
1524
    
cleanup
Yuki Kimoto authored on 2011-07-29
1525
    my $q = substr($quote, 0, 1) || '';
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1526
    my $p;
1527
    if (defined $quote && length $quote > 1) {
1528
        $p = substr($quote, 1, 1);
1529
    }
1530
    else { $p = $q }
cleanup
Yuki Kimoto authored on 2011-07-29
1531
    
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1532
    if ($quotemeta) {
1533
        $q = quotemeta($q);
1534
        $p = quotemeta($p);
1535
    }
1536
    
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1537
    return "$q$value$p";
cleanup
Yuki Kimoto authored on 2011-07-29
1538
}
1539

            
cleanup
Yuki Kimoto authored on 2011-04-02
1540
sub _remove_duplicate_table {
1541
    my ($self, $tables, $main_table) = @_;
1542
    
1543
    # Remove duplicate table
1544
    my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1545
    delete $tables{$main_table} if $main_table;
1546
    
micro optimization
Yuki Kimoto authored on 2011-07-30
1547
    my $new_tables = [keys %tables, $main_table ? $main_table : ()];
1548
    if (my $q = $self->_quote) {
1549
        $q = quotemeta($q);
1550
        $_ =~ s/[$q]//g for @$new_tables;
1551
    }
1552

            
1553
    return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1554
}
1555

            
cleanup
Yuki Kimoto authored on 2011-04-02
1556
sub _search_tables {
cleanup
Yuki Kimoto authored on 2011-04-02
1557
    my ($self, $source) = @_;
1558
    
cleanup
Yuki Kimoto authored on 2011-04-02
1559
    # Search tables
cleanup
Yuki Kimoto authored on 2011-04-02
1560
    my $tables = [];
1561
    my $safety_character = $self->safety_character;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1562
    my $q = $self->_quote;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1563
    my $quoted_safety_character_re = $self->_q("?([$safety_character]+)", 1);
1564
    my $table_re = $q ? qr/(?:^|[^$safety_character])${quoted_safety_character_re}?\./
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1565
                      : qr/(?:^|[^$safety_character])([$safety_character]+)\./;
cleanup
Yuki Kimoto authored on 2011-04-02
1566
    while ($source =~ /$table_re/g) {
1567
        push @$tables, $1;
1568
    }
1569
    
1570
    return $tables;
1571
}
1572

            
cleanup
Yuki Kimoto authored on 2011-10-21
1573
sub _where_clause_and_param {
cleanup
Yuki Kimoto authored on 2011-10-25
1574
    my ($self, $where, $where_param, $id, $primary_key, $table) = @_;
cleanup
Yuki Kimoto authored on 2011-10-25
1575

            
cleanup
Yuki Kimoto authored on 2011-10-21
1576
    $where ||= {};
cleanup
Yuki Kimoto authored on 2011-10-25
1577
    $where = $self->_id_to_param($id, $primary_key, $table) if defined $id;
cleanup
Yuki Kimoto authored on 2011-10-25
1578
    $where_param ||= {};
cleanup
Yuki Kimoto authored on 2011-10-21
1579
    my $w = {};
1580
    my $where_clause = '';
cleanup
Yuki Kimoto authored on 2011-10-25
1581

            
cleanup
Yuki Kimoto authored on 2011-10-25
1582
    my $obj;
1583
    
cleanup
Yuki Kimoto authored on 2011-10-21
1584
    if (ref $where eq 'ARRAY' && !ref $where->[0]) {
1585
        $w->{clause} = "where " . $where->[0];
1586
        $w->{param} = $where->[1];
1587
    }
1588
    elsif (ref $where) {
cleanup
Yuki Kimoto authored on 2011-10-25
1589

            
1590
        # Hash
1591
        if (ref $where eq 'HASH') {
1592
            my $clause = ['and'];
1593
            my $q = $self->_quote;
1594
            for my $column (keys %$where) {
1595
                my $table;
1596
                my $c;
1597
                if ($column =~ /(?:(.*?)\.)?(.*)/) {
1598
                    $table = $1;
1599
                    $c = $2;
1600
                }
1601
                
1602
                my $table_quote;
1603
                $table_quote = $self->_q($table) if defined $table;
1604
                my $column_quote = $self->_q($c);
1605
                $column_quote = $table_quote . '.' . $column_quote
1606
                  if defined $table_quote;
1607
                push @$clause, "$column_quote = :$column" for keys %$where;
1608
            }
1609
            $obj = $self->where(clause => $clause, param => $where);
1610
        }
1611
        
1612
        # DBIx::Custom::Where object
1613
        elsif (ref $where eq 'DBIx::Custom::Where') {
1614
            $obj = $where;
1615
        }
1616
        
1617
        # Array
1618
        elsif (ref $where eq 'ARRAY') {
1619
            $obj = $self->where(
1620
                clause => $where->[0],
1621
                param  => $where->[1]
1622
            );
1623
        }
1624
        
1625
        # Check where argument
1626
        croak qq{"where" must be hash reference or DBIx::Custom::Where object}
1627
            . qq{or array reference, which contains where clause and parameter}
1628
            . _subname
1629
          unless ref $obj eq 'DBIx::Custom::Where';
1630

            
1631

            
1632
        $w->{param} = keys %$where_param
1633
                    ? $self->merge_param($where_param, $obj->param)
1634
                    : $obj->param;
1635
        $w->{clause} = $obj->to_string;
cleanup
Yuki Kimoto authored on 2011-10-21
1636
    }
1637
    elsif ($where) {
1638
        $w->{clause} = "where $where";
cleanup
Yuki Kimoto authored on 2011-10-25
1639
        $w->{param} = $where_param;
cleanup
Yuki Kimoto authored on 2011-10-21
1640
    }
1641
    
1642
    return $w;
1643
}
1644

            
updated pod
Yuki Kimoto authored on 2011-06-21
1645
sub _apply_filter {
1646
    my ($self, $table, @cinfos) = @_;
1647

            
1648
    # Initialize filters
1649
    $self->{filter} ||= {};
micro optimization
Yuki Kimoto authored on 2011-07-30
1650
    $self->{filter}{on} = 1;
updated pod
Yuki Kimoto authored on 2011-06-21
1651
    $self->{filter}{out} ||= {};
1652
    $self->{filter}{in} ||= {};
1653
    $self->{filter}{end} ||= {};
1654
    
1655
    # Usage
1656
    my $usage = "Usage: \$dbi->apply_filter(" .
1657
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
1658
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
1659
    
1660
    # Apply filter
1661
    for (my $i = 0; $i < @cinfos; $i += 2) {
1662
        
1663
        # Column
1664
        my $column = $cinfos[$i];
1665
        if (ref $column eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-10-21
1666
            for my $c (@$column) {
updated pod
Yuki Kimoto authored on 2011-06-21
1667
                push @cinfos, $c, $cinfos[$i + 1];
1668
            }
1669
            next;
1670
        }
1671
        
1672
        # Filter infomation
1673
        my $finfo = $cinfos[$i + 1] || {};
1674
        croak "$usage (table: $table) " . _subname
1675
          unless  ref $finfo eq 'HASH';
cleanup
Yuki Kimoto authored on 2011-10-21
1676
        for my $ftype (keys %$finfo) {
updated pod
Yuki Kimoto authored on 2011-06-21
1677
            croak "$usage (table: $table) " . _subname
1678
              unless $ftype eq 'in' || $ftype eq 'out' || $ftype eq 'end'; 
1679
        }
1680
        
1681
        # Set filters
cleanup
Yuki Kimoto authored on 2011-10-21
1682
        for my $way (qw/in out end/) {
updated pod
Yuki Kimoto authored on 2011-06-21
1683
        
1684
            # Filter
1685
            my $filter = $finfo->{$way};
1686
            
1687
            # Filter state
1688
            my $state = !exists $finfo->{$way} ? 'not_exists'
1689
                      : !defined $filter        ? 'not_defined'
1690
                      : ref $filter eq 'CODE'   ? 'code'
1691
                      : 'name';
1692
            
1693
            # Filter is not exists
1694
            next if $state eq 'not_exists';
1695
            
1696
            # Check filter name
1697
            croak qq{Filter "$filter" is not registered } . _subname
1698
              if  $state eq 'name'
1699
               && ! exists $self->filters->{$filter};
1700
            
1701
            # Set filter
1702
            my $f = $state eq 'not_defined' ? undef
1703
                  : $state eq 'code'        ? $filter
1704
                  : $self->filters->{$filter};
1705
            $self->{filter}{$way}{$table}{$column} = $f;
1706
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
1707
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
1708
            $self->{filter}{$way}{$table}{"${table}-$column"} = $f;
1709
        }
1710
    }
1711
    
1712
    return $self;
1713
}
1714

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1715
# DEPRECATED!
1716
has 'data_source';
1717
has dbi_options => sub { {} };
1718
has filter_check  => 1;
1719
has 'reserved_word_quote';
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1720
has dbi_option => sub { {} };
1721
has default_dbi_option => sub {
1722
    warn "default_dbi_option is DEPRECATED! use default_option instead";
1723
    return shift->default_option;
1724
};
1725

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1726
# DEPRECATED!
1727
sub method {
1728
    warn "method is DEPRECATED! use helper instead";
1729
    return shift->helper(@_);
1730
}
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1731

            
1732
# DEPRECATED!
1733
sub assign_param {
1734
    my $self = shift;
1735
    warn "assing_param is DEPRECATED! use assign_clause instead";
1736
    return $self->assign_clause(@_);
1737
}
1738

            
1739
# DEPRECATED
1740
sub update_param {
1741
    my ($self, $param, $opts) = @_;
1742
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1743
    warn "update_param is DEPRECATED! use assign_clause instead.";
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1744
    
1745
    # Create update parameter tag
1746
    my $tag = $self->assign_clause($param, $opts);
1747
    $tag = "set $tag" unless $opts->{no_set};
1748

            
1749
    return $tag;
1750
}
1751

            
updated pod
Yuki Kimoto authored on 2011-06-21
1752
# DEPRECATED!
1753
sub create_query {
1754
    warn "create_query is DEPRECATED! use query option of each method";
1755
    shift->_create_query(@_);
1756
}
1757

            
cleanup
Yuki Kimoto authored on 2011-06-13
1758
# DEPRECATED!
1759
sub apply_filter {
1760
    my $self = shift;
1761
    
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1762
    warn "apply_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-06-13
1763
    return $self->_apply_filter(@_);
1764
}
1765

            
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1766
# DEPRECATED!
1767
sub select_at {
cleanup
Yuki Kimoto authored on 2011-10-21
1768
    my ($self, %opt) = @_;
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1769

            
cleanup
Yuki Kimoto authored on 2011-10-21
1770
    warn "select_at is DEPRECATED! use select method id option instead";
updated pod
Yuki Kimoto authored on 2011-06-08
1771

            
cleanup
Yuki Kimoto authored on 2011-10-21
1772
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1773
    my $primary_keys = delete $opt{primary_key};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1774
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1775
    my $where = delete $opt{where};
1776
    my $param = delete $opt{param};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1777
    
1778
    # Table
1779
    croak qq{"table" option must be specified } . _subname
cleanup
Yuki Kimoto authored on 2011-10-21
1780
      unless $opt{table};
1781
    my $table = ref $opt{table} ? $opt{table}->[-1] : $opt{table};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1782
    
1783
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1784
    my $where_param = $self->_id_to_param($where, $primary_keys);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1785
    
cleanup
Yuki Kimoto authored on 2011-10-21
1786
    return $self->select(where => $where_param, %opt);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1787
}
1788

            
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1789
# DEPRECATED!
1790
sub delete_at {
cleanup
Yuki Kimoto authored on 2011-10-21
1791
    my ($self, %opt) = @_;
updated pod
Yuki Kimoto authored on 2011-06-08
1792

            
cleanup
Yuki Kimoto authored on 2011-10-21
1793
    warn "delete_at is DEPRECATED! use delete method id option instead";
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1794
    
cleanup
Yuki Kimoto authored on 2011-10-21
1795
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1796
    my $primary_keys = delete $opt{primary_key};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1797
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1798
    my $where = delete $opt{where};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1799
    
1800
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1801
    my $where_param = $self->_id_to_param($where, $primary_keys);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1802
    
cleanup
Yuki Kimoto authored on 2011-10-21
1803
    return $self->delete(where => $where_param, %opt);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1804
}
1805

            
cleanup
Yuki Kimoto authored on 2011-06-08
1806
# DEPRECATED!
1807
sub update_at {
1808
    my $self = shift;
1809

            
cleanup
Yuki Kimoto authored on 2011-10-21
1810
    warn "update_at is DEPRECATED! use update method id option instead";
cleanup
Yuki Kimoto authored on 2011-06-08
1811
    
cleanup
Yuki Kimoto authored on 2011-10-21
1812
    # Options
cleanup
Yuki Kimoto authored on 2011-06-08
1813
    my $param;
1814
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1815
    my %opt = @_;
1816
    my $primary_keys = delete $opt{primary_key};
cleanup
Yuki Kimoto authored on 2011-06-08
1817
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1818
    my $where = delete $opt{where};
1819
    my $p = delete $opt{param} || {};
cleanup
Yuki Kimoto authored on 2011-06-08
1820
    $param  ||= $p;
1821
    
1822
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1823
    my $where_param = $self->_id_to_param($where, $primary_keys);
cleanup
Yuki Kimoto authored on 2011-06-08
1824
    
cleanup
Yuki Kimoto authored on 2011-10-21
1825
    return $self->update(where => $where_param, param => $param, %opt);
cleanup
Yuki Kimoto authored on 2011-06-08
1826
}
1827

            
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1828
# DEPRECATED!
1829
sub insert_at {
1830
    my $self = shift;
1831
    
cleanup
Yuki Kimoto authored on 2011-10-21
1832
    warn "insert_at is DEPRECATED! use insert method id option instead";
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1833
    
cleanup
Yuki Kimoto authored on 2011-10-21
1834
    # Options
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1835
    my $param;
1836
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1837
    my %opt = @_;
1838
    my $primary_key = delete $opt{primary_key};
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1839
    $primary_key = [$primary_key] unless ref $primary_key;
cleanup
Yuki Kimoto authored on 2011-10-21
1840
    my $where = delete $opt{where};
1841
    my $p = delete $opt{param} || {};
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1842
    $param  ||= $p;
1843
    
1844
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1845
    my $where_param = $self->_id_to_param($where, $primary_key);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1846
    $param = $self->merge_param($where_param, $param);
1847
    
cleanup
Yuki Kimoto authored on 2011-10-21
1848
    return $self->insert(param => $param, %opt);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1849
}
1850

            
added warnings
Yuki Kimoto authored on 2011-06-07
1851
# DEPRECATED!
1852
sub register_tag {
test cleanup
Yuki Kimoto authored on 2011-08-10
1853
    my $self = shift;
1854
    
added warnings
Yuki Kimoto authored on 2011-06-07
1855
    warn "register_tag is DEPRECATED!";
test cleanup
Yuki Kimoto authored on 2011-08-10
1856
    
1857
    # Merge tag
1858
    my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1859
    $self->{_tags} = {%{$self->{_tags} || {}}, %$tags};
1860
    
1861
    return $self;
1862
}
1863

            
1864
# DEPRECATED!
1865
sub register_tag_processor {
1866
    my $self = shift;
1867
    warn "register_tag_processor is DEPRECATED!";
1868
    # Merge tag
1869
    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1870
    $self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}};
1871
    return $self;
added warnings
Yuki Kimoto authored on 2011-06-07
1872
}
1873

            
cleanup
Yuki Kimoto authored on 2011-01-25
1874
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1875
sub default_bind_filter {
1876
    my $self = shift;
1877
    
cleanup
Yuki Kimoto authored on 2011-06-13
1878
    warn "default_bind_filter is DEPRECATED!";
added warnings
Yuki Kimoto authored on 2011-06-07
1879
    
cleanup
Yuki Kimoto authored on 2011-01-12
1880
    if (@_) {
1881
        my $fname = $_[0];
1882
        
1883
        if (@_ && !$fname) {
1884
            $self->{default_out_filter} = undef;
1885
        }
1886
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1887
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1888
              unless exists $self->filters->{$fname};
1889
        
1890
            $self->{default_out_filter} = $self->filters->{$fname};
1891
        }
1892
        return $self;
1893
    }
1894
    
1895
    return $self->{default_out_filter};
1896
}
1897

            
cleanup
Yuki Kimoto authored on 2011-01-25
1898
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1899
sub default_fetch_filter {
1900
    my $self = shift;
added warnings
Yuki Kimoto authored on 2011-06-07
1901

            
cleanup
Yuki Kimoto authored on 2011-06-13
1902
    warn "default_fetch_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-01-12
1903
    
1904
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1905
        my $fname = $_[0];
1906

            
cleanup
Yuki Kimoto authored on 2011-01-12
1907
        if (@_ && !$fname) {
1908
            $self->{default_in_filter} = undef;
1909
        }
1910
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1911
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1912
              unless exists $self->filters->{$fname};
1913
        
1914
            $self->{default_in_filter} = $self->filters->{$fname};
1915
        }
1916
        
1917
        return $self;
1918
    }
1919
    
many changed
Yuki Kimoto authored on 2011-01-23
1920
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1921
}
1922

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1923
# DEPRECATED!
1924
sub insert_param {
1925
    my $self = shift;
1926
    warn "insert_param is DEPRECATED! use values_clause instead";
1927
    return $self->values_clause(@_);
1928
}
1929

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1930
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1931
sub insert_param_tag {
1932
    warn "insert_param_tag is DEPRECATED! " .
1933
         "use insert_param instead!";
1934
    return shift->insert_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1935
}
1936

            
1937
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1938
sub update_param_tag {
fixed DEPRECATED message bug
Yuki Kimoto authored on 2011-06-10
1939
    warn "update_param_tag is DEPRECATED! " .
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1940
         "use update_param instead";
1941
    return shift->update_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1942
}
cleanup
Yuki Kimoto authored on 2011-03-08
1943
# DEPRECATED!
1944
sub _push_relation {
1945
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1946
    
1947
    if (keys %{$relation || {}}) {
micro optimization
Yuki Kimoto authored on 2011-09-30
1948
        $$sql .= $need_where ? 'where ' : 'and ';
cleanup
Yuki Kimoto authored on 2011-10-21
1949
        for my $rcolumn (keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-03-08
1950
            my $table1 = (split (/\./, $rcolumn))[0];
1951
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1952
            push @$tables, ($table1, $table2);
micro optimization
Yuki Kimoto authored on 2011-09-30
1953
            $$sql .= "$rcolumn = " . $relation->{$rcolumn} .  'and ';
cleanup
Yuki Kimoto authored on 2011-03-08
1954
        }
1955
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
1956
    $$sql =~ s/and $/ /;
cleanup
Yuki Kimoto authored on 2011-03-08
1957
}
1958

            
1959
# DEPRECATED!
1960
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1961
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1962
    
1963
    if (keys %{$relation || {}}) {
cleanup
Yuki Kimoto authored on 2011-10-21
1964
        for my $rcolumn (keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-03-08
1965
            my $table1 = (split (/\./, $rcolumn))[0];
1966
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1967
            my $table1_exists;
1968
            my $table2_exists;
cleanup
Yuki Kimoto authored on 2011-10-21
1969
            for my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-03-08
1970
                $table1_exists = 1 if $table eq $table1;
1971
                $table2_exists = 1 if $table eq $table2;
1972
            }
1973
            unshift @$tables, $table1 unless $table1_exists;
1974
            unshift @$tables, $table2 unless $table2_exists;
1975
        }
1976
    }
1977
}
1978

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1981
=head1 NAME
1982

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
1983
DBIx::Custom - DBI extension to execute insert, update, delete, and select easily
removed reconnect method
yuki-kimoto authored on 2010-05-28
1984

            
fix heading typos
Terrence Brannon authored on 2011-08-17
1985
=head1 SYNOPSIS
cleanup
yuki-kimoto authored on 2010-08-05
1986

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1987
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1988
    
1989
    # Connect
1990
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1991
        dsn => "dbi:mysql:database=dbname",
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1992
        user => 'ken',
1993
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1994
        option => {mysql_enable_utf8 => 1}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1995
    );
cleanup
yuki-kimoto authored on 2010-08-05
1996

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1997
    # Insert 
updated pod
Yuki Kimoto authored on 2011-06-21
1998
    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
removed reconnect method
yuki-kimoto authored on 2010-05-28
1999
    
2000
    # Update 
updated pod
Yuki Kimoto authored on 2011-06-21
2001
    $dbi->update({title => 'Perl', author => 'Ken'}, table  => 'book',
2002
      where  => {id => 5});
removed reconnect method
yuki-kimoto authored on 2010-05-28
2003
    
2004
    # Delete
updated pod
Yuki Kimoto authored on 2011-06-21
2005
    $dbi->delete(table  => 'book', where => {author => 'Ken'});
cleanup
yuki-kimoto authored on 2010-08-05
2006

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2007
    # Select
updated pod
Yuki Kimoto authored on 2011-06-21
2008
    my $result = $dbi->select(table  => 'book',
2009
      column => ['title', 'author'], where  => {author => 'Ken'});
cleanup
yuki-kimoto authored on 2010-08-05
2010

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2011
    # Select, more complex
2012
    my $result = $dbi->select(
2013
        table  => 'book',
2014
        column => [
cleanup
Yuki Kimoto authored on 2011-06-13
2015
            {book => [qw/title author/]},
2016
            {company => ['name']}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2017
        ],
2018
        where  => {'book.author' => 'Ken'},
2019
        join => ['left outer join company on book.company_id = company.id'],
2020
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
2021
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2022
    
removed register_format()
yuki-kimoto authored on 2010-05-26
2023
    # Fetch
2024
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2025
        
removed register_format()
yuki-kimoto authored on 2010-05-26
2026
    }
2027
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2028
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
2029
    while (my $row = $result->fetch_hash) {
2030
        
2031
    }
2032
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2033
    # Execute SQL with parameter.
2034
    $dbi->execute(
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
2035
        "select id from book where author = :author and title like :title",
updated pod
Yuki Kimoto authored on 2011-06-21
2036
        {author => 'ken', title => '%Perl%'}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2037
    );
2038
    
fix heading typos
Terrence Brannon authored on 2011-08-17
2039
=head1 DESCRIPTION
removed reconnect method
yuki-kimoto authored on 2010-05-28
2040

            
cleanup
Yuki Kimoto authored on 2011-07-29
2041
L<DBIx::Custom> is L<DBI> wrapper module to execute SQL easily.
updated pod
Yuki Kimoto authored on 2011-06-21
2042
This module have the following features.
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2043

            
updated pod
Yuki Kimoto authored on 2011-06-21
2044
=over 4
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2045

            
cleanup
Yuki Kimoto authored on 2011-07-29
2046
=item *
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2047

            
cleanup
Yuki Kimoto authored on 2011-07-29
2048
Execute C<insert>, C<update>, C<delete>, or C<select> statement easily
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2049

            
cleanup
Yuki Kimoto authored on 2011-07-29
2050
=item *
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2051

            
cleanup
Yuki Kimoto authored on 2011-07-29
2052
Create C<where> clause flexibly
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2053

            
cleanup
Yuki Kimoto authored on 2011-07-29
2054
=item *
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2055

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2056
Named place holder support
2057

            
2058
=item *
2059

            
cleanup
Yuki Kimoto authored on 2011-07-29
2060
Model support
2061

            
2062
=item *
2063

            
2064
Connection manager support
2065

            
2066
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
2067

            
cleanup
Yuki Kimoto authored on 2011-07-30
2068
Choice your favorite relational database management system,
cleanup
Yuki Kimoto authored on 2011-07-29
2069
C<MySQL>, C<SQLite>, C<PostgreSQL>, C<Oracle>,
2070
C<Microsoft SQL Server>, C<Microsoft Access>, C<DB2> or anything, 
2071

            
2072
=item *
2073

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2074
Filtering by data type or column name
cleanup
Yuki Kimoto authored on 2011-07-29
2075

            
2076
=item *
2077

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2078
Create C<order by> clause flexibly
cleanup
Yuki Kimoto authored on 2011-07-29
2079

            
2080
=back
pod fix
Yuki Kimoto authored on 2011-01-21
2081

            
fix heading typos
Terrence Brannon authored on 2011-08-17
2082
=head1 DOCUMENTATION
pod fix
Yuki Kimoto authored on 2011-01-21
2083

            
cleanup
Yuki Kimoto authored on 2011-07-29
2084
L<DBIx::Custom::Guide> - How to use L<DBIx::Custom>
pod fix
Yuki Kimoto authored on 2011-01-21
2085

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2086
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki>
cleanup
Yuki Kimoto authored on 2011-07-29
2087
- Theare are various examples.
2088

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2089
Module documentations - 
cleanup
Yuki Kimoto authored on 2011-07-29
2090
L<DBIx::Custom::Result>,
2091
L<DBIx::Custom::Query>,
2092
L<DBIx::Custom::Where>,
2093
L<DBIx::Custom::Model>,
2094
L<DBIx::Custom::Order>
updated document
yuki-kimoto authored on 2010-08-08
2095

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

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2098
=head2 C<connector>
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2099

            
2100
    my $connector = $dbi->connector;
micro optimization
Yuki Kimoto authored on 2011-07-30
2101
    $dbi = $dbi->connector($connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2102

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2103
Connection manager object. if C<connector> is set, you can get C<dbh>
2104
through connection manager. Conection manager object must have C<dbh> mehtod.
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2105

            
2106
This is L<DBIx::Connector> example. Please pass
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2107
C<default_option> to L<DBIx::Connector> C<new> method.
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2108

            
2109
    my $connector = DBIx::Connector->new(
cleanup
Yuki Kimoto authored on 2011-08-16
2110
        "dbi:mysql:database=$database",
2111
        $user,
2112
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2113
        DBIx::Custom->new->default_option
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2114
    );
2115
    
updated pod
Yuki Kimoto authored on 2011-06-21
2116
    my $dbi = DBIx::Custom->connect(connector => $connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2117

            
cleanup
Yuki Kimoto authored on 2011-08-16
2118
If C<connector> is set to 1 when connect method is called,
2119
L<DBIx::Connector> is automatically set to C<connector>
2120

            
2121
    my $dbi = DBIx::Custom->connect(
2122
      dsn => $dsn, user => $user, password => $password, connector => 1);
2123
    
2124
    my $connector = $dbi->connector; # DBIx::Connector
2125

            
2126
Note that L<DBIx::Connector> must be installed.
2127

            
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2128
=head2 C<dsn>
2129

            
2130
    my $dsn = $dbi->dsn;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2131
    $dbi = $dbi->dsn("DBI:mysql:database=dbname");
packaging one directory
yuki-kimoto authored on 2009-11-16
2132

            
updated pod
Yuki Kimoto authored on 2011-06-21
2133
Data source name, used when C<connect> method is executed.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
2134

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2135
=head2 C<default_option>
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2136

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2137
    my $default_option = $dbi->default_option;
2138
    $dbi = $dbi->default_option($default_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2139

            
updated pod
Yuki Kimoto authored on 2011-06-21
2140
L<DBI> default option, used when C<connect> method is executed,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2141
default to the following values.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2142

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2143
    {
2144
        RaiseError => 1,
2145
        PrintError => 0,
2146
        AutoCommit => 1,
2147
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
2148

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2149
=head2 C<exclude_table>
2150

            
2151
    my $exclude_table = $dbi->exclude_table;
2152
    $dbi = $dbi->exclude_table(qr/pg_/);
2153

            
2154
Excluded table regex.
2155
C<each_column>, C<each_table>, C<type_rule>,
2156
and C<setup_model> methods ignore matching tables.
2157

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2160
    my $filters = $dbi->filters;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2161
    $dbi = $dbi->filters(\%filters);
packaging one directory
yuki-kimoto authored on 2009-11-16
2162

            
updated pod
Yuki Kimoto authored on 2011-06-21
2163
Filters, registered by C<register_filter> method.
add models() attribute
Yuki Kimoto authored on 2011-02-21
2164

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
2165
=head2 C<last_sql>
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
2166

            
2167
    my $last_sql = $dbi->last_sql;
2168
    $dbi = $dbi->last_sql($last_sql);
2169

            
2170
Get last successed SQL executed by C<execute> method.
2171

            
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2172
=head2 C<now EXPERIMENTAL>
2173

            
2174
    my $now = $dbi->now;
2175
    $dbi = $dbi->now($now);
2176

            
2177
Code reference which return time now, default to the following code reference.
2178

            
2179
    sub {
2180
        my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
2181
        $mon++;
2182
        $year += 1900;
2183
        return sprintf("%04d-%02d-%02d %02d:%02d:%02d");
2184
    }
2185

            
2186
This return the time like "2011-10-14 05:05:27".
2187

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2188
=head2 C<models>
add models() attribute
Yuki Kimoto authored on 2011-02-21
2189

            
2190
    my $models = $dbi->models;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2191
    $dbi = $dbi->models(\%models);
add models() attribute
Yuki Kimoto authored on 2011-02-21
2192

            
updated pod
Yuki Kimoto authored on 2011-06-21
2193
Models, included by C<include_model> method.
add models() attribute
Yuki Kimoto authored on 2011-02-21
2194

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2195
=head2 C<option>
2196

            
2197
    my $option = $dbi->option;
2198
    $dbi = $dbi->option($option);
2199

            
2200
L<DBI> option, used when C<connect> method is executed.
2201
Each value in option override the value of C<default_option>.
2202

            
cleanup
yuki-kimoto authored on 2010-10-17
2203
=head2 C<password>
2204

            
2205
    my $password = $dbi->password;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2206
    $dbi = $dbi->password('lkj&le`@s');
cleanup
yuki-kimoto authored on 2010-10-17
2207

            
updated pod
Yuki Kimoto authored on 2011-06-21
2208
Password, used when C<connect> method is executed.
update document
yuki-kimoto authored on 2010-01-30
2209

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

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2212
    my $builder = $dbi->query_builder;
added commit method
yuki-kimoto authored on 2010-05-27
2213

            
test cleanup
Yuki Kimoto authored on 2011-08-10
2214
Creat query builder. This is L<DBIx::Custom::QueryBuilder>.
cleanup
yuki-kimoto authored on 2010-08-05
2215

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2216
=head2 C<quote>
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
2217

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2218
     my quote = $dbi->quote;
2219
     $dbi = $dbi->quote('"');
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
2220

            
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
2221
Reserved word quote.
2222
Default to double quote '"' except for mysql.
2223
In mysql, default to back quote '`'
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
2224

            
cleanup
Yuki Kimoto authored on 2011-07-30
2225
You can set quote pair.
2226

            
2227
    $dbi->quote('[]');
2228

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2231
    my $result_class = $dbi->result_class;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2232
    $dbi = $dbi->result_class('DBIx::Custom::Result');
cleanup
yuki-kimoto authored on 2010-08-05
2233

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

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

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2238
    my $safety_character = $dbi->safety_character;
2239
    $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2240

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

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2244
=head2 C<separator>
2245

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2246
    my $separator = $dbi->separator;
2247
    $dbi = $dbi->separator('-');
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
2248

            
2249
Separator which join table name and column name.
2250
This have effect to C<column> and C<mycolumn> method,
2251
and C<select> method's column option.
cleanup test
Yuki Kimoto authored on 2011-08-10
2252

            
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
2253
Default to C<.>.
cleanup test
Yuki Kimoto authored on 2011-08-10
2254

            
added tag_parse attribute
Yuki Kimoto authored on 2011-06-28
2255
=head2 C<tag_parse>
2256

            
2257
    my $tag_parse = $dbi->tag_parse(0);
2258
    $dbi = $dbi->tag_parse;
2259

            
2260
Enable DEPRECATED tag parsing functionality, default to 1.
2261
If you want to disable tag parsing functionality, set to 0.
2262

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2265
    my $user = $dbi->user;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
2266
    $dbi = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
2267

            
updated pod
Yuki Kimoto authored on 2011-06-21
2268
User name, used when C<connect> method is executed.
update pod
Yuki Kimoto authored on 2011-01-27
2269

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2270
=head2 C<user_column_info>
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2271

            
2272
    my $user_column_info = $dbi->user_column_info;
2273
    $dbi = $dbi->user_column_info($user_column_info);
2274

            
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
2275
You can set the date like the following one.
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2276

            
2277
    [
2278
        {table => 'book', column => 'title', info => {...}},
2279
        {table => 'author', column => 'name', info => {...}}
2280
    ]
2281

            
improved join clause parsing
Yuki Kimoto authored on 2011-09-30
2282
Usually, you set return value of C<get_column_info>.
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2283

            
2284
    my $user_column_info
2285
      = $dbi->get_column_info(exclude_table => qr/^system/);
2286
    $dbi->user_column_info($user_column_info);
2287

            
2288
If C<user_column_info> is set, C<each_column> use C<user_column_info>
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2289
to find column info. this is very fast.
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2290

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2291
=head2 C<user_table_info>
added test
Yuki Kimoto authored on 2011-08-16
2292

            
2293
    my $user_table_info = $dbi->user_table_info;
2294
    $dbi = $dbi->user_table_info($user_table_info);
2295

            
2296
You can set the following data.
2297

            
2298
    [
2299
        {table => 'book', info => {...}},
2300
        {table => 'author', info => {...}}
2301
    ]
2302

            
2303
Usually, you can set return value of C<get_table_info>.
2304

            
2305
    my $user_table_info = $dbi->get_table_info(exclude => qr/^system/);
2306
    $dbi->user_table_info($user_table_info);
2307

            
2308
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2309
to find table info.
2310

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

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

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2317
=head2 C<available_datatype>
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
2318

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
2319
    print $dbi->available_datatype;
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
2320

            
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2321
Get available data types. You can use these data types
updated pod
Yuki Kimoto authored on 2011-06-21
2322
in C<type rule>'s C<from1> and C<from2> section.
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2323

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2324
=head2 C<available_typename>
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2325

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-08-09
2326
    print $dbi->available_typename;
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2327

            
2328
Get available type names. You can use these type names in
updated pod
Yuki Kimoto authored on 2011-06-21
2329
C<type_rule>'s C<into1> and C<into2> section.
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
2330

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2331
=head2 C<assign_clause>
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2332

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2333
    my $assign_clause = $dbi->assign_clause({title => 'a', age => 2});
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2334

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2335
Create assign clause
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2336

            
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
2337
    title = :title, author = :author
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2338

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2339
This is used to create update clause.
2340

            
2341
    "update book set " . $dbi->assign_clause({title => 'a', age => 2});
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2342

            
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2343
=head2 C<column>
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
2344

            
cleanup
Yuki Kimoto authored on 2011-06-13
2345
    my $column = $dbi->column(book => ['author', 'title']);
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
2346

            
2347
Create column clause. The follwoing column clause is created.
2348

            
2349
    book.author as "book.author",
2350
    book.title as "book.title"
2351

            
cleanup test
Yuki Kimoto authored on 2011-08-10
2352
You can change separator by C<separator> attribute.
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
2353

            
cleanup
Yuki Kimoto authored on 2011-06-13
2354
    # Separator is hyphen
2355
    $dbi->separator('-');
2356
    
2357
    book.author as "book-author",
2358
    book.title as "book-title"
2359
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2360
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
2361

            
update pod
Yuki Kimoto authored on 2011-03-13
2362
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2363
        dsn => "dbi:mysql:database=dbname",
update pod
Yuki Kimoto authored on 2011-03-13
2364
        user => 'ken',
2365
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2366
        option => {mysql_enable_utf8 => 1}
update pod
Yuki Kimoto authored on 2011-03-13
2367
    );
2368

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

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

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2375
=head2 C<count>
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
2376

            
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
2377
    my $count = $dbi->count(table => 'book');
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
2378

            
2379
Get rows count.
2380

            
2381
Options is same as C<select> method's ones.
2382

            
2383
=head2 C<create_model>
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2384

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
2385
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2386
        table => 'book',
2387
        primary_key => 'id',
2388
        join => [
2389
            'inner join company on book.comparny_id = company.id'
2390
        ],
2391
    );
2392

            
2393
Create L<DBIx::Custom::Model> object and initialize model.
updated pod
Yuki Kimoto authored on 2011-06-21
2394
the module is also used from C<model> method.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2395

            
2396
   $dbi->model('book')->select(...);
2397

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

            
2400
    my $dbh = $dbi->dbh;
2401

            
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2402
Get L<DBI> database handle. if C<connector> is set, you can get
updated pod
Yuki Kimoto authored on 2011-06-21
2403
database handle through C<connector> object.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2404

            
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2405
=head2 C<delete>
2406

            
2407
    $dbi->delete(table => 'book', where => {title => 'Perl'});
2408

            
2409
Execute delete statement.
2410

            
2411
The following opitons are available.
2412

            
cleanup
Yuki Kimoto authored on 2011-10-20
2413
B<OPTIONS>
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2414

            
cleanup
Yuki Kimoto authored on 2011-10-20
2415
C<delete> method use all of C<execute> method's options,
2416
and use the following new ones.
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2417

            
cleanup
Yuki Kimoto authored on 2011-10-20
2418
=over 4
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2419

            
2420
=item C<id>
2421

            
2422
    id => 4
2423
    id => [4, 5]
2424

            
2425
ID corresponding to C<primary_key>.
2426
You can delete rows by C<id> and C<primary_key>.
2427

            
2428
    $dbi->delete(
fixed pod
Yuki Kimoto authored on 2011-10-20
2429
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2430
        id => [4, 5],
2431
        table => 'book',
2432
    );
2433

            
2434
The above is same as the followin one.
2435

            
2436
    $dbi->delete(where => {id1 => 4, id2 => 5}, table => 'book');
2437

            
2438
=item C<prefix>
2439

            
2440
    prefix => 'some'
2441

            
2442
prefix before table name section.
2443

            
2444
    delete some from book
2445

            
2446
=item C<table>
2447

            
2448
    table => 'book'
2449

            
2450
Table name.
2451

            
2452
=item C<where>
2453

            
2454
Same as C<select> method's C<where> option.
2455

            
2456
=back
2457

            
2458
=head2 C<delete_all>
2459

            
2460
    $dbi->delete_all(table => $table);
2461

            
2462
Execute delete statement for all rows.
2463
Options is same as C<delete>.
2464

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2465
=head2 C<each_column>
2466

            
2467
    $dbi->each_column(
2468
        sub {
2469
            my ($dbi, $table, $column, $column_info) = @_;
2470
            
2471
            my $type = $column_info->{TYPE_NAME};
2472
            
2473
            if ($type eq 'DATE') {
2474
                # ...
2475
            }
2476
        }
2477
    );
2478

            
improved pod
Yuki Kimoto authored on 2011-10-14
2479
Iterate all column informations in database.
2480
Argument is callback which is executed when one column is found.
2481
Callback receive four arguments. C<DBIx::Custom object>, C<table name>,
2482
C<column name>, and C<column information>.
2483

            
2484
If C<user_column_info> is set, C<each_column> method use C<user_column_info>
2485
infromation, you can improve the performance of C<each_column> in
2486
the following way.
2487

            
2488
    my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/);
2489
    $dbi->user_column_info($column_info);
2490
    $dbi->each_column(sub { ... });
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
2491

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
2492
=head2 C<each_table>
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
2493

            
2494
    $dbi->each_table(
2495
        sub {
2496
            my ($dbi, $table, $table_info) = @_;
2497
            
2498
            my $table_name = $table_info->{TABLE_NAME};
2499
        }
2500
    );
2501

            
improved pod
Yuki Kimoto authored on 2011-10-14
2502
Iterate all table informationsfrom in database.
2503
Argument is callback which is executed when one table is found.
2504
Callback receive three arguments, C<DBIx::Custom object>, C<table name>,
2505
C<table information>.
2506

            
2507
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2508
infromation, you can improve the performance of C<each_table> in
2509
the following way.
2510

            
2511
    my $table_infos = $dbi->get_table_info(exclude => qr/^system_/);
2512
    $dbi->user_table_info($table_info);
2513
    $dbi->each_table(sub { ... });
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
2514

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2517
    my $result = $dbi->execute(
updated pod
Yuki Kimoto authored on 2011-06-21
2518
      "select * from book where title = :title and author like :author",
2519
      {title => 'Perl', author => '%Ken%'}
2520
    );
2521

            
2522
    my $result = $dbi->execute(
2523
      "select * from book where title = :book.title and author like :book.author",
2524
      {'book.title' => 'Perl', 'book.author' => '%Ken%'}
update pod
Yuki Kimoto authored on 2011-03-13
2525
    );
2526

            
updated pod
Yuki Kimoto authored on 2011-06-21
2527
Execute SQL. SQL can contain column parameter such as :author and :title.
2528
You can append table name to column name such as :book.title and :book.author.
2529
Second argunet is data, embedded into column parameter.
2530
Return value is L<DBIx::Custom::Result> object when select statement is executed,
2531
or the count of affected rows when insert, update, delete statement is executed.
update pod
Yuki Kimoto authored on 2011-03-13
2532

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2533
Named placeholder such as C<:title> is replaced by placeholder C<?>.
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2534
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2535
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2536
    select * from book where title = :title and author like :author
2537
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2538
    # Replaced
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2539
    select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2540

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2541
You can specify operator with named placeholder
fixed pod
Yuki Kimoto authored on 2011-10-20
2542
by C<name{operator}> syntax.
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2543

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2544
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2545
    select * from book where :title{=} and :author{like}
2546
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2547
    # Replaced
update pod
Yuki Kimoto authored on 2011-03-13
2548
    select * from where title = ? and author like ?;
2549

            
fixed named placeholder bug ...
Yuki Kimoto authored on 2011-08-01
2550
Note that colons in time format such as 12:13:15 is exeption,
2551
it is not parsed as named placeholder.
2552
If you want to use colon generally, you must escape it by C<\\>
2553

            
2554
    select * from where title = "aa\\:bb";
2555

            
cleanup
Yuki Kimoto authored on 2011-10-20
2556
B<OPTIONS>
2557

            
updated pod
Yuki Kimoto authored on 2011-06-09
2558
The following opitons are available.
update pod
Yuki Kimoto authored on 2011-03-13
2559

            
2560
=over 4
2561

            
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2562
=item C<after_build_sql> 
2563

            
2564
You can filter sql after the sql is build.
2565

            
2566
    after_build_sql => $code_ref
2567

            
2568
The following one is one example.
2569

            
2570
    $dbi->select(
2571
        table => 'book',
2572
        column => 'distinct(name)',
2573
        after_build_sql => sub {
2574
            "select count(*) from ($_[0]) as t1"
2575
        }
2576
    );
2577

            
2578
The following SQL is executed.
2579

            
2580
    select count(*) from (select distinct(name) from book) as t1;
2581

            
cleanup
Yuki Kimoto authored on 2011-10-20
2582
=item C<append>
2583

            
2584
    append => 'order by name'
2585

            
2586
Append some statement after SQL.
2587

            
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2588
=item C<bind_type>
2589

            
2590
Specify database bind data type.
2591

            
2592
    bind_type => [image => DBI::SQL_BLOB]
2593
    bind_type => [[qw/image audio/] => DBI::SQL_BLOB]
2594

            
2595
This is used to bind parameter by C<bind_param> of statment handle.
2596

            
2597
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2598

            
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2599
=item C<created_at EXPERIMETNAL>
2600

            
2601
    created_at => 'created_datetime'
2602

            
2603
Created timestamp column name. time when row is created is set to the column.
2604
default time format is "YYYY-mm-dd HH:MM:SS", which can be changed by
2605
C<now> attribute.
2606

            
update pod
Yuki Kimoto authored on 2011-03-13
2607
=item C<filter>
updated pod
Yuki Kimoto authored on 2011-06-09
2608
    
2609
    filter => {
2610
        title  => sub { uc $_[0] }
2611
        author => sub { uc $_[0] }
2612
    }
update pod
Yuki Kimoto authored on 2011-03-13
2613

            
updated pod
Yuki Kimoto authored on 2011-06-09
2614
    # Filter name
2615
    filter => {
2616
        title  => 'upper_case',
2617
        author => 'upper_case'
2618
    }
2619
        
2620
    # At once
2621
    filter => [
2622
        [qw/title author/]  => sub { uc $_[0] }
2623
    ]
2624

            
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
2625
Filter. You can set subroutine or filter name
updated pod
Yuki Kimoto authored on 2011-06-21
2626
registered by by C<register_filter>.
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
2627
This filter is executed before data is saved into database.
2628
and before type rule filter is executed.
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2629

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2630
=item C<id>
2631

            
2632
    id => 4
2633
    id => [4, 5]
2634

            
2635
ID corresponding to C<primary_key>.
2636
You can delete rows by C<id> and C<primary_key>.
2637

            
2638
    $dbi->execute(
2639
        "select * from book where id1 = :id1 and id2 = :id2",
2640
        {},
fixed pod
Yuki Kimoto authored on 2011-10-20
2641
        primary_key => ['id1', 'id2'],
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2642
        id => [4, 5],
2643
    );
2644

            
2645
The above is same as the followin one.
2646

            
2647
    $dbi->execute(
2648
        "select * from book where id1 = :id1 and id2 = :id2",
2649
        {id1 => 4, id2 => 5}
2650
    );
2651

            
updated document
Yuki Kimoto authored on 2011-06-09
2652
=item C<query>
2653

            
2654
    query => 1
2655

            
2656
C<execute> method return L<DBIx::Custom::Query> object, not executing SQL.
micro optimization and
Yuki Kimoto authored on 2011-10-25
2657
You can check SQL, column, or get statment handle.
updated pod
Yuki Kimoto authored on 2011-06-21
2658

            
2659
    my $sql = $query->sql;
cleanup
Yuki Kimoto authored on 2011-07-30
2660
    my $sth = $query->sth;
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2661
    my $columns = $query->columns;
2662
    
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2663
=item C<primary_key>
2664

            
cleanup
Yuki Kimoto authored on 2011-10-20
2665
    primary_key => 'id'
2666
    primary_key => ['id1', 'id2']
2667

            
2668
Priamry key. This is used when C<id> option find primary key.
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2669

            
updated pod
Yuki Kimoto authored on 2011-06-09
2670
=item C<table>
2671
    
2672
    table => 'author'
2673

            
updated pod
Yuki Kimoto authored on 2011-06-21
2674
If you want to omit table name in column name
2675
and enable C<into1> and C<into2> type filter,
2676
You must set C<table> option.
updated pod
Yuki Kimoto authored on 2011-06-09
2677

            
updated pod
Yuki Kimoto authored on 2011-06-21
2678
    $dbi->execute("select * from book where title = :title and author = :author",
2679
        {title => 'Perl', author => 'Ken', table => 'book');
updated pod
Yuki Kimoto authored on 2011-06-09
2680

            
updated pod
Yuki Kimoto authored on 2011-06-21
2681
    # Same
2682
    $dbi->execute(
2683
      "select * from book where title = :book.title and author = :book.author",
2684
      {title => 'Perl', author => 'Ken');
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2685

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2686
=item C<table_alias>
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-06-27
2687

            
cleanup
Yuki Kimoto authored on 2011-10-20
2688
    table_alias => {user => 'worker'}
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-06-27
2689

            
2690
Table alias. Key is real table name, value is alias table name.
2691
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2692
on alias table name.
2693

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2694
=item C<reuse EXPERIMENTAL>
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2695
    
micro optimization
Yuki Kimoto authored on 2011-10-23
2696
    reuse => $has_ref
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2697

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2698
Reuse query object if the hash reference variable is set.
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2699
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2700
    my $queries = {};
2701
    $dbi->execute($sql, $param, reuse => $queries);
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2702

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2703
This will improved performance when you want to execute same query repeatedly
2704
because generally creating query object is slow.
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2705

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2706
=item C<type_rule_off>
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2707

            
2708
    type_rule_off => 1
2709

            
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
2710
Turn C<into1> and C<into2> type rule off.
2711

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2712
=item C<type_rule1_off>
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
2713

            
2714
    type_rule1_off => 1
2715

            
2716
Turn C<into1> type rule off.
2717

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2718
=item C<type_rule2_off>
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
2719

            
2720
    type_rule2_off => 1
2721

            
2722
Turn C<into2> type rule off.
update document
yuki-kimoto authored on 2009-11-19
2723

            
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2724
=item C<updated_at EXPERIMETNAL>
2725

            
2726
    updated_at => 'updated_datetime'
2727

            
2728
Updated timestamp column name. time when row is updated is set to the column.
2729
default time format is C<YYYY-mm-dd HH:MM:SS>, which can be changed by
2730
C<now> attribute.
2731

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

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2734
=head2 C<get_column_info>
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2735

            
improved pod
Yuki Kimoto authored on 2011-10-14
2736
    my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/);
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2737

            
2738
get column infomation except for one which match C<exclude_table> pattern.
2739

            
2740
    [
2741
        {table => 'book', column => 'title', info => {...}},
2742
        {table => 'author', column => 'name' info => {...}}
2743
    ]
2744

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2745
=head2 C<get_table_info>
added test
Yuki Kimoto authored on 2011-08-16
2746

            
improved pod
Yuki Kimoto authored on 2011-10-14
2747
    my $table_infos = $dbi->get_table_info(exclude => qr/^system_/);
update pod
Yuki Kimoto authored on 2011-03-13
2748

            
added test
Yuki Kimoto authored on 2011-08-16
2749
get table infomation except for one which match C<exclude> pattern.
2750

            
2751
    [
2752
        {table => 'book', info => {...}},
2753
        {table => 'author', info => {...}}
2754
    ]
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2755

            
added test
Yuki Kimoto authored on 2011-08-16
2756
You can set this value to C<user_table_info>.
update pod
Yuki Kimoto authored on 2011-03-13
2757

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2758
=head2 C<helper>
2759

            
2760
    $dbi->helper(
2761
        update_or_insert => sub {
2762
            my $self = shift;
2763
            
2764
            # Process
2765
        },
2766
        find_or_create   => sub {
2767
            my $self = shift;
2768
            
2769
            # Process
2770
        }
2771
    );
2772

            
2773
Register helper. These helper is called directly from L<DBIx::Custom> object.
2774

            
2775
    $dbi->update_or_insert;
2776
    $dbi->find_or_create;
2777

            
cleanup
yuki-kimoto authored on 2010-10-17
2778
=head2 C<insert>
2779

            
cleanup
Yuki Kimoto authored on 2011-06-09
2780
    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
update pod
Yuki Kimoto authored on 2011-03-13
2781

            
updated pod
Yuki Kimoto authored on 2011-06-21
2782
Execute insert statement. First argument is row data. Return value is
2783
affected row count.
update pod
Yuki Kimoto authored on 2011-03-13
2784

            
insert and update method's p...
Yuki Kimoto authored on 2011-07-29
2785
If you want to set constant value to row data, use scalar reference
2786
as parameter value.
2787

            
2788
    {date => \"NOW()"}
2789

            
cleanup
Yuki Kimoto authored on 2011-10-20
2790
B<options>
2791

            
cleanup
Yuki Kimoto authored on 2011-10-20
2792
C<insert> method use all of C<execute> method's options,
cleanup
Yuki Kimoto authored on 2011-10-20
2793
and use the following new ones.
update pod
Yuki Kimoto authored on 2011-03-13
2794

            
cleanup
Yuki Kimoto authored on 2011-06-09
2795
=over 4
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
2796

            
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2797
=item C<id>
2798

            
updated document
Yuki Kimoto authored on 2011-06-09
2799
    id => 4
2800
    id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
2801

            
updated document
Yuki Kimoto authored on 2011-06-09
2802
ID corresponding to C<primary_key>.
2803
You can insert a row by C<id> and C<primary_key>.
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2804

            
update pod
Yuki Kimoto authored on 2011-03-13
2805
    $dbi->insert(
updated document
Yuki Kimoto authored on 2011-06-09
2806
        {title => 'Perl', author => 'Ken'}
fixed pod
Yuki Kimoto authored on 2011-10-20
2807
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2808
        id => [4, 5],
updated document
Yuki Kimoto authored on 2011-06-09
2809
        table => 'book'
update pod
Yuki Kimoto authored on 2011-03-13
2810
    );
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2811

            
updated document
Yuki Kimoto authored on 2011-06-09
2812
The above is same as the followin one.
update pod
Yuki Kimoto authored on 2011-03-13
2813

            
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2814
    $dbi->insert(
updated document
Yuki Kimoto authored on 2011-06-09
2815
        {id1 => 4, id2 => 5, title => 'Perl', author => 'Ken'},
2816
        table => 'book'
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2817
    );
update pod
Yuki Kimoto authored on 2011-03-13
2818

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
2819
=item C<prefix>
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
2820

            
2821
    prefix => 'or replace'
2822

            
2823
prefix before table name section
2824

            
2825
    insert or replace into book
2826

            
updated document
Yuki Kimoto authored on 2011-06-09
2827
=item C<table>
2828

            
2829
    table => 'book'
2830

            
2831
Table name.
2832

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2833
=item C<wrap>
updated pod
Yuki Kimoto authored on 2011-09-02
2834

            
2835
    wrap => {price => sub { "max($_[0])" }}
2836

            
2837
placeholder wrapped string.
2838

            
2839
If the following statement
2840

            
2841
    $dbi->insert({price => 100}, table => 'book',
2842
      {price => sub { "$_[0] + 5" }});
2843

            
2844
is executed, the following SQL is executed.
2845

            
2846
    insert into book price values ( ? + 5 );
2847

            
update pod
Yuki Kimoto authored on 2011-03-13
2848
=back
2849

            
2850
=over 4
2851

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2852
=head2 C<include_model>
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2853

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2859
    lib / MyModel.pm
2860
        / MyModel / book.pm
2861
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2862

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

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

            
2867
    package MyModel;
updated pod
Yuki Kimoto authored on 2011-06-21
2868
    use DBIx::Custom::Model -base;
update pod
Yuki Kimoto authored on 2011-03-13
2869
    
2870
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2871

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2876
    package MyModel::book;
updated pod
Yuki Kimoto authored on 2011-06-21
2877
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2878
    
2879
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2880

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2883
    package MyModel::company;
updated pod
Yuki Kimoto authored on 2011-06-21
2884
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2885
    
2886
    1;
2887
    
updated pod
Yuki Kimoto authored on 2011-06-21
2888
MyModel::book and MyModel::company is included by C<include_model>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2889

            
updated pod
Yuki Kimoto authored on 2011-06-21
2890
You can get model object by C<model>.
update pod
Yuki Kimoto authored on 2011-03-13
2891

            
updated pod
Yuki Kimoto authored on 2011-06-21
2892
    my $book_model = $dbi->model('book');
update pod
Yuki Kimoto authored on 2011-03-13
2893
    my $company_model = $dbi->model('company');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2894

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2897
=head2 C<like_value>
added EXPERIMENTAL like_valu...
Yuki Kimoto authored on 2011-09-16
2898

            
2899
    my $like_value = $dbi->like_value
2900

            
cleanup
Yuki Kimoto authored on 2011-10-20
2901
Code reference which return a value for the like value.
added EXPERIMENTAL like_valu...
Yuki Kimoto authored on 2011-09-16
2902

            
2903
    sub { "%$_[0]%" }
2904

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2905
=head2 C<mapper>
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
2906

            
2907
    my $mapper = $dbi->mapper(param => $param);
2908

            
2909
Create a new L<DBIx::Custom::Mapper> object.
2910

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2911
=head2 C<merge_param>
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2912

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2915
Merge parameters. The following new parameter is created.
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2916

            
2917
    {key1 => [1, 1], key2 => 2}
2918

            
cleanup
Yuki Kimoto authored on 2011-10-20
2919
If same keys contains, the value is converted to array reference.
2920

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2921
=head2 C<model>
update pod
Yuki Kimoto authored on 2011-03-13
2922

            
2923
    my $model = $dbi->model('book');
2924

            
cleanup
Yuki Kimoto authored on 2011-10-20
2925
Get a L<DBIx::Custom::Model> object
2926
create by C<create_model> or C<include_model>
update pod
Yuki Kimoto authored on 2011-03-13
2927

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2928
=head2 C<mycolumn>
cleanup
Yuki Kimoto authored on 2011-03-21
2929

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2930
    my $column = $dbi->mycolumn(book => ['author', 'title']);
cleanup
Yuki Kimoto authored on 2011-03-21
2931

            
2932
Create column clause for myself. The follwoing column clause is created.
2933

            
2934
    book.author as author,
2935
    book.title as title
2936

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2939
    my $dbi = DBIx::Custom->new(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2940
        dsn => "dbi:mysql:database=dbname",
update pod
Yuki Kimoto authored on 2011-03-13
2941
        user => 'ken',
2942
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2943
        option => {mysql_enable_utf8 => 1}
update pod
Yuki Kimoto authored on 2011-03-13
2944
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2945

            
2946
Create a new L<DBIx::Custom> object.
2947

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

            
2950
    my $not_exists = $dbi->not_exists;
2951

            
update pod
Yuki Kimoto authored on 2011-03-13
2952
DBIx::Custom::NotExists object, indicating the column is not exists.
cleanup
Yuki Kimoto authored on 2011-10-20
2953
This is used in C<param> of L<DBIx::Custom::Where> .
experimental extended select...
Yuki Kimoto authored on 2011-01-17
2954

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
2955
=head2 C<order>
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
2956

            
2957
    my $order = $dbi->order;
2958

            
2959
Create a new L<DBIx::Custom::Order> object.
2960

            
cleanup
yuki-kimoto authored on 2010-10-17
2961
=head2 C<register_filter>
2962

            
update pod
Yuki Kimoto authored on 2011-03-13
2963
    $dbi->register_filter(
2964
        # Time::Piece object to database DATE format
2965
        tp_to_date => sub {
2966
            my $tp = shift;
2967
            return $tp->strftime('%Y-%m-%d');
2968
        },
2969
        # database DATE format to Time::Piece object
2970
        date_to_tp => sub {
2971
           my $date = shift;
2972
           return Time::Piece->strptime($date, '%Y-%m-%d');
2973
        }
2974
    );
cleanup
yuki-kimoto authored on 2010-10-17
2975
    
update pod
Yuki Kimoto authored on 2011-03-13
2976
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2977

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2980
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2981
        table  => 'book',
2982
        column => ['author', 'title'],
2983
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2984
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2985
    
updated document
Yuki Kimoto authored on 2011-06-09
2986
Execute select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2987

            
cleanup
Yuki Kimoto authored on 2011-10-20
2988
B<OPTIONS>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2989

            
cleanup
Yuki Kimoto authored on 2011-10-20
2990
C<select> method use all of C<execute> method's options,
2991
and use the following new ones.
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2992

            
cleanup
Yuki Kimoto authored on 2011-10-20
2993
=over 4
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2994

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2995
=item C<column>
2996
    
updated document
Yuki Kimoto authored on 2011-06-09
2997
    column => 'author'
2998
    column => ['author', 'title']
2999

            
3000
Column clause.
updated pod
Yuki Kimoto authored on 2011-06-07
3001
    
updated document
Yuki Kimoto authored on 2011-06-09
3002
if C<column> is not specified, '*' is set.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3003

            
updated document
Yuki Kimoto authored on 2011-06-09
3004
    column => '*'
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3005

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
3006
You can specify hash of array reference.
updated pod
Yuki Kimoto authored on 2011-06-07
3007

            
updated document
Yuki Kimoto authored on 2011-06-09
3008
    column => [
updated pod
Yuki Kimoto authored on 2011-06-07
3009
        {book => [qw/author title/]},
3010
        {person => [qw/name age/]}
updated document
Yuki Kimoto authored on 2011-06-09
3011
    ]
updated pod
Yuki Kimoto authored on 2011-06-07
3012

            
updated pod
Yuki Kimoto authored on 2011-06-21
3013
This is expanded to the following one by using C<colomn> method.
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3014

            
3015
    book.author as "book.author",
3016
    book.title as "book.title",
3017
    person.name as "person.name",
3018
    person.age as "person.age"
3019

            
- select method column optio...
Yuki Kimoto authored on 2011-07-11
3020
You can specify array of array reference, first argument is
3021
column name, second argument is alias.
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3022

            
updated document
Yuki Kimoto authored on 2011-06-09
3023
    column => [
- select method column optio...
Yuki Kimoto authored on 2011-07-11
3024
        ['date(book.register_datetime)' => 'book.register_date']
updated document
Yuki Kimoto authored on 2011-06-09
3025
    ];
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3026

            
- select method column optio...
Yuki Kimoto authored on 2011-07-11
3027
Alias is quoted properly and joined.
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3028

            
3029
    date(book.register_datetime) as "book.register_date"
updated pod
Yuki Kimoto authored on 2011-06-07
3030

            
updated document
Yuki Kimoto authored on 2011-06-09
3031
=item C<filter>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3032

            
updated document
Yuki Kimoto authored on 2011-06-09
3033
Same as C<execute> method's C<filter> option.
3034

            
3035
=item C<id>
3036

            
3037
    id => 4
3038
    id => [4, 5]
3039

            
3040
ID corresponding to C<primary_key>.
3041
You can select rows by C<id> and C<primary_key>.
3042

            
3043
    $dbi->select(
fixed pod
Yuki Kimoto authored on 2011-10-20
3044
        primary_key => ['id1', 'id2'],
updated document
Yuki Kimoto authored on 2011-06-09
3045
        id => [4, 5],
3046
        table => 'book'
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3047
    );
3048

            
updated document
Yuki Kimoto authored on 2011-06-09
3049
The above is same as the followin one.
3050

            
updated pod
Yuki Kimoto authored on 2011-04-25
3051
    $dbi->select(
updated document
Yuki Kimoto authored on 2011-06-09
3052
        where => {id1 => 4, id2 => 5},
3053
        table => 'book'
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
3054
    );
3055
    
cleanup
Yuki Kimoto authored on 2011-10-20
3056
=item C<param>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3057

            
updated document
Yuki Kimoto authored on 2011-06-09
3058
    param => {'table2.key3' => 5}
update pod
Yuki Kimoto authored on 2011-03-12
3059

            
updated document
Yuki Kimoto authored on 2011-06-09
3060
Parameter shown before where clause.
3061
    
3062
For example, if you want to contain tag in join clause, 
3063
you can pass parameter by C<param> option.
update pod
Yuki Kimoto authored on 2011-03-12
3064

            
updated document
Yuki Kimoto authored on 2011-06-09
3065
    join  => ['inner join (select * from table2 where table2.key3 = :table2.key3)' . 
3066
              ' as table2 on table1.key1 = table2.key1']
3067

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
3068
=itme C<prefix>
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
3069

            
3070
    prefix => 'SQL_CALC_FOUND_ROWS'
3071

            
3072
Prefix of column cluase
3073

            
3074
    select SQL_CALC_FOUND_ROWS title, author from book;
3075

            
updated document
Yuki Kimoto authored on 2011-06-09
3076
=item C<join>
3077

            
3078
    join => [
3079
        'left outer join company on book.company_id = company_id',
3080
        'left outer join location on company.location_id = location.id'
3081
    ]
3082
        
3083
Join clause. If column cluase or where clause contain table name like "company.name",
3084
join clausees needed when SQL is created is used automatically.
update pod
Yuki Kimoto authored on 2011-03-12
3085

            
3086
    $dbi->select(
3087
        table => 'book',
cleanup
Yuki Kimoto authored on 2011-06-13
3088
        column => ['company.location_id as location_id'],
update pod
Yuki Kimoto authored on 2011-03-12
3089
        where => {'company.name' => 'Orange'},
3090
        join => [
3091
            'left outer join company on book.company_id = company.id',
3092
            'left outer join location on company.location_id = location.id'
3093
        ]
3094
    );
3095

            
updated document
Yuki Kimoto authored on 2011-06-09
3096
In above select, column and where clause contain "company" table,
3097
the following SQL is created
update pod
Yuki Kimoto authored on 2011-03-12
3098

            
cleanup
Yuki Kimoto authored on 2011-06-13
3099
    select company.location_id as location_id
update pod
Yuki Kimoto authored on 2011-03-12
3100
    from book
3101
      left outer join company on book.company_id = company.id
cleanup
Yuki Kimoto authored on 2011-06-13
3102
    where company.name = ?;
update pod
Yuki Kimoto authored on 2011-03-12
3103

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3104
You can specify two table by yourself. This is useful when join parser can't parse
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
3105
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3106

            
3107
    $dbi->select(
3108
        table => 'book',
3109
        column => ['company.location_id as location_id'],
3110
        where => {'company.name' => 'Orange'},
3111
        join => [
3112
            {
3113
                clause => 'left outer join location on company.location_id = location.id',
3114
                table => ['company', 'location']
3115
            }
3116
        ]
3117
    );
3118

            
updated document
Yuki Kimoto authored on 2011-06-09
3119
=item C<table>
updated pod
Yuki Kimoto authored on 2011-06-08
3120

            
updated document
Yuki Kimoto authored on 2011-06-09
3121
    table => 'book'
updated pod
Yuki Kimoto authored on 2011-06-08
3122

            
updated document
Yuki Kimoto authored on 2011-06-09
3123
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3124

            
updated document
Yuki Kimoto authored on 2011-06-09
3125
=item C<where>
3126
    
3127
    # Hash refrence
3128
    where => {author => 'Ken', 'title' => 'Perl'}
3129
    
3130
    # DBIx::Custom::Where object
3131
    where => $dbi->where(
3132
        clause => ['and', 'author = :author', 'title like :title'],
3133
        param  => {author => 'Ken', title => '%Perl%'}
3134
    );
updated pod
Yuki Kimoto authored on 2011-06-21
3135
    
3136
    # Array reference 1 (array reference, hash referenc). same as above
3137
    where => [
3138
        ['and', 'author = :author', 'title like :title'],
3139
        {author => 'Ken', title => '%Perl%'}
3140
    ];    
3141
    
3142
    # Array reference 2 (String, hash reference)
3143
    where => [
3144
        'title like :title',
3145
        {title => '%Perl%'}
3146
    ]
3147
    
3148
    # String
3149
    where => 'title is null'
update pod
Yuki Kimoto authored on 2011-03-12
3150

            
cleanup
Yuki Kimoto authored on 2011-10-20
3151
Where clause. See L<DBIx::Custom::Where>.
updated document
Yuki Kimoto authored on 2011-06-09
3152
    
update pod
Yuki Kimoto authored on 2011-03-12
3153
=back
cleanup
Yuki Kimoto authored on 2011-03-08
3154

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3155
=head2 C<setup_model>
3156

            
3157
    $dbi->setup_model;
3158

            
3159
Setup all model objects.
3160
C<columns> of model object is automatically set, parsing database information.
3161

            
3162
=head2 C<type_rule>
3163

            
3164
    $dbi->type_rule(
3165
        into1 => {
3166
            date => sub { ... },
3167
            datetime => sub { ... }
3168
        },
3169
        into2 => {
3170
            date => sub { ... },
3171
            datetime => sub { ... }
3172
        },
3173
        from1 => {
3174
            # DATE
3175
            9 => sub { ... },
3176
            # DATETIME or TIMESTAMP
3177
            11 => sub { ... },
3178
        }
3179
        from2 => {
3180
            # DATE
3181
            9 => sub { ... },
3182
            # DATETIME or TIMESTAMP
3183
            11 => sub { ... },
3184
        }
3185
    );
3186

            
3187
Filtering rule when data is send into and get from database.
3188
This has a little complex problem.
3189

            
3190
In C<into1> and C<into2> you can specify
3191
type name as same as type name defined
3192
by create table, such as C<DATETIME> or C<DATE>.
3193

            
3194
Note that type name and data type don't contain upper case.
3195
If these contain upper case charactor, you convert it to lower case.
3196

            
3197
C<into2> is executed after C<into1>.
3198

            
3199
Type rule of C<into1> and C<into2> is enabled on the following
3200
column name.
3201

            
3202
=over 4
3203

            
3204
=item 1. column name
3205

            
3206
    issue_date
3207
    issue_datetime
3208

            
3209
This need C<table> option in each method.
3210

            
3211
=item 2. table name and column name, separator is dot
3212

            
3213
    book.issue_date
3214
    book.issue_datetime
3215

            
3216
=back
3217

            
3218
You get all type name used in database by C<available_typename>.
3219

            
3220
    print $dbi->available_typename;
3221

            
3222
In C<from1> and C<from2> you specify data type, not type name.
3223
C<from2> is executed after C<from1>.
3224
You get all data type by C<available_datatype>.
3225

            
3226
    print $dbi->available_datatype;
3227

            
3228
You can also specify multiple types at once.
3229

            
3230
    $dbi->type_rule(
3231
        into1 => [
3232
            [qw/DATE DATETIME/] => sub { ... },
3233
        ],
3234
    );
3235

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3238
    $dbi->update({title => 'Perl'}, table  => 'book', where  => {id => 4});
removed reconnect method
yuki-kimoto authored on 2010-05-28
3239

            
insert and update method's p...
Yuki Kimoto authored on 2011-07-29
3240
Execute update statement. First argument is update row data.
3241

            
3242
If you want to set constant value to row data, use scalar reference
3243
as parameter value.
3244

            
3245
    {date => \"NOW()"}
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
3246

            
cleanup
Yuki Kimoto authored on 2011-10-20
3247
B<OPTIONS>
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3248

            
cleanup
Yuki Kimoto authored on 2011-10-20
3249
C<update> method use all of C<execute> method's options,
3250
and use the following new ones.
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3251

            
cleanup
Yuki Kimoto authored on 2011-10-20
3252
=over 4
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3253

            
updated document
Yuki Kimoto authored on 2011-06-09
3254
=item C<id>
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3255

            
updated document
Yuki Kimoto authored on 2011-06-09
3256
    id => 4
3257
    id => [4, 5]
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3258

            
updated document
Yuki Kimoto authored on 2011-06-09
3259
ID corresponding to C<primary_key>.
3260
You can update rows by C<id> and C<primary_key>.
update pod
Yuki Kimoto authored on 2011-03-13
3261

            
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
3262
    $dbi->update(
updated document
Yuki Kimoto authored on 2011-06-09
3263
        {title => 'Perl', author => 'Ken'}
fixed pod
Yuki Kimoto authored on 2011-10-20
3264
        primary_key => ['id1', 'id2'],
updated document
Yuki Kimoto authored on 2011-06-09
3265
        id => [4, 5],
3266
        table => 'book'
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
3267
    );
update pod
Yuki Kimoto authored on 2011-03-13
3268

            
updated document
Yuki Kimoto authored on 2011-06-09
3269
The above is same as the followin one.
update pod
Yuki Kimoto authored on 2011-03-13
3270

            
updated document
Yuki Kimoto authored on 2011-06-09
3271
    $dbi->update(
3272
        {title => 'Perl', author => 'Ken'}
3273
        where => {id1 => 4, id2 => 5},
3274
        table => 'book'
3275
    );
update pod
Yuki Kimoto authored on 2011-03-13
3276

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
3277
=item C<prefix>
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
3278

            
3279
    prefix => 'or replace'
3280

            
3281
prefix before table name section
3282

            
3283
    update or replace book
3284

            
updated document
Yuki Kimoto authored on 2011-06-09
3285
=item C<table>
update pod
Yuki Kimoto authored on 2011-03-13
3286

            
updated document
Yuki Kimoto authored on 2011-06-09
3287
    table => 'book'
update pod
Yuki Kimoto authored on 2011-03-13
3288

            
updated document
Yuki Kimoto authored on 2011-06-09
3289
Table name.
update pod
Yuki Kimoto authored on 2011-03-13
3290

            
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
3291
=item C<where>
3292

            
3293
Same as C<select> method's C<where> option.
3294

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
3295
=item C<wrap>
updated pod
Yuki Kimoto authored on 2011-09-02
3296

            
3297
    wrap => {price => sub { "max($_[0])" }}
3298

            
3299
placeholder wrapped string.
3300

            
3301
If the following statement
3302

            
3303
    $dbi->update({price => 100}, table => 'book',
3304
      {price => sub { "$_[0] + 5" }});
3305

            
3306
is executed, the following SQL is executed.
3307

            
3308
    update book set price =  ? + 5;
3309

            
updated pod
Yuki Kimoto authored on 2011-06-08
3310
=back
update pod
Yuki Kimoto authored on 2011-03-13
3311

            
updated pod
Yuki Kimoto authored on 2011-06-08
3312
=head2 C<update_all>
update pod
Yuki Kimoto authored on 2011-03-13
3313

            
updated pod
Yuki Kimoto authored on 2011-06-21
3314
    $dbi->update_all({title => 'Perl'}, table => 'book', );
update pod
Yuki Kimoto authored on 2011-03-13
3315

            
updated document
Yuki Kimoto authored on 2011-06-09
3316
Execute update statement for all rows.
updated pod
Yuki Kimoto authored on 2011-06-21
3317
Options is same as C<update> method.
update pod
Yuki Kimoto authored on 2011-03-13
3318

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3319
=head2 C<update_or_insert EXPERIMENTAL>
3320
    
3321
    # Where
3322
    $dbi->update_or_insert(
3323
        {id => 1, title => 'Perl'},
3324
        table => 'book',
3325
        where => {id => 1},
3326
        select_option => {append => 'for update'}
3327
    );
3328
    
3329
    # ID
3330
    $dbi->update_or_insert(
3331
        {title => 'Perl'},
3332
        table => 'book',
3333
        id => 1,
3334
        primary_key => 'id',
3335
        select_option => {append => 'for update'}
3336
    );
3337
    
3338
Update or insert.
3339

            
3340
In both examples, the following SQL is executed.
3341

            
3342
    # In case insert
3343
    insert into book (id, title) values (?, ?)
3344
    
3345
    # In case update
3346
    update book set (id = ?, title = ?) where book.id = ?
3347

            
3348
The following opitons are available adding to C<update> option.
3349

            
3350
=over 4
3351

            
3352
=item C<select_option>
3353

            
3354
    select_option => {append => 'for update'}
3355

            
3356
select method option,
3357
select method is used to check the row is already exists.
3358

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
3359
=head2 C<show_datatype>
update pod
Yuki Kimoto authored on 2011-08-10
3360

            
3361
    $dbi->show_datatype($table);
3362

            
3363
Show data type of the columns of specified table.
3364

            
3365
    book
3366
    title: 5
3367
    issue_date: 91
3368

            
3369
This data type is used in C<type_rule>'s C<from1> and C<from2>.
3370

            
- removed EXPERIMENTAL the f...
Yuki Kimoto authored on 2011-09-12
3371
=head2 C<show_tables>
test cleanup
Yuki Kimoto authored on 2011-08-15
3372

            
3373
    $dbi->show_tables;
3374

            
3375
Show tables.
3376

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
3377
=head2 C<show_typename>
update pod
Yuki Kimoto authored on 2011-08-10
3378

            
3379
    $dbi->show_typename($table);
3380

            
3381
Show type name of the columns of specified table.
3382

            
3383
    book
3384
    title: varchar
3385
    issue_date: date
3386

            
3387
This type name is used in C<type_rule>'s C<into1> and C<into2>.
3388

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3389
=head2 C<values_clause>
3390

            
3391
    my $values_clause = $dbi->values_clause({title => 'a', age => 2});
3392

            
3393
Create values clause.
3394

            
3395
    (title, author) values (title = :title, age = :age);
3396

            
3397
You can use this in insert statement.
3398

            
3399
    my $insert_sql = "insert into book $values_clause";
3400

            
3401
=head2 C<where>
3402

            
3403
    my $where = $dbi->where(
3404
        clause => ['and', 'title = :title', 'author = :author'],
3405
        param => {title => 'Perl', author => 'Ken'}
3406
    );
3407

            
3408
Create a new L<DBIx::Custom::Where> object.
3409

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
3410
=head1 ENVIRONMENTAL VARIABLES
3411

            
3412
=head2 C<DBIX_CUSTOM_DEBUG>
3413

            
3414
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3415
executed SQL and bind values are printed to STDERR.
3416

            
improved debug message
Yuki Kimoto authored on 2011-05-23
3417
=head2 C<DBIX_CUSTOM_DEBUG_ENCODING>
3418

            
3419
DEBUG output encoding. Default to UTF-8.
added environment variable D...
Yuki Kimoto authored on 2011-04-02
3420

            
fix heading typos
Terrence Brannon authored on 2011-08-17
3421
=head1 DEPRECATED FUNCTIONALITY
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3422

            
3423
L<DBIx::Custom>
3424

            
3425
    # Attribute methods
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
3426
    default_dbi_option # will be removed 2017/1/1
3427
    dbi_option # will be removed 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3428
    data_source # will be removed at 2017/1/1
3429
    dbi_options # will be removed at 2017/1/1
3430
    filter_check # will be removed at 2017/1/1
3431
    reserved_word_quote # will be removed at 2017/1/1
3432
    cache_method # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3433
    
3434
    # Methods
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
3435
    update_timestamp # will be removed at 2017/1/1
3436
    insert_timestamp # will be removed at 2017/1/1
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
3437
    method # will be removed at 2017/1/1
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
3438
    assign_param # will be removed at 2017/1/1
3439
    update_param # will be removed at 2017/1/1
3440
    insert_param # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3441
    create_query # will be removed at 2017/1/1
3442
    apply_filter # will be removed at 2017/1/1
3443
    select_at # will be removed at 2017/1/1
3444
    delete_at # will be removed at 2017/1/1
3445
    update_at # will be removed at 2017/1/1
3446
    insert_at # will be removed at 2017/1/1
3447
    register_tag # will be removed at 2017/1/1
3448
    default_bind_filter # will be removed at 2017/1/1
3449
    default_fetch_filter # will be removed at 2017/1/1
3450
    insert_param_tag # will be removed at 2017/1/1
update pod
Yuki Kimoto authored on 2011-08-10
3451
    register_tag # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3452
    register_tag_processor # will be removed at 2017/1/1
3453
    update_param_tag # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3454
    
3455
    # Options
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
3456
    update timestamp option # will be removed 2017/1/1
3457
    insert timestamp option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3458
    select method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3459
    delete method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3460
    update method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3461
    insert method param option # will be removed at 2017/1/1
insert method's id option is...
Yuki Kimoto authored on 2011-10-10
3462
    insert method id option # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3463
    select method relation option # will be removed at 2017/1/1
3464
    select method column option [COLUMN, as => ALIAS] format
3465
      # will be removed at 2017/1/1
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
3466
    execute method's sqlfilter option # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3467
    
3468
    # Others
micro optimization and
Yuki Kimoto authored on 2011-10-25
3469
    execute($query, ...) # execute method receiving query object.
3470
                         # this is removed at 2017/1/1
cleanup
Yuki Kimoto authored on 2011-07-28
3471
    execute("select * from {= title}"); # execute method's
3472
                                        # tag parsing functionality
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3473
                                        # will be removed at 2017/1/1
3474
    Query caching # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3475

            
3476
L<DBIx::Custom::Model>
3477

            
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3478
    # Attribute methods
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
3479
    method # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3480
    filter # will be removed at 2017/1/1
3481
    name # will be removed at 2017/1/1
3482
    type # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3483

            
3484
L<DBIx::Custom::Query>
3485
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3486
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3487
    default_filter # will be removed at 2017/1/1
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3488
    table # will be removed at 2017/1/1
3489
    filters # will be removed at 2017/1/1
3490
    
3491
    # Methods
3492
    filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3493

            
3494
L<DBIx::Custom::QueryBuilder>
3495
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3496
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3497
    tags # will be removed at 2017/1/1
3498
    tag_processors # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3499
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3500
    # Methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3501
    register_tag # will be removed at 2017/1/1
3502
    register_tag_processor # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3503
    
3504
    # Others
3505
    build_query("select * from {= title}"); # tag parsing functionality
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3506
                                            # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3507

            
3508
L<DBIx::Custom::Result>
3509
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3510
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3511
    filter_check # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3512
    
3513
    # Methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3514
    end_filter # will be removed at 2017/1/1
3515
    remove_end_filter # will be removed at 2017/1/1
3516
    remove_filter # will be removed at 2017/1/1
3517
    default_filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3518

            
3519
L<DBIx::Custom::Tag>
3520

            
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3521
    This module is DEPRECATED! # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3522

            
fix heading typos
Terrence Brannon authored on 2011-08-17
3523
=head1 BACKWARDS COMPATIBILITY POLICY
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3524

            
3525
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings
3526
except for attribute method.
3527
You can check all DEPRECATED functionalities by document.
3528
DEPRECATED functionality is removed after five years,
3529
but if at least one person use the functionality and tell me that thing
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3530
I extend one year each time he tell me it.
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3531

            
3532
EXPERIMENTAL functionality will be changed without warnings.
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
3533

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

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

            
3538
C<< <kimoto.yuki at gmail.com> >>
3539

            
3540
L<http://github.com/yuki-kimoto/DBIx-Custom>
3541

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3542
=head1 AUTHOR
3543

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

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

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

            
3550
This program is free software; you can redistribute it and/or modify it
3551
under the same terms as Perl itself.
3552

            
3553
=cut