DBIx-Custom / lib / DBIx / Custom.pm /
Newer Older
2778 lines | 66.962kb
cleanup
yuki-kimoto authored on 2009-12-22
1
package DBIx::Custom;
2

            
improved table search in col...
Yuki Kimoto authored on 2011-04-12
3
our $VERSION = '0.1677';
fixed DBIx::Custom::QueryBui...
yuki-kimoto authored on 2010-08-15
4

            
5
use 5.008001;
cleanup
yuki-kimoto authored on 2009-12-22
6
use strict;
7
use warnings;
8

            
remove run_transaction().
yuki-kimoto authored on 2010-01-30
9
use base 'Object::Simple';
many change
yuki-kimoto authored on 2010-02-11
10

            
packaging one directory
yuki-kimoto authored on 2009-11-16
11
use Carp 'croak';
12
use DBI;
13
use DBIx::Custom::Result;
cleanup
yuki-kimoto authored on 2010-02-11
14
use DBIx::Custom::Query;
cleanup
yuki-kimoto authored on 2010-08-05
15
use DBIx::Custom::QueryBuilder;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
16
use DBIx::Custom::Where;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
17
use DBIx::Custom::Model;
cleanup
Yuki Kimoto authored on 2011-01-25
18
use DBIx::Custom::Tag;
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
19
use DBIx::Custom::Util;
update document
yuki-kimoto authored on 2010-05-27
20
use Encode qw/encode_utf8 decode_utf8/;
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;
23

            
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
24
our @COMMON_ARGS = qw/table query filter type/;
cleanup
Yuki Kimoto authored on 2011-03-21
25

            
fix tests
Yuki Kimoto authored on 2011-01-13
26
__PACKAGE__->attr(
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
27
    [qw/connector data_source password pid user/],
removed from cache() and cac...
Yuki Kimoto authored on 2011-03-29
28
    cache => 0,
many changed
Yuki Kimoto authored on 2011-01-23
29
    cache_method => sub {
30
        sub {
31
            my $self = shift;
32
            
33
            $self->{_cached} ||= {};
34
            
35
            if (@_ > 1) {
update pod
Yuki Kimoto authored on 2011-03-13
36
                $self->{_cached}{$_[0]} = $_[1];
many changed
Yuki Kimoto authored on 2011-01-23
37
            }
38
            else {
update pod
Yuki Kimoto authored on 2011-03-13
39
                return $self->{_cached}{$_[0]};
many changed
Yuki Kimoto authored on 2011-01-23
40
            }
41
        }
update pod
Yuki Kimoto authored on 2011-03-13
42
    },
43
    dbi_option => sub { {} },
44
    default_dbi_option => sub {
45
        {
46
            RaiseError => 1,
47
            PrintError => 0,
48
            AutoCommit => 1
49
        }
50
    },
fix tests
Yuki Kimoto authored on 2011-01-13
51
    filters => sub {
52
        {
53
            encode_utf8 => sub { encode_utf8($_[0]) },
54
            decode_utf8 => sub { decode_utf8($_[0]) }
55
        }
update pod
Yuki Kimoto authored on 2011-03-13
56
    },
57
    models => sub { {} },
58
    query_builder => sub { DBIx::Custom::QueryBuilder->new },
59
    result_class  => 'DBIx::Custom::Result',
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
60
    reserved_word_quote => '',
update pod
Yuki Kimoto authored on 2011-03-13
61
    safety_character => '\w',
62
    stash => sub { {} }
fix tests
Yuki Kimoto authored on 2011-01-13
63
);
cleanup
yuki-kimoto authored on 2010-10-17
64

            
added helper method
yuki-kimoto authored on 2010-10-17
65
our $AUTOLOAD;
66
sub AUTOLOAD {
67
    my $self = shift;
68

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

            
cleanup
Yuki Kimoto authored on 2011-04-02
72
    # Call method
renamed helper to method.
Yuki Kimoto authored on 2011-01-25
73
    $self->{_methods} ||= {};
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
74
    if (my $method = $self->{_methods}->{$mname}) {
75
        return $self->$method(@_)
76
    }
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
77
    elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
78
        $self->dbh->$dbh_method(@_);
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
79
    }
80
    else {
81
        croak qq/Can't locate object method "$mname" via "$package"/
82
    }
