DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
3514 lines | 90.711kb
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

            
micro optimization
Yuki Kimoto authored on 2011-10-23
4
our $VERSION = '0.1735';
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 = '';
cleanup
Yuki Kimoto authored on 2011-10-21
74
    for my $i (-1000 .. 1000) {
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
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} || {};
micro optimization
Yuki Kimoto authored on 2011-10-23
126
    my $safety = $self->{safety_character} || $self->safety_character;
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
127
    my $qp = $self->_q('');
128
    my $q = substr($qp, 0, 1) || '';
129
    my $p = substr($qp, 1, 1) || '';
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
130
    
micro optimization
Yuki Kimoto authored on 2011-10-23
131
    # Check unsafety keys
132
    unless ((join('', keys %$param) || '') =~ /^[$safety\.]+$/) {
133
        for my $column (keys %$param) {
134
            croak qq{"$column" is not safety column name } . _subname
135
              unless $column =~ /^[$safety\.]+$/;
136
        }
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
137
    }
138
    
micro optimization
Yuki Kimoto authored on 2011-10-23
139
    # Create set tag
140
    join(
141
      ', ',
142
      map {
143
          ref $param->{$_} eq 'SCALAR' ? "$q$_$p = " . ${$param->{$_}}
144
          : $wrap->{$_} ? "$q$_$p = " . $wrap->{$_}->(":$_")
145
          : "$q$_$p = :$_";
146
      } sort keys %$param
147
    );
added EXPERIMENTAL assign_ta...
Yuki Kimoto authored on 2011-04-26
148
}
149

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

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

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

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

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

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

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

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

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

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

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

            
- added EXPERIMENTAL get_col...
Yuki Kimoto authored on 2011-08-16
314
        # Iterate all tables
315
        my @tables = sort keys %tables;
316
        for (my $i = 0; $i < @tables; $i++) {
317
            my $table = $tables[$i];
318
            
319
            # Iterate all columns
320
            my $sth_columns;
321
            eval {$sth_columns = $self->dbh->column_info(undef, undef, $table, '%')};
322
            next if $@;
323
            while (my $column_info = $sth_columns->fetchrow_hashref) {
324
                my $column = $column_info->{COLUMN_NAME};
325
                $self->$cb($table, $column, $column_info);
326
            }
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
327
        }
328
    }
329
}
330

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
353
sub execute {
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
354
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-10-20
355
    my $sql = shift;
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
356

            
357
    # Options
execute method can second ar...
Yuki Kimoto authored on 2011-06-09
358
    my $param;
359
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
360
    my %opt = @_;
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
361
    warn "sqlfilter option is DEPRECATED" if $opt{sqlfilter};
362
    $param ||= $opt{param} || {};
cleanup
Yuki Kimoto authored on 2011-10-21
363
    my $tables = $opt{table} || [];
cleanup
Yuki Kimoto authored on 2011-04-02
364
    $tables = [$tables] unless ref $tables eq 'ARRAY';
micro optimization
Yuki Kimoto authored on 2011-10-23
365
    my $filter = ref $opt{filter} eq 'ARRAY' ?
366
      _array_to_hash($opt{filter}) : $opt{filter};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
367
    
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
368
    # Append
369
    $sql .= $opt{append} if defined $opt{append} && !ref $sql;
370
    
371
    # Query
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
372
    my $query;
373
    if (ref $sql) { $query = $sql }
374
    else {
375
        $query = $opt{reuse}->{$sql} if $opt{reuse};
376
        $query = $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter})
377
          unless $query;
378
        $opt{reuse}->{$sql} = $query if $opt{reuse};
379
    }
380
        
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
381
    # Save query
micro optimization
Yuki Kimoto authored on 2011-10-23
382
    $self->{last_sql} = $query->{sql};
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
383

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

            
cleanup
yuki-kimoto authored on 2010-10-17
459
    # Execute
