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

            
- removed argument checking ...
Yuki Kimoto authored on 2011-10-21
4
our $VERSION = '0.1733';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6

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

            
added environment variable D...
Yuki Kimoto authored on 2011-04-02
22
use constant DEBUG => $ENV{DBIX_CUSTOM_DEBUG} || 0;
improved debug message
Yuki Kimoto authored on 2011-05-23
23
use constant DEBUG_ENCODING => $ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8';
added environment variable D...
Yuki Kimoto authored on 2011-04-02
24

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

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

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

            
added helper method
yuki-kimoto authored on 2010-10-17
101
our $AUTOLOAD;
102
sub AUTOLOAD {
103
    my $self = shift;
104

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

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

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
122
sub assign_clause {
updated pod
Yuki Kimoto authored on 2011-09-02
123
    my ($self, $param, $opts) = @_;
124
    
125
    my $wrap = $opts->{wrap} || {};
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
126
    
127
    # Create set tag
128
    my @params;
129
    my $safety = $self->safety_character;
insert and update method's p...
Yuki Kimoto authored on 2011-07-29
130
    foreach my $column (sort keys %$param) {
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
131
        croak qq{"$column" is not safety column name } . _subname
132
          unless $column =~ /^[$safety\.]+$/;
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
133
        my $column_quote = $self->_q($column);
134
        $column_quote =~ s/\./$self->_q(".")/e;
updated pod
Yuki Kimoto authored on 2011-09-02
135
        my $func = $wrap->{$column} || sub { $_[0] };
136
        push @params,
137
          ref $param->{$column} eq 'SCALAR' ? "$column_quote = " . ${$param->{$column}}
138
        : "$column_quote = " . $func->(":$column");
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
139
    }
140
    my $tag = join(', ', @params);
141
    
142
    return $tag;
143
}
144

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
247
    # Delete statement
cleanup
Yuki Kimoto authored on 2011-10-21
248
    my $sql = "delete ";
cleanup
Yuki Kimoto authored on 2011-10-21
249
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-21
250
    $sql .= "from " . $self->_q($opt{table}) . " $w->{clause} ";
packaging one directory
yuki-kimoto authored on 2009-11-16
251
    
252
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
253
    return $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
254
}
255

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

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

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

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

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
297
    my $user_column_info = $self->user_column_info;
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
298
    
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
299
    if ($user_column_info) {
300
        $self->$cb($_->{table}, $_->{column}, $_->{info}) for @$user_column_info;
301
    }
