DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
3867 lines | 94.706kb
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
1
use 5.008007;
cleanup
yuki-kimoto authored on 2009-12-22
2
package DBIx::Custom;
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
3
use Object::Simple -base;
cleanup
yuki-kimoto authored on 2009-12-22
4

            
- DBIx::Custom::Mapper::map ...
Yuki Kimoto authored on 2012-02-29
5
our $VERSION = '0.23';
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/;
added experimental use_next_...
Yuki Kimoto authored on 2011-11-16
21

            
packaging one directory
yuki-kimoto authored on 2009-11-16
22

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
23
has [qw/connector dsn password quote user exclude_table user_table_info
cleanup
Yuki Kimoto authored on 2012-01-20
24
     user_column_info safety_character/],
added EXPERIMETNAL aysnc_con...
Yuki Kimoto authored on 2012-02-10
25
  async_conf => sub { {} },
cleanup
Yuki Kimoto authored on 2012-01-20
26
  cache => 0,
27
  cache_method => sub {
28
    sub {
29
      my $self = shift;
30
      $self->{_cached} ||= {};
31
      if (@_ > 1) { $self->{_cached}{$_[0]} = $_[1] }
32
      else { return $self->{_cached}{$_[0]} }
33
    }
34
  },
35
  option => sub { {} },
36
  default_option => sub {
37
    {
38
      RaiseError => 1,
39
      PrintError => 0,
40
      AutoCommit => 1
41
    }
42
  },
43
  filters => sub {
44
    {
45
      encode_utf8 => sub { encode_utf8($_[0]) },
46
      decode_utf8 => sub { decode_utf8($_[0]) }
47
    }
48
  },
49
  last_sql => '',
50
  models => sub { {} },
51
  now => sub {
52
    sub {
53
      my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
54
      $mon++;
55
      $year += 1900;
56
      my $now = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
57
        $year, $mon, $mday, $hour, $min, $sec);
58
      return $now;
59
    }
60
  },
61
  query_builder => sub {
62
    my $self = shift;
63
    my $builder = DBIx::Custom::QueryBuilder->new(dbi => $self);
64
    weaken $builder->{dbi};
65
    return $builder;
66
  },
67
  result_class  => 'DBIx::Custom::Result',
68
  separator => '.',
69
  stash => sub { {} };
cleanup
yuki-kimoto authored on 2010-10-17
70

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
102
our $AUTOLOAD;
103
sub AUTOLOAD {
cleanup
Yuki Kimoto authored on 2012-01-20
104
  my $self = shift;
added helper method
yuki-kimoto authored on 2010-10-17
105

            
cleanup
Yuki Kimoto authored on 2012-01-20
106
  # Method name
107
  my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/;
added helper method
yuki-kimoto authored on 2010-10-17
108

            
cleanup
Yuki Kimoto authored on 2012-01-20
109
  # Call method
110
  $self->{_methods} ||= {};
111
  if (my $method = $self->{_methods}->{$mname}) {
112
    return $self->$method(@_)
113
  }
114
  elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
115
    $self->dbh->$dbh_method(@_);
116
  }
117
  else {
118
    croak qq{Can't locate object method "$mname" via "$package" }
119
      . _subname;
120
  }
added helper method
yuki-kimoto authored on 2010-10-17
121
}
122

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
123
sub assign_clause {
cleanup
Yuki Kimoto authored on 2012-01-20
124
  my ($self, $param, $opts) = @_;
125
  
126
  my $wrap = $opts->{wrap} || {};
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
127
  my ($q, $p) = $self->_qp;
cleanup
Yuki Kimoto authored on 2012-01-20
128
  
129
  # Assign clause (performance is important)
130
  join(
131
    ', ',
132
    map {
133
      ref $param->{$_} eq 'SCALAR' ? "$q$_$p = " . ${$param->{$_}}
134
      : $wrap->{$_} ? "$q$_$p = " . $wrap->{$_}->(":$_")
135
      : "$q$_$p = :$_";
136
    } sort keys %$param
137
  );
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
138
}
139

            
cleanup
Yuki Kimoto authored on 2011-03-21
140
sub column {
cleanup
Yuki Kimoto authored on 2012-01-20
141
  my $self = shift;
142
  my $option = pop if ref $_[-1] eq 'HASH';
143
  my $real_table = shift;
144
  my $columns = shift;
145
  my $table = $option->{alias} || $real_table;
146
  
147
  # Columns
148
  unless (defined $columns) {
149
    $columns ||= $self->model($real_table)->columns;
150
  }
151
  
152
  # Separator
153
  my $separator = $self->separator;
154
  
fixed test
Yuki Kimoto authored on 2012-03-01
155
  # . is replaced
improved test
Yuki Kimoto authored on 2012-03-01
156
  my $t = $table;
157
  $t =~ s/\./$separator/g;
fixed test
Yuki Kimoto authored on 2012-03-01
158
  
cleanup
Yuki Kimoto authored on 2012-01-20
159
  # Column clause
160
  my @column;
161
  $columns ||= [];
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
162
  push @column, $self->_tq($table) . "." . $self->q($_) .
improved test
Yuki Kimoto authored on 2012-03-01
163
    " as " . $self->q("${t}${separator}$_")
cleanup
Yuki Kimoto authored on 2012-01-20
164
    for @$columns;
165
  
166
  return join (', ', @column);
added helper method
yuki-kimoto authored on 2010-10-17
167
}
168

            
packaging one directory
yuki-kimoto authored on 2009-11-16
169
sub connect {
cleanup
Yuki Kimoto authored on 2012-01-20
170
  my $self = ref $_[0] ? shift : shift->new(@_);
171
  
172
  my $connector = $self->connector;
173
  
174
  if (!ref $connector && $connector) {
175
    require DBIx::Connector;
176
    
177
    my $dsn = $self->dsn;
178
    my $user = $self->user;
179
    my $password = $self->password;
180
    my $option = $self->_option;
181
    my $connector = DBIx::Connector->new($dsn, $user, $password,
182
      {%{$self->default_option} , %$option});
183
    $self->connector($connector);
184
  }
185
  
186
  # Connect
187
  $self->dbh;
188
  
189
  return $self;
packaging one directory
yuki-kimoto authored on 2009-11-16
190
}
191

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
192
sub count { shift->select(column => 'count(*)', @_)->fetch_one->[0] }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
193

            
update pod
Yuki Kimoto authored on 2011-03-13
194
sub dbh {
cleanup
Yuki Kimoto authored on 2012-01-20
195
  my $self = shift;
196
  
197
  # Set
198
  if (@_) {
199
    $self->{dbh} = $_[0];
cleanup
Yuki Kimoto authored on 2011-04-02
200
    
cleanup
Yuki Kimoto authored on 2012-01-20
201
    return $self;
202
  }
203
  
204
  # Get
205
  else {
206
    # From Connction manager
207
    if (my $connector = $self->connector) {
208
      croak "connector must have dbh() method " . _subname
209
        unless ref $connector && $connector->can('dbh');
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
210
        
cleanup
Yuki Kimoto authored on 2012-01-20
211
      $self->{dbh} = $connector->dbh;
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
212
    }
213
    
cleanup
Yuki Kimoto authored on 2012-01-20
214
    # Connect
215
    $self->{dbh} ||= $self->_connect;
216
    
217
    # Quote
218
    if (!defined $self->reserved_word_quote && !defined $self->quote) {
219
      my $driver = $self->_driver;
220
      my $quote =  $driver eq 'odbc' ? '[]'
221
        : $driver eq 'ado' ? '[]'
222
        : $driver eq 'mysql' ? '`'
223
        : '"';
224
      $self->quote($quote);
update pod
Yuki Kimoto authored on 2011-03-13
225
    }
cleanup
Yuki Kimoto authored on 2012-01-20
226
    
227
    return $self->{dbh};
228
  }
update pod
Yuki Kimoto authored on 2011-03-13
229
}
230

            
cleanup
Yuki Kimoto authored on 2011-10-21
231
sub delete {
cleanup
Yuki Kimoto authored on 2012-01-20
232
  my ($self, %opt) = @_;
233
  warn "delete method where_param option is DEPRECATED!"
234
    if $opt{where_param};
235
  
236
  # Don't allow delete all rows
237
  croak qq{delete method where or id option must be specified } . _subname
238
    if !$opt{where} && !defined $opt{id} && !$opt{allow_delete_all};
239
  
240
  # Where
241
  my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
242
    delete $opt{id}, $opt{primary_key}, $opt{table});
243

            
244
  # Delete statement
245
  my $sql = "delete ";
246
  $sql .= "$opt{prefix} " if defined $opt{prefix};
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
247
  $sql .= "from " . $self->_tq($opt{table}) . " $w->{clause} ";
cleanup
Yuki Kimoto authored on 2012-01-20
248
  
249
  # Execute query
250
  $opt{statement} = 'delete';
251
  $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