micro optimization
Yuki Kimoto authored on 2011-10-23
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 {
micro optimization
Yuki Kimoto authored on 2011-10-23
463
        if ($opt{bind_type} || $opt{type}) {
464
            $sth->bind_param($_ + 1, $bind->[$_],
465
                $bind_types->[$_] ? $bind_types->[$_] : ())
466
              for (0 .. @$bind - 1);
467
            $affected = $sth->execute;
468
        }
469
        else {
470
            $affected = $sth->execute(@$bind);
471
        }
cleanup
Yuki Kimoto authored on 2011-03-21
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;
cleanup
Yuki Kimoto authored on 2011-10-21
481
        for my $b (@$bind) {
improved debug message
Yuki Kimoto authored on 2011-05-23
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
    
micro optimization
Yuki Kimoto authored on 2011-10-23
491
    # Not select statement
492
    return $affected unless $sth->{NUM_OF_FIELDS};
493

            
494
    # Filter(DEPRECATED!)
495
    my $infilter = {};
496
    if ($self->{filter}{on}) {
497
        $infilter->{in}  = {};
498
        $infilter->{end} = {};
499
        push @$tables, $main_table if $main_table;
500
        for my $table (@$tables) {
501
            for my $way (qw/in end/) {
502
                $infilter->{$way} = {%{$infilter->{$way}},
503
                  %{$self->{filter}{$way}{$table} || {}}};
cleanup
Yuki Kimoto authored on 2011-04-02
504
            }
cleanup
Yuki Kimoto authored on 2011-01-12
505
        }
cleanup
yuki-kimoto authored on 2010-10-17
506
    }
micro optimization
Yuki Kimoto authored on 2011-10-23
507
    
508
    # Result
509
    my $result = $self->result_class->new(
510
        sth => $sth,
511
        dbi => $self,
512
        default_filter => $self->{default_in_filter},
513
        filter => $infilter->{in} || {},
514
        end_filter => $infilter->{end} || {},
515
        type_rule => {
516
            from1 => $self->type_rule->{from1},
517
            from2 => $self->type_rule->{from2}
518
        },
519
    );
520
    $result;
cleanup
yuki-kimoto authored on 2010-10-17
521
}
522

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

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

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
555
sub helper {
556
    my $self = shift;
557
    
558
    # Register method
559
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
560
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
561
    
562
    return $self;
563
}
564

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

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
598
sub insert_timestamp {
599
    my $self = shift;
600
    
601
    if (@_) {
602
        $self->{insert_timestamp} = [@_];
603
        
604
        return $self;
605
    }
606
    return $self->{insert_timestamp};
607
}
608

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
609
sub include_model {
610
    my ($self, $name_space, $model_infos) = @_;
611
    
cleanup
Yuki Kimoto authored on 2011-04-02
612
    # Name space
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
613
    $name_space ||= '';
cleanup
Yuki Kimoto authored on 2011-04-02
614
    
615
    # Get Model infomations
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
616
    unless ($model_infos) {
cleanup
Yuki Kimoto authored on 2011-04-02
617

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

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

            
added DBIx::Custom::Mapper
Yuki Kimoto authored on 2011-08-26
676
sub mapper {
677
    my $self = shift;
678
    return DBIx::Custom::Mapper->new(@_);
679
}
680

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

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

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

            
added dbi_options attribute
kimoto authored on 2010-12-20
735
sub new {
736
    my $self = shift->SUPER::new(@_);
737
    
cleanup
Yuki Kimoto authored on 2011-04-02
738
    # Check attributes
added dbi_options attribute
kimoto authored on 2010-12-20
739
    my @attrs = keys %$self;
cleanup
Yuki Kimoto authored on 2011-10-21
740
    for my $attr (@attrs) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-08-20
741
        croak qq{Invalid attribute: "$attr" } . _subname
added dbi_options attribute
kimoto authored on 2010-12-20
742
          unless $self->can($attr);
743
    }
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
744

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

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

            
765
sub order {
766
    my $self = shift;
767
    return DBIx::Custom::Order->new(dbi => $self, @_);
added EXPERIMENTAL system_ta...
Yuki Kimoto authored on 2011-08-10
768
}
769

            
cleanup
yuki-kimoto authored on 2010-10-17
770
sub register_filter {
cleanup
Yuki Kimoto authored on 2011-04-02
771
    my $self = shift;
cleanup
yuki-kimoto authored on 2010-10-17
772
    
773
    # Register filter
774
    my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_};
cleanup
Yuki Kimoto authored on 2011-04-02
775
    $self->filters({%{$self->filters}, %$filters});
cleanup
yuki-kimoto authored on 2010-10-17
776
    
cleanup
Yuki Kimoto authored on 2011-04-02
777
    return $self;
cleanup
yuki-kimoto authored on 2010-10-17
778
}
packaging one directory
yuki-kimoto authored on 2009-11-16
779

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
783
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
784
    my $tables = ref $opt{table} eq 'ARRAY' ? $opt{table}
785
               : defined $opt{table} ? [$opt{table}]
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
786
               : [];
cleanup
Yuki Kimoto authored on 2011-10-21
787
    $opt{table} = $tables;
cleanup
Yuki Kimoto authored on 2011-10-21
788
    my $where_param = $opt{where_param} || delete $opt{param} || {};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
789
    
cleanup
Yuki Kimoto authored on 2011-03-09
790
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-10-21
791
    if ($opt{relation}) {
792
        warn "select() relation option is DEPRECATED!";
793
        $self->_add_relation_table($tables, $opt{relation});
794
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
795
    
cleanup
Yuki Kimoto authored on 2011-04-02
796
    # Select statement
micro optimization
Yuki Kimoto authored on 2011-09-30
797
    my $sql = 'select ';
packaging one directory
yuki-kimoto authored on 2009-11-16
798
    
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
799
    # Prefix
cleanup
Yuki Kimoto authored on 2011-10-21
800
    $sql .= "$opt{prefix} " if defined $opt{prefix};
added EXPERIMENTAL select pr...
Yuki Kimoto authored on 2011-06-13
801
    
cleanup
Yuki Kimoto authored on 2011-10-21
802
    # Column
cleanup
Yuki Kimoto authored on 2011-10-21
803
    if (defined $opt{column}) {
804
        my $columns
805
          = ref $opt{column} eq 'ARRAY' ? $opt{column} : [$opt{column}];
cleanup
Yuki Kimoto authored on 2011-10-21
806
        for my $column (@$columns) {
- select() column option can...
Yuki Kimoto authored on 2011-06-08
807
            if (ref $column eq 'HASH') {
EXPERIMTANL column method th...
Yuki Kimoto authored on 2011-06-13
808
                $column = $self->column(%$column) if ref $column eq 'HASH';
- select() column option can...
Yuki Kimoto authored on 2011-06-08
809
            }
810
            elsif (ref $column eq 'ARRAY') {
- select method column optio...
Yuki Kimoto authored on 2011-07-11
811
                if (@$column == 3 && $column->[1] eq 'as') {
812
                    warn "[COLUMN, as => ALIAS] is DEPRECATED! use [COLUMN => ALIAS]";
813
                    splice @$column, 1, 1;
814
                }
815
                
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
816
                $column = join(' ', $column->[0], 'as', $self->_q($column->[1]));
- select() column option can...
Yuki Kimoto authored on 2011-06-08
817
            }
cleanup
Yuki Kimoto authored on 2011-04-02
818
            unshift @$tables, @{$self->_search_tables($column)};
micro optimization
Yuki Kimoto authored on 2011-09-30
819
            $sql .= "$column, ";
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
820
        }
micro optimization
Yuki Kimoto authored on 2011-09-30
821
        $sql =~ s/, $/ /;
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
822
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
823
    else { $sql .= '* ' }
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
824
    
825
    # Table
micro optimization
Yuki Kimoto authored on 2011-09-30
826
    $sql .= 'from ';
cleanup
Yuki Kimoto authored on 2011-10-21
827
    if ($opt{relation}) {
cleanup
Yuki Kimoto authored on 2011-03-30
828
        my $found = {};
cleanup
Yuki Kimoto authored on 2011-10-21
829
        for my $table (@$tables) {
micro optimization
Yuki Kimoto authored on 2011-09-30
830
            $sql .= $self->_q($table) . ', ' unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
831
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
832
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
833
    }
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
834
    else { $sql .= $self->_q($tables->[-1] || '') . ' ' }
micro optimization
Yuki Kimoto authored on 2011-09-30
835
    $sql =~ s/, $/ /;
cleanup
Yuki Kimoto authored on 2011-10-21
836
    croak "select method table option must be specified " . _subname
improved error messages
Yuki Kimoto authored on 2011-04-18
837
      unless $tables->[-1];
cleanup
Yuki Kimoto authored on 2011-04-01
838

            
cleanup
Yuki Kimoto authored on 2011-04-02
839
    # Add tables in parameter
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
840
    unshift @$tables,
841
            @{$self->_search_tables(join(' ', keys %$where_param) || '')};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
842
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
843
    # Where
cleanup
Yuki Kimoto authored on 2011-10-21
844
    my $where = defined $opt{id}
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
845
              ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $tables->[-1])
cleanup
Yuki Kimoto authored on 2011-10-21
846
              : $opt{where};
847
    my $w = $self->_where_clause_and_param($where, $where_param);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
848
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
849
    # Add table names in where clause
cleanup
Yuki Kimoto authored on 2011-10-21
850
    unshift @$tables, @{$self->_search_tables($w->{clause})};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
851
    
cleanup
Yuki Kimoto authored on 2011-10-21
852
    # Join statement
cleanup
Yuki Kimoto authored on 2011-10-21
853
    $self->_push_join(\$sql, $opt{join}, $tables) if defined $opt{join};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
854
    
cleanup
Yuki Kimoto authored on 2011-03-09
855
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-10-21
856
    $sql .= "$w->{clause} ";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
857
    
cleanup
Yuki Kimoto authored on 2011-03-08
858
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-10-21
859
    $self->_push_relation(\$sql, $tables, $opt{relation}, $w->{clause} eq '' ? 1 : 0)
cleanup
Yuki Kimoto authored on 2011-10-21
860
      if $opt{relation};
cleanup
Yuki Kimoto authored on 2011-03-08
861
    
packaging one directory
yuki-kimoto authored on 2009-11-16
862
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
863
    my $result = $self->execute($sql, $w->{param}, %opt);
packaging one directory
yuki-kimoto authored on 2009-11-16
864
    
865
    return $result;
866
}
867

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
868
sub setup_model {
869
    my $self = shift;
870
    
cleanup
Yuki Kimoto authored on 2011-04-02
871
    # Setup model
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
872
    $self->each_column(
873
        sub {
874
            my ($self, $table, $column, $column_info) = @_;
875
            if (my $model = $self->models->{$table}) {
876
                push @{$model->columns}, $column;
877
            }
878
        }
879
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
880
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
881
}
882

            
update pod
Yuki Kimoto authored on 2011-08-10
883
sub show_datatype {
884
    my ($self, $table) = @_;
885
    croak "Table name must be specified" unless defined $table;
886
    print "$table\n";
887
    
888
    my $result = $self->select(table => $table, where => "'0' <> '0'");
889
    my $sth = $result->sth;
890

            
891
    my $columns = $sth->{NAME};
892
    my $data_types = $sth->{TYPE};
893
    
894
    for (my $i = 0; $i < @$columns; $i++) {
895
        my $column = $columns->[$i];
896
        my $data_type = $data_types->[$i];
897
        print "$column: $data_type\n";
898
    }
899
}
900

            
901
sub show_typename {
902
    my ($self, $t) = @_;
903
    croak "Table name must be specified" unless defined $t;
904
    print "$t\n";
905
    
906
    $self->each_column(sub {
907
        my ($self, $table, $column, $infos) = @_;
908
        return unless $table eq $t;
909
        my $typename = $infos->{TYPE_NAME};
910
        print "$column: $typename\n";
911
    });
912
    
913
    return $self;
914
}
915

            
test cleanup
Yuki Kimoto authored on 2011-08-15
916
sub show_tables {
917
    my $self = shift;
918
    
919
    my %tables;
920
    $self->each_table(sub { $tables{$_[1]}++ });
921
    print join("\n", sort keys %tables) . "\n";
922
    return $self;
923
}
924

            
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
925
sub type_rule {
926
    my $self = shift;
micro optimization
Yuki Kimoto authored on 2011-10-23
927

            
928
    $self->{_type_rule_is_called} = 1;
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
929
    
930
    if (@_) {
changed type_rule arguments ...
Yuki Kimoto authored on 2011-06-12
931
        my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
932
        
933
        # Into
cleanup
Yuki Kimoto authored on 2011-10-21
934
        for my $i (1 .. 2) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
935
            my $into = "into$i";
cleanup
Yuki Kimoto authored on 2011-08-16
936
            my $exists_into = exists $type_rule->{$into};
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
937
            $type_rule->{$into} = _array_to_hash($type_rule->{$into});
938
            $self->{type_rule} = $type_rule;
939
            $self->{"_$into"} = {};
cleanup
Yuki Kimoto authored on 2011-10-21
940
            for my $type_name (keys %{$type_rule->{$into} || {}}) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
941
                croak qq{type name of $into section must be lower case}
942
                  if $type_name =~ /[A-Z]/;
943
            }
cleanup
Yuki Kimoto authored on 2011-08-16
944
            
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
945
            $self->each_column(sub {
946
                my ($dbi, $table, $column, $column_info) = @_;
947
                
948
                my $type_name = lc $column_info->{TYPE_NAME};
949
                if ($type_rule->{$into} &&
950
                    (my $filter = $type_rule->{$into}->{$type_name}))
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
951
                {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
952
                    return unless exists $type_rule->{$into}->{$type_name};
953
                    if  (defined $filter && ref $filter ne 'CODE') 
954
                    {
955
                        my $fname = $filter;
956
                        croak qq{Filter "$fname" is not registered" } . _subname
957
                          unless exists $self->filters->{$fname};
958
                        
959
                        $filter = $self->filters->{$fname};
960
                    }
961

            
micro optimization
Yuki Kimoto authored on 2011-07-30
962
                    $self->{"_$into"}{key}{$table}{$column} = $filter;
963
                    $self->{"_$into"}{dot}{"$table.$column"} = $filter;
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
964
                }
965
            });
966
        }
967

            
968
        # From
cleanup
Yuki Kimoto authored on 2011-10-21
969
        for my $i (1 .. 2) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
970
            $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
cleanup
Yuki Kimoto authored on 2011-10-21
971
            for my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
972
                croak qq{data type of from$i section must be lower case or number}
973
                  if $data_type =~ /[A-Z]/;
974
                my $fname = $type_rule->{"from$i"}{$data_type};
975
                if (defined $fname && ref $fname ne 'CODE') {
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
976
                    croak qq{Filter "$fname" is not registered" } . _subname
977
                      unless exists $self->filters->{$fname};
978
                    
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
979
                    $type_rule->{"from$i"}{$data_type} = $self->filters->{$fname};
type_rule can receive filter...
Yuki Kimoto authored on 2011-06-12
980
                }
fixed bug that type_rule fro...
Yuki Kimoto authored on 2011-06-13
981
            }
982
        }
983
        
added type_rule method and f...
Yuki Kimoto authored on 2011-06-09
984
        return $self;
985
    }
986
    
987
    return $self->{type_rule} || {};
988
}
989

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
993
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
994
    my $param = @_ % 2 ? shift : undef;
cleanup
Yuki Kimoto authored on 2011-10-21
995
    my %opt = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
996
    warn "update param option is DEPRECATED!" if $opt{param};
cleanup
Yuki Kimoto authored on 2011-10-21
997
    warn "update method where_param option is DEPRECATED!"
998
      if $opt{where_param};
cleanup
Yuki Kimoto authored on 2011-10-21
999
    $param ||= $opt{param} || {};
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1000
    
cleanup
Yuki Kimoto authored on 2011-10-21
1001
    # Don't allow update all rows
1002
    croak qq{update method where option must be specified } . _subname
1003
      if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all};
1004
    
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1005
    # Timestamp
cleanup
Yuki Kimoto authored on 2011-10-21
1006
    if ($opt{timestamp} && (my $update_timestamp = $self->update_timestamp)) {
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1007
        my $columns = $update_timestamp->[0];
1008
        $columns = [$columns] unless ref $columns eq 'ARRAY';
1009
        my $value = $update_timestamp->[1];
1010
        $value = $value->() if ref $value eq 'CODE';
1011
        $param->{$_} = $value for @$columns;
- added EXPERIMENTAL timesta...
Yuki Kimoto authored on 2011-09-02
1012
    }
1013

            
cleanup
Yuki Kimoto authored on 2011-10-21
1014
    # Assign clause
cleanup
Yuki Kimoto authored on 2011-10-21
1015
    my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
cleanup
Yuki Kimoto authored on 2011-10-21
1016
    
1017
    # Convert id to where parameter
1018
    my $where = defined $opt{id}
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
1019
      ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $opt{table})