302
    else {
303
    
304
        my $re = $self->exclude_table || $options{exclude_table};
305
        # Tables
306
        my %tables;
307
        $self->each_table(sub { $tables{$_[1]}++ });
added SQL Server test
Yuki Kimoto authored on 2011-08-14
308

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
309
        # Iterate all tables
310
        my @tables = sort keys %tables;
311
        for (my $i = 0; $i < @tables; $i++) {
312
            my $table = $tables[$i];
313
            
314
            # Iterate all columns
315
            my $sth_columns;
316
            eval {$sth_columns = $self->dbh->column_info(undef, undef, $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
            }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
322
        }
323
    }
324
}
325

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
348
sub execute {
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
349
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-10-20
350
    my $sql = shift;
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
351
    my $param;
352
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
353
    my %opt = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
354
    
cleanup
Yuki Kimoto authored on 2011-10-21
355
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
356
    my $p = $opt{param} || {};
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
357
    $param ||= $p;
cleanup
Yuki Kimoto authored on 2011-10-21
358
    my $tables = $opt{table} || [];
cleanup
Yuki Kimoto authored on 2011-04-02
359
    $tables = [$tables] unless ref $tables eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-10-21
360
    my $filter = $opt{filter};
cleanup
Yuki Kimoto authored on 2011-04-25
361
    $filter = _array_to_hash($filter);
cleanup
Yuki Kimoto authored on 2011-10-21
362
    my $bind_type = $opt{bind_type} || $opt{type};
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
363
    $bind_type = _array_to_hash($bind_type);
cleanup
Yuki Kimoto authored on 2011-10-21
364
    my $type_rule_off = $opt{type_rule_off};
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
365
    my $type_rule_off_parts = {
cleanup
Yuki Kimoto authored on 2011-10-21
366
        1 => $opt{type_rule1_off},
367
        2 => $opt{type_rule2_off}
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
368
    };
cleanup
Yuki Kimoto authored on 2011-10-21
369
    my $query_return = $opt{query};
370
    my $table_alias = $opt{table_alias} || {};
371
    my $after_build_sql = $opt{after_build_sql} || $opt{sqlfilter};
372
    warn "sqlfilter option is DEPRECATED" if $opt{sqlfilter};
373
    my $id = $opt{id};
374
    my $primary_key = $opt{primary_key};
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
375
    croak "execute method primary_key option " .
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
376
          "must be specified when id is specified " . _subname
377
      if defined $id && !defined $primary_key;
378
    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-10-21
379
    my $append = $opt{append};
cleanup
Yuki Kimoto authored on 2011-10-20
380
    $sql .= $append if defined $append && !ref $sql;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
381
    
cleanup
Yuki Kimoto authored on 2011-10-20
382
    my $query
383
      = ref $sql ? $sql : $self->_create_query($sql, $after_build_sql);
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
384
    
385
    # Save query
386
    $self->last_sql($query->sql);
387

            
cleanup
Yuki Kimoto authored on 2011-06-09
388
    return $query if $query_return;
micro optimization
Yuki Kimoto authored on 2011-07-30
389
    
390
    # DEPRECATED! Merge query filter
DBIx::Custom::Query filter m...
Yuki Kimoto authored on 2011-07-30
391
    $filter ||= $query->{filter} || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
392
    
cleanup
Yuki Kimoto authored on 2011-04-02
393
    # Tables
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
394
    unshift @$tables, @{$query->{tables} || []};
micro optimization
Yuki Kimoto authored on 2011-07-30
395
    my $main_table = @{$tables}[-1];
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
396

            
397
    if (defined $id) {
cleanup
Yuki Kimoto authored on 2011-10-21
398
        my $id_param = $self->_id_to_param($id, $primary_key, $main_table);
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
399
        $param = $self->merge_param($id_param, $param);
400
    }
micro optimization
Yuki Kimoto authored on 2011-07-30
401
    
micro optimization
Yuki Kimoto authored on 2011-07-30
402
    # DEPRECATED! Cleanup tables
micro optimization
Yuki Kimoto authored on 2011-07-30
403
    $tables = $self->_remove_duplicate_table($tables, $main_table)
404
      if @$tables > 1;
cleanup
Yuki Kimoto authored on 2011-04-02
405
    
added type_rule into logic
Yuki Kimoto authored on 2011-06-09
406
    # Type rule
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
407
    my $type_filters = {};
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
408
    unless ($type_rule_off) {
micro optimization
Yuki Kimoto authored on 2011-07-30
409
        foreach my $i (1, 2) {
410
            unless ($type_rule_off_parts->{$i}) {
411
                $type_filters->{$i} = {};
412
                foreach my $alias (keys %$table_alias) {
413
                    my $table = $table_alias->{$alias};
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-06-27
414
                    
micro optimization
Yuki Kimoto authored on 2011-07-30
415
                    foreach my $column (keys %{$self->{"_into$i"}{key}{$table} || {}}) {
416
                        $type_filters->{$i}->{"$alias.$column"} = $self->{"_into$i"}{key}{$table}{$column};
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
417
                    }
418
                }
micro optimization
Yuki Kimoto authored on 2011-07-30
419
                $type_filters->{$i} = {%{$type_filters->{$i}}, %{$self->{"_into$i"}{key}{$main_table} || {}}}
420
                  if $main_table;
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
421
            }
added type_rule into logic
Yuki Kimoto authored on 2011-06-09
422
        }
423
    }
cleanup
Yuki Kimoto authored on 2011-04-02
424
    
micro optimization
Yuki Kimoto authored on 2011-07-30
425
    # DEPRECATED! Applied filter
micro optimization
Yuki Kimoto authored on 2011-07-30
426
    if ($self->{filter}{on}) {
427
        my $applied_filter = {};
428
        foreach my $table (@$tables) {
429
            $applied_filter = {
430
                %$applied_filter,
431
                %{$self->{filter}{out}->{$table} || {}}
432
            }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
433
        }
micro optimization
Yuki Kimoto authored on 2011-07-30
434
        $filter = {%$applied_filter, %$filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
435
    }
436
    
cleanup
Yuki Kimoto authored on 2011-04-02
437
    # Replace filter name to code
438
    foreach my $column (keys %$filter) {
439
        my $name = $filter->{$column};
440
        if (!defined $name) {
441
            $filter->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
442
        }
cleanup
Yuki Kimoto authored on 2011-04-02
443
        elsif (ref $name ne 'CODE') {
cleanup
Yuki Kimoto authored on 2011-04-25
444
          croak qq{Filter "$name" is not registered" } . _subname
cleanup
Yuki Kimoto authored on 2011-04-02
445
            unless exists $self->filters->{$name};
446
          $filter->{$column} = $self->filters->{$name};
cleanup
Yuki Kimoto authored on 2010-12-21
447
        }
448
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
449
    
cleanup
Yuki Kimoto authored on 2011-04-02
450
    # Create bind values
451
    my $bind = $self->_create_bind_values(
452
        $param,
453
        $query->columns,
454
        $filter,
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
455
        $type_filters,
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
456
        $bind_type
cleanup
Yuki Kimoto authored on 2011-04-02
457
    );
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
458

            
cleanup
yuki-kimoto authored on 2010-10-17
459
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
460
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
461
    my $affected;
cleanup
Yuki Kimoto authored on 2011-03-21
462
    eval {
463
        for (my $i = 0; $i < @$bind; $i++) {
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
464
            my $bind_type = $bind->[$i]->{bind_type};
465
            $sth->bind_param(
466
                $i + 1,
467
                $bind->[$i]->{value},
468
                $bind_type ? $bind_type : ()
469
            );
cleanup
Yuki Kimoto authored on 2011-03-21
470
        }
471
        $affected = $sth->execute;
472
    };
improved error messages
Yuki Kimoto authored on 2011-04-18
473
    
micro optimization
Yuki Kimoto authored on 2011-07-30
474
    $self->_croak($@, qq{. Following SQL is executed.\n}
475
      . qq{$query->{sql}\n} . _subname) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
476
    
improved debug message
Yuki Kimoto authored on 2011-05-23
477
    # DEBUG message
478
    if (DEBUG) {
479
        print STDERR "SQL:\n" . $query->sql . "\n";
480
        my @output;
481
        foreach my $b (@$bind) {
482
            my $value = $b->{value};
483
            $value = 'undef' unless defined $value;
484
            $value = encode(DEBUG_ENCODING(), $value)
485
              if utf8::is_utf8($value);
486
            push @output, $value;
487
        }
488
        print STDERR "Bind values: " . join(', ', @output) . "\n\n";
489
    }
added environment variable D...
Yuki Kimoto authored on 2011-04-02
490
    
cleanup
Yuki Kimoto authored on 2011-04-02
491
    # Select statement
cleanup
yuki-kimoto authored on 2010-10-17
492
    if ($sth->{NUM_OF_FIELDS}) {
493
        
micro optimization
Yuki Kimoto authored on 2011-07-30
494
        # DEPRECATED! Filter
cleanup
Yuki Kimoto authored on 2011-04-02
495
        my $filter = {};
micro optimization
Yuki Kimoto authored on 2011-07-30
496
        if ($self->{filter}{on}) {
497
            $filter->{in}  = {};
498
            $filter->{end} = {};
499
            push @$tables, $main_table if $main_table;
500
            foreach my $table (@$tables) {
501
                foreach my $way (qw/in end/) {
502
                    $filter->{$way} = {
503
                        %{$filter->{$way}},
504
                        %{$self->{filter}{$way}{$table} || {}}
505
                    };
506
                }
cleanup
Yuki Kimoto authored on 2011-04-02
507
            }
cleanup
Yuki Kimoto authored on 2011-01-12
508
        }
509
        
510
        # Result
511
        my $result = $self->result_class->new(
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
512
            sth => $sth,
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
513
            dbi => $self,
cleanup
Yuki Kimoto authored on 2011-01-12
514
            default_filter => $self->{default_in_filter},
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
515
            filter => $filter->{in} || {},
516
            end_filter => $filter->{end} || {},
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
517
            type_rule => {
518
                from1 => $self->type_rule->{from1},
519
                from2 => $self->type_rule->{from2}
520
            },
cleanup
yuki-kimoto authored on 2010-10-17
521
        );
522

            
523
        return $result;
524
    }
cleanup
Yuki Kimoto authored on 2011-04-02
525
    
526
    # Not select statement
527
    else { return $affected }
cleanup
yuki-kimoto authored on 2010-10-17
528
}
529

            
added test
Yuki Kimoto authored on 2011-08-16
530
sub get_table_info {
cleanup
Yuki Kimoto authored on 2011-10-21
531
    my ($self, %opt) = @_;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
532
    
cleanup
Yuki Kimoto authored on 2011-10-21
533
    my $exclude = delete $opt{exclude};
534
    croak qq/"$_" is wrong option/ for keys %opt;
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
535
    
added test
Yuki Kimoto authored on 2011-08-16
536
    my $table_info = [];
537
    $self->each_table(
538
        sub { push @$table_info, {table => $_[1], info => $_[2] } },
539
        exclude => $exclude
540
    );
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
541
    
cleanup test
Yuki Kimoto authored on 2011-08-16
542
    return [sort {$a->{table} cmp $b->{table} } @$table_info];
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
543
}
544

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
545
sub get_column_info {
cleanup
Yuki Kimoto authored on 2011-10-21
546
    my ($self, %opt) = @_;
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
547
    
cleanup
Yuki Kimoto authored on 2011-10-21
548
    my $exclude_table = delete $opt{exclude_table};
549
    croak qq/"$_" is wrong option/ for keys %opt;
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
550
    
551
    my $column_info = [];
552
    $self->each_column(
553
        sub { push @$column_info, {table => $_[1], column => $_[2], info => $_[3] } },
554
        exclude_table => $exclude_table
555
    );
556
    
557
    return [
558
      sort {$a->{table} cmp $b->{table} || $a->{column} cmp $b->{column} }
cleanup
Yuki Kimoto authored on 2011-08-16
559
        @$column_info];
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
560
}
561

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
562
sub helper {
563
    my $self = shift;
564
    
565
    # Register method
566
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
567
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
568
    
569
    return $self;
570
}
571

            
cleanup
yuki-kimoto authored on 2010-10-17
572
sub insert {
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
573
    my $self = shift;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
574
    
cleanup
Yuki Kimoto authored on 2011-10-21
575
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
576
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
577
    my %opt = @_;
578
    warn "insert method param option is DEPRECATED" if $opt{param};
579
    $param ||= delete $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
580
    
581
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
582
    if ($opt{timestamp} && (my $insert_timestamp = $self->insert_timestamp)) {
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
583
        my $columns = $insert_timestamp->[0];
584
        $columns = [$columns] unless ref $columns eq 'ARRAY';
585
        my $value = $insert_timestamp->[1];
586
        $value = $value->() if ref $value eq 'CODE';
587
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
588
    }
cleanup
Yuki Kimoto authored on 2011-10-21
589
    
590
    # Merge id to parameter
591
    $param = $self->merge_param(
cleanup
Yuki Kimoto authored on 2011-10-21
592
        $self->_id_to_param($opt{id}, $opt{primary_key}), $param)
593
      if defined $opt{id};
cleanup
Yuki Kimoto authored on 2011-10-21
594
    
cleanup
Yuki Kimoto authored on 2011-04-02
595
    # Insert statement
cleanup
Yuki Kimoto authored on 2011-10-21
596
    my $sql = "insert ";
cleanup
Yuki Kimoto authored on 2011-10-21
597
    $sql .= "$opt{prefix} " if defined $opt{prefix};
598
    $sql .= "into " . $self->_q($opt{table}) . " "
599
      . $self->values_clause($param, {wrap => $opt{wrap}}) . " ";
packaging one directory
yuki-kimoto authored on 2009-11-16
600
    
601
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
602
    return $self->execute($sql, $param, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
603
}
604

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
605
sub insert_timestamp {
606
    my $self = shift;
607
    
608
    if (@_) {
609
        $self->{insert_timestamp} = [@_];
610
        
611
        return $self;
612
    }
613
    return $self->{insert_timestamp};
614
}
615

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
616
sub include_model {
617
    my ($self, $name_space, $model_infos) = @_;
618
    
cleanup
Yuki Kimoto authored on 2011-04-02
619
    # Name space
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
620
    $name_space ||= '';
cleanup
Yuki Kimoto authored on 2011-04-02
621
    
622
    # Get Model infomations
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
623
    unless ($model_infos) {
cleanup
Yuki Kimoto authored on 2011-04-02
624

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
625
        # Load name space module
cleanup
Yuki Kimoto authored on 2011-04-25
626
        croak qq{"$name_space" is invalid class name } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
627
          if $name_space =~ /[^\w:]/;
628
        eval "use $name_space";
cleanup
Yuki Kimoto authored on 2011-04-25
629
        croak qq{Name space module "$name_space.pm" is needed. $@ }
630
            . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
631
          if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
632
        
633
        # Search model modules
634
        my $path = $INC{"$name_space.pm"};
635
        $path =~ s/\.pm$//;
636
        opendir my $dh, $path
cleanup
Yuki Kimoto authored on 2011-04-25
637
          or croak qq{Can't open directory "$path": $! } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
638
        $model_infos = [];
639
        while (my $module = readdir $dh) {
640
            push @$model_infos, $module
641
              if $module =~ s/\.pm$//;
642
        }
643
        close $dh;
644
    }
645
    
cleanup
Yuki Kimoto authored on 2011-04-02
646
    # Include models
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
647
    foreach my $model_info (@$model_infos) {
648
        
cleanup
Yuki Kimoto authored on 2011-04-02
649
        # Load model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
650
        my $model_class;
651
        my $model_name;
652
        my $model_table;
653
        if (ref $model_info eq 'HASH') {
654
            $model_class = $model_info->{class};
655
            $model_name  = $model_info->{name};
656
            $model_table = $model_info->{table};
657
            
658
            $model_name  ||= $model_class;
659
            $model_table ||= $model_name;
660
        }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
661
        else { $model_class = $model_name = $model_table = $model_info }
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
662
        my $mclass = "${name_space}::$model_class";
cleanup
Yuki Kimoto authored on 2011-04-25
663
        croak qq{"$mclass" is invalid class name } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
664
          if $mclass =~ /[^\w:]/;
665
        unless ($mclass->can('isa')) {
666
            eval "use $mclass";
cleanup
Yuki Kimoto authored on 2011-04-25
667
            croak "$@ " . _subname if $@;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
668
        }
669
        
cleanup
Yuki Kimoto authored on 2011-04-02
670
        # Create model
cleanup
Yuki Kimoto authored on 2011-10-21
671
        my $opt = {};
672
        $opt->{model_class} = $mclass if $mclass;
673
        $opt->{name}        = $model_name if $model_name;
674
        $opt->{table}       = $model_table if $model_table;
675
        $self->create_model($opt);
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
676
    }
677
    
678
    return $self;
679
}
680

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

            
added DBIx::Custom::Mapper
Yuki Kimoto authored on 2011-08-26
683
sub mapper {
684
    my $self = shift;
685
    return DBIx::Custom::Mapper->new(@_);
686
}
687

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
688
sub merge_param {
689
    my ($self, @params) = @_;
690
    
cleanup
Yuki Kimoto authored on 2011-04-02
691
    # Merge parameters
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
692
    my $merge = {};
693
    foreach my $param (@params) {
694
        foreach my $column (keys %$param) {
695
            my $param_is_array = ref $param->{$column} eq 'ARRAY' ? 1 : 0;
696
            
697
            if (exists $merge->{$column}) {
698
                $merge->{$column} = [$merge->{$column}]
699
                  unless ref $merge->{$column} eq 'ARRAY';
700
                push @{$merge->{$column}},
701
                  ref $param->{$column} ? @{$param->{$column}} : $param->{$column};
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
702
            }
703
            else {
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
704
                $merge->{$column} = $param->{$column};
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
705
            }
706
        }
707
    }
708
    
fixed merge_param bug
Yuki Kimoto authored on 2011-05-23
709
    return $merge;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
710
}
711

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
712
sub model {
713
    my ($self, $name, $model) = @_;
714
    
cleanup
Yuki Kimoto authored on 2011-04-02
715
    # Set model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
716
    if ($model) {
717
        $self->models->{$name} = $model;
718
        return $self;
719
    }
720
    
721
    # Check model existance
cleanup
Yuki Kimoto authored on 2011-04-25
722
    croak qq{Model "$name" is not included } . _subname
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
723
      unless $self->models->{$name};
724
    
cleanup
Yuki Kimoto authored on 2011-04-02
725
    # Get model
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
726
    return $self->models->{$name};
727
}
728

            
cleanup
Yuki Kimoto authored on 2011-03-21
729
sub mycolumn {
730
    my ($self, $table, $columns) = @_;
731
    
cleanup
Yuki Kimoto authored on 2011-04-02
732
    # Create column clause
733
    my @column;
cleanup
Yuki Kimoto authored on 2011-03-21
734
    $columns ||= [];
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
735
    push @column, $self->_q($table) . "." . $self->_q($_) .
736
      " as " . $self->_q($_)
737
      for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
738
    
739
    return join (', ', @column);
740
}
741

            
added dbi_options attribute
kimoto authored on 2010-12-20
742
sub new {
743
    my $self = shift->SUPER::new(@_);
744
    
cleanup
Yuki Kimoto authored on 2011-04-02
745
    # Check attributes
added dbi_options attribute
kimoto authored on 2010-12-20
746
    my @attrs = keys %$self;
747
    foreach my $attr (@attrs) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
748
        croak qq{Invalid attribute: "$attr" } . _subname
added dbi_options attribute
kimoto authored on 2010-12-20
749
          unless $self->can($attr);
750
    }
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
751

            
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
752
    # DEPRECATED
cleanup
Yuki Kimoto authored on 2011-08-13
753
    $self->{_tags} = {
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
754
        '?'     => \&DBIx::Custom::Tag::placeholder,
755
        '='     => \&DBIx::Custom::Tag::equal,
756
        '<>'    => \&DBIx::Custom::Tag::not_equal,
757
        '>'     => \&DBIx::Custom::Tag::greater_than,
758
        '<'     => \&DBIx::Custom::Tag::lower_than,
759
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
760
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
761
        'like'  => \&DBIx::Custom::Tag::like,
762
        'in'    => \&DBIx::Custom::Tag::in,
763
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
764
        'update_param' => \&DBIx::Custom::Tag::update_param
cleanup
Yuki Kimoto authored on 2011-08-13
765
    };
766
    
767
    return $self;
768
}
769

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

            
772
sub order {
773
    my $self = shift;
774
    return DBIx::Custom::Order->new(dbi => $self, @_);
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
775
}
776

            
cleanup
yuki-kimoto authored on 2010-10-17
777
sub register_filter {
cleanup
Yuki Kimoto authored on 2011-04-02
778
    my $self = shift;
cleanup
yuki-kimoto authored on 2010-10-17
779
    
780
    # Register filter
781
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
cleanup
Yuki Kimoto authored on 2011-04-02
782
    $self->filters({%{$self->filters}, %$filters});
cleanup
yuki-kimoto authored on 2010-10-17
783
    
cleanup
Yuki Kimoto authored on 2011-04-02
784
    return $self;
cleanup
yuki-kimoto authored on 2010-10-17
785
}
packaging one directory
yuki-kimoto authored on 2009-11-16
786

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
790
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
791
    my $table = $opt{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
792
    my $tables = ref $table eq 'ARRAY' ? $table
793
               : defined $table ? [$table]
794
               : [];
cleanup
Yuki Kimoto authored on 2011-10-21
795
    $opt{table} = $tables;
796
    my $columns   = $opt{column};
797
    my $where     = $opt{where} || {};
798
    my $join      = $opt{join} || [];
cleanup
Yuki Kimoto authored on 2011-04-25
799
    croak qq{"join" must be array reference } . _subname
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
800
      unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-10-21
801
    my $relation = $opt{relation};
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
802
    warn "select() relation option is DEPRECATED!"
added warnings
Yuki Kimoto authored on 2011-06-07
803
      if $relation;
cleanup
Yuki Kimoto authored on 2011-10-21
804
    my $param = $opt{param} || {}; # DEPRECATED!
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
805
    warn "select() param option is DEPRECATED!"
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
806
      if keys %$param;
cleanup
Yuki Kimoto authored on 2011-10-21
807
    my $where_param = $opt{where_param} || $param || {};
808
    my $id = $opt{id};
809
    my $primary_key = $opt{primary_key};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
810
    croak "update method primary_key option " .
811
          "must be specified when id is specified " . _subname
812
      if defined $id && !defined $primary_key;
813
    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-10-21
814
    my $prefix = $opt{prefix};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
815
    
cleanup
Yuki Kimoto authored on 2011-03-09
816
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
817
    $self->_add_relation_table($tables, $relation);
packaging one directory
yuki-kimoto authored on 2009-11-16
818
    
cleanup
Yuki Kimoto authored on 2011-04-02
819
    # Select statement
micro optimization
Yuki Kimoto authored on 2011-09-30
820
    my $sql = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
821
    
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
822
    # Prefix
micro optimization
Yuki Kimoto authored on 2011-09-30
823
    $sql .= "$prefix " if defined $prefix;
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
824
    
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
825
    # Column clause
cleanup
Yuki Kimoto authored on 2011-03-30
826
    if ($columns) {
- select() column option can...
Yuki Kimoto authored on 2011-06-07
827
        $columns = [$columns] unless ref $columns eq 'ARRAY';
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
828
        foreach my $column (@$columns) {
- select() column option can...
Yuki Kimoto authored on 2011-06-08
829
            if (ref $column eq 'HASH') {
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
830
                $column = $self->column(%$column) if ref $column eq 'HASH';
- select() column option can...
Yuki Kimoto authored on 2011-06-08
831
            }
832
            elsif (ref $column eq 'ARRAY') {
- select method column optio...
Yuki Kimoto authored on 2011-07-11
833
                if (@$column == 3 && $column->[1] eq 'as') {
834
                    warn "[COLUMN, as => ALIAS] is DEPRECATED! use [COLUMN => ALIAS]";
835
                    splice @$column, 1, 1;
836
                }
837
                
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
838
                $column = join(' ', $column->[0], 'as', $self->_q($column->[1]));
- select() column option can...
Yuki Kimoto authored on 2011-06-08
839
            }
cleanup
Yuki Kimoto authored on 2011-04-02
840
            unshift @$tables, @{$self->_search_tables($column)};
micro optimization
Yuki Kimoto authored on 2011-09-30
841
            $sql .= "$column, ";
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
842
        }
micro optimization
Yuki Kimoto authored on 2011-09-30
843
        $sql =~ s/, $/ /;
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
844
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
845
    else { $sql .= '* ' }
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
846
    
847
    # Table
micro optimization
Yuki Kimoto authored on 2011-09-30
848
    $sql .= 'from ';
cleanup
Yuki Kimoto authored on 2011-03-30
849
    if ($relation) {
850
        my $found = {};
851
        foreach my $table (@$tables) {
micro optimization
Yuki Kimoto authored on 2011-09-30
852
            $sql .= $self->_q($table) . ', ' unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
853
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
854
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
855
    }
cleanup
Yuki Kimoto authored on 2011-03-30
856
    else {
857
        my $main_table = $tables->[-1] || '';
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
858
        $sql .= $self->_q($main_table) . ' ';
cleanup
Yuki Kimoto authored on 2011-03-30
859
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
860
    $sql =~ s/, $/ /;
cleanup
Yuki Kimoto authored on 2011-04-25
861
    croak "Not found table name " . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
862
      unless $tables->[-1];
cleanup
Yuki Kimoto authored on 2011-04-01
863

            
cleanup
Yuki Kimoto authored on 2011-04-02
864
    # Add tables in parameter
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
865
    unshift @$tables,
866
            @{$self->_search_tables(join(' ', keys %$where_param) || '')};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
867
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
868
    # Where
select, update, and delete w...
Yuki Kimoto authored on 2011-04-25
869
    my $where_clause = '';
cleanup
Yuki Kimoto authored on 2011-10-21
870
    $where = $self->_id_to_param($id, $primary_key, $tables->[-1])
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
871
      if defined $id;
updated pod
Yuki Kimoto authored on 2011-06-21
872
    if (ref $where eq 'ARRAY' && !ref $where->[0]) {
873
        $where_clause = "where " . $where->[0];
874
        $where_param = $where->[1];
875
    }
876
    elsif (ref $where) {
cleanup
Yuki Kimoto authored on 2011-04-25
877
        $where = $self->_where_to_obj($where);
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
878
        $where_param = keys %$where_param
879
                     ? $self->merge_param($where_param, $where->param)
880
                     : $where->param;
cleanup
Yuki Kimoto authored on 2011-04-25
881
        
882
        # String where
883
        $where_clause = $where->to_string;
884
    }
select, update, and delete w...
Yuki Kimoto authored on 2011-04-25
885
    elsif ($where) { $where_clause = "where $where" }
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
886
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
887
    # Add table names in where clause
cleanup
Yuki Kimoto authored on 2011-04-02
888
    unshift @$tables, @{$self->_search_tables($where_clause)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
889
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
890
    # Push join
micro optimization
Yuki Kimoto authored on 2011-09-30
891
    $self->_push_join(\$sql, $join, $tables);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
892
    
cleanup
Yuki Kimoto authored on 2011-03-09
893
    # Add where clause
micro optimization
Yuki Kimoto authored on 2011-09-30
894
    $sql .= "$where_clause ";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
895
    
cleanup
Yuki Kimoto authored on 2011-03-08
896
    # Relation(DEPRECATED!);
micro optimization
Yuki Kimoto authored on 2011-09-30
897
    $self->_push_relation(\$sql, $tables, $relation, $where_clause eq '' ? 1 : 0)
898
      if $relation;
cleanup
Yuki Kimoto authored on 2011-03-08
899
    
packaging one directory
yuki-kimoto authored on 2009-11-16
900
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
901
    my $result = $self->execute($sql, $where_param, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
902
    
903
    return $result;
904
}
905

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
906
sub setup_model {
907
    my $self = shift;
908
    
cleanup
Yuki Kimoto authored on 2011-04-02
909
    # Setup model
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
910
    $self->each_column(
911
        sub {
912
            my ($self, $table, $column, $column_info) = @_;
913
            if (my $model = $self->models->{$table}) {
914
                push @{$model->columns}, $column;
915
            }
916
        }
917
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
918
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
919
}
920

            
update pod
Yuki Kimoto authored on 2011-08-10
921
sub show_datatype {
922
    my ($self, $table) = @_;
923
    croak "Table name must be specified" unless defined $table;
924
    print "$table\n";
925
    
926
    my $result = $self->select(table => $table, where => "'0' <> '0'");
927
    my $sth = $result->sth;
928

            
929
    my $columns = $sth->{NAME};
930
    my $data_types = $sth->{TYPE};
931
    
932
    for (my $i = 0; $i < @$columns; $i++) {
933
        my $column = $columns->[$i];
934
        my $data_type = $data_types->[$i];
935
        print "$column: $data_type\n";
936
    }
937
}
938

            
939
sub show_typename {
940
    my ($self, $t) = @_;
941
    croak "Table name must be specified" unless defined $t;
942
    print "$t\n";
943
    
944
    $self->each_column(sub {
945
        my ($self, $table, $column, $infos) = @_;
946
        return unless $table eq $t;
947
        my $typename = $infos->{TYPE_NAME};
948
        print "$column: $typename\n";
949
    });
950
    
951
    return $self;
952
}
953

            
test cleanup
Yuki Kimoto authored on 2011-08-15
954
sub show_tables {
955
    my $self = shift;
956
    
957
    my %tables;
958
    $self->each_table(sub { $tables{$_[1]}++ });
959
    print join("\n", sort keys %tables) . "\n";
960
    return $self;
961
}
962

            
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
963
sub type_rule {
964
    my $self = shift;
965
    
966
    if (@_) {
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
967
        my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
968
        
969
        # Into
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
970
        foreach my $i (1 .. 2) {
971
            my $into = "into$i";
cleanup
Yuki Kimoto authored on 2011-08-16
972
            my $exists_into = exists $type_rule->{$into};
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
973
            $type_rule->{$into} = _array_to_hash($type_rule->{$into});
974
            $self->{type_rule} = $type_rule;
975
            $self->{"_$into"} = {};
976
            foreach my $type_name (keys %{$type_rule->{$into} || {}}) {
977
                croak qq{type name of $into section must be lower case}
978
                  if $type_name =~ /[A-Z]/;
979
            }
cleanup
Yuki Kimoto authored on 2011-08-16
980
            
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
981
            $self->each_column(sub {
982
                my ($dbi, $table, $column, $column_info) = @_;
983
                
984
                my $type_name = lc $column_info->{TYPE_NAME};
985
                if ($type_rule->{$into} &&
986
                    (my $filter = $type_rule->{$into}->{$type_name}))
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
987
                {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
988
                    return unless exists $type_rule->{$into}->{$type_name};
989
                    if  (defined $filter && ref $filter ne 'CODE') 
990
                    {
991
                        my $fname = $filter;
992
                        croak qq{Filter "$fname" is not registered" } . _subname
993
                          unless exists $self->filters->{$fname};
994
                        
995
                        $filter = $self->filters->{$fname};
996
                    }
997

            
micro optimization
Yuki Kimoto authored on 2011-07-30
998
                    $self->{"_$into"}{key}{$table}{$column} = $filter;
999
                    $self->{"_$into"}{dot}{"$table.$column"} = $filter;
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1000
                }
1001
            });
1002
        }
1003

            
1004
        # From
1005
        foreach my $i (1 .. 2) {
1006
            $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
1007
            foreach my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
1008
                croak qq{data type of from$i section must be lower case or number}
1009
                  if $data_type =~ /[A-Z]/;
1010
                my $fname = $type_rule->{"from$i"}{$data_type};
1011
                if (defined $fname && ref $fname ne 'CODE') {
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
1012
                    croak qq{Filter "$fname" is not registered" } . _subname
1013
                      unless exists $self->filters->{$fname};
1014
                    
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1015
                    $type_rule->{"from$i"}{$data_type} = $self->filters->{$fname};
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
1016
                }
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
1017
            }
1018
        }
1019
        
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
1020
        return $self;
1021
    }
1022
    
1023
    return $self->{type_rule} || {};
1024
}
1025

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1029
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1030
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
1031
    my %opt = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
1032
    warn "update param option is DEPRECATED!" if $opt{param};
cleanup
Yuki Kimoto authored on 2011-10-21
1033
    warn "update method where_param option is DEPRECATED!"
1034
      if $opt{where_param};
cleanup
Yuki Kimoto authored on 2011-10-21
1035
    $param ||= $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1036
    
cleanup
Yuki Kimoto authored on 2011-10-21
1037
    # Don't allow update all rows
1038
    croak qq{update method where option must be specified } . _subname
1039
      if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all};
1040
    
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1041
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
1042
    if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1043
        my $columns = $update_timestamp->[0];
1044
        $columns = [$columns] unless ref $columns eq 'ARRAY';
1045
        my $value = $update_timestamp->[1];
1046
        $value = $value->() if ref $value eq 'CODE';
1047
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1048
    }
1049

            
cleanup
Yuki Kimoto authored on 2011-10-21
1050
    # Assign clause
cleanup
Yuki Kimoto authored on 2011-10-21
1051
    my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
cleanup
Yuki Kimoto authored on 2011-10-21
1052
    
1053
    # Convert id to where parameter
1054
    my $where = defined $opt{id}
1055
      ? $self->_id_to_param($opt{id}, $opt{primary_key}, $opt{table})
1056
      : $opt{where};
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1057

            
1058
    # Where
cleanup
Yuki Kimoto authored on 2011-10-21
1059
    my $w = $self->_where_clause_and_param($where, $opt{where_param});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1060
    
cleanup
Yuki Kimoto authored on 2011-10-21
1061
    # Merge where parameter to parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1062
    $param = $self->merge_param($param, $w->{param}) if keys %{$w->{param}};
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
1063
    
cleanup
Yuki Kimoto authored on 2011-04-02
1064
    # Update statement
cleanup
Yuki Kimoto authored on 2011-10-21
1065
    my $sql = "update ";
1066
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-21
1067
    $sql .= $self->_q($opt{table}) . " set $assign_clause $w->{clause} ";
cleanup
Yuki Kimoto authored on 2011-01-27
1068
    
cleanup
yuki-kimoto authored on 2010-10-17
1069
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
1070
    return $self->execute($sql, $param, %opt);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1071
}
1072

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1075
sub update_or_insert {
1076
    my $self = shift;
1077

            
cleanup
Yuki Kimoto authored on 2011-10-21
1078
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1079
    my $param  = shift;
1080
    my %opt = @_;
1081
    my $id = $opt{id};
1082
    my $primary_key = $opt{primary_key};
1083
    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
1084
    croak "update_or_insert method need primary_key option " .
1085
          "when id is specified" . _subname
1086
      if defined $id && !defined $primary_key;
1087
    my $table  = $opt{table};
1088
    croak qq{"table" option must be specified } . _subname
1089
      unless defined $table;
1090
    my $select_option = $opt{select_option};
1091
    
1092
    my $rows = $self->select(table => $table, id => $id,
1093
        primary_key => $primary_key, %$select_option)->all;
1094
    
1095
    croak "selected row count must be one or zero" . _subname
1096
      if @$rows > 1;
1097
    
1098
    my $row = $rows->[0];
1099
    my @opt = (table => $table);
1100
    push @opt, id => $id, primary_key => $primary_key if defined $id;
1101
    push @opt, %opt;
1102
    
1103
    if ($row) {
1104
        return $self->update($param, @opt);
1105
    }
1106
    else {
1107
        return $self->insert($param, @opt);
1108
    }
1109
}
1110

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1111
sub update_timestamp {
1112
    my $self = shift;
1113
    
1114
    if (@_) {
1115
        $self->{update_timestamp} = [@_];
1116
        
1117
        return $self;
1118
    }
1119
    return $self->{update_timestamp};
1120
}
1121

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1122
sub values_clause {
1123
    my ($self, $param, $opts) = @_;
1124
    
1125
    my $wrap = $opts->{wrap} || {};
1126
    
1127
    # Create insert parameter tag
1128
    my $safety = $self->safety_character;
1129
    my @columns;
1130
    my @placeholders;
1131
    foreach my $column (sort keys %$param) {
1132
        croak qq{"$column" is not safety column name } . _subname
1133
          unless $column =~ /^[$safety\.]+$/;
1134
        my $column_quote = $self->_q($column);
1135
        $column_quote =~ s/\./$self->_q(".")/e;
1136
        push @columns, $column_quote;
1137
        
1138
        my $func = $wrap->{$column} || sub { $_[0] };
1139
        push @placeholders,
1140
          ref $param->{$column} eq 'SCALAR' ? ${$param->{$column}}
1141
        : $func->(":$column");
1142
    }
1143
    
1144
    return '(' . join(', ', @columns) . ') ' . 'values ' .
1145
           '(' . join(', ', @placeholders) . ')'
1146
}
1147

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

            
updated pod
Yuki Kimoto authored on 2011-06-21
1150
sub _create_query {
cleanup
Yuki Kimoto authored on 2011-06-13
1151
    
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1152
    my ($self, $source, $after_build_sql) = @_;
cleanup
Yuki Kimoto authored on 2011-06-13
1153
    
updated pod
Yuki Kimoto authored on 2011-06-21
1154
    # Cache
1155
    my $cache = $self->cache;
1156
    
1157
    # Query
1158
    my $query;
1159
    
1160
    # Get cached query
1161
    if ($cache) {
cleanup
Yuki Kimoto authored on 2011-06-13
1162
        
updated pod
Yuki Kimoto authored on 2011-06-21
1163
        # Get query
1164
        my $q = $self->cache_method->($self, $source);
cleanup
Yuki Kimoto authored on 2011-06-13
1165
        
updated pod
Yuki Kimoto authored on 2011-06-21
1166
        # Create query
1167
        if ($q) {
1168
            $query = DBIx::Custom::Query->new($q);
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1169
            $query->{filters} = $self->filters;
cleanup
Yuki Kimoto authored on 2011-06-13
1170
        }
updated pod
Yuki Kimoto authored on 2011-06-21
1171
    }
1172
    
1173
    # Create query
1174
    unless ($query) {
1175

            
1176
        # Create query
1177
        my $builder = $self->query_builder;
1178
        $query = $builder->build_query($source);
1179

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

            
1186
        # Save query to cache
1187
        $self->cache_method->(
1188
            $self, $source,
1189
            {
1190
                sql     => $query->sql, 
1191
                columns => $query->columns,
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
1192
                tables  => $query->{tables} || []
updated pod
Yuki Kimoto authored on 2011-06-21
1193
            }
1194
        ) if $cache;
cleanup
Yuki Kimoto authored on 2011-06-13
1195
    }
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1196

            
1197
    # Filter SQL
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1198
    if ($after_build_sql) {
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1199
        my $sql = $query->sql;
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1200
        $sql = $after_build_sql->($sql);
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1201
        $query->sql($sql);
1202
    }
1203
        
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
1204
    # Save sql
1205
    $self->last_sql($query->sql);
1206
    
updated pod
Yuki Kimoto authored on 2011-06-21
1207
    # Prepare statement handle
1208
    my $sth;
1209
    eval { $sth = $self->dbh->prepare($query->{sql})};
1210
    
1211
    if ($@) {
1212
        $self->_croak($@, qq{. Following SQL is executed.\n}
1213
                        . qq{$query->{sql}\n} . _subname);
1214
    }
1215
    
1216
    # Set statement handle
1217
    $query->sth($sth);
1218
    
1219
    # Set filters
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1220
    $query->{filters} = $self->filters;
updated pod
Yuki Kimoto authored on 2011-06-21
1221
    
1222
    return $query;
cleanup
Yuki Kimoto authored on 2011-06-13
1223
}
1224

            
cleanup
Yuki Kimoto authored on 2011-04-02
1225
sub _create_bind_values {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1226
    my ($self, $params, $columns, $filter, $type_filters, $bind_type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1227
    
cleanup
Yuki Kimoto authored on 2011-04-02
1228
    # Create bind values
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1229
    my $bind = [];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1230
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1231
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1232
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1233
        
1234
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1235
        my $value;
1236
        if(ref $params->{$column} eq 'ARRAY') {
1237
            my $i = $count->{$column} || 0;
1238
            $i += $not_exists->{$column} || 0;
1239
            my $found;
1240
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1241
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1242
                    $not_exists->{$column}++;
1243
                }
1244
                else  {
1245
                    $value = $params->{$column}->[$k];
1246
                    $found = 1;
1247
                    last
1248
                }
1249
            }
1250
            next unless $found;
1251
        }
1252
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1253
        
cleanup
Yuki Kimoto authored on 2011-01-12
1254
        # Filter
1255
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
1256
        $value = $f->($value) if $f;
1257
        
1258
        # Type rule
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1259
        foreach my $i (1 .. 2) {
1260
            my $type_filter = $type_filters->{$i};
micro optimization
Yuki Kimoto authored on 2011-07-30
1261
            my $tf = $self->{"_into$i"}->{dot}->{$column} || $type_filter->{$column};
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1262
            $value = $tf->($value) if $tf;
1263
        }
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1264
        
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
1265
        # Bind values
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
1266
        push @$bind, {value => $value, bind_type => $bind_type->{$column}};
removed reconnect method
yuki-kimoto authored on 2010-05-28
1267
        
1268
        # Count up 
1269
        $count->{$column}++;
1270
    }
1271
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1272
    return $bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1273
}
1274

            
cleanup
Yuki Kimoto authored on 2011-10-21
1275
sub _id_to_param {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1276
    my ($self, $id, $primary_keys, $table) = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
1277
    
1278
    # Check primary key
cleanup
Yuki Kimoto authored on 2011-10-21
1279
    croak "primary_key option " .
1280
          "must be specified with id option " . _subname
1281
      unless defined $primary_keys;
1282
    $primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY';
improved error messages
Yuki Kimoto authored on 2011-04-18
1283
    
cleanup
Yuki Kimoto authored on 2011-06-08
1284
    # Create parameter
1285
    my $param = {};
fixed small insert, update, ...
Yuki Kimoto authored on 2011-06-21
1286
    if (defined $id) {
cleanup
Yuki Kimoto authored on 2011-06-08
1287
        $id = [$id] unless ref $id;
1288
        croak qq{"id" must be constant value or array reference}
improved error messages
Yuki Kimoto authored on 2011-04-18
1289
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1290
          unless !ref $id || ref $id eq 'ARRAY';
1291
        croak qq{"id" must contain values same count as primary key}
improved error messages
Yuki Kimoto authored on 2011-04-18
1292
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1293
          unless @$primary_keys eq @$id;
improved error messages
Yuki Kimoto authored on 2011-04-18
1294
        for(my $i = 0; $i < @$primary_keys; $i ++) {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1295
           my $key = $primary_keys->[$i];
1296
           $key = "$table." . $key if $table;
1297
           $param->{$key} = $id->[$i];
improved error messages
Yuki Kimoto authored on 2011-04-18
1298
        }
1299
    }
1300
    
cleanup
Yuki Kimoto authored on 2011-06-08
1301
    return $param;
improved error messages
Yuki Kimoto authored on 2011-04-18
1302
}
1303

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1304
sub _connect {
1305
    my $self = shift;
1306
    
1307
    # Attributes
added warnings
Yuki Kimoto authored on 2011-06-07
1308
    my $dsn = $self->data_source;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1309
    warn "data_source is DEPRECATED!\n"
fixed bug that data_source D...
Yuki Kimoto authored on 2011-06-13
1310
      if $dsn;
added warnings
Yuki Kimoto authored on 2011-06-07
1311
    $dsn ||= $self->dsn;
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1312
    croak qq{"dsn" must be specified } . _subname
1313
      unless $dsn;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1314
    my $user        = $self->user;
1315
    my $password    = $self->password;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1316
    my $option = $self->_option;
1317
    $option = {%{$self->default_option}, %$option};
cleanup
Yuki Kimoto authored on 2011-08-16
1318
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1319
    # Connect
cleanup
Yuki Kimoto authored on 2011-08-16
1320
    my $dbh;
1321
    eval {
1322
        $dbh = DBI->connect(
1323
            $dsn,
1324
            $user,
1325
            $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1326
            $option
cleanup
Yuki Kimoto authored on 2011-08-16
1327
        );
1328
    };
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1329
    
1330
    # Connect error
cleanup
Yuki Kimoto authored on 2011-04-25
1331
    croak "$@ " . _subname if $@;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1332
    
1333
    return $dbh;
1334
}
1335

            
cleanup
yuki-kimoto authored on 2010-10-17
1336
sub _croak {
1337
    my ($self, $error, $append) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
1338
    
1339
    # Append
cleanup
yuki-kimoto authored on 2010-10-17
1340
    $append ||= "";
1341
    
1342
    # Verbose
1343
    if ($Carp::Verbose) { croak $error }
1344
    
1345
    # Not verbose
1346
    else {
1347
        
1348
        # Remove line and module infromation
1349
        my $at_pos = rindex($error, ' at ');
1350
        $error = substr($error, 0, $at_pos);
1351
        $error =~ s/\s+$//;
1352
        croak "$error$append";
1353
    }
1354
}
1355

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1358
sub _need_tables {
1359
    my ($self, $tree, $need_tables, $tables) = @_;
1360
    
cleanup
Yuki Kimoto authored on 2011-04-02
1361
    # Get needed tables
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1362
    foreach my $table (@$tables) {
1363
        if ($tree->{$table}) {
1364
            $need_tables->{$table} = 1;
1365
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1366
        }
1367
    }
1368
}
1369

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1370
sub _option {
1371
    my $self = shift;
1372
    my $option = {%{$self->dbi_options}, %{$self->dbi_option}, %{$self->option}};
1373
    warn "dbi_options is DEPRECATED! use option instead\n"
1374
      if keys %{$self->dbi_options};
1375
    warn "dbi_option is DEPRECATED! use option instead\n"
1376
      if keys %{$self->dbi_option};
1377
    return $option;
1378
}
1379

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1380
sub _push_join {
1381
    my ($self, $sql, $join, $join_tables) = @_;
1382
    
cleanup
Yuki Kimoto authored on 2011-04-02
1383
    # No join
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1384
    return unless @$join;
1385
    
cleanup
Yuki Kimoto authored on 2011-04-02
1386
    # Push join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1387
    my $tree = {};
1388
    for (my $i = 0; $i < @$join; $i++) {
1389
        
cleanup
Yuki Kimoto authored on 2011-07-28
1390
        # Arrange
added join new syntax
Yuki Kimoto authored on 2011-07-28
1391
        my $join_clause;;
1392
        my $option;
1393
        if (ref $join->[$i] eq 'HASH') {
1394
            $join_clause = $join->[$i]->{clause};
1395
            $option = {table => $join->[$i]->{table}};
1396
        }
1397
        else {
1398
            $join_clause = $join->[$i];
1399
            $option = {};
1400
        };
cleanup
Yuki Kimoto authored on 2011-07-28
1401

            
1402
        # Find tables in join clause
added join new syntax
Yuki Kimoto authored on 2011-07-28
1403
        my $table1;
1404
        my $table2;
1405
        if (my $table = $option->{table}) {
1406
            $table1 = $table->[0];
1407
            $table2 = $table->[1];
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1408
        }
cleanup
Yuki Kimoto authored on 2011-07-28
1409
        else {
1410
            my $q = $self->_quote;
1411
            my $j_clause = (split /\s+on\s+/, $join_clause)[-1];
1412
            $j_clause =~ s/'.+?'//g;
1413
            my $q_re = quotemeta($q);
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1414
            $j_clause =~ s/[$q_re]//g;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1415
            
1416
            my @j_clauses = reverse split /\s(and|on)\s/, $j_clause;
cleanup
Yuki Kimoto authored on 2011-07-28
1417
            my $c = $self->safety_character;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1418
            my $join_re = qr/($c+)\.$c+[^$c].*?($c+)\.$c+/sm;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1419
            for my $clause (@j_clauses) {
1420
                if ($clause =~ $join_re) {
1421
                    $table1 = $1;
1422
                    $table2 = $2;
1423
                    last;
1424
                }                
cleanup
Yuki Kimoto authored on 2011-07-28
1425
            }
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1426
        }
added join new syntax
Yuki Kimoto authored on 2011-07-28
1427
        croak qq{join clause must have two table name after "on" keyword. } .
1428
              qq{"$join_clause" is passed }  . _subname
1429
          unless defined $table1 && defined $table2;
1430
        croak qq{right side table of "$join_clause" must be unique }
1431
            . _subname
1432
          if exists $tree->{$table2};
1433
        croak qq{Same table "$table1" is specified} . _subname
1434
          if $table1 eq $table2;
1435
        $tree->{$table2}
1436
          = {position => $i, parent => $table1, join => $join_clause};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1437
    }
1438
    
cleanup
Yuki Kimoto authored on 2011-04-02
1439
    # Search need tables
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1440
    my $need_tables = {};
1441
    $self->_need_tables($tree, $need_tables, $join_tables);
1442
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1443
    
1444
    # Add join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1445
    foreach my $need_table (@need_tables) {
micro optimization
Yuki Kimoto authored on 2011-09-30
1446
        $$sql .= $tree->{$need_table}{join} . ' ';
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1447
    }
1448
}
cleanup
Yuki Kimoto authored on 2011-03-08
1449

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1450
sub _quote {
1451
    my $self = shift;
1452
    
1453
    return defined $self->reserved_word_quote ? $self->reserved_word_quote
1454
         : defined $self->quote ? $self->quote
1455
         : '';
1456
}
1457

            
cleanup
Yuki Kimoto authored on 2011-07-29
1458
sub _q {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1459
    my ($self, $value, $quotemeta) = @_;
cleanup
Yuki Kimoto authored on 2011-07-29
1460
    
1461
    my $quote = $self->_quote;
1462
    my $q = substr($quote, 0, 1) || '';
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1463
    my $p;
1464
    if (defined $quote && length $quote > 1) {
1465
        $p = substr($quote, 1, 1);
1466
    }
1467
    else { $p = $q }
cleanup
Yuki Kimoto authored on 2011-07-29
1468
    
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1469
    if ($quotemeta) {
1470
        $q = quotemeta($q);
1471
        $p = quotemeta($p);
1472
    }
1473
    
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1474
    return "$q$value$p";
cleanup
Yuki Kimoto authored on 2011-07-29
1475
}
1476

            
cleanup
Yuki Kimoto authored on 2011-04-02
1477
sub _remove_duplicate_table {
1478
    my ($self, $tables, $main_table) = @_;
1479
    
1480
    # Remove duplicate table
1481
    my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1482
    delete $tables{$main_table} if $main_table;
1483
    
micro optimization
Yuki Kimoto authored on 2011-07-30
1484
    my $new_tables = [keys %tables, $main_table ? $main_table : ()];
1485
    if (my $q = $self->_quote) {
1486
        $q = quotemeta($q);
1487
        $_ =~ s/[$q]//g for @$new_tables;
1488
    }
1489

            
1490
    return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1491
}
1492

            
cleanup
Yuki Kimoto authored on 2011-04-02
1493
sub _search_tables {
cleanup
Yuki Kimoto authored on 2011-04-02
1494
    my ($self, $source) = @_;
1495
    
cleanup
Yuki Kimoto authored on 2011-04-02
1496
    # Search tables
cleanup
Yuki Kimoto authored on 2011-04-02
1497
    my $tables = [];
1498
    my $safety_character = $self->safety_character;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1499
    my $q = $self->_quote;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1500
    my $quoted_safety_character_re = $self->_q("?([$safety_character]+)", 1);
1501
    my $table_re = $q ? qr/(?:^|[^$safety_character])${quoted_safety_character_re}?\./
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1502
                      : qr/(?:^|[^$safety_character])([$safety_character]+)\./;
cleanup
Yuki Kimoto authored on 2011-04-02
1503
    while ($source =~ /$table_re/g) {
1504
        push @$tables, $1;
1505
    }
1506
    
1507
    return $tables;
1508
}
1509

            
cleanup
Yuki Kimoto authored on 2011-04-02
1510
sub _where_to_obj {
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1511
    my ($self, $where) = @_;
1512
    
cleanup
Yuki Kimoto authored on 2011-04-02
1513
    my $obj;
1514
    
1515
    # Hash
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1516
    if (ref $where eq 'HASH') {
1517
        my $clause = ['and'];
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1518
        my $q = $self->_quote;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1519
        foreach my $column (keys %$where) {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1520
            my $table;
1521
            my $c;
1522
            if ($column =~ /(?:(.*?)\.)?(.*)/) {
1523
                $table = $1;
1524
                $c = $2;
1525
            }
1526
            
1527
            my $table_quote;
1528
            $table_quote = $self->_q($table) if defined $table;
1529
            my $column_quote = $self->_q($c);
1530
            $column_quote = $table_quote . '.' . $column_quote
1531
              if defined $table_quote;
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1532
            push @$clause, "$column_quote = :$column" for keys %$where;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1533
        }
cleanup
Yuki Kimoto authored on 2011-04-02
1534
        $obj = $self->where(clause => $clause, param => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1535
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1536
    
1537
    # DBIx::Custom::Where object
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1538
    elsif (ref $where eq 'DBIx::Custom::Where') {
cleanup
Yuki Kimoto authored on 2011-04-02
1539
        $obj = $where;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1540
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1541
    
updated pod
Yuki Kimoto authored on 2011-06-21
1542
    # Array
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1543
    elsif (ref $where eq 'ARRAY') {
cleanup
Yuki Kimoto authored on 2011-04-02
1544
        $obj = $self->where(
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1545
            clause => $where->[0],
1546
            param  => $where->[1]
1547
        );
1548
    }
1549
    
cleanup
Yuki Kimoto authored on 2011-04-02
1550
    # Check where argument
improved error messages
Yuki Kimoto authored on 2011-04-18
1551
    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
DBIx::Custom::Model type att...
Yuki Kimoto authored on 2011-06-17
1552
        . qq{or array reference, which contains where clause and parameter}
cleanup
Yuki Kimoto authored on 2011-04-25
1553
        . _subname
cleanup
Yuki Kimoto authored on 2011-04-02
1554
      unless ref $obj eq 'DBIx::Custom::Where';
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1555
    
cleanup
Yuki Kimoto authored on 2011-04-02
1556
    return $obj;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1557
}
1558

            
cleanup
Yuki Kimoto authored on 2011-10-21
1559
sub _where_clause_and_param {
1560
    my ($self, $where, $param) = @_;
1561
 
1562
    $where ||= {};
1563
    $param ||= {};
1564
    my $w = {};
1565
    my $where_clause = '';
1566
    if (ref $where eq 'ARRAY' && !ref $where->[0]) {
1567
        $w->{clause} = "where " . $where->[0];
1568
        $w->{param} = $where->[1];
1569
    }
1570
    elsif (ref $where) {
1571
        $where = $self->_where_to_obj($where);
1572
        $w->{param} = keys %$param
1573
                    ? $self->merge_param($param, $where->param)
1574
                    : $where->param;
1575
        $w->{clause} = $where->to_string;
1576
    }
1577
    elsif ($where) {
1578
        $w->{clause} = "where $where";
1579
        $w->{param} = $param;
1580
    }
1581
    
1582
    return $w;
1583
}
1584

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

            
1588
    # Initialize filters
1589
    $self->{filter} ||= {};
micro optimization
Yuki Kimoto authored on 2011-07-30
1590
    $self->{filter}{on} = 1;
updated pod
Yuki Kimoto authored on 2011-06-21
1591
    $self->{filter}{out} ||= {};
1592
    $self->{filter}{in} ||= {};
1593
    $self->{filter}{end} ||= {};
1594
    
1595
    # Usage
1596
    my $usage = "Usage: \$dbi->apply_filter(" .
1597
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
1598
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
1599
    
1600
    # Apply filter
1601
    for (my $i = 0; $i < @cinfos; $i += 2) {
1602
        
1603
        # Column
1604
        my $column = $cinfos[$i];
1605
        if (ref $column eq 'ARRAY') {
1606
            foreach my $c (@$column) {
1607
                push @cinfos, $c, $cinfos[$i + 1];
1608
            }
1609
            next;
1610
        }
1611
        
1612
        # Filter infomation
1613
        my $finfo = $cinfos[$i + 1] || {};
1614
        croak "$usage (table: $table) " . _subname
1615
          unless  ref $finfo eq 'HASH';
1616
        foreach my $ftype (keys %$finfo) {
1617
            croak "$usage (table: $table) " . _subname
1618
              unless $ftype eq 'in' || $ftype eq 'out' || $ftype eq 'end'; 
1619
        }
1620
        
1621
        # Set filters
1622
        foreach my $way (qw/in out end/) {
1623
        
1624
            # Filter
1625
            my $filter = $finfo->{$way};
1626
            
1627
            # Filter state
1628
            my $state = !exists $finfo->{$way} ? 'not_exists'
1629
                      : !defined $filter        ? 'not_defined'
1630
                      : ref $filter eq 'CODE'   ? 'code'
1631
                      : 'name';
1632
            
1633
            # Filter is not exists
1634
            next if $state eq 'not_exists';
1635
            
1636
            # Check filter name
1637
            croak qq{Filter "$filter" is not registered } . _subname
1638
              if  $state eq 'name'
1639
               && ! exists $self->filters->{$filter};
1640
            
1641
            # Set filter
1642
            my $f = $state eq 'not_defined' ? undef
1643
                  : $state eq 'code'        ? $filter
1644
                  : $self->filters->{$filter};
1645
            $self->{filter}{$way}{$table}{$column} = $f;
1646
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
1647
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
1648
            $self->{filter}{$way}{$table}{"${table}-$column"} = $f;
1649
        }
1650
    }
1651
    
1652
    return $self;
1653
}
1654

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1655
# DEPRECATED!
1656
has 'data_source';
1657
has dbi_options => sub { {} };
1658
has filter_check  => 1;
1659
has 'reserved_word_quote';
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1660
has dbi_option => sub { {} };
1661
has default_dbi_option => sub {
1662
    warn "default_dbi_option is DEPRECATED! use default_option instead";
1663
    return shift->default_option;
1664
};
1665

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1666
# DEPRECATED!
1667
sub method {
1668
    warn "method is DEPRECATED! use helper instead";
1669
    return shift->helper(@_);
1670
}
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1671

            
1672
# DEPRECATED!
1673
sub assign_param {
1674
    my $self = shift;
1675
    warn "assing_param is DEPRECATED! use assign_clause instead";
1676
    return $self->assign_clause(@_);
1677
}
1678

            
1679
# DEPRECATED
1680
sub update_param {
1681
    my ($self, $param, $opts) = @_;
1682
    
1683
    warn "update_param is DEPRECATED! use assing_clause instead.";
1684
    
1685
    # Create update parameter tag
1686
    my $tag = $self->assign_clause($param, $opts);
1687
    $tag = "set $tag" unless $opts->{no_set};
1688

            
1689
    return $tag;
1690
}
1691

            
updated pod
Yuki Kimoto authored on 2011-06-21
1692
# DEPRECATED!
1693
sub create_query {
1694
    warn "create_query is DEPRECATED! use query option of each method";
1695
    shift->_create_query(@_);
1696
}
1697

            
cleanup
Yuki Kimoto authored on 2011-06-13
1698
# DEPRECATED!
1699
sub apply_filter {
1700
    my $self = shift;
1701
    
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1702
    warn "apply_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-06-13
1703
    return $self->_apply_filter(@_);
1704
}
1705

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

            
updated pod
Yuki Kimoto authored on 2011-06-08
1710
    warn "select_at is DEPRECATED! use update and id option instead";
1711

            
cleanup
Yuki Kimoto authored on 2011-10-21
1712
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1713
    my $primary_keys = delete $opt{primary_key};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1714
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1715
    my $where = delete $opt{where};
1716
    my $param = delete $opt{param};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1717
    
1718
    # Table
1719
    croak qq{"table" option must be specified } . _subname
cleanup
Yuki Kimoto authored on 2011-10-21
1720
      unless $opt{table};
1721
    my $table = ref $opt{table} ? $opt{table}->[-1] : $opt{table};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1722
    
1723
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1724
    my $where_param = $self->_id_to_param($where, $primary_keys);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1725
    
cleanup
Yuki Kimoto authored on 2011-10-21
1726
    return $self->select(where => $where_param, %opt);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1727
}
1728

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

            
1733
    warn "delete_at is DEPRECATED! use update and id option instead";
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1734
    
cleanup
Yuki Kimoto authored on 2011-10-21
1735
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1736
    my $primary_keys = delete $opt{primary_key};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1737
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1738
    my $where = delete $opt{where};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1739
    
1740
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1741
    my $where_param = $self->_id_to_param($where, $primary_keys);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1742
    
cleanup
Yuki Kimoto authored on 2011-10-21
1743
    return $self->delete(where => $where_param, %opt);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1744
}
1745

            
cleanup
Yuki Kimoto authored on 2011-06-08
1746
# DEPRECATED!
1747
sub update_at {
1748
    my $self = shift;
1749

            
1750
    warn "update_at is DEPRECATED! use update and id option instead";
1751
    
cleanup
Yuki Kimoto authored on 2011-10-21
1752
    # Options
cleanup
Yuki Kimoto authored on 2011-06-08
1753
    my $param;
1754
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1755
    my %opt = @_;
1756
    my $primary_keys = delete $opt{primary_key};
cleanup
Yuki Kimoto authored on 2011-06-08
1757
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1758
    my $where = delete $opt{where};
1759
    my $p = delete $opt{param} || {};
cleanup
Yuki Kimoto authored on 2011-06-08
1760
    $param  ||= $p;
1761
    
1762
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1763
    my $where_param = $self->_id_to_param($where, $primary_keys);
cleanup
Yuki Kimoto authored on 2011-06-08
1764
    
cleanup
Yuki Kimoto authored on 2011-10-21
1765
    return $self->update(where => $where_param, param => $param, %opt);
cleanup
Yuki Kimoto authored on 2011-06-08
1766
}
1767

            
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1768
# DEPRECATED!
1769
sub insert_at {
1770
    my $self = shift;
1771
    
1772
    warn "insert_at is DEPRECATED! use insert and id option instead";
1773
    
cleanup
Yuki Kimoto authored on 2011-10-21
1774
    # Options
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1775
    my $param;
1776
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1777
    my %opt = @_;
1778
    my $primary_key = delete $opt{primary_key};
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1779
    $primary_key = [$primary_key] unless ref $primary_key;
cleanup
Yuki Kimoto authored on 2011-10-21
1780
    my $where = delete $opt{where};
1781
    my $p = delete $opt{param} || {};
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1782
    $param  ||= $p;
1783
    
1784
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1785
    my $where_param = $self->_id_to_param($where, $primary_key);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1786
    $param = $self->merge_param($where_param, $param);
1787
    
cleanup
Yuki Kimoto authored on 2011-10-21
1788
    return $self->insert(param => $param, %opt);
insert_at is DEPRECATED! add...
Yuki Kimoto authored on 2011-06-08
1789
}
1790

            
added warnings
Yuki Kimoto authored on 2011-06-07
1791
# DEPRECATED!
1792
sub register_tag {
test cleanup
Yuki Kimoto authored on 2011-08-10
1793
    my $self = shift;
1794
    
added warnings
Yuki Kimoto authored on 2011-06-07
1795
    warn "register_tag is DEPRECATED!";
test cleanup
Yuki Kimoto authored on 2011-08-10
1796
    
1797
    # Merge tag
1798
    my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1799
    $self->{_tags} = {%{$self->{_tags} || {}}, %$tags};
1800
    
1801
    return $self;
1802
}
1803

            
1804
# DEPRECATED!
1805
sub register_tag_processor {
1806
    my $self = shift;
1807
    warn "register_tag_processor is DEPRECATED!";
1808
    # Merge tag
1809
    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1810
    $self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}};
1811
    return $self;
added warnings
Yuki Kimoto authored on 2011-06-07
1812
}
1813

            
cleanup
Yuki Kimoto authored on 2011-01-25
1814
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1815
sub default_bind_filter {
1816
    my $self = shift;
1817
    
cleanup
Yuki Kimoto authored on 2011-06-13
1818
    warn "default_bind_filter is DEPRECATED!";
added warnings
Yuki Kimoto authored on 2011-06-07
1819
    
cleanup
Yuki Kimoto authored on 2011-01-12
1820
    if (@_) {
1821
        my $fname = $_[0];
1822
        
1823
        if (@_ && !$fname) {
1824
            $self->{default_out_filter} = undef;
1825
        }
1826
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1827
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1828
              unless exists $self->filters->{$fname};
1829
        
1830
            $self->{default_out_filter} = $self->filters->{$fname};
1831
        }
1832
        return $self;
1833
    }
1834
    
1835
    return $self->{default_out_filter};
1836
}
1837

            
cleanup
Yuki Kimoto authored on 2011-01-25
1838
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1839
sub default_fetch_filter {
1840
    my $self = shift;
added warnings
Yuki Kimoto authored on 2011-06-07
1841

            
cleanup
Yuki Kimoto authored on 2011-06-13
1842
    warn "default_fetch_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-01-12
1843
    
1844
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1845
        my $fname = $_[0];
1846

            
cleanup
Yuki Kimoto authored on 2011-01-12
1847
        if (@_ && !$fname) {
1848
            $self->{default_in_filter} = undef;
1849
        }
1850
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1851
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1852
              unless exists $self->filters->{$fname};
1853
        
1854
            $self->{default_in_filter} = $self->filters->{$fname};
1855
        }
1856
        
1857
        return $self;
1858
    }
1859
    
many changed
Yuki Kimoto authored on 2011-01-23
1860
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1861
}
1862

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1863
# DEPRECATED!
1864
sub insert_param {
1865
    my $self = shift;
1866
    warn "insert_param is DEPRECATED! use values_clause instead";
1867
    return $self->values_clause(@_);
1868
}
1869

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1870
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1871
sub insert_param_tag {
1872
    warn "insert_param_tag is DEPRECATED! " .
1873
         "use insert_param instead!";
1874
    return shift->insert_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1875
}
1876

            
1877
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1878
sub update_param_tag {
fixed DEPRECATED message bug
Yuki Kimoto authored on 2011-06-10
1879
    warn "update_param_tag is DEPRECATED! " .
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1880
         "use update_param instead";
1881
    return shift->update_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1882
}
cleanup
Yuki Kimoto authored on 2011-03-08
1883
# DEPRECATED!
1884
sub _push_relation {
1885
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1886
    
1887
    if (keys %{$relation || {}}) {
micro optimization
Yuki Kimoto authored on 2011-09-30
1888
        $$sql .= $need_where ? 'where ' : 'and ';
cleanup
Yuki Kimoto authored on 2011-03-08
1889
        foreach my $rcolumn (keys %$relation) {
1890
            my $table1 = (split (/\./, $rcolumn))[0];
1891
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1892
            push @$tables, ($table1, $table2);
micro optimization
Yuki Kimoto authored on 2011-09-30
1893
            $$sql .= "$rcolumn = " . $relation->{$rcolumn} .  'and ';
cleanup
Yuki Kimoto authored on 2011-03-08
1894
        }
1895
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
1896
    $$sql =~ s/and $/ /;
cleanup
Yuki Kimoto authored on 2011-03-08
1897
}
1898

            
1899
# DEPRECATED!
1900
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1901
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1902
    
1903
    if (keys %{$relation || {}}) {
1904
        foreach my $rcolumn (keys %$relation) {
1905
            my $table1 = (split (/\./, $rcolumn))[0];
1906
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1907
            my $table1_exists;
1908
            my $table2_exists;
1909
            foreach my $table (@$tables) {
1910
                $table1_exists = 1 if $table eq $table1;
1911
                $table2_exists = 1 if $table eq $table2;
1912
            }
1913
            unshift @$tables, $table1 unless $table1_exists;
1914
            unshift @$tables, $table2 unless $table2_exists;
1915
        }
1916
    }
1917
}
1918

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1921
=head1 NAME
1922

            
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1923
DBIx::Custom - Execute insert, update, delete, and select statement easily
removed reconnect method
yuki-kimoto authored on 2010-05-28
1924

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1927
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1928
    
1929
    # Connect
1930
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1931
        dsn => "dbi:mysql:database=dbname",
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1932
        user => 'ken',
1933
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1934
        option => {mysql_enable_utf8 => 1}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1935
    );
