DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
3870 lines | 94.905kb
- "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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1246
sub update {
cleanup
Yuki Kimoto authored on 2012-01-20
1247
  my $self = shift;
1248

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
1716
  return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1717
}
1718

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

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
1916
  return $tag;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1917
}
1918

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

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

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

            
1937
  warn "select_at is DEPRECATED! use select method id option instead";
1938

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2145
=head1 NAME
2146

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

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

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

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

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

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

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

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

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

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

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2220
Named place holder support
2221

            
2222
=item *
2223

            
cleanup
Yuki Kimoto authored on 2011-07-29
2224
Model support
2225

            
2226
=item *
2227

            
2228
Connection manager support
2229

            
2230
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
2231

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

            
2236
=item *
2237

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

            
2240
=item *
2241

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

            
2244
=back
pod fix
Yuki Kimoto authored on 2011-01-21
2245

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

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

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

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

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

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

            
2264
  my $async_conf = $dbi->async_conf;
2265
  $dbi = $dbi->async_conf($conf);
2266

            
2267
Setting when C<async> option is used.
2268

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

            
2275
C<prepare_attr> is DBI's C<prepare> method second argument,
2276
C<fh> is callback that return file handle to watch.
2277

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

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

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

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

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

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

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

            
2306
Note that L<DBIx::Connector> must be installed.
2307

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2317
  my $default_option = $dbi->default_option;