cleanup
Yuki Kimoto authored on 2011-10-21
1020
      : $opt{where};
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
1021

            
1022
    # Where
cleanup
Yuki Kimoto authored on 2011-10-21
1023
    my $w = $self->_where_clause_and_param($where, $opt{where_param});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1024
    
cleanup
Yuki Kimoto authored on 2011-10-21
1025
    # Merge where parameter to parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1026
    $param = $self->merge_param($param, $w->{param}) if keys %{$w->{param}};
DEPRECATED select() param op...
Yuki Kimoto authored on 2011-04-25
1027
    
cleanup
Yuki Kimoto authored on 2011-04-02
1028
    # Update statement
cleanup
Yuki Kimoto authored on 2011-10-21
1029
    my $sql = "update ";
1030
    $sql .= "$opt{prefix} " if defined $opt{prefix};
cleanup
Yuki Kimoto authored on 2011-10-21
1031
    $sql .= $self->_q($opt{table}) . " set $assign_clause $w->{clause} ";
cleanup
Yuki Kimoto authored on 2011-01-27
1032
    
cleanup
yuki-kimoto authored on 2010-10-17
1033
    # Execute query
cleanup
Yuki Kimoto authored on 2011-10-21
1034
    return $self->execute($sql, $param, %opt);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1035
}
1036

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1039
sub update_or_insert {
1040
    my $self = shift;
1041

            
cleanup
Yuki Kimoto authored on 2011-10-21
1042
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1043
    my $param  = shift;
1044
    my %opt = @_;
1045
    my $id = $opt{id};
1046
    my $primary_key = $opt{primary_key};
1047
    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
1048
    croak "update_or_insert method need primary_key option " .
1049
          "when id is specified" . _subname
1050
      if defined $id && !defined $primary_key;
1051
    my $table  = $opt{table};
1052
    croak qq{"table" option must be specified } . _subname
1053
      unless defined $table;
1054
    my $select_option = $opt{select_option};
1055
    
1056
    my $rows = $self->select(table => $table, id => $id,
1057
        primary_key => $primary_key, %$select_option)->all;
1058
    
1059
    croak "selected row count must be one or zero" . _subname
1060
      if @$rows > 1;
1061
    
1062
    my $row = $rows->[0];
1063
    my @opt = (table => $table);
1064
    push @opt, id => $id, primary_key => $primary_key if defined $id;
1065
    push @opt, %opt;
1066
    
1067
    if ($row) {
1068
        return $self->update($param, @opt);
1069
    }
1070
    else {
1071
        return $self->insert($param, @opt);
1072
    }
1073
}
1074

            
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
1075
sub update_timestamp {
1076
    my $self = shift;
1077
    
1078
    if (@_) {
1079
        $self->{update_timestamp} = [@_];
1080
        
1081
        return $self;
1082
    }
1083
    return $self->{update_timestamp};
1084
}
1085

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1086
sub values_clause {
1087
    my ($self, $param, $opts) = @_;
1088
    
1089
    my $wrap = $opts->{wrap} || {};
1090
    
1091
    # Create insert parameter tag
micro optimization
Yuki Kimoto authored on 2011-10-23
1092
    my $safety = $self->{safety_character} || $self->safety_character;
improved performance
Yuki Kimoto authored on 2011-10-22
1093
    my $qp = $self->_q('');
fixed some quoted bug
Yuki Kimoto authored on 2011-10-22
1094
    my $q = substr($qp, 0, 1) || '';
1095
    my $p = substr($qp, 1, 1) || '';
improved performance
Yuki Kimoto authored on 2011-10-22
1096
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1097
    # Check unsafety keys
1098
    unless ((join('', keys %$param) || '') =~ /^[$safety\.]+$/) {
1099
        for my $column (keys %$param) {
1100
            croak qq{"$column" is not safety column name } . _subname
1101
              unless $column =~ /^[$safety\.]+$/;
1102
        }
1103
    }
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1104
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1105
    # Assign clause(performance is important)
1106
    '(' .
1107
    join(
1108
      ', ',
1109
      map { "$q$_$p" } sort keys %$param
1110
    ) .
1111
    ') values (' .
1112
    join(
1113
      ', ',
1114
      map {
1115
          ref $param->{$_} eq 'SCALAR' ? ${$param->{$_}} :
1116
          $wrap->{$_} ? $wrap->{$_}->(":$_") :
1117
          ":$_";
1118
      } sort keys %$param
1119
    ) .
1120
    ')'
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1121
}
1122

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

            
updated pod
Yuki Kimoto authored on 2011-06-21
1125
sub _create_query {
cleanup
Yuki Kimoto authored on 2011-06-13
1126
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
1127
    my ($self, $source, $after_build_sql) = @_;
cleanup
Yuki Kimoto authored on 2011-06-13
1128
    
updated pod
Yuki Kimoto authored on 2011-06-21
1129
    # Cache
1130
    my $cache = $self->cache;
1131
    
1132
    # Query
1133
    my $query;
1134
    
1135
    # Get cached query
1136
    if ($cache) {
cleanup
Yuki Kimoto authored on 2011-06-13
1137
        
updated pod
Yuki Kimoto authored on 2011-06-21
1138
        # Get query
1139
        my $q = $self->cache_method->($self, $source);
cleanup
Yuki Kimoto authored on 2011-06-13
1140
        
updated pod
Yuki Kimoto authored on 2011-06-21
1141
        # Create query
1142
        if ($q) {
1143
            $query = DBIx::Custom::Query->new($q);
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1144
            $query->{filters} = $self->filters;
cleanup
Yuki Kimoto authored on 2011-06-13
1145
        }
updated pod
Yuki Kimoto authored on 2011-06-21
1146
    }
1147
    
1148
    # Create query
1149
    unless ($query) {
1150

            
1151
        # Create query
1152
        my $builder = $self->query_builder;
1153
        $query = $builder->build_query($source);
1154

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

            
1161
        # Save query to cache
1162
        $self->cache_method->(
1163
            $self, $source,
1164
            {
1165
                sql     => $query->sql, 
1166
                columns => $query->columns,
DBIx::Custom::Query tables a...
Yuki Kimoto authored on 2011-07-30
1167
                tables  => $query->{tables} || []
updated pod
Yuki Kimoto authored on 2011-06-21
1168
            }
1169
        ) if $cache;
cleanup
Yuki Kimoto authored on 2011-06-13
1170
    }
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1171

            
1172
    # Filter SQL
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1173
    if ($after_build_sql) {
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1174
        my $sql = $query->sql;
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
1175
        $sql = $after_build_sql->($sql);
added EXPERIMENTAL execute m...
Yuki Kimoto authored on 2011-08-14
1176
        $query->sql($sql);
1177
    }
1178
        
added EXPERIMENTAL last_sql ...
Yuki Kimoto authored on 2011-07-11
1179
    # Save sql
1180
    $self->last_sql($query->sql);
1181
    
updated pod
Yuki Kimoto authored on 2011-06-21
1182
    # Prepare statement handle
1183
    my $sth;
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
1184
    eval { $sth = $self->dbh->prepare($query->{sql}) };
updated pod
Yuki Kimoto authored on 2011-06-21
1185
    
1186
    if ($@) {
1187
        $self->_croak($@, qq{. Following SQL is executed.\n}
1188
                        . qq{$query->{sql}\n} . _subname);
1189
    }
1190
    
1191
    # Set statement handle
1192
    $query->sth($sth);
1193
    
1194
    # Set filters
DBIx::Custom::Query filters ...
Yuki Kimoto authored on 2011-07-30
1195
    $query->{filters} = $self->filters;
updated pod
Yuki Kimoto authored on 2011-06-21
1196
    
1197
    return $query;
cleanup
Yuki Kimoto authored on 2011-06-13
1198
}
1199

            
cleanup
Yuki Kimoto authored on 2011-04-02
1200
sub _create_bind_values {
EXPERIMENTAL type_rule argum...
Yuki Kimoto authored on 2011-06-17
1201
    my ($self, $params, $columns, $filter, $type_filters, $bind_type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1202
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1203
    $bind_type = _array_to_hash($bind_type) if ref $bind_type eq 'ARRAY';
1204
    
cleanup
Yuki Kimoto authored on 2011-04-02
1205
    # Create bind values
micro optimization
Yuki Kimoto authored on 2011-10-23
1206
    my @bind;
1207
    my @types;
1208
    my %count;
1209
    my %not_exists;
cleanup
Yuki Kimoto authored on 2011-10-21
1210
    for my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1211
        
micro optimization
Yuki Kimoto authored on 2011-10-23
1212
        # Bind value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1213
        if(ref $params->{$column} eq 'ARRAY') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1214
            my $i = $count{$column} || 0;
1215
            $i += $not_exists{$column} || 0;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1216
            my $found;
1217
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1218
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1219
                    $not_exists{$column}++;
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1220
                }
1221
                else  {
micro optimization
Yuki Kimoto authored on 2011-10-23
1222
                    push @bind, $params->{$column}->[$k];
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1223
                    $found = 1;
1224
                    last
1225
                }
1226
            }
1227
            next unless $found;
1228
        }
micro optimization
Yuki Kimoto authored on 2011-10-23
1229
        else { push @bind, $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1230
        
cleanup
Yuki Kimoto authored on 2011-01-12
1231
        # Filter
micro optimization
Yuki Kimoto authored on 2011-10-23
1232
        if (my $f = $filter->{$column} || $self->{default_out_filter} || '') {
micro optimization
Yuki Kimoto authored on 2011-10-23
1233
            $bind[-1] = $f->($bind[-1]);
micro optimization
Yuki Kimoto authored on 2011-10-23
1234
        }
separate DBIx::Custom type_r...
Yuki Kimoto authored on 2011-06-15
1235
        
1236
        # Type rule
micro optimization
Yuki Kimoto authored on 2011-10-23
1237
        if ($self->{_type_rule_is_called}) {
1238
            my $tf1 = $self->{"_into1"}->{dot}->{$column}
1239
              || $type_filters->{1}->{$column};
micro optimization
Yuki Kimoto authored on 2011-10-23
1240
            $bind[-1] = $tf1->($bind[-1]) if $tf1;
micro optimization
Yuki Kimoto authored on 2011-10-23
1241
            my $tf2 = $self->{"_into2"}->{dot}->{$column}
1242
              || $type_filters->{2}->{$column};
micro optimization
Yuki Kimoto authored on 2011-10-23
1243
            $bind[-1] = $tf2->($bind[-1]) if $tf2;
micro optimization
Yuki Kimoto authored on 2011-10-23
1244
        }
micro optimization
Yuki Kimoto authored on 2011-10-22
1245
       
micro optimization
Yuki Kimoto authored on 2011-10-23
1246
        # Bind types
micro optimization
Yuki Kimoto authored on 2011-10-23
1247
        push @types, $bind_type->{$column};
removed reconnect method
yuki-kimoto authored on 2010-05-28
1248
        
1249
        # Count up 
micro optimization
Yuki Kimoto authored on 2011-10-23
1250
        $count{$column}++;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1251
    }