cleanup
yuki-kimoto authored on 2010-08-05
1936

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1937
    # Insert 
updated pod
Yuki Kimoto authored on 2011-06-21
1938
    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
removed reconnect method
yuki-kimoto authored on 2010-05-28
1939
    
1940
    # Update 
updated pod
Yuki Kimoto authored on 2011-06-21
1941
    $dbi->update({title => 'Perl', author => 'Ken'}, table  => 'book',
1942
      where  => {id => 5});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1943
    
1944
    # Delete
updated pod
Yuki Kimoto authored on 2011-06-21
1945
    $dbi->delete(table  => 'book', where => {author => 'Ken'});
cleanup
yuki-kimoto authored on 2010-08-05
1946

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1951
    # Select, more complex
1952
    my $result = $dbi->select(
1953
        table  => 'book',
1954
        column => [
cleanup
Yuki Kimoto authored on 2011-06-13
1955
            {book => [qw/title author/]},
1956
            {company => ['name']}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1957
        ],
1958
        where  => {'book.author' => 'Ken'},
1959
        join => ['left outer join company on book.company_id = company.id'],
1960
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1961
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1962
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1963
    # Fetch
1964
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1965
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1966
    }
1967
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1968
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1969
    while (my $row = $result->fetch_hash) {
1970
        
1971
    }