added helper method
yuki-kimoto authored on 2010-10-17
83
}
84

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
85
sub apply_filter {
many changed
Yuki Kimoto authored on 2011-01-23
86
    my ($self, $table, @cinfos) = @_;
87

            
88
    # Initialize filters
cleanup
Yuki Kimoto authored on 2011-01-12
89
    $self->{filter} ||= {};
many changed
Yuki Kimoto authored on 2011-01-23
90
    $self->{filter}{out} ||= {};
91
    $self->{filter}{in} ||= {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
92
    $self->{filter}{end} ||= {};
cleanup
Yuki Kimoto authored on 2010-12-22
93
    
cleanup
Yuki Kimoto authored on 2011-04-02
94
    # Usage
many changed
Yuki Kimoto authored on 2011-01-23
95
    my $usage = "Usage: \$dbi->apply_filter(" .
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
96
                "TABLE, COLUMN1, {in => INFILTER1, out => OUTFILTER1, end => ENDFILTER1}, " .
97
                "COLUMN2, {in => INFILTER2, out => OUTFILTER2, end => ENDFILTER2}, ...)";
cleanup
Yuki Kimoto authored on 2011-04-02
98
    
99
    # Apply filter
many changed
Yuki Kimoto authored on 2011-01-23
100
    for (my $i = 0; $i < @cinfos; $i += 2) {
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
101
        
many changed
Yuki Kimoto authored on 2011-01-23
102
        # Column
103
        my $column = $cinfos[$i];
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
104
        if (ref $column eq 'ARRAY') {
105
            foreach my $c (@$column) {
106
                push @cinfos, $c, $cinfos[$i + 1];
107
            }
108
            next;
109
        }
110
        
cleanup
Yuki Kimoto authored on 2011-04-02
111
        # Filter infomation
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
112
        my $finfo = $cinfos[$i + 1] || {};
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
113
        croak "$usage (table: $table)" unless  ref $finfo eq 'HASH';
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
114
        foreach my $ftype (keys %$finfo) {
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
115
            croak "$usage (table: $table 2)" unless $ftype eq 'in' || $ftype eq 'out'
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
116
                             || $ftype eq 'end'; 
many changed
Yuki Kimoto authored on 2011-01-23
117
        }
118
        
cleanup
Yuki Kimoto authored on 2011-04-02
119
        # Set filters
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
120
        foreach my $way (qw/in out end/) {
cleanup
Yuki Kimoto authored on 2011-04-02
121
        
122
            # Filter
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
123
            my $filter = $finfo->{$way};
cleanup
Yuki Kimoto authored on 2010-12-22
124
            
cleanup
Yuki Kimoto authored on 2011-04-02
125
            # Filter state
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
126
            my $state = !exists $finfo->{$way} ? 'not_exists'
127
                      : !defined $filter        ? 'not_defined'
128
                      : ref $filter eq 'CODE'   ? 'code'
129
                      : 'name';
130
            
cleanup
Yuki Kimoto authored on 2011-04-02
131
            # Filter is not exists
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
132
            next if $state eq 'not_exists';
133
            
cleanup
Yuki Kimoto authored on 2011-04-02
134
            # Check filter name
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
135
            croak qq{Filter "$filter" is not registered}
136
              if  $state eq 'name'
137
               && ! exists $self->filters->{$filter};
138
            
cleanup
Yuki Kimoto authored on 2011-04-02
139
            # Set filter
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
140
            my $f = $state eq 'not_defined' ? undef
141
                  : $state eq 'code'        ? $filter
142
                  : $self->filters->{$filter};
143
            $self->{filter}{$way}{$table}{$column} = $f;
144
            $self->{filter}{$way}{$table}{"$table.$column"} = $f;
145
            $self->{filter}{$way}{$table}{"${table}__$column"} = $f;
many changed
Yuki Kimoto authored on 2011-01-23
146
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
147
    }
148
    
many changed
Yuki Kimoto authored on 2011-01-23
149
    return $self;
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
150
}
151

            
cleanup
Yuki Kimoto authored on 2011-03-21
152
sub column {
153
    my ($self, $table, $columns) = @_;
added helper method
yuki-kimoto authored on 2010-10-17
154
    
cleanup
Yuki Kimoto authored on 2011-04-02
155
    # Reserved word quote
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
156
    my $q = $self->reserved_word_quote;
157
    
cleanup
Yuki Kimoto authored on 2011-04-02
158
    # Column clause
cleanup
Yuki Kimoto authored on 2011-03-21
159
    my @column;
cleanup
Yuki Kimoto authored on 2011-04-02
160
    $columns ||= [];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
161
    push @column, "$q$table$q.$q$_$q as $q${table}${q}__$q$_$q" for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
162
    
163
    return join (', ', @column);
added helper method
yuki-kimoto authored on 2010-10-17
164
}
165

            
packaging one directory
yuki-kimoto authored on 2009-11-16
166
sub connect {
cleanup
Yuki Kimoto authored on 2011-01-25
167
    my $self = ref $_[0] ? shift : shift->new(@_);;
removed register_format()
yuki-kimoto authored on 2010-05-26
168
    
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
169
    # Connect
170
    $self->dbh;
update document
yuki-kimoto authored on 2010-01-30
171
    
cleanup
Yuki Kimoto authored on 2011-04-02
172
    # Set process ID
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
173
    $self->pid($$);
174
    
packaging one directory
yuki-kimoto authored on 2009-11-16
175
    return $self;
176
}
177

            
cleanup
yuki-kimoto authored on 2010-10-17
178
sub create_query {
179
    my ($self, $source) = @_;
update document
yuki-kimoto authored on 2010-01-30
180
    
cleanup
yuki-kimoto authored on 2010-10-17
181
    # Cache
182
    my $cache = $self->cache;
update document
yuki-kimoto authored on 2010-01-30
183
    
cleanup
Yuki Kimoto authored on 2011-04-02
184
    # Query
cleanup
yuki-kimoto authored on 2010-10-17
185
    my $query;
cleanup
Yuki Kimoto authored on 2011-04-02
186
    
187
    # Get cached query
cleanup
yuki-kimoto authored on 2010-10-17
188
    if ($cache) {
189
        
190
        # Get query
191
        my $q = $self->cache_method->($self, $source);
192
        
193
        # Create query
add table tag
Yuki Kimoto authored on 2011-02-09
194
        if ($q) {
195
            $query = DBIx::Custom::Query->new($q);
196
            $query->filters($self->filters);
197
        }
cleanup
yuki-kimoto authored on 2010-10-17
198
    }
199
    
cleanup
Yuki Kimoto authored on 2011-04-02
200
    # Create query
cleanup
yuki-kimoto authored on 2010-10-17
201
    unless ($query) {
cleanup insert
yuki-kimoto authored on 2010-04-28
202

            
cleanup
yuki-kimoto authored on 2010-10-17
203
        # Create query
cleanup
Yuki Kimoto authored on 2011-04-02
204
        my $builder = $self->query_builder;
cleanup
yuki-kimoto authored on 2010-10-17
205
        $query = $builder->build_query($source);
removed register_format()
yuki-kimoto authored on 2010-05-26
206

            
cleanup
Yuki Kimoto authored on 2011-04-02
207
        # Remove reserved word quote
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
208
        if (my $q = $self->reserved_word_quote) {
cleanup
Yuki Kimoto authored on 2011-04-02
209
            $_ =~ s/$q//g for @{$query->columns}
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
210
        }
211

            
cleanup
Yuki Kimoto authored on 2011-04-02
212
        # Save query to cache
213
        $self->cache_method->(
214
            $self, $source,
215
            {
216
                sql     => $query->sql, 
217
                columns => $query->columns,
218
                tables  => $query->tables
219
            }
220
        ) if $cache;
cleanup insert
yuki-kimoto authored on 2010-04-28
221
    }
222
    
cleanup
yuki-kimoto authored on 2010-10-17
223
    # Prepare statement handle
224
    my $sth;
225
    eval { $sth = $self->dbh->prepare($query->{sql})};
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
226
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
packaging one directory
yuki-kimoto authored on 2009-11-16
227
    
cleanup
yuki-kimoto authored on 2010-10-17
228
    # Set statement handle
229
    $query->sth($sth);
packaging one directory
yuki-kimoto authored on 2009-11-16
230
    
cleanup
Yuki Kimoto authored on 2011-02-09
231
    # Set filters
232
    $query->filters($self->filters);
233
    
cleanup
yuki-kimoto authored on 2010-10-17
234
    return $query;
packaging one directory
yuki-kimoto authored on 2009-11-16
235
}
236

            
update pod
Yuki Kimoto authored on 2011-03-13
237
sub dbh {
238
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-04-02
239
    
fixed dbh() method bug:wq
Yuki Kimoto authored on 2011-04-05
240
    # Set
241
    if (@_) {
242
        $self->{dbh} = $_[0];
243
        
244
        return $self;
245
    }
246
    
247
    # Get
248
    else {
249
        # From Connction manager
250
        if (my $connector = $self->connector) {
251
            croak "connector must have dbh() method"
252
              unless ref $connector && $connector->can('dbh');
253
              
254
            return $self->{dbh} = $connector->dbh;
255
        }
256
        
257
        return $self->{dbh} ||= $self->_connect;
update pod
Yuki Kimoto authored on 2011-03-13
258
    }
259
}
260

            
cleanup
Yuki Kimoto authored on 2011-03-21
261
our %DELETE_ARGS
cleanup
Yuki Kimoto authored on 2011-03-21
262
  = map { $_ => 1 } @COMMON_ARGS, qw/where append allow_delete_all/;
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
263

            
cleanup
yuki-kimoto authored on 2010-10-17
264
sub delete {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
265
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
266

            
cleanup
Yuki Kimoto authored on 2011-04-02
267
    # Check arguments
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
268
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
269
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
270
          unless $DELETE_ARGS{$name};
cleanup update and update_al...
yuki-kimoto authored on 2010-04-28
271
    }
272
    
273
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
274
    my $table = $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
275
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
276
    my $where            = delete $args{where} || {};
277
    my $append           = delete $args{append};
278
    my $allow_delete_all = delete $args{allow_delete_all};
cleanup
Yuki Kimoto authored on 2011-04-02
279
    my $query_return     = delete $args{query};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
280

            
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
281
    # Where
cleanup
Yuki Kimoto authored on 2011-04-02
282
    $where = $self->_where_to_obj($where);
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
283
    
cleanup
Yuki Kimoto authored on 2011-04-02
284
    # Where clause
285
    my $where_clause = $where->to_string;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
286
    croak qq{"where" must be specified}
cleanup
Yuki Kimoto authored on 2011-04-02
287
      if $where_clause eq '' && !$allow_delete_all;
make delete() using where ob...
Yuki Kimoto authored on 2011-01-26
288

            
cleanup
Yuki Kimoto authored on 2011-04-02
289
    # Delete statement
cleanup
Yuki Kimoto authored on 2011-01-27
290
    my @sql;
cleanup
Yuki Kimoto authored on 2011-04-02
291
    my $q = $self->reserved_word_quote;
292
    push @sql, "delete from $q$table$q $where_clause";
cleanup
Yuki Kimoto authored on 2011-01-27
293
    push @sql, $append if $append;
294
    my $sql = join(' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
295
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
296
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
297
    my $query = $self->create_query($sql);
cleanup
Yuki Kimoto authored on 2011-04-02
298
    return $query if $query_return;
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
299
    
packaging one directory
yuki-kimoto authored on 2009-11-16
300
    # Execute query
cleanup
Yuki Kimoto authored on 2011-04-02
301
    return $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
302
        $query,
cleanup
Yuki Kimoto authored on 2011-04-02
303
        param => $where->param,
cleanup
Yuki Kimoto authored on 2011-03-21
304
        table => $table,
305
        %args
306
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
307
}
308

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
311
our %DELETE_AT_ARGS = (%DELETE_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
312

            
313
sub delete_at {
314
    my ($self, %args) = @_;
315
    
cleanup
Yuki Kimoto authored on 2011-04-02
316
    # Arguments
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
317
    my $primary_keys = delete $args{primary_key};
318
    $primary_keys = [$primary_keys] unless ref $primary_keys;
cleanup
Yuki Kimoto authored on 2011-04-02
319
    my $where = delete $args{where};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
320
    
cleanup
Yuki Kimoto authored on 2011-04-02
321
    # Check arguments
322
    foreach my $name (keys %args) {
323
        croak qq{Argument "$name" is wrong name}
324
          unless $DELETE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
325
    }
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
326
    
cleanup
Yuki Kimoto authored on 2011-04-02
327
    # Create where parameter
328
    my $where_param = $self->_create_where_param($where, $primary_keys);
329

            
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
330
    
cleanup
Yuki Kimoto authored on 2011-04-02
331
    return $self->delete(where => $where_param, %args);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
332
}
333

            
added helper method
yuki-kimoto authored on 2010-10-17
334
sub DESTROY { }
335

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
336
sub create_model {
337
    my $self = shift;
338
    
cleanup
Yuki Kimoto authored on 2011-04-02
339
    # Arguments
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
340
    my $args = ref $_[0] eq 'HASH' ? $_[0] : {@_};
341
    $args->{dbi} = $self;
342
    my $model_class = delete $args->{model_class} || 'DBIx::Custom::Model';
343
    my $model_name  = delete $args->{name};
344
    my $model_table = delete $args->{table};
345
    $model_name ||= $model_table;
346
    
cleanup
Yuki Kimoto authored on 2011-04-02
347
    # Create model
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
348
    my $model = $model_class->new($args);
349
    $model->name($model_name) unless $model->name;
350
    $model->table($model_table) unless $model->table;
351
    
352
    # Apply filter
353
    croak "$model_class filter must be array reference"
354
      unless ref $model->filter eq 'ARRAY';
355
    $self->apply_filter($model->table, @{$model->filter});
356
    
cleanup
Yuki Kimoto authored on 2011-04-02
357
    # Associate table with model
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
358
    croak "Table name is duplicated"
359
      if exists $self->{_model_from}->{$model->table};
360
    $self->{_model_from}->{$model->table} = $model->name;
361

            
362
    # Table alias
363
    $self->{_table_alias} ||= {};
364
    $self->{_table_alias} = {%{$self->{_table_alias}}, %{$model->table_alias}};
365
    
366
    # Set model
367
    $self->model($model->name, $model);
368
    
create_model() return model
Yuki Kimoto authored on 2011-03-29
369
    return $self->model($model->name);
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
370
}
371

            
372
sub each_column {
373
    my ($self, $cb) = @_;
374
    
375
    # Iterate all tables
376
    my $sth_tables = $self->dbh->table_info;
377
    while (my $table_info = $sth_tables->fetchrow_hashref) {
378
        
379
        # Table
380
        my $table = $table_info->{TABLE_NAME};
381
        
382
        # Iterate all columns
383
        my $sth_columns = $self->dbh->column_info(undef, undef, $table, '%');
384
        while (my $column_info = $sth_columns->fetchrow_hashref) {
385
            my $column = $column_info->{COLUMN_NAME};
386
            $self->$cb($table, $column, $column_info);
387
        }
388
    }
389
}
390

            
cleanup
Yuki Kimoto authored on 2011-04-02
391
our %EXECUTE_ARGS = map { $_ => 1 } @COMMON_ARGS, 'param';
392

            
393
sub execute {
cleanup
yuki-kimoto authored on 2010-10-17
394
    my ($self, $query, %args)  = @_;
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
395
    
cleanup
Yuki Kimoto authored on 2011-04-02
396
    # Arguments
cleanup
Yuki Kimoto authored on 2011-04-02
397
    my $param  = delete $args{param} || {};
cleanup
Yuki Kimoto authored on 2011-04-02
398
    my $tables = delete $args{table} || [];
399
    $tables = [$tables] unless ref $tables eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-04-02
400
    my $filter = delete $args{filter};
401
    $filter = DBIx::Custom::Util::array_to_hash($filter);
402
    my $type = delete $args{type};
403
    $type = DBIx::Custom::Util::array_to_hash($type);
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
404
    
cleanup
Yuki Kimoto authored on 2011-03-09
405
    # Check argument names
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
406
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
407
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
408
          unless $EXECUTE_ARGS{$name};
refactoring delete and delet...
yuki-kimoto authored on 2010-04-28
409
    }
410
    
cleanup
Yuki Kimoto authored on 2011-04-02
411
    # Create query
412
    $query = $self->create_query($query) unless ref $query;
cleanup
Yuki Kimoto authored on 2011-04-02
413
    $filter ||= $query->filter;
all filter can receive array...
Yuki Kimoto authored on 2011-02-25
414
    
cleanup
Yuki Kimoto authored on 2011-04-02
415
    # Tables
416
    unshift @$tables, @{$query->tables};
cleanup
Yuki Kimoto authored on 2011-03-09
417
    my $main_table = pop @$tables;
cleanup
Yuki Kimoto authored on 2011-04-02
418
    $tables = $self->_remove_duplicate_table($tables, $main_table);
419
    if (my $q = $self->reserved_word_quote) {
420
        $_ =~ s/$q//g for @$tables;
421
    }
cleanup
Yuki Kimoto authored on 2011-04-02
422
    
423
    # Table alias
cleanup
Yuki Kimoto authored on 2011-04-02
424
    foreach my $table (@$tables) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
425
        
cleanup
Yuki Kimoto authored on 2011-04-02
426
        # No need
427
        next unless my $alias = $self->{_table_alias}->{$table};
428
        $self->{filter} ||= {};
429
        next if $self->{filter}{out}{$table};
430
        
431
        # Filter
432
        $self->{filter}{out} ||= {};
433
        $self->{filter}{in}  ||= {};
434
        $self->{filter}{end} ||= {};
435
        
436
        # Create alias filter
437
        foreach my $type (qw/out in end/) {
438
            my @filter_names = keys %{$self->{filter}{$type}{$alias} || {}};
439
            foreach my $filter_name (@filter_names) {
440
                my $filter_name_alias = $filter_name;
441
                $filter_name_alias =~ s/^$alias\./$table\./;
442
                $filter_name_alias =~ s/^${alias}__/${table}__/; 
443
                $self->{filter}{$type}{$table}{$filter_name_alias}
444
                  = $self->{filter}{$type}{$alias}{$filter_name}
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
445
            }
446
        }
447
    }
cleanup
Yuki Kimoto authored on 2011-04-02
448
    
449
    # Applied filter
450
    my $applied_filter = {};
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
451
    foreach my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-04-02
452
        $applied_filter = {
453
            %$applied_filter,
cleanup
Yuki Kimoto authored on 2011-01-12
454
            %{$self->{filter}{out}->{$table} || {}}
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
455
        }
456
    }
cleanup
Yuki Kimoto authored on 2011-04-02
457
    $filter = {%$applied_filter, %$filter};
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
458
    
cleanup
Yuki Kimoto authored on 2011-04-02
459
    # Replace filter name to code
460
    foreach my $column (keys %$filter) {
461
        my $name = $filter->{$column};
462
        if (!defined $name) {
463
            $filter->{$column} = undef;
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
464
        }
cleanup
Yuki Kimoto authored on 2011-04-02
465
        elsif (ref $name ne 'CODE') {
466
          croak qq{Filter "$name" is not registered"}
467
            unless exists $self->filters->{$name};
468
          $filter->{$column} = $self->filters->{$name};
cleanup
Yuki Kimoto authored on 2010-12-21
469
        }
470
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
471
    
cleanup
Yuki Kimoto authored on 2011-04-02
472
    # Create bind values
473
    my $bind = $self->_create_bind_values(
474
        $param,
475
        $query->columns,
476
        $filter,
477
        $type
478
    );
cleanup
yuki-kimoto authored on 2010-10-17
479
    
480
    # Execute
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
481
    my $sth = $query->sth;
cleanup
yuki-kimoto authored on 2010-10-17
482
    my $affected;
cleanup
Yuki Kimoto authored on 2011-03-21
483
    eval {
484
        for (my $i = 0; $i < @$bind; $i++) {
cleanup
Yuki Kimoto authored on 2011-04-02
485
            my $type = $bind->[$i]->{type};
486
            $sth->bind_param($i + 1, $bind->[$i]->{value}, $type ? $type : ());
cleanup
Yuki Kimoto authored on 2011-03-21
487
        }
488
        $affected = $sth->execute;
489
    };
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
490
    $self->_croak($@, qq{. Following SQL is executed. "$query->{sql}"}) if $@;
cleanup
yuki-kimoto authored on 2010-10-17
491
    
added environment variable D...
Yuki Kimoto authored on 2011-04-02
492
    # Output SQL for debug
493
    warn $query->sql . "\n" if DEBUG;
494
    
cleanup
Yuki Kimoto authored on 2011-04-02
495
    # Select statement
cleanup
yuki-kimoto authored on 2010-10-17
496
    if ($sth->{NUM_OF_FIELDS}) {
497
        
cleanup
Yuki Kimoto authored on 2011-04-02
498
        # Filter
499
        my $filter = {};
500
        $filter->{in}  = {};
501
        $filter->{end} = {};
cleanup
Yuki Kimoto authored on 2011-01-12
502
        foreach my $table (@$tables) {
cleanup
Yuki Kimoto authored on 2011-04-02
503
            foreach my $way (qw/in end/) {
504
                $filter->{$way} = {
505
                    %{$filter->{$way}},
506
                    %{$self->{filter}{$way}{$table} || {}}
507
                };
508
            }
cleanup
Yuki Kimoto authored on 2011-01-12
509
        }
510
        
511
        # Result
512
        my $result = $self->result_class->new(
cleanup
Yuki Kimoto authored on 2010-12-22
513
            sth            => $sth,
514
            filters        => $self->filters,
515
            filter_check   => $self->filter_check,
cleanup
Yuki Kimoto authored on 2011-01-12
516
            default_filter => $self->{default_in_filter},
cleanup
Yuki Kimoto authored on 2011-04-02
517
            filter         => $filter->{in} || {},
518
            end_filter     => $filter->{end} || {}
cleanup
yuki-kimoto authored on 2010-10-17
519
        );
520

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
528
our %INSERT_ARGS = map { $_ => 1 } @COMMON_ARGS, qw/param append/;
update pod
Yuki Kimoto authored on 2011-03-13
529

            
cleanup
yuki-kimoto authored on 2010-10-17
530
sub insert {
531
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
532
    
cleanup
yuki-kimoto authored on 2010-10-17
533
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
534
    my $table  = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
535
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
536
    my $param  = delete $args{param} || {};
537
    my $append = delete $args{append} || '';
cleanup
Yuki Kimoto authored on 2011-04-02
538
    my $query_return  = delete $args{query};
539

            
540
    # Check arguments
541
    foreach my $name (keys %args) {
542
        croak qq{Argument "$name" is wrong name}
543
          unless $INSERT_ARGS{$name};
544
    }
545

            
546
    # Reserved word quote
547
    my $q = $self->reserved_word_quote;
cleanup
yuki-kimoto authored on 2010-10-17
548
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
549
    # Columns
550
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
551
    my $safety = $self->safety_character;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
552
    foreach my $column (keys %$param) {
553
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
554
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
555
          $column = "$q$column$q";
556
          $column =~ s/\./$q.$q/;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
557
        push @columns, $column;
558
    }
cleanup
yuki-kimoto authored on 2010-10-17
559
    
cleanup
Yuki Kimoto authored on 2011-04-02
560
    # Insert statement
cleanup
Yuki Kimoto authored on 2011-01-27
561
    my @sql;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
562
    push @sql, "insert into $q$table$q {insert_param ". join(' ', @columns) . '}';
cleanup
Yuki Kimoto authored on 2011-01-27
563
    push @sql, $append if $append;
564
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
565
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
566
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
567
    my $query = $self->create_query($sql);
cleanup
Yuki Kimoto authored on 2011-04-02
568
    return $query if $query_return;
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
569
    
packaging one directory
yuki-kimoto authored on 2009-11-16
570
    # Execute query
cleanup
Yuki Kimoto authored on 2011-04-02
571
    return $self->execute(
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
572
        $query,
cleanup
Yuki Kimoto authored on 2011-04-02
573
        param => $param,
cleanup
Yuki Kimoto authored on 2011-03-21
574
        table => $table,
575
        %args
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
576
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
577
}
578

            
cleanup
Yuki Kimoto authored on 2011-03-21
579
our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1);
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
580

            
581
sub insert_at {
582
    my ($self, %args) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
583

            
584
    # Arguments
585
    my $primary_keys = delete $args{primary_key};
586
    $primary_keys = [$primary_keys] unless ref $primary_keys;
587
    my $where = delete $args{where};
588
    my $param = delete $args{param};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
589
    
cleanup
Yuki Kimoto authored on 2011-04-02
590
    # Check arguments
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
591
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
592
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
593
          unless $INSERT_AT_ARGS{$name};
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
594
    }
595
    
cleanup
Yuki Kimoto authored on 2011-04-02
596
    # Create where parameter
597
    my $where_param = $self->_create_where_param($where, $primary_keys);
cleanup
Yuki Kimoto authored on 2011-04-02
598
    $param = $self->merge_param($where_param, $param);
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
599
    
600
    return $self->insert(param => $param, %args);
601
}
602

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
603
sub insert_param_tag {
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
604
    my ($self, $param) = @_;
605
    
cleanup
Yuki Kimoto authored on 2011-04-02
606
    # Create insert parameter tag
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
607
    my $safety = $self->safety_character;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
608
    my $q = $self->reserved_word_quote;
cleanup
Yuki Kimoto authored on 2011-04-02
609
    my @columns;
610
    my @placeholders;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
611
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
612
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
613
          unless $column =~ /^[$safety\.]+$/;
cleanup
Yuki Kimoto authored on 2011-04-02
614
        $column = "$q$column$q";
615
        $column =~ s/\./$q.$q/;
616
        push @columns, $column;
617
        push @placeholders, "{? $column}";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
618
    }
619
    
cleanup
Yuki Kimoto authored on 2011-04-02
620
    return '(' . join(', ', @columns) . ') ' . 'values ' .
621
           '(' . join(', ', @placeholders) . ')'
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
622
}
623

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
624
sub include_model {
625
    my ($self, $name_space, $model_infos) = @_;
626
    
cleanup
Yuki Kimoto authored on 2011-04-02
627
    # Name space
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
628
    $name_space ||= '';
cleanup
Yuki Kimoto authored on 2011-04-02
629
    
630
    # Get Model infomations
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
631
    unless ($model_infos) {
cleanup
Yuki Kimoto authored on 2011-04-02
632

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

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
708
sub method {
709
    my $self = shift;
710
    
cleanup
Yuki Kimoto authored on 2011-04-02
711
    # Register method
cleanup
Yuki Kimoto authored on 2011-03-21
712
    my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_};
713
    $self->{_methods} = {%{$self->{_methods} || {}}, %$methods};
714
    
715
    return $self;
716
}
717

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
735
sub mycolumn {
736
    my ($self, $table, $columns) = @_;
737
    
cleanup
Yuki Kimoto authored on 2011-04-02
738
    # Create column clause
739
    my @column;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
740
    my $q = $self->reserved_word_quote;
cleanup
Yuki Kimoto authored on 2011-03-21
741
    $columns ||= [];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
742
    push @column, "$q$table$q.$q$_$q as $q$_$q" for @$columns;
cleanup
Yuki Kimoto authored on 2011-03-21
743
    
744
    return join (', ', @column);
745
}
746

            
added dbi_options attribute
kimoto authored on 2010-12-20
747
sub new {
748
    my $self = shift->SUPER::new(@_);
749
    
cleanup
Yuki Kimoto authored on 2011-04-02
750
    # Check attributes
added dbi_options attribute
kimoto authored on 2010-12-20
751
    my @attrs = keys %$self;
752
    foreach my $attr (@attrs) {
cleanup
Yuki Kimoto authored on 2011-04-02
753
        croak qq{"$attr" is wrong name}
added dbi_options attribute
kimoto authored on 2010-12-20
754
          unless $self->can($attr);
755
    }
cleanup
Yuki Kimoto authored on 2011-04-02
756
    
757
    # Register tag
cleanup
Yuki Kimoto authored on 2011-01-25
758
    $self->register_tag(
759
        '?'     => \&DBIx::Custom::Tag::placeholder,
760
        '='     => \&DBIx::Custom::Tag::equal,
761
        '<>'    => \&DBIx::Custom::Tag::not_equal,
762
        '>'     => \&DBIx::Custom::Tag::greater_than,
763
        '<'     => \&DBIx::Custom::Tag::lower_than,
764
        '>='    => \&DBIx::Custom::Tag::greater_than_equal,
765
        '<='    => \&DBIx::Custom::Tag::lower_than_equal,
766
        'like'  => \&DBIx::Custom::Tag::like,
767
        'in'    => \&DBIx::Custom::Tag::in,
768
        'insert_param' => \&DBIx::Custom::Tag::insert_param,
769
        'update_param' => \&DBIx::Custom::Tag::update_param
770
    );
added dbi_options attribute
kimoto authored on 2010-12-20
771
    
772
    return $self;
773
}
774

            
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
775
sub not_exists { bless {}, 'DBIx::Custom::NotExists' }
776

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

            
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
787
sub register_tag { shift->query_builder->register_tag(@_) }
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
788

            
cleanup
Yuki Kimoto authored on 2011-03-21
789
our %SELECT_ARGS
cleanup
Yuki Kimoto authored on 2011-04-01
790
  = map { $_ => 1 } @COMMON_ARGS, qw/column where append relation join param/;
refactoring select
yuki-kimoto authored on 2010-04-28
791

            
packaging one directory
yuki-kimoto authored on 2009-11-16
792
sub select {
select, insert, update, upda...
yuki-kimoto authored on 2010-06-14
793
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
794

            
refactoring select
yuki-kimoto authored on 2010-04-28
795
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
796
    my $table = delete $args{table};
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
797
    my $tables = ref $table eq 'ARRAY' ? $table
798
               : defined $table ? [$table]
799
               : [];
cleanup
Yuki Kimoto authored on 2011-03-21
800
    my $columns   = delete $args{column};
801
    my $where     = delete $args{where} || {};
802
    my $append    = delete $args{append};
803
    my $join      = delete $args{join} || [];
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-03-08
804
    croak qq{"join" must be array reference}
805
      unless ref $join eq 'ARRAY';
cleanup
Yuki Kimoto authored on 2011-03-21
806
    my $relation = delete $args{relation};
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
807
    my $param = delete $args{param} || {};
cleanup
Yuki Kimoto authored on 2011-04-02
808
    my $query_return = $args{query};
809

            
810
    # Check arguments
811
    foreach my $name (keys %args) {
812
        croak qq{Argument "$name" is wrong name}
813
          unless $SELECT_ARGS{$name};
814
    }
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
815
    
cleanup
Yuki Kimoto authored on 2011-03-09
816
    # Add relation tables(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-03-21
817
    $self->_add_relation_table($tables, $relation);
packaging one directory
yuki-kimoto authored on 2009-11-16
818
    
cleanup
Yuki Kimoto authored on 2011-04-02
819
    # Select statement
cleanup
Yuki Kimoto authored on 2011-01-27
820
    my @sql;
821
    push @sql, 'select';
packaging one directory
yuki-kimoto authored on 2009-11-16
822
    
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
823
    # Column clause
cleanup
Yuki Kimoto authored on 2011-03-30
824
    if ($columns) {
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-15
825
        $columns = [$columns] if ! ref $columns;
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
826
        foreach my $column (@$columns) {
cleanup
Yuki Kimoto authored on 2011-04-02
827
            unshift @$tables, @{$self->_search_tables($column)};
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
828
            push @sql, ($column, ',');
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
829
        }
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
830
        pop @sql if $sql[-1] eq ',';
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
831
    }
832
    else { push @sql, '*' }
833
    
834
    # Table
cleanup
Yuki Kimoto authored on 2011-03-30
835
    push @sql, 'from';
cleanup
Yuki Kimoto authored on 2011-04-02
836
    my $q = $self->reserved_word_quote;
cleanup
Yuki Kimoto authored on 2011-03-30
837
    if ($relation) {
838
        my $found = {};
839
        foreach my $table (@$tables) {
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
840
            push @sql, ("$q$table$q", ',') unless $found->{$table};
cleanup
Yuki Kimoto authored on 2011-03-30
841
            $found->{$table} = 1;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2011-03-14
842
        }
packaging one directory
yuki-kimoto authored on 2009-11-16
843
    }
cleanup
Yuki Kimoto authored on 2011-03-30
844
    else {
845
        my $main_table = $tables->[-1] || '';
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
846
        push @sql, "$q$main_table$q";
cleanup
Yuki Kimoto authored on 2011-03-30
847
    }
848
    pop @sql if ($sql[-1] || '') eq ',';
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
849
    croak "Not found table name" unless $tables->[-1];
cleanup
Yuki Kimoto authored on 2011-04-01
850

            
cleanup
Yuki Kimoto authored on 2011-04-02
851
    # Add tables in parameter
852
    unshift @$tables, @{$self->_search_tables(join(' ', keys %$param) || '')};
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
853
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
854
    # Where
cleanup
Yuki Kimoto authored on 2011-04-02
855
    $where = $self->_where_to_obj($where);
856
    $param = keys %$param ? $self->merge_param($param, $where->param)
857
                          : $where->param;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
858
    
859
    # String where
cleanup
Yuki Kimoto authored on 2011-04-02
860
    my $where_clause = $where->to_string;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
861
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
862
    # Add table names in where clause
cleanup
Yuki Kimoto authored on 2011-04-02
863
    unshift @$tables, @{$self->_search_tables($where_clause)};
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
864
    
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
865
    # Push join
866
    $self->_push_join(\@sql, $join, $tables);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
867
    
cleanup
Yuki Kimoto authored on 2011-03-09
868
    # Add where clause
cleanup
Yuki Kimoto authored on 2011-04-02
869
    push @sql, $where_clause;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
870
    
cleanup
Yuki Kimoto authored on 2011-03-08
871
    # Relation(DEPRECATED!);
cleanup
Yuki Kimoto authored on 2011-04-02
872
    $self->_push_relation(\@sql, $tables, $relation, $where_clause eq '' ? 1 : 0);
cleanup
Yuki Kimoto authored on 2011-03-08
873
    
cleanup
Yuki Kimoto authored on 2011-04-02
874
    # Append
cleanup
Yuki Kimoto authored on 2011-01-27
875
    push @sql, $append if $append;
876
    
877
    # SQL
878
    my $sql = join (' ', @sql);
packaging one directory
yuki-kimoto authored on 2009-11-16
879
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
880
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
881
    my $query = $self->create_query($sql);
cleanup
Yuki Kimoto authored on 2011-04-02
882
    return $query if $query_return;
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
883
    
packaging one directory
yuki-kimoto authored on 2009-11-16
884
    # Execute query
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
885
    my $result = $self->execute(
cleanup
Yuki Kimoto authored on 2011-03-21
886
        $query,
cleanup
Yuki Kimoto authored on 2011-04-02
887
        param => $param, 
cleanup
Yuki Kimoto authored on 2011-03-21
888
        table => $tables,
889
        %args
890
    );
packaging one directory
yuki-kimoto authored on 2009-11-16
891
    
892
    return $result;
893
}
894

            
cleanup
Yuki Kimoto authored on 2011-03-21
895
our %SELECT_AT_ARGS = (%SELECT_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
896

            
897
sub select_at {
898
    my ($self, %args) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
899

            
900
    # Arguments
901
    my $primary_keys = delete $args{primary_key};
902
    $primary_keys = [$primary_keys] unless ref $primary_keys;
903
    my $where = delete $args{where};
904
    my $param = delete $args{param};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
905
    
cleanup
Yuki Kimoto authored on 2011-04-02
906
    # Check arguments
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
907
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
908
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
909
          unless $SELECT_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
910
    }
911
    
DBIx::Custom::Model select()...
Yuki Kimoto authored on 2011-02-22
912
    # Table
913
    croak qq{"table" option must be specified} unless $args{table};
914
    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
915
    
cleanup
Yuki Kimoto authored on 2011-04-02
916
    # Create where parameter
917
    my $where_param = $self->_create_where_param($where, $primary_keys);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
918
    
cleanup
Yuki Kimoto authored on 2011-04-02
919
    return $self->select(where => $where_param, %args);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
920
}
921

            
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
922
sub setup_model {
923
    my $self = shift;
924
    
cleanup
Yuki Kimoto authored on 2011-04-02
925
    # Setup model
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
926
    $self->each_column(
927
        sub {
928
            my ($self, $table, $column, $column_info) = @_;
929
            if (my $model = $self->models->{$table}) {
930
                push @{$model->columns}, $column;
931
            }
932
        }
933
    );
add experimental DBIx::Custo...
Yuki Kimoto authored on 2011-02-22
934
    return $self;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
935
}
936

            
cleanup
Yuki Kimoto authored on 2011-03-21
937
our %UPDATE_ARGS
938
  = map { $_ => 1 } @COMMON_ARGS, qw/param where append allow_update_all/;
cleanup
yuki-kimoto authored on 2010-10-17
939

            
940
sub update {
941
    my ($self, %args) = @_;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
942

            
cleanup
yuki-kimoto authored on 2010-10-17
943
    # Arguments
cleanup
Yuki Kimoto authored on 2011-03-21
944
    my $table = delete $args{table} || '';
added table not specified ex...
Yuki Kimoto authored on 2011-01-21
945
    croak qq{"table" option must be specified} unless $table;
cleanup
Yuki Kimoto authored on 2011-03-21
946
    my $param            = delete $args{param} || {};
947
    my $where            = delete $args{where} || {};
948
    my $append           = delete $args{append} || '';
949
    my $allow_update_all = delete $args{allow_update_all};
version 0.0901
yuki-kimoto authored on 2009-12-17
950
    
cleanup
Yuki Kimoto authored on 2011-04-02
951
    # Check argument names
952
    foreach my $name (keys %args) {
953
        croak qq{Argument "$name" is wrong name}
954
          unless $UPDATE_ARGS{$name};
955
    }
956
    
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
957
    # Columns
958
    my @columns;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
959
    my $safety = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-04-02
960
    my $q = $self->reserved_word_quote;
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
961
    foreach my $column (keys %$param) {
962
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
963
          unless $column =~ /^[$safety\.]+$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
964
          $column = "$q$column$q";
965
          $column =~ s/\./$q.$q/;
966
        push @columns, "$column";
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
967
    }
968
        
cleanup
yuki-kimoto authored on 2010-10-17
969
    # Update clause
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
970
    my $update_clause = '{update_param ' . join(' ', @columns) . '}';
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
971

            
972
    # Where
cleanup
Yuki Kimoto authored on 2011-04-02
973
    $where = $self->_where_to_obj($where);
974
    my $where_clause = $where->to_string;
improved delete() and update...
Yuki Kimoto authored on 2011-01-26
975
    croak qq{"where" must be specified}
cleanup
Yuki Kimoto authored on 2011-04-02
976
      if "$where_clause" eq '' && !$allow_update_all;
removed reconnect method
yuki-kimoto authored on 2010-05-28
977
    
cleanup
Yuki Kimoto authored on 2011-04-02
978
    # Update statement
cleanup
Yuki Kimoto authored on 2011-01-27
979
    my @sql;
cleanup
Yuki Kimoto authored on 2011-04-02
980
    push @sql, "update $q$table$q $update_clause $where_clause";
cleanup
Yuki Kimoto authored on 2011-01-27
981
    push @sql, $append if $append;
removed reconnect method
yuki-kimoto authored on 2010-05-28
982
    
cleanup
Yuki Kimoto authored on 2011-04-02
983
    # Merge parameters
984
    $param = $self->merge_param($param, $where->param);
cleanup
yuki-kimoto authored on 2010-10-17
985
    
cleanup
Yuki Kimoto authored on 2011-01-27
986
    # SQL
987
    my $sql = join(' ', @sql);
988
    
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
989
    # Create query
cleanup
Yuki Kimoto authored on 2011-01-27
990
    my $query = $self->create_query($sql);
added experimental sugar met...
Yuki Kimoto authored on 2011-01-17
991
    return $query if $args{query};
992
    
cleanup
yuki-kimoto authored on 2010-10-17
993
    # Execute query
cleanup
Yuki Kimoto authored on 2011-03-21
994
    my $ret_val = $self->execute(
995
        $query,
996
        param  => $param, 
997
        table => $table,
998
        %args
999
    );
cleanup
yuki-kimoto authored on 2010-10-17
1000
    
1001
    return $ret_val;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1002
}
1003

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

            
cleanup
Yuki Kimoto authored on 2011-03-21
1006
our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1007

            
1008
sub update_at {
1009
    my ($self, %args) = @_;
1010
    
cleanup
Yuki Kimoto authored on 2011-04-02
1011
    # Arguments
1012
    my $primary_keys = delete $args{primary_key};
1013
    $primary_keys = [$primary_keys] unless ref $primary_keys;
1014
    my $where = delete $args{where};
1015
    
1016

            
1017
    # Check arguments
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1018
    foreach my $name (keys %args) {
cleanup
Yuki Kimoto authored on 2011-04-02
1019
        croak qq{Argument "$name" is wrong name}
cleanup
Yuki Kimoto authored on 2011-03-21
1020
          unless $UPDATE_AT_ARGS{$name};
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1021
    }
1022
    
cleanup
Yuki Kimoto authored on 2011-04-02
1023
    # Create where parameter
1024
    my $where_param = $self->_create_where_param($where, $primary_keys);
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1025
    
cleanup
Yuki Kimoto authored on 2011-04-02
1026
    return $self->update(where => $where_param, %args);
1027
}
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1028

            
cleanup
Yuki Kimoto authored on 2011-04-02
1029
sub _create_where_param {
1030
    my ($self, $where, $primary_keys) = @_;
- added experimental DBIx::C...
Yuki Kimoto authored on 2011-02-28
1031
    
cleanup
Yuki Kimoto authored on 2011-04-02
1032
    # Create where parameter
1033
    my $where_param = {};
1034
    if ($where) {
1035
        $where = [$where] unless ref $where;
1036
        croak qq{"where" must be constant value or array reference}
1037
          unless !ref $where || ref $where eq 'ARRAY';
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1038
        for(my $i = 0; $i < @$primary_keys; $i ++) {
cleanup
Yuki Kimoto authored on 2011-04-02
1039
           $where_param->{$primary_keys->[$i]} = $where->[$i];
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1040
        }
1041
    }
1042
    
cleanup
Yuki Kimoto authored on 2011-04-02
1043
    return $where_param;
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1044
}
1045

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1046
sub update_param_tag {
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1047
    my ($self, $param, $opt) = @_;
1048
    
cleanup
Yuki Kimoto authored on 2011-04-02
1049
    # Create update parameter tag
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1050
    my @params;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1051
    my $safety = $self->safety_character;
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
1052
    my $q = $self->reserved_word_quote;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1053
    foreach my $column (keys %$param) {
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
1054
        croak qq{"$column" is not safety column name}
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1055
          unless $column =~ /^[$safety\.]+$/;
cleanup
Yuki Kimoto authored on 2011-04-02
1056
        my $column = "$q$column$q";
1057
        $column =~ s/\./$q.$q/;
1058
        push @params, "$column = {? $column}";
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1059
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1060
    my $tag;
1061
    $tag .= 'set ' unless $opt->{no_set};
1062
    $tag .= join(', ', @params);
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1063
    
cleanup
Yuki Kimoto authored on 2011-04-02
1064
    return $tag;
remove experimental DBIx::Cu...
Yuki Kimoto authored on 2011-03-08
1065
}
1066

            
cleanup
Yuki Kimoto authored on 2011-01-25
1067
sub where {
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1068
    my $self = shift;
cleanup
Yuki Kimoto authored on 2011-04-02
1069
    
1070
    # Create where
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1071
    return DBIx::Custom::Where->new(
1072
        query_builder => $self->query_builder,
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1073
        safety_character => $self->safety_character,
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1074
        reserved_word_quote => $self->reserved_word_quote,
cleanup
Yuki Kimoto authored on 2011-03-09
1075
        @_
select() where can't receive...
Yuki Kimoto authored on 2011-01-27
1076
    );
cleanup
Yuki Kimoto authored on 2011-01-25
1077
}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-18
1078

            
cleanup
Yuki Kimoto authored on 2011-04-02
1079
sub _create_bind_values {
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1080
    my ($self, $params, $columns, $filter, $type) = @_;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1081
    
cleanup
Yuki Kimoto authored on 2011-04-02
1082
    # Create bind values
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1083
    my $bind = [];
removed reconnect method
yuki-kimoto authored on 2010-05-28
1084
    my $count = {};
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1085
    my $not_exists = {};
cleanup
Yuki Kimoto authored on 2011-01-12
1086
    foreach my $column (@$columns) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
1087
        
1088
        # Value
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
1089
        my $value;
1090
        if(ref $params->{$column} eq 'ARRAY') {
1091
            my $i = $count->{$column} || 0;
1092
            $i += $not_exists->{$column} || 0;
1093
            my $found;
1094
            for (my $k = $i; $i < @{$params->{$column}}; $k++) {
1095
                if (ref $params->{$column}->[$k] eq 'DBIx::Custom::NotExists') {
1096
                    $not_exists->{$column}++;
1097
                }
1098
                else  {
1099
                    $value = $params->{$column}->[$k];
1100
                    $found = 1;
1101
                    last
1102
                }
1103
            }
1104
            next unless $found;
1105
        }
1106
        else { $value = $params->{$column} }
removed reconnect method
yuki-kimoto authored on 2010-05-28
1107
        
cleanup
Yuki Kimoto authored on 2011-01-12
1108
        # Filter
1109
        my $f = $filter->{$column} || $self->{default_out_filter} || '';
cleanup
kimoto.yuki@gmail.com authored on 2010-12-21
1110
        
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1111
        # Type
1112
        push @$bind, {
1113
            value => $f ? $f->($value) : $value,
1114
            type => $type->{$column}
1115
        };
removed reconnect method
yuki-kimoto authored on 2010-05-28
1116
        
1117
        # Count up 
1118
        $count->{$column}++;
1119
    }
1120
    
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
1121
    return $bind;
removed reconnect method
yuki-kimoto authored on 2010-05-28
1122
}
1123

            
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1124
sub _connect {
1125
    my $self = shift;
1126
    
1127
    # Attributes
1128
    my $data_source = $self->data_source;
1129
    croak qq{"data_source" must be specified to connect()"}
1130
      unless $data_source;
1131
    my $user        = $self->user;
1132
    my $password    = $self->password;
1133
    my $dbi_option = {%{$self->dbi_options}, %{$self->dbi_option}};
1134
    
1135
    # Connect
1136
    my $dbh = eval {DBI->connect(
1137
        $data_source,
1138
        $user,
1139
        $password,
1140
        {
1141
            %{$self->default_dbi_option},
1142
            %$dbi_option
1143
        }
1144
    )};
1145
    
1146
    # Connect error
1147
    croak $@ if $@;
1148
    
1149
    return $dbh;
1150
}
1151

            
cleanup
yuki-kimoto authored on 2010-10-17
1152
sub _croak {
1153
    my ($self, $error, $append) = @_;
cleanup
Yuki Kimoto authored on 2011-04-02
1154
    
1155
    # Append
cleanup
yuki-kimoto authored on 2010-10-17
1156
    $append ||= "";
1157
    
1158
    # Verbose
1159
    if ($Carp::Verbose) { croak $error }
1160
    
1161
    # Not verbose
1162
    else {
1163
        
1164
        # Remove line and module infromation
1165
        my $at_pos = rindex($error, ' at ');
1166
        $error = substr($error, 0, $at_pos);
1167
        $error =~ s/\s+$//;
1168
        croak "$error$append";
1169
    }
1170
}
1171

            
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1172
sub _need_tables {
1173
    my ($self, $tree, $need_tables, $tables) = @_;
1174
    
cleanup
Yuki Kimoto authored on 2011-04-02
1175
    # Get needed tables
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
1176
    foreach my $table (@$tables) {
1177
        if ($tree->{$table}) {
1178
            $need_tables->{$table} = 1;
1179
            $self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}])
1180
        }
1181
    }
1182
}
1183

            
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1184
sub _push_join {
1185
    my ($self, $sql, $join, $join_tables) = @_;
1186
    
cleanup
Yuki Kimoto authored on 2011-04-02
1187
    # No join
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1188
    return unless @$join;
1189
    
cleanup
Yuki Kimoto authored on 2011-04-02
1190
    # Push join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1191
    my $tree = {};
cleanup
Yuki Kimoto authored on 2011-04-02
1192
    my $q = $self->reserved_word_quote;
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1193
    for (my $i = 0; $i < @$join; $i++) {
1194
        
cleanup
Yuki Kimoto authored on 2011-04-02
1195
        # Search table in join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1196
        my $join_clause = $join->[$i];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1197
        my $q_re = quotemeta($q);
cleanup
Yuki Kimoto authored on 2011-04-01
1198
        my $join_re = $q ? qr/\s$q_re?([^\.\s$q_re]+?)$q_re?\..+?\s$q_re?([^\.\s$q_re]+?)$q_re?\..+?$/
1199
                         : qr/\s([^\.\s]+?)\..+?\s([^\.\s]+?)\..+?$/;
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1200
        if ($join_clause =~ $join_re) {
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1201
            my $table1 = $1;
1202
            my $table2 = $2;
1203
            croak qq{right side table of "$join_clause" must be uniq}
1204
              if exists $tree->{$table2};
1205
            $tree->{$table2}
1206
              = {position => $i, parent => $table1, join => $join_clause};
1207
        }
1208
        else {
1209
            croak qq{join "$join_clause" must be two table name};
1210
        }
1211
    }
1212
    
cleanup
Yuki Kimoto authored on 2011-04-02
1213
    # Search need tables
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1214
    my $need_tables = {};
1215
    $self->_need_tables($tree, $need_tables, $join_tables);
1216
    my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } keys %$need_tables;
cleanup
Yuki Kimoto authored on 2011-04-02
1217
    
1218
    # Add join clause
fixed some select() join opi...
Yuki Kimoto authored on 2011-03-09
1219
    foreach my $need_table (@need_tables) {
1220
        push @$sql, $tree->{$need_table}{join};
1221
    }
1222
}
cleanup
Yuki Kimoto authored on 2011-03-08
1223

            
cleanup
Yuki Kimoto authored on 2011-04-02
1224
sub _remove_duplicate_table {
1225
    my ($self, $tables, $main_table) = @_;
1226
    
1227
    # Remove duplicate table
1228
    my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables;
1229
    delete $tables{$main_table} if $main_table;
1230
    
1231
    return [keys %tables, $main_table ? $main_table : ()];
1232
}
1233

            
cleanup
Yuki Kimoto authored on 2011-04-02
1234
sub _search_tables {
cleanup
Yuki Kimoto authored on 2011-04-02
1235
    my ($self, $source) = @_;
1236
    
cleanup
Yuki Kimoto authored on 2011-04-02
1237
    # Search tables
cleanup
Yuki Kimoto authored on 2011-04-02
1238
    my $tables = [];
1239
    my $safety_character = $self->safety_character;
1240
    my $q = $self->reserved_word_quote;
1241
    my $q_re = quotemeta($q);
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1242
    my $table_re = $q ? qr/(?:^|[^$safety_character])$q_re?([$safety_character]+)$q_re?\./
1243
                      : qr/(?:^|[^$safety_character])([$safety_character]+)\./;
cleanup
Yuki Kimoto authored on 2011-04-02
1244
    while ($source =~ /$table_re/g) {
1245
        push @$tables, $1;
1246
    }
1247
    
1248
    return $tables;
1249
}
1250

            
cleanup
Yuki Kimoto authored on 2011-04-02
1251
sub _where_to_obj {
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1252
    my ($self, $where) = @_;
1253
    
cleanup
Yuki Kimoto authored on 2011-04-02
1254
    my $obj;
1255
    
1256
    # Hash
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1257
    if (ref $where eq 'HASH') {
1258
        my $clause = ['and'];
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1259
        my $q = $self->reserved_word_quote;
1260
        foreach my $column (keys %$where) {
1261
            $column = "$q$column$q";
1262
            $column =~ s/\./$q.$q/;
1263
            push @$clause, "{= $column}" for keys %$where;
1264
        }
cleanup
Yuki Kimoto authored on 2011-04-02
1265
        $obj = $self->where(clause => $clause, param => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1266
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1267
    
1268
    # DBIx::Custom::Where object
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1269
    elsif (ref $where eq 'DBIx::Custom::Where') {
cleanup
Yuki Kimoto authored on 2011-04-02
1270
        $obj = $where;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1271
    }
cleanup
Yuki Kimoto authored on 2011-04-02
1272
    
1273
    # Array(DEPRECATED!)
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1274
    elsif (ref $where eq 'ARRAY') {
removed EXPERIMETNAL select(...
Yuki Kimoto authored on 2011-04-01
1275
        warn "\$dbi->select(where => [CLAUSE, PARAMETER]) is DEPRECATED." .
1276
             "use \$dbi->select(where => \$dbi->where(clause => " .
1277
             "CLAUSE, param => PARAMETER));";
cleanup
Yuki Kimoto authored on 2011-04-02
1278
        $obj = $self->where(
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1279
            clause => $where->[0],
1280
            param  => $where->[1]
1281
        );
1282
    }
1283
    
cleanup
Yuki Kimoto authored on 2011-04-02
1284
    # Check where argument
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1285
    croak qq{"where" must be hash reference or DBIx::Custom::Where object} .
1286
          qq{or array reference, which contains where clause and paramter}
cleanup
Yuki Kimoto authored on 2011-04-02
1287
      unless ref $obj eq 'DBIx::Custom::Where';
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1288
    
cleanup
Yuki Kimoto authored on 2011-04-02
1289
    return $obj;
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1290
}
1291

            
cleanup
Yuki Kimoto authored on 2011-01-25
1292
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-23
1293
__PACKAGE__->attr(
1294
    dbi_options => sub { {} },
1295
    filter_check  => 1
1296
);
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1297

            
cleanup
Yuki Kimoto authored on 2011-01-25
1298
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1299
sub default_bind_filter {
1300
    my $self = shift;
1301
    
1302
    if (@_) {
1303
        my $fname = $_[0];
1304
        
1305
        if (@_ && !$fname) {
1306
            $self->{default_out_filter} = undef;
1307
        }
1308
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1309
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1310
              unless exists $self->filters->{$fname};
1311
        
1312
            $self->{default_out_filter} = $self->filters->{$fname};
1313
        }
1314
        return $self;
1315
    }
1316
    
1317
    return $self->{default_out_filter};
1318
}
1319

            
cleanup
Yuki Kimoto authored on 2011-01-25
1320
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
1321
sub default_fetch_filter {
1322
    my $self = shift;
1323
    
1324
    if (@_) {
many changed
Yuki Kimoto authored on 2011-01-23
1325
        my $fname = $_[0];
1326

            
cleanup
Yuki Kimoto authored on 2011-01-12
1327
        if (@_ && !$fname) {
1328
            $self->{default_in_filter} = undef;
1329
        }
1330
        else {
many changed
Yuki Kimoto authored on 2011-01-23
1331
            croak qq{Filter "$fname" is not registered}
cleanup
Yuki Kimoto authored on 2011-01-12
1332
              unless exists $self->filters->{$fname};
1333
        
1334
            $self->{default_in_filter} = $self->filters->{$fname};
1335
        }
1336
        
1337
        return $self;
1338
    }
1339
    
many changed
Yuki Kimoto authored on 2011-01-23
1340
    return $self->{default_in_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
1341
}
1342

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1343
# DEPRECATED!
1344
sub insert_param {
1345
    warn "insert_param is renamed to insert_param_tag."
1346
       . " insert_param is DEPRECATED!";
1347
    return shift->insert_param_tag(@_);
1348
}
1349

            
cleanup
Yuki Kimoto authored on 2011-01-25
1350
# DEPRECATED!
renamed DBIx::Custom::TagPro...
Yuki Kimoto authored on 2011-01-24
1351
sub register_tag_processor {
1352
    return shift->query_builder->register_tag_processor(@_);
1353
}
1354

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
1355
# DEPRECATED!
1356
sub update_param {
1357
    warn "update_param is renamed to update_param_tag."
1358
       . " update_param is DEPRECATED!";
1359
    return shift->update_param_tag(@_);
1360
}
cleanup
Yuki Kimoto authored on 2011-03-08
1361
# DEPRECATED!
1362
sub _push_relation {
1363
    my ($self, $sql, $tables, $relation, $need_where) = @_;
1364
    
1365
    if (keys %{$relation || {}}) {
1366
        push @$sql, $need_where ? 'where' : 'and';
1367
        foreach my $rcolumn (keys %$relation) {
1368
            my $table1 = (split (/\./, $rcolumn))[0];
1369
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1370
            push @$tables, ($table1, $table2);
1371
            push @$sql, ("$rcolumn = " . $relation->{$rcolumn},  'and');
1372
        }
1373
    }
1374
    pop @$sql if $sql->[-1] eq 'and';    
1375
}
1376

            
1377
# DEPRECATED!
1378
sub _add_relation_table {
cleanup
Yuki Kimoto authored on 2011-03-09
1379
    my ($self, $tables, $relation) = @_;
cleanup
Yuki Kimoto authored on 2011-03-08
1380
    
1381
    if (keys %{$relation || {}}) {
1382
        foreach my $rcolumn (keys %$relation) {
1383
            my $table1 = (split (/\./, $rcolumn))[0];
1384
            my $table2 = (split (/\./, $relation->{$rcolumn}))[0];
1385
            my $table1_exists;
1386
            my $table2_exists;
1387
            foreach my $table (@$tables) {
1388
                $table1_exists = 1 if $table eq $table1;
1389
                $table2_exists = 1 if $table eq $table2;
1390
            }
1391
            unshift @$tables, $table1 unless $table1_exists;
1392
            unshift @$tables, $table2 unless $table2_exists;
1393
        }
1394
    }
1395
}
1396

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

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1399
=head1 NAME
1400

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1401
DBIx::Custom - Useful database access, respecting SQL!
removed reconnect method
yuki-kimoto authored on 2010-05-28
1402

            
1403
=head1 SYNOPSYS
cleanup
yuki-kimoto authored on 2010-08-05
1404

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1405
    use DBIx::Custom;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1406
    
1407
    # Connect
1408
    my $dbi = DBIx::Custom->connect(
1409
        data_source => "dbi:mysql:database=dbname",
1410
        user => 'ken',
1411
        password => '!LFKD%$&',
1412
        dbi_option => {mysql_enable_utf8 => 1}
1413
    );
cleanup
yuki-kimoto authored on 2010-08-05
1414

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1415
    # Insert 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1416
    $dbi->insert(
1417
        table  => 'book',
1418
        param  => {title => 'Perl', author => 'Ken'}
1419
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1420
    
1421
    # Update 
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1422
    $dbi->update(
1423
        table  => 'book', 
1424
        param  => {title => 'Perl', author => 'Ken'}, 
1425
        where  => {id => 5},
1426
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
1427
    
1428
    # Delete
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1429
    $dbi->delete(
1430
        table  => 'book',
1431
        where  => {author => 'Ken'},
1432
    );
cleanup
yuki-kimoto authored on 2010-08-05
1433

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
1434
    # Select
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
1435
    my $result = $dbi->select(
added insert, update, update...
Yuki Kimoto authored on 2011-01-04
1436
        table  => 'book',
update document
yuki-kimoto authored on 2010-05-27
1437
        where  => {author => 'Ken'},
added commit method
yuki-kimoto authored on 2010-05-27
1438
    );
cleanup
yuki-kimoto authored on 2010-08-05
1439

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1440
    # Select, more complex
1441
    my $result = $dbi->select(
1442
        table  => 'book',
1443
        column => [
1444
            'book.author as book__author',
1445
            'company.name as company__name'
1446
        ],
1447
        where  => {'book.author' => 'Ken'},
1448
        join => ['left outer join company on book.company_id = company.id'],
1449
        append => 'order by id limit 5'
removed reconnect method
yuki-kimoto authored on 2010-05-28
1450
    );
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1451
    
removed register_format()
yuki-kimoto authored on 2010-05-26
1452
    # Fetch
1453
    while (my $row = $result->fetch) {
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1454
        
removed register_format()
yuki-kimoto authored on 2010-05-26
1455
    }
1456
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1457
    # Fetch as hash
removed register_format()
yuki-kimoto authored on 2010-05-26
1458
    while (my $row = $result->fetch_hash) {
1459
        
1460
    }
1461
    
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1462
    # Execute SQL with parameter.
1463
    $dbi->execute(
1464
        "select id from book where {= author} and {like title}",
1465
        param  => {author => 'ken', title => '%Perl%'}
1466
    );
1467
    
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1468
=head1 DESCRIPTIONS
removed reconnect method
yuki-kimoto authored on 2010-05-28
1469

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1470
L<DBIx::Custom> is L<DBI> wrapper module.
1471

            
1472
=head1 FEATURES
removed reconnect method
yuki-kimoto authored on 2010-05-28
1473

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1474
=over 4
removed reconnect method
yuki-kimoto authored on 2010-05-28
1475

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1476
=item *
removed reconnect method
yuki-kimoto authored on 2010-05-28
1477

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1478
There are many basic methods to execute various queries.
1479
C<insert()>, C<update()>, C<update_all()>,C<delete()>,
1480
C<delete_all()>, C<select()>,
1481
C<insert_at()>, C<update_at()>, 
1482
C<delete_at()>, C<select_at()>, C<execute()>
removed reconnect method
yuki-kimoto authored on 2010-05-28
1483

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1484
=item *
1485

            
1486
Filter when data is send or receive.
1487

            
1488
=item *
1489

            
1490
Data filtering system
1491

            
1492
=item *
1493

            
1494
Model support.
1495

            
1496
=item *
1497

            
1498
Generate where clause dinamically.
1499

            
1500
=item *
1501

            
1502
Generate join clause dinamically.
1503

            
1504
=back
pod fix
Yuki Kimoto authored on 2011-01-21
1505

            
1506
=head1 GUIDE
1507

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1508
L<DBIx::Custom::Guide> - L<DBIx::Custom> Guide
pod fix
Yuki Kimoto authored on 2011-01-21
1509

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1510
=head1 Wiki
pod fix
Yuki Kimoto authored on 2011-01-21
1511

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1512
L<DBIx::Custom Wiki|https://github.com/yuki-kimoto/DBIx-Custom/wiki>
updated document
yuki-kimoto authored on 2010-08-08
1513

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

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

            
1518
    my $connector = $dbi->connector;
1519
    $dbi          = $dbi->connector(DBIx::Connector->new(...));
1520

            
1521
Connection manager object. if connector is set, you can get C<dbh()>
1522
from connection manager. conection manager object must have dbh() mehtod.
1523

            
1524
This is L<DBIx::Connector> example. Please pass
1525
C<default_dbi_option> to L<DBIx::Connector>.
1526

            
1527
    my $connector = DBIx::Connector->new(
1528
        "dbi:mysql:database=$DATABASE",
1529
        $USER,
1530
        $PASSWORD,
1531
        DBIx::Custom->new->default_dbi_option
1532
    );
1533
    
1534
    my $dbi = DBIx::Custom->new(connector => $connector);
1535

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1536
=head2 C<data_source>
packaging one directory
yuki-kimoto authored on 2009-11-16
1537

            
cleanup
yuki-kimoto authored on 2010-08-03
1538
    my $data_source = $dbi->data_source;
cleanup
yuki-kimoto authored on 2010-08-05
1539
    $dbi            = $dbi->data_source("DBI:mysql:database=dbname");
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1540

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1541
Data source, used when C<connect()> is executed.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
1542

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1543
=head2 C<dbi_option>
added dbi_options attribute
kimoto authored on 2010-12-20
1544

            
renamed dbi_options to dbi_o...
Yuki Kimoto authored on 2011-01-23
1545
    my $dbi_option = $dbi->dbi_option;
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1546
    $dbi           = $dbi->dbi_option($dbi_option);
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1547

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1548
L<DBI> option, used when C<connect()> is executed.
1549
Each value in option override the value of C<default_dbi_option>.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1550

            
1551
=head2 C<default_dbi_option>
1552

            
1553
    my $default_dbi_option = $dbi->default_dbi_option;
1554
    $dbi            = $dbi->default_dbi_option($default_dbi_option);
1555

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1556
L<DBI> default option, used when C<connect()> is executed,
1557
default to the following values.
add default_dbi_option()
Yuki Kimoto authored on 2011-02-19
1558

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1559
    {
1560
        RaiseError => 1,
1561
        PrintError => 0,
1562
        AutoCommit => 1,
1563
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
1564

            
update pod
Yuki Kimoto authored on 2011-03-13
1565
You should not change C<AutoCommit> value directly,
1566
the value is used to check if the process is in transaction.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1567

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1570
    my $filters = $dbi->filters;
1571
    $dbi        = $dbi->filters(\%filters);
packaging one directory
yuki-kimoto authored on 2009-11-16
1572

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1573
Filters, registered by C<register_filter()>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
1574

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

            
1577
    my $models = $dbi->models;
1578
    $dbi       = $dbi->models(\%models);
1579

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1580
Models, included by C<include_model()>.
add models() attribute
Yuki Kimoto authored on 2011-02-21
1581

            
cleanup
yuki-kimoto authored on 2010-10-17
1582
=head2 C<password>
1583

            
1584
    my $password = $dbi->password;
1585
    $dbi         = $dbi->password('lkj&le`@s');
1586

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1587
Password, used when C<connect()> is executed.
update document
yuki-kimoto authored on 2010-01-30
1588

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

            
renamed update tag to update...
yuki-kimoto authored on 2010-08-09
1591
    my $sql_class = $dbi->query_builder;
1592
    $dbi          = $dbi->query_builder(DBIx::Custom::QueryBuilder->new);
added commit method
yuki-kimoto authored on 2010-05-27
1593

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1594
Query builder, default to L<DBIx::Custom::QueryBuilder> object.
cleanup
yuki-kimoto authored on 2010-08-05
1595

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
1596
=head2 C<reserved_word_quote>
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1597

            
1598
     my reserved_word_quote = $dbi->reserved_word_quote;
1599
     $dbi                   = $dbi->reserved_word_quote('"');
1600

            
cleanup
Yuki Kimoto authored on 2011-04-02
1601
Reserved word quote, default to empty string.
added EXPERIMENTAL reserved_...
Yuki Kimoto authored on 2011-03-30
1602

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1605
    my $result_class = $dbi->result_class;
1606
    $dbi             = $dbi->result_class('DBIx::Custom::Result');
cleanup
yuki-kimoto authored on 2010-08-05
1607

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

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

            
- remaned experimental safty...
Yuki Kimoto authored on 2011-03-10
1612
    my $safety_character = $self->safety_character;
cleanup
Yuki Kimoto authored on 2011-03-10
1613
    $dbi                 = $self->safety_character($character);
update pod
Yuki Kimoto authored on 2011-01-27
1614

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
1620
    my $user = $dbi->user;
1621
    $dbi     = $dbi->user('Ken');
cleanup
yuki-kimoto authored on 2010-08-05
1622

            
cleanup
Yuki Kimoto authored on 2011-03-10
1623
User name, used when C<connect()> is executed.
update pod
Yuki Kimoto authored on 2011-01-27
1624

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

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

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
1631
=head2 C<apply_filter>
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1632

            
renamed auto_filter to apply...
Yuki Kimoto authored on 2011-01-12
1633
    $dbi->apply_filter(
cleanup
Yuki Kimoto authored on 2011-03-10
1634
        'book',
update pod
Yuki Kimoto authored on 2011-03-13
1635
        'issue_date' => {
1636
            out => 'tp_to_date',
1637
            in  => 'date_to_tp',
1638
            end => 'tp_to_displaydate'
1639
        },
1640
        'write_date' => {
1641
            out => 'tp_to_date',
1642
            in  => 'date_to_tp',
1643
            end => 'tp_to_displaydate'
1644
        }
added auto_filter method
kimoto.yuki@gmail.com authored on 2010-12-21
1645
    );
1646

            
update pod
Yuki Kimoto authored on 2011-03-13
1647
Apply filter to columns.
1648
C<out> filter is executed before data is send to database.
1649
C<in> filter is executed after a row is fetch.
1650
C<end> filter is execute after C<in> filter is executed.
1651

            
1652
Filter is applied to the follwoing tree column name pattern.
cleanup
Yuki Kimoto authored on 2010-12-21
1653

            
update pod
Yuki Kimoto authored on 2011-03-13
1654
       PETTERN         EXAMPLE
1655
    1. Column        : author
1656
    2. Table.Column  : book.author
1657
    3. Table__Column : book__author
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1658

            
update pod
Yuki Kimoto authored on 2011-03-13
1659
If column name is duplicate with other table,
1660
Main filter specified by C<table> option is used.
1661

            
1662
You can set multiple filters at once.
1663

            
1664
    $dbi->apply_filter(
1665
        'book',
1666
        [qw/issue_date write_date/] => {
1667
            out => 'tp_to_date',
1668
            in  => 'date_to_tp',
1669
            end => 'tp_to_displaydate'
1670
        }
1671
    );
fix bug : filter can't over...
Yuki Kimoto authored on 2011-02-09
1672

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1673
=head2 C<connect>
packaging one directory
yuki-kimoto authored on 2009-11-16
1674

            
update pod
Yuki Kimoto authored on 2011-03-13
1675
    my $dbi = DBIx::Custom->connect(
1676
        data_source => "dbi:mysql:database=dbname",
1677
        user => 'ken',
1678
        password => '!LFKD%$&',
1679
        dbi_option => {mysql_enable_utf8 => 1}
1680
    );
1681

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

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

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1688
=head2 create_model
1689

            
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
1690
    my $model = $dbi->create_model(
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1691
        table => 'book',
1692
        primary_key => 'id',
1693
        join => [
1694
            'inner join company on book.comparny_id = company.id'
1695
        ],
1696
        filter => [
1697
            publish_date => {
1698
                out => 'tp_to_date',
1699
                in => 'date_to_tp',
1700
                end => 'tp_to_displaydate'
1701
            }
1702
        ]
1703
    );
1704

            
1705
Create L<DBIx::Custom::Model> object and initialize model.
adeed EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-03-29
1706
the module is also used from model() method.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1707

            
1708
   $dbi->model('book')->select(...);
1709

            
cleanup
yuki-kimoto authored on 2010-10-17
1710
=head2 C<create_query>
1711
    
1712
    my $query = $dbi->create_query(
update pod
Yuki Kimoto authored on 2011-03-13
1713
        "insert into book {insert_param title author};";
cleanup
yuki-kimoto authored on 2010-10-17
1714
    );
update document
yuki-kimoto authored on 2009-11-19
1715

            
update pod
Yuki Kimoto authored on 2011-03-13
1716
Create L<DBIx::Custom::Query> object.
1717

            
cleanup
yuki-kimoto authored on 2010-10-17
1718
If you want to get high performance,
update pod
Yuki Kimoto authored on 2011-03-13
1719
create L<DBIx::Custom::Query> object and execute the query by C<execute()>
1720
instead of other methods, such as C<insert>, C<update>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1721

            
cleanup
yuki-kimoto authored on 2010-10-17
1722
    $dbi->execute($query, {author => 'Ken', title => '%Perl%'});
version 0.0901
yuki-kimoto authored on 2009-12-17
1723

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

            
1726
    my $dbh = $dbi->dbh;
1727

            
- removed EXPERIMENTAL Prefo...
Yuki Kimoto authored on 2011-04-04
1728
Get L<DBI> database handle. if C<connector> is set, you can get
1729
database handle from C<connector>.
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1730

            
1731
=head2 C<each_column>
1732

            
1733
    $dbi->each_column(
1734
        sub {
1735
            my ($dbi, $table, $column, $column_info) = @_;
1736
            
1737
            my $type = $column_info->{TYPE_NAME};
1738
            
1739
            if ($type eq 'DATE') {
1740
                # ...
1741
            }
1742
        }
1743
    );
1744

            
1745
Iterate all column informations of all table from database.
1746
Argument is callback when one column is found.
1747
Callback receive four arguments, dbi object, table name,
1748
column name and column information.
EXPERIMETAL fork safety impl...
Yuki Kimoto authored on 2011-03-12
1749

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

            
update pod
Yuki Kimoto authored on 2011-03-13
1752
    my $result = $dbi->execute(
1753
        "select * from book where {= title} and {like author}",
1754
        param => {title => 'Perl', author => '%Ken%'}
1755
    );
1756

            
1757
Execute SQL, containing tags.
1758
Return value is L<DBIx::Custom::Result> in select statement, or
1759
the count of affected rows in insert, update, delete statement.
1760

            
1761
Tag is turned into the statement containing place holder
1762
before SQL is executed.
1763

            
1764
    select * from where title = ? and author like ?;
1765

            
1766
See also L<Tags/Tags>.
1767

            
1768
The following opitons are currently available.
1769

            
1770
=over 4
1771

            
improved table search in col...
Yuki Kimoto authored on 2011-04-12
1772
=item C<table>
1773

            
1774
Table names for filtering.
1775

            
1776
    $dbi->execute(table => ['author', 'book']);
1777

            
1778
C<execute()> is unlike C<insert()>, C<update()>, C<delete()>, C<select(),
1779
Filtering is off because we don't know what filter is applied.
1780

            
1781

            
1782

            
1783

            
1784

            
1785

            
update pod
Yuki Kimoto authored on 2011-03-13
1786
=item C<filter>
1787

            
1788
Filter, executed before data is send to database. This is array reference.
1789
Filter value is code reference or
1790
filter name registerd by C<register_filter()>.
1791

            
1792
    # Basic
1793
    $dbi->execute(
1794
        $sql,
1795
        filter => [
1796
            title  => sub { uc $_[0] }
1797
            author => sub { uc $_[0] }
1798
        ]
1799
    );
1800
    
1801
    # At once
1802
    $dbi->execute(
1803
        $sql,
1804
        filter => [
1805
            [qw/title author/]  => sub { uc $_[0] }
1806
        ]
1807
    );
1808
    
1809
    # Filter name
1810
    $dbi->execute(
1811
        $sql,
1812
        filter => [
1813
            title  => 'upper_case',
1814
            author => 'upper_case'
1815
        ]
1816
    );
1817

            
1818
These filters are added to the C<out> filters, set by C<apply_filter()>.
update document
yuki-kimoto authored on 2009-11-19
1819

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

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1822
=head2 C<delete>
packaging one directory
yuki-kimoto authored on 2009-11-16
1823

            
update pod
Yuki Kimoto authored on 2011-03-13
1824
    $dbi->delete(table => 'book', where => {title => 'Perl'});
1825

            
1826
Delete statement.
1827

            
1828
The following opitons are currently available.
1829

            
update pod
Yuki Kimoto authored on 2011-03-13
1830
=over 4
1831

            
update pod
Yuki Kimoto authored on 2011-03-13
1832
=item C<table>
1833

            
1834
Table name.
1835

            
1836
    $dbi->delete(table => 'book');
1837

            
1838
=item C<where>
1839

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1840
Where clause. This is hash reference or L<DBIx::Custom::Where> object
1841
or array refrence, which contains where clause and paramter.
update pod
Yuki Kimoto authored on 2011-03-13
1842
    
1843
    # Hash reference
1844
    $dbi->delete(where => {title => 'Perl'});
1845
    
1846
    # DBIx::Custom::Where object
1847
    my $where = $dbi->where(
1848
        clause => ['and', '{= author}', '{like title}'],
1849
        param  => {author => 'Ken', title => '%Perl%'}
1850
    );
1851
    $dbi->delete(where => $where);
1852

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
1853
    # Array refrendce (where clause and parameter)
1854
    $dbi->delete(where =>
1855
        [
1856
            ['and', '{= author}', '{like title}'],
1857
            {author => 'Ken', title => '%Perl%'}
1858
        ]
1859
    );
1860
    
update pod
Yuki Kimoto authored on 2011-03-13
1861
=item C<append>
1862

            
1863
Append statement to last of SQL. This is string.
1864

            
1865
    $dbi->delete(append => 'order by title');
1866

            
1867
=item C<filter>
1868

            
1869
Filter, executed before data is send to database. This is array reference.
1870
Filter value is code reference or
1871
filter name registerd by C<register_filter()>.
1872

            
1873
    # Basic
1874
    $dbi->delete(
1875
        filter => [
1876
            title  => sub { uc $_[0] }
1877
            author => sub { uc $_[0] }
1878
        ]
1879
    );
1880
    
1881
    # At once
1882
    $dbi->delete(
1883
        filter => [
1884
            [qw/title author/]  => sub { uc $_[0] }
1885
        ]
1886
    );
1887
    
1888
    # Filter name
1889
    $dbi->delete(
1890
        filter => [
1891
            title  => 'upper_case',
1892
            author => 'upper_case'
1893
        ]
1894
    );
1895

            
1896
These filters are added to the C<out> filters, set by C<apply_filter()>.
1897

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
1898
=head2 C<column>
cleanup
Yuki Kimoto authored on 2011-03-21
1899

            
1900
    my $column = $self->column(book => ['author', 'title']);
1901

            
1902
Create column clause. The follwoing column clause is created.
1903

            
1904
    book.author as book__author,
1905
    book.title as book__title
1906

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1907
=item C<query>
update pod
Yuki Kimoto authored on 2011-03-13
1908

            
1909
Get L<DBIx::Custom::Query> object instead of executing SQL.
1910
This is true or false value.
1911

            
1912
    my $query = $dbi->delete(query => 1);
1913

            
1914
You can check SQL.
1915

            
1916
    my $sql = $query->sql;
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
1917

            
update pod
Yuki Kimoto authored on 2011-03-13
1918
=back
1919

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
1920
=head2 C<delete_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
1921

            
cleanup
yuki-kimoto authored on 2010-08-05
1922
    $dbi->delete_all(table => $table);
packaging one directory
yuki-kimoto authored on 2009-11-16
1923

            
update pod
Yuki Kimoto authored on 2011-03-13
1924
Delete statement to delete all rows.
1925
Options is same as C<delete()>.
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
1926

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
1927
=head2 C<delete_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1928

            
update pod
Yuki Kimoto authored on 2011-03-13
1929
Delete statement, using primary key.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1930

            
1931
    $dbi->delete_at(
1932
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
1933
        primary_key => 'id',
1934
        where => '5'
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1935
    );
1936

            
update pod
Yuki Kimoto authored on 2011-03-13
1937
This method is same as C<delete()> exept that
1938
C<primary_key> is specified and C<where> is constant value or array refrence.
1939
all option of C<delete()> is available.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1940

            
update pod
Yuki Kimoto authored on 2011-03-13
1941
=over 4
1942

            
1943
=item C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1944

            
update pod
Yuki Kimoto authored on 2011-03-13
1945
Primary key. This is constant value or array reference.
1946
    
1947
    # Constant value
1948
    $dbi->delete(primary_key => 'id');
1949

            
1950
    # Array reference
1951
    $dbi->delete(primary_key => ['id1', 'id2' ]);
1952

            
1953
This is used to create where clause.
1954

            
update pod
Yuki Kimoto authored on 2011-03-13
1955
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
1956

            
1957
Where clause, created from primary key information.
1958
This is constant value or array reference.
1959

            
1960
    # Constant value
1961
    $dbi->delete(where => 5);
1962

            
1963
    # Array reference
1964
    $dbi->delete(where => [3, 5]);
1965

            
1966
In first examle, the following SQL is created.
1967

            
1968
    delete from book where id = ?;
1969

            
1970
Place holder is set to 5.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
1971

            
update pod
Yuki Kimoto authored on 2011-03-13
1972
=back
1973

            
cleanup
yuki-kimoto authored on 2010-10-17
1974
=head2 C<insert>
1975

            
update pod
Yuki Kimoto authored on 2011-03-13
1976
    $dbi->insert(
1977
        table  => 'book', 
1978
        param  => {title => 'Perl', author => 'Ken'}
1979
    );
1980

            
1981
Insert statement.
1982

            
1983
The following opitons are currently available.
1984

            
update pod
Yuki Kimoto authored on 2011-03-13
1985
=over 4
1986

            
update pod
Yuki Kimoto authored on 2011-03-13
1987
=item C<table>
1988

            
1989
Table name.
1990

            
1991
    $dbi->insert(table => 'book');
1992

            
1993
=item C<param>
1994

            
1995
Insert data. This is hash reference.
1996

            
1997
    $dbi->insert(param => {title => 'Perl'});
1998

            
1999
=item C<append>
2000

            
2001
Append statement to last of SQL. This is string.
2002

            
2003
    $dbi->insert(append => 'order by title');
2004

            
2005
=item C<filter>
2006

            
2007
Filter, executed before data is send to database. This is array reference.
2008
Filter value is code reference or
2009
filter name registerd by C<register_filter()>.
2010

            
2011
    # Basic
2012
    $dbi->insert(
2013
        filter => [
2014
            title  => sub { uc $_[0] }
2015
            author => sub { uc $_[0] }
2016
        ]
2017
    );
2018
    
2019
    # At once
2020
    $dbi->insert(
2021
        filter => [
2022
            [qw/title author/]  => sub { uc $_[0] }
2023
        ]
2024
    );
2025
    
2026
    # Filter name
2027
    $dbi->insert(
2028
        filter => [
2029
            title  => 'upper_case',
2030
            author => 'upper_case'
2031
        ]
2032
    );
2033

            
2034
These filters are added to the C<out> filters, set by C<apply_filter()>.
2035

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2036
=item C<query>
update pod
Yuki Kimoto authored on 2011-03-13
2037

            
2038
Get L<DBIx::Custom::Query> object instead of executing SQL.
2039
This is true or false value.
2040

            
2041
    my $query = $dbi->insert(query => 1);
cleanup
yuki-kimoto authored on 2010-10-17
2042

            
update pod
Yuki Kimoto authored on 2011-03-13
2043
You can check SQL.
cleanup
yuki-kimoto authored on 2010-10-17
2044

            
update pod
Yuki Kimoto authored on 2011-03-13
2045
    my $sql = $query->sql;
2046

            
update pod
Yuki Kimoto authored on 2011-03-13
2047
=back
2048

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2049
=head2 C<insert_at()>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2050

            
update pod
Yuki Kimoto authored on 2011-03-13
2051
Insert statement, using primary key.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2052

            
2053
    $dbi->insert_at(
2054
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2055
        primary_key => 'id',
2056
        where => '5',
2057
        param => {title => 'Perl'}
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2058
    );
2059

            
update pod
Yuki Kimoto authored on 2011-03-13
2060
This method is same as C<insert()> exept that
2061
C<primary_key> is specified and C<where> is constant value or array refrence.
2062
all option of C<insert()> is available.
2063

            
update pod
Yuki Kimoto authored on 2011-03-13
2064
=over 4
2065

            
2066
=item C<primary_key>
update pod
Yuki Kimoto authored on 2011-03-13
2067

            
2068
Primary key. This is constant value or array reference.
2069
    
2070
    # Constant value
2071
    $dbi->insert(primary_key => 'id');
2072

            
2073
    # Array reference
2074
    $dbi->insert(primary_key => ['id1', 'id2' ]);
2075

            
2076
This is used to create parts of insert data.
2077

            
update pod
Yuki Kimoto authored on 2011-03-13
2078
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
2079

            
2080
Parts of Insert data, create from primary key information.
2081
This is constant value or array reference.
2082

            
2083
    # Constant value
2084
    $dbi->insert(where => 5);
2085

            
2086
    # Array reference
2087
    $dbi->insert(where => [3, 5]);
2088

            
2089
In first examle, the following SQL is created.
2090

            
2091
    insert into book (id, title) values (?, ?);
2092

            
2093
Place holders are set to 5 and 'Perl'.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
2094

            
update pod
Yuki Kimoto authored on 2011-03-13
2095
=back
2096

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2097
=head2 C<insert_param_tag>
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2098

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2099
    my $insert_param_tag = $dbi->insert_param_tag({title => 'a', age => 2});
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2100

            
2101
Create insert parameter tag.
2102

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2103
    (title, author) values ({? title}, {? author});
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2104

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

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2112
    lib / MyModel.pm
2113
        / MyModel / book.pm
2114
                  / company.pm
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2115

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

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

            
2120
    package MyModel;
2121
    
2122
    use base 'DBIx::Custom::Model';
update pod
Yuki Kimoto authored on 2011-03-13
2123
    
2124
    1;
add feture. all model class ...
Yuki Kimoto authored on 2011-02-18
2125

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

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2130
    package MyModel::book;
2131
    
2132
    use base 'MyModel';
2133
    
2134
    1;
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2135

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2138
    package MyModel::company;
2139
    
2140
    use base 'MyModel';
2141
    
2142
    1;
2143
    
2144
MyModel::book and MyModel::company is included by C<include_model()>.
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2145

            
update pod
Yuki Kimoto authored on 2011-03-13
2146
You can get model object by C<model()>.
2147

            
2148
    my $book_model    = $dbi->model('book');
2149
    my $company_model = $dbi->model('company');
removed experimental base_ta...
Yuki Kimoto authored on 2011-02-15
2150

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

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

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

            
2157
Merge paramters.
2158

            
2159
$param:
2160

            
2161
    {key1 => [1, 1], key2 => 2}
2162

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2163
=head2 C<method>
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2164

            
2165
    $dbi->method(
2166
        update_or_insert => sub {
2167
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2168
            
2169
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2170
        },
2171
        find_or_create   => sub {
2172
            my $self = shift;
update pod
Yuki Kimoto authored on 2011-03-13
2173
            
2174
            # Process
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2175
        }
2176
    );
2177

            
update pod
Yuki Kimoto authored on 2011-03-13
2178
Register method. These method is called directly from L<DBIx::Custom> object.
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2179

            
2180
    $dbi->update_or_insert;
2181
    $dbi->find_or_create;
2182

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

            
2185
    $dbi->model('book')->method(
2186
        insert => sub { ... },
2187
        update => sub { ... }
2188
    );
2189
    
2190
    my $model = $dbi->model('book');
2191

            
2192
Set and get a L<DBIx::Custom::Model> object,
2193

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

            
2196
    my $column = $self->mycolumn(book => ['author', 'title']);
2197

            
2198
Create column clause for myself. The follwoing column clause is created.
2199

            
2200
    book.author as author,
2201
    book.title as title
2202

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2205
    my $dbi = DBIx::Custom->new(
2206
        data_source => "dbi:mysql:database=dbname",
2207
        user => 'ken',
2208
        password => '!LFKD%$&',
2209
        dbi_option => {mysql_enable_utf8 => 1}
2210
    );
added experimental not_exist...
Yuki Kimoto authored on 2011-01-26
2211

            
2212
Create a new L<DBIx::Custom> object.
2213

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

            
2216
    my $not_exists = $dbi->not_exists;
2217

            
update pod
Yuki Kimoto authored on 2011-03-13
2218
DBIx::Custom::NotExists object, indicating the column is not exists.
2219
This is used by C<clause> of L<DBIx::Custom::Where> .
experimental extended select...
Yuki Kimoto authored on 2011-01-17
2220

            
cleanup
yuki-kimoto authored on 2010-10-17
2221
=head2 C<register_filter>
2222

            
update pod
Yuki Kimoto authored on 2011-03-13
2223
    $dbi->register_filter(
2224
        # Time::Piece object to database DATE format
2225
        tp_to_date => sub {
2226
            my $tp = shift;
2227
            return $tp->strftime('%Y-%m-%d');
2228
        },
2229
        # database DATE format to Time::Piece object
2230
        date_to_tp => sub {
2231
           my $date = shift;
2232
           return Time::Piece->strptime($date, '%Y-%m-%d');
2233
        }
2234
    );
cleanup
yuki-kimoto authored on 2010-10-17
2235
    
update pod
Yuki Kimoto authored on 2011-03-13
2236
Register filters, used by C<filter> option of many methods.
cleanup
yuki-kimoto authored on 2010-10-17
2237

            
update pod
Yuki Kimoto authored on 2011-03-13
2238
=head2 C<register_tag>
cleanup
yuki-kimoto authored on 2010-10-17
2239

            
update pod
Yuki Kimoto authored on 2011-03-13
2240
    $dbi->register_tag(
2241
        update => sub {
2242
            my @columns = @_;
2243
            
2244
            # Update parameters
2245
            my $s = 'set ';
2246
            $s .= "$_ = ?, " for @columns;
2247
            $s =~ s/, $//;
2248
            
2249
            return [$s, \@columns];
2250
        }
2251
    );
cleanup
yuki-kimoto authored on 2010-10-17
2252

            
update pod
Yuki Kimoto authored on 2011-03-13
2253
Register tag, used by C<execute()>.
cleanup
yuki-kimoto authored on 2010-10-17
2254

            
update pod
Yuki Kimoto authored on 2011-03-13
2255
See also L<Tags/Tags> about tag registered by default.
cleanup
yuki-kimoto authored on 2010-10-17
2256

            
update pod
Yuki Kimoto authored on 2011-03-13
2257
Tag parser receive arguments specified in tag.
2258
In the following tag, 'title' and 'author' is parser arguments
cleanup
yuki-kimoto authored on 2010-10-17
2259

            
update pod
Yuki Kimoto authored on 2011-03-13
2260
    {update_param title author} 
cleanup
yuki-kimoto authored on 2010-10-17
2261

            
update pod
Yuki Kimoto authored on 2011-03-13
2262
Tag parser must return array refrence,
2263
first element is the result statement, 
2264
second element is column names corresponding to place holders.
cleanup
yuki-kimoto authored on 2010-10-17
2265

            
update pod
Yuki Kimoto authored on 2011-03-13
2266
In this example, result statement is 
cleanup
yuki-kimoto authored on 2010-10-17
2267

            
update pod
Yuki Kimoto authored on 2011-03-13
2268
    set title = ?, author = ?
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2269

            
update pod
Yuki Kimoto authored on 2011-03-13
2270
Column names is
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2271

            
update pod
Yuki Kimoto authored on 2011-03-13
2272
    ['title', 'author']
added register_tag_processor
Yuki Kimoto authored on 2011-01-20
2273

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

            
select method column option ...
Yuki Kimoto authored on 2011-02-22
2276
    my $result = $dbi->select(
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2277
        table  => 'book',
2278
        column => ['author', 'title'],
2279
        where  => {author => 'Ken'},
select method column option ...
Yuki Kimoto authored on 2011-02-22
2280
    );
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2281
    
update pod
Yuki Kimoto authored on 2011-03-12
2282
Select statement.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2283

            
2284
The following opitons are currently available.
2285

            
2286
=over 4
2287

            
2288
=item C<table>
2289

            
2290
Table name.
2291

            
update pod
Yuki Kimoto authored on 2011-03-12
2292
    $dbi->select(table => 'book');
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2293

            
2294
=item C<column>
2295

            
2296
Column clause. This is array reference or constant value.
2297

            
2298
    # Hash refernce
2299
    $dbi->select(column => ['author', 'title']);
2300
    
2301
    # Constant value
2302
    $dbi->select(column => 'author');
2303

            
2304
Default is '*' unless C<column> is specified.
2305

            
2306
    # Default
2307
    $dbi->select(column => '*');
2308

            
2309
=item C<where>
2310

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2311
Where clause. This is hash reference or L<DBIx::Custom::Where> object,
2312
or array refrence, which contains where clause and paramter.
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2313
    
2314
    # Hash reference
update pod
Yuki Kimoto authored on 2011-03-12
2315
    $dbi->select(where => {author => 'Ken', 'title' => 'Perl'});
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2316
    
update pod
Yuki Kimoto authored on 2011-03-12
2317
    # DBIx::Custom::Where object
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2318
    my $where = $dbi->where(
2319
        clause => ['and', '{= author}', '{like title}'],
2320
        param  => {author => 'Ken', title => '%Perl%'}
2321
    );
update pod
Yuki Kimoto authored on 2011-03-12
2322
    $dbi->select(where => $where);
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2323

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2324
    # Array refrendce (where clause and parameter)
2325
    $dbi->select(where =>
2326
        [
2327
            ['and', '{= author}', '{like title}'],
2328
            {author => 'Ken', title => '%Perl%'}
2329
        ]
2330
    );
2331
    
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2332
=item C<join>
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2333

            
update pod
Yuki Kimoto authored on 2011-03-12
2334
Join clause used in need. This is array reference.
2335

            
2336
    $dbi->select(join =>
2337
        [
2338
            'left outer join company on book.company_id = company_id',
2339
            'left outer join location on company.location_id = location.id'
2340
        ]
2341
    );
2342

            
2343
If column cluase or where clause contain table name like "company.name",
2344
needed join clause is used automatically.
2345

            
2346
    $dbi->select(
2347
        table => 'book',
2348
        column => ['company.location_id as company__location_id'],
2349
        where => {'company.name' => 'Orange'},
2350
        join => [
2351
            'left outer join company on book.company_id = company.id',
2352
            'left outer join location on company.location_id = location.id'
2353
        ]
2354
    );
2355

            
2356
In above select, the following SQL is created.
2357

            
2358
    select company.location_id as company__location_id
2359
    from book
2360
      left outer join company on book.company_id = company.id
2361
    where company.name = Orange
2362

            
added EXPERIMENTAL replace()...
Yuki Kimoto authored on 2011-04-01
2363
=item C<param> EXPERIMETNAL
2364

            
2365
Parameter shown before where clause.
2366
    
2367
    $dbi->select(
2368
        table => 'table1',
2369
        column => 'table1.key1 as table1_key1, key2, key3',
2370
        where   => {'table1.key2' => 3},
2371
        join  => ['inner join (select * from table2 where {= table2.key3})' . 
2372
                  ' as table2 on table1.key1 = table2.key1'],
2373
        param => {'table2.key3' => 5}
2374
    );
2375

            
2376
For example, if you want to contain tag in join clause, 
2377
you can pass parameter by C<param> option.
2378

            
update pod
Yuki Kimoto authored on 2011-03-12
2379
=item C<append>
2380

            
update pod
Yuki Kimoto authored on 2011-03-13
2381
Append statement to last of SQL. This is string.
update pod
Yuki Kimoto authored on 2011-03-12
2382

            
2383
    $dbi->select(append => 'order by title');
2384

            
2385
=item C<filter>
2386

            
update pod
Yuki Kimoto authored on 2011-03-13
2387
Filter, executed before data is send to database. This is array reference.
2388
Filter value is code reference or
update pod
Yuki Kimoto authored on 2011-03-12
2389
filter name registerd by C<register_filter()>.
2390

            
2391
    # Basic
2392
    $dbi->select(
2393
        filter => [
2394
            title  => sub { uc $_[0] }
2395
            author => sub { uc $_[0] }
2396
        ]
2397
    );
2398
    
2399
    # At once
2400
    $dbi->select(
2401
        filter => [
2402
            [qw/title author/]  => sub { uc $_[0] }
2403
        ]
2404
    );
2405
    
2406
    # Filter name
2407
    $dbi->select(
2408
        filter => [
2409
            title  => 'upper_case',
2410
            author => 'upper_case'
2411
        ]
2412
    );
add experimental selection o...
Yuki Kimoto authored on 2011-02-09
2413

            
update pod
Yuki Kimoto authored on 2011-03-13
2414
These filters are added to the C<out> filters, set by C<apply_filter()>.
update document
yuki-kimoto authored on 2009-11-19
2415

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2416
=item C<query>
cleanup
yuki-kimoto authored on 2010-08-09
2417

            
update pod
Yuki Kimoto authored on 2011-03-12
2418
Get L<DBIx::Custom::Query> object instead of executing SQL.
2419
This is true or false value.
2420

            
update pod
Yuki Kimoto authored on 2011-03-13
2421
    my $query = $dbi->select(query => 1);
update pod
Yuki Kimoto authored on 2011-03-12
2422

            
update pod
Yuki Kimoto authored on 2011-03-13
2423
You can check SQL.
update pod
Yuki Kimoto authored on 2011-03-12
2424

            
2425
    my $sql = $query->sql;
2426

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2427
=item C<type>
- added EXPERIMENTAL type() ...
Yuki Kimoto authored on 2011-03-21
2428

            
2429
Specify database data type.
2430

            
2431
    $dbi->select(type => [image => DBI::SQL_BLOB]);
2432
    $dbi->select(type => [[qw/image audio/] => DBI::SQL_BLOB]);
2433

            
2434
This is used to bind paramter by C<bind_param()> of statment handle.
2435

            
2436
    $sth->bind_param($pos, $value, DBI::SQL_BLOB);
2437

            
update pod
Yuki Kimoto authored on 2011-03-12
2438
=back
cleanup
Yuki Kimoto authored on 2011-03-08
2439

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2440
=head2 C<select_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2441

            
update pod
Yuki Kimoto authored on 2011-03-12
2442
Select statement, using primary key.
2443

            
2444
    $dbi->select_at(
2445
        table => 'book',
2446
        primary_key => 'id',
2447
        where => '5'
2448
    );
2449

            
update pod
Yuki Kimoto authored on 2011-03-13
2450
This method is same as C<select()> exept that
2451
C<primary_key> is specified and C<where> is constant value or array refrence.
update pod
Yuki Kimoto authored on 2011-03-12
2452
all option of C<select()> is available.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2453

            
update pod
Yuki Kimoto authored on 2011-03-13
2454
=over 4
2455

            
2456
=item C<primary_key>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2457

            
update pod
Yuki Kimoto authored on 2011-03-12
2458
Primary key. This is constant value or array reference.
2459
    
2460
    # Constant value
2461
    $dbi->select(primary_key => 'id');
2462

            
2463
    # Array reference
2464
    $dbi->select(primary_key => ['id1', 'id2' ]);
2465

            
update pod
Yuki Kimoto authored on 2011-03-13
2466
This is used to create where clause.
2467

            
update pod
Yuki Kimoto authored on 2011-03-13
2468
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-12
2469

            
update pod
Yuki Kimoto authored on 2011-03-13
2470
Where clause, created from primary key information.
update pod
Yuki Kimoto authored on 2011-03-12
2471
This is constant value or array reference.
2472

            
2473
    # Constant value
2474
    $dbi->select(where => 5);
2475

            
2476
    # Array reference
2477
    $dbi->select(where => [3, 5]);
2478

            
2479
In first examle, the following SQL is created.
2480

            
2481
    select * from book where id = ?
2482

            
2483
Place holder is set to 5.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2484

            
update pod
Yuki Kimoto authored on 2011-03-13
2485
=back
2486

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

            
update pod
Yuki Kimoto authored on 2011-03-13
2489
    $dbi->update(
2490
        table  => 'book',
2491
        param  => {title => 'Perl'},
2492
        where  => {id => 4}
2493
    );
removed reconnect method
yuki-kimoto authored on 2010-05-28
2494

            
update pod
Yuki Kimoto authored on 2011-03-13
2495
Update statement.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2496

            
update pod
Yuki Kimoto authored on 2011-03-13
2497
The following opitons are currently available.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2498

            
update pod
Yuki Kimoto authored on 2011-03-13
2499
=over 4
2500

            
update pod
Yuki Kimoto authored on 2011-03-13
2501
=item C<table>
2502

            
update pod
Yuki Kimoto authored on 2011-03-13
2503
Table name.
2504

            
2505
    $dbi->update(table => 'book');
2506

            
2507
=item C<param>
2508

            
2509
Update data. This is hash reference.
2510

            
2511
    $dbi->update(param => {title => 'Perl'});
2512

            
2513
=item C<where>
2514

            
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2515
Where clause. This is hash reference or L<DBIx::Custom::Where> object
2516
or array refrence.
update pod
Yuki Kimoto authored on 2011-03-13
2517
    
2518
    # Hash reference
2519
    $dbi->update(where => {author => 'Ken', 'title' => 'Perl'});
2520
    
2521
    # DBIx::Custom::Where object
2522
    my $where = $dbi->where(
2523
        clause => ['and', '{= author}', '{like title}'],
2524
        param  => {author => 'Ken', title => '%Perl%'}
2525
    );
2526
    $dbi->update(where => $where);
where can recieve array refr...
Yuki Kimoto authored on 2011-03-24
2527
    
2528
    # Array refrendce (where clause and parameter)
2529
    $dbi->update(where =>
2530
        [
2531
            ['and', '{= author}', '{like title}'],
2532
            {author => 'Ken', title => '%Perl%'}
2533
        ]
2534
    );
update pod
Yuki Kimoto authored on 2011-03-13
2535

            
2536
=item C<append>
2537

            
2538
Append statement to last of SQL. This is string.
2539

            
2540
    $dbi->update(append => 'order by title');
2541

            
2542
=item C<filter>
2543

            
2544
Filter, executed before data is send to database. This is array reference.
2545
Filter value is code reference or
2546
filter name registerd by C<register_filter()>.
2547

            
2548
    # Basic
2549
    $dbi->update(
2550
        filter => [
2551
            title  => sub { uc $_[0] }
2552
            author => sub { uc $_[0] }
2553
        ]
2554
    );
2555
    
2556
    # At once
2557
    $dbi->update(
2558
        filter => [
2559
            [qw/title author/]  => sub { uc $_[0] }
2560
        ]
2561
    );
2562
    
2563
    # Filter name
2564
    $dbi->update(
2565
        filter => [
2566
            title  => 'upper_case',
2567
            author => 'upper_case'
2568
        ]
2569
    );
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2570

            
update pod
Yuki Kimoto authored on 2011-03-13
2571
These filters are added to the C<out> filters, set by C<apply_filter()>.
added experimental update_pa...
Yuki Kimoto authored on 2011-03-08
2572

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2573
=item C<query>
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2574

            
update pod
Yuki Kimoto authored on 2011-03-13
2575
Get L<DBIx::Custom::Query> object instead of executing SQL.
2576
This is true or false value.
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2577

            
update pod
Yuki Kimoto authored on 2011-03-13
2578
    my $query = $dbi->update(query => 1);
2579

            
2580
You can check SQL.
2581

            
2582
    my $sql = $query->sql;
add experimental setup_model...
Yuki Kimoto authored on 2011-02-21
2583

            
update pod
Yuki Kimoto authored on 2011-03-13
2584
=back
2585

            
cleanup
yuki-kimoto authored on 2010-10-17
2586
=head2 C<update_all>
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
2587

            
update pod
Yuki Kimoto authored on 2011-03-13
2588
    $dbi->update_all(table => 'book', param => {title => 'Perl'});
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
2589

            
update pod
Yuki Kimoto authored on 2011-03-13
2590
Update statement to update all rows.
2591
Options is same as C<update()>.
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
2592

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2593
=head2 C<update_at()>
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2594

            
update pod
Yuki Kimoto authored on 2011-03-13
2595
Update statement, using primary key.
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2596

            
2597
    $dbi->update_at(
2598
        table => 'book',
update pod
Yuki Kimoto authored on 2011-03-13
2599
        primary_key => 'id',
2600
        where => '5',
2601
        param => {title => 'Perl'}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2602
    );
2603

            
update pod
Yuki Kimoto authored on 2011-03-13
2604
This method is same as C<update()> exept that
2605
C<primary_key> is specified and C<where> is constant value or array refrence.
2606
all option of C<update()> is available.
2607

            
update pod
Yuki Kimoto authored on 2011-03-13
2608
=over 4
2609

            
2610
=item C<primary_key>
update pod
Yuki Kimoto authored on 2011-03-13
2611

            
2612
Primary key. This is constant value or array reference.
2613
    
2614
    # Constant value
2615
    $dbi->update(primary_key => 'id');
2616

            
2617
    # Array reference
2618
    $dbi->update(primary_key => ['id1', 'id2' ]);
2619

            
2620
This is used to create where clause.
2621

            
update pod
Yuki Kimoto authored on 2011-03-13
2622
=item C<where>
update pod
Yuki Kimoto authored on 2011-03-13
2623

            
2624
Where clause, created from primary key information.
2625
This is constant value or array reference.
2626

            
2627
    # Constant value
2628
    $dbi->update(where => 5);
2629

            
2630
    # Array reference
2631
    $dbi->update(where => [3, 5]);
2632

            
2633
In first examle, the following SQL is created.
2634

            
2635
    update book set title = ? where id = ?
2636

            
2637
Place holders are set to 'Perl' and 5.
2638

            
update pod
Yuki Kimoto authored on 2011-03-13
2639
=back
2640

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2641
=head2 C<update_param_tag>
update pod
Yuki Kimoto authored on 2011-03-13
2642

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2643
    my $update_param_tag = $dbi->update_param_tag({title => 'a', age => 2});
update pod
Yuki Kimoto authored on 2011-03-13
2644

            
2645
Create update parameter tag.
2646

            
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2647
    set title = {? title}, author = {? author}
2648

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2649
You can create tag without 'set '
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2650
by C<no_set> option.
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2651

            
- renamed update_param to up...
Yuki Kimoto authored on 2011-03-30
2652
    my $update_param_tag = $dbi->update_param_tag(
added EXPERIMENTAL updat_par...
Yuki Kimoto authored on 2011-03-30
2653
        {title => 'a', age => 2}
2654
        {no_set => 1}
2655
    );
2656

            
2657
    title = {? title}, author = {? author}
add experimental update_at()...
Yuki Kimoto authored on 2011-02-21
2658

            
removed EXPERIMETNAL flag fr...
Yuki Kimoto authored on 2011-03-25
2659
=head2 C<where>
fix tests
Yuki Kimoto authored on 2011-01-18
2660

            
cleanup
Yuki Kimoto authored on 2011-03-09
2661
    my $where = $dbi->where(
2662
        clause => ['and', '{= title}', '{= author}'],
2663
        param => {title => 'Perl', author => 'Ken'}
2664
    );
fix tests
Yuki Kimoto authored on 2011-01-18
2665

            
2666
Create a new L<DBIx::Custom::Where> object.
2667

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
2668
=head2 C<setup_model>
cleanup
Yuki Kimoto authored on 2011-01-12
2669

            
update pod
Yuki Kimoto authored on 2011-03-13
2670
    $dbi->setup_model;
cleanup
Yuki Kimoto authored on 2011-01-12
2671

            
update pod
Yuki Kimoto authored on 2011-03-13
2672
Setup all model objects.
update pod
Yuki Kimoto authored on 2011-03-13
2673
C<columns> of model object is automatically set, parsing database information.
cleanup
Yuki Kimoto authored on 2011-01-12
2674

            
cleanup
Yuki Kimoto authored on 2011-01-25
2675
=head1 Tags
2676

            
2677
The following tags is available.
2678

            
2679
=head2 C<?>
2680

            
2681
Placeholder tag.
2682

            
2683
    {? NAME}    ->   ?
2684

            
2685
=head2 C<=>
2686

            
2687
Equal tag.
2688

            
2689
    {= NAME}    ->   NAME = ?
2690

            
2691
=head2 C<E<lt>E<gt>>
2692

            
2693
Not equal tag.
2694

            
2695
    {<> NAME}   ->   NAME <> ?
2696

            
2697
=head2 C<E<lt>>
2698

            
2699
Lower than tag
2700

            
2701
    {< NAME}    ->   NAME < ?
2702

            
2703
=head2 C<E<gt>>
2704

            
2705
Greater than tag
2706

            
2707
    {> NAME}    ->   NAME > ?
2708

            
2709
=head2 C<E<gt>=>
2710

            
2711
Greater than or equal tag
2712

            
2713
    {>= NAME}   ->   NAME >= ?
2714

            
2715
=head2 C<E<lt>=>
2716

            
2717
Lower than or equal tag
2718

            
2719
    {<= NAME}   ->   NAME <= ?
2720

            
2721
=head2 C<like>
2722

            
2723
Like tag
2724

            
2725
    {like NAME}   ->   NAME like ?
2726

            
2727
=head2 C<in>
2728

            
2729
In tag.
2730

            
2731
    {in NAME COUNT}   ->   NAME in [?, ?, ..]
2732

            
2733
=head2 C<insert_param>
2734

            
2735
Insert parameter tag.
2736

            
2737
    {insert_param NAME1 NAME2}   ->   (NAME1, NAME2) values (?, ?)
2738

            
2739
=head2 C<update_param>
2740

            
2741
Updata parameter tag.
2742

            
2743
    {update_param NAME1 NAME2}   ->   set NAME1 = ?, NAME2 = ?
2744

            
added environment variable D...
Yuki Kimoto authored on 2011-04-02
2745
=head1 ENVIRONMENT VARIABLE
2746

            
2747
=head2 C<DBIX_CUSTOM_DEBUG>
2748

            
2749
If environment variable C<DBIX_CUSTOM_DEBUG> is set to true,
2750
executed SQL is printed to STDERR.
2751

            
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
2752
=head1 STABILITY
2753

            
cleanup
Yuki Kimoto authored on 2011-01-25
2754
L<DBIx::Custom> is stable. APIs keep backword compatible
added select() all_column op...
Yuki Kimoto authored on 2011-03-12
2755
except EXPERIMENTAL one in the feature.
DBIx::Custom is now stable
yuki-kimoto authored on 2010-09-07
2756

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

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

            
2761
C<< <kimoto.yuki at gmail.com> >>
2762

            
2763
L<http://github.com/yuki-kimoto/DBIx-Custom>
2764

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
2765
=head1 AUTHOR
2766

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

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

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

            
2773
This program is free software; you can redistribute it and/or modify it
2774
under the same terms as Perl itself.
2775

            
2776
=cut
added cache_method attribute
yuki-kimoto authored on 2010-06-25
2777

            
2778