1252
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1253
    return (\@bind, \@types);
removed reconnect method
yuki-kimoto authored on 2010-05-28
1254
}
1255

            
cleanup
Yuki Kimoto authored on 2011-10-21
1256
sub _id_to_param {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1257
    my ($self, $id, $primary_keys, $table) = @_;
cleanup
Yuki Kimoto authored on 2011-10-21
1258
    
1259
    # Check primary key
cleanup
Yuki Kimoto authored on 2011-10-21
1260
    croak "primary_key option " .
1261
          "must be specified with id option " . _subname
1262
      unless defined $primary_keys;
1263
    $primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY';
improved error messages
Yuki Kimoto authored on 2011-04-18
1264
    
cleanup
Yuki Kimoto authored on 2011-06-08
1265
    # Create parameter
1266
    my $param = {};
fixed small insert, update, ...
Yuki Kimoto authored on 2011-06-21
1267
    if (defined $id) {
cleanup
Yuki Kimoto authored on 2011-06-08
1268
        $id = [$id] unless ref $id;
1269
        croak qq{"id" must be constant value or array reference}
improved error messages
Yuki Kimoto authored on 2011-04-18
1270
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1271
          unless !ref $id || ref $id eq 'ARRAY';
1272
        croak qq{"id" must contain values same count as primary key}
improved error messages
Yuki Kimoto authored on 2011-04-18
1273
            . " (" . (caller 1)[3] . ")"
cleanup
Yuki Kimoto authored on 2011-06-08
1274
          unless @$primary_keys eq @$id;
improved error messages
Yuki Kimoto authored on 2011-04-18
1275
        for(my $i = 0; $i < @$primary_keys; $i ++) {
fixed id option bug when col...
Yuki Kimoto authored on 2011-10-10
1276
           my $key = $primary_keys->[$i];
1277
           $key = "$table." . $key if $table;
1278
           $param->{$key} = $id->[$i];
improved error messages
Yuki Kimoto authored on 2011-04-18
1279
        }
1280
    }
1281
    
cleanup
Yuki Kimoto authored on 2011-06-08
1282
    return $param;
improved error messages
Yuki Kimoto authored on 2011-04-18
1283
}
1284

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1285
sub _connect {
1286
    my $self = shift;
1287
    
1288
    # Attributes
added warnings
Yuki Kimoto authored on 2011-06-07
1289
    my $dsn = $self->data_source;
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1290
    warn "data_source is DEPRECATED!\n"
fixed bug that data_source D...
Yuki Kimoto authored on 2011-06-13
1291
      if $dsn;
added warnings
Yuki Kimoto authored on 2011-06-07
1292
    $dsn ||= $self->dsn;
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1293
    croak qq{"dsn" must be specified } . _subname
1294
      unless $dsn;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1295
    my $user        = $self->user;
1296
    my $password    = $self->password;
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1297
    my $option = $self->_option;
1298
    $option = {%{$self->default_option}, %$option};
cleanup
Yuki Kimoto authored on 2011-08-16
1299
    
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1300
    # Connect
cleanup
Yuki Kimoto authored on 2011-08-16
1301
    my $dbh;
1302
    eval {
1303
        $dbh = DBI->connect(
1304
            $dsn,
1305
            $user,
1306
            $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1307
            $option
cleanup
Yuki Kimoto authored on 2011-08-16
1308
        );
1309
    };
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1310
    
1311
    # Connect error
cleanup
Yuki Kimoto authored on 2011-04-25
1312
    croak "$@ " . _subname if $@;
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1313
    
1314
    return $dbh;
1315
}
1316

            
cleanup
yuki-kimoto authored on 2010-10-17
1317
sub _croak {
1318
    my ($self, $error, $append) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
1319
    
1320
    # Append
cleanup
yuki-kimoto authored on 2010-10-17
1321
    $append ||= "";
1322
    
1323
    # Verbose
1324
    if ($Carp::Verbose) { croak $error }
1325
    
1326
    # Not verbose
1327
    else {
1328
        
1329
        # Remove line and module infromation
1330
        my $at_pos = rindex($error, ' at ');
1331
        $error = substr($error, 0, $at_pos);
1332
        $error =~ s/\s+$//;
1333
        croak "$error$append";
1334
    }
1335
}
1336

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1339
sub _need_tables {
1340
    my ($self, $tree, $need_tables, $tables) = @_;
1341
    
cleanup
Yuki Kimoto authored on 2011-04-02
1342
    # Get needed tables
cleanup
Yuki Kimoto authored on 2011-10-21
1343
    for my $table (@$tables) {
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1344
        if ($tree->{$table}) {
1345
            $need_tables->{$table} = 1;
1346
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1347
        }
1348
    }
1349
}
1350

            
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1351
sub _option {
1352
    my $self = shift;
1353
    my $option = {%{$self->dbi_options}, %{$self->dbi_option}, %{$self->option}};
1354
    warn "dbi_options is DEPRECATED! use option instead\n"
1355
      if keys %{$self->dbi_options};
1356
    warn "dbi_option is DEPRECATED! use option instead\n"
1357
      if keys %{$self->dbi_option};
1358
    return $option;
1359
}
1360

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1361
sub _push_join {
1362
    my ($self, $sql, $join, $join_tables) = @_;
1363
    
cleanup
Yuki Kimoto authored on 2011-10-21
1364
    $join = [$join] unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-10-21
1365
    
cleanup
Yuki Kimoto authored on 2011-04-02
1366
    # No join
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1367
    return unless @$join;
1368
    
cleanup
Yuki Kimoto authored on 2011-04-02
1369
    # Push join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1370
    my $tree = {};
1371
    for (my $i = 0; $i < @$join; $i++) {
1372
        
cleanup
Yuki Kimoto authored on 2011-07-28
1373
        # Arrange
added join new syntax
Yuki Kimoto authored on 2011-07-28
1374
        my $join_clause;;
1375
        my $option;
1376
        if (ref $join->[$i] eq 'HASH') {
1377
            $join_clause = $join->[$i]->{clause};
1378
            $option = {table => $join->[$i]->{table}};
1379
        }
1380
        else {
1381
            $join_clause = $join->[$i];
1382
            $option = {};
1383
        };
cleanup
Yuki Kimoto authored on 2011-07-28
1384

            
1385
        # Find tables in join clause
added join new syntax
Yuki Kimoto authored on 2011-07-28
1386
        my $table1;
1387
        my $table2;
1388
        if (my $table = $option->{table}) {
1389
            $table1 = $table->[0];
1390
            $table2 = $table->[1];
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1391
        }
cleanup
Yuki Kimoto authored on 2011-07-28
1392
        else {
1393
            my $q = $self->_quote;
1394
            my $j_clause = (split /\s+on\s+/, $join_clause)[-1];
1395
            $j_clause =~ s/'.+?'//g;
1396
            my $q_re = quotemeta($q);
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1397
            $j_clause =~ s/[$q_re]//g;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1398
            
1399
            my @j_clauses = reverse split /\s(and|on)\s/, $j_clause;
cleanup
Yuki Kimoto authored on 2011-07-28
1400
            my $c = $self->safety_character;
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1401
            my $join_re = qr/($c+)\.$c+[^$c].*?($c+)\.$c+/sm;
improved join clause parsing
Yuki Kimoto authored on 2011-09-27
1402
            for my $clause (@j_clauses) {
1403
                if ($clause =~ $join_re) {
1404
                    $table1 = $1;
1405
                    $table2 = $2;
1406
                    last;
1407
                }                
cleanup
Yuki Kimoto authored on 2011-07-28
1408
            }
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1409
        }
added join new syntax
Yuki Kimoto authored on 2011-07-28
1410
        croak qq{join clause must have two table name after "on" keyword. } .
1411
              qq{"$join_clause" is passed }  . _subname
1412
          unless defined $table1 && defined $table2;
1413
        croak qq{right side table of "$join_clause" must be unique }
1414
            . _subname
1415
          if exists $tree->{$table2};
1416
        croak qq{Same table "$table1" is specified} . _subname
1417
          if $table1 eq $table2;
1418
        $tree->{$table2}
1419
          = {position => $i, parent => $table1, join => $join_clause};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1420
    }
1421
    
cleanup
Yuki Kimoto authored on 2011-04-02
1422
    # Search need tables
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1423
    my $need_tables = {};
1424
    $self->_need_tables($tree, $need_tables, $join_tables);
cleanup
Yuki Kimoto authored on 2011-10-21
1425
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} }
1426
      keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1427
    
1428
    # Add join clause