2318
  $dbi = $dbi->default_option($default_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2319

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

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

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

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

            
2334
Excluded table regex.
2335
C<each_column>, C<each_table>, C<type_rule>,
2336
and C<setup_model> methods ignore matching tables.
2337

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

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

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

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

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

            
2350
Get last successed SQL executed by C<execute> method.
2351

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

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

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

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

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

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

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

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

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

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

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

            
2383
L<DBI> option, used when C<connect> method is executed.
2384
Each value in option override the value of C<default_option>.
2385

            
cleanup
yuki-kimoto authored on 2010-10-17
2386
=head2 C<password>
2387

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-07-30
2408
You can set quote pair.
2409

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2421
  my $safety_character = $dbi->safety_character;
2422
  $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2423

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

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

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

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

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

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

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

            
2443
Enable DEPRECATED tag parsing functionality, default to 1.
2444
If you want to disable tag parsing functionality, set to 0.
2445

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2476
  my $user_table_info = $dbi->user_table_info;
2477
  $dbi = $dbi->user_table_info($user_table_info);
added test
Yuki Kimoto authored on 2011-08-16
2478

            
2479
You can set the following data.
2480

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

            
2486
Usually, you can set return value of C<get_table_info>.
2487

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

            
2491
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2492
to find table info.
2493

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2528
  async => sub {
2529
    my ($dbi, $result) = @_;
2530
    ...
2531
  };
2532

            
2533
Database async access. L<AnyEvent> is required.
2534

            
2535
This is C<mysql> async access example.
2536

            
2537
  use AnyEvent;
2538

            
2539
  my $cond = AnyEvent->condvar;
2540

            
2541
  my $timer = AnyEvent->timer(
2542
    interval => 1,
2543
    cb => sub { 1 }
2544
  );
2545

            
2546
  my $count = 0;
2547

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

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

            
2574
  $cond->recv;
2575

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

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

            
2580
Create column clause. The follwoing column clause is created.
2581

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

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

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

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

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

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

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

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

            
2612
Get rows count.
2613

            
2614
Options is same as C<select> method's ones.
2615

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

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

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

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

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

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

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

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

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

            
2642
Execute delete statement.
2643

            
2644
The following opitons are available.
2645

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

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

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

            
2653
=item C<id>
2654

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

            
2658
ID corresponding to C<primary_key>.
2659
You can delete rows by C<id> and C<primary_key>.
2660

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

            
2667
The above is same as the followin one.
2668

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

            
2671
=item C<prefix>
2672

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

            
2675
prefix before table name section.
2676

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

            
2679
=item C<table>
2680

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

            
2683
Table name.
2684

            
2685
=item C<where>
2686

            
2687
Same as C<select> method's C<where> option.
2688

            
2689
=back
2690

            
2691
=head2 C<delete_all>
2692

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

            
2695
Execute delete statement for all rows.
2696
Options is same as C<delete>.
2697

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

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

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

            
2717
If C<user_column_info> is set, C<each_column> method use C<user_column_info>
2718
infromation, you can improve the performance of C<each_column> in
2719
the following way.
2720

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

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

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

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

            
2740
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2741
infromation, you can improve the performance of C<each_table> in
2742
the following way.
2743

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2789
B<OPTIONS>
2790

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

            
2793
=over 4
2794

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

            
2797
You can filter sql after the sql is build.
2798

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

            
2801
The following one is one example.
2802

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

            
2811
The following SQL is executed.
2812

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2815
=item C<append>
2816

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

            
2819
Append some statement after SQL.
2820

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

            
2823
  prepare_attr => {async => 1}
2824

            
2825
Statemend handle attributes,
2826
this is L<DBI>'s C<prepare> method second argument.
2827

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

            
2830
Specify database bind data type.
2831

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

            
2835
This is used to bind parameter by C<bind_param> of statment handle.
2836

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

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

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

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

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

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

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

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

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

            
2881
This will improved performance when you want to execute same query repeatedly
2882
because generally creating query object is slow.
2883

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2886
  primary_key => 'id'
2887
  primary_key => ['id1', 'id2']
cleanup
Yuki Kimoto authored on 2011-10-20
2888

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

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

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-09
2898
=item C<table>
cleanup
Yuki Kimoto authored on 2012-01-20
2899
  
2900
  table => 'author'
updated pod
Yuki Kimoto authored on 2011-06-09
2901

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

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

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

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

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

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

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

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

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

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

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

            
2932
Turn C<into1> type rule off.
2933

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

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

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

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

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

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

            
2946
get column infomation except for one which match C<exclude_table> pattern.
2947

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

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

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

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

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

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

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

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

            
2977
Register helper. These helper is called directly from L<DBIx::Custom> object.
2978

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2981
=head2 C<insert>
2982

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
3006
B<options>
3007

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

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

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

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

            
3017
bulk insert is executed if database support bulk insert and 
3018
multiple parameters is passed to C<insert>.
3019
The SQL like the following one is executed.
3020

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

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

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

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3027
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
3028
default time format is "YYYY-mm-dd HH:MM:SS", which can be changed by
3029
C<now> attribute.
3030

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3033
  id => 4
3034
  id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
3035

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

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

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

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

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

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

            
3057
prefix before table name section
3058

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

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

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

            
3065
Table name.
3066

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

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

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

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

            
3075
placeholder wrapped string.
3076

            
3077
If the following statement
3078

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

            
3082
is executed, the following SQL is executed.
3083

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

            
update pod
Yuki Kimoto authored on 2011-03-13
3086
=back
3087

            
3088
=over 4
3089

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3135
  lib / MyModel.pm
3136
      / MyModel / main / book.pm
3137
                       / company.pm
3138

            
3139
  my $main_book = $self->model('main.book');
3140

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

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

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

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

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

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

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

            
3155
Create a new L<DBIx::Custom::Mapper> object.
3156

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

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

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

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

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

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

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

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

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

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

            
3178
Create column clause for myself. The follwoing column clause is created.
3179

            
cleanup
Yuki Kimoto authored on 2012-01-20
3180
  book.author as author,
3181
  book.title as title
cleanup
Yuki Kimoto authored on 2011-03-21
3182

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

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

            
3192
Create a new L<DBIx::Custom> object.
3193

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

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

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

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

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

            
3205
Create a new L<DBIx::Custom::Order> object.
3206

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

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

            
3211
Quote string by value of C<quote>.
3212

            
cleanup
yuki-kimoto authored on 2010-10-17
3213
=head2 C<register_filter>
3214

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3289
  id => 4
3290
  id => [4, 5]
updated document
Yuki Kimoto authored on 2011-06-09
3291

            
3292
ID corresponding to C<primary_key>.
3293
You can select rows by C<id> and C<primary_key>.
3294

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

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

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

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

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

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

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

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

            
3324
Prefix of column cluase
3325

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

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

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

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

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

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

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3356
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
3357
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3358

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3375
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3376

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

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

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

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

            
3419
Setup all model objects.
3420
C<columns> of model object is automatically set, parsing database information.
3421

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

            
3424
If C<prefix> option is specified, Target model is qualified by dabtabase name
3425
like C<main.book>.
3426

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

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

            
3452
Filtering rule when data is send into and get from database.
3453
This has a little complex problem.
3454

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

            
3459
Note that type name and data type don't contain upper case.
3460
If these contain upper case charactor, you convert it to lower case.
3461

            
3462
C<into2> is executed after C<into1>.
3463

            
3464
Type rule of C<into1> and C<into2> is enabled on the following
3465
column name.
3466

            
3467
=over 4
3468

            
3469
=item 1. column name
3470

            
cleanup
Yuki Kimoto authored on 2012-01-20
3471
  issue_date
3472
  issue_datetime
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3473

            
3474
This need C<table> option in each method.
3475

            
3476
=item 2. table name and column name, separator is dot
3477

            
cleanup
Yuki Kimoto authored on 2012-01-20
3478
  book.issue_date
3479
  book.issue_datetime
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3480

            
3481
=back
3482

            
3483
You get all type name used in database by C<available_typename>.
3484

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

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

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

            
3493
You can also specify multiple types at once.
3494

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

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

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

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

            
3507
If you want to set constant value to row data, use scalar reference
3508
as parameter value.
3509

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

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

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

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

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

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

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

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

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

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

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

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

            
3546
prefix before table name section
3547

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

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

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

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

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

            
3558
Same as C<select> method's C<where> option.
3559

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

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

            
3564
placeholder wrapped string.
3565

            
3566
If the following statement
3567

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

            
3571
is executed, the following SQL is executed.
3572

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

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

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-08
3583
=back
update pod
Yuki Kimoto authored on 2011-03-13
3584

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

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

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

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

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3607
Update or insert.
3608

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

            
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3613
C<OPTIONS>
3614

            
3615
C<update_or_insert> method use all common option
3616
in C<select>, C<update>, C<delete>, and has the following new ones.
3617

            
3618
=over 4
3619

            
3620
=item C<option>
3621

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

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

            
3637
=over 4
3638

            
3639
=item C<select_option>
3640

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

            
3643
select method option,
3644
select method is used to check the row is already exists.
3645

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

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

            
3650
Show data type of the columns of specified table.
3651

            
cleanup
Yuki Kimoto authored on 2012-01-20
3652
  book
3653
  title: 5
3654
  issue_date: 91
update pod
Yuki Kimoto authored on 2011-08-10
3655

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

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

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

            
3662
Show tables.
3663

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

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

            
3668
Show type name of the columns of specified table.
3669

            
cleanup
Yuki Kimoto authored on 2012-01-20
3670
  book
3671
  title: varchar
3672
  issue_date: date
update pod
Yuki Kimoto authored on 2011-08-10
3673

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

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

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

            
3680
Create values clause.
3681

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

            
3684
You can use this in insert statement.
3685

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

            
3688
=head2 C<where>
3689

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

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

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

            
3700
=head2 C<DBIX_CUSTOM_DEBUG>
3701

            
3702
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3703
executed SQL and bind values are printed to STDERR.
3704

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

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

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

            
3711
If you set DBIX_CUSTOM_TAG_PARSE to 0, tag parsing is off.
3712

            
3713
=head2 C<DBIX_CUSTOM_DISABLE_MODEL_EXECUTE>
3714

            
3715
If you set DBIX_CUSTOM_DISABLE_MODEL_EXECUTE to 1,
3716
L<DBIx::Custom::Model> execute method call L<DBIx::Custom> execute.
3717

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

            
3720
L<DBIx::Custom>
3721

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

            
3778
L<DBIx::Custom::Model>
3779

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

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

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

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

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

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

            
3830
L<DBIx::Custom::Tag>
3831

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

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

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

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

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

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

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

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

            
3855
C<< <kimoto.yuki at gmail.com> >>
3856

            
3857
L<http://github.com/yuki-kimoto/DBIx-Custom>
3858

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3859
=head1 AUTHOR
3860

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

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

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

            
3867
This program is free software; you can redistribute it and/or modify it
3868
under the same terms as Perl itself.
3869

            
3870
=cut