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

            
versionup
Yuki Kimoto authored on 2012-03-02
5
our $VERSION = '0.24';
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 default_s...
Yuki Kimoto authored on 2012-03-02
23
has [qw/connector dsn default_schema 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
fixed MySQL column_info don'...
Yuki Kimoto authored on 2012-03-02
303
    my $tables = {};
304
    $self->each_table(sub {
305
      my ($dbi, $table, $table_info) = @_;
306
      my $schema = $table_info->{TABLE_SCHEM};
307
      $tables->{$schema}{$table}++;
308
    });
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
309

            
cleanup
Yuki Kimoto authored on 2012-01-20
310
    # Iterate all tables
fixed MySQL column_info don'...
Yuki Kimoto authored on 2012-03-02
311
    for my $schema (sort keys %$tables) {
312
      for my $table (sort keys %{$tables->{$schema}}) {
313
        
314
        # Iterate all columns
315
        my $sth_columns;
316
        eval {$sth_columns = $self->dbh->column_info(undef, $schema, $table, '%')};
317
        next if $@;
318
        while (my $column_info = $sth_columns->fetchrow_hashref) {
319
          my $column = $column_info->{COLUMN_NAME};
320
          $self->$cb($table, $column, $column_info);
321
        }
cleanup
Yuki Kimoto authored on 2012-01-20
322
      }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
323
    }
cleanup
Yuki Kimoto authored on 2012-01-20
324
  }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
325
}
326

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
348
sub execute {
cleanup
Yuki Kimoto authored on 2012-01-20
349
  my $self = shift;
350
  my $sql = shift;
351

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
844
    $model_class =~ s/\./::/g;
845
    $model_name =~ s/::/./;
846
    $model_table =~ s/::/./;
847

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

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

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

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

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

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

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

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

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

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

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

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

            
994
sub _qp {
995
  my ($self, %opt) = @_;
996

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

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

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

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

            
1081
  # Execute query without table
1082
  return $self->execute($sql, {}, %opt) if $table_is_empty;
1083

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

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

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1121
sub setup_model {
improved test
Yuki Kimoto authored on 2012-03-01
1122
  my ($self, %opt) = @_;
cleanup
Yuki Kimoto authored on 2012-01-20
1123
  
1124
  # Setup model
1125
  $self->each_column(
1126
    sub {
1127
      my ($self, $table, $column, $column_info) = @_;
cleanup
Yuki Kimoto authored on 2012-03-02
1128
      my $schema = $column_info->{TABLE_SCHEM};
improved test
Yuki Kimoto authored on 2012-03-01
1129
      
fixed setup method logic
Yuki Kimoto authored on 2012-03-02
1130
      my $default_schema = $self->default_schema;
1131
      
cleanup
Yuki Kimoto authored on 2012-01-20
1132
      if (my $model = $self->models->{$table}) {
fixed setup method logic
Yuki Kimoto authored on 2012-03-02
1133
        if (!defined $default_schema || $default_schema eq $schema) {
1134
          push @{$model->columns}, $column;
1135
        }
1136
      }
1137
      if (my $fullqualified_model = $self->models->{"$schema.$table"}) {
1138
        push @{$fullqualified_model->columns}, $column;
cleanup
Yuki Kimoto authored on 2012-01-20
1139
      }
1140
    }
1141
  );
1142
  return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1143
}
1144

            
update pod
Yuki Kimoto authored on 2011-08-10
1145
sub show_datatype {
cleanup
Yuki Kimoto authored on 2012-01-20
1146
  my ($self, $table) = @_;
1147
  croak "Table name must be specified" unless defined $table;
1148
  print "$table\n";
1149
  
1150
  my $result = $self->select(table => $table, where => "'0' <> '0'");
1151
  my $sth = $result->sth;
1152

            
1153
  my $columns = $sth->{NAME};
1154
  my $data_types = $sth->{TYPE};
1155
  
1156
  for (my $i = 0; $i < @$columns; $i++) {
1157
    my $column = $columns->[$i];
1158
    my $data_type = lc $data_types->[$i];
1159
    print "$column: $data_type\n";
1160
  }
update pod
Yuki Kimoto authored on 2011-08-10
1161
}
1162

            
1163
sub show_typename {
cleanup
Yuki Kimoto authored on 2012-01-20
1164
  my ($self, $t) = @_;
1165
  croak "Table name must be specified" unless defined $t;
1166
  print "$t\n";
1167
  
1168
  $self->each_column(sub {
1169
    my ($self, $table, $column, $infos) = @_;
1170
    return unless $table eq $t;
1171
    my $typename = lc $infos->{TYPE_NAME};
1172
    print "$column: $typename\n";
1173
  });
1174
  
1175
  return $self;
update pod
Yuki Kimoto authored on 2011-08-10
1176
}
1177

            
test cleanup
Yuki Kimoto authored on 2011-08-15
1178
sub show_tables {
cleanup
Yuki Kimoto authored on 2012-01-20
1179
  my $self = shift;
1180
  
1181
  my %tables;
1182
  $self->each_table(sub { $tables{$_[1]}++ });
1183
  print join("\n", sort keys %tables) . "\n";
1184
  return $self;
test cleanup
Yuki Kimoto authored on 2011-08-15
1185
}
1186

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

            
1190
  $self->{_type_rule_is_called} = 1;
1191
  
1192
  if (@_) {
1193
    my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1194
    
1195
    # Into
1196
    for my $i (1 .. 2) {
1197
      my $into = "into$i";
1198
      my $exists_into = exists $type_rule->{$into};
1199
      $type_rule->{$into} = _array_to_hash($type_rule->{$into});
1200
      $self->{type_rule} = $type_rule;
1201
      $self->{"_$into"} = {};
1202
      for my $type_name (keys %{$type_rule->{$into} || {}}) {
1203
        croak qq{type name of $into section must be lower case}
1204
          if $type_name =~ /[A-Z]/;
1205
      }
1206
      
1207
      $self->each_column(sub {
1208
        my ($dbi, $table, $column, $column_info) = @_;
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
1209
        
cleanup
Yuki Kimoto authored on 2012-01-20
1210
        my $type_name = lc $column_info->{TYPE_NAME};
1211
        if ($type_rule->{$into} &&
1212
            (my $filter = $type_rule->{$into}->{$type_name}))
1213
        {
1214
          return unless exists $type_rule->{$into}->{$type_name};
1215
          if (defined $filter && ref $filter ne 'CODE') 
1216
          {
1217
            my $fname = $filter;
1218
            croak qq{Filter "$fname" is not registered" } . _subname
1219
              unless exists $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2011-08-16
1220
            
cleanup
Yuki Kimoto authored on 2012-01-20
1221
            $filter = $self->filters->{$fname};
1222
          }
fixed MySQL column_info don'...
Yuki Kimoto authored on 2012-03-02
1223
          
cleanup
Yuki Kimoto authored on 2012-03-02
1224
          my $schema = $column_info->{TABLE_SCHEM};
added EXPERIMENTAL default_s...
Yuki Kimoto authored on 2012-03-02
1225
          my $default_schema = $self->default_schema;
fixed setup method logic
Yuki Kimoto authored on 2012-03-02
1226
          if (!defined $default_schema || $default_schema eq $schema) {
added EXPERIMENTAL default_s...
Yuki Kimoto authored on 2012-03-02
1227
            $self->{"_$into"}{key}{$table}{$column} = $filter;
1228
            $self->{"_$into"}{dot}{"$table.$column"} = $filter;
1229
          }
1230
          
cleanup
Yuki Kimoto authored on 2012-03-02
1231
          $self->{"_$into"}{key}{"$schema.$table"}{$column} = $filter;
1232
          $self->{"_$into"}{dot}{"$schema.$table.$column"} = $filter;
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
1233
        }
cleanup
Yuki Kimoto authored on 2012-01-20
1234
      });
1235
    }
1236

            
1237
    # From
1238
    for my $i (1 .. 2) {
1239
      $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
1240
      for my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
1241
        croak qq{data type of from$i section must be lower case or number}
1242
          if $data_type =~ /[A-Z]/;
1243
        my $fname = $type_rule->{"from$i"}{$data_type};
1244
        if (defined $fname && ref $fname ne 'CODE') {
1245
          croak qq{Filter "$fname" is not registered" } . _subname
1246
            unless exists $self->filters->{$fname};
1247
          
1248
          $type_rule->{"from$i"}{$data_type} = $self->filters->{$fname};
1249
        }
1250
      }
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1251
    }
1252
    
cleanup
Yuki Kimoto authored on 2012-01-20
1253
    return $self;
1254
  }
1255
  
1256
  return $self->{type_rule} || {};
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1257
}
1258

            
cleanup
yuki-kimoto authored on 2010-10-17
1259
sub update {
cleanup
Yuki Kimoto authored on 2012-01-20
1260
  my $self = shift;
1261

            
1262
  # Options
1263
  my $param = @_ % 2 ? shift : undef;
1264
  my %opt = @_;
1265
  warn "update param option is DEPRECATED!" if $opt{param};
1266
  warn "update method where_param option is DEPRECATED!"
1267
    if $opt{where_param};
1268
  $param ||= $opt{param} || {};
1269
  
1270
  # Don't allow update all rows
1271
  croak qq{update method where option must be specified } . _subname
1272
    if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all};
1273
  
1274
  # Timestamp(DEPRECATED!)
1275
  if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
1276
    warn "update timestamp option is DEPRECATED! use mtime";
cleanup
Yuki Kimoto authored on 2012-01-20
1277
    my $columns = $update_timestamp->[0];
1278
    $columns = [$columns] unless ref $columns eq 'ARRAY';
1279
    my $value = $update_timestamp->[1];
1280
    $value = $value->() if ref $value eq 'CODE';
1281
    $param->{$_} = $value for @$columns;
1282
  }
1283

            
1284
  # Created time and updated time
1285
  my @timestamp_cleanup;
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
1286
  warn "update method update_at option is DEPRECATED! "
1287
      . "use mtime option instead " . _subname
1288
    if $opt{updated_at};
1289
  $opt{mtime} ||= $opt{updated_at};
1290
  if (defined $opt{mtime}) {
cleanup
Yuki Kimoto authored on 2012-01-20
1291
    my $now = $self->now;
1292
    $now = $now->() if ref $now eq 'CODE';
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
1293
    $param->{$opt{mtime}} = $self->now->();
1294
    push @timestamp_cleanup, $opt{mtime};
cleanup
Yuki Kimoto authored on 2012-01-20
1295
  }
1296

            
1297
  # Assign clause
1298
  my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
1299
  
1300
  # Where
1301
  my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
1302
    delete $opt{id}, $opt{primary_key}, $opt{table});