cleanup
Yuki Kimoto authored on 2011-10-21
1429
    $$sql .= $tree->{$_}{join} . ' ' for @need_tables;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1430
}
cleanup
Yuki Kimoto authored on 2011-03-08
1431

            
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1432
sub _quote {
1433
    my $self = shift;
micro optimization
Yuki Kimoto authored on 2011-10-22
1434
    return $self->{reserved_word_quote} || $self->quote || '';
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1435
}
1436

            
cleanup
Yuki Kimoto authored on 2011-07-29
1437
sub _q {
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1438
    my ($self, $value, $quotemeta) = @_;
cleanup
Yuki Kimoto authored on 2011-07-29
1439
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1440
    my $quote = $self->{reserved_word_quote}
1441
      || $self->{quote} || $self->quote || '';
1442
    return "$quote$value$quote"
1443
      if !$quotemeta && ($quote eq '`' || $quote eq '"');
1444
    
cleanup
Yuki Kimoto authored on 2011-07-29
1445
    my $q = substr($quote, 0, 1) || '';
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1446
    my $p;
1447
    if (defined $quote && length $quote > 1) {
1448
        $p = substr($quote, 1, 1);
1449
    }
1450
    else { $p = $q }
cleanup
Yuki Kimoto authored on 2011-07-29
1451
    
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1452
    if ($quotemeta) {
1453
        $q = quotemeta($q);
1454
        $p = quotemeta($p);
1455
    }
1456
    
added quote method's two cha...
Yuki Kimoto authored on 2011-07-29
1457
    return "$q$value$p";
cleanup
Yuki Kimoto authored on 2011-07-29
1458
}
1459

            
cleanup
Yuki Kimoto authored on 2011-04-02
1460
sub _remove_duplicate_table {
1461
    my ($self, $tables, $main_table) = @_;
1462
    
1463
    # Remove duplicate table
1464
    my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1465
    delete $tables{$main_table} if $main_table;
1466
    
micro optimization
Yuki Kimoto authored on 2011-07-30
1467
    my $new_tables = [keys %tables, $main_table ? $main_table : ()];
1468
    if (my $q = $self->_quote) {
1469
        $q = quotemeta($q);
1470
        $_ =~ s/[$q]//g for @$new_tables;
1471
    }
1472

            
1473
    return $new_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1474
}
1475

            
cleanup
Yuki Kimoto authored on 2011-04-02
1476
sub _search_tables {
cleanup
Yuki Kimoto authored on 2011-04-02
1477
    my ($self, $source) = @_;
1478
    
cleanup
Yuki Kimoto authored on 2011-04-02
1479
    # Search tables
cleanup
Yuki Kimoto authored on 2011-04-02
1480
    my $tables = [];
1481
    my $safety_character = $self->safety_character;
reserved_word_quote is DEPRE...
Yuki Kimoto authored on 2011-06-17
1482
    my $q = $self->_quote;
fixex [] reserved_word_quote...
Yuki Kimoto authored on 2011-08-14
1483
    my $quoted_safety_character_re = $self->_q("?([$safety_character]+)", 1);
1484
    my $table_re = $q ? qr/(?:^|[^$safety_character])${quoted_safety_character_re}?\./
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1485
                      : qr/(?:^|[^$safety_character])([$safety_character]+)\./;
cleanup
Yuki Kimoto authored on 2011-04-02
1486
    while ($source =~ /$table_re/g) {
1487
        push @$tables, $1;
1488
    }
1489
    
1490
    return $tables;
1491
}
1492

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1542
sub _where_clause_and_param {
1543
    my ($self, $where, $param) = @_;
1544
 
1545
    $where ||= {};
1546
    $param ||= {};
1547
    my $w = {};
1548
    my $where_clause = '';
1549
    if (ref $where eq 'ARRAY' && !ref $where->[0]) {
1550
        $w->{clause} = "where " . $where->[0];
1551
        $w->{param} = $where->[1];
1552
    }
1553
    elsif (ref $where) {
1554
        $where = $self->_where_to_obj($where);
1555
        $w->{param} = keys %$param
1556
                    ? $self->merge_param($param, $where->param)
1557
                    : $where->param;
1558
        $w->{clause} = $where->to_string;
1559
    }
1560
    elsif ($where) {
1561
        $w->{clause} = "where $where";
1562
        $w->{param} = $param;
1563
    }
1564
    
1565
    return $w;
1566
}
1567

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

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

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1638
# DEPRECATED!
1639
has 'data_source';
1640
has dbi_options => sub { {} };
1641
has filter_check  => 1;
1642
has 'reserved_word_quote';
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1643
has dbi_option => sub { {} };
1644
has default_dbi_option => sub {
1645
    warn "default_dbi_option is DEPRECATED! use default_option instead";
1646
    return shift->default_option;
1647
};
1648

            
- method method of DBIx::Cus...
Yuki Kimoto authored on 2011-10-10
1649
# DEPRECATED!
1650
sub method {
1651
    warn "method is DEPRECATED! use helper instead";
1652
    return shift->helper(@_);
1653
}
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1654

            
1655
# DEPRECATED!
1656
sub assign_param {
1657
    my $self = shift;
1658
    warn "assing_param is DEPRECATED! use assign_clause instead";
1659
    return $self->assign_clause(@_);
1660
}
1661

            
1662
# DEPRECATED
1663
sub update_param {
1664
    my ($self, $param, $opts) = @_;
1665
    
micro optimization
Yuki Kimoto authored on 2011-10-23
1666
    warn "update_param is DEPRECATED! use assign_clause instead.";
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1667
    
1668
    # Create update parameter tag
1669
    my $tag = $self->assign_clause($param, $opts);
1670
    $tag = "set $tag" unless $opts->{no_set};
1671

            
1672
    return $tag;
1673
}
1674

            
updated pod
Yuki Kimoto authored on 2011-06-21
1675
# DEPRECATED!
1676
sub create_query {
1677
    warn "create_query is DEPRECATED! use query option of each method";
1678
    shift->_create_query(@_);
1679
}
1680

            
cleanup
Yuki Kimoto authored on 2011-06-13
1681
# DEPRECATED!
1682
sub apply_filter {
1683
    my $self = shift;
1684
    
- added EXPERIMENTAL order m...
Yuki Kimoto authored on 2011-06-28
1685
    warn "apply_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-06-13
1686
    return $self->_apply_filter(@_);
1687
}
1688

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1693
    warn "select_at is DEPRECATED! use select method id option instead";
updated pod
Yuki Kimoto authored on 2011-06-08
1694

            
cleanup
Yuki Kimoto authored on 2011-10-21
1695
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1696
    my $primary_keys = delete $opt{primary_key};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1697
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1698
    my $where = delete $opt{where};
1699
    my $param = delete $opt{param};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1700
    
1701
    # Table
1702
    croak qq{"table" option must be specified } . _subname
cleanup
Yuki Kimoto authored on 2011-10-21
1703
      unless $opt{table};
1704
    my $table = ref $opt{table} ? $opt{table}->[-1] : $opt{table};
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1705
    
1706
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1707
    my $where_param = $self->_id_to_param($where, $primary_keys);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1708
    
cleanup
Yuki Kimoto authored on 2011-10-21
1709
    return $self->select(where => $where_param, %opt);
select_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1710
}
1711

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

            
cleanup
Yuki Kimoto authored on 2011-10-21
1716
    warn "delete_at is DEPRECATED! use delete method id option instead";
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1717
    
cleanup
Yuki Kimoto authored on 2011-10-21
1718
    # Options
cleanup
Yuki Kimoto authored on 2011-10-21
1719
    my $primary_keys = delete $opt{primary_key};
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1720
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1721
    my $where = delete $opt{where};
delete_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);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1725
    
cleanup
Yuki Kimoto authored on 2011-10-21
1726
    return $self->delete(where => $where_param, %opt);
delete_at is DEPRECATED! use...
Yuki Kimoto authored on 2011-06-08
1727
}
1728

            
cleanup
Yuki Kimoto authored on 2011-06-08
1729
# DEPRECATED!
1730
sub update_at {
1731
    my $self = shift;
1732

            
cleanup
Yuki Kimoto authored on 2011-10-21
1733
    warn "update_at is DEPRECATED! use update method id option instead";
cleanup
Yuki Kimoto authored on 2011-06-08
1734
    
cleanup
Yuki Kimoto authored on 2011-10-21
1735
    # Options
cleanup
Yuki Kimoto authored on 2011-06-08
1736
    my $param;
1737
    $param = shift if @_ % 2;
cleanup
Yuki Kimoto authored on 2011-10-21
1738
    my %opt = @_;
1739
    my $primary_keys = delete $opt{primary_key};
cleanup
Yuki Kimoto authored on 2011-06-08
1740
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-10-21
1741
    my $where = delete $opt{where};
1742
    my $p = delete $opt{param} || {};
cleanup
Yuki Kimoto authored on 2011-06-08
1743
    $param  ||= $p;
1744
    
1745
    # Create where parameter
cleanup
Yuki Kimoto authored on 2011-10-21
1746
    my $where_param = $self->_id_to_param($where, $primary_keys);
cleanup
Yuki Kimoto authored on 2011-06-08
1747
    
cleanup
Yuki Kimoto authored on 2011-10-21
1748
    return $self->update(where => $where_param, param => $param, %opt);
cleanup
Yuki Kimoto authored on 2011-06-08
1749
}
1750

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

            
added warnings
Yuki Kimoto authored on 2011-06-07
1774
# DEPRECATED!
1775
sub register_tag {
test cleanup
Yuki Kimoto authored on 2011-08-10
1776
    my $self = shift;
1777
    
added warnings
Yuki Kimoto authored on 2011-06-07
1778
    warn "register_tag is DEPRECATED!";
test cleanup
Yuki Kimoto authored on 2011-08-10
1779
    
1780
    # Merge tag
1781
    my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1782
    $self->{_tags} = {%{$self->{_tags} || {}}, %$tags};
1783
    
1784
    return $self;
1785
}
1786

            
1787
# DEPRECATED!
1788
sub register_tag_processor {
1789
    my $self = shift;
1790
    warn "register_tag_processor is DEPRECATED!";
1791
    # Merge tag
1792
    my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
1793
    $self->{_tags} = {%{$self->{_tags} || {}}, %{$tag_processors}};
1794
    return $self;
added warnings
Yuki Kimoto authored on 2011-06-07
1795
}
1796

            
cleanup
Yuki Kimoto authored on 2011-01-25
1797
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1798
sub default_bind_filter {
1799
    my $self = shift;
1800
    
cleanup
Yuki Kimoto authored on 2011-06-13
1801
    warn "default_bind_filter is DEPRECATED!";
added warnings
Yuki Kimoto authored on 2011-06-07
1802
    
cleanup
Yuki Kimoto authored on 2011-01-12
1803
    if (@_) {
1804
        my $fname = $_[0];
1805
        
1806
        if (@_ && !$fname) {
1807
            $self->{default_out_filter} = undef;
1808
        }
1809
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1810
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1811
              unless exists $self->filters->{$fname};
1812
        
1813
            $self->{default_out_filter} = $self->filters->{$fname};
1814
        }
1815
        return $self;
1816
    }
1817
    
1818
    return $self->{default_out_filter};