252
}
253

            
cleanup
Yuki Kimoto authored on 2011-11-01
254
sub delete_all { shift->delete(@_, allow_delete_all => 1) }
packaging one directory
yuki-kimoto authored on 2009-11-16
255

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

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
258
sub create_model {
cleanup
Yuki Kimoto authored on 2012-01-20
259
  my $self = shift;
260
  
261
  # Options
262
  my $opt = ref $_[0] eq 'HASH' ? $_[0] : {@_};
263
  $opt->{dbi} = $self;
264
  my $model_class = delete $opt->{model_class} || 'DBIx::Custom::Model';
265
  my $model_name  = delete $opt->{name};
266
  my $model_table = delete $opt->{table};
267
  $model_name ||= $model_table;
268
  
269
  # Create model
270
  my $model = $model_class->new($opt);
271
  weaken $model->{dbi};
272
  $model->name($model_name) unless $model->name;
273
  $model->table($model_table) unless $model->table;
274
  
275
  # Apply filter(DEPRECATED logic)
276
  if ($model->{filter}) {
277
    my $filter = ref $model->filter eq 'HASH'
278
      ? [%{$model->filter}]
279
      : $model->filter;
280
    $filter ||= [];
281
    warn "DBIx::Custom::Model filter method is DEPRECATED!"
282
      if @$filter;
283
    $self->_apply_filter($model->table, @$filter);
284
  }
285
  
286
  # Set model
287
  $self->model($model->name, $model);
288
  
289
  return $self->model($model->name);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
290
}
291

            
292
sub each_column {
cleanup
Yuki Kimoto authored on 2012-01-20
293
  my ($self, $cb, %options) = @_;
improved test
Yuki Kimoto authored on 2012-03-01
294
  
cleanup
Yuki Kimoto authored on 2012-01-20
295
  my $user_column_info = $self->user_column_info;
296
  
297
  if ($user_column_info) {
298
    $self->$cb($_->{table}, $_->{column}, $_->{info}) for @$user_column_info;
299
  }
300
  else {
301
    my $re = $self->exclude_table || $options{exclude_table};
302
    # Tables
303
    my %tables;
304
    $self->each_table(sub { $tables{$_[1]}++ });
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
305

            
cleanup
Yuki Kimoto authored on 2012-01-20
306
    # Iterate all tables
307
    my @tables = sort keys %tables;
308
    for (my $i = 0; $i < @tables; $i++) {
309
      my $table = $tables[$i];
310
      
311
      # Iterate all columns
312
      my $sth_columns;
313
      eval {$sth_columns = $self->dbh->column_info(undef, undef, $table, '%')};
314
      next if $@;
315
      while (my $column_info = $sth_columns->fetchrow_hashref) {
316
        my $column = $column_info->{COLUMN_NAME};
317
        $self->$cb($table, $column, $column_info);
318
      }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
319
    }
cleanup
Yuki Kimoto authored on 2012-01-20
320
  }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
321
}
322

            
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
323
sub each_table {
cleanup
Yuki Kimoto authored on 2012-01-20
324
  my ($self, $cb, %option) = @_;
325
  
326
  my $user_table_infos = $self->user_table_info;
327
  
328
  # Iterate tables
329
  if ($user_table_infos) {
330
      $self->$cb($_->{table}, $_->{info}) for @$user_table_infos;
331
  }
332
  else {
333
    my $re = $self->exclude_table || $option{exclude};
334
    my $sth_tables = $self->dbh->table_info;
335
    while (my $table_info = $sth_tables->fetchrow_hashref) {
336
      # Table
337
      my $table = $table_info->{TABLE_NAME};
338
      next if defined $re && $table =~ /$re/;
339
      $self->$cb($table, $table_info);
340
    }
341
  }
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
342
}
343

            
cleanup
Yuki Kimoto authored on 2011-04-02
344
sub execute {
cleanup
Yuki Kimoto authored on 2012-01-20
345
  my $self = shift;
346
  my $sql = shift;
347

            
348
  # Options
349
  my $params;
350
  $params = shift if @_ % 2;
351
  my %opt = @_;
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
352
  
353
  # Async query
added EXPERIMETNAL aysnc_con...
Yuki Kimoto authored on 2012-02-10
354
  $opt{prepare_attr} = $self->async_conf->{prepare_attr} if $opt{async};
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
355
  if ($opt{async} && !$self->{_new_connection}) {
356
    my $dsn = $self->dsn;
357
    croak qq/Data source must be specified when "async" option is used/
358
      unless defined $dsn;
359
    
360
    my $user = $self->user;
361
    my $password = $self->password;
362
    my $option = $self->_option;
363
    
364
    my $new_dbi = bless {%$self}, ref $self;
365
    $new_dbi->connector(undef);
366
    $new_dbi->{dbh} = DBI->connect($dsn, $user, $password,
367
      {%{$new_dbi->default_option}, %$option});
368
    
369
    $new_dbi->{_new_connection} = 1;
370
    return $new_dbi->execute($sql, defined $params ? ($params) : (), %opt);
371
  }
372
  
373
  # Options
cleanup
Yuki Kimoto authored on 2012-01-20
374
  warn "sqlfilter option is DEPRECATED" if $opt{sqlfilter};
375
  $params ||= $opt{param} || {};
376
  my $tables = $opt{table} || [];
377
  $tables = [$tables] unless ref $tables eq 'ARRAY';
378
  my $filter = ref $opt{filter} eq 'ARRAY' ?
379
    _array_to_hash($opt{filter}) : $opt{filter};
380
  
381
  # Merge second parameter
382
  my @cleanup;
383
  my $saved_param;
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
384
  $opt{statement} ||= '';
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
385
  $opt{statement} = 'select' if $opt{select};
cleanup
Yuki Kimoto authored on 2012-01-20
386
  if (($opt{statement} || '') ne 'insert' && ref $params eq 'ARRAY') {
387
    my $params2 = $params->[1];
388
    $params = $params->[0];
389
    for my $column (keys %$params2) {
390
      if (!exists $params->{$column}) {
391
        $params->{$column} = $params2->{$column};
392
        push @cleanup, $column;
393
      }
394
      else {
395
        delete $params->{$_} for @cleanup;
396
        @cleanup = ();
397
        $saved_param  = $params;
398
        $params = $self->merge_param($params, $params2);
399
        delete $saved_param->{$_} for (@{$opt{cleanup} || []});
400
        last;
401
      }
402
    }
403
  }
404
  $params = [$params] unless ref $params eq 'ARRAY';
405
  
406
  # Append
407
  $sql .= $opt{append} if defined $opt{append} && !ref $sql;
408
  
409
  # Query
410
  my $query;
411
  if (ref $sql) {
412
    $query = $sql;
413
    warn "execute method receiving query object as first parameter is DEPRECATED!" .
414
      "because this is very buggy.";
415
  }
416
  else {
417
    $query = $opt{reuse}->{$sql} if $opt{reuse};
418
    unless ($query) {
419
      my $c = $self->{safety_character};
420
      # Check unsafety keys
421
      unless ((join('', keys %{$params->[0]}) || '') =~ /^[$c\.]+$/) {
422
        for my $column (keys %{$params->[0]}) {
423
          croak qq{"$column" is not safety column name } . _subname
424
            unless $column =~ /^[$c\.]+$/;
micro optimization
Yuki Kimoto authored on 2011-11-16
425
        }
cleanup
Yuki Kimoto authored on 2012-01-20
426
      }
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
427
      $query = $self->_create_query($sql,
428
        $opt{after_build_sql} || $opt{sqlfilter}, $opt{prepare_attr});
cleanup
Yuki Kimoto authored on 2012-01-20
429
    }
430
    $query->{statement} = $opt{statement} || '';
431
    $opt{reuse}->{$sql} = $query if $opt{reuse};
432
  }
433
      
434
  # Save query
435
  $self->{last_sql} = $query->{sql};
436

            
437
  # Return query
438
  if ($opt{query}) {
439
    for my $column (@cleanup, @{$opt{cleanup} || []}) {
440
      delete $_->{$column} for @$params;
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
441
    }
cleanup
Yuki Kimoto authored on 2012-01-20
442
    return $query;
443
  };
444
  
445
  # Merge query filter(DEPRECATED!)
446
  $filter ||= $query->{filter} || {};
447
  
448
  # Tables
449
  unshift @$tables, @{$query->{tables} || []};
450
  my $main_table = @{$tables}[-1];
451

            
452
  # Merge id to parameter
453
  if (defined $opt{id}) {
454
    my $statement = $query->{statement};
455
    warn "execute method id option is DEPRECATED!" unless $statement;
456
    croak "execute id option must be specified with primary_key option"
457
      unless $opt{primary_key};
458
    $opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
459
    $opt{id} = [$opt{id}] unless ref $opt{id};
460
    for (my $i = 0; $i < @{$opt{id}}; $i++) {
461
      my $key = $opt{primary_key}->[$i];
462
      $key = "$main_table.$key" if $statement eq 'update' ||
463
        $statement eq 'delete' || $statement eq 'select';
464
      next if exists $params->[0]->{$key};
465
      $params->[0]->{$key} = $opt{id}->[$i];
466
      push @cleanup, $key;1
467
    }
468
  }
469
  
470
  # Cleanup tables(DEPRECATED!)
471
  $tables = $self->_remove_duplicate_table($tables, $main_table)
472
    if @$tables > 1;
473
  
474
  # Type rule
475
  my $type_filters = {};
476
  my $type_rule_off = !$self->{_type_rule_is_called} || $opt{type_rule_off};
477
  unless ($type_rule_off) {
478
    my $type_rule_off_parts = {
479
      1 => $opt{type_rule1_off},
480
      2 => $opt{type_rule2_off}
cleanup
Yuki Kimoto authored on 2011-11-16
481
    };
cleanup
Yuki Kimoto authored on 2012-01-20
482
    for my $i (1, 2) {
483
      unless ($type_rule_off_parts->{$i}) {
484
        $type_filters->{$i} = {};
485
        my $table_alias = $opt{table_alias} || {};
486
        for my $alias (keys %$table_alias) {
487
          my $table = $table_alias->{$alias};
488
          
489
          for my $column (keys %{$self->{"_into$i"}{key}{$table} || {}}) {
490
            $type_filters->{$i}->{"$alias.$column"} = $self->{"_into$i"}{key}{$table}{$column};
491
          }
- insert method id value is ...
Yuki Kimoto authored on 2011-10-25
492
        }
cleanup
Yuki Kimoto authored on 2012-01-20
493
        $type_filters->{$i} = {%{$type_filters->{$i}}, %{$self->{"_into$i"}{key}{$main_table} || {}}}
494
          if $main_table;
495
      }
496
    }
497
  }
498
  
499
  # Applied filter(DEPRECATED!)
500
  if ($self->{filter}{on}) {
501
    my $applied_filter = {};
502
    for my $table (@$tables) {
503
      $applied_filter = {
504
        %$applied_filter,
505
        %{$self->{filter}{out}->{$table} || {}}
506
      }
507
    }
508
    $filter = {%$applied_filter, %$filter};
509
  }
510
  
511
  # Replace filter name to code
512
  for my $column (keys %$filter) {
513
    my $name = $filter->{$column};
514
    if (!defined $name) {
515
      $filter->{$column} = undef;
516
    }
517
    elsif (ref $name ne 'CODE') {
518
      croak qq{Filter "$name" is not registered" } . _subname
519
        unless exists $self->filters->{$name};
520
      $filter->{$column} = $self->filters->{$name};
521
    }
522
  }
523

            
524
  # Execute
525
  my $sth = $query->{sth};
526
  my $affected;
527
  if ((!$query->{duplicate} || $opt{bulk_insert}) && $type_rule_off
528
    && !keys %$filter && !$self->{default_out_filter}
529
    && !$opt{bind_type} && !$opt{type} && !$ENV{DBIX_CUSTOM_DEBUG})
530
  {
531
    eval {
532
      if ($opt{bulk_insert}) {
533
        my %count;
534
        my $param = $params->[0];
535
        $affected = $sth->execute(map { $param->{$_}->[++$count{$_} - 1] }
536
          @{$query->{columns}});
537
      }
538
      else {
- insert method can receive ...
Yuki Kimoto authored on 2011-11-25
539
        for my $param (@$params) {
cleanup
Yuki Kimoto authored on 2012-01-20
540
          $affected = $sth->execute(map { $param->{$_} }
541
            @{$query->{columns}});
- insert method can receive ...
Yuki Kimoto authored on 2011-11-25
542
        }
cleanup
Yuki Kimoto authored on 2012-01-20
543
      }
544
    };
545
  }
546
  else {
547
    for my $param (@$params) {
548
      # Create bind values
549
      my ($bind, $bind_types) = $self->_create_bind_values($param, $query->{columns},
550
        $filter, $type_filters, $opt{bind_type} || $opt{type} || {});
551

            
552
      # Execute
553
      eval {
554
        if ($opt{bind_type} || $opt{type}) {
555
          $sth->bind_param($_ + 1, $bind->[$_],
556
              $bind_types->[$_] ? $bind_types->[$_] : ())
557
            for (0 .. @$bind - 1);
558
          $affected = $sth->execute;
cleanup
Yuki Kimoto authored on 2011-01-12
559
        }
cleanup
Yuki Kimoto authored on 2012-01-20
560
        else { $affected = $sth->execute(@$bind) }
561

            
562
        # DEBUG message
563
        if ($ENV{DBIX_CUSTOM_DEBUG}) {
564
          warn "SQL:\n" . $query->{sql} . "\n";
565
          my @output;
566
          for my $value (@$bind) {
567
            $value = 'undef' unless defined $value;
568
            $value = encode($ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8', $value)
569
              if utf8::is_utf8($value);
570
            push @output, $value;
571
          }
572
          warn "Bind values: " . join(', ', @output) . "\n\n";
573
        }
574
      };
575
    }
576
  }
577
  
578
  $self->_croak($@, qq{. Following SQL is executed.\n}
579
    . qq{$query->{sql}\n} . _subname) if $@;
580

            
581
  # Remove id from parameter
582
  for my $column (@cleanup, @{$opt{cleanup} || []}) {
583
    delete $_->{$column} for @$params;
584
  }
585
  
586
  # Not select statement
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
587
  return $affected if !$sth->{NUM_OF_FIELDS} && $opt{statement} ne 'select';
cleanup
Yuki Kimoto authored on 2012-01-20
588

            
589
  # Filter(DEPRECATED!)
590
  my $infilter = {};
591
  if ($self->{filter}{on}) {
592
    $infilter->{in}  = {};
593
    $infilter->{end} = {};
594
    push @$tables, $main_table if $main_table;
595
    for my $table (@$tables) {
596
      for my $way (qw/in end/) {
597
        $infilter->{$way} = {%{$infilter->{$way}},
598
          %{$self->{filter}{$way}{$table} || {}}};
599
      }
600
    }
601
  }
602
  
603
  # Result
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
604
  my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2012-01-20
605
    sth => $sth,
606
    dbi => $self,
607
    default_filter => $self->{default_in_filter},
608
    filter => $infilter->{in} || {},
609
    end_filter => $infilter->{end} || {},
610
    type_rule => {
611
      from1 => $self->type_rule->{from1},
612
      from2 => $self->type_rule->{from2}
613
    },
614
  );
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
615
  
616
  if (my $cb = $opt{async}) {
617
    require AnyEvent;
618
    my $watcher;
619
    weaken $self;
620
    $watcher = AnyEvent->io(
added EXPERIMETNAL aysnc_con...
Yuki Kimoto authored on 2012-02-10
621
      fh => $self->async_conf->{fh}->($self),
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
622
      poll => 'r',
623
      cb   => sub {
624
        $cb->($self, $result);
select method can be called ...
Yuki Kimoto authored on 2012-02-07
625
        undef $watcher;
626
        undef $result;
627
        undef $cb;
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
628
      },
629
    );
630
  }
631
  else { $result }
cleanup
yuki-kimoto authored on 2010-10-17
632
}
633

            
added test
Yuki Kimoto authored on 2011-08-16
634
sub get_table_info {
cleanup
Yuki Kimoto authored on 2012-01-20
635
  my ($self, %opt) = @_;
636
  
637
  my $exclude = delete $opt{exclude};
638
  croak qq/"$_" is wrong option/ for keys %opt;
639
  
640
  my $table_info = [];
641
  $self->each_table(
642
    sub { push @$table_info, {table => $_[1], info => $_[2] } },
643
    exclude => $exclude
644
  );
645
  
646
  return [sort {$a->{table} cmp $b->{table} } @$table_info];
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
647
}
648

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
649
sub get_column_info {
cleanup
Yuki Kimoto authored on 2012-01-20
650
  my ($self, %opt) = @_;
651
  
652
  my $exclude_table = delete $opt{exclude_table};
653
  croak qq/"$_" is wrong option/ for keys %opt;
654
  
655
  my $column_info = [];
656
  $self->each_column(
657
    sub { push @$column_info, {table => $_[1], column => $_[2], info => $_[3] } },
658
    exclude_table => $exclude_table
659
  );
660
  
661
  return [
662
    sort {$a->{table} cmp $b->{table} || $a->{column} cmp $b->{column} }
663
      @$column_info];
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
664
}
665

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
666
sub helper {
cleanup
Yuki Kimoto authored on 2012-01-20
667
  my $self = shift;
668
  
669
  # Register method
670
  my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
671
  $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
672
  
673
  return $self;
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
674
}
675

            
cleanup
yuki-kimoto authored on 2010-10-17
676
sub insert {
cleanup
Yuki Kimoto authored on 2012-01-20
677
  my $self = shift;
678
  
679
  # Options
680
  my $params = @_ % 2 ? shift : undef;
681
  my %opt = @_;
682
  warn "insert method param option is DEPRECATED!" if $opt{param};
683
  $params ||= delete $opt{param} || {};
684
  
685
  my $multi;
686
  if (ref $params eq 'ARRAY') { $multi = 1 }
687
  else { $params = [$params] }
688
  
689
  # Timestamp(DEPRECATED!)
690
  if (!$multi && $opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
691
    warn "insert timestamp option is DEPRECATED! use ctime option";
cleanup
Yuki Kimoto authored on 2012-01-20
692
    my $columns = $insert_timestamp->[0];
693
    $columns = [$columns] unless ref $columns eq 'ARRAY';
694
    my $value = $insert_timestamp->[1];
695
    $value = $value->() if ref $value eq 'CODE';
696
    $params->[0]->{$_} = $value for @$columns;
697
  }
698

            
699
  # Created time and updated time
700
  my @timestamp_cleanup;
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
701
  warn "insert method created_at option is DEPRECATED! "
702
      . "use ctime option instead. " . _subname
703
    if $opt{created_at};
704
  warn "insert method updated_at option is DEPRECATED! "
705
      . "use mtime option instead. " . _subname
706
    if $opt{updated_at};
707
  $opt{ctime} ||= $opt{created_at};
708
  $opt{mtime} ||= $opt{updated_at};
709
  if (defined $opt{ctime} || defined $opt{mtime}) {
cleanup
Yuki Kimoto authored on 2012-01-20
710
    my $now = $self->now;
711
    $now = $now->() if ref $now eq 'CODE';
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
712
    if (defined $opt{ctime}) {
713
      $_->{$opt{ctime}} = $now for @$params;
714
      push @timestamp_cleanup, $opt{ctime};
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
715
    }
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
716
    if (defined $opt{mtime}) {
717
      $_->{$opt{mtime}} = $now for @$params;
718
      push @timestamp_cleanup, $opt{mtime};
cleanup
Yuki Kimoto authored on 2012-01-20
719
    }
720
  }
721
  
722
  # Merge id to parameter
723
  my @cleanup;
724
  my $id_param = {};
725
  if (defined $opt{id} && !$multi) {
726
    croak "insert id option must be specified with primary_key option"
727
      unless $opt{primary_key};
728
    $opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key};
729
    $opt{id} = [$opt{id}] unless ref $opt{id};
730
    for (my $i = 0; $i < @{$opt{primary_key}}; $i++) {
731
      my $key = $opt{primary_key}->[$i];
732
      next if exists $params->[0]->{$key};
733
      $params->[0]->{$key} = $opt{id}->[$i];
734
      push @cleanup, $key;
735
    }
736
  }
737
  
738
  # Insert statement
739
  my $sql = "insert ";
740
  $sql .= "$opt{prefix} " if defined $opt{prefix};
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
741
  $sql .= "into " . $self->_tq($opt{table}) . " ";
cleanup
Yuki Kimoto authored on 2012-01-20
742
  if ($opt{bulk_insert}) {
743
    $sql .= $self->_multi_values_clause($params, {wrap => $opt{wrap}}) . " ";
744
    my $new_param = {};
745
    $new_param->{$_} = [] for keys %{$params->[0]};
746
    for my $param (@$params) {
747
      push @{$new_param->{$_}}, $param->{$_} for keys %$param;
748
    }
749
    $params = [$new_param];
750
  }
751
  else {
752
    $sql .= $self->values_clause($params->[0], {wrap => $opt{wrap}}) . " ";
753
  }
754

            
755
  # Remove id from parameter
756
  delete $params->[0]->{$_} for @cleanup;
757
  
758
  # Execute query
759
  $opt{statement} = 'insert';
760
  $opt{cleanup} = \@timestamp_cleanup;
761
  $self->execute($sql, $params, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
762
}
763

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
764
sub insert_timestamp {
cleanup
Yuki Kimoto authored on 2012-01-20
765
  my $self = shift;
766
  
767
  warn "insert_timestamp method is DEPRECATED! use now attribute";
768
  
769
  if (@_) {
770
    $self->{insert_timestamp} = [@_];
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
771
    
cleanup
Yuki Kimoto authored on 2012-01-20
772
    return $self;
773
  }
774
  return $self->{insert_timestamp};
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
775
}
776

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
777
sub include_model {
cleanup
Yuki Kimoto authored on 2012-01-20
778
  my ($self, $name_space, $model_infos) = @_;
779
  
780
  # Name space
781
  $name_space ||= '';
782
  
783
  # Get Model infomations
784
  unless ($model_infos) {
785

            
786
    # Load name space module
787
    croak qq{"$name_space" is invalid class name } . _subname
788
      if $name_space =~ /[^\w:]/;
789
    eval "use $name_space";
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
790
    croak qq{Name space module "$name_space.pm" is needed. $@ } . _subname
cleanup
Yuki Kimoto authored on 2012-01-20
791
      if $@;
792
    
793
    # Search model modules
794
    my $path = $INC{"$name_space.pm"};
795
    $path =~ s/\.pm$//;
796
    opendir my $dh, $path
797
      or croak qq{Can't open directory "$path": $! } . _subname
support model full-qualified...
Yuki Kimoto authored on 2012-03-01
798
    my @modules;
799
    while (my $file = readdir $dh) {
800
      my $file_abs = "$path/$file";
801
      if (-d $file_abs) {
802
        next if $file eq '.' || $file eq '..';
803
        opendir my $fq_dh, $file_abs
804
          or croak qq{Can't open directory "$file_abs": $! } . _subname;
805
        while (my $fq_file = readdir $fq_dh) {
806
          my $fq_file_abs = "$file_abs/$fq_file";
807
          push @modules, "${file}::$fq_file" if -f $fq_file_abs;
808
        }
809
        close $fq_dh;
810
      }
811
      elsif(-f $file_abs) { push @modules, $file }
cleanup
Yuki Kimoto authored on 2012-01-20
812
    }
813
    close $dh;
support model full-qualified...
Yuki Kimoto authored on 2012-03-01
814
    
815
    $model_infos = [];
816
    for my $module (@modules) {
817
      if ($module =~ s/\.pm$//) { push @$model_infos, $module }
818
    }
cleanup
Yuki Kimoto authored on 2012-01-20
819
  }
820
  
821
  # Include models
822
  for my $model_info (@$model_infos) {
823
    
824
    # Load model
825
    my $model_class;
826
    my $model_name;
827
    my $model_table;
828
    if (ref $model_info eq 'HASH') {
829
      $model_class = $model_info->{class};
830
      $model_name  = $model_info->{name};
831
      $model_table = $model_info->{table};
832
      
833
      $model_name  ||= $model_class;
834
      $model_table ||= $model_name;
835
    }
support model full-qualified...
Yuki Kimoto authored on 2012-03-01
836
    else {
837
      $model_class = $model_name = $model_table = $model_info;
838
    }
improved test
Yuki Kimoto authored on 2012-03-01
839

            
840
    $model_class =~ s/\./::/g;
841
    $model_name =~ s/::/./;
842
    $model_table =~ s/::/./;
843

            
cleanup
Yuki Kimoto authored on 2012-01-20
844
    my $mclass = "${name_space}::$model_class";
845
    croak qq{"$mclass" is invalid class name } . _subname
846
      if $mclass =~ /[^\w:]/;
847
    unless ($mclass->can('isa')) {
848
      eval "use $mclass";
849
      croak "$@ " . _subname if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
850
    }
851
    
cleanup
Yuki Kimoto authored on 2012-01-20
852
    # Create model
853
    my $opt = {};
854
    $opt->{model_class} = $mclass if $mclass;
855
    $opt->{name}        = $model_name if $model_name;
856
    $opt->{table}       = $model_table if $model_table;
857
    $self->create_model($opt);
858
  }
859
  
860
  return $self;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
861
}
862

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

            
added DBIx::Custom::Mapper
Yuki Kimoto authored on 2011-08-26
865
sub mapper {
cleanup
Yuki Kimoto authored on 2012-01-20
866
  my $self = shift;
867
  return DBIx::Custom::Mapper->new(@_);
added DBIx::Custom::Mapper
Yuki Kimoto authored on 2011-08-26
868
}
869

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
870
sub merge_param {
cleanup
Yuki Kimoto authored on 2012-01-20
871
  my ($self, @params) = @_;
872
  
873
  # Merge parameters
874
  my $merge = {};
875
  for my $param (@params) {
876
    for my $column (keys %$param) {
877
      my $param_is_array = ref $param->{$column} eq 'ARRAY' ? 1 : 0;
878
      
879
      if (exists $merge->{$column}) {
880
        $merge->{$column} = [$merge->{$column}]
881
          unless ref $merge->{$column} eq 'ARRAY';
882
        push @{$merge->{$column}},
883
          ref $param->{$column} ? @{$param->{$column}} : $param->{$column};
884
      }
885
      else { $merge->{$column} = $param->{$column} }
886
    }
887
  }
888
  
889
  return $merge;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
890
}
891

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
892
sub model {
cleanup
Yuki Kimoto authored on 2012-01-20
893
  my ($self, $name, $model) = @_;
894
  
895
  # Set model
896
  if ($model) {
897
    $self->models->{$name} = $model;
898
    return $self;
899
  }
900
  
901
  # Check model existance
902
  croak qq{Model "$name" is not included } . _subname
903
    unless $self->models->{$name};
904
  
905
  # Get model
906
  return $self->models->{$name};
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
907
}
908

            
cleanup
Yuki Kimoto authored on 2011-03-21
909
sub mycolumn {
cleanup
Yuki Kimoto authored on 2012-01-20
910
  my ($self, $table, $columns) = @_;
911
  
912
  # Create column clause
913
  my @column;
914
  $columns ||= [];
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
915
  push @column, $self->_tq($table) . "." . $self->q($_) . " as " . $self->q($_)
cleanup
Yuki Kimoto authored on 2012-01-20
916
    for @$columns;
917
  
918
  return join (', ', @column);
cleanup
Yuki Kimoto authored on 2011-03-21
919
}
920

            
added dbi_options attribute
kimoto authored on 2010-12-20
921
sub new {
cleanup
Yuki Kimoto authored on 2012-01-20
922
  my $self = shift->SUPER::new(@_);
923
  
924
  # Check attributes
925
  my @attrs = keys %$self;
926
  for my $attr (@attrs) {
927
    croak qq{Invalid attribute: "$attr" } . _subname
928
      unless $self->can($attr);
929
  }
930
  
931
  $self->{safety_character} = 'a-zA-Z0-9_'
932
    unless exists $self->{safety_character};
933

            
934
  # DEPRECATED
935
  $self->{_tags} = {
936
    '?'     => \&DBIx::Custom::Tag::placeholder,
937
    '='     => \&DBIx::Custom::Tag::equal,
938
    '<>'    => \&DBIx::Custom::Tag::not_equal,
939
    '>'     => \&DBIx::Custom::Tag::greater_than,
940
    '<'     => \&DBIx::Custom::Tag::lower_than,
941
    '>='    => \&DBIx::Custom::Tag::greater_than_equal,
942
    '<='    => \&DBIx::Custom::Tag::lower_than_equal,
943
    'like'  => \&DBIx::Custom::Tag::like,
944
    'in'    => \&DBIx::Custom::Tag::in,
945
    'insert_param' => \&DBIx::Custom::Tag::insert_param,
946
    'update_param' => \&DBIx::Custom::Tag::update_param
947
  };
948
  $self->{tag_parse} = 1 unless exists $self->{tag_parse};
949
  $self->{cache} = 0 unless exists $self->{cache};
950
  
951
  return $self;
cleanup
Yuki Kimoto authored on 2011-08-13
952
}
953

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

            
956
sub order {
cleanup
Yuki Kimoto authored on 2012-01-20
957
  my $self = shift;
958
  return DBIx::Custom::Order->new(dbi => $self, @_);
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
959
}
960

            
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
961
sub q { shift->_tq($_[0], $_[1], whole => 1) }
962

            
963
sub _tq {
964
  my ($self, $value, $quotemeta, %opt) = @_;
cleanup
Yuki Kimoto authored on 2012-01-20
965
  
966
  my $quote = $self->{reserved_word_quote}
967
    || $self->{quote} || $self->quote || '';
968
  
969
  my $q = substr($quote, 0, 1) || '';
970
  my $p;
971
  if (defined $quote && length $quote > 1) {
972
    $p = substr($quote, 1, 1);
973
  }
974
  else { $p = $q }
975
  
976
  if ($quotemeta) {
977
    $q = quotemeta($q);
978
    $p = quotemeta($p);
979
  }
980
  
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
981
  if ($opt{whole}) { return "$q$value$p" }
982
  else {
fixed relation full qualifie...
Yuki Kimoto authored on 2012-03-01
983
    my @values = split /\./, $value;
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
984
    push @values, '' unless @values;
fixed relation full qualifie...
Yuki Kimoto authored on 2012-03-01
985
    for my $v (@values) { $v = "$q$v$p" }
986
    return join '.', @values;
987
  }
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
988
}
989

            
990
sub _qp {
991
  my ($self, %opt) = @_;
992

            
993
  my $quote = $self->{reserved_word_quote}
994
    || $self->{quote} || $self->quote || '';
995
  
996
  my $q = substr($quote, 0, 1) || '';
997
  my $p;
998
  if (defined $quote && length $quote > 1) {
999
    $p = substr($quote, 1, 1);
1000
  }
1001
  else { $p = $q }
1002
  
1003
  if ($opt{quotemeta}) {
1004
    $q = quotemeta($q);
1005
    $p = quotemeta($p);
1006
  }
1007
  
1008
  return ($q, $p);
added EXPERIMENTAL q method
Yuki Kimoto authored on 2011-10-27
1009
}
1010

            
cleanup
yuki-kimoto authored on 2010-10-17
1011
sub register_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
1012
  my $self = shift;
1013
  
1014
  # Register filter
1015
  my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1016
  $self->filters({%{$self->filters}, %$filters});
1017
  
1018
  return $self;
cleanup
yuki-kimoto authored on 2010-10-17
1019
}
packaging one directory
yuki-kimoto authored on 2009-11-16
1020

            
1021
sub select {
cleanup
Yuki Kimoto authored on 2012-01-20
1022
  my $self = shift;
1023
  my $column = shift if @_ % 2;
1024
  my %opt = @_;
select method can be called ...
Yuki Kimoto authored on 2012-02-07
1025
  $opt{statement} = 'select';
cleanup
Yuki Kimoto authored on 2012-01-20
1026
  $opt{column} = $column if defined $column;
1027

            
1028
  # Options
select method can be called ...
Yuki Kimoto authored on 2012-02-07
1029
  my $table_is_empty;
cleanup
Yuki Kimoto authored on 2012-01-20
1030
  my $tables = ref $opt{table} eq 'ARRAY' ? $opt{table}
1031
    : defined $opt{table} ? [$opt{table}]
1032
    : [];
1033
  $opt{table} = $tables;
select method can be called ...
Yuki Kimoto authored on 2012-02-07
1034
  $table_is_empty = 1 unless @$tables;
cleanup
Yuki Kimoto authored on 2012-01-20
1035
  my $where_param = $opt{where_param} || delete $opt{param} || {};
1036
  warn "select method where_param option is DEPRECATED!"
1037
    if $opt{where_param};
1038
  
1039
  # Add relation tables(DEPRECATED!);
1040
  if ($opt{relation}) {
1041
    warn "select() relation option is DEPRECATED!";
1042
    $self->_add_relation_table($tables, $opt{relation});
1043
  }
1044
  
1045
  # Select statement
1046
  my $sql = 'select ';
1047
  
1048
  # Prefix
1049
  $sql .= "$opt{prefix} " if defined $opt{prefix};
1050
  
1051
  # Column
1052
  if (defined $opt{column}) {
1053
    my $columns
1054
      = ref $opt{column} eq 'ARRAY' ? $opt{column} : [$opt{column}];
1055
    for my $column (@$columns) {
1056
      if (ref $column eq 'HASH') {
1057
        $column = $self->column(%$column) if ref $column eq 'HASH';
1058
      }
1059
      elsif (ref $column eq 'ARRAY') {
1060
        warn "select column option [COLUMN => ALIAS] syntax is DEPRECATED!" .
1061
          "use q method to quote the value";
1062
        if (@$column == 3 && $column->[1] eq 'as') {
1063
          warn "[COLUMN, as => ALIAS] is DEPRECATED! use [COLUMN => ALIAS]";
1064
          splice @$column, 1, 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
1065
        }
cleanup
Yuki Kimoto authored on 2012-01-20
1066
        
1067
        $column = join(' ', $column->[0], 'as', $self->q($column->[1]));
1068
      }
select method can be called ...
Yuki Kimoto authored on 2012-02-07
1069
      unshift @$tables, @{$self->_search_tables($column)}
1070
        unless $table_is_empty;
cleanup
Yuki Kimoto authored on 2012-01-20
1071
      $sql .= "$column, ";
packaging one directory
yuki-kimoto authored on 2009-11-16
1072
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
1073
    $sql =~ s/, $/ /;
cleanup
Yuki Kimoto authored on 2012-01-20
1074
  }
1075
  else { $sql .= '* ' }
select method can be called ...
Yuki Kimoto authored on 2012-02-07
1076

            
1077
  # Execute query without table
1078
  return $self->execute($sql, {}, %opt) if $table_is_empty;
1079

            
cleanup
Yuki Kimoto authored on 2012-01-20
1080
  # Table
1081
  $sql .= 'from ';
1082
  if ($opt{relation}) {
1083
    my $found = {};
1084
    for my $table (@$tables) {
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1085
      $sql .= $self->_tq($table) . ', ' unless $found->{$table};
cleanup
Yuki Kimoto authored on 2012-01-20
1086
      $found->{$table} = 1;
1087
    }
1088
  }
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1089
  else { $sql .= $self->_tq($tables->[-1] || '') . ' ' }
cleanup
Yuki Kimoto authored on 2012-01-20
1090
  $sql =~ s/, $/ /;
1091

            
1092
  # Add tables in parameter
1093
  unshift @$tables,
1094
    @{$self->_search_tables(join(' ', keys %$where_param) || '')};
1095
  
1096
  # Where
1097
  my $w = $self->_where_clause_and_param($opt{where}, $where_param,
select method can be called ...
Yuki Kimoto authored on 2012-02-07
1098
    delete $opt{id}, $opt{primary_key}, @$tables ? $tables->[-1] : undef);
cleanup
Yuki Kimoto authored on 2012-01-20
1099
  
1100
  # Add table names in where clause
1101
  unshift @$tables, @{$self->_search_tables($w->{clause})};
1102
  
1103
  # Join statement
1104
  $self->_push_join(\$sql, $opt{join}, $tables) if defined $opt{join};
1105
  
1106
  # Add where clause
1107
  $sql .= "$w->{clause} ";
1108
  
1109
  # Relation(DEPRECATED!);
1110
  $self->_push_relation(\$sql, $tables, $opt{relation}, $w->{clause} eq '' ? 1 : 0)
1111
    if $opt{relation};
1112
  
1113
  # Execute query
select method can be called ...
Yuki Kimoto authored on 2012-02-07
1114
  return $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
1115
}
1116

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1117
sub setup_model {
improved test
Yuki Kimoto authored on 2012-03-01
1118
  my ($self, %opt) = @_;
cleanup
Yuki Kimoto authored on 2012-01-20
1119
  
1120
  # Setup model
1121
  $self->each_column(
1122
    sub {
1123
      my ($self, $table, $column, $column_info) = @_;
improved test
Yuki Kimoto authored on 2012-03-01
1124
      my $database = $column_info->{TABLE_SCHEM};
1125
      return if exists $opt{database} && $opt{database} ne $database;
1126
      
1127
      $table = "$database.$table" if exists $opt{prefix};
cleanup
Yuki Kimoto authored on 2012-01-20
1128
      if (my $model = $self->models->{$table}) {
1129
        push @{$model->columns}, $column;
1130
      }
1131
    }
1132
  );
1133
  return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1134
}
1135

            
update pod
Yuki Kimoto authored on 2011-08-10
1136
sub show_datatype {
cleanup
Yuki Kimoto authored on 2012-01-20
1137
  my ($self, $table) = @_;
1138
  croak "Table name must be specified" unless defined $table;
1139
  print "$table\n";
1140
  
1141
  my $result = $self->select(table => $table, where => "'0' <> '0'");
1142
  my $sth = $result->sth;
1143

            
1144
  my $columns = $sth->{NAME};
1145
  my $data_types = $sth->{TYPE};
1146
  
1147
  for (my $i = 0; $i < @$columns; $i++) {
1148
    my $column = $columns->[$i];
1149
    my $data_type = lc $data_types->[$i];
1150
    print "$column: $data_type\n";
1151
  }
update pod
Yuki Kimoto authored on 2011-08-10
1152
}
1153

            
1154
sub show_typename {
cleanup
Yuki Kimoto authored on 2012-01-20
1155
  my ($self, $t) = @_;
1156
  croak "Table name must be specified" unless defined $t;
1157
  print "$t\n";
1158
  
1159
  $self->each_column(sub {
1160
    my ($self, $table, $column, $infos) = @_;
1161
    return unless $table eq $t;
1162
    my $typename = lc $infos->{TYPE_NAME};
1163
    print "$column: $typename\n";
1164
  });
1165
  
1166
  return $self;
update pod
Yuki Kimoto authored on 2011-08-10
1167
}
1168

            
test cleanup
Yuki Kimoto authored on 2011-08-15
1169
sub show_tables {
cleanup
Yuki Kimoto authored on 2012-01-20
1170
  my $self = shift;
1171
  
1172
  my %tables;
1173
  $self->each_table(sub { $tables{$_[1]}++ });
1174
  print join("\n", sort keys %tables) . "\n";
1175
  return $self;
test cleanup
Yuki Kimoto authored on 2011-08-15
1176
}
1177

            
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1178
sub type_rule {
cleanup
Yuki Kimoto authored on 2012-01-20
1179
  my $self = shift;
1180

            
1181
  $self->{_type_rule_is_called} = 1;
1182
  
1183
  if (@_) {
1184
    my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1185
    
1186
    # Into
1187
    for my $i (1 .. 2) {
1188
      my $into = "into$i";
1189
      my $exists_into = exists $type_rule->{$into};
1190
      $type_rule->{$into} = _array_to_hash($type_rule->{$into});
1191
      $self->{type_rule} = $type_rule;
1192
      $self->{"_$into"} = {};
1193
      for my $type_name (keys %{$type_rule->{$into} || {}}) {
1194
        croak qq{type name of $into section must be lower case}
1195
          if $type_name =~ /[A-Z]/;
1196
      }
1197
      
1198
      $self->each_column(sub {
1199
        my ($dbi, $table, $column, $column_info) = @_;
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
1200
        
cleanup
Yuki Kimoto authored on 2012-01-20
1201
        my $type_name = lc $column_info->{TYPE_NAME};
1202
        if ($type_rule->{$into} &&
1203
            (my $filter = $type_rule->{$into}->{$type_name}))
1204
        {
1205
          return unless exists $type_rule->{$into}->{$type_name};
1206
          if (defined $filter && ref $filter ne 'CODE') 
1207
          {
1208
            my $fname = $filter;
1209
            croak qq{Filter "$fname" is not registered" } . _subname
1210
              unless exists $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2011-08-16
1211
            
cleanup
Yuki Kimoto authored on 2012-01-20
1212
            $filter = $self->filters->{$fname};
1213
          }
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1214

            
cleanup
Yuki Kimoto authored on 2012-01-20
1215
          $self->{"_$into"}{key}{$table}{$column} = $filter;
1216
          $self->{"_$into"}{dot}{"$table.$column"} = $filter;
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
1217
        }
cleanup
Yuki Kimoto authored on 2012-01-20
1218
      });
1219
    }
1220

            
1221
    # From
1222
    for my $i (1 .. 2) {
1223
      $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
1224
      for my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
1225
        croak qq{data type of from$i section must be lower case or number}
1226
          if $data_type =~ /[A-Z]/;
1227
        my $fname = $type_rule->{"from$i"}{$data_type};
1228
        if (defined $fname && ref $fname ne 'CODE') {
1229
          croak qq{Filter "$fname" is not registered" } . _subname
1230
            unless exists $self->filters->{$fname};
1231
          
1232
          $type_rule->{"from$i"}{$data_type} = $self->filters->{$fname};
1233
        }
1234
      }
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1235
    }
1236
    
cleanup
Yuki Kimoto authored on 2012-01-20
1237
    return $self;
1238
  }
1239
  
1240
  return $self->{type_rule} || {};
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1241
}
1242

            
cleanup
yuki-kimoto authored on 2010-10-17
1243
sub update {
cleanup
Yuki Kimoto authored on 2012-01-20
1244
  my $self = shift;
1245

            
1246
  # Options
1247
  my $param = @_ % 2 ? shift : undef;
1248
  my %opt = @_;
1249
  warn "update param option is DEPRECATED!" if $opt{param};
1250
  warn "update method where_param option is DEPRECATED!"
1251
    if $opt{where_param};
1252
  $param ||= $opt{param} || {};
1253
  
1254
  # Don't allow update all rows
1255
  croak qq{update method where option must be specified } . _subname
1256
    if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all};
1257
  
1258
  # Timestamp(DEPRECATED!)
1259
  if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
1260
    warn "update timestamp option is DEPRECATED! use mtime";
cleanup
Yuki Kimoto authored on 2012-01-20
1261
    my $columns = $update_timestamp->[0];
1262
    $columns = [$columns] unless ref $columns eq 'ARRAY';
1263
    my $value = $update_timestamp->[1];
1264
    $value = $value->() if ref $value eq 'CODE';
1265
    $param->{$_} = $value for @$columns;
1266
  }
1267

            
1268
  # Created time and updated time
1269
  my @timestamp_cleanup;
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
1270
  warn "update method update_at option is DEPRECATED! "
1271
      . "use mtime option instead " . _subname
1272
    if $opt{updated_at};
1273
  $opt{mtime} ||= $opt{updated_at};
1274
  if (defined $opt{mtime}) {
cleanup
Yuki Kimoto authored on 2012-01-20
1275
    my $now = $self->now;
1276
    $now = $now->() if ref $now eq 'CODE';
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
1277
    $param->{$opt{mtime}} = $self->now->();
1278
    push @timestamp_cleanup, $opt{mtime};
cleanup
Yuki Kimoto authored on 2012-01-20
1279
  }
1280

            
1281
  # Assign clause
1282
  my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
1283
  
1284
  # Where
1285
  my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
1286
    delete $opt{id}, $opt{primary_key}, $opt{table});
1287
  
1288
  # Update statement
1289
  my $sql = "update ";
1290
  $sql .= "$opt{prefix} " if defined $opt{prefix};
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1291
  $sql .= $self->_tq($opt{table}) . " set $assign_clause $w->{clause} ";
cleanup
Yuki Kimoto authored on 2012-01-20
1292
  
1293
  # Execute query
1294
  $opt{statement} = 'update';
1295
  $opt{cleanup} = \@timestamp_cleanup;
1296
  $self->execute($sql, [$param, $w->{param}], %opt);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1297
}
1298

            
cleanup
Yuki Kimoto authored on 2011-11-01
1299
sub update_all { shift->update(@_, allow_update_all => 1) };
cleanup
yuki-kimoto authored on 2010-10-17
1300

            
cleanup
Yuki Kimoto authored on 2011-10-21
1301
sub update_or_insert {
cleanup
Yuki Kimoto authored on 2012-01-20
1302
  my ($self, $param, %opt) = @_;
1303
  croak "update_or_insert method need primary_key and id option "
1304
    unless defined $opt{id} && defined $opt{primary_key};
1305
  my $statement_opt = $opt{option} || {};
1306

            
1307
  my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all;
1308
  if (@$rows == 0) {
1309
    return $self->insert($param, %opt, %{$statement_opt->{insert} || {}});
1310
  }
1311
  elsif (@$rows == 1) {
1312
    return 0 unless keys %$param;
1313
    return $self->update($param, %opt, %{$statement_opt->{update} || {}});
1314
  }
1315
  else { croak "selected row must be one " . _subname }
cleanup
Yuki Kimoto authored on 2011-10-21
1316
}
1317

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1318
sub update_timestamp {
cleanup
Yuki Kimoto authored on 2012-01-20
1319
  my $self = shift;
1320
  
1321
  warn "update_timestamp method is DEPRECATED! use now method";
1322
  
1323
  if (@_) {
1324
    $self->{update_timestamp} = [@_];
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1325
    
cleanup
Yuki Kimoto authored on 2012-01-20
1326
    return $self;
1327
  }
1328
  return $self->{update_timestamp};
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1329
}
1330

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1331
sub values_clause {
cleanup
Yuki Kimoto authored on 2012-01-20
1332
  my ($self, $param, $opts) = @_;
1333
  
1334
  my $wrap = $opts->{wrap} || {};
1335
  
1336
  # Create insert parameter tag
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1337
  my ($q, $p) = $self->_qp;
cleanup
Yuki Kimoto authored on 2012-01-20
1338
  
1339
  # values clause(performance is important)
1340
  '(' .
1341
  join(
1342
    ', ',
1343
    map { "$q$_$p" } sort keys %$param
1344
  ) .
1345
  ') values (' .
1346
  join(
1347
    ', ',
1348
    map {
1349
      ref $param->{$_} eq 'SCALAR' ? ${$param->{$_}} :
1350
      $wrap->{$_} ? $wrap->{$_}->(":$_") :
1351
      ":$_";
1352
    } sort keys %$param
1353
  ) .
1354
  ')'
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1355
}
1356

            
added EXPERIMENTAL bulk_inse...
Yuki Kimoto authored on 2011-11-27
1357
sub _multi_values_clause {
cleanup
Yuki Kimoto authored on 2012-01-20
1358
  my ($self, $params, $opts) = @_;
1359
  
1360
  my $wrap = $opts->{wrap} || {};
1361
  
1362
  # Create insert parameter tag
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1363
  my ($q, $p) = $self->_qp;
cleanup
Yuki Kimoto authored on 2012-01-20
1364
  
1365
  # Multi values clause
1366
  my $clause = '(' . join(', ', map { "$q$_$p" } sort keys %{$params->[0]}) . ') values ';
1367
  
1368
  for (1 .. @$params) {
1369
    $clause .= '(' . join(', ', 
1370
      map {
1371
        ref $params->[0]->{$_} eq 'SCALAR' ? ${$params->[0]->{$_}} :
1372
        $wrap->{$_} ? $wrap->{$_}->(":$_") :
1373
        ":$_";
1374
      } sort keys %{$params->[0]}
1375
    ) . '), '
1376
  }
1377
  $clause =~ s/, $//;
1378
  return $clause;
added EXPERIMENTAL bulk_inse...
Yuki Kimoto authored on 2011-11-27
1379
}
1380

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

            
updated pod
Yuki Kimoto authored on 2011-06-21
1383
sub _create_query {
cleanup
Yuki Kimoto authored on 2012-01-20
1384
  
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
1385
  my ($self, $source, $after_build_sql, $prepare_attr) = @_;
1386
  
1387
  $prepare_attr ||= {};
cleanup
Yuki Kimoto authored on 2012-01-20
1388
  
1389
  # Cache
1390
  my $cache = $self->{cache};
1391
  
1392
  # Query
1393
  my $query;
1394
  
1395
  # Get cached query
1396
  if ($cache) {
1397
    
1398
    # Get query
1399
    my $q = $self->cache_method->($self, $source);
updated pod
Yuki Kimoto authored on 2011-06-21
1400
    
1401
    # Create query
cleanup
Yuki Kimoto authored on 2012-01-20
1402
    if ($q) {
1403
      $query = DBIx::Custom::Query->new($q);
1404
      $query->{filters} = $self->filters;
cleanup
Yuki Kimoto authored on 2011-06-13
1405
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1406
  }
1407
  
1408
  # Create query
1409
  unless ($query) {
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1410

            
cleanup
Yuki Kimoto authored on 2012-01-20
1411
    # Create query
1412
    my $tag_parse = exists $ENV{DBIX_CUSTOM_TAG_PARSE}
1413
      ? $ENV{DBIX_CUSTOM_TAG_PARSE} : $self->{tag_parse};
1414

            
1415
    my $sql = " " . $source || '';
1416
    if ($tag_parse && ($sql =~ /\s\{/)) {
1417
      $query = $self->query_builder->build_query($sql);
updated pod
Yuki Kimoto authored on 2011-06-21
1418
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1419
    else {
1420
      my @columns;
1421
      my $c = $self->{safety_character};
1422
      my $re = $c eq 'a-zA-Z0-9_'
1423
        ? qr/(.*?[^\\]):([$c\.]+)(?:\{(.*?)\})?(.*)/so
1424
        : qr/(.*?[^\\]):([$c\.]+)(?:\{(.*?)\})?(.*)/s;
1425
      my %duplicate;
1426
      my $duplicate;
1427
      # Parameter regex
1428
      $sql =~ s/([0-9]):/$1\\:/g;
1429
      my $new_sql = '';
1430
      while ($sql =~ /$re/) {
1431
        push @columns, $2;
1432
        $duplicate = 1 if ++$duplicate{$columns[-1]} > 1;
1433
        ($new_sql, $sql) = defined $3 ?
1434
          ($new_sql . "$1$2 $3 ?", " $4") : ($new_sql . "$1?", " $4");
1435
      }
1436
      $new_sql .= $sql;
1437
      $new_sql =~ s/\\:/:/g if index($new_sql, "\\:") != -1;
1438

            
1439
      # Create query
1440
      $query = {sql => $new_sql, columns => \@columns, duplicate => $duplicate};
1441
    }
1442
    
1443
    # Save query to cache
1444
    $self->cache_method->(
1445
      $self, $source,
1446
      {
1447
        sql     => $query->{sql}, 
1448
        columns => $query->{columns},
1449
        tables  => $query->{tables} || []
1450
      }
1451
    ) if $cache;
1452
  }
1453

            
1454
  # Filter SQL
1455
  $query->{sql} = $after_build_sql->($query->{sql}) if $after_build_sql;
1456
  
1457
  # Save sql
1458
  $self->{last_sql} = $query->{sql};
1459
  
1460
  # Prepare statement handle
1461
  my $sth;
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
1462
  eval { $sth = $self->dbh->prepare($query->{sql}, $prepare_attr) };
cleanup
Yuki Kimoto authored on 2012-01-20
1463
  
1464
  if ($@) {
1465
    $self->_croak($@, qq{. Following SQL is executed.\n}
1466
                    . qq{$query->{sql}\n} . _subname);
1467
  }
1468
  
1469
  # Set statement handle
1470
  $query->{sth} = $sth;
1471
  
1472
  # Set filters
1473
  $query->{filters} = $self->{filters} || $self->filters;
1474
  
1475
  return $query;
cleanup
Yuki Kimoto authored on 2011-06-13
1476
}
1477

            
cleanup
Yuki Kimoto authored on 2012-01-20
1478
sub _create_bind_values {
1479
  my ($self, $params, $columns, $filter, $type_filters, $bind_type) = @_;
1480
  
1481
  $bind_type = _array_to_hash($bind_type) if ref $bind_type eq 'ARRAY';
1482
  
1483
  # Create bind values
1484
  my @bind;
1485
  my @types;
1486
  my %count;
1487
  my %not_exists;
1488
  for my $column (@$columns) {
1489
    
1490
    # Bind value
1491
    if(ref $params->{$column} eq 'ARRAY') {
1492
      my $i = $count{$column} || 0;
1493
      $i += $not_exists{$column} || 0;
1494
      my $found;
1495
      for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1496
        if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1497
            $not_exists{$column}++;
micro optimization
Yuki Kimoto authored on 2011-10-23
1498
        }
cleanup
Yuki Kimoto authored on 2012-01-20
1499
        else  {
1500
          push @bind, $params->{$column}->[$k];
1501
          $found = 1;
1502
          last
micro optimization
Yuki Kimoto authored on 2011-10-23
1503
        }
cleanup
Yuki Kimoto authored on 2012-01-20
1504
      }
1505
      next unless $found;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1506
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1507
    else { push @bind, $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1508
    
cleanup
Yuki Kimoto authored on 2012-01-20
1509
    # Filter
1510
    if (my $f = $filter->{$column} || $self->{default_out_filter} || '') {
1511
      $bind[-1] = $f->($bind[-1]);
1512
    }
improved error messages
Yuki Kimoto authored on 2011-04-18
1513
    
cleanup
Yuki Kimoto authored on 2012-01-20
1514
    # Type rule
1515
    if ($self->{_type_rule_is_called}) {
1516
      my $tf1 = $self->{"_into1"}->{dot}->{$column}
1517
        || $type_filters->{1}->{$column};
1518
      $bind[-1] = $tf1->($bind[-1]) if $tf1;
1519
      my $tf2 = $self->{"_into2"}->{dot}->{$column}
1520
        || $type_filters->{2}->{$column};
1521
      $bind[-1] = $tf2->($bind[-1]) if $tf2;
improved error messages
Yuki Kimoto authored on 2011-04-18
1522
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1523
   
1524
    # Bind types
1525
    push @types, $bind_type->{$column};
improved error messages
Yuki Kimoto authored on 2011-04-18
1526
    
cleanup
Yuki Kimoto authored on 2012-01-20
1527
    # Count up 
1528
    $count{$column}++;
1529
  }
1530
  
1531
  return (\@bind, \@types);
1532
}
1533

            
1534
sub _id_to_param {
1535
  my ($self, $id, $primary_keys, $table) = @_;
1536
  
1537
  # Check primary key
1538
  croak "primary_key option " .
1539
        "must be specified when id option is used" . _subname
1540
    unless defined $primary_keys;
1541
  $primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY';
1542
  
1543
  # Create parameter
1544
  my $param = {};
1545
  if (defined $id) {
1546
    $id = [$id] unless ref $id;
1547
    for(my $i = 0; $i < @$id; $i++) {
1548
      my $key = $primary_keys->[$i];
1549
      $key = "$table." . $key if $table;
1550
      $param->{$key} = $id->[$i];
1551
    }
1552
  }
1553
  
1554
  return $param;
improved error messages
Yuki Kimoto authored on 2011-04-18
1555
}
1556

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1557
sub _connect {
cleanup
Yuki Kimoto authored on 2012-01-20
1558
  my $self = shift;
1559
  
1560
  # Attributes
1561
  my $dsn = $self->data_source;
1562
  warn "data_source is DEPRECATED!\n"
1563
    if $dsn;
1564
  $dsn ||= $self->dsn;
1565
  croak qq{"dsn" must be specified } . _subname
1566
    unless $dsn;
1567
  my $user        = $self->user;
1568
  my $password    = $self->password;
1569
  my $option = $self->_option;
1570
  $option = {%{$self->default_option}, %$option};
1571
  
1572
  # Connect
1573
  my $dbh;
1574
  eval { $dbh = DBI->connect($dsn, $user, $password, $option) };
1575
  
1576
  # Connect error
1577
  croak "$@ " . _subname if $@;
1578
  
1579
  return $dbh;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1580
}
1581

            
cleanup
yuki-kimoto authored on 2010-10-17
1582
sub _croak {
cleanup
Yuki Kimoto authored on 2012-01-20
1583
  my ($self, $error, $append) = @_;
1584
  
1585
  # Append
1586
  $append ||= "";
1587
  
1588
  # Verbose
1589
  if ($Carp::Verbose) { croak $error }
1590
  
1591
  # Not verbose
1592
  else {
1593
    # Remove line and module infromation
1594
    my $at_pos = rindex($error, ' at ');
1595
    $error = substr($error, 0, $at_pos);
1596
    $error =~ s/\s+$//;
1597
    croak "$error$append";
1598
  }
cleanup
yuki-kimoto authored on 2010-10-17
1599
}
1600

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1603
sub _need_tables {
cleanup
Yuki Kimoto authored on 2012-01-20
1604
  my ($self, $tree, $need_tables, $tables) = @_;
1605
  
1606
  # Get needed tables
1607
  for my $table (@$tables) {
1608
    if ($tree->{$table}) {
1609
      $need_tables->{$table} = 1;
1610
      $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1611
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1612
  }
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1613
}
1614

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1615
sub _option {
cleanup
Yuki Kimoto authored on 2012-01-20
1616
  my $self = shift;
1617
  my $option = {%{$self->dbi_options}, %{$self->dbi_option}, %{$self->option}};
1618
  warn "dbi_options is DEPRECATED! use option instead\n"
1619
    if keys %{$self->dbi_options};
1620
  warn "dbi_option is DEPRECATED! use option instead\n"
1621
    if keys %{$self->dbi_option};
1622
  return $option;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1623
}
1624

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1625
sub _push_join {
cleanup
Yuki Kimoto authored on 2012-01-20
1626
  my ($self, $sql, $join, $join_tables) = @_;
1627
  
1628
  $join = [$join] unless ref $join eq 'ARRAY';
1629
  
1630
  # No join
1631
  return unless @$join;
1632
  
1633
  # Push join clause
1634
  my $tree = {};
1635
  for (my $i = 0; $i < @$join; $i++) {
1636
    
1637
    # Arrange
1638
    my $join_clause;;
1639
    my $option;
1640
    if (ref $join->[$i] eq 'HASH') {
1641
      $join_clause = $join->[$i]->{clause};
1642
      $option = {table => $join->[$i]->{table}};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1643
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1644
    else {
1645
      $join_clause = $join->[$i];
1646
      $option = {};
1647
    };
1648

            
1649
    # Find tables in join clause
1650
    my $table1;
1651
    my $table2;
1652
    if (my $table = $option->{table}) {
1653
      $table1 = $table->[0];
1654
      $table2 = $table->[1];
1655
    }
1656
    else {
1657
      my $q = $self->_quote;
1658
      my $j_clause = (split /\s+on\s+/, $join_clause)[-1];
1659
      $j_clause =~ s/'.+?'//g;
1660
      my $q_re = quotemeta($q);
1661
      $j_clause =~ s/[$q_re]//g;
1662
      
1663
      my @j_clauses = reverse split /\s(and|on)\s/, $j_clause;
1664
      my $c = $self->{safety_character};
improved test
Yuki Kimoto authored on 2012-03-01
1665
      my $join_re = qr/((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+[^$c].*?((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+/sm;
cleanup
Yuki Kimoto authored on 2012-01-20
1666
      for my $clause (@j_clauses) {
1667
        if ($clause =~ $join_re) {
1668
          $table1 = $1;
1669
          $table2 = $2;
1670
          last;
1671
        }                
1672
      }
1673
    }
1674
    croak qq{join clause must have two table name after "on" keyword. } .
1675
        qq{"$join_clause" is passed }  . _subname
1676
      unless defined $table1 && defined $table2;
1677
    croak qq{right side table of "$join_clause" must be unique } . _subname
1678
      if exists $tree->{$table2};
1679
    croak qq{Same table "$table1" is specified} . _subname
1680
      if $table1 eq $table2;
1681
    $tree->{$table2}
1682
      = {position => $i, parent => $table1, join => $join_clause};
1683
  }
1684
  
1685
  # Search need tables
1686
  my $need_tables = {};
1687
  $self->_need_tables($tree, $need_tables, $join_tables);
1688
  my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} }
1689
    keys %$need_tables;
1690
  
1691
  # Add join clause
1692
  $$sql .= $tree->{$_}{join} . ' ' for @need_tables;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1693
}
cleanup
Yuki Kimoto authored on 2011-03-08
1694

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1695
sub _quote {
cleanup
Yuki Kimoto authored on 2012-01-20
1696
  my $self = shift;
1697
  return $self->{reserved_word_quote} || $self->quote || '';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1698
}
1699

            
cleanup
Yuki Kimoto authored on 2011-04-02
1700
sub _remove_duplicate_table {
cleanup
Yuki Kimoto authored on 2012-01-20
1701
  my ($self, $tables, $main_table) = @_;
1702
  
1703
  # Remove duplicate table
1704
  my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1705
  delete $tables{$main_table} if $main_table;
1706
  
1707
  my $new_tables = [keys %tables, $main_table ? $main_table : ()];
1708
  if (my $q = $self->_quote) {
1709
    $q = quotemeta($q);
1710
    $_ =~ s/[$q]//g for @$new_tables;
1711
  }
micro optimization
Yuki Kimoto authored on 2011-07-30
1712

            
cleanup
Yuki Kimoto authored on 2012-01-20
1713
  return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1714
}
1715

            
cleanup
Yuki Kimoto authored on 2011-04-02
1716
sub _search_tables {
cleanup
Yuki Kimoto authored on 2012-01-20
1717
  my ($self, $source) = @_;
1718
  
1719
  # Search tables
1720
  my $tables = [];
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1721
  my ($q, $p) = $self->_qp(quotemeta => 1);
1722
  $source =~ s/$q//g;
1723
  $source =~ s/$p//g;
1724
  my $c = $self->safety_character;
1725
  
1726
  while ($source =~ /((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+/g) {
1727
    push @$tables, $1;
1728
  }
cleanup
Yuki Kimoto authored on 2012-01-20
1729
  return $tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1730
}
1731

            
cleanup
Yuki Kimoto authored on 2011-10-21
1732
sub _where_clause_and_param {
cleanup
Yuki Kimoto authored on 2012-01-20
1733
  my ($self, $where, $where_param, $id, $primary_key, $table) = @_;
1734

            
1735
  $where ||= {};
1736
  $where = $self->_id_to_param($id, $primary_key, $table) if defined $id;
1737
  $where_param ||= {};
1738
  my $w = {};
1739

            
cleanup
Yuki Kimoto authored on 2012-02-28
1740
  if (ref $where eq 'HASH') {
1741
    my $clause = [];
1742
    my $column_join = '';
1743
    for my $column (keys %$where) {
1744
      $column_join .= $column;
1745
      my $table;
1746
      my $c;
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1747
      if ($column =~ /(?:(.*)\.)?(.*)/) {
cleanup
Yuki Kimoto authored on 2012-02-28
1748
        $table = $1;
1749
        $c = $2;
cleanup
Yuki Kimoto authored on 2012-01-20
1750
      }
cleanup
Yuki Kimoto authored on 2012-02-28
1751
      
1752
      my $table_quote;
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1753
      $table_quote = $self->_tq($table) if defined $table;
cleanup
Yuki Kimoto authored on 2012-02-28
1754
      my $column_quote = $self->q($c);
1755
      $column_quote = $table_quote . '.' . $column_quote
1756
        if defined $table_quote;
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
1757
      if (ref $where->{$column} eq 'ARRAY') {
1758
        my $c = join(', ', (":$column") x @{$where->{$column}});
fixed where multi value bug
Yuki Kimoto authored on 2012-02-28
1759
        if (@{$where->{$column}}) {
1760
          push @$clause, "$column_quote in ( $c )";
1761
        }
1762
        else { push @$clause, '1 <> 1' }
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
1763
      }
1764
      else { push @$clause, "$column_quote = :$column" }
cleanup
Yuki Kimoto authored on 2012-02-28
1765
    }
1766
    
1767
    $w->{clause} = @$clause ? "where ( " . join(' and ', @$clause) . " ) " : '' ;
1768
    $w->{param} = $where;
1769
    $w->{param} = keys %$where_param
1770
      ? $self->merge_param($where_param, $where)
1771
      : $where;
1772
  }  
1773
  elsif (ref $where) {
1774
    my $obj;
1775

            
1776
    if (ref $where eq 'DBIx::Custom::Where') { $obj = $where }
cleanup
Yuki Kimoto authored on 2012-01-20
1777
    elsif (ref $where eq 'ARRAY') {
1778
      $obj = $self->where(clause => $where->[0], param => $where->[1]);
1779
    }
1780
    
1781
    # Check where argument
1782
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
1783
        . qq{or array reference, which contains where clause and parameter}
1784
        . _subname
1785
      unless ref $obj eq 'DBIx::Custom::Where';
1786

            
cleanup
Yuki Kimoto authored on 2012-02-28
1787
    $w->{clause} = $obj->to_string;
cleanup
Yuki Kimoto authored on 2012-01-20
1788
    $w->{param} = keys %$where_param
1789
      ? $self->merge_param($where_param, $obj->param)
1790
      : $obj->param;
1791
  }
1792
  elsif ($where) {
1793
    $w->{clause} = "where $where";
1794
    $w->{param} = $where_param;
1795
  }
1796
  
1797
  return $w;
cleanup
Yuki Kimoto authored on 2011-10-21
1798
}
1799

            
updated pod
Yuki Kimoto authored on 2011-06-21
1800
sub _apply_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
1801
  my ($self, $table, @cinfos) = @_;
1802

            
1803
  # Initialize filters
1804
  $self->{filter} ||= {};
1805
  $self->{filter}{on} = 1;
1806
  $self->{filter}{out} ||= {};
1807
  $self->{filter}{in} ||= {};
1808
  $self->{filter}{end} ||= {};
1809
  
1810
  # Usage
1811
  my $usage = "Usage: \$dbi->apply_filter(" .
1812
    "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
1813
    "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
1814
  
1815
  # Apply filter
1816
  for (my $i = 0; $i < @cinfos; $i += 2) {
updated pod
Yuki Kimoto authored on 2011-06-21
1817
    
cleanup
Yuki Kimoto authored on 2012-01-20
1818
    # Column
1819
    my $column = $cinfos[$i];
1820
    if (ref $column eq 'ARRAY') {
1821
      for my $c (@$column) { push @cinfos, $c, $cinfos[$i + 1] }
1822
      next;
1823
    }
updated pod
Yuki Kimoto authored on 2011-06-21
1824
    
cleanup
Yuki Kimoto authored on 2012-01-20
1825
    # Filter infomation
1826
    my $finfo = $cinfos[$i + 1] || {};
1827
    croak "$usage (table: $table) " . _subname
1828
      unless  ref $finfo eq 'HASH';
1829
    for my $ftype (keys %$finfo) {
1830
      croak "$usage (table: $table) " . _subname
1831
        unless $ftype eq 'in' || $ftype eq 'out' || $ftype eq 'end'; 
updated pod
Yuki Kimoto authored on 2011-06-21
1832
    }
1833
    
cleanup
Yuki Kimoto authored on 2012-01-20
1834
    # Set filters
1835
    for my $way (qw/in out end/) {
1836
  
1837
      # Filter
1838
      my $filter = $finfo->{$way};
1839
      
1840
      # Filter state
1841
      my $state = !exists $finfo->{$way} ? 'not_exists'
1842
        : !defined $filter        ? 'not_defined'
1843
        : ref $filter eq 'CODE'   ? 'code'
1844
        : 'name';
1845
      
1846
      # Filter is not exists
1847
      next if $state eq 'not_exists';
1848
      
1849
      # Check filter name
1850
      croak qq{Filter "$filter" is not registered } . _subname
1851
        if  $state eq 'name' && ! exists $self->filters->{$filter};
1852
      
1853
      # Set filter
1854
      my $f = $state eq 'not_defined' ? undef
1855
        : $state eq 'code' ? $filter
1856
        : $self->filters->{$filter};
1857
      $self->{filter}{$way}{$table}{$column} = $f;
1858
      $self->{filter}{$way}{$table}{"$table.$column"} = $f;
1859
      $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
1860
      $self->{filter}{$way}{$table}{"${table}-$column"} = $f;
1861
    }
1862
  }
1863
  
1864
  return $self;
updated pod
Yuki Kimoto authored on 2011-06-21
1865
}
1866

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1867
# DEPRECATED!
1868
has 'data_source';
1869
has dbi_options => sub { {} };
1870
has filter_check  => 1;
1871
has 'reserved_word_quote';
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1872
has dbi_option => sub { {} };
1873
has default_dbi_option => sub {
cleanup
Yuki Kimoto authored on 2012-01-20
1874
  warn "default_dbi_option is DEPRECATED! use default_option instead";
1875
  return shift->default_option;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1876
};
1877

            
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
1878
# DEPRECATED
1879
sub tag_parse {
cleanup
Yuki Kimoto authored on 2012-01-20
1880
 my $self = shift;
1881
 warn "tag_parse is DEPRECATED! use \$ENV{DBIX_CUSTOM_TAG_PARSE} " .
1882
   "environment variable";
1883
  if (@_) {
1884
    $self->{tag_parse} = $_[0];
1885
    return $self;
1886
  }
1887
  return $self->{tag_parse};
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
1888
}
1889

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1890
# DEPRECATED!
1891
sub method {
cleanup
Yuki Kimoto authored on 2012-01-20
1892
  warn "method is DEPRECATED! use helper instead";
1893
  return shift->helper(@_);
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1894
}
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1895

            
1896
# DEPRECATED!
1897
sub assign_param {
cleanup
Yuki Kimoto authored on 2012-01-20
1898
  my $self = shift;
1899
  warn "assing_param is DEPRECATED! use assign_clause instead";
1900
  return $self->assign_clause(@_);
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1901
}
1902

            
1903
# DEPRECATED
1904
sub update_param {
cleanup
Yuki Kimoto authored on 2012-01-20
1905
  my ($self, $param, $opts) = @_;
1906
  
1907
  warn "update_param is DEPRECATED! use assign_clause instead.";
1908
  
1909
  # Create update parameter tag
1910
  my $tag = $self->assign_clause($param, $opts);
1911
  $tag = "set $tag" unless $opts->{no_set};
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1912

            
cleanup
Yuki Kimoto authored on 2012-01-20
1913
  return $tag;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1914
}
1915

            
updated pod
Yuki Kimoto authored on 2011-06-21
1916
# DEPRECATED!
1917
sub create_query {
cleanup
Yuki Kimoto authored on 2012-01-20
1918
  warn "create_query is DEPRECATED! use query option of each method";
1919
  shift->_create_query(@_);
updated pod
Yuki Kimoto authored on 2011-06-21
1920
}
1921

            
cleanup
Yuki Kimoto authored on 2011-06-13
1922
# DEPRECATED!
1923
sub apply_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
1924
  my $self = shift;
1925
  
1926
  warn "apply_filter is DEPRECATED!";
1927
  return $self->_apply_filter(@_);
cleanup
Yuki Kimoto authored on 2011-06-13
1928
}
1929

            
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1930
# DEPRECATED!
1931
sub select_at {
cleanup
Yuki Kimoto authored on 2012-01-20
1932
  my ($self, %opt) = @_;
1933

            
1934
  warn "select_at is DEPRECATED! use select method id option instead";
1935

            
1936
  # Options
1937
  my $primary_keys = delete $opt{primary_key};
1938
  my $where = delete $opt{where};
1939
  my $param = delete $opt{param};
1940
  
1941
  # Table
1942
  croak qq{"table" option must be specified } . _subname
1943
    unless $opt{table};
1944
  my $table = ref $opt{table} ? $opt{table}->[-1] : $opt{table};
1945
  
1946
  # Create where parameter
1947
  my $where_param = $self->_id_to_param($where, $primary_keys);
1948
  
1949
  return $self->select(where => $where_param, %opt);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1950
}
1951

            
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1952
# DEPRECATED!
1953
sub delete_at {
cleanup
Yuki Kimoto authored on 2012-01-20
1954
  my ($self, %opt) = @_;
updated pod
Yuki Kimoto authored on 2011-06-08
1955

            
cleanup
Yuki Kimoto authored on 2012-01-20
1956
  warn "delete_at is DEPRECATED! use delete method id option instead";
1957
  
1958
  # Options
1959
  my $primary_keys = delete $opt{primary_key};
1960
  my $where = delete $opt{where};
1961
  
1962
  # Create where parameter
1963
  my $where_param = $self->_id_to_param($where, $primary_keys);
1964
  
1965
  return $self->delete(where => $where_param, %opt);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1966
}
1967

            
cleanup
Yuki Kimoto authored on 2011-06-08
1968
# DEPRECATED!
1969
sub update_at {
cleanup
Yuki Kimoto authored on 2012-01-20
1970
  my $self = shift;
1971

            
1972
  warn "update_at is DEPRECATED! use update method id option instead";
1973
  
1974
  # Options
1975
  my $param;
1976
  $param = shift if @_ % 2;
1977
  my %opt = @_;
1978
  my $primary_keys = delete $opt{primary_key};
1979
  my $where = delete $opt{where};
1980
  my $p = delete $opt{param} || {};
1981
  $param  ||= $p;
1982
  
1983
  # Create where parameter
1984
  my $where_param = $self->_id_to_param($where, $primary_keys);
1985
  
1986
  return $self->update(where => $where_param, param => $param, %opt);
cleanup
Yuki Kimoto authored on 2011-06-08
1987
}
1988

            
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1989
# DEPRECATED!
1990
sub insert_at {
cleanup
Yuki Kimoto authored on 2012-01-20
1991
  my $self = shift;
1992
  
1993
  warn "insert_at is DEPRECATED! use insert method id option instead";
1994
  
1995
  # Options
1996
  my $param;
1997
  $param = shift if @_ % 2;
1998
  my %opt = @_;
1999
  my $primary_key = delete $opt{primary_key};
2000
  $primary_key = [$primary_key] unless ref $primary_key;
2001
  my $where = delete $opt{where};
2002
  my $p = delete $opt{param} || {};
2003
  $param  ||= $p;
2004
  
2005
  # Create where parameter
2006
  my $where_param = $self->_id_to_param($where, $primary_key);
2007
  $param = $self->merge_param($where_param, $param);
2008
  
2009
  return $self->insert(param => $param, %opt);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
2010
}
2011

            
added warnings
Yuki Kimoto authored on 2011-06-07
2012
# DEPRECATED!
2013
sub register_tag {
cleanup
Yuki Kimoto authored on 2012-01-20
2014
  my $self = shift;
2015
  
2016
  warn "register_tag is DEPRECATED!";
2017
  
2018
  # Merge tag
2019
  my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
2020
  $self->{_tags} = {%{$self->{_tags} || {}}, %$tags};
2021
  
2022
  return $self;
test cleanup
Yuki Kimoto authored on 2011-08-10
2023
}
2024

            
2025
# DEPRECATED!
2026
sub register_tag_processor {
cleanup
Yuki Kimoto authored on 2012-01-20
2027
  my $self = shift;
2028
  warn "register_tag_processor is DEPRECATED!";
2029
  # Merge tag
2030
  my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
2031
  $self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}};
2032
  return $self;
added warnings
Yuki Kimoto authored on 2011-06-07
2033
}
2034

            
cleanup
Yuki Kimoto authored on 2011-01-25
2035
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
2036
sub default_bind_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
2037
  my $self = shift;
2038
  
2039
  warn "default_bind_filter is DEPRECATED!";
2040
  
2041
  if (@_) {
2042
    my $fname = $_[0];
added warnings
Yuki Kimoto authored on 2011-06-07
2043
    
cleanup
Yuki Kimoto authored on 2012-01-20
2044
    if (@_ && !$fname) {
2045
      $self->{default_out_filter} = undef;
cleanup
Yuki Kimoto authored on 2011-01-12
2046
    }
cleanup
Yuki Kimoto authored on 2012-01-20
2047
    else {
2048
      croak qq{Filter "$fname" is not registered}
2049
        unless exists $self->filters->{$fname};
2050
  
2051
      $self->{default_out_filter} = $self->filters->{$fname};
2052
    }
2053
    return $self;
2054
  }
2055
  
2056
  return $self->{default_out_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
2057
}
2058

            
cleanup
Yuki Kimoto authored on 2011-01-25
2059
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
2060
sub default_fetch_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
2061
  my $self = shift;
added warnings
Yuki Kimoto authored on 2011-06-07
2062

            
cleanup
Yuki Kimoto authored on 2012-01-20
2063
  warn "default_fetch_filter is DEPRECATED!";
2064
  
2065
  if (@_) {
2066
    my $fname = $_[0];
many changed
Yuki Kimoto authored on 2011-01-23
2067

            
cleanup
Yuki Kimoto authored on 2012-01-20
2068
    if (@_ && !$fname) {
2069
      $self->{default_in_filter} = undef;
2070
    }
2071
    else {
2072
      croak qq{Filter "$fname" is not registered}
2073
        unless exists $self->filters->{$fname};
2074
  
2075
      $self->{default_in_filter} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2011-01-12
2076
    }
2077
    
cleanup
Yuki Kimoto authored on 2012-01-20
2078
    return $self;
2079
  }
2080
  
2081
  return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
2082
}
2083

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2084
# DEPRECATED!
2085
sub insert_param {
cleanup
Yuki Kimoto authored on 2012-01-20
2086
  my $self = shift;
2087
  warn "insert_param is DEPRECATED! use values_clause instead";
2088
  return $self->values_clause(@_);
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2089
}
2090

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2091
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
2092
sub insert_param_tag {
cleanup
Yuki Kimoto authored on 2012-01-20
2093
  warn "insert_param_tag is DEPRECATED! " .
2094
    "use insert_param instead!";
2095
  return shift->insert_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2096
}
2097

            
2098
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
2099
sub update_param_tag {
cleanup
Yuki Kimoto authored on 2012-01-20
2100
  warn "update_param_tag is DEPRECATED! " .
2101
    "use update_param instead";
2102
  return shift->update_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2103
}
cleanup
Yuki Kimoto authored on 2011-03-08
2104
# DEPRECATED!
2105
sub _push_relation {
cleanup
Yuki Kimoto authored on 2012-01-20
2106
  my ($self, $sql, $tables, $relation, $need_where) = @_;
2107
  
2108
  if (keys %{$relation || {}}) {
2109
    $$sql .= $need_where ? 'where ' : 'and ';
2110
    for my $rcolumn (keys %$relation) {
fixed relation full qualifie...
Yuki Kimoto authored on 2012-03-01
2111
      my ($table1) = $rcolumn =~ /^(.+)\.(.+)$/;
2112
      my ($table2) = $relation->{$rcolumn} =~ /^(.+)\.(.+)$/;
cleanup
Yuki Kimoto authored on 2012-01-20
2113
      push @$tables, ($table1, $table2);
2114
      $$sql .= "$rcolumn = " . $relation->{$rcolumn} .  'and ';
cleanup
Yuki Kimoto authored on 2011-03-08
2115
    }
cleanup
Yuki Kimoto authored on 2012-01-20
2116
  }
2117
  $$sql =~ s/and $/ /;
cleanup
Yuki Kimoto authored on 2011-03-08
2118
}
2119

            
2120
# DEPRECATED!
2121
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2012-01-20
2122
  my ($self, $tables, $relation) = @_;
2123
  
2124
  if (keys %{$relation || {}}) {
2125
    for my $rcolumn (keys %$relation) {
fixed relation full qualifie...
Yuki Kimoto authored on 2012-03-01
2126
      my ($table1) = $rcolumn =~ /^(.+)\.(.+)$/;
2127
      my ($table2) = $relation->{$rcolumn} =~ /^(.+)\.(.+)$/;
cleanup
Yuki Kimoto authored on 2012-01-20
2128
      my $table1_exists;
2129
      my $table2_exists;
2130
      for my $table (@$tables) {
2131
        $table1_exists = 1 if $table eq $table1;
2132
        $table2_exists = 1 if $table eq $table2;
2133
      }
2134
      unshift @$tables, $table1 unless $table1_exists;
2135
      unshift @$tables, $table2 unless $table2_exists;
2136
    }
2137
  }
cleanup
Yuki Kimoto authored on 2011-03-08
2138
}
2139

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2142
=head1 NAME
2143

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2148
  use DBIx::Custom;
2149
  
2150
  # Connect
2151
  my $dbi = DBIx::Custom->connect(
2152
    dsn => "dbi:mysql:database=dbname",
2153
    user => 'ken',
2154
    password => '!LFKD%$&',
2155
    option => {mysql_enable_utf8 => 1}
2156
  );
2157

            
2158
  # Insert 
2159
  $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
2160
  
2161
  # Update 
2162
  $dbi->update({title => 'Perl', author => 'Ken'}, table  => 'book',
2163
    where  => {id => 5});
2164
  
2165
  # Delete
2166
  $dbi->delete(table  => 'book', where => {author => 'Ken'});
2167

            
2168
  # Select
2169
  my $result = $dbi->select(table  => 'book',
2170
    column => ['title', 'author'], where  => {author => 'Ken'});
2171

            
2172
  # Select, more complex
2173
  my $result = $dbi->select(
2174
    table  => 'book',
2175
    column => [
2176
      {book => [qw/title author/]},
2177
      {company => ['name']}
2178
    ],
2179
    where  => {'book.author' => 'Ken'},
2180
    join => ['left outer join company on book.company_id = company.id'],
2181
    append => 'order by id limit 5'
2182
  );
2183
  
2184
  # Fetch
2185
  while (my $row = $result->fetch) {
2186
      
2187
  }
2188
  
2189
  # Fetch as hash
2190
  while (my $row = $result->fetch_hash) {
2191
      
2192
  }
2193
  
2194
  # Execute SQL with parameter.
2195
  $dbi->execute(
2196
    "select id from book where author = :author and title like :title",
2197
    {author => 'ken', title => '%Perl%'}
2198
  );
2199
  
fix heading typos
Terrence Brannon authored on 2011-08-17
2200
=head1 DESCRIPTION
removed reconnect method
yuki-kimoto authored on 2010-05-28
2201

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

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

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

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

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2217
Named place holder support
2218

            
2219
=item *
2220

            
cleanup
Yuki Kimoto authored on 2011-07-29
2221
Model support
2222

            
2223
=item *
2224

            
2225
Connection manager support
2226

            
2227
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
2228

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

            
2233
=item *
2234

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

            
2237
=item *
2238

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

            
2241
=back
pod fix
Yuki Kimoto authored on 2011-01-21
2242

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2250
Module documentations - 
cleanup
Yuki Kimoto authored on 2011-07-29
2251
L<DBIx::Custom::Result>,
2252
L<DBIx::Custom::Query>,
2253
L<DBIx::Custom::Where>,
2254
L<DBIx::Custom::Model>,
2255
L<DBIx::Custom::Order>
updated document
yuki-kimoto authored on 2010-08-08
2256

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

            
added EXPERIMETNAL aysnc_con...
Yuki Kimoto authored on 2012-02-10
2259
=head2 C<async_conf> EXPERIMENTAL
2260

            
2261
  my $async_conf = $dbi->async_conf;
2262
  $dbi = $dbi->async_conf($conf);
2263

            
2264
Setting when C<async> option is used.
2265

            
2266
  # MySQL
2267
  $dbi->async_conf({
2268
    prepare_attr => {async => 1},
2269
    fh => sub { shift->dbh->mysql_fd }
2270
  })
2271

            
2272
C<prepare_attr> is DBI's C<prepare> method second argument,
2273
C<fh> is callback that return file handle to watch.
2274

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2277
  my $connector = $dbi->connector;
2278
  $dbi = $dbi->connector($connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2279

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2286
  my $connector = DBIx::Connector->new(
2287
    "dbi:mysql:database=$database",
2288
    $user,
2289
    $password,
2290
    DBIx::Custom->new->default_option
2291
  );
2292
  
2293
  my $dbi = DBIx::Custom->connect(connector => $connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2294

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2298
  my $dbi = DBIx::Custom->connect(
2299
    dsn => $dsn, user => $user, password => $password, connector => 1);
2300
  
2301
  my $connector = $dbi->connector; # DBIx::Connector
cleanup
Yuki Kimoto authored on 2011-08-16
2302

            
2303
Note that L<DBIx::Connector> must be installed.
2304

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2307
  my $dsn = $dbi->dsn;
2308
  $dbi = $dbi->dsn("DBI:mysql:database=dbname");
packaging one directory
yuki-kimoto authored on 2009-11-16
2309

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2314
  my $default_option = $dbi->default_option;
2315
  $dbi = $dbi->default_option($default_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2316

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2320
  {
2321
    RaiseError => 1,
2322
    PrintError => 0,
2323
    AutoCommit => 1,
2324
  }
packaging one directory
yuki-kimoto authored on 2009-11-16
2325

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2328
  my $exclude_table = $dbi->exclude_table;
2329
  $dbi = $dbi->exclude_table(qr/pg_/);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2330

            
2331
Excluded table regex.
2332
C<each_column>, C<each_table>, C<type_rule>,
2333
and C<setup_model> methods ignore matching tables.
2334

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2337
  my $filters = $dbi->filters;
2338
  $dbi = $dbi->filters(\%filters);
packaging one directory
yuki-kimoto authored on 2009-11-16
2339

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2344
  my $last_sql = $dbi->last_sql;
2345
  $dbi = $dbi->last_sql($last_sql);
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
2346

            
2347
Get last successed SQL executed by C<execute> method.
2348

            
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
2349
=head2 C<now>
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2350

            
cleanup
Yuki Kimoto authored on 2012-01-20
2351
  my $now = $dbi->now;
2352
  $dbi = $dbi->now($now);
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2353

            
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
2354
Code reference which return current time, default to the following code reference.
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2355

            
cleanup
Yuki Kimoto authored on 2012-01-20
2356
  sub {
2357
    my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
2358
    $mon++;
2359
    $year += 1900;
2360
    return sprintf("%04d-%02d-%02d %02d:%02d:%02d");
2361
  }
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2362

            
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
2363
This return the time like C<2011-10-14 05:05:27>.
2364

            
2365
This is used by C<insert> method's C<created_at> option and C<updated_at> option,
2366
and C<update> method's C<updated_at> option.
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2367

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2370
  my $models = $dbi->models;
2371
  $dbi = $dbi->models(\%models);
add models() attribute
Yuki Kimoto authored on 2011-02-21
2372

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2377
  my $option = $dbi->option;
2378
  $dbi = $dbi->option($option);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2379

            
2380
L<DBI> option, used when C<connect> method is executed.
2381
Each value in option override the value of C<default_option>.
2382

            
cleanup
yuki-kimoto authored on 2010-10-17
2383
=head2 C<password>
2384

            
cleanup
Yuki Kimoto authored on 2012-01-20
2385
  my $password = $dbi->password;
2386
  $dbi = $dbi->password('lkj&le`@s');
cleanup
yuki-kimoto authored on 2010-10-17
2387

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2392
  my $builder = $dbi->query_builder;
added commit method
yuki-kimoto authored on 2010-05-27
2393

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2398
  my quote = $dbi->quote;
2399
  $dbi = $dbi->quote('"');
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
2400

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

            
cleanup
Yuki Kimoto authored on 2011-07-30
2405
You can set quote pair.
2406

            
cleanup
Yuki Kimoto authored on 2012-01-20
2407
  $dbi->quote('[]');
cleanup
Yuki Kimoto authored on 2011-07-30
2408

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2411
  my $result_class = $dbi->result_class;
2412
  $dbi = $dbi->result_class('DBIx::Custom::Result');
cleanup
yuki-kimoto authored on 2010-08-05
2413

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2418
  my $safety_character = $dbi->safety_character;
2419
  $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2420

            
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
2421
Regex of safety character for table and column name, default to 'a-zA-Z_'.
2422
Note that you don't have to specify like '[a-zA-Z_]'.
update pod
Yuki Kimoto authored on 2011-01-27
2423

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2426
  my $separator = $dbi->separator;
2427
  $dbi = $dbi->separator('-');
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
2428

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2437
  my $tag_parse = $dbi->tag_parse(0);
2438
  $dbi = $dbi->tag_parse;
added tag_parse attribute
Yuki Kimoto authored on 2011-06-28
2439

            
2440
Enable DEPRECATED tag parsing functionality, default to 1.
2441
If you want to disable tag parsing functionality, set to 0.
2442

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2445
  my $user = $dbi->user;
2446
  $dbi = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
2447

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2452
  my $user_column_info = $dbi->user_column_info;
2453
  $dbi = $dbi->user_column_info($user_column_info);
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2454

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2457
  [
2458
    {table => 'book', column => 'title', info => {...}},
2459
    {table => 'author', column => 'name', info => {...}}
2460
  ]
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2461

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2464
  my $user_column_info
2465
    = $dbi->get_column_info(exclude_table => qr/^system/);
2466
  $dbi->user_column_info($user_column_info);
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2467

            
2468
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
2469
to find column info. this is very fast.
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2470

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2473
  my $user_table_info = $dbi->user_table_info;
2474
  $dbi = $dbi->user_table_info($user_table_info);
added test
Yuki Kimoto authored on 2011-08-16
2475

            
2476
You can set the following data.
2477

            
cleanup
Yuki Kimoto authored on 2012-01-20
2478
  [
2479
    {table => 'book', info => {...}},
2480
    {table => 'author', info => {...}}
2481
  ]
added test
Yuki Kimoto authored on 2011-08-16
2482

            
2483
Usually, you can set return value of C<get_table_info>.
2484

            
cleanup
Yuki Kimoto authored on 2012-01-20
2485
  my $user_table_info = $dbi->get_table_info(exclude => qr/^system/);
2486
  $dbi->user_table_info($user_table_info);
added test
Yuki Kimoto authored on 2011-08-16
2487

            
2488
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2489
to find table info.
2490

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2499
  print $dbi->available_datatype;
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
2500

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2506
  print $dbi->available_typename;
added EXPERIMENTAL available...
Yuki Kimoto authored on 2011-06-14
2507

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2513
  my $assign_clause = $dbi->assign_clause({title => 'a', age => 2});
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2514

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2517
  title = :title, author = :author
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
2518

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

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

            
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
2523
=head2 C<async> EXPERIMENTAL
2524

            
2525
  async => sub {
2526
    my ($dbi, $result) = @_;
2527
    ...
2528
  };
2529

            
2530
Database async access. L<AnyEvent> is required.
2531

            
2532
This is C<mysql> async access example.
2533

            
2534
  use AnyEvent;
2535

            
2536
  my $cond = AnyEvent->condvar;
2537

            
2538
  my $timer = AnyEvent->timer(
2539
    interval => 1,
2540
    cb => sub { 1 }
2541
  );
2542

            
2543
  my $count = 0;
2544

            
2545
  $dbi->execute('SELECT SLEEP(1), 3', undef,
2546
    prepare_attr => {async => 1}, statement => 'select',
2547
    async => sub {
2548
      my ($dbi, $result) = @_;
2549
      my $row = $result->fetch_one;
2550
      is($row->[1], 3, 'before');
2551
      $cond->send if ++$count == 2;
2552
    }
2553
  );
2554

            
2555
  $dbi->select('key1', table => 'table1', prepare_attr => {async => 1},
2556
    async => sub {
2557
      my ($dbi, $result) = @_;
2558
      my $row = $result->fetch_one;
2559
      is($row->[0], 1, 'after1');
2560
      $dbi->select('key1', table => 'table1', prepare_attr => {async => 1},
2561
        async => sub {
2562
          my ($dbi, $result) = @_;
2563
          my $row = $result->fetch_one;
2564
          is($row->[0], 1, 'after2');
2565
          $cond->send if ++$count == 2;
2566
        }
2567
      )
2568
    }
2569
  );
2570

            
2571
  $cond->recv;
2572

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2575
  my $column = $dbi->column(book => ['author', 'title']);
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
2576

            
2577
Create column clause. The follwoing column clause is created.
2578

            
cleanup
Yuki Kimoto authored on 2012-01-20
2579
  book.author as "book.author",
2580
  book.title as "book.title"
- select() EXPERIMETNAL colu...
Yuki Kimoto authored on 2011-06-08
2581

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2584
  # Separator is hyphen
2585
  $dbi->separator('-');
2586
  
2587
  book.author as "book-author",
2588
  book.title as "book-title"
2589
  
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2590
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
2591

            
cleanup
Yuki Kimoto authored on 2012-01-20
2592
  my $dbi = DBIx::Custom->connect(
2593
    dsn => "dbi:mysql:database=dbname",
2594
    user => 'ken',
2595
    password => '!LFKD%$&',
2596
    option => {mysql_enable_utf8 => 1}
2597
  );
update pod
Yuki Kimoto authored on 2011-03-13
2598

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2607
  my $count = $dbi->count(table => 'book');
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
2608

            
2609
Get rows count.
2610

            
2611
Options is same as C<select> method's ones.
2612

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2615
  my $model = $dbi->create_model(
2616
    table => 'book',
2617
    primary_key => 'id',
2618
    join => [
2619
      'inner join company on book.comparny_id = company.id'
2620
    ],
2621
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2622

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2626
 $dbi->model('book')->select(...);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2627

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2630
  my $dbh = $dbi->dbh;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
2631

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2637
  $dbi->delete(table => 'book', where => {title => 'Perl'});
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2638

            
2639
Execute delete statement.
2640

            
2641
The following opitons are available.
2642

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

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

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

            
2650
=item C<id>
2651

            
cleanup
Yuki Kimoto authored on 2012-01-20
2652
  id => 4
2653
  id => [4, 5]
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2654

            
2655
ID corresponding to C<primary_key>.
2656
You can delete rows by C<id> and C<primary_key>.
2657

            
cleanup
Yuki Kimoto authored on 2012-01-20
2658
  $dbi->delete(
2659
    primary_key => ['id1', 'id2'],
2660
    id => [4, 5],
2661
    table => 'book',
2662
  );
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2663

            
2664
The above is same as the followin one.
2665

            
cleanup
Yuki Kimoto authored on 2012-01-20
2666
  $dbi->delete(where => {id1 => 4, id2 => 5}, table => 'book');
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2667

            
2668
=item C<prefix>
2669

            
cleanup
Yuki Kimoto authored on 2012-01-20
2670
  prefix => 'some'
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2671

            
2672
prefix before table name section.
2673

            
cleanup
Yuki Kimoto authored on 2012-01-20
2674
  delete some from book
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2675

            
2676
=item C<table>
2677

            
cleanup
Yuki Kimoto authored on 2012-01-20
2678
  table => 'book'
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2679

            
2680
Table name.
2681

            
2682
=item C<where>
2683

            
2684
Same as C<select> method's C<where> option.
2685

            
2686
=back
2687

            
2688
=head2 C<delete_all>
2689

            
cleanup
Yuki Kimoto authored on 2012-01-20
2690
  $dbi->delete_all(table => $table);
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2691

            
2692
Execute delete statement for all rows.
2693
Options is same as C<delete>.
2694

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2697
  $dbi->each_column(
2698
    sub {
2699
      my ($dbi, $table, $column, $column_info) = @_;
2700
      
2701
      my $type = $column_info->{TYPE_NAME};
2702
      
2703
      if ($type eq 'DATE') {
2704
          # ...
2705
      }
2706
    }
2707
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2708

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

            
2714
If C<user_column_info> is set, C<each_column> method use C<user_column_info>
2715
infromation, you can improve the performance of C<each_column> in
2716
the following way.
2717

            
cleanup
Yuki Kimoto authored on 2012-01-20
2718
  my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/);
2719
  $dbi->user_column_info($column_info);
2720
  $dbi->each_column(sub { ... });
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
2721

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2724
  $dbi->each_table(
2725
    sub {
2726
      my ($dbi, $table, $table_info) = @_;
2727
      
2728
      my $table_name = $table_info->{TABLE_NAME};
2729
    }
2730
  );
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
2731

            
improved pod
Yuki Kimoto authored on 2011-10-14
2732
Iterate all table informationsfrom in database.
2733
Argument is callback which is executed when one table is found.
2734
Callback receive three arguments, C<DBIx::Custom object>, C<table name>,
2735
C<table information>.
2736

            
2737
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2738
infromation, you can improve the performance of C<each_table> in
2739
the following way.
2740

            
cleanup
Yuki Kimoto authored on 2012-01-20
2741
  my $table_infos = $dbi->get_table_info(exclude => qr/^system_/);
2742
  $dbi->user_table_info($table_info);
2743
  $dbi->each_table(sub { ... });
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
2744

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2747
  my $result = $dbi->execute(
2748
    "select * from book where title = :title and author like :author",
2749
    {title => 'Perl', author => '%Ken%'}
2750
  );
updated pod
Yuki Kimoto authored on 2011-06-21
2751

            
cleanup
Yuki Kimoto authored on 2012-01-20
2752
  my $result = $dbi->execute(
2753
    "select * from book where title = :book.title and author like :book.author",
2754
    {'book.title' => 'Perl', 'book.author' => '%Ken%'}
2755
  );
update pod
Yuki Kimoto authored on 2011-03-13
2756

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

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2763
Named placeholder such as C<:title> is replaced by placeholder C<?>.
cleanup
Yuki Kimoto authored on 2012-01-20
2764
  
2765
  # Original
2766
  select * from book where title = :title and author like :author
2767
  
2768
  # Replaced
2769
  select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2770

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2774
  # Original
2775
  select * from book where :title{=} and :author{like}
2776
  
2777
  # Replaced
2778
  select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2779

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2784
  select * from where title = "aa\\:bb";
fixed named placeholder bug ...
Yuki Kimoto authored on 2011-08-01
2785

            
cleanup
Yuki Kimoto authored on 2011-10-20
2786
B<OPTIONS>
2787

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

            
2790
=over 4
2791

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

            
2794
You can filter sql after the sql is build.
2795

            
cleanup
Yuki Kimoto authored on 2012-01-20
2796
  after_build_sql => $code_ref
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2797

            
2798
The following one is one example.
2799

            
cleanup
Yuki Kimoto authored on 2012-01-20
2800
  $dbi->select(
2801
    table => 'book',
2802
    column => 'distinct(name)',
2803
    after_build_sql => sub {
2804
      "select count(*) from ($_[0]) as t1"
2805
    }
2806
  );
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2807

            
2808
The following SQL is executed.
2809

            
cleanup
Yuki Kimoto authored on 2012-01-20
2810
  select count(*) from (select distinct(name) from book) as t1;
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2811

            
cleanup
Yuki Kimoto authored on 2011-10-20
2812
=item C<append>
2813

            
cleanup
Yuki Kimoto authored on 2012-01-20
2814
  append => 'order by name'
cleanup
Yuki Kimoto authored on 2011-10-20
2815

            
2816
Append some statement after SQL.
2817

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
2818
=item C<prepare_attr> EXPERIMENTAL
2819

            
2820
  prepare_attr => {async => 1}
2821

            
2822
Statemend handle attributes,
2823
this is L<DBI>'s C<prepare> method second argument.
2824

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

            
2827
Specify database bind data type.
2828

            
cleanup
Yuki Kimoto authored on 2012-01-20
2829
  bind_type => [image => DBI::SQL_BLOB]
2830
  bind_type => [[qw/image audio/] => DBI::SQL_BLOB]
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2831

            
2832
This is used to bind parameter by C<bind_param> of statment handle.
2833

            
cleanup
Yuki Kimoto authored on 2012-01-20
2834
  $sth->bind_param($pos, $value, DBI::SQL_BLOB);
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2835

            
update pod
Yuki Kimoto authored on 2011-03-13
2836
=item C<filter>
cleanup
Yuki Kimoto authored on 2012-01-20
2837
  
2838
  filter => {
2839
    title  => sub { uc $_[0] }
2840
    author => sub { uc $_[0] }
2841
  }
2842

            
2843
  # Filter name
2844
  filter => {
2845
    title  => 'upper_case',
2846
    author => 'upper_case'
2847
  }
2848
      
2849
  # At once
2850
  filter => [
2851
    [qw/title author/]  => sub { uc $_[0] }
2852
  ]
updated pod
Yuki Kimoto authored on 2011-06-09
2853

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2861
  query => 1
updated document
Yuki Kimoto authored on 2011-06-09
2862

            
DBIx::Custom::Query is DEPRE...
Yuki Kimoto authored on 2011-11-15
2863
C<execute> method return hash reference which contain SQL and column
2864
infromation
updated pod
Yuki Kimoto authored on 2011-06-21
2865

            
cleanup
Yuki Kimoto authored on 2012-01-20
2866
  my $sql = $query->{sql};
2867
  my $columns = $query->{columns};
2868
  
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
2869
=item C<reuse>
cleanup
Yuki Kimoto authored on 2012-01-20
2870
  
2871
  reuse => $hash_ref
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
2872

            
2873
Reuse query object if the hash reference variable is set.
cleanup
Yuki Kimoto authored on 2012-01-20
2874
  
2875
  my $queries = {};
2876
  $dbi->execute($sql, $param, reuse => $queries);
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
2877

            
2878
This will improved performance when you want to execute same query repeatedly
2879
because generally creating query object is slow.
2880

            
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2881
=item C<primary_key>
2882

            
cleanup
Yuki Kimoto authored on 2012-01-20
2883
  primary_key => 'id'
2884
  primary_key => ['id1', 'id2']
cleanup
Yuki Kimoto authored on 2011-10-20
2885

            
execute method id option is ...
Yuki Kimoto authored on 2011-10-27
2886
Priamry key. This is used for C<id> option.
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2887

            
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
2888
=item C<select> EXPERIMETAL
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
2889

            
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
2890
  select => 1
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
2891

            
added async asccess and stat...
Yuki Kimoto authored on 2012-01-29
2892
If you set C<select> to 1, this statement become select statement
2893
and return value is always L<DBIx::Custom::Result> object.
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
2894

            
updated pod
Yuki Kimoto authored on 2011-06-09
2895
=item C<table>
cleanup
Yuki Kimoto authored on 2012-01-20
2896
  
2897
  table => 'author'
updated pod
Yuki Kimoto authored on 2011-06-09
2898

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2903
  $dbi->execute("select * from book where title = :title and author = :author",
2904
    {title => 'Perl', author => 'Ken', table => 'book');
updated pod
Yuki Kimoto authored on 2011-06-09
2905

            
cleanup
Yuki Kimoto authored on 2012-01-20
2906
  # Same
2907
  $dbi->execute(
2908
    "select * from book where title = :book.title and author = :book.author",
2909
    {title => 'Perl', author => 'Ken');
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2910

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2913
  table_alias => {user => 'worker'}
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-06-27
2914

            
2915
Table alias. Key is real table name, value is alias table name.
2916
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2917
on alias table name.
2918

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2921
  type_rule_off => 1
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2922

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2927
  type_rule1_off => 1
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
2928

            
2929
Turn C<into1> type rule off.
2930

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2933
  type_rule2_off => 1
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
2934

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2941
  my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/);
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2942

            
2943
get column infomation except for one which match C<exclude_table> pattern.
2944

            
cleanup
Yuki Kimoto authored on 2012-01-20
2945
  [
2946
    {table => 'book', column => 'title', info => {...}},
2947
    {table => 'author', column => 'name' info => {...}}
2948
  ]
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2949

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2952
  my $table_infos = $dbi->get_table_info(exclude => qr/^system_/);
update pod
Yuki Kimoto authored on 2011-03-13
2953

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2956
  [
2957
    {table => 'book', info => {...}},
2958
    {table => 'author', info => {...}}
2959
  ]
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2960

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2965
  $dbi->helper(
2966
    find_or_create   => sub {
2967
      my $self = shift;
2968
      
2969
      # Process
2970
    },
2971
    ...
2972
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2973

            
2974
Register helper. These helper is called directly from L<DBIx::Custom> object.
2975

            
cleanup
Yuki Kimoto authored on 2012-01-20
2976
  $dbi->find_or_create;
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2977

            
cleanup
yuki-kimoto authored on 2010-10-17
2978
=head2 C<insert>
2979

            
cleanup
Yuki Kimoto authored on 2012-01-20
2980
  $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
update pod
Yuki Kimoto authored on 2011-03-13
2981

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2988
  {date => \"NOW()"}
insert and update method's p...
Yuki Kimoto authored on 2011-07-29
2989

            
updated pod
Yuki Kimoto authored on 2011-11-25
2990
You can pass multiple parameters, this is very fast.
2991

            
cleanup
Yuki Kimoto authored on 2012-01-20
2992
  $dbi->insert(
2993
    [
2994
      {title => 'Perl', author => 'Ken'},
2995
      {title => 'Ruby', author => 'Tom'}
2996
    ],
2997
    table  => 'book'
2998
  );
updated pod
Yuki Kimoto authored on 2011-11-25
2999

            
3000
In multiple insert, you can't use C<id> option.
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
3001
and only first parameter is used to create sql.
updated pod
Yuki Kimoto authored on 2011-11-25
3002

            
cleanup
Yuki Kimoto authored on 2011-10-20
3003
B<options>
3004

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

            
cleanup
Yuki Kimoto authored on 2011-06-09
3008
=over 4
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
3009

            
3010
=item C<bulk_insert>
added EXPERIMENTAL bulk_inse...
Yuki Kimoto authored on 2011-11-27
3011

            
cleanup
Yuki Kimoto authored on 2012-01-20
3012
  bulk_insert => 1
added EXPERIMENTAL bulk_inse...
Yuki Kimoto authored on 2011-11-27
3013

            
3014
bulk insert is executed if database support bulk insert and 
3015
multiple parameters is passed to C<insert>.
3016
The SQL like the following one is executed.
3017

            
cleanup
Yuki Kimoto authored on 2012-01-20
3018
  insert into book (id, title) values (?, ?), (?, ?);
added EXPERIMENTAL bulk_inse...
Yuki Kimoto authored on 2011-11-27
3019

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3020
=item C<ctime>
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3021

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3022
  ctime => 'created_time'
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3023

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3024
Created time column name. time when row is created is set to the column.
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3025
default time format is "YYYY-mm-dd HH:MM:SS", which can be changed by
3026
C<now> attribute.
3027

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3030
  id => 4
3031
  id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
3032

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3036
  $dbi->insert(
3037
    {title => 'Perl', author => 'Ken'}
3038
    primary_key => ['id1', 'id2'],
3039
    id => [4, 5],
3040
    table => 'book'
3041
  );
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
3042

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3045
  $dbi->insert(
3046
    {id1 => 4, id2 => 5, title => 'Perl', author => 'Ken'},
3047
    table => 'book'
3048
  );
update pod
Yuki Kimoto authored on 2011-03-13
3049

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3052
  prefix => 'or replace'
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
3053

            
3054
prefix before table name section
3055

            
cleanup
Yuki Kimoto authored on 2012-01-20
3056
  insert or replace into book
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
3057

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3060
  table => 'book'
updated document
Yuki Kimoto authored on 2011-06-09
3061

            
3062
Table name.
3063

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3064
=item C<mtime>
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3065

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3066
This option is same as C<update> method C<mtime> option.
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3067

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3070
  wrap => {price => sub { "max($_[0])" }}
updated pod
Yuki Kimoto authored on 2011-09-02
3071

            
3072
placeholder wrapped string.
3073

            
3074
If the following statement
3075

            
cleanup
Yuki Kimoto authored on 2012-01-20
3076
  $dbi->insert({price => 100}, table => 'book',
3077
    {price => sub { "$_[0] + 5" }});
updated pod
Yuki Kimoto authored on 2011-09-02
3078

            
3079
is executed, the following SQL is executed.
3080

            
cleanup
Yuki Kimoto authored on 2012-01-20
3081
  insert into book price values ( ? + 5 );
updated pod
Yuki Kimoto authored on 2011-09-02
3082

            
update pod
Yuki Kimoto authored on 2011-03-13
3083
=back
3084

            
3085
=over 4
3086

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3089
  $dbi->include_model('MyModel');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
3090

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3094
  lib / MyModel.pm
3095
      / MyModel / book.pm
3096
                / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
3097

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3102
  package MyModel;
3103
  use DBIx::Custom::Model -base;
3104
  
3105
  1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
3106

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3111
  package MyModel::book;
3112
  use MyModel -base;
3113
  
3114
  1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
3115

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3118
  package MyModel::company;
3119
  use MyModel -base;
3120
  
3121
  1;
3122
  
updated pod
Yuki Kimoto authored on 2011-06-21
3123
MyModel::book and MyModel::company is included by C<include_model>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
3124

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3127
  my $book_model = $dbi->model('book');
3128
  my $company_model = $dbi->model('company');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
3129

            
support model full-qualified...
Yuki Kimoto authored on 2012-03-01
3130
You can include full-qualified table name like C<main.book>
3131

            
3132
  lib / MyModel.pm
3133
      / MyModel / main / book.pm
3134
                       / company.pm
3135

            
3136
  my $main_book = $self->model('main.book');
3137

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3142
  my $like_value = $dbi->like_value
added EXPERIMENTAL like_valu...
Yuki Kimoto authored on 2011-09-16
3143

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3146
  sub { "%$_[0]%" }
added EXPERIMENTAL like_valu...
Yuki Kimoto authored on 2011-09-16
3147

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3150
  my $mapper = $dbi->mapper(param => $param);
- added EXPERIMENTAL pass at...
Yuki Kimoto authored on 2011-09-02
3151

            
3152
Create a new L<DBIx::Custom::Mapper> object.
3153

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3156
  my $param = $dbi->merge_param({key1 => 1}, {key1 => 1, key2 => 2});
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
3157

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3160
  {key1 => [1, 1], key2 => 2}
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
3161

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3166
  my $model = $dbi->model('book');
update pod
Yuki Kimoto authored on 2011-03-13
3167

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3173
  my $column = $dbi->mycolumn(book => ['author', 'title']);
cleanup
Yuki Kimoto authored on 2011-03-21
3174

            
3175
Create column clause for myself. The follwoing column clause is created.
3176

            
cleanup
Yuki Kimoto authored on 2012-01-20
3177
  book.author as author,
3178
  book.title as title
cleanup
Yuki Kimoto authored on 2011-03-21
3179

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3182
  my $dbi = DBIx::Custom->new(
3183
    dsn => "dbi:mysql:database=dbname",
3184
    user => 'ken',
3185
    password => '!LFKD%$&',
3186
    option => {mysql_enable_utf8 => 1}
3187
  );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
3188

            
3189
Create a new L<DBIx::Custom> object.
3190

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3193
  my $not_exists = $dbi->not_exists;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
3194

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3200
  my $order = $dbi->order;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3201

            
3202
Create a new L<DBIx::Custom::Order> object.
3203

            
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
3204
=head2 C<q>
added EXPERIMENTAL q method
Yuki Kimoto authored on 2011-10-27
3205

            
cleanup
Yuki Kimoto authored on 2012-01-20
3206
  my $quooted = $dbi->q("title");
added EXPERIMENTAL q method
Yuki Kimoto authored on 2011-10-27
3207

            
3208
Quote string by value of C<quote>.
3209

            
cleanup
yuki-kimoto authored on 2010-10-17
3210
=head2 C<register_filter>
3211

            
cleanup
Yuki Kimoto authored on 2012-01-20
3212
  $dbi->register_filter(
3213
    # Time::Piece object to database DATE format
3214
    tp_to_date => sub {
3215
      my $tp = shift;
3216
      return $tp->strftime('%Y-%m-%d');
3217
    },
3218
    # database DATE format to Time::Piece object
3219
    date_to_tp => sub {
3220
      my $date = shift;
3221
      return Time::Piece->strptime($date, '%Y-%m-%d');
3222
    }
3223
  );
3224
  
update pod
Yuki Kimoto authored on 2011-03-13
3225
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
3226

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3229
  my $result = $dbi->select(
3230
    column => ['author', 'title'],
3231
    table  => 'book',
3232
    where  => {author => 'Ken'},
3233
  );
3234
  
updated document
Yuki Kimoto authored on 2011-06-09
3235
Execute select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3236

            
- select method can receive ...
Yuki Kimoto authored on 2011-11-18
3237
You can pass odd number arguments. first argument is C<column>.
3238

            
cleanup
Yuki Kimoto authored on 2012-01-20
3239
  my $result = $dbi->select(['author', 'title'], table => 'book');
- select method can receive ...
Yuki Kimoto authored on 2011-11-18
3240

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3248
=item C<column>
cleanup
Yuki Kimoto authored on 2012-01-20
3249
  
3250
  column => 'author'
3251
  column => ['author', 'title']
updated document
Yuki Kimoto authored on 2011-06-09
3252

            
3253
Column clause.
cleanup
Yuki Kimoto authored on 2012-01-20
3254
  
updated document
Yuki Kimoto authored on 2011-06-09
3255
if C<column> is not specified, '*' is set.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3256

            
cleanup
Yuki Kimoto authored on 2012-01-20
3257
  column => '*'
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3258

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3261
  column => [
3262
    {book => [qw/author title/]},
3263
    {person => [qw/name age/]}
3264
  ]
updated pod
Yuki Kimoto authored on 2011-06-07
3265

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3268
  book.author as "book.author",
3269
  book.title as "book.title",
3270
  person.name as "person.name",
3271
  person.age as "person.age"
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3272

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3276
  column => [
3277
    ['date(book.register_datetime)' => 'book.register_date']
3278
  ];
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3279

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3282
  date(book.register_datetime) as "book.register_date"
updated pod
Yuki Kimoto authored on 2011-06-07
3283

            
updated document
Yuki Kimoto authored on 2011-06-09
3284
=item C<id>
3285

            
cleanup
Yuki Kimoto authored on 2012-01-20
3286
  id => 4
3287
  id => [4, 5]
updated document
Yuki Kimoto authored on 2011-06-09
3288

            
3289
ID corresponding to C<primary_key>.
3290
You can select rows by C<id> and C<primary_key>.
3291

            
cleanup
Yuki Kimoto authored on 2012-01-20
3292
  $dbi->select(
3293
    primary_key => ['id1', 'id2'],
3294
    id => [4, 5],
3295
    table => 'book'
3296
  );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3297

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3300
  $dbi->select(
3301
    where => {id1 => 4, id2 => 5},
3302
    table => 'book'
3303
  );
3304
  
cleanup
Yuki Kimoto authored on 2011-10-20
3305
=item C<param>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3306

            
cleanup
Yuki Kimoto authored on 2012-01-20
3307
  param => {'table2.key3' => 5}
update pod
Yuki Kimoto authored on 2011-03-12
3308

            
updated document
Yuki Kimoto authored on 2011-06-09
3309
Parameter shown before where clause.
cleanup
Yuki Kimoto authored on 2012-01-20
3310
  
cleanup
Yuki Kimoto authored on 2012-02-28
3311
For example, if you want to contain named placeholder in join clause, 
updated document
Yuki Kimoto authored on 2011-06-09
3312
you can pass parameter by C<param> option.
update pod
Yuki Kimoto authored on 2011-03-12
3313

            
cleanup
Yuki Kimoto authored on 2012-01-20
3314
  join  => ['inner join (select * from table2 where table2.key3 = :table2.key3)' . 
3315
            ' as table2 on table1.key1 = table2.key1']
updated document
Yuki Kimoto authored on 2011-06-09
3316

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3319
  prefix => 'SQL_CALC_FOUND_ROWS'
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
3320

            
3321
Prefix of column cluase
3322

            
cleanup
Yuki Kimoto authored on 2012-01-20
3323
  select SQL_CALC_FOUND_ROWS title, author from book;
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
3324

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3327
  join => [
3328
    'left outer join company on book.company_id = company_id',
3329
    'left outer join location on company.location_id = location.id'
3330
  ]
3331
      
updated document
Yuki Kimoto authored on 2011-06-09
3332
Join clause. If column cluase or where clause contain table name like "company.name",
3333
join clausees needed when SQL is created is used automatically.
update pod
Yuki Kimoto authored on 2011-03-12
3334

            
cleanup
Yuki Kimoto authored on 2012-01-20
3335
  $dbi->select(
3336
    table => 'book',
3337
    column => ['company.location_id as location_id'],
3338
    where => {'company.name' => 'Orange'},
3339
    join => [
3340
      'left outer join company on book.company_id = company.id',
3341
      'left outer join location on company.location_id = location.id'
3342
    ]
3343
  );
update pod
Yuki Kimoto authored on 2011-03-12
3344

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3348
  select company.location_id as location_id
3349
  from book
3350
    left outer join company on book.company_id = company.id
3351
  where company.name = ?;
update pod
Yuki Kimoto authored on 2011-03-12
3352

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3353
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
3354
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3355

            
cleanup
Yuki Kimoto authored on 2012-01-20
3356
  $dbi->select(
3357
    table => 'book',
3358
    column => ['company.location_id as location_id'],
3359
    where => {'company.name' => 'Orange'},
3360
    join => [
3361
      {
3362
        clause => 'left outer join location on company.location_id = location.id',
3363
        table => ['company', 'location']
3364
      }
3365
    ]
3366
  );
added join new syntax
Yuki Kimoto authored on 2011-07-28
3367

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3370
  table => 'book'
updated pod
Yuki Kimoto authored on 2011-06-08
3371

            
updated document
Yuki Kimoto authored on 2011-06-09
3372
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3373

            
updated document
Yuki Kimoto authored on 2011-06-09
3374
=item C<where>
cleanup
Yuki Kimoto authored on 2012-01-20
3375
  
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3376
  # (1) Hash reference
3377
  where => {author => 'Ken', 'title' => ['Perl', 'Ruby']}
3378
  # -> where author = 'Ken' and title in ('Perl', 'Ruby')
cleanup
Yuki Kimoto authored on 2012-01-20
3379
  
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3380
  # (2) DBIx::Custom::Where object
cleanup
Yuki Kimoto authored on 2012-01-20
3381
  where => $dbi->where(
3382
    clause => ['and', ':author{=}', ':title{like}'],
3383
    param  => {author => 'Ken', title => '%Perl%'}
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3384
  )
3385
  # -> where author = 'Ken' and title like '%Perl%'
cleanup
Yuki Kimoto authored on 2012-01-20
3386
  
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3387
  # (3) Array reference[Array refenrece, Hash reference]
cleanup
Yuki Kimoto authored on 2012-01-20
3388
  where => [
3389
    ['and', ':author{=}', ':title{like}'],
3390
    {author => 'Ken', title => '%Perl%'}
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3391
  ]
3392
  # -> where author = 'Ken' and title like '%Perl%'
3393
  
3394
  # (4) Array reference[String, Hash reference]
3395
  where => [
3396
    ':author{=} and :title{like}',
3397
    {author => 'Ken', title => '%Perl%'}
3398
  ]
3399
  #  -> where author = 'Ken' and title like '%Perl%'
cleanup
Yuki Kimoto authored on 2012-01-20
3400
  
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3401
  # (5) String
cleanup
Yuki Kimoto authored on 2012-01-20
3402
  where => 'title is null'
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3403
  #  -> where title is null
update pod
Yuki Kimoto authored on 2011-03-12
3404

            
improved where document
Yuki Kimoto authored on 2012-03-01
3405
Where clause.
3406
See also L<DBIx::Custom::Where> to know how to create where clause.
cleanup
Yuki Kimoto authored on 2012-01-20
3407
  
update pod
Yuki Kimoto authored on 2011-03-12
3408
=back
cleanup
Yuki Kimoto authored on 2011-03-08
3409

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3412
  $dbi->setup_model;
improved test
Yuki Kimoto authored on 2012-03-01
3413
  $dbi->setup_model(database => 'main');
3414
  $dbi->setup_model(database => 'main', prefix => 1);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3415

            
3416
Setup all model objects.
3417
C<columns> of model object is automatically set, parsing database information.
3418

            
improved test
Yuki Kimoto authored on 2012-03-01
3419
If C<database> option is specified, only the database is searched.
3420

            
3421
If C<prefix> option is specified, Target model is qualified by dabtabase name
3422
like C<main.book>.
3423

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3424
=head2 C<type_rule>
3425

            
cleanup
Yuki Kimoto authored on 2012-01-20
3426
  $dbi->type_rule(
3427
    into1 => {
3428
      date => sub { ... },
3429
      datetime => sub { ... }
3430
    },
3431
    into2 => {
3432
      date => sub { ... },
3433
      datetime => sub { ... }
3434
    },
3435
    from1 => {
3436
      # DATE
3437
      9 => sub { ... },
3438
      # DATETIME or TIMESTAMP
3439
      11 => sub { ... },
3440
    }
3441
    from2 => {
3442
      # DATE
3443
      9 => sub { ... },
3444
      # DATETIME or TIMESTAMP
3445
      11 => sub { ... },
3446
    }
3447
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3448

            
3449
Filtering rule when data is send into and get from database.
3450
This has a little complex problem.
3451

            
3452
In C<into1> and C<into2> you can specify
3453
type name as same as type name defined
3454
by create table, such as C<DATETIME> or C<DATE>.
3455

            
3456
Note that type name and data type don't contain upper case.
3457
If these contain upper case charactor, you convert it to lower case.
3458

            
3459
C<into2> is executed after C<into1>.
3460

            
3461
Type rule of C<into1> and C<into2> is enabled on the following
3462
column name.
3463

            
3464
=over 4
3465

            
3466
=item 1. column name
3467

            
cleanup
Yuki Kimoto authored on 2012-01-20
3468
  issue_date
3469
  issue_datetime
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3470

            
3471
This need C<table> option in each method.
3472

            
3473
=item 2. table name and column name, separator is dot
3474

            
cleanup
Yuki Kimoto authored on 2012-01-20
3475
  book.issue_date
3476
  book.issue_datetime
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3477

            
3478
=back
3479

            
3480
You get all type name used in database by C<available_typename>.
3481

            
cleanup
Yuki Kimoto authored on 2012-01-20
3482
  print $dbi->available_typename;
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3483

            
3484
In C<from1> and C<from2> you specify data type, not type name.
3485
C<from2> is executed after C<from1>.
3486
You get all data type by C<available_datatype>.
3487

            
cleanup
Yuki Kimoto authored on 2012-01-20
3488
  print $dbi->available_datatype;
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3489

            
3490
You can also specify multiple types at once.
3491

            
cleanup
Yuki Kimoto authored on 2012-01-20
3492
  $dbi->type_rule(
3493
    into1 => [
3494
      [qw/DATE DATETIME/] => sub { ... },
3495
    ],
3496
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3497

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3500
  $dbi->update({title => 'Perl'}, table  => 'book', where  => {id => 4});
removed reconnect method
yuki-kimoto authored on 2010-05-28
3501

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

            
3504
If you want to set constant value to row data, use scalar reference
3505
as parameter value.
3506

            
cleanup
Yuki Kimoto authored on 2012-01-20
3507
  {date => \"NOW()"}
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
3508

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3518
  id => 4
3519
  id => [4, 5]
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3520

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3524
  $dbi->update(
3525
    {title => 'Perl', author => 'Ken'}
3526
    primary_key => ['id1', 'id2'],
3527
    id => [4, 5],
3528
    table => 'book'
3529
  );
update pod
Yuki Kimoto authored on 2011-03-13
3530

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3533
  $dbi->update(
3534
    {title => 'Perl', author => 'Ken'}
3535
    where => {id1 => 4, id2 => 5},
3536
    table => 'book'
3537
  );
update pod
Yuki Kimoto authored on 2011-03-13
3538

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3541
  prefix => 'or replace'
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
3542

            
3543
prefix before table name section
3544

            
cleanup
Yuki Kimoto authored on 2012-01-20
3545
  update or replace book
added EXPERIMENTAL insert, u...
Yuki Kimoto authored on 2011-06-21
3546

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3549
  table => 'book'
update pod
Yuki Kimoto authored on 2011-03-13
3550

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

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

            
3555
Same as C<select> method's C<where> option.
3556

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3559
  wrap => {price => sub { "max($_[0])" }}
updated pod
Yuki Kimoto authored on 2011-09-02
3560

            
3561
placeholder wrapped string.
3562

            
3563
If the following statement
3564

            
cleanup
Yuki Kimoto authored on 2012-01-20
3565
  $dbi->update({price => 100}, table => 'book',
3566
    {price => sub { "$_[0] + 5" }});
updated pod
Yuki Kimoto authored on 2011-09-02
3567

            
3568
is executed, the following SQL is executed.
3569

            
cleanup
Yuki Kimoto authored on 2012-01-20
3570
  update book set price =  ? + 5;
updated pod
Yuki Kimoto authored on 2011-09-02
3571

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3572
=item C<mtime>
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3573

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3574
  mtime => 'modified_time'
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3575

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3576
Modified time column name. time row is updated is set to the column.
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3577
default time format is C<YYYY-mm-dd HH:MM:SS>, which can be changed by
3578
C<now> attribute.
3579

            
updated pod
Yuki Kimoto authored on 2011-06-08
3580
=back
update pod
Yuki Kimoto authored on 2011-03-13
3581

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3584
  $dbi->update_all({title => 'Perl'}, table => 'book', );
update pod
Yuki Kimoto authored on 2011-03-13
3585

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

            
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
3589
=head2 C<update_or_insert>
cleanup
Yuki Kimoto authored on 2012-01-20
3590
  
3591
  # ID
3592
  $dbi->update_or_insert(
3593
    {title => 'Perl'},
3594
    table => 'book',
3595
    id => 1,
3596
    primary_key => 'id',
3597
    option => {
3598
      select => {
3599
         append => 'for update'
3600
      }
3601
    }
3602
  );
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3603

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3604
Update or insert.
3605

            
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3606
C<update_or_insert> method execute C<select> method first to find row.
3607
If the row is exists, C<update> is executed.
3608
If not, C<insert> is executed.
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3609

            
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3610
C<OPTIONS>
3611

            
3612
C<update_or_insert> method use all common option
3613
in C<select>, C<update>, C<delete>, and has the following new ones.
3614

            
3615
=over 4
3616

            
3617
=item C<option>
3618

            
cleanup
Yuki Kimoto authored on 2012-01-20
3619
  option => {
3620
    select => {
3621
      append => '...'
3622
    },
3623
    insert => {
3624
      prefix => '...'
3625
    },
3626
    update => {
3627
      filter => {}
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3628
    }
cleanup
Yuki Kimoto authored on 2012-01-20
3629
  }
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3630

            
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3631
If you want to pass option to each method,
3632
you can use C<option> option.
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3633

            
3634
=over 4
3635

            
3636
=item C<select_option>
3637

            
cleanup
Yuki Kimoto authored on 2012-01-20
3638
  select_option => {append => 'for update'}
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3639

            
3640
select method option,
3641
select method is used to check the row is already exists.
3642

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3645
  $dbi->show_datatype($table);
update pod
Yuki Kimoto authored on 2011-08-10
3646

            
3647
Show data type of the columns of specified table.
3648

            
cleanup
Yuki Kimoto authored on 2012-01-20
3649
  book
3650
  title: 5
3651
  issue_date: 91
update pod
Yuki Kimoto authored on 2011-08-10
3652

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3657
  $dbi->show_tables;
test cleanup
Yuki Kimoto authored on 2011-08-15
3658

            
3659
Show tables.
3660

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3663
  $dbi->show_typename($table);
update pod
Yuki Kimoto authored on 2011-08-10
3664

            
3665
Show type name of the columns of specified table.
3666

            
cleanup
Yuki Kimoto authored on 2012-01-20
3667
  book
3668
  title: varchar
3669
  issue_date: date
update pod
Yuki Kimoto authored on 2011-08-10
3670

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3675
  my $values_clause = $dbi->values_clause({title => 'a', age => 2});
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3676

            
3677
Create values clause.
3678

            
cleanup
Yuki Kimoto authored on 2012-01-20
3679
  (title, author) values (title = :title, age = :age);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3680

            
3681
You can use this in insert statement.
3682

            
cleanup
Yuki Kimoto authored on 2012-01-20
3683
  my $insert_sql = "insert into book $values_clause";
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3684

            
3685
=head2 C<where>
3686

            
cleanup
Yuki Kimoto authored on 2012-01-20
3687
  my $where = $dbi->where(
3688
    clause => ['and', 'title = :title', 'author = :author'],
3689
    param => {title => 'Perl', author => 'Ken'}
3690
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3691

            
3692
Create a new L<DBIx::Custom::Where> object.
improved where document
Yuki Kimoto authored on 2012-03-01
3693
See L<DBIx::Custom::Where> to know how to create where clause.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3694

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

            
3697
=head2 C<DBIX_CUSTOM_DEBUG>
3698

            
3699
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3700
executed SQL and bind values are printed to STDERR.
3701

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

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

            
- fixed bug DBIx::Custom::Re...
Yuki Kimoto authored on 2011-11-08
3706
=head2 C<DBIX_CUSTOM_TAG_PARSE>
3707

            
3708
If you set DBIX_CUSTOM_TAG_PARSE to 0, tag parsing is off.
3709

            
3710
=head2 C<DBIX_CUSTOM_DISABLE_MODEL_EXECUTE>
3711

            
3712
If you set DBIX_CUSTOM_DISABLE_MODEL_EXECUTE to 1,
3713
L<DBIx::Custom::Model> execute method call L<DBIx::Custom> execute.
3714

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

            
3717
L<DBIx::Custom>
3718

            
cleanup
Yuki Kimoto authored on 2012-01-20
3719
  # Attribute methods
3720
  tag_parse # will be removed 2017/1/1
3721
  default_dbi_option # will be removed 2017/1/1
3722
  dbi_option # will be removed 2017/1/1
3723
  data_source # will be removed at 2017/1/1
3724
  dbi_options # will be removed at 2017/1/1
3725
  filter_check # will be removed at 2017/1/1
3726
  reserved_word_quote # will be removed at 2017/1/1
3727
  cache_method # will be removed at 2017/1/1
3728
  
3729
  # Methods
3730
  update_timestamp # will be removed at 2017/1/1
3731
  insert_timestamp # will be removed at 2017/1/1
3732
  method # will be removed at 2017/1/1
3733
  assign_param # will be removed at 2017/1/1
3734
  update_param # will be removed at 2017/1/1
3735
  insert_param # will be removed at 2017/1/1
3736
  create_query # will be removed at 2017/1/1
3737
  apply_filter # will be removed at 2017/1/1
3738
  select_at # will be removed at 2017/1/1
3739
  delete_at # will be removed at 2017/1/1
3740
  update_at # will be removed at 2017/1/1
3741
  insert_at # will be removed at 2017/1/1
3742
  register_tag # will be removed at 2017/1/1
3743
  default_bind_filter # will be removed at 2017/1/1
3744
  default_fetch_filter # will be removed at 2017/1/1
3745
  insert_param_tag # will be removed at 2017/1/1
3746
  register_tag # will be removed at 2017/1/1
3747
  register_tag_processor # will be removed at 2017/1/1
3748
  update_param_tag # will be removed at 2017/1/1
3749
  
3750
  # Options
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3751
  insert method created_at option # will be removed 2017/3/1
3752
  update method updated_at option # will be removed 2017/3/1
cleanup
Yuki Kimoto authored on 2012-01-20
3753
  select column option [COLUMN => ALIAS] syntax # will be removed 2017/1/1
3754
  execute method id option # will be removed 2017/1/1
3755
  update timestamp option # will be removed 2017/1/1
3756
  insert timestamp option # will be removed 2017/1/1
3757
  select method where_param option # will be removed 2017/1/1
3758
  delete method where_param option # will be removed 2017/1/1
3759
  update method where_param option # will be removed 2017/1/1
3760
  insert method param option # will be removed at 2017/1/1
3761
  insert method id option # will be removed at 2017/1/1
3762
  select method relation option # will be removed at 2017/1/1
3763
  select method column option [COLUMN, as => ALIAS] format
3764
    # will be removed at 2017/1/1
3765
  execute method's sqlfilter option # will be removed at 2017/1/1
3766
  
3767
  # Others
3768
  execute($query, ...) # execute method receiving query object.
3769
                       # this is removed at 2017/1/1
3770
  execute("select * from {= title}"); # execute method's
3771
                                      # tag parsing functionality
3772
                                      # will be removed at 2017/1/1
3773
  Query caching # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3774

            
3775
L<DBIx::Custom::Model>
3776

            
cleanup
Yuki Kimoto authored on 2012-01-20
3777
  # Attribute methods
3778
  execute # will be removed at 2017/1/1
3779
  method # will be removed at 2017/1/1
3780
  filter # will be removed at 2017/1/1
3781
  name # will be removed at 2017/1/1
3782
  type # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3783

            
3784
L<DBIx::Custom::Query>
DBIx::Custom::Query is DEPRE...
Yuki Kimoto authored on 2011-11-15
3785

            
3786
This module is DEPRECATED! # will be removed at 2017/1/1
cleanup
Yuki Kimoto authored on 2012-01-20
3787
  
3788
  # Attribute methods
3789
  default_filter # will be removed at 2017/1/1
3790
  table # will be removed at 2017/1/1
3791
  filters # will be removed at 2017/1/1
3792
  
3793
  # Methods
3794
  filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3795

            
3796
L<DBIx::Custom::QueryBuilder>
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
3797

            
3798
This module is DEPRECATED! # will be removed at 2017/1/1
cleanup
Yuki Kimoto authored on 2012-01-20
3799
  
3800
  # Attribute methods
3801
  tags # will be removed at 2017/1/1
3802
  tag_processors # will be removed at 2017/1/1
3803
  
3804
  # Methods
3805
  register_tag # will be removed at 2017/1/1
3806
  register_tag_processor # will be removed at 2017/1/1
3807
  
3808
  # Others
3809
  build_query("select * from {= title}"); # tag parsing functionality
3810
                                          # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3811

            
3812
L<DBIx::Custom::Result>
cleanup
Yuki Kimoto authored on 2012-01-20
3813
  
3814
  # Attribute methods
3815
  filter_check # will be removed at 2017/1/1
3816
  
3817
  # Methods
3818
  fetch_first # will be removed at 2017/2/1
3819
  fetch_hash_first # will be removed 2017/2/1
3820
  filter_on # will be removed at 2017/1/1
3821
  filter_off # will be removed at 2017/1/1
3822
  end_filter # will be removed at 2017/1/1
3823
  remove_end_filter # will be removed at 2017/1/1
3824
  remove_filter # will be removed at 2017/1/1
3825
  default_filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3826

            
3827
L<DBIx::Custom::Tag>
3828

            
cleanup
Yuki Kimoto authored on 2012-01-20
3829
  This module is DEPRECATED! # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3830

            
micro optimization
Yuki Kimoto authored on 2011-11-07
3831
L<DBIx::Custom::Order>
3832

            
cleanup
Yuki Kimoto authored on 2012-01-20
3833
  # Other
3834
  prepend method array reference receiving
3835
    $order->prepend(['book', 'desc']); # will be removed 2017/1/1
micro optimization
Yuki Kimoto authored on 2011-11-07
3836

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

            
3839
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings
3840
except for attribute method.
3841
You can check all DEPRECATED functionalities by document.
3842
DEPRECATED functionality is removed after five years,
3843
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
3844
I extend one year each time he tell me it.
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3845

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

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

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

            
3852
C<< <kimoto.yuki at gmail.com> >>
3853

            
3854
L<http://github.com/yuki-kimoto/DBIx-Custom>
3855

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3856
=head1 AUTHOR
3857

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

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

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

            
3864
This program is free software; you can redistribute it and/or modify it
3865
under the same terms as Perl itself.
3866

            
3867
=cut