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

            
- DBIx::Custom::Mapper::map ...
Yuki Kimoto authored on 2012-02-29
5
our $VERSION = '0.23';
cleanup
yuki-kimoto authored on 2009-12-22
6

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

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

            
added EXPERIMENTAL 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};
renamed setup_model database...
Yuki Kimoto authored on 2012-03-02
1129
      return if exists $opt{schema} && $opt{schema} ne $schema;
improved test
Yuki Kimoto authored on 2012-03-01
1130
      
cleanup
Yuki Kimoto authored on 2012-03-02
1131
      $table = "$schema.$table" if exists $opt{prefix};
cleanup
Yuki Kimoto authored on 2012-01-20
1132
      if (my $model = $self->models->{$table}) {
1133
        push @{$model->columns}, $column;
1134
      }
1135
    }
1136
  );
1137
  return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
1138
}
1139

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1254
sub update {
cleanup
Yuki Kimoto authored on 2012-01-20
1255
  my $self = shift;
1256

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
1422
    # Create query
1423
    my $tag_parse = exists $ENV{DBIX_CUSTOM_TAG_PARSE}
1424
      ? $ENV{DBIX_CUSTOM_TAG_PARSE} : $self->{tag_parse};
1425

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

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

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

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

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

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

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

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

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

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

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

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

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1706
sub _quote {
cleanup
Yuki Kimoto authored on 2012-01-20
1707
  my $self = shift;
1708
  return $self->{reserved_word_quote} || $self->quote || '';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1709
}
1710

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
1724
  return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1725
}
1726

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

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

            
1746
  $where ||= {};
1747
  $where = $self->_id_to_param($id, $primary_key, $table) if defined $id;
1748
  $where_param ||= {};
1749
  my $w = {};
1750

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

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

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
1924
  return $tag;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1925
}
1926

            
updated pod
Yuki Kimoto authored on 2011-06-21
1927
# DEPRECATED!
1928
sub create_query {
cleanup
Yuki Kimoto authored on 2012-01-20
1929
  warn "create_query is DEPRECATED! use query option of each method";
1930
  shift->_create_query(@_);
updated pod
Yuki Kimoto authored on 2011-06-21
1931
}
1932

            
cleanup
Yuki Kimoto authored on 2011-06-13
1933
# DEPRECATED!
1934
sub apply_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
1935
  my $self = shift;
1936
  
1937
  warn "apply_filter is DEPRECATED!";
1938
  return $self->_apply_filter(@_);
cleanup
Yuki Kimoto authored on 2011-06-13
1939
}
1940

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

            
1945
  warn "select_at is DEPRECATED! use select method id option instead";