1819
}
1820

            
cleanup
Yuki Kimoto authored on 2011-01-25
1821
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1822
sub default_fetch_filter {
1823
    my $self = shift;
added warnings
Yuki Kimoto authored on 2011-06-07
1824

            
cleanup
Yuki Kimoto authored on 2011-06-13
1825
    warn "default_fetch_filter is DEPRECATED!";
cleanup
Yuki Kimoto authored on 2011-01-12
1826
    
1827
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1828
        my $fname = $_[0];
1829

            
cleanup
Yuki Kimoto authored on 2011-01-12
1830
        if (@_ && !$fname) {
1831
            $self->{default_in_filter} = undef;
1832
        }
1833
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1834
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1835
              unless exists $self->filters->{$fname};
1836
        
1837
            $self->{default_in_filter} = $self->filters->{$fname};
1838
        }
1839
        
1840
        return $self;
1841
    }
1842
    
many changed
Yuki Kimoto authored on 2011-01-23
1843
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1844
}
1845

            
- update_param is DEPRECATED...
Yuki Kimoto authored on 2011-10-04
1846
# DEPRECATED!
1847
sub insert_param {
1848
    my $self = shift;
1849
    warn "insert_param is DEPRECATED! use values_clause instead";
1850
    return $self->values_clause(@_);
1851
}
1852

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1853
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1854
sub insert_param_tag {
1855
    warn "insert_param_tag is DEPRECATED! " .
1856
         "use insert_param instead!";
1857
    return shift->insert_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1858
}
1859

            
1860
# DEPRECATED!
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1861
sub update_param_tag {
fixed DEPRECATED message bug
Yuki Kimoto authored on 2011-06-10
1862
    warn "update_param_tag is DEPRECATED! " .
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1863
         "use update_param instead";
1864
    return shift->update_param(@_);
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1865
}
cleanup
Yuki Kimoto authored on 2011-03-08
1866
# DEPRECATED!
1867
sub _push_relation {
1868
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1869
    
1870
    if (keys %{$relation || {}}) {
micro optimization
Yuki Kimoto authored on 2011-09-30
1871
        $$sql .= $need_where ? 'where ' : 'and ';
cleanup
Yuki Kimoto authored on 2011-10-21
1872
        for my $rcolumn (keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-03-08
1873
            my $table1 = (split (/\./, $rcolumn))[0];
1874
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1875
            push @$tables, ($table1, $table2);
micro optimization
Yuki Kimoto authored on 2011-09-30
1876
            $$sql .= "$rcolumn = " . $relation->{$rcolumn} .  'and ';
cleanup
Yuki Kimoto authored on 2011-03-08
1877
        }
1878
    }
micro optimization
Yuki Kimoto authored on 2011-09-30
1879
    $$sql =~ s/and $/ /;
cleanup
Yuki Kimoto authored on 2011-03-08
1880
}
1881

            
1882
# DEPRECATED!
1883
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1884
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1885
    
1886
    if (keys %{$relation || {}}) {
cleanup
Yuki Kimoto authored on 2011-10-21
1887
        for my $rcolumn (keys %$relation) {
cleanup
Yuki Kimoto authored on 2011-03-08
1888
            my $table1 = (split (/\./, $rcolumn))[0];
1889
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1890
            my $table1_exists;
1891
            my $table2_exists;
cleanup
Yuki Kimoto authored on 2011-10-21
1892
            for my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-03-08
1893
                $table1_exists = 1 if $table eq $table1;
1894
                $table2_exists = 1 if $table eq $table2;
1895
            }
1896
            unshift @$tables, $table1 unless $table1_exists;
1897
            unshift @$tables, $table2 unless $table2_exists;
1898
        }
1899
    }
1900
}
1901

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1904
=head1 NAME
1905

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

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

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1910
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1911
    
1912
    # Connect
1913
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
1914
        dsn => "dbi:mysql:database=dbname",
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1915
        user => 'ken',
1916
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
1917
        option => {mysql_enable_utf8 => 1}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1918
    );
cleanup
yuki-kimoto authored on 2010-08-05
1919

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1920
    # Insert 
updated pod
Yuki Kimoto authored on 2011-06-21
1921
    $dbi->insert({title => 'Perl', author => 'Ken'}, table  => 'book');
removed reconnect method
yuki-kimoto authored on 2010-05-28
1922
    
1923
    # Update 
updated pod
Yuki Kimoto authored on 2011-06-21
1924
    $dbi->update({title => 'Perl', author => 'Ken'}, table  => 'book',
1925
      where  => {id => 5});
removed reconnect method
yuki-kimoto authored on 2010-05-28
1926
    
1927
    # Delete
updated pod
Yuki Kimoto authored on 2011-06-21
1928
    $dbi->delete(table  => 'book', where => {author => 'Ken'});
cleanup
yuki-kimoto authored on 2010-08-05
1929

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1934
    # Select, more complex
1935
    my $result = $dbi->select(
1936
        table  => 'book',
1937
        column => [
cleanup
Yuki Kimoto authored on 2011-06-13
1938
            {book => [qw/title author/]},
1939
            {company => ['name']}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1940
        ],
1941
        where  => {'book.author' => 'Ken'},
1942
        join => ['left outer join company on book.company_id = company.id'],
1943
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1944
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1945
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1946
    # Fetch
1947
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1948
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1949
    }
1950
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1951
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1952
    while (my $row = $result->fetch_hash) {
1953
        
1954
    }
1955
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1956
    # Execute SQL with parameter.
1957
    $dbi->execute(
- update_param_tag is DEPREC...
Yuki Kimoto authored on 2011-06-07
1958
        "select id from book where author = :author and title like :title",
updated pod
Yuki Kimoto authored on 2011-06-21
1959
        {author => 'ken', title => '%Perl%'}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1960
    );
1961
    
fix heading typos
Terrence Brannon authored on 2011-08-17
1962
=head1 DESCRIPTION
removed reconnect method
yuki-kimoto authored on 2010-05-28
1963

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

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

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

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

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
1979
Named place holder support
1980

            
1981
=item *
1982

            
cleanup
Yuki Kimoto authored on 2011-07-29
1983
Model support
1984

            
1985
=item *
1986

            
1987
Connection manager support
1988

            
1989
=item *
pod fix
Yuki Kimoto authored on 2011-01-21
1990

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

            
1995
=item *
1996

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

            
1999
=item *
2000

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

            
2003
=back
pod fix
Yuki Kimoto authored on 2011-01-21
2004

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

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

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2012
Module documentations - 
cleanup
Yuki Kimoto authored on 2011-07-29
2013
L<DBIx::Custom::Result>,
2014
L<DBIx::Custom::Query>,
2015
L<DBIx::Custom::Where>,
2016
L<DBIx::Custom::Model>,
2017
L<DBIx::Custom::Order>
updated document
yuki-kimoto authored on 2010-08-08
2018

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

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

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

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

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

            
2032
    my $connector = DBIx::Connector->new(
cleanup
Yuki Kimoto authored on 2011-08-16
2033
        "dbi:mysql:database=$database",
2034
        $user,
2035
        $password,
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2036
        DBIx::Custom->new->default_option
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2037
    );
2038
    
updated pod
Yuki Kimoto authored on 2011-06-21
2039
    my $dbi = DBIx::Custom->connect(connector => $connector);
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
2040

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

            
2044
    my $dbi = DBIx::Custom->connect(
2045
      dsn => $dsn, user => $user, password => $password, connector => 1);
2046
    
2047
    my $connector = $dbi->connector; # DBIx::Connector
2048

            
2049
Note that L<DBIx::Connector> must be installed.
2050

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

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

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

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

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
2066
    {
2067
        RaiseError => 1,
2068
        PrintError => 0,
2069
        AutoCommit => 1,
2070
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
2071

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

            
2074
    my $exclude_table = $dbi->exclude_table;
2075
    $dbi = $dbi->exclude_table(qr/pg_/);
2076

            
2077
Excluded table regex.
2078
C<each_column>, C<each_table>, C<type_rule>,
2079
and C<setup_model> methods ignore matching tables.
2080

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

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

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

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

            
2090
    my $last_sql = $dbi->last_sql;
2091
    $dbi = $dbi->last_sql($last_sql);
2092

            
2093
Get last successed SQL executed by C<execute> method.
2094

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

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

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

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

            
2104
    my $option = $dbi->option;
2105
    $dbi = $dbi->option($option);
2106

            
2107
L<DBI> option, used when C<connect> method is executed.
2108
Each value in option override the value of C<default_option>.
2109

            
cleanup
yuki-kimoto authored on 2010-10-17
2110
=head2 C<password>
2111

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-07-30
2132
You can set quote pair.
2133

            
2134
    $dbi->quote('[]');
2135

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

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

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

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

            
fixed pod
Yuki Kimoto authored on 2011-09-27
2145
    my $safety_character = $dbi->safety_character;
2146
    $dbi = $dbi->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
2147

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

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

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

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

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

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

            
2164
    my $tag_parse = $dbi->tag_parse(0);
2165
    $dbi = $dbi->tag_parse;
2166

            
2167
Enable DEPRECATED tag parsing functionality, default to 1.
2168
If you want to disable tag parsing functionality, set to 0.
2169

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

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

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

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

            
2179
    my $user_column_info = $dbi->user_column_info;
2180
    $dbi = $dbi->user_column_info($user_column_info);
2181

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

            
2184
    [
2185
        {table => 'book', column => 'title', info => {...}},
2186
        {table => 'author', column => 'name', info => {...}}
2187
    ]
2188

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

            
2191
    my $user_column_info
2192
      = $dbi->get_column_info(exclude_table => qr/^system/);
2193
    $dbi->user_column_info($user_column_info);
2194

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

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

            
2200
    my $user_table_info = $dbi->user_table_info;
2201
    $dbi = $dbi->user_table_info($user_table_info);
2202

            
2203
You can set the following data.
2204

            
2205
    [
2206
        {table => 'book', info => {...}},
2207
        {table => 'author', info => {...}}
2208
    ]
2209

            
2210
Usually, you can set return value of C<get_table_info>.
2211

            
2212
    my $user_table_info = $dbi->get_table_info(exclude => qr/^system/);
2213
    $dbi->user_table_info($user_table_info);
2214

            
2215
If C<user_table_info> is set, C<each_table> use C<user_table_info>
2216
to find table info.
2217

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
2254
Create column clause. The follwoing column clause is created.
2255

            
2256
    book.author as "book.author",
2257
    book.title as "book.title"
2258

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

            
cleanup
Yuki Kimoto authored on 2011-06-13
2261
    # Separator is hyphen
2262
    $dbi->separator('-');
2263
    
2264
    book.author as "book-author",
2265
    book.title as "book-title"
2266
    
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2267
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
2268

            
update pod
Yuki Kimoto authored on 2011-03-13
2269
    my $dbi = DBIx::Custom->connect(
data_source is DEPRECATED! I...
Yuki Kimoto authored on 2011-06-06
2270
        dsn => "dbi:mysql:database=dbname",
update pod
Yuki Kimoto authored on 2011-03-13
2271
        user => 'ken',
2272
        password => '!LFKD%$&',
- dbi_option attribute is re...
Yuki Kimoto authored on 2011-10-05
2273
        option => {mysql_enable_utf8 => 1}
update pod
Yuki Kimoto authored on 2011-03-13
2274
    );
2275

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

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

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

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

            
2286
Get rows count.
2287

            
2288
Options is same as C<select> method's ones.
2289

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

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
2292
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2293
        table => 'book',
2294
        primary_key => 'id',
2295
        join => [
2296
            'inner join company on book.comparny_id = company.id'
2297
        ],
2298
    );