1972
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1973
    # Execute SQL with parameter.
1974
    $dbi->execute(
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1975
        "select id from book where author = :author and title like :title",
updated pod
Yuki Kimoto authored on 2011-06-21
1976
        {author => 'ken', title => '%Perl%'}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1977
    );
1978
    
fix heading typos
Terrence Brannon authored on 2011-08-17
1979
=head1 DESCRIPTION
removed reconnect method
yuki-kimoto authored on 2010-05-28
1980

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

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

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

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

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
1996
Named place holder support
1997

            
1998
=item *
1999

            
cleanup
Yuki Kimoto authored on 2011-07-29
2000
Model support
2001

            
2002
=item *
2003

            
2004
Connection manager support
2005

            
2006
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
2007

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

            
2012
=item *
2013

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

            
2016
=item *
2017

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

            
2020
=back
pod fix
Yuki Kimoto authored on 2011-01-21
2021

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2029
Module documentations - 
cleanup
Yuki Kimoto authored on 2011-07-29
2030
L<DBIx::Custom::Result>,
2031
L<DBIx::Custom::Query>,
2032
L<DBIx::Custom::Where>,
2033
L<DBIx::Custom::Model>,
2034
L<DBIx::Custom::Order>
updated document
yuki-kimoto authored on 2010-08-08
2035

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

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

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

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

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

            
2049
    my $connector = DBIx::Connector->new(
cleanup
Yuki Kimoto authored on 2011-08-16
2050
        "dbi:mysql:database=$database",
2051
        $user,
2052
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2053
        DBIx::Custom->new->default_option
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2054
    );