1946

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-06-08
1979
# DEPRECATED!
1980
sub update_at {
cleanup
Yuki Kimoto authored on 2012-01-20
1981
  my $self = shift;
1982

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-01-25
2070
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
2071
sub default_fetch_filter {
cleanup
Yuki Kimoto authored on 2012-01-20
2072
  my $self = shift;
added warnings
Yuki Kimoto authored on 2011-06-07
2073

            
cleanup
Yuki Kimoto authored on 2012-01-20
2074
  warn "default_fetch_filter is DEPRECATED!";
2075
  
2076
  if (@_) {
2077
    my $fname = $_[0];
many changed
Yuki Kimoto authored on 2011-01-23
2078

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

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

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

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

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

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2153
=head1 NAME
2154

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

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

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

            
2169
  # Insert 
2170
  $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
2171
  
2172
  # Update 
2173
  $dbi->update({title => 'Perl', author => 'Ken'}, table  => 'book',
2174
    where  => {id => 5});
2175
  
2176
  # Delete
2177
  $dbi->delete(table  => 'book', where => {author => 'Ken'});
2178

            
2179
  # Select
2180
  my $result = $dbi->select(table  => 'book',
2181
    column => ['title', 'author'], where  => {author => 'Ken'});
2182

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

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

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

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

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

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2228
Named place holder support
2229

            
2230
=item *
2231

            
cleanup
Yuki Kimoto authored on 2011-07-29
2232
Model support
2233

            
2234
=item *
2235

            
2236
Connection manager support
2237

            
2238
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
2239

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

            
2244
=item *
2245

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

            
2248
=item *
2249

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

            
2252
=back
pod fix
Yuki Kimoto authored on 2011-01-21
2253

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2261
Module documentations - 
cleanup
Yuki Kimoto authored on 2011-07-29
2262
L<DBIx::Custom::Result>,
2263
L<DBIx::Custom::Query>,
2264
L<DBIx::Custom::Where>,
2265
L<DBIx::Custom::Model>,
2266
L<DBIx::Custom::Order>
updated document
yuki-kimoto authored on 2010-08-08
2267

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

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

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

            
2275
Setting when C<async> option is used.
2276

            
2277
  # MySQL
2278
  $dbi->async_conf({
2279
    prepare_attr => {async => 1},
2280
    fh => sub { shift->dbh->mysql_fd }
2281
  })
2282

            
2283
C<prepare_attr> is DBI's C<prepare> method second argument,
2284
C<fh> is callback that return file handle to watch.
2285

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2309
  my $dbi = DBIx::Custom->connect(
2310
    dsn => $dsn, user => $user, password => $password, connector => 1);
2311
  
2312
  my $connector = $dbi->connector; # DBIx::Connector
cleanup
Yuki Kimoto authored on 2011-08-16
2313

            
2314
Note that L<DBIx::Connector> must be installed.
2315

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

            
2318
  my $default_schema = $self->default_schema;
2319
  $dbi = $self->default_schema('public');
2320

            
2321
schema name. if database has multiple schema,
2322
type_rule->{into} filter don't work well.
2323

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2335
  my $default_option = $dbi->default_option;
2336
  $dbi = $dbi->default_option($default_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
2337

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2341
  {
2342
    RaiseError => 1,
2343
    PrintError => 0,
2344
    AutoCommit => 1,
2345
  }
packaging one directory
yuki-kimoto authored on 2009-11-16
2346

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

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

            
2352
Excluded table regex.
2353
C<each_column>, C<each_table>, C<type_rule>,
2354
and C<setup_model> methods ignore matching tables.
2355

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

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

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

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

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

            
2368
Get last successed SQL executed by C<execute> method.
2369

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

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

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

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

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

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

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

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

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

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

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

            
2401
L<DBI> option, used when C<connect> method is executed.
2402
Each value in option override the value of C<default_option>.
2403

            
cleanup
yuki-kimoto authored on 2010-10-17
2404
=head2 C<password>
2405

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-07-30
2426
You can set quote pair.
2427

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2439
  my $safety_character = $dbi->safety_character;
2440
  $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2441

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

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

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

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

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

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

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

            
2461
Enable DEPRECATED tag parsing functionality, default to 1.
2462
If you want to disable tag parsing functionality, set to 0.
2463

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2485
  my $user_column_info
2486
    = $dbi->get_column_info(exclude_table => qr/^system/);
2487
  $dbi->user_column_info($user_column_info);
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2488

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2494
  my $user_table_info = $dbi->user_table_info;
2495
  $dbi = $dbi->user_table_info($user_table_info);
added test
Yuki Kimoto authored on 2011-08-16
2496

            
2497
You can set the following data.
2498

            
cleanup
Yuki Kimoto authored on 2012-01-20
2499
  [
2500
    {table => 'book', info => {...}},
2501
    {table => 'author', info => {...}}
2502
  ]
added test
Yuki Kimoto authored on 2011-08-16
2503

            
2504
Usually, you can set return value of C<get_table_info>.
2505

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

            
2509
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2510
to find table info.
2511

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2546
  async => sub {
2547
    my ($dbi, $result) = @_;
2548
    ...
2549
  };
2550

            
2551
Database async access. L<AnyEvent> is required.
2552

            
2553
This is C<mysql> async access example.
2554

            
2555
  use AnyEvent;
2556

            
2557
  my $cond = AnyEvent->condvar;
2558

            
2559
  my $timer = AnyEvent->timer(
2560
    interval => 1,
2561
    cb => sub { 1 }
2562
  );
2563

            
2564
  my $count = 0;
2565

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

            
2576
  $dbi->select('key1', table => 'table1', prepare_attr => {async => 1},
2577
    async => sub {
2578
      my ($dbi, $result) = @_;
2579
      my $row = $result->fetch_one;
2580
      is($row->[0], 1, 'after1');
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, 'after2');
2586
          $cond->send if ++$count == 2;
2587
        }
2588
      )
2589
    }
2590
  );
2591

            
2592
  $cond->recv;
2593

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

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

            
2598
Create column clause. The follwoing column clause is created.
2599

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

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

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

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

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

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

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

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

            
2630
Get rows count.
2631

            
2632
Options is same as C<select> method's ones.
2633

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

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

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

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

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

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

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

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

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

            
2660
Execute delete statement.
2661

            
2662
The following opitons are available.
2663

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

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

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

            
2671
=item C<id>
2672

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

            
2676
ID corresponding to C<primary_key>.
2677
You can delete rows by C<id> and C<primary_key>.
2678

            
cleanup
Yuki Kimoto authored on 2012-01-20
2679
  $dbi->delete(
2680
    primary_key => ['id1', 'id2'],
2681
    id => [4, 5],
2682
    table => 'book',
2683
  );
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2684

            
2685
The above is same as the followin one.
2686

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

            
2689
=item C<prefix>
2690

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

            
2693
prefix before table name section.
2694

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

            
2697
=item C<table>
2698

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

            
2701
Table name.
2702

            
2703
=item C<where>
2704

            
2705
Same as C<select> method's C<where> option.
2706

            
2707
=back
2708

            
2709
=head2 C<delete_all>
2710

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

            
2713
Execute delete statement for all rows.
2714
Options is same as C<delete>.
2715

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

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

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

            
2735
If C<user_column_info> is set, C<each_column> method use C<user_column_info>
2736
infromation, you can improve the performance of C<each_column> in
2737
the following way.
2738

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2745
  $dbi->each_table(
2746
    sub {
2747
      my ($dbi, $table, $table_info) = @_;
2748
      
2749
      my $table_name = $table_info->{TABLE_NAME};
2750
    }
2751
  );
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
2752

            
improved pod
Yuki Kimoto authored on 2011-10-14
2753
Iterate all table informationsfrom in database.
2754
Argument is callback which is executed when one table is found.
2755
Callback receive three arguments, C<DBIx::Custom object>, C<table name>,
2756
C<table information>.
2757

            
2758
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2759
infromation, you can improve the performance of C<each_table> in
2760
the following way.
2761

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2795
  # Original
2796
  select * from book where :title{=} and :author{like}
2797
  
2798
  # Replaced
2799
  select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2800

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

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2807
B<OPTIONS>
2808

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

            
2811
=over 4
2812

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

            
2815
You can filter sql after the sql is build.
2816

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

            
2819
The following one is one example.
2820

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

            
2829
The following SQL is executed.
2830

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2833
=item C<append>
2834

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

            
2837
Append some statement after SQL.
2838

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

            
2841
  prepare_attr => {async => 1}
2842

            
2843
Statemend handle attributes,
2844
this is L<DBI>'s C<prepare> method second argument.
2845

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

            
2848
Specify database bind data type.
2849

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

            
2853
This is used to bind parameter by C<bind_param> of statment handle.
2854

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2857
=item C<filter>
cleanup
Yuki Kimoto authored on 2012-01-20
2858
  
2859
  filter => {
2860
    title  => sub { uc $_[0] }
2861
    author => sub { uc $_[0] }
2862
  }
2863

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2887
  my $sql = $query->{sql};
2888
  my $columns = $query->{columns};
2889
  
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
2890
=item C<reuse>
cleanup
Yuki Kimoto authored on 2012-01-20
2891
  
2892
  reuse => $hash_ref
- DBIx::Custom::QueryBuilder...
Yuki Kimoto authored on 2011-11-04
2893

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

            
2899
This will improved performance when you want to execute same query repeatedly
2900
because generally creating query object is slow.
2901

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2904
  primary_key => 'id'
2905
  primary_key => ['id1', 'id2']
cleanup
Yuki Kimoto authored on 2011-10-20
2906

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

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

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-09
2916
=item C<table>
cleanup
Yuki Kimoto authored on 2012-01-20
2917
  
2918
  table => 'author'
updated pod
Yuki Kimoto authored on 2011-06-09
2919

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

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

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

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

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

            
2936
Table alias. Key is real table name, value is alias table name.
2937
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2938
on alias table name.
2939

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

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

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

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

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

            
2950
Turn C<into1> type rule off.
2951

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

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

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

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

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

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

            
2964
get column infomation except for one which match C<exclude_table> pattern.
2965

            
cleanup
Yuki Kimoto authored on 2012-01-20
2966
  [
2967
    {table => 'book', column => 'title', info => {...}},
2968
    {table => 'author', column => 'name' info => {...}}
2969
  ]
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
2970

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2977
  [
2978
    {table => 'book', info => {...}},
2979
    {table => 'author', info => {...}}
2980
  ]
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2981

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
2986
  $dbi->helper(
2987
    find_or_create   => sub {
2988
      my $self = shift;
2989
      
2990
      # Process
2991
    },
2992
    ...
2993
  );
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2994

            
2995
Register helper. These helper is called directly from L<DBIx::Custom> object.
2996

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

            
cleanup
yuki-kimoto authored on 2010-10-17
2999
=head2 C<insert>
3000

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3013
  $dbi->insert(
3014
    [
3015
      {title => 'Perl', author => 'Ken'},
3016
      {title => 'Ruby', author => 'Tom'}
3017
    ],
3018
    table  => 'book'
3019
  );
updated pod
Yuki Kimoto authored on 2011-11-25
3020

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
3024
B<options>
3025

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

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

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

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

            
3035
bulk insert is executed if database support bulk insert and 
3036
multiple parameters is passed to C<insert>.
3037
The SQL like the following one is executed.
3038

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3051
  id => 4
3052
  id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
3053

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3066
  $dbi->insert(
3067
    {id1 => 4, id2 => 5, title => 'Perl', author => 'Ken'},
3068
    table => 'book'
3069
  );
update pod
Yuki Kimoto authored on 2011-03-13
3070

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

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

            
3075
prefix before table name section
3076

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

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

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

            
3083
Table name.
3084

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

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

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

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

            
3093
placeholder wrapped string.
3094

            
3095
If the following statement
3096

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

            
3100
is executed, the following SQL is executed.
3101

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

            
update pod
Yuki Kimoto authored on 2011-03-13
3104
=back
3105

            
3106
=over 4
3107

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3115
  lib / MyModel.pm
3116
      / MyModel / book.pm
3117
                / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
3118

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3123
  package MyModel;
3124
  use DBIx::Custom::Model -base;
3125
  
3126
  1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
3127

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3132
  package MyModel::book;
3133
  use MyModel -base;
3134
  
3135
  1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
3136

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3139
  package MyModel::company;
3140
  use MyModel -base;
3141
  
3142
  1;
3143
  
updated pod
Yuki Kimoto authored on 2011-06-21
3144
MyModel::book and MyModel::company is included by C<include_model>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
3145

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

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

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

            
3153
  lib / MyModel.pm
3154
      / MyModel / main / book.pm
3155
                       / company.pm
3156

            
3157
  my $main_book = $self->model('main.book');
3158

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

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

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

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

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

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

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

            
3173
Create a new L<DBIx::Custom::Mapper> object.
3174

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

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

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

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

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

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

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

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

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

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

            
3196
Create column clause for myself. The follwoing column clause is created.
3197

            
cleanup
Yuki Kimoto authored on 2012-01-20
3198
  book.author as author,
3199
  book.title as title
cleanup
Yuki Kimoto authored on 2011-03-21
3200

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

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

            
3210
Create a new L<DBIx::Custom> object.
3211

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

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

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

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

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

            
3223
Create a new L<DBIx::Custom::Order> object.
3224

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

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

            
3229
Quote string by value of C<quote>.
3230

            
cleanup
yuki-kimoto authored on 2010-10-17
3231
=head2 C<register_filter>
3232

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

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

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

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

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

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3269
=item C<column>
cleanup
Yuki Kimoto authored on 2012-01-20
3270
  
3271
  column => 'author'
3272
  column => ['author', 'title']
updated document
Yuki Kimoto authored on 2011-06-09
3273

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3282
  column => [
3283
    {book => [qw/author title/]},
3284
    {person => [qw/name age/]}
3285
  ]
updated pod
Yuki Kimoto authored on 2011-06-07
3286

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3289
  book.author as "book.author",
3290
  book.title as "book.title",
3291
  person.name as "person.name",
3292
  person.age as "person.age"
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3293

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3297
  column => [
3298
    ['date(book.register_datetime)' => 'book.register_date']
3299
  ];
- select() column option can...
Yuki Kimoto authored on 2011-06-08
3300

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3307
  id => 4
3308
  id => [4, 5]
updated document
Yuki Kimoto authored on 2011-06-09
3309

            
3310
ID corresponding to C<primary_key>.
3311
You can select rows by C<id> and C<primary_key>.
3312

            
cleanup
Yuki Kimoto authored on 2012-01-20
3313
  $dbi->select(
3314
    primary_key => ['id1', 'id2'],
3315
    id => [4, 5],
3316
    table => 'book'
3317
  );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3318

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

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

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

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

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

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

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

            
3342
Prefix of column cluase
3343

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3369
  select company.location_id as location_id
3370
  from book
3371
    left outer join company on book.company_id = company.id
3372
  where company.name = ?;
update pod
Yuki Kimoto authored on 2011-03-12
3373

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3374
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
3375
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3376

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3393
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3394

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

            
improved where document
Yuki Kimoto authored on 2012-03-01
3426
Where clause.
3427
See also L<DBIx::Custom::Where> to know how to create where clause.
cleanup
Yuki Kimoto authored on 2012-01-20
3428
  
update pod
Yuki Kimoto authored on 2011-03-12
3429
=back
cleanup
Yuki Kimoto authored on 2011-03-08
3430

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
3433
  $dbi->setup_model;
renamed setup_model database...
Yuki Kimoto authored on 2012-03-02
3434
  $dbi->setup_model(schema => 'main');
3435
  $dbi->setup_model(schema => 'main', prefix => 1);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3436

            
3437
Setup all model objects.
3438
C<columns> of model object is automatically set, parsing database information.
3439

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

            
3442
If C<prefix> option is specified, Target model is qualified by dabtabase name
3443
like C<main.book>.
3444

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

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

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

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

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

            
3480
C<into2> is executed after C<into1>.
3481

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

            
3485
=over 4
3486

            
3487
=item 1. column name
3488

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

            
3492
This need C<table> option in each method.
3493

            
3494
=item 2. table name and column name, separator is dot
3495

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

            
3499
=back
3500

            
3501
You get all type name used in database by C<available_typename>.
3502

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

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

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

            
3511
You can also specify multiple types at once.
3512

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
3564
prefix before table name section
3565

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

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

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

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

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

            
3576
Same as C<select> method's C<where> option.
3577

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

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

            
3582
placeholder wrapped string.
3583

            
3584
If the following statement
3585

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

            
3589
is executed, the following SQL is executed.
3590

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

            
- "created_at" option is ren...
Yuki Kimoto authored on 2012-02-11
3593
=item C<mtime>
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
  mtime => 'modified_time'
added EXPERIMENTAL insert cr...
Yuki Kimoto authored on 2011-10-25
3596

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

            
updated pod
Yuki Kimoto authored on 2011-06-08
3601
=back
update pod
Yuki Kimoto authored on 2011-03-13
3602

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

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

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

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

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3625
Update or insert.
3626

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

            
- EXPERIMENTAL update_or_ins...
Yuki Kimoto authored on 2011-10-27
3631
C<OPTIONS>
3632

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

            
3636
=over 4
3637

            
3638
=item C<option>
3639

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

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

            
3655
=over 4
3656

            
3657
=item C<select_option>
3658

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

            
3661
select method option,
3662
select method is used to check the row is already exists.
3663

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

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

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

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

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

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

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

            
3680
Show tables.
3681

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

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

            
3686
Show type name of the columns of specified table.
3687

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

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

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

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

            
3698
Create values clause.
3699

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

            
3702
You can use this in insert statement.
3703

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

            
3706
=head2 C<where>
3707

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

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

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

            
3718
=head2 C<DBIX_CUSTOM_DEBUG>
3719

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

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

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

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

            
3729
If you set DBIX_CUSTOM_TAG_PARSE to 0, tag parsing is off.
3730

            
3731
=head2 C<DBIX_CUSTOM_DISABLE_MODEL_EXECUTE>
3732

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

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

            
3738
L<DBIx::Custom>
3739

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

            
3796
L<DBIx::Custom::Model>
3797

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

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

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

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

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

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

            
3848
L<DBIx::Custom::Tag>
3849

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

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

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

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

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

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

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

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

            
3873
C<< <kimoto.yuki at gmail.com> >>
3874

            
3875
L<http://github.com/yuki-kimoto/DBIx-Custom>
3876

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3877
=head1 AUTHOR
3878

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

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

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

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

            
3888
=cut