2299

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

            
2303
   $dbi->model('book')->select(...);
2304

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

            
2307
    my $dbh = $dbi->dbh;
2308

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

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

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

            
2316
Execute delete statement.
2317

            
2318
The following opitons are available.
2319

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

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

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

            
2327
=item C<id>
2328

            
2329
    id => 4
2330
    id => [4, 5]
2331

            
2332
ID corresponding to C<primary_key>.
2333
You can delete rows by C<id> and C<primary_key>.
2334

            
2335
    $dbi->delete(
fixed pod
Yuki Kimoto authored on 2011-10-20
2336
        primary_key => ['id1', 'id2'],
added EXPERIMENTAL find_tabl...
Yuki Kimoto authored on 2011-08-16
2337
        id => [4, 5],
2338
        table => 'book',
2339
    );
2340

            
2341
The above is same as the followin one.
2342

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

            
2345
=item C<prefix>
2346

            
2347
    prefix => 'some'
2348

            
2349
prefix before table name section.
2350

            
2351
    delete some from book
2352

            
2353
=item C<table>
2354

            
2355
    table => 'book'
2356

            
2357
Table name.
2358

            
2359
=item C<where>
2360

            
2361
Same as C<select> method's C<where> option.
2362

            
2363
=back
2364

            
2365
=head2 C<delete_all>
2366

            
2367
    $dbi->delete_all(table => $table);
2368

            
2369
Execute delete statement for all rows.
2370
Options is same as C<delete>.
2371

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

            
2374
    $dbi->each_column(
2375
        sub {
2376
            my ($dbi, $table, $column, $column_info) = @_;
2377
            
2378
            my $type = $column_info->{TYPE_NAME};
2379
            
2380
            if ($type eq 'DATE') {
2381
                # ...
2382
            }
2383
        }
2384
    );
2385

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

            
2391
If C<user_column_info> is set, C<each_column> method use C<user_column_info>
2392
infromation, you can improve the performance of C<each_column> in
2393
the following way.
2394

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

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

            
2401
    $dbi->each_table(
2402
        sub {
2403
            my ($dbi, $table, $table_info) = @_;
2404
            
2405
            my $table_name = $table_info->{TABLE_NAME};
2406
        }
2407
    );
2408

            
improved pod
Yuki Kimoto authored on 2011-10-14
2409
Iterate all table informationsfrom in database.
2410
Argument is callback which is executed when one table is found.
2411
Callback receive three arguments, C<DBIx::Custom object>, C<table name>,
2412
C<table information>.
2413

            
2414
If C<user_table_info> is set, C<each_table> method use C<user_table_info>
2415
infromation, you can improve the performance of C<each_table> in
2416
the following way.
2417

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

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

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

            
2429
    my $result = $dbi->execute(
2430
      "select * from book where title = :book.title and author like :book.author",
2431
      {'book.title' => 'Perl', 'book.author' => '%Ken%'}
update pod
Yuki Kimoto authored on 2011-03-13
2432
    );
2433

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

            
I call :title named placehol...
Yuki Kimoto authored on 2011-07-30
2440
Named placeholder such as C<:title> is replaced by placeholder C<?>.
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2441
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2442
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2443
    select * from book where title = :title and author like :author
2444
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2445
    # Replaced
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2446
    select * from where title = ? and author like ?;
update pod
Yuki Kimoto authored on 2011-03-13
2447

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

            
micro optimization
Yuki Kimoto authored on 2011-07-30
2451
    # Original
added EXPERIMENTAL parameter...
Yuki Kimoto authored on 2011-07-29
2452
    select * from book where :title{=} and :author{like}
2453
    
micro optimization
Yuki Kimoto authored on 2011-07-30
2454
    # Replaced
update pod
Yuki Kimoto authored on 2011-03-13
2455
    select * from where title = ? and author like ?;
2456

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

            
2461
    select * from where title = "aa\\:bb";
2462

            
cleanup
Yuki Kimoto authored on 2011-10-20
2463
B<OPTIONS>
2464

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

            
2467
=over 4
2468

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

            
2471
You can filter sql after the sql is build.
2472

            
2473
    after_build_sql => $code_ref
2474

            
2475
The following one is one example.
2476

            
2477
    $dbi->select(
2478
        table => 'book',
2479
        column => 'distinct(name)',
2480
        after_build_sql => sub {
2481
            "select count(*) from ($_[0]) as t1"
2482
        }
2483
    );
2484

            
2485
The following SQL is executed.
2486

            
2487
    select count(*) from (select distinct(name) from book) as t1;
2488

            
cleanup
Yuki Kimoto authored on 2011-10-20
2489
=item C<append>
2490

            
2491
    append => 'order by name'
2492

            
2493
Append some statement after SQL.
2494

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

            
2497
Specify database bind data type.
2498

            
2499
    bind_type => [image => DBI::SQL_BLOB]
2500
    bind_type => [[qw/image audio/] => DBI::SQL_BLOB]
2501

            
2502
This is used to bind parameter by C<bind_param> of statment handle.
2503

            
2504
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2505

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

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

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

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

            
2531
    id => 4
2532
    id => [4, 5]
2533

            
2534
ID corresponding to C<primary_key>.
2535
You can delete rows by C<id> and C<primary_key>.
2536

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

            
2544
The above is same as the followin one.
2545

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

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

            
2553
    query => 1
2554

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

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

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

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

            
2573
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
2574
You can do the following way.
cleanup
Yuki Kimoto authored on 2011-07-30
2575
    
2576
    my $query;
2577
    my $sth;
cleanup
Yuki Kimoto authored on 2011-10-21
2578
    for my $row (@$rows) {
cleanup
Yuki Kimoto authored on 2011-07-30
2579
      $query ||= $dbi->insert($row, table => 'book', query => 1);
2580
      $sth ||= $query->sth;
2581
      $sth->execute(map { $row->{$_} } sort keys %$row);
2582
    }
2583

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

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
2590
    primary_key => 'id'
2591
    primary_key => ['id1', 'id2']
2592

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

            
updated pod
Yuki Kimoto authored on 2011-06-09
2595
=item C<table>
2596
    
2597
    table => 'author'
2598

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

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

            
updated pod
Yuki Kimoto authored on 2011-06-21
2606
    # Same
2607
    $dbi->execute(
2608
      "select * from book where title = :book.title and author = :book.author",
2609
      {title => 'Perl', author => 'Ken');
added EXPERIMENTAL execute()...
Yuki Kimoto authored on 2011-06-09
2610

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

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

            
2615
Table alias. Key is real table name, value is alias table name.
2616
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2617
on alias table name.
2618

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2619
=item C<reuse EXPERIMENTAL>
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2620
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2621
    reuse_query => $has_ref
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2622

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2623
Reuse query object if the hash reference variable is set.
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2624
    
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2625
    my $queries = {};
2626
    $dbi->execute($sql, $param, reuse => $queries);
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2627

            
added EXPERIMENTAL reuse_que...
Yuki Kimoto authored on 2011-10-22
2628
This will improved performance when you want to execute same query repeatedly
2629
because generally creating query object is slow.
added EXPERIMENTAL reuse_sth...
Yuki Kimoto authored on 2011-10-22
2630

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

            
2633
    type_rule_off => 1
2634

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

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

            
2639
    type_rule1_off => 1
2640

            
2641
Turn C<into1> type rule off.
2642

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

            
2645
    type_rule2_off => 1
2646

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

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

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

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

            
2655
get column infomation except for one which match C<exclude_table> pattern.
2656

            
2657
    [
2658
        {table => 'book', column => 'title', info => {...}},
2659
        {table => 'author', column => 'name' info => {...}}
2660
    ]
2661

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

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

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

            
2668
    [
2669
        {table => 'book', info => {...}},
2670
        {table => 'author', info => {...}}
2671
    ]
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2672

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

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

            
2677
    $dbi->helper(
2678
        update_or_insert => sub {
2679
            my $self = shift;
2680
            
2681
            # Process
2682
        },
2683
        find_or_create   => sub {
2684
            my $self = shift;
2685
            
2686
            # Process
2687
        }
2688
    );
2689

            
2690
Register helper. These helper is called directly from L<DBIx::Custom> object.
2691

            
2692
    $dbi->update_or_insert;
2693
    $dbi->find_or_create;
2694

            
cleanup
yuki-kimoto authored on 2010-10-17
2695
=head2 C<insert>
2696

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

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

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

            
2705
    {date => \"NOW()"}
2706

            
cleanup
Yuki Kimoto authored on 2011-10-20
2707
B<options>
2708

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
2716
    id => 4
2717
    id => [4, 5]
update pod
Yuki Kimoto authored on 2011-03-13
2718

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

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

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

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

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

            
2738
    prefix => 'or replace'
2739

            
2740
prefix before table name section
2741

            
2742
    insert or replace into book
2743

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

            
2746
    table => 'book'
2747

            
2748
Table name.
2749

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

            
2752
    timestamp => 1
2753

            
2754
If this value is set to 1,
2755
automatically created timestamp column is set based on
2756
C<timestamp> attribute's C<insert> value.
2757

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

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

            
2762
placeholder wrapped string.
2763

            
2764
If the following statement
2765

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

            
2769
is executed, the following SQL is executed.
2770

            
2771
    insert into book price values ( ? + 5 );
2772

            
update pod
Yuki Kimoto authored on 2011-03-13
2773
=back
2774

            
2775
=over 4
2776

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2784
    lib / MyModel.pm
2785
        / MyModel / book.pm
2786
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2787

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

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

            
2792
    package MyModel;
updated pod
Yuki Kimoto authored on 2011-06-21
2793
    use DBIx::Custom::Model -base;
update pod
Yuki Kimoto authored on 2011-03-13
2794
    
2795
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2796

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2801
    package MyModel::book;
updated pod
Yuki Kimoto authored on 2011-06-21
2802
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2803
    
2804
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2805

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2808
    package MyModel::company;
updated pod
Yuki Kimoto authored on 2011-06-21
2809
    use MyModel -base;
update pod
Yuki Kimoto authored on 2011-03-13
2810
    
2811
    1;
2812
    
updated pod
Yuki Kimoto authored on 2011-06-21
2813
MyModel::book and MyModel::company is included by C<include_model>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2814

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

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

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

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

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
2824
    $dbi->insert_timestamp(
2825
      [qw/created_at updated_at/]
2826
        => sub { Time::Piece->localtime->strftime("%Y-%m-%d %H:%M:%S") }
2827
    );
- added EXPERIMENTAL update_...
Yuki Kimoto authored on 2011-09-12
2828

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

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

            
moved DBIx::Custom::Guide to...
Yuki Kimoto authored on 2011-10-22
2836
    $dbi->insert($param, table => 'book', timestamp => 1);
sqlfilter option is renamed ...
Yuki Kimoto authored on 2011-09-16
2837

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

            
2840
    my $like_value = $dbi->like_value
2841

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

            
2844
    sub { "%$_[0]%" }
2845

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

            
2848
    my $mapper = $dbi->mapper(param => $param);
2849

            
2850
Create a new L<DBIx::Custom::Mapper> object.
2851

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

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

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

            
2858
    {key1 => [1, 1], key2 => 2}
2859

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

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

            
2864
    my $model = $dbi->model('book');
2865

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

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

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

            
2873
Create column clause for myself. The follwoing column clause is created.
2874

            
2875
    book.author as author,
2876
    book.title as title
2877

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

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

            
2887
Create a new L<DBIx::Custom> object.
2888

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

            
2891
    my $not_exists = $dbi->not_exists;
2892

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

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

            
2898
    my $order = $dbi->order;
2899

            
2900
Create a new L<DBIx::Custom::Order> object.
2901

            
cleanup
yuki-kimoto authored on 2010-10-17
2902
=head2 C<register_filter>
2903

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

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

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

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

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

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

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2936
=item C<column>
2937
    
updated document
Yuki Kimoto authored on 2011-06-09
2938
    column => 'author'
2939
    column => ['author', 'title']
2940

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
2949
    column => [
updated pod
Yuki Kimoto authored on 2011-06-07
2950
        {book => [qw/author title/]},
2951
        {person => [qw/name age/]}
updated document
Yuki Kimoto authored on 2011-06-09
2952
    ]
updated pod
Yuki Kimoto authored on 2011-06-07
2953

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

            
2956
    book.author as "book.author",
2957
    book.title as "book.title",
2958
    person.name as "person.name",
2959
    person.age as "person.age"
2960

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

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

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

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

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

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

            
2976
=item C<id>
2977

            
2978
    id => 4
2979
    id => [4, 5]
2980

            
2981
ID corresponding to C<primary_key>.
2982
You can select rows by C<id> and C<primary_key>.
2983

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

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

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

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

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

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

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

            
3011
    prefix => 'SQL_CALC_FOUND_ROWS'
3012

            
3013
Prefix of column cluase
3014

            
3015
    select SQL_CALC_FOUND_ROWS title, author from book;
3016

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2011-06-13
3040
    select company.location_id as location_id
update pod
Yuki Kimoto authored on 2011-03-12
3041
    from book
3042
      left outer join company on book.company_id = company.id
cleanup
Yuki Kimoto authored on 2011-06-13
3043
    where company.name = ?;
update pod
Yuki Kimoto authored on 2011-03-12
3044

            
added join new syntax
Yuki Kimoto authored on 2011-07-28
3045
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
3046
the join clause correctly.
added join new syntax
Yuki Kimoto authored on 2011-07-28
3047

            
3048
    $dbi->select(
3049
        table => 'book',
3050
        column => ['company.location_id as location_id'],
3051
        where => {'company.name' => 'Orange'},
3052
        join => [
3053
            {
3054
                clause => 'left outer join location on company.location_id = location.id',
3055
                table => ['company', 'location']
3056
            }
3057
        ]
3058
    );
3059

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3064
Table name.
updated pod
Yuki Kimoto authored on 2011-06-08
3065

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

            
cleanup
Yuki Kimoto authored on 2011-10-20
3092
Where clause. See L<DBIx::Custom::Where>.
updated document
Yuki Kimoto authored on 2011-06-09
3093
    
update pod
Yuki Kimoto authored on 2011-03-12
3094
=back
cleanup
Yuki Kimoto authored on 2011-03-08
3095

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

            
3098
    $dbi->setup_model;
3099

            
3100
Setup all model objects.
3101
C<columns> of model object is automatically set, parsing database information.
3102

            
3103
=head2 C<type_rule>
3104

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

            
3128
Filtering rule when data is send into and get from database.
3129
This has a little complex problem.
3130

            
3131
In C<into1> and C<into2> you can specify
3132
type name as same as type name defined
3133
by create table, such as C<DATETIME> or C<DATE>.
3134

            
3135
Note that type name and data type don't contain upper case.
3136
If these contain upper case charactor, you convert it to lower case.
3137

            
3138
C<into2> is executed after C<into1>.
3139

            
3140
Type rule of C<into1> and C<into2> is enabled on the following
3141
column name.
3142

            
3143
=over 4
3144

            
3145
=item 1. column name
3146

            
3147
    issue_date
3148
    issue_datetime
3149

            
3150
This need C<table> option in each method.
3151

            
3152
=item 2. table name and column name, separator is dot
3153

            
3154
    book.issue_date
3155
    book.issue_datetime
3156

            
3157
=back
3158

            
3159
You get all type name used in database by C<available_typename>.
3160

            
3161
    print $dbi->available_typename;
3162

            
3163
In C<from1> and C<from2> you specify data type, not type name.
3164
C<from2> is executed after C<from1>.
3165
You get all data type by C<available_datatype>.
3166

            
3167
    print $dbi->available_datatype;
3168

            
3169
You can also specify multiple types at once.
3170

            
3171
    $dbi->type_rule(
3172
        into1 => [
3173
            [qw/DATE DATETIME/] => sub { ... },
3174
        ],
3175
    );
3176

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

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

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

            
3183
If you want to set constant value to row data, use scalar reference
3184
as parameter value.
3185

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

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

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3197
    id => 4
3198
    id => [4, 5]
- insert, insert_at, update,...
Yuki Kimoto authored on 2011-06-08
3199

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

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

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

            
updated document
Yuki Kimoto authored on 2011-06-09
3212
    $dbi->update(
3213
        {title => 'Perl', author => 'Ken'}
3214
        where => {id1 => 4, id2 => 5},
3215
        table => 'book'
3216
    );
update pod
Yuki Kimoto authored on 2011-03-13
3217

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

            
3220
    prefix => 'or replace'
3221

            
3222
prefix before table name section
3223

            
3224
    update or replace book
3225

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

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

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

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

            
3234
    timestamp => 1
3235

            
3236
If this value is set to 1,
3237
automatically updated timestamp column is set based on
3238
C<timestamp> attribute's C<update> value.
3239

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

            
3242
Same as C<select> method's C<where> option.
3243

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

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

            
3248
placeholder wrapped string.
3249

            
3250
If the following statement
3251

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

            
3255
is executed, the following SQL is executed.
3256

            
3257
    update book set price =  ? + 5;
3258

            
updated pod
Yuki Kimoto authored on 2011-06-08
3259
=back
update pod
Yuki Kimoto authored on 2011-03-13
3260

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

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

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

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

            
3289
In both examples, the following SQL is executed.
3290

            
3291
    # In case insert
3292
    insert into book (id, title) values (?, ?)
3293
    
3294
    # In case update
3295
    update book set (id = ?, title = ?) where book.id = ?
3296

            
3297
The following opitons are available adding to C<update> option.
3298

            
3299
=over 4
3300

            
3301
=item C<select_option>
3302

            
3303
    select_option => {append => 'for update'}
3304

            
3305
select method option,
3306
select method is used to check the row is already exists.
3307

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

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

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

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

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

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

            
3328
    $dbi->show_datatype($table);
3329

            
3330
Show data type of the columns of specified table.
3331

            
3332
    book
3333
    title: 5
3334
    issue_date: 91
3335

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

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

            
3340
    $dbi->show_tables;
3341

            
3342
Show tables.
3343

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

            
3346
    $dbi->show_typename($table);
3347

            
3348
Show type name of the columns of specified table.
3349

            
3350
    book
3351
    title: varchar
3352
    issue_date: date
3353

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

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

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

            
3360
Create values clause.
3361

            
3362
    (title, author) values (title = :title, age = :age);
3363

            
3364
You can use this in insert statement.
3365

            
3366
    my $insert_sql = "insert into book $values_clause";
3367

            
3368
=head2 C<where>
3369

            
3370
    my $where = $dbi->where(
3371
        clause => ['and', 'title = :title', 'author = :author'],
3372
        param => {title => 'Perl', author => 'Ken'}
3373
    );
3374

            
3375
Create a new L<DBIx::Custom::Where> object.
3376

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

            
3379
=head2 C<DBIX_CUSTOM_DEBUG>
3380

            
3381
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
3382
executed SQL and bind values are printed to STDERR.
3383

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

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

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

            
3390
L<DBIx::Custom>
3391

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

            
3437
L<DBIx::Custom::Model>
3438

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

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

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

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

            
3480
L<DBIx::Custom::Tag>
3481

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

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

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

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

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

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

            
3499
C<< <kimoto.yuki at gmail.com> >>
3500

            
3501
L<http://github.com/yuki-kimoto/DBIx-Custom>
3502

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
3503
=head1 AUTHOR
3504

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

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

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

            
3511
This program is free software; you can redistribute it and/or modify it
3512
under the same terms as Perl itself.
3513

            
3514
=cut