2055
    
updated pod
Yuki Kimoto authored on 2011-06-21
2056
    my $dbi = DBIx::Custom->connect(connector => $connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2057

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

            
2061
    my $dbi = DBIx::Custom->connect(
2062
      dsn => $dsn, user => $user, password => $password, connector => 1);
2063
    
2064
    my $connector = $dbi->connector; # DBIx::Connector
2065

            
2066
Note that L<DBIx::Connector> must be installed.
2067

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

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

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

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

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2083
    {
2084
        RaiseError => 1,
2085
        PrintError => 0,
2086
        AutoCommit => 1,
2087
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
2088

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

            
2091
    my $exclude_table = $dbi->exclude_table;
2092
    $dbi = $dbi->exclude_table(qr/pg_/);
2093

            
2094
Excluded table regex.
2095
C<each_column>, C<each_table>, C<type_rule>,
2096
and C<setup_model> methods ignore matching tables.
2097

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

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

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

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

            
2107
    my $last_sql = $dbi->last_sql;
2108
    $dbi = $dbi->last_sql($last_sql);
2109

            
2110
Get last successed SQL executed by C<execute> method.
2111

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

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

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

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

            
2121
    my $option = $dbi->option;
2122
    $dbi = $dbi->option($option);
2123

            
2124
L<DBI> option, used when C<connect> method is executed.
2125
Each value in option override the value of C<default_option>.
2126

            
cleanup
yuki-kimoto authored on 2010-10-17
2127
=head2 C<password>
2128

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-07-30
2149
You can set quote pair.
2150

            
2151
    $dbi->quote('[]');
2152

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

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

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

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

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2162
    my $safety_character = $dbi->safety_character;
2163
    $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2164

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

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

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

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

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

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

            
2181
    my $tag_parse = $dbi->tag_parse(0);
2182
    $dbi = $dbi->tag_parse;
2183

            
2184
Enable DEPRECATED tag parsing functionality, default to 1.
2185
If you want to disable tag parsing functionality, set to 0.
2186

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

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

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

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

            
2196
    my $user_column_info = $dbi->user_column_info;
2197
    $dbi = $dbi->user_column_info($user_column_info);
2198

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

            
2201
    [
2202
        {table => 'book', column => 'title', info => {...}},
2203
        {table => 'author', column => 'name', info => {...}}
2204
    ]
2205

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

            
2208
    my $user_column_info
2209
      = $dbi->get_column_info(exclude_table => qr/^system/);
2210
    $dbi->user_column_info($user_column_info);
2211

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

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

            
2217
    my $user_table_info = $dbi->user_table_info;
2218
    $dbi = $dbi->user_table_info($user_table_info);
2219

            
2220
You can set the following data.
2221

            
2222
    [
2223
        {table => 'book', info => {...}},
2224
        {table => 'author', info => {...}}
2225
    ]
2226

            
2227
Usually, you can set return value of C<get_table_info>.
2228

            
2229
    my $user_table_info = $dbi->get_table_info(exclude => qr/^system/);
2230
    $dbi->user_table_info($user_table_info);
2231

            
2232
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2233
to find table info.
2234

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2271
Create column clause. The follwoing column clause is created.
2272

            
2273
    book.author as "book.author",
2274
    book.title as "book.title"
2275

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

            
cleanup
Yuki Kimoto authored on 2011-06-13
2278
    # Separator is hyphen
2279
    $dbi->separator('-');
2280
    
2281
    book.author as "book-author",
2282
    book.title as "book-title"
2283
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2284
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
2285

            
update pod
Yuki Kimoto authored on 2011-03-13
2286
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2287
        dsn => "dbi:mysql:database=dbname",
update pod
Yuki Kimoto authored on 2011-03-13
2288
        user => 'ken',
2289
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2290
        option => {mysql_enable_utf8 => 1}
update pod
Yuki Kimoto authored on 2011-03-13
2291
    );
2292

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

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

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

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

            
2303
Get rows count.
2304

            
2305
Options is same as C<select> method's ones.
2306

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
2309
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2310
        table => 'book',
2311
        primary_key => 'id',
2312
        join => [
2313
            'inner join company on book.comparny_id = company.id'
2314
        ],
2315
    );