1303
  
1304
  # Update statement
1305
  my $sql = "update ";
1306
  $sql .= "$opt{prefix} " if defined $opt{prefix};
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1307
  $sql .= $self->_tq($opt{table}) . " set $assign_clause $w->{clause} ";
cleanup
Yuki Kimoto authored on 2012-01-20
1308
  
1309
  # Execute query
1310
  $opt{statement} = 'update';
1311
  $opt{cleanup} = \@timestamp_cleanup;
1312
  $self->execute($sql, [$param, $w->{param}], %opt);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1313
}
1314

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1317
sub update_or_insert {
cleanup
Yuki Kimoto authored on 2012-01-20
1318
  my ($self, $param, %opt) = @_;
1319
  croak "update_or_insert method need primary_key and id option "
1320
    unless defined $opt{id} && defined $opt{primary_key};
1321
  my $statement_opt = $opt{option} || {};
1322

            
1323
  my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all;
1324
  if (@$rows == 0) {
1325
    return $self->insert($param, %opt, %{$statement_opt->{insert} || {}});
1326
  }
1327
  elsif (@$rows == 1) {
1328
    return 0 unless keys %$param;
1329
    return $self->update($param, %opt, %{$statement_opt->{update} || {}});
1330
  }
1331
  else { croak "selected row must be one " . _subname }
cleanup
Yuki Kimoto authored on 2011-10-21
1332
}
1333

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1334
sub update_timestamp {
cleanup
Yuki Kimoto authored on 2012-01-20
1335
  my $self = shift;
1336
  
1337
  warn "update_timestamp method is DEPRECATED! use now method";
1338
  
1339
  if (@_) {
1340
    $self->{update_timestamp} = [@_];
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1341
    
cleanup
Yuki Kimoto authored on 2012-01-20
1342
    return $self;
1343
  }
1344
  return $self->{update_timestamp};
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1345
}
1346

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1347
sub values_clause {
cleanup
Yuki Kimoto authored on 2012-01-20
1348
  my ($self, $param, $opts) = @_;
1349
  
1350
  my $wrap = $opts->{wrap} || {};
1351
  
1352
  # Create insert parameter tag
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1353
  my ($q, $p) = $self->_qp;
cleanup
Yuki Kimoto authored on 2012-01-20
1354
  
1355
  # values clause(performance is important)
1356
  '(' .
1357
  join(
1358
    ', ',
1359
    map { "$q$_$p" } sort keys %$param
1360
  ) .
1361
  ') values (' .
1362
  join(
1363
    ', ',
1364
    map {
1365
      ref $param->{$_} eq 'SCALAR' ? ${$param->{$_}} :
1366
      $wrap->{$_} ? $wrap->{$_}->(":$_") :
1367
      ":$_";
1368
    } sort keys %$param
1369
  ) .
1370
  ')'
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1371
}
1372

            
added EXPERIMENTAL bulk_inse...
Yuki Kimoto authored on 2011-11-27
1373
sub _multi_values_clause {
cleanup
Yuki Kimoto authored on 2012-01-20
1374
  my ($self, $params, $opts) = @_;
1375
  
1376
  my $wrap = $opts->{wrap} || {};
1377
  
1378
  # Create insert parameter tag
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1379
  my ($q, $p) = $self->_qp;
cleanup
Yuki Kimoto authored on 2012-01-20
1380
  
1381
  # Multi values clause
1382
  my $clause = '(' . join(', ', map { "$q$_$p" } sort keys %{$params->[0]}) . ') values ';
1383
  
1384
  for (1 .. @$params) {
1385
    $clause .= '(' . join(', ', 
1386
      map {
1387
        ref $params->[0]->{$_} eq 'SCALAR' ? ${$params->[0]->{$_}} :
1388
        $wrap->{$_} ? $wrap->{$_}->(":$_") :
1389
        ":$_";
1390
      } sort keys %{$params->[0]}
1391
    ) . '), '
1392
  }
1393
  $clause =~ s/, $//;
1394
  return $clause;
added EXPERIMENTAL bulk_inse...
Yuki Kimoto authored on 2011-11-27
1395
}
1396

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

            
updated pod
Yuki Kimoto authored on 2011-06-21
1399
sub _create_query {
cleanup
Yuki Kimoto authored on 2012-01-20
1400
  
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
1401
  my ($self, $source, $after_build_sql, $prepare_attr) = @_;
1402
  
1403
  $prepare_attr ||= {};
cleanup
Yuki Kimoto authored on 2012-01-20
1404
  
1405
  # Cache
1406
  my $cache = $self->{cache};
1407
  
1408
  # Query
1409
  my $query;
1410
  
1411
  # Get cached query
1412
  if ($cache) {
1413
    
1414
    # Get query
1415
    my $q = $self->cache_method->($self, $source);
updated pod
Yuki Kimoto authored on 2011-06-21
1416
    
1417
    # Create query
cleanup
Yuki Kimoto authored on 2012-01-20
1418
    if ($q) {
1419
      $query = DBIx::Custom::Query->new($q);
1420
      $query->{filters} = $self->filters;
cleanup
Yuki Kimoto authored on 2011-06-13
1421
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1422
  }
1423
  
1424
  # Create query
1425
  unless ($query) {
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1426

            
cleanup
Yuki Kimoto authored on 2012-01-20
1427
    # Create query
1428
    my $tag_parse = exists $ENV{DBIX_CUSTOM_TAG_PARSE}
1429
      ? $ENV{DBIX_CUSTOM_TAG_PARSE} : $self->{tag_parse};
1430

            
1431
    my $sql = " " . $source || '';
1432
    if ($tag_parse && ($sql =~ /\s\{/)) {
1433
      $query = $self->query_builder->build_query($sql);
updated pod
Yuki Kimoto authored on 2011-06-21
1434
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1435
    else {
1436
      my @columns;
1437
      my $c = $self->{safety_character};
1438
      my $re = $c eq 'a-zA-Z0-9_'
1439
        ? qr/(.*?[^\\]):([$c\.]+)(?:\{(.*?)\})?(.*)/so
1440
        : qr/(.*?[^\\]):([$c\.]+)(?:\{(.*?)\})?(.*)/s;
1441
      my %duplicate;
1442
      my $duplicate;
1443
      # Parameter regex
1444
      $sql =~ s/([0-9]):/$1\\:/g;
1445
      my $new_sql = '';
1446
      while ($sql =~ /$re/) {
1447
        push @columns, $2;
1448
        $duplicate = 1 if ++$duplicate{$columns[-1]} > 1;
1449
        ($new_sql, $sql) = defined $3 ?
1450
          ($new_sql . "$1$2 $3 ?", " $4") : ($new_sql . "$1?", " $4");
1451
      }
1452
      $new_sql .= $sql;
1453
      $new_sql =~ s/\\:/:/g if index($new_sql, "\\:") != -1;
1454

            
1455
      # Create query
1456
      $query = {sql => $new_sql, columns => \@columns, duplicate => $duplicate};
1457
    }
1458
    
1459
    # Save query to cache
1460
    $self->cache_method->(
1461
      $self, $source,
1462
      {
1463
        sql     => $query->{sql}, 
1464
        columns => $query->{columns},
1465
        tables  => $query->{tables} || []
1466
      }
1467
    ) if $cache;
1468
  }
1469

            
1470
  # Filter SQL
1471
  $query->{sql} = $after_build_sql->($query->{sql}) if $after_build_sql;
1472
  
1473
  # Save sql
1474
  $self->{last_sql} = $query->{sql};
1475
  
1476
  # Prepare statement handle
1477
  my $sth;
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
1478
  eval { $sth = $self->dbh->prepare($query->{sql}, $prepare_attr) };
cleanup
Yuki Kimoto authored on 2012-01-20
1479
  
1480
  if ($@) {
1481
    $self->_croak($@, qq{. Following SQL is executed.\n}
1482
                    . qq{$query->{sql}\n} . _subname);
1483
  }
1484
  
1485
  # Set statement handle
1486
  $query->{sth} = $sth;
1487
  
1488
  # Set filters
1489
  $query->{filters} = $self->{filters} || $self->filters;
1490
  
1491
  return $query;
cleanup
Yuki Kimoto authored on 2011-06-13
1492
}
1493

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

            
1550
sub _id_to_param {
1551
  my ($self, $id, $primary_keys, $table) = @_;
1552
  
1553
  # Check primary key
1554
  croak "primary_key option " .
1555
        "must be specified when id option is used" . _subname
1556
    unless defined $primary_keys;
1557
  $primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY';
1558
  
1559
  # Create parameter
1560
  my $param = {};
1561
  if (defined $id) {
1562
    $id = [$id] unless ref $id;
1563
    for(my $i = 0; $i < @$id; $i++) {
1564
      my $key = $primary_keys->[$i];
1565
      $key = "$table." . $key if $table;
1566
      $param->{$key} = $id->[$i];
1567
    }
1568
  }
1569
  
1570
  return $param;
improved error messages
Yuki Kimoto authored on 2011-04-18
1571
}
1572

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1573
sub _connect {
cleanup
Yuki Kimoto authored on 2012-01-20
1574
  my $self = shift;
1575
  
1576
  # Attributes
1577
  my $dsn = $self->data_source;
1578
  warn "data_source is DEPRECATED!\n"
1579
    if $dsn;
1580
  $dsn ||= $self->dsn;
1581
  croak qq{"dsn" must be specified } . _subname
1582
    unless $dsn;
1583
  my $user        = $self->user;
1584
  my $password    = $self->password;
1585
  my $option = $self->_option;
1586
  $option = {%{$self->default_option}, %$option};
1587
  
1588
  # Connect
1589
  my $dbh;
1590
  eval { $dbh = DBI->connect($dsn, $user, $password, $option) };
1591
  
1592
  # Connect error
1593
  croak "$@ " . _subname if $@;
1594
  
1595
  return $dbh;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1596
}
1597

            
cleanup
yuki-kimoto authored on 2010-10-17
1598
sub _croak {
cleanup
Yuki Kimoto authored on 2012-01-20
1599
  my ($self, $error, $append) = @_;
1600
  
1601
  # Append
1602
  $append ||= "";
1603
  
1604
  # Verbose
1605
  if ($Carp::Verbose) { croak $error }
1606
  
1607
  # Not verbose
1608
  else {
1609
    # Remove line and module infromation
1610
    my $at_pos = rindex($error, ' at ');
1611
    $error = substr($error, 0, $at_pos);
1612
    $error =~ s/\s+$//;
1613
    croak "$error$append";
1614
  }
cleanup
yuki-kimoto authored on 2010-10-17
1615
}
1616

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1619
sub _need_tables {
cleanup
Yuki Kimoto authored on 2012-01-20
1620
  my ($self, $tree, $need_tables, $tables) = @_;
1621
  
1622
  # Get needed tables
1623
  for my $table (@$tables) {
1624
    if ($tree->{$table}) {
1625
      $need_tables->{$table} = 1;
1626
      $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1627
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1628
  }
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1629
}
1630

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1631
sub _option {
cleanup
Yuki Kimoto authored on 2012-01-20
1632
  my $self = shift;
1633
  my $option = {%{$self->dbi_options}, %{$self->dbi_option}, %{$self->option}};
1634
  warn "dbi_options is DEPRECATED! use option instead\n"
1635
    if keys %{$self->dbi_options};
1636
  warn "dbi_option is DEPRECATED! use option instead\n"
1637
    if keys %{$self->dbi_option};
1638
  return $option;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1639
}
1640

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1641
sub _push_join {
cleanup
Yuki Kimoto authored on 2012-01-20
1642
  my ($self, $sql, $join, $join_tables) = @_;
1643
  
1644
  $join = [$join] unless ref $join eq 'ARRAY';
1645
  
1646
  # No join
1647
  return unless @$join;
1648
  
1649
  # Push join clause
1650
  my $tree = {};
1651
  for (my $i = 0; $i < @$join; $i++) {
1652
    
1653
    # Arrange
1654
    my $join_clause;;
1655
    my $option;
1656
    if (ref $join->[$i] eq 'HASH') {
1657
      $join_clause = $join->[$i]->{clause};
1658
      $option = {table => $join->[$i]->{table}};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1659
    }
cleanup
Yuki Kimoto authored on 2012-01-20
1660
    else {
1661
      $join_clause = $join->[$i];
1662
      $option = {};
1663
    };
1664

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

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1711
sub _quote {
cleanup
Yuki Kimoto authored on 2012-01-20
1712
  my $self = shift;
1713
  return $self->{reserved_word_quote} || $self->quote || '';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1714
}
1715

            
cleanup
Yuki Kimoto authored on 2011-04-02
1716
sub _remove_duplicate_table {
cleanup
Yuki Kimoto authored on 2012-01-20
1717
  my ($self, $tables, $main_table) = @_;
1718
  
1719
  # Remove duplicate table
1720
  my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1721
  delete $tables{$main_table} if $main_table;
1722
  
1723
  my $new_tables = [keys %tables, $main_table ? $main_table : ()];
1724
  if (my $q = $self->_quote) {
1725
    $q = quotemeta($q);
1726
    $_ =~ s/[$q]//g for @$new_tables;
1727
  }
micro optimization
Yuki Kimoto authored on 2011-07-30
1728

            
cleanup
Yuki Kimoto authored on 2012-01-20
1729
  return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1730
}
1731

            
cleanup
Yuki Kimoto authored on 2011-04-02
1732
sub _search_tables {
cleanup
Yuki Kimoto authored on 2012-01-20
1733
  my ($self, $source) = @_;
1734
  
1735
  # Search tables
1736
  my $tables = [];
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1737
  my ($q, $p) = $self->_qp(quotemeta => 1);
1738
  $source =~ s/$q//g;
1739
  $source =~ s/$p//g;
1740
  my $c = $self->safety_character;
1741
  
1742
  while ($source =~ /((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+/g) {
1743
    push @$tables, $1;
1744
  }
cleanup
Yuki Kimoto authored on 2012-01-20
1745
  return $tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1746
}
1747

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

            
1751
  $where ||= {};
1752
  $where = $self->_id_to_param($id, $primary_key, $table) if defined $id;
1753
  $where_param ||= {};
1754
  my $w = {};
1755

            
cleanup
Yuki Kimoto authored on 2012-02-28
1756
  if (ref $where eq 'HASH') {
1757
    my $clause = [];
1758
    my $column_join = '';
1759
    for my $column (keys %$where) {
1760
      $column_join .= $column;
1761
      my $table;
1762
      my $c;
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1763
      if ($column =~ /(?:(.*)\.)?(.*)/) {
cleanup
Yuki Kimoto authored on 2012-02-28
1764
        $table = $1;
1765
        $c = $2;
cleanup
Yuki Kimoto authored on 2012-01-20
1766
      }
cleanup
Yuki Kimoto authored on 2012-02-28
1767
      
1768
      my $table_quote;
_search_tables can parse dat...
Yuki Kimoto authored on 2012-03-01
1769
      $table_quote = $self->_tq($table) if defined $table;
cleanup
Yuki Kimoto authored on 2012-02-28
1770
      my $column_quote = $self->q($c);
1771
      $column_quote = $table_quote . '.' . $column_quote
1772
        if defined $table_quote;
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
1773
      if (ref $where->{$column} eq 'ARRAY') {
1774
        my $c = join(', ', (":$column") x @{$where->{$column}});
fixed where multi value bug
Yuki Kimoto authored on 2012-02-28
1775
        if (@{$where->{$column}}) {
1776
          push @$clause, "$column_quote in ( $c )";
1777
        }
1778
        else { push @$clause, '1 <> 1' }
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
1779
      }
1780
      else { push @$clause, "$column_quote = :$column" }
cleanup
Yuki Kimoto authored on 2012-02-28
1781
    }
1782
    
1783
    $w->{clause} = @$clause ? "where ( " . join(' and ', @$clause) . " ) " : '' ;
1784
    $w->{param} = $where;
1785
    $w->{param} = keys %$where_param
1786
      ? $self->merge_param($where_param, $where)
1787
      : $where;
1788
  }  
1789
  elsif (ref $where) {
1790
    my $obj;
1791

            
1792
    if (ref $where eq 'DBIx::Custom::Where') { $obj = $where }
cleanup
Yuki Kimoto authored on 2012-01-20
1793
    elsif (ref $where eq 'ARRAY') {
1794
      $obj = $self->where(clause => $where->[0], param => $where->[1]);
1795
    }
1796
    
1797
    # Check where argument
1798
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
1799
        . qq{or array reference, which contains where clause and parameter}
1800
        . _subname
1801
      unless ref $obj eq 'DBIx::Custom::Where';
1802

            
cleanup
Yuki Kimoto authored on 2012-02-28
1803
    $w->{clause} = $obj->to_string;
cleanup
Yuki Kimoto authored on 2012-01-20
1804
    $w->{param} = keys %$where_param
1805
      ? $self->merge_param($where_param, $obj->param)
1806
      : $obj->param;
1807
  }
1808
  elsif ($where) {
1809
    $w->{clause} = "where $where";
1810
    $w->{param} = $where_param;
1811
  }
1812
  
1813
  return $w;
cleanup
Yuki Kimoto authored on 2011-10-21
1814
}
1815

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

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

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1883
# DEPRECATED!
1884
has 'data_source';
1885
has dbi_options => sub { {} };
1886
has filter_check  => 1;
1887
has 'reserved_word_quote';
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1888
has dbi_option => sub { {} };
1889
has default_dbi_option => sub {
cleanup
Yuki Kimoto authored on 2012-01-20
1890
  warn "default_dbi_option is DEPRECATED! use default_option instead";
1891
  return shift->default_option;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1892
};
1893

            
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
1894
# DEPRECATED
1895
sub tag_parse {
cleanup
Yuki Kimoto authored on 2012-01-20
1896
 my $self = shift;
1897
 warn "tag_parse is DEPRECATED! use \$ENV{DBIX_CUSTOM_TAG_PARSE} " .
1898
   "environment variable";
1899
  if (@_) {
1900
    $self->{tag_parse} = $_[0];
1901
    return $self;
1902
  }
1903
  return $self->{tag_parse};
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
1904
}
1905

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1906
# DEPRECATED!
1907
sub method {
cleanup
Yuki Kimoto authored on 2012-01-20
1908
  warn "method is DEPRECATED! use helper instead";
1909
  return shift->helper(@_);
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1910
}
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1911

            
1912
# DEPRECATED!
1913
sub assign_param {
cleanup
Yuki Kimoto authored on 2012-01-20
1914
  my $self = shift;
1915
  warn "assing_param is DEPRECATED! use assign_clause instead";
1916
  return $self->assign_clause(@_);
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1917
}
1918

            
1919
# DEPRECATED
1920
sub update_param {
cleanup
Yuki Kimoto authored on 2012-01-20
1921
  my ($self, $param, $opts) = @_;
1922
  
1923
  warn "update_param is DEPRECATED! use assign_clause instead.";
1924
  
1925
  # Create update parameter tag
1926
  my $tag = $self->assign_clause($param, $opts);
1927
  $tag = "set $tag" unless $opts->{no_set};
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1928

            
cleanup
Yuki Kimoto authored on 2012-01-20
1929
  return $tag;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1930
}
1931

            
updated pod
Yuki Kimoto authored on 2011-06-21
1932
# DEPRECATED!
1933
sub create_query {
cleanup
Yuki Kimoto authored on 2012-01-20
1934
  warn "create_query is DEPRECATED! use query option of each method";
1935
  shift->_create_query(@_);
updated pod
Yuki Kimoto authored on 2011-06-21
1936
}
1937

            
cleanup
Yuki Kimoto authored on 2011-06-13
1938
# DEPRECATED!
1939
sub apply_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
1940
  my $self = shift;
1941
  
1942
  warn "apply_filter is DEPRECATED!";
1943
  return $self->_apply_filter(@_);
cleanup
Yuki Kimoto authored on 2011-06-13
1944
}
1945

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

            
1950
  warn "select_at is DEPRECATED! use select method id option instead";
1951

            
1952
  # Options
1953
  my $primary_keys = delete $opt{primary_key};
1954
  my $where = delete $opt{where};
1955
  my $param = delete $opt{param};
1956
  
1957
  # Table
1958
  croak qq{"table" option must be specified } . _subname
1959
    unless $opt{table};
1960
  my $table = ref $opt{table} ? $opt{table}->[-1] : $opt{table};
1961
  
1962
  # Create where parameter
1963
  my $where_param = $self->_id_to_param($where, $primary_keys);
1964
  
1965
  return $self->select(where => $where_param, %opt);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1966
}
1967

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
1972
  warn "delete_at is DEPRECATED! use delete method id option instead";
1973
  
1974
  # Options
1975
  my $primary_keys = delete $opt{primary_key};
1976
  my $where = delete $opt{where};
1977
  
1978
  # Create where parameter
1979
  my $where_param = $self->_id_to_param($where, $primary_keys);
1980
  
1981
  return $self->delete(where => $where_param, %opt);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1982
}
1983

            
cleanup
Yuki Kimoto authored on 2011-06-08
1984
# DEPRECATED!
1985
sub update_at {
cleanup
Yuki Kimoto authored on 2012-01-20
1986
  my $self = shift;
1987

            
1988
  warn "update_at is DEPRECATED! use update method id option instead";
1989
  
1990
  # Options
1991
  my $param;
1992
  $param = shift if @_ % 2;
1993
  my %opt = @_;
1994
  my $primary_keys = delete $opt{primary_key};
1995
  my $where = delete $opt{where};
1996
  my $p = delete $opt{param} || {};
1997
  $param  ||= $p;
1998
  
1999
  # Create where parameter
2000
  my $where_param = $self->_id_to_param($where, $primary_keys);
2001
  
2002
  return $self->update(where => $where_param, param => $param, %opt);
cleanup
Yuki Kimoto authored on 2011-06-08
2003
}
2004

            
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
2005
# DEPRECATED!
2006
sub insert_at {
cleanup
Yuki Kimoto authored on 2012-01-20
2007
  my $self = shift;
2008
  
2009
  warn "insert_at is DEPRECATED! use insert method id option instead";
2010
  
2011
  # Options
2012
  my $param;
2013
  $param = shift if @_ % 2;
2014
  my %opt = @_;
2015
  my $primary_key = delete $opt{primary_key};
2016
  $primary_key = [$primary_key] unless ref $primary_key;
2017
  my $where = delete $opt{where};
2018
  my $p = delete $opt{param} || {};
2019
  $param  ||= $p;
2020
  
2021
  # Create where parameter
2022
  my $where_param = $self->_id_to_param($where, $primary_key);
2023
  $param = $self->merge_param($where_param, $param);
2024
  
2025
  return $self->insert(param => $param, %opt);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
2026
}
2027

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

            
2041
# DEPRECATED!
2042
sub register_tag_processor {
cleanup
Yuki Kimoto authored on 2012-01-20
2043
  my $self = shift;
2044
  warn "register_tag_processor is DEPRECATED!";
2045
  # Merge tag
2046
  my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
2047
  $self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}};
2048
  return $self;
added warnings
Yuki Kimoto authored on 2011-06-07
2049
}
2050

            
cleanup
Yuki Kimoto authored on 2011-01-25
2051
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
2052
sub default_bind_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
2053
  my $self = shift;
2054
  
2055
  warn "default_bind_filter is DEPRECATED!";
2056
  
2057
  if (@_) {
2058
    my $fname = $_[0];
added warnings
Yuki Kimoto authored on 2011-06-07
2059
    
cleanup
Yuki Kimoto authored on 2012-01-20
2060
    if (@_ && !$fname) {
2061
      $self->{default_out_filter} = undef;
cleanup
Yuki Kimoto authored on 2011-01-12
2062
    }
cleanup
Yuki Kimoto authored on 2012-01-20
2063
    else {
2064
      croak qq{Filter "$fname" is not registered}
2065
        unless exists $self->filters->{$fname};
2066
  
2067
      $self->{default_out_filter} = $self->filters->{$fname};
2068
    }
2069
    return $self;
2070
  }
2071
  
2072
  return $self->{default_out_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
2073
}
2074

            
cleanup
Yuki Kimoto authored on 2011-01-25
2075
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
2076
sub default_fetch_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
2077
  my $self = shift;
added warnings
Yuki Kimoto authored on 2011-06-07
2078

            
cleanup
Yuki Kimoto authored on 2012-01-20
2079
  warn "default_fetch_filter is DEPRECATED!";
2080
  
2081
  if (@_) {
2082
    my $fname = $_[0];
many changed
Yuki Kimoto authored on 2011-01-23
2083

            
cleanup
Yuki Kimoto authored on 2012-01-20
2084
    if (@_ && !$fname) {
2085
      $self->{default_in_filter} = undef;
2086
    }
2087
    else {
2088
      croak qq{Filter "$fname" is not registered}
2089
        unless exists $self->filters->{$fname};
2090
  
2091
      $self->{default_in_filter} = $self->filters->{$fname};
cleanup
Yuki Kimoto authored on 2011-01-12
2092
    }
2093
    
cleanup
Yuki Kimoto authored on 2012-01-20
2094
    return $self;
2095
  }
2096
  
2097
  return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
2098
}
2099

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2100
# DEPRECATED!
2101
sub insert_param {
cleanup
Yuki Kimoto authored on 2012-01-20
2102
  my $self = shift;
2103
  warn "insert_param is DEPRECATED! use values_clause instead";
2104
  return $self->values_clause(@_);
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
2105
}
2106

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2107
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
2108
sub insert_param_tag {
cleanup
Yuki Kimoto authored on 2012-01-20
2109
  warn "insert_param_tag is DEPRECATED! " .
2110
    "use insert_param instead!";
2111
  return shift->insert_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2112
}
2113

            
2114
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
2115
sub update_param_tag {
cleanup
Yuki Kimoto authored on 2012-01-20
2116
  warn "update_param_tag is DEPRECATED! " .
2117
    "use update_param instead";
2118
  return shift->update_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2119
}
cleanup
Yuki Kimoto authored on 2011-03-08
2120
# DEPRECATED!
2121
sub _push_relation {
cleanup
Yuki Kimoto authored on 2012-01-20
2122
  my ($self, $sql, $tables, $relation, $need_where) = @_;
2123
  
2124
  if (keys %{$relation || {}}) {
2125
    $$sql .= $need_where ? 'where ' : 'and ';
2126
    for my $rcolumn (keys %$relation) {
fixed relation full qualifie...
Yuki Kimoto authored on 2012-03-01
2127
      my ($table1) = $rcolumn =~ /^(.+)\.(.+)$/;
2128
      my ($table2) = $relation->{$rcolumn} =~ /^(.+)\.(.+)$/;
cleanup
Yuki Kimoto authored on 2012-01-20
2129
      push @$tables, ($table1, $table2);
2130
      $$sql .= "$rcolumn = " . $relation->{$rcolumn} .  'and ';
cleanup
Yuki Kimoto authored on 2011-03-08
2131
    }
cleanup
Yuki Kimoto authored on 2012-01-20
2132
  }
2133
  $$sql =~ s/and $/ /;
cleanup
Yuki Kimoto authored on 2011-03-08
2134
}
2135

            
2136
# DEPRECATED!
2137
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2012-01-20
2138
  my ($self, $tables, $relation) = @_;
2139
  
2140
  if (keys %{$relation || {}}) {
2141
    for my $rcolumn (keys %$relation) {
fixed relation full qualifie...
Yuki Kimoto authored on 2012-03-01
2142
      my ($table1) = $rcolumn =~ /^(.+)\.(.+)$/;
2143
      my ($table2) = $relation->{$rcolumn} =~ /^(.+)\.(.+)$/;
cleanup
Yuki Kimoto authored on 2012-01-20
2144
      my $table1_exists;
2145
      my $table2_exists;
2146
      for my $table (@$tables) {
2147
        $table1_exists = 1 if $table eq $table1;
2148
        $table2_exists = 1 if $table eq $table2;
2149
      }
2150
      unshift @$tables, $table1 unless $table1_exists;
2151
      unshift @$tables, $table2 unless $table2_exists;
2152
    }
2153
  }
cleanup
Yuki Kimoto authored on 2011-03-08
2154
}
2155

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2158
=head1 NAME
2159

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2164
  use DBIx::Custom;
2165
  
2166
  # Connect
2167
  my $dbi = DBIx::Custom->connect(
2168
    dsn => "dbi:mysql:database=dbname",
2169
    user => 'ken',
2170
    password => '!LFKD%$&',
2171
    option => {mysql_enable_utf8 => 1}
2172
  );
2173

            
2174
  # Insert 
2175
  $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
2176
  
2177
  # Update 
2178
  $dbi->update({title => 'Perl', author => 'Ken'}, table  => 'book',
2179
    where  => {id => 5});
2180
  
2181
  # Delete
2182
  $dbi->delete(table  => 'book', where => {author => 'Ken'});
2183

            
2184
  # Select
2185
  my $result = $dbi->select(table  => 'book',
2186
    column => ['title', 'author'], where  => {author => 'Ken'});
2187

            
2188
  # Select, more complex
2189
  my $result = $dbi->select(
2190
    table  => 'book',
2191
    column => [
2192
      {book => [qw/title author/]},
2193
      {company => ['name']}
2194
    ],
2195
    where  => {'book.author' => 'Ken'},
2196
    join => ['left outer join company on book.company_id = company.id'],
2197
    append => 'order by id limit 5'
2198
  );
2199
  
2200
  # Fetch
2201
  while (my $row = $result->fetch) {
2202
      
2203
  }
2204
  
2205
  # Fetch as hash
2206
  while (my $row = $result->fetch_hash) {
2207
      
2208
  }
2209
  
2210
  # Execute SQL with parameter.
2211
  $dbi->execute(
2212
    "select id from book where author = :author and title like :title",
2213
    {author => 'ken', title => '%Perl%'}
2214
  );
2215
  
fix heading typos
Terrence Brannon authored on 2011-08-17
2216
=head1 DESCRIPTION
removed reconnect method
yuki-kimoto authored on 2010-05-28
2217

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

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

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

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

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2233
Named place holder support
2234

            
2235
=item *
2236

            
cleanup
Yuki Kimoto authored on 2011-07-29
2237
Model support
2238

            
2239
=item *
2240

            
2241
Connection manager support
2242

            
2243
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
2244

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

            
2249
=item *
2250

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

            
2253
=item *
2254

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

            
2257
=back
pod fix
Yuki Kimoto authored on 2011-01-21
2258

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2266
Module documentations - 
cleanup
Yuki Kimoto authored on 2011-07-29
2267
L<DBIx::Custom::Result>,
2268
L<DBIx::Custom::Query>,
2269
L<DBIx::Custom::Where>,
2270
L<DBIx::Custom::Model>,
2271
L<DBIx::Custom::Order>
updated document
yuki-kimoto authored on 2010-08-08
2272

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

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

            
2277
  my $async_conf = $dbi->async_conf;
2278
  $dbi = $dbi->async_conf($conf);
2279

            
2280
Setting when C<async> option is used.
2281

            
2282
  # MySQL
2283
  $dbi->async_conf({
2284
    prepare_attr => {async => 1},
2285
    fh => sub { shift->dbh->mysql_fd }
2286
  })
2287

            
2288
C<prepare_attr> is DBI's C<prepare> method second argument,
2289
C<fh> is callback that return file handle to watch.
2290

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2302
  my $connector = DBIx::Connector->new(
2303
    "dbi:mysql:database=$database",
2304
    $user,
2305
    $password,
2306
    DBIx::Custom->new->default_option
2307
  );
2308
  
2309
  my $dbi = DBIx::Custom->connect(connector => $connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2310

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2314
  my $dbi = DBIx::Custom->connect(
2315
    dsn => $dsn, user => $user, password => $password, connector => 1);
2316
  
2317
  my $connector = $dbi->connector; # DBIx::Connector
cleanup
Yuki Kimoto authored on 2011-08-16
2318

            
2319
Note that L<DBIx::Connector> must be installed.
2320

            
added EXPERIMENTAL default_s...
Yuki Kimoto authored on 2012-03-02
2321
=head2 C<default_schema> EXPERIMETNAL
2322

            
2323
  my $default_schema = $self->default_schema;
2324
  $dbi = $self->default_schema('public');
2325

            
2326
schema name. if database has multiple schema,
2327
type_rule->{into} filter don't work well.
2328

            
2329
If you set C<default_schema>, type_rule->{into} filter work well.
2330

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2340
  my $default_option = $dbi->default_option;
2341
  $dbi = $dbi->default_option($default_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2342

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2346
  {
2347
    RaiseError => 1,
2348
    PrintError => 0,
2349
    AutoCommit => 1,
2350
  }
packaging one directory
yuki-kimoto authored on 2009-11-16
2351

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

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

            
2357
Excluded table regex.
2358
C<each_column>, C<each_table>, C<type_rule>,
2359
and C<setup_model> methods ignore matching tables.
2360

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

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

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

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

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

            
2373
Get last successed SQL executed by C<execute> method.
2374

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2382
  sub {
2383
    my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
2384
    $mon++;
2385
    $year += 1900;
2386
    return sprintf("%04d-%02d-%02d %02d:%02d:%02d");
2387
  }
- insert timestamp option is...
Yuki Kimoto authored on 2011-10-25
2388

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

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

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

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

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

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

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

            
2406
L<DBI> option, used when C<connect> method is executed.
2407
Each value in option override the value of C<default_option>.
2408

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-07-30
2431
You can set quote pair.
2432

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2444
  my $safety_character = $dbi->safety_character;
2445
  $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2446

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

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

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

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

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

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

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

            
2466
Enable DEPRECATED tag parsing functionality, default to 1.
2467
If you want to disable tag parsing functionality, set to 0.
2468

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2483
  [
2484
    {table => 'book', column => 'title', info => {...}},
2485
    {table => 'author', column => 'name', info => {...}}
2486
  ]
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2487

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2490
  my $user_column_info
2491
    = $dbi->get_column_info(exclude_table => qr/^system/);
2492
  $dbi->user_column_info($user_column_info);
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2493

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2499
  my $user_table_info = $dbi->user_table_info;
2500
  $dbi = $dbi->user_table_info($user_table_info);
added test
Yuki Kimoto authored on 2011-08-16
2501

            
2502
You can set the following data.
2503

            
cleanup
Yuki Kimoto authored on 2012-01-20
2504
  [
2505
    {table => 'book', info => {...}},
2506
    {table => 'author', info => {...}}
2507
  ]
added test
Yuki Kimoto authored on 2011-08-16
2508

            
2509
Usually, you can set return value of C<get_table_info>.
2510

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

            
2514
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2515
to find table info.
2516

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2551
  async => sub {
2552
    my ($dbi, $result) = @_;
2553
    ...
2554
  };
2555

            
2556
Database async access. L<AnyEvent> is required.
2557

            
2558
This is C<mysql> async access example.
2559

            
2560
  use AnyEvent;
2561

            
2562
  my $cond = AnyEvent->condvar;
2563

            
2564
  my $timer = AnyEvent->timer(
2565
    interval => 1,
2566
    cb => sub { 1 }
2567
  );
2568

            
2569
  my $count = 0;
2570

            
2571
  $dbi->execute('SELECT SLEEP(1), 3', undef,
2572
    prepare_attr => {async => 1}, statement => 'select',
2573
    async => sub {
2574
      my ($dbi, $result) = @_;
2575
      my $row = $result->fetch_one;
2576
      is($row->[1], 3, 'before');
2577
      $cond->send if ++$count == 2;
2578
    }
2579
  );
2580

            
2581
  $dbi->select('key1', table => 'table1', prepare_attr => {async => 1},
2582
    async => sub {
2583
      my ($dbi, $result) = @_;
2584
      my $row = $result->fetch_one;
2585
      is($row->[0], 1, 'after1');
2586
      $dbi->select('key1', table => 'table1', prepare_attr => {async => 1},
2587
        async => sub {
2588
          my ($dbi, $result) = @_;
2589
          my $row = $result->fetch_one;
2590
          is($row->[0], 1, 'after2');
2591
          $cond->send if ++$count == 2;
2592
        }
2593
      )
2594
    }
2595
  );
2596

            
2597
  $cond->recv;
2598

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

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

            
2603
Create column clause. The follwoing column clause is created.
2604

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2610
  # Separator is hyphen
2611
  $dbi->separator('-');
2612
  
2613
  book.author as "book-author",
2614
  book.title as "book-title"
2615
  
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2616
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
2617

            
cleanup
Yuki Kimoto authored on 2012-01-20
2618
  my $dbi = DBIx::Custom->connect(
2619
    dsn => "dbi:mysql:database=dbname",
2620
    user => 'ken',
2621
    password => '!LFKD%$&',
2622
    option => {mysql_enable_utf8 => 1}
2623
  );
update pod
Yuki Kimoto authored on 2011-03-13
2624

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

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

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

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

            
2635
Get rows count.
2636

            
2637
Options is same as C<select> method's ones.
2638

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2641
  my $model = $dbi->create_model(
2642
    table => 'book',
2643
    primary_key => 'id',
2644
    join => [
2645
      'inner join company on book.comparny_id = company.id'
2646
    ],
2647
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2648

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

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

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

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

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

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

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

            
2665
Execute delete statement.
2666

            
2667
The following opitons are available.
2668

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

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

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

            
2676
=item C<id>
2677

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

            
2681
ID corresponding to C<primary_key>.
2682
You can delete rows by C<id> and C<primary_key>.
2683

            
cleanup
Yuki Kimoto authored on 2012-01-20
2684
  $dbi->delete(
2685
    primary_key => ['id1', 'id2'],
2686
    id => [4, 5],
2687
    table => 'book',
2688
  );
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2689

            
2690
The above is same as the followin one.
2691

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

            
2694
=item C<prefix>
2695

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

            
2698
prefix before table name section.
2699

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

            
2702
=item C<table>
2703

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

            
2706
Table name.
2707

            
2708
=item C<where>
2709

            
2710
Same as C<select> method's C<where> option.
2711

            
2712
=back
2713

            
2714
=head2 C<delete_all>
2715

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

            
2718
Execute delete statement for all rows.
2719
Options is same as C<delete>.
2720

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2723
  $dbi->each_column(
2724
    sub {
2725
      my ($dbi, $table, $column, $column_info) = @_;
2726
      
2727
      my $type = $column_info->{TYPE_NAME};
2728
      
2729
      if ($type eq 'DATE') {
2730
          # ...
2731
      }
2732
    }
2733
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2734

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2750
  $dbi->each_table(
2751
    sub {
2752
      my ($dbi, $table, $table_info) = @_;
2753
      
2754
      my $table_name = $table_info->{TABLE_NAME};
2755
    }
2756
  );
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
2757

            
improved pod
Yuki Kimoto authored on 2011-10-14
2758
Iterate all table informationsfrom in database.
2759
Argument is callback which is executed when one table is found.
2760
Callback receive three arguments, C<DBIx::Custom object>, C<table name>,
2761
C<table information>.
2762

            
2763
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2764
infromation, you can improve the performance of C<each_table> in
2765
the following way.
2766

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

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

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

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

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

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2789
Named placeholder such as C<:title> is replaced by placeholder C<?>.
cleanup
Yuki Kimoto authored on 2012-01-20
2790
  
2791
  # Original
2792
  select * from book where title = :title and author like :author
2793
  
2794
  # Replaced
2795
  select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2796

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2800
  # Original
2801
  select * from book where :title{=} and :author{like}
2802
  
2803
  # Replaced
2804
  select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2805

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

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2812
B<OPTIONS>
2813

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

            
2816
=over 4
2817

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

            
2820
You can filter sql after the sql is build.
2821

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

            
2824
The following one is one example.
2825

            
cleanup
Yuki Kimoto authored on 2012-01-20
2826
  $dbi->select(
2827
    table => 'book',
2828
    column => 'distinct(name)',
2829
    after_build_sql => sub {
2830
      "select count(*) from ($_[0]) as t1"
2831
    }
2832
  );
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2833

            
2834
The following SQL is executed.
2835

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2838
=item C<append>
2839

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

            
2842
Append some statement after SQL.
2843

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

            
2846
  prepare_attr => {async => 1}
2847

            
2848
Statemend handle attributes,
2849
this is L<DBI>'s C<prepare> method second argument.
2850

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

            
2853
Specify database bind data type.
2854

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

            
2858
This is used to bind parameter by C<bind_param> of statment handle.
2859

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2862
=item C<filter>
cleanup
Yuki Kimoto authored on 2012-01-20
2863
  
2864
  filter => {
2865
    title  => sub { uc $_[0] }
2866
    author => sub { uc $_[0] }
2867
  }
2868

            
2869
  # Filter name
2870
  filter => {
2871
    title  => 'upper_case',
2872
    author => 'upper_case'
2873
  }
2874
      
2875
  # At once
2876
  filter => [
2877
    [qw/title author/]  => sub { uc $_[0] }
2878
  ]
updated pod
Yuki Kimoto authored on 2011-06-09
2879

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2892
  my $sql = $query->{sql};
2893
  my $columns = $query->{columns};
2894
  
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
2895
=item C<reuse>
cleanup
Yuki Kimoto authored on 2012-01-20
2896
  
2897
  reuse => $hash_ref
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
2898

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

            
2904
This will improved performance when you want to execute same query repeatedly
2905
because generally creating query object is slow.
2906

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2909
  primary_key => 'id'
2910
  primary_key => ['id1', 'id2']
cleanup
Yuki Kimoto authored on 2011-10-20
2911

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

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

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-09
2921
=item C<table>
cleanup
Yuki Kimoto authored on 2012-01-20
2922
  
2923
  table => 'author'
updated pod
Yuki Kimoto authored on 2011-06-09
2924

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

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

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

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

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

            
2941
Table alias. Key is real table name, value is alias table name.
2942
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2943
on alias table name.
2944

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

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

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

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

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

            
2955
Turn C<into1> type rule off.
2956

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

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

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

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

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

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

            
2969
get column infomation except for one which match C<exclude_table> pattern.
2970

            
cleanup
Yuki Kimoto authored on 2012-01-20
2971
  [
2972
    {table => 'book', column => 'title', info => {...}},
2973
    {table => 'author', column => 'name' info => {...}}
2974
  ]
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2975

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2982
  [
2983
    {table => 'book', info => {...}},
2984
    {table => 'author', info => {...}}
2985
  ]
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2986

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2991
  $dbi->helper(
2992
    find_or_create   => sub {
2993
      my $self = shift;
2994
      
2995
      # Process
2996
    },
2997
    ...
2998
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2999

            
3000
Register helper. These helper is called directly from L<DBIx::Custom> object.
3001

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

            
cleanup
yuki-kimoto authored on 2010-10-17
3004
=head2 C<insert>
3005

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3018
  $dbi->insert(
3019
    [
3020
      {title => 'Perl', author => 'Ken'},
3021
      {title => 'Ruby', author => 'Tom'}
3022
    ],
3023
    table  => 'book'
3024
  );
updated pod
Yuki Kimoto authored on 2011-11-25
3025

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
3029
B<options>
3030

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

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

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

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

            
3040
bulk insert is executed if database support bulk insert and 
3041
multiple parameters is passed to C<insert>.
3042
The SQL like the following one is executed.
3043

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3056
  id => 4
3057
  id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
3058

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3062
  $dbi->insert(
3063
    {title => 'Perl', author => 'Ken'}
3064
    primary_key => ['id1', 'id2'],
3065
    id => [4, 5],
3066
    table => 'book'
3067
  );
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
3068

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3071
  $dbi->insert(
3072
    {id1 => 4, id2 => 5, title => 'Perl', author => 'Ken'},
3073
    table => 'book'
3074
  );
update pod
Yuki Kimoto authored on 2011-03-13
3075

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

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

            
3080
prefix before table name section
3081

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

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

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

            
3088
Table name.
3089

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

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

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

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

            
3098
placeholder wrapped string.
3099

            
3100
If the following statement
3101

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

            
3105
is executed, the following SQL is executed.
3106

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

            
update pod
Yuki Kimoto authored on 2011-03-13
3109
=back
3110

            
3111
=over 4
3112

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3120
  lib / MyModel.pm
3121
      / MyModel / book.pm
3122
                / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
3123

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3128
  package MyModel;
3129
  use DBIx::Custom::Model -base;
3130
  
3131
  1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
3132

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3137
  package MyModel::book;
3138
  use MyModel -base;
3139
  
3140
  1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
3141

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3144
  package MyModel::company;
3145
  use MyModel -base;
3146
  
3147
  1;
3148
  
updated pod
Yuki Kimoto authored on 2011-06-21
3149
MyModel::book and MyModel::company is included by C<include_model>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
3150

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

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

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

            
3158
  lib / MyModel.pm
3159
      / MyModel / main / book.pm
3160
                       / company.pm
3161

            
3162
  my $main_book = $self->model('main.book');
3163

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

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

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

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

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

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

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

            
3178
Create a new L<DBIx::Custom::Mapper> object.
3179

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

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

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

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

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

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

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

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

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

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

            
3201
Create column clause for myself. The follwoing column clause is created.
3202

            
cleanup
Yuki Kimoto authored on 2012-01-20
3203
  book.author as author,
3204
  book.title as title
cleanup
Yuki Kimoto authored on 2011-03-21
3205

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3208
  my $dbi = DBIx::Custom->new(
3209
    dsn => "dbi:mysql:database=dbname",
3210
    user => 'ken',
3211
    password => '!LFKD%$&',
3212
    option => {mysql_enable_utf8 => 1}
3213
  );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
3214

            
3215
Create a new L<DBIx::Custom> object.
3216

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

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

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

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

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

            
3228
Create a new L<DBIx::Custom::Order> object.
3229

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

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

            
3234
Quote string by value of C<quote>.
3235

            
cleanup
yuki-kimoto authored on 2010-10-17
3236
=head2 C<register_filter>
3237

            
cleanup
Yuki Kimoto authored on 2012-01-20
3238
  $dbi->register_filter(
3239
    # Time::Piece object to database DATE format
3240
    tp_to_date => sub {
3241
      my $tp = shift;
3242
      return $tp->strftime('%Y-%m-%d');
3243
    },
3244
    # database DATE format to Time::Piece object
3245
    date_to_tp => sub {
3246
      my $date = shift;
3247
      return Time::Piece->strptime($date, '%Y-%m-%d');
3248
    }
3249
  );
3250
  
update pod
Yuki Kimoto authored on 2011-03-13
3251
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
3252

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3255
  my $result = $dbi->select(
3256
    column => ['author', 'title'],
3257
    table  => 'book',
3258
    where  => {author => 'Ken'},
3259
  );
3260
  
updated document
Yuki Kimoto authored on 2011-06-09
3261
Execute select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3262

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

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

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3274
=item C<column>
cleanup
Yuki Kimoto authored on 2012-01-20
3275
  
3276
  column => 'author'
3277
  column => ['author', 'title']
updated document
Yuki Kimoto authored on 2011-06-09
3278

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3287
  column => [
3288
    {book => [qw/author title/]},
3289
    {person => [qw/name age/]}
3290
  ]
updated pod
Yuki Kimoto authored on 2011-06-07
3291

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3294
  book.author as "book.author",
3295
  book.title as "book.title",
3296
  person.name as "person.name",
3297
  person.age as "person.age"
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3298

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3302
  column => [
3303
    ['date(book.register_datetime)' => 'book.register_date']
3304
  ];
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3305

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3312
  id => 4
3313
  id => [4, 5]
updated document
Yuki Kimoto authored on 2011-06-09
3314

            
3315
ID corresponding to C<primary_key>.
3316
You can select rows by C<id> and C<primary_key>.
3317

            
cleanup
Yuki Kimoto authored on 2012-01-20
3318
  $dbi->select(
3319
    primary_key => ['id1', 'id2'],
3320
    id => [4, 5],
3321
    table => 'book'
3322
  );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3323

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3326
  $dbi->select(
3327
    where => {id1 => 4, id2 => 5},
3328
    table => 'book'
3329
  );
3330
  
cleanup
Yuki Kimoto authored on 2011-10-20
3331
=item C<param>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3332

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

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

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

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

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

            
3347
Prefix of column cluase
3348

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3353
  join => [
3354
    'left outer join company on book.company_id = company_id',
3355
    'left outer join location on company.location_id = location.id'
3356
  ]
3357
      
updated document
Yuki Kimoto authored on 2011-06-09
3358
Join clause. If column cluase or where clause contain table name like "company.name",
3359
join clausees needed when SQL is created is used automatically.
update pod
Yuki Kimoto authored on 2011-03-12
3360

            
cleanup
Yuki Kimoto authored on 2012-01-20
3361
  $dbi->select(
3362
    table => 'book',
3363
    column => ['company.location_id as location_id'],
3364
    where => {'company.name' => 'Orange'},
3365
    join => [
3366
      'left outer join company on book.company_id = company.id',
3367
      'left outer join location on company.location_id = location.id'
3368
    ]
3369
  );
update pod
Yuki Kimoto authored on 2011-03-12
3370

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3374
  select company.location_id as location_id
3375
  from book
3376
    left outer join company on book.company_id = company.id
3377
  where company.name = ?;
update pod
Yuki Kimoto authored on 2011-03-12
3378

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3379
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
3380
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3381

            
cleanup
Yuki Kimoto authored on 2012-01-20
3382
  $dbi->select(
3383
    table => 'book',
3384
    column => ['company.location_id as location_id'],
3385
    where => {'company.name' => 'Orange'},
3386
    join => [
3387
      {
3388
        clause => 'left outer join location on company.location_id = location.id',
3389
        table => ['company', 'location']
3390
      }
3391
    ]
3392
  );
added join new syntax
Yuki Kimoto authored on 2011-07-28
3393

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3398
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3399

            
updated document
Yuki Kimoto authored on 2011-06-09
3400
=item C<where>
cleanup
Yuki Kimoto authored on 2012-01-20
3401
  
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3402
  # (1) Hash reference
3403
  where => {author => 'Ken', 'title' => ['Perl', 'Ruby']}
3404
  # -> where author = 'Ken' and title in ('Perl', 'Ruby')
cleanup
Yuki Kimoto authored on 2012-01-20
3405
  
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3406
  # (2) DBIx::Custom::Where object
cleanup
Yuki Kimoto authored on 2012-01-20
3407
  where => $dbi->where(
3408
    clause => ['and', ':author{=}', ':title{like}'],
3409
    param  => {author => 'Ken', title => '%Perl%'}
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3410
  )
3411
  # -> where author = 'Ken' and title like '%Perl%'
cleanup
Yuki Kimoto authored on 2012-01-20
3412
  
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3413
  # (3) Array reference[Array refenrece, Hash reference]
cleanup
Yuki Kimoto authored on 2012-01-20
3414
  where => [
3415
    ['and', ':author{=}', ':title{like}'],
3416
    {author => 'Ken', title => '%Perl%'}
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3417
  ]
3418
  # -> where author = 'Ken' and title like '%Perl%'
3419
  
3420
  # (4) Array reference[String, Hash reference]
3421
  where => [
3422
    ':author{=} and :title{like}',
3423
    {author => 'Ken', title => '%Perl%'}
3424
  ]
3425
  #  -> where author = 'Ken' and title like '%Perl%'
cleanup
Yuki Kimoto authored on 2012-01-20
3426
  
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3427
  # (5) String
cleanup
Yuki Kimoto authored on 2012-01-20
3428
  where => 'title is null'
- added where => {name => [v...
Yuki Kimoto authored on 2012-02-28
3429
  #  -> where title is null
update pod
Yuki Kimoto authored on 2011-03-12
3430

            
improved where document
Yuki Kimoto authored on 2012-03-01
3431
Where clause.
3432
See also L<DBIx::Custom::Where> to know how to create where clause.
cleanup
Yuki Kimoto authored on 2012-01-20
3433
  
update pod
Yuki Kimoto authored on 2011-03-12
3434
=back
cleanup
Yuki Kimoto authored on 2011-03-08
3435

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3438
  $dbi->setup_model;
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3439

            
3440
Setup all model objects.
3441
C<columns> of model object is automatically set, parsing database information.
3442

            
3443
=head2 C<type_rule>
3444

            
cleanup
Yuki Kimoto authored on 2012-01-20
3445
  $dbi->type_rule(
3446
    into1 => {
3447
      date => sub { ... },
3448
      datetime => sub { ... }
3449
    },
3450
    into2 => {
3451
      date => sub { ... },
3452
      datetime => sub { ... }
3453
    },
3454
    from1 => {
3455
      # DATE
3456
      9 => sub { ... },
3457
      # DATETIME or TIMESTAMP
3458
      11 => sub { ... },
3459
    }
3460
    from2 => {
3461
      # DATE
3462
      9 => sub { ... },
3463
      # DATETIME or TIMESTAMP
3464
      11 => sub { ... },
3465
    }
3466
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3467

            
3468
Filtering rule when data is send into and get from database.
3469
This has a little complex problem.
3470

            
3471
In C<into1> and C<into2> you can specify
3472
type name as same as type name defined
3473
by create table, such as C<DATETIME> or C<DATE>.
3474

            
3475
Note that type name and data type don't contain upper case.
3476
If these contain upper case charactor, you convert it to lower case.
3477

            
3478
C<into2> is executed after C<into1>.
3479

            
3480
Type rule of C<into1> and C<into2> is enabled on the following
3481
column name.
3482

            
3483
=over 4
3484

            
3485
=item 1. column name
3486

            
cleanup
Yuki Kimoto authored on 2012-01-20
3487
  issue_date
3488
  issue_datetime
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3489

            
3490
This need C<table> option in each method.
3491

            
3492
=item 2. table name and column name, separator is dot
3493

            
cleanup
Yuki Kimoto authored on 2012-01-20
3494
  book.issue_date
3495
  book.issue_datetime
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3496

            
3497
=back
3498

            
3499
You get all type name used in database by C<available_typename>.
3500

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

            
3503
In C<from1> and C<from2> you specify data type, not type name.
3504
C<from2> is executed after C<from1>.
3505
You get all data type by C<available_datatype>.
3506

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

            
3509
You can also specify multiple types at once.
3510

            
cleanup
Yuki Kimoto authored on 2012-01-20
3511
  $dbi->type_rule(
3512
    into1 => [
3513
      [qw/DATE DATETIME/] => sub { ... },
3514
    ],
3515
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3516

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

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

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

            
3523
If you want to set constant value to row data, use scalar reference
3524
as parameter value.
3525

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3543
  $dbi->update(
3544
    {title => 'Perl', author => 'Ken'}
3545
    primary_key => ['id1', 'id2'],
3546
    id => [4, 5],
3547
    table => 'book'
3548
  );
update pod
Yuki Kimoto authored on 2011-03-13
3549

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3552
  $dbi->update(
3553
    {title => 'Perl', author => 'Ken'}
3554
    where => {id1 => 4, id2 => 5},
3555
    table => 'book'
3556
  );
update pod
Yuki Kimoto authored on 2011-03-13
3557

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

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

            
3562
prefix before table name section
3563

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

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

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

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

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

            
3574
Same as C<select> method's C<where> option.
3575

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

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

            
3580
placeholder wrapped string.
3581

            
3582
If the following statement
3583

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

            
3587
is executed, the following SQL is executed.
3588

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

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

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-08
3599
=back
update pod
Yuki Kimoto authored on 2011-03-13
3600

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

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

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

            
- id option work if id count...
Yuki Kimoto authored on 2011-11-03
3608
=head2 C<update_or_insert>
cleanup
Yuki Kimoto authored on 2012-01-20
3609
  
3610
  # ID
3611
  $dbi->update_or_insert(
3612
    {title => 'Perl'},
3613
    table => 'book',
3614
    id => 1,
3615
    primary_key => 'id',
3616
    option => {
3617
      select => {
3618
         append => 'for update'
3619
      }
3620
    }
3621
  );
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3622

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3623
Update or insert.
3624

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

            
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3629
C<OPTIONS>
3630

            
3631
C<update_or_insert> method use all common option
3632
in C<select>, C<update>, C<delete>, and has the following new ones.
3633

            
3634
=over 4
3635

            
3636
=item C<option>
3637

            
cleanup
Yuki Kimoto authored on 2012-01-20
3638
  option => {
3639
    select => {
3640
      append => '...'
3641
    },
3642
    insert => {
3643
      prefix => '...'
3644
    },
3645
    update => {
3646
      filter => {}
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3647
    }
cleanup
Yuki Kimoto authored on 2012-01-20
3648
  }
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3649

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

            
3653
=over 4
3654

            
3655
=item C<select_option>
3656

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

            
3659
select method option,
3660
select method is used to check the row is already exists.
3661

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

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

            
3666
Show data type of the columns of specified table.
3667

            
cleanup
Yuki Kimoto authored on 2012-01-20
3668
  book
3669
  title: 5
3670
  issue_date: 91
update pod
Yuki Kimoto authored on 2011-08-10
3671

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

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

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

            
3678
Show tables.
3679

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

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

            
3684
Show type name of the columns of specified table.
3685

            
cleanup
Yuki Kimoto authored on 2012-01-20
3686
  book
3687
  title: varchar
3688
  issue_date: date
update pod
Yuki Kimoto authored on 2011-08-10
3689

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

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

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

            
3696
Create values clause.
3697

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

            
3700
You can use this in insert statement.
3701

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

            
3704
=head2 C<where>
3705

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

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

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

            
3716
=head2 C<DBIX_CUSTOM_DEBUG>
3717

            
3718
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3719
executed SQL and bind values are printed to STDERR.
3720

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

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

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

            
3727
If you set DBIX_CUSTOM_TAG_PARSE to 0, tag parsing is off.
3728

            
3729
=head2 C<DBIX_CUSTOM_DISABLE_MODEL_EXECUTE>
3730

            
3731
If you set DBIX_CUSTOM_DISABLE_MODEL_EXECUTE to 1,
3732
L<DBIx::Custom::Model> execute method call L<DBIx::Custom> execute.
3733

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

            
3736
L<DBIx::Custom>
3737

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

            
3794
L<DBIx::Custom::Model>
3795

            
cleanup
Yuki Kimoto authored on 2012-01-20
3796
  # Attribute methods
3797
  execute # will be removed at 2017/1/1
3798
  method # will be removed at 2017/1/1
3799
  filter # will be removed at 2017/1/1
3800
  name # will be removed at 2017/1/1
3801
  type # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3802

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

            
3805
This module is DEPRECATED! # will be removed at 2017/1/1
cleanup
Yuki Kimoto authored on 2012-01-20
3806
  
3807
  # Attribute methods
3808
  default_filter # will be removed at 2017/1/1
3809
  table # will be removed at 2017/1/1
3810
  filters # will be removed at 2017/1/1
3811
  
3812
  # Methods
3813
  filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3814

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

            
3817
This module is DEPRECATED! # will be removed at 2017/1/1
cleanup
Yuki Kimoto authored on 2012-01-20
3818
  
3819
  # Attribute methods
3820
  tags # will be removed at 2017/1/1
3821
  tag_processors # will be removed at 2017/1/1
3822
  
3823
  # Methods
3824
  register_tag # will be removed at 2017/1/1
3825
  register_tag_processor # will be removed at 2017/1/1
3826
  
3827
  # Others
3828
  build_query("select * from {= title}"); # tag parsing functionality
3829
                                          # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3830

            
3831
L<DBIx::Custom::Result>
cleanup
Yuki Kimoto authored on 2012-01-20
3832
  
3833
  # Attribute methods
3834
  filter_check # will be removed at 2017/1/1
3835
  
3836
  # Methods
3837
  fetch_first # will be removed at 2017/2/1
3838
  fetch_hash_first # will be removed 2017/2/1
3839
  filter_on # will be removed at 2017/1/1
3840
  filter_off # will be removed at 2017/1/1
3841
  end_filter # will be removed at 2017/1/1
3842
  remove_end_filter # will be removed at 2017/1/1
3843
  remove_filter # will be removed at 2017/1/1
3844
  default_filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3845

            
3846
L<DBIx::Custom::Tag>
3847

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

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

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

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

            
3858
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings
3859
except for attribute method.
3860
You can check all DEPRECATED functionalities by document.
3861
DEPRECATED functionality is removed after five years,
3862
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
3863
I extend one year each time he tell me it.
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3864

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

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

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

            
3871
C<< <kimoto.yuki at gmail.com> >>
3872

            
3873
L<http://github.com/yuki-kimoto/DBIx-Custom>
3874

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3875
=head1 AUTHOR
3876

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

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

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

            
3883
This program is free software; you can redistribute it and/or modify it
3884
under the same terms as Perl itself.
3885

            
3886
=cut