2316

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

            
2320
   $dbi->model('book')->select(...);
2321

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

            
2324
    my $dbh = $dbi->dbh;
2325

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

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

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

            
2333
Execute delete statement.
2334

            
2335
The following opitons are available.
2336

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

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

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

            
2344
=item C<id>
2345

            
2346
    id => 4
2347
    id => [4, 5]
2348

            
2349
ID corresponding to C<primary_key>.
2350
You can delete rows by C<id> and C<primary_key>.
2351

            
2352
    $dbi->delete(
fixed pod
Yuki Kimoto authored on 2011-10-20
2353
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2354
        id => [4, 5],
2355
        table => 'book',
2356
    );
2357

            
2358
The above is same as the followin one.
2359

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

            
2362
=item C<prefix>
2363

            
2364
    prefix => 'some'
2365

            
2366
prefix before table name section.
2367

            
2368
    delete some from book
2369

            
2370
=item C<table>
2371

            
2372
    table => 'book'
2373

            
2374
Table name.
2375

            
2376
=item C<where>
2377

            
2378
Same as C<select> method's C<where> option.
2379

            
2380
=back
2381

            
2382
=head2 C<delete_all>
2383

            
2384
    $dbi->delete_all(table => $table);
2385

            
2386
Execute delete statement for all rows.
2387
Options is same as C<delete>.
2388

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

            
2391
    $dbi->each_column(
2392
        sub {
2393
            my ($dbi, $table, $column, $column_info) = @_;
2394
            
2395
            my $type = $column_info->{TYPE_NAME};
2396
            
2397
            if ($type eq 'DATE') {
2398
                # ...
2399
            }
2400
        }
2401
    );
2402

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

            
2408
If C<user_column_info> is set, C<each_column> method use C<user_column_info>
2409
infromation, you can improve the performance of C<each_column> in
2410
the following way.
2411

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

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

            
2418
    $dbi->each_table(
2419
        sub {
2420
            my ($dbi, $table, $table_info) = @_;
2421
            
2422
            my $table_name = $table_info->{TABLE_NAME};
2423
        }
2424
    );
2425

            
improved pod
Yuki Kimoto authored on 2011-10-14
2426
Iterate all table informationsfrom in database.
2427
Argument is callback which is executed when one table is found.
2428
Callback receive three arguments, C<DBIx::Custom object>, C<table name>,
2429
C<table information>.
2430

            
2431
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2432
infromation, you can improve the performance of C<each_table> in
2433
the following way.
2434

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

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

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

            
2446
    my $result = $dbi->execute(
2447
      "select * from book where title = :book.title and author like :book.author",
2448
      {'book.title' => 'Perl', 'book.author' => '%Ken%'}
update pod
Yuki Kimoto authored on 2011-03-13
2449
    );
2450

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

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2457
Named placeholder such as C<:title> is replaced by placeholder C<?>.
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2458
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2459
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2460
    select * from book where title = :title and author like :author
2461
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2462
    # Replaced
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2463
    select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2464

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2468
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2469
    select * from book where :title{=} and :author{like}
2470
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2471
    # Replaced
update pod
Yuki Kimoto authored on 2011-03-13
2472
    select * from where title = ? and author like ?;
2473

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

            
2478
    select * from where title = "aa\\:bb";
2479

            
cleanup
Yuki Kimoto authored on 2011-10-20
2480
B<OPTIONS>
2481

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

            
2484
=over 4
2485

            
cleanup
Yuki Kimoto authored on 2011-10-20
2486
=item C<append>
2487

            
2488
    append => 'order by name'
2489

            
2490
Append some statement after SQL.
2491

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

            
2494
Specify database bind data type.
2495

            
2496
    bind_type => [image => DBI::SQL_BLOB]
2497
    bind_type => [[qw/image audio/] => DBI::SQL_BLOB]
2498

            
2499
This is used to bind parameter by C<bind_param> of statment handle.
2500

            
2501
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2502

            
update pod
Yuki Kimoto authored on 2011-03-13
2503
=item C<filter>
updated pod
Yuki Kimoto authored on 2011-06-09
2504
    
2505
    filter => {
2506
        title  => sub { uc $_[0] }
2507
        author => sub { uc $_[0] }
2508
    }
update pod
Yuki Kimoto authored on 2011-03-13
2509

            
updated pod
Yuki Kimoto authored on 2011-06-09
2510
    # Filter name
2511
    filter => {
2512
        title  => 'upper_case',
2513
        author => 'upper_case'
2514
    }
2515
        
2516
    # At once
2517
    filter => [
2518
        [qw/title author/]  => sub { uc $_[0] }
2519
    ]
2520

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

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

            
2528
    id => 4
2529
    id => [4, 5]
2530

            
2531
ID corresponding to C<primary_key>.
2532
You can delete rows by C<id> and C<primary_key>.
2533

            
2534
    $dbi->execute(
2535
        "select * from book where id1 = :id1 and id2 = :id2",
2536
        {},
fixed pod
Yuki Kimoto authored on 2011-10-20
2537
        primary_key => ['id1', 'id2'],
- removed placeholder count ...
Yuki Kimoto authored on 2011-08-22
2538
        id => [4, 5],
2539
    );
2540

            
2541
The above is same as the followin one.
2542

            
2543
    $dbi->execute(
2544
        "select * from book where id1 = :id1 and id2 = :id2",
2545
        {id1 => 4, id2 => 5}
2546
    );
2547

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

            
2550
    query => 1
2551

            
2552
C<execute> method return L<DBIx::Custom::Query> object, not executing SQL.
cleanup
Yuki Kimoto authored on 2011-07-30
2553
You can check SQL or get statment handle.
updated pod
Yuki Kimoto authored on 2011-06-21
2554

            
2555
    my $sql = $query->sql;
cleanup
Yuki Kimoto authored on 2011-07-30
2556
    my $sth = $query->sth;
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2557
    my $columns = $query->columns;
2558
    
2559
If you want to execute SQL fast, you can do the following way.
2560

            
2561
    my $query;
2562
    foreach my $row (@$rows) {
2563
      $query ||= $dbi->insert($row, table => 'table1', query => 1);
cleanup
Yuki Kimoto authored on 2011-10-20
2564
      $dbi->execute($query, $row);
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2565
    }
2566

            
2567
Statement handle is reused and SQL parsing is finished,
2568
so you can get more performance than normal way.
cleanup
Yuki Kimoto authored on 2011-07-30
2569

            
2570
If you want to execute SQL as possible as fast and don't need filtering.
Added execute method's query...
Yuki Kimoto authored on 2011-07-30
2571
You can do the following way.
cleanup
Yuki Kimoto authored on 2011-07-30
2572
    
2573
    my $query;
2574
    my $sth;
2575
    foreach my $row (@$rows) {
2576
      $query ||= $dbi->insert($row, table => 'book', query => 1);
2577
      $sth ||= $query->sth;
2578
      $sth->execute(map { $row->{$_} } sort keys %$row);
2579
    }
2580

            
2581
Note that $row must be simple hash reference, such as
2582
{title => 'Perl', author => 'Ken'}.
2583
and don't forget to sort $row values by $row key asc order.
updated document
Yuki Kimoto authored on 2011-06-09
2584

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2587
    primary_key => 'id'
2588
    primary_key => ['id1', 'id2']
2589

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

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2592
=item C<after_build_sql> 
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2593

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2594
You can filter sql after the sql is build.
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2595

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2596
    after_build_sql => $code_ref
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2597

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2598
The following one is one example.
2599

            
2600
    $dbi->select(
2601
        table => 'book',
2602
        column => 'distinct(name)',
2603
        after_build_sql => sub {
2604
            "select count(*) from ($_[0]) as t1"
2605
        }
2606
    );
2607

            
2608
The following SQL is executed.
2609

            
2610
    select count(*) from (select distinct(name) from book) as t1;
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
2611

            
updated pod
Yuki Kimoto authored on 2011-06-09
2612
=item C<table>
2613
    
2614
    table => 'author'
2615

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-21
2623
    # Same
2624
    $dbi->execute(
2625
      "select * from book where title = :book.title and author = :book.author",
2626
      {title => 'Perl', author => 'Ken');
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2627

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

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

            
2632
Table alias. Key is real table name, value is alias table name.
2633
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2634
on alias table name.
2635

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

            
2638
    type_rule_off => 1
2639

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

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

            
2644
    type_rule1_off => 1
2645

            
2646
Turn C<into1> type rule off.
2647

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

            
2650
    type_rule2_off => 1
2651

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

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

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

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

            
2660
get column infomation except for one which match C<exclude_table> pattern.
2661

            
2662
    [
2663
        {table => 'book', column => 'title', info => {...}},
2664
        {table => 'author', column => 'name' info => {...}}
2665
    ]
2666

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

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

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

            
2673
    [
2674
        {table => 'book', info => {...}},
2675
        {table => 'author', info => {...}}
2676
    ]
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2677

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

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

            
2682
    $dbi->helper(
2683
        update_or_insert => sub {
2684
            my $self = shift;
2685
            
2686
            # Process
2687
        },
2688
        find_or_create   => sub {
2689
            my $self = shift;
2690
            
2691
            # Process
2692
        }
2693
    );
2694

            
2695
Register helper. These helper is called directly from L<DBIx::Custom> object.
2696

            
2697
    $dbi->update_or_insert;
2698
    $dbi->find_or_create;
2699

            
cleanup
yuki-kimoto authored on 2010-10-17
2700
=head2 C<insert>
2701

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

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

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

            
2710
    {date => \"NOW()"}
2711

            
cleanup
Yuki Kimoto authored on 2011-10-20
2712
B<options>
2713

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
2721
    id => 4
2722
    id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
2723

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2727
    $dbi->insert(
updated document
Yuki Kimoto authored on 2011-06-09
2728
        {title => 'Perl', author => 'Ken'}
fixed pod
Yuki Kimoto authored on 2011-10-20
2729
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2730
        id => [4, 5],
updated document
Yuki Kimoto authored on 2011-06-09
2731
        table => 'book'
update pod
Yuki Kimoto authored on 2011-03-13
2732
    );
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2733

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

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

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

            
2743
    prefix => 'or replace'
2744

            
2745
prefix before table name section
2746

            
2747
    insert or replace into book
2748

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

            
2751
    table => 'book'
2752

            
2753
Table name.
2754

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2755
=item C<timestamp>
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
2756

            
2757
    timestamp => 1
2758

            
2759
If this value is set to 1,
2760
automatically created timestamp column is set based on
2761
C<timestamp> attribute's C<insert> value.
2762

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

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

            
2767
placeholder wrapped string.
2768

            
2769
If the following statement
2770

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

            
2774
is executed, the following SQL is executed.
2775

            
2776
    insert into book price values ( ? + 5 );
2777

            
update pod
Yuki Kimoto authored on 2011-03-13
2778
=back
2779

            
2780
=over 4
2781

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2789
    lib / MyModel.pm
2790
        / MyModel / book.pm
2791
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2792

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

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

            
2797
    package MyModel;
updated pod
Yuki Kimoto authored on 2011-06-21
2798
    use DBIx::Custom::Model -base;
update pod
Yuki Kimoto authored on 2011-03-13
2799
    
2800
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2801

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2806
    package MyModel::book;
updated pod
Yuki Kimoto authored on 2011-06-21
2807
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2808
    
2809
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2810

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2813
    package MyModel::company;
updated pod
Yuki Kimoto authored on 2011-06-21
2814
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2815
    
2816
    1;
2817
    
updated pod
Yuki Kimoto authored on 2011-06-21
2818
MyModel::book and MyModel::company is included by C<include_model>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2819

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

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

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

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2827
=head2 C<insert_timestamp>
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
2828

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2829
$dbi->insert_timestamp(
2830
  [qw/created_at updated_at/]
2831
    => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") }
2832
);
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
2833

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2834
Timestamp value when C<insert> method is executed
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
2835
with C<timestamp> option.
2836

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
2837
If C<insert_timestamp> is set and C<insert> method is executed
2838
with C<timestamp> option, column C<created_at> and C<update_at>
2839
is automatically set to the value like "2010-10-11 10:12:54".
2840

            
2841
$dbi->insert($param, table => 'book', timestamp => 1);
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2842

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

            
2845
    my $like_value = $dbi->like_value
2846

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

            
2849
    sub { "%$_[0]%" }
2850

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

            
2853
    my $mapper = $dbi->mapper(param => $param);
2854

            
2855
Create a new L<DBIx::Custom::Mapper> object.
2856

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

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

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

            
2863
    {key1 => [1, 1], key2 => 2}
2864

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

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

            
2869
    my $model = $dbi->model('book');
2870

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

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

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

            
2878
Create column clause for myself. The follwoing column clause is created.
2879

            
2880
    book.author as author,
2881
    book.title as title
2882

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

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

            
2892
Create a new L<DBIx::Custom> object.
2893

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

            
2896
    my $not_exists = $dbi->not_exists;
2897

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

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

            
2903
    my $order = $dbi->order;
2904

            
2905
Create a new L<DBIx::Custom::Order> object.
2906

            
cleanup
yuki-kimoto authored on 2010-10-17
2907
=head2 C<register_filter>
2908

            
update pod
Yuki Kimoto authored on 2011-03-13
2909
    $dbi->register_filter(
2910
        # Time::Piece object to database DATE format
2911
        tp_to_date => sub {
2912
            my $tp = shift;
2913
            return $tp->strftime('%Y-%m-%d');
2914
        },
2915
        # database DATE format to Time::Piece object
2916
        date_to_tp => sub {
2917
           my $date = shift;
2918
           return Time::Piece->strptime($date, '%Y-%m-%d');
2919
        }
2920
    );
cleanup
yuki-kimoto authored on 2010-10-17
2921
    
update pod
Yuki Kimoto authored on 2011-03-13
2922
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2923

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2926
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2927
        table  => 'book',
2928
        column => ['author', 'title'],
2929
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2930
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2931
    
updated document
Yuki Kimoto authored on 2011-06-09
2932
Execute select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2933

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2941
=item C<column>
2942
    
updated document
Yuki Kimoto authored on 2011-06-09
2943
    column => 'author'
2944
    column => ['author', 'title']
2945

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
2954
    column => [
updated pod
Yuki Kimoto authored on 2011-06-07
2955
        {book => [qw/author title/]},
2956
        {person => [qw/name age/]}
updated document
Yuki Kimoto authored on 2011-06-09
2957
    ]
updated pod
Yuki Kimoto authored on 2011-06-07
2958

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

            
2961
    book.author as "book.author",
2962
    book.title as "book.title",
2963
    person.name as "person.name",
2964
    person.age as "person.age"
2965

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

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

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

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

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

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

            
2981
=item C<id>
2982

            
2983
    id => 4
2984
    id => [4, 5]
2985

            
2986
ID corresponding to C<primary_key>.
2987
You can select rows by C<id> and C<primary_key>.
2988

            
2989
    $dbi->select(
fixed pod
Yuki Kimoto authored on 2011-10-20
2990
        primary_key => ['id1', 'id2'],
updated document
Yuki Kimoto authored on 2011-06-09
2991
        id => [4, 5],
2992
        table => 'book'
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2993
    );
2994

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

            
updated pod
Yuki Kimoto authored on 2011-04-25
2997
    $dbi->select(
updated document
Yuki Kimoto authored on 2011-06-09
2998
        where => {id1 => 4, id2 => 5},
2999
        table => 'book'
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
3000
    );
3001
    
cleanup
Yuki Kimoto authored on 2011-10-20
3002
=item C<param>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
3003

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

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

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

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

            
3016
    prefix => 'SQL_CALC_FOUND_ROWS'
3017

            
3018
Prefix of column cluase
3019

            
3020
    select SQL_CALC_FOUND_ROWS title, author from book;
3021

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

            
3024
    join => [
3025
        'left outer join company on book.company_id = company_id',
3026
        'left outer join location on company.location_id = location.id'
3027
    ]
3028
        
3029
Join clause. If column cluase or where clause contain table name like "company.name",
3030
join clausees needed when SQL is created is used automatically.
update pod
Yuki Kimoto authored on 2011-03-12
3031

            
3032
    $dbi->select(
3033
        table => 'book',
cleanup
Yuki Kimoto authored on 2011-06-13
3034
        column => ['company.location_id as location_id'],
update pod
Yuki Kimoto authored on 2011-03-12
3035
        where => {'company.name' => 'Orange'},
3036
        join => [
3037
            'left outer join company on book.company_id = company.id',
3038
            'left outer join location on company.location_id = location.id'
3039
        ]
3040
    );
3041

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

            
cleanup
Yuki Kimoto authored on 2011-06-13
3045
    select company.location_id as location_id
update pod
Yuki Kimoto authored on 2011-03-12
3046
    from book
3047
      left outer join company on book.company_id = company.id
cleanup
Yuki Kimoto authored on 2011-06-13
3048
    where company.name = ?;
update pod
Yuki Kimoto authored on 2011-03-12
3049

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3050
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
3051
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3052

            
3053
    $dbi->select(
3054
        table => 'book',
3055
        column => ['company.location_id as location_id'],
3056
        where => {'company.name' => 'Orange'},
3057
        join => [
3058
            {
3059
                clause => 'left outer join location on company.location_id = location.id',
3060
                table => ['company', 'location']
3061
            }
3062
        ]
3063
    );
3064

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3069
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3070

            
updated document
Yuki Kimoto authored on 2011-06-09
3071
=item C<where>
3072
    
3073
    # Hash refrence
3074
    where => {author => 'Ken', 'title' => 'Perl'}
3075
    
3076
    # DBIx::Custom::Where object
3077
    where => $dbi->where(
3078
        clause => ['and', 'author = :author', 'title like :title'],
3079
        param  => {author => 'Ken', title => '%Perl%'}
3080
    );
updated pod
Yuki Kimoto authored on 2011-06-21
3081
    
3082
    # Array reference 1 (array reference, hash referenc). same as above
3083
    where => [
3084
        ['and', 'author = :author', 'title like :title'],
3085
        {author => 'Ken', title => '%Perl%'}
3086
    ];    
3087
    
3088
    # Array reference 2 (String, hash reference)
3089
    where => [
3090
        'title like :title',
3091
        {title => '%Perl%'}
3092
    ]
3093
    
3094
    # String
3095
    where => 'title is null'
update pod
Yuki Kimoto authored on 2011-03-12
3096

            
cleanup
Yuki Kimoto authored on 2011-10-20
3097
Where clause. See L<DBIx::Custom::Where>.
updated document
Yuki Kimoto authored on 2011-06-09
3098
    
update pod
Yuki Kimoto authored on 2011-03-12
3099
=back
cleanup
Yuki Kimoto authored on 2011-03-08
3100

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

            
3103
    $dbi->setup_model;
3104

            
3105
Setup all model objects.
3106
C<columns> of model object is automatically set, parsing database information.
3107

            
3108
=head2 C<type_rule>
3109

            
3110
    $dbi->type_rule(
3111
        into1 => {
3112
            date => sub { ... },
3113
            datetime => sub { ... }
3114
        },
3115
        into2 => {
3116
            date => sub { ... },
3117
            datetime => sub { ... }
3118
        },
3119
        from1 => {
3120
            # DATE
3121
            9 => sub { ... },
3122
            # DATETIME or TIMESTAMP
3123
            11 => sub { ... },
3124
        }
3125
        from2 => {
3126
            # DATE
3127
            9 => sub { ... },
3128
            # DATETIME or TIMESTAMP
3129
            11 => sub { ... },
3130
        }
3131
    );
3132

            
3133
Filtering rule when data is send into and get from database.
3134
This has a little complex problem.
3135

            
3136
In C<into1> and C<into2> you can specify
3137
type name as same as type name defined
3138
by create table, such as C<DATETIME> or C<DATE>.
3139

            
3140
Note that type name and data type don't contain upper case.
3141
If these contain upper case charactor, you convert it to lower case.
3142

            
3143
C<into2> is executed after C<into1>.
3144

            
3145
Type rule of C<into1> and C<into2> is enabled on the following
3146
column name.
3147

            
3148
=over 4
3149

            
3150
=item 1. column name
3151

            
3152
    issue_date
3153
    issue_datetime
3154

            
3155
This need C<table> option in each method.
3156

            
3157
=item 2. table name and column name, separator is dot
3158

            
3159
    book.issue_date
3160
    book.issue_datetime
3161

            
3162
=back
3163

            
3164
You get all type name used in database by C<available_typename>.
3165

            
3166
    print $dbi->available_typename;
3167

            
3168
In C<from1> and C<from2> you specify data type, not type name.
3169
C<from2> is executed after C<from1>.
3170
You get all data type by C<available_datatype>.
3171

            
3172
    print $dbi->available_datatype;
3173

            
3174
You can also specify multiple types at once.
3175

            
3176
    $dbi->type_rule(
3177
        into1 => [
3178
            [qw/DATE DATETIME/] => sub { ... },
3179
        ],
3180
    );
3181

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

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

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

            
3188
If you want to set constant value to row data, use scalar reference
3189
as parameter value.
3190

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

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

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3202
    id => 4
3203
    id => [4, 5]
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3204

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

            
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
3208
    $dbi->update(
updated document
Yuki Kimoto authored on 2011-06-09
3209
        {title => 'Perl', author => 'Ken'}
fixed pod
Yuki Kimoto authored on 2011-10-20
3210
        primary_key => ['id1', 'id2'],
updated document
Yuki Kimoto authored on 2011-06-09
3211
        id => [4, 5],
3212
        table => 'book'
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
3213
    );
update pod
Yuki Kimoto authored on 2011-03-13
3214

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3217
    $dbi->update(
3218
        {title => 'Perl', author => 'Ken'}
3219
        where => {id1 => 4, id2 => 5},
3220
        table => 'book'
3221
    );
update pod
Yuki Kimoto authored on 2011-03-13
3222

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

            
3225
    prefix => 'or replace'
3226

            
3227
prefix before table name section
3228

            
3229
    update or replace book
3230

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

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

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

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
3237
=item C<timestamp>
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
3238

            
3239
    timestamp => 1
3240

            
3241
If this value is set to 1,
3242
automatically updated timestamp column is set based on
3243
C<timestamp> attribute's C<update> value.
3244

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

            
3247
Same as C<select> method's C<where> option.
3248

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

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

            
3253
placeholder wrapped string.
3254

            
3255
If the following statement
3256

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

            
3260
is executed, the following SQL is executed.
3261

            
3262
    update book set price =  ? + 5;
3263

            
updated pod
Yuki Kimoto authored on 2011-06-08
3264
=back
update pod
Yuki Kimoto authored on 2011-03-13
3265

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

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

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

            
- removed DEPRECATED status ...
Yuki Kimoto authored on 2011-10-11
3273
=head2 C<update_or_insert EXPERIMENTAL>
3274
    
3275
    # Where
3276
    $dbi->update_or_insert(
3277
        {id => 1, title => 'Perl'},
3278
        table => 'book',
3279
        where => {id => 1},
3280
        select_option => {append => 'for update'}
3281
    );
3282
    
3283
    # ID
3284
    $dbi->update_or_insert(
3285
        {title => 'Perl'},
3286
        table => 'book',
3287
        id => 1,
3288
        primary_key => 'id',
3289
        select_option => {append => 'for update'}
3290
    );
3291
    
3292
Update or insert.
3293

            
3294
In both examples, the following SQL is executed.
3295

            
3296
    # In case insert
3297
    insert into book (id, title) values (?, ?)
3298
    
3299
    # In case update
3300
    update book set (id = ?, title = ?) where book.id = ?
3301

            
3302
The following opitons are available adding to C<update> option.
3303

            
3304
=over 4
3305

            
3306
=item C<select_option>
3307

            
3308
    select_option => {append => 'for update'}
3309

            
3310
select method option,
3311
select method is used to check the row is already exists.
3312

            
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
3313
=head2 C<update_timestamp>
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
3314

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3315
    $dbi->update_timestamp(
3316
      updated_at
3317
        => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") }
cleanup
Yuki Kimoto authored on 2011-03-09
3318
    );
fix tests
Yuki Kimoto authored on 2011-01-18
3319

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3320
Timestamp value when C<update> method is executed
3321
with C<timestamp> option.
cleanup
Yuki Kimoto authored on 2011-01-12
3322

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3323
If C<insert_timestamp> is set and C<insert> method is executed
3324
with C<timestamp> option, column C<update_at>
3325
is automatically set to the value like "2010-10-11 10:12:54".
cleanup
Yuki Kimoto authored on 2011-01-12
3326

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-10-19
3327
>|perl|
3328
$dbi->update($param, table => 'book', timestamp => 1);
3329
||<
cleanup
Yuki Kimoto authored on 2011-01-12
3330

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

            
3333
    $dbi->show_datatype($table);
3334

            
3335
Show data type of the columns of specified table.
3336

            
3337
    book
3338
    title: 5
3339
    issue_date: 91
3340

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

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

            
3345
    $dbi->show_tables;
3346

            
3347
Show tables.
3348

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

            
3351
    $dbi->show_typename($table);
3352

            
3353
Show type name of the columns of specified table.
3354

            
3355
    book
3356
    title: varchar
3357
    issue_date: date
3358

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

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

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

            
3365
Create values clause.
3366

            
3367
    (title, author) values (title = :title, age = :age);
3368

            
3369
You can use this in insert statement.
3370

            
3371
    my $insert_sql = "insert into book $values_clause";
3372

            
3373
=head2 C<where>
3374

            
3375
    my $where = $dbi->where(
3376
        clause => ['and', 'title = :title', 'author = :author'],
3377
        param => {title => 'Perl', author => 'Ken'}
3378
    );
3379

            
3380
Create a new L<DBIx::Custom::Where> object.
3381

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

            
3384
=head2 C<DBIX_CUSTOM_DEBUG>
3385

            
3386
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3387
executed SQL and bind values are printed to STDERR.
3388

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

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

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

            
3395
L<DBIx::Custom>
3396

            
3397
    # Attribute methods
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
3398
    default_dbi_option # will be removed 2017/1/1
3399
    dbi_option # will be removed 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3400
    data_source # will be removed at 2017/1/1
3401
    dbi_options # will be removed at 2017/1/1
3402
    filter_check # will be removed at 2017/1/1
3403
    reserved_word_quote # will be removed at 2017/1/1
3404
    cache_method # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3405
    
3406
    # Methods
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
3407
    method # will be removed at 2017/1/1
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
3408
    assign_param # will be removed at 2017/1/1
3409
    update_param # will be removed at 2017/1/1
3410
    insert_param # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3411
    create_query # will be removed at 2017/1/1
3412
    apply_filter # will be removed at 2017/1/1
3413
    select_at # will be removed at 2017/1/1
3414
    delete_at # will be removed at 2017/1/1
3415
    update_at # will be removed at 2017/1/1
3416
    insert_at # will be removed at 2017/1/1
3417
    register_tag # will be removed at 2017/1/1
3418
    default_bind_filter # will be removed at 2017/1/1
3419
    default_fetch_filter # will be removed at 2017/1/1
3420
    insert_param_tag # will be removed at 2017/1/1
update pod
Yuki Kimoto authored on 2011-08-10
3421
    register_tag # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3422
    register_tag_processor # will be removed at 2017/1/1
3423
    update_param_tag # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3424
    
3425
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
3426
    delete method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3427
    update method where_param option # will be removed 2017/1/1
cleanup
Yuki Kimoto authored on 2011-10-21
3428
    insert method param option # will be removed at 2017/1/1
insert method's id option is...
Yuki Kimoto authored on 2011-10-10
3429
    insert method id option # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3430
    select method relation option # will be removed at 2017/1/1
3431
    select method param option # will be removed at 2017/1/1
3432
    select method column option [COLUMN, as => ALIAS] format
3433
      # will be removed at 2017/1/1
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
3434
    execute method's sqlfilter option # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3435
    
3436
    # Others
cleanup
Yuki Kimoto authored on 2011-07-28
3437
    execute("select * from {= title}"); # execute method's
3438
                                        # tag parsing functionality
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3439
                                        # will be removed at 2017/1/1
3440
    Query caching # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3441

            
3442
L<DBIx::Custom::Model>
3443

            
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3444
    # Attribute methods
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
3445
    method # will be removed at 2017/1/1
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3446
    filter # will be removed at 2017/1/1
3447
    name # will be removed at 2017/1/1
3448
    type # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3449

            
3450
L<DBIx::Custom::Query>
3451
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3452
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3453
    default_filter # will be removed at 2017/1/1
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3454
    table # will be removed at 2017/1/1
3455
    filters # will be removed at 2017/1/1
3456
    
3457
    # Methods
3458
    filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3459

            
3460
L<DBIx::Custom::QueryBuilder>
3461
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3462
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3463
    tags # will be removed at 2017/1/1
3464
    tag_processors # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3465
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3466
    # Methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3467
    register_tag # will be removed at 2017/1/1
3468
    register_tag_processor # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3469
    
3470
    # Others
3471
    build_query("select * from {= title}"); # tag parsing functionality
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3472
                                            # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3473

            
3474
L<DBIx::Custom::Result>
3475
    
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
3476
    # Attribute methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3477
    filter_check # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3478
    
3479
    # Methods
added EXPERIMENTAL each_tabl...
Yuki Kimoto authored on 2011-07-11
3480
    end_filter # will be removed at 2017/1/1
3481
    remove_end_filter # will be removed at 2017/1/1
3482
    remove_filter # will be removed at 2017/1/1
3483
    default_filter # will be removed at 2017/1/1
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3484

            
3485
L<DBIx::Custom::Tag>
3486

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

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

            
3491
If a functionality is DEPRECATED, you can know it by DEPRECATED warnings
3492
except for attribute method.
3493
You can check all DEPRECATED functionalities by document.
3494
DEPRECATED functionality is removed after five years,
3495
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
3496
I extend one year each time he tell me it.
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
3497

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

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

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

            
3504
C<< <kimoto.yuki at gmail.com> >>
3505

            
3506
L<http://github.com/yuki-kimoto/DBIx-Custom>
3507

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3508
=head1 AUTHOR
3509

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

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

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

            
3516
This program is free software; you can redistribute it and/or modify it
3517
under the same terms as Perl itself.
3518

            
3519
=cut