line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
16
|
|
|
16
|
|
609330
|
use 5.008007; |
|
16
|
|
|
|
|
181
|
|
2
|
|
|
|
|
|
|
package DBIx::Custom; |
3
|
16
|
|
|
16
|
|
8588
|
use Object::Simple -base; |
|
16
|
|
|
|
|
21814
|
|
|
16
|
|
|
|
|
114
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.45'; |
6
|
|
|
|
|
|
|
|
7
|
16
|
|
|
16
|
|
1921
|
use Carp 'confess'; |
|
16
|
|
|
|
|
44
|
|
|
16
|
|
|
|
|
865
|
|
8
|
16
|
|
|
16
|
|
25319
|
use DBI; |
|
16
|
|
|
|
|
297530
|
|
|
16
|
|
|
|
|
1104
|
|
9
|
16
|
|
|
16
|
|
9754
|
use DBIx::Custom::Result; |
|
16
|
|
|
|
|
48
|
|
|
16
|
|
|
|
|
171
|
|
10
|
16
|
|
|
16
|
|
8745
|
use DBIx::Custom::Where; |
|
16
|
|
|
|
|
44
|
|
|
16
|
|
|
|
|
140
|
|
11
|
16
|
|
|
16
|
|
8632
|
use DBIx::Custom::Model; |
|
16
|
|
|
|
|
48
|
|
|
16
|
|
|
|
|
162
|
|
12
|
16
|
|
|
16
|
|
8617
|
use DBIx::Custom::Order; |
|
16
|
|
|
|
|
45
|
|
|
16
|
|
|
|
|
101
|
|
13
|
16
|
|
|
16
|
|
599
|
use DBIx::Custom::Util qw/_array_to_hash _subname _deprecate/; |
|
16
|
|
|
|
|
32
|
|
|
16
|
|
|
|
|
782
|
|
14
|
16
|
|
|
16
|
|
7741
|
use DBIx::Custom::Mapper; |
|
16
|
|
|
|
|
47
|
|
|
16
|
|
|
|
|
251
|
|
15
|
16
|
|
|
16
|
|
595
|
use DBIx::Custom::NotExists; |
|
16
|
|
|
|
|
32
|
|
|
16
|
|
|
|
|
308
|
|
16
|
16
|
|
|
16
|
|
7930
|
use DBIx::Custom::Query; |
|
16
|
|
|
|
|
45
|
|
|
16
|
|
|
|
|
144
|
|
17
|
16
|
|
|
16
|
|
9758
|
use DBIx::Connector; |
|
16
|
|
|
|
|
65589
|
|
|
16
|
|
|
|
|
683
|
|
18
|
|
|
|
|
|
|
|
19
|
16
|
|
|
16
|
|
10042
|
use Encode qw/encode encode_utf8 decode_utf8/; |
|
16
|
|
|
|
|
167190
|
|
|
16
|
|
|
|
|
1404
|
|
20
|
16
|
|
|
16
|
|
133
|
use Scalar::Util qw/weaken/; |
|
16
|
|
|
|
|
36
|
|
|
16
|
|
|
|
|
185560
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has [qw/dsn password quote user exclude_table user_table_info |
23
|
|
|
|
|
|
|
user_column_info safety_character/]; |
24
|
|
|
|
|
|
|
has connector => 1; |
25
|
|
|
|
|
|
|
has option => sub { {} }; |
26
|
|
|
|
|
|
|
has default_option => sub { |
27
|
|
|
|
|
|
|
{ |
28
|
|
|
|
|
|
|
RaiseError => 1, |
29
|
|
|
|
|
|
|
PrintError => 0, |
30
|
|
|
|
|
|
|
AutoCommit => 1 |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
}; |
33
|
|
|
|
|
|
|
has filters => sub { |
34
|
|
|
|
|
|
|
{ |
35
|
|
|
|
|
|
|
encode_utf8 => sub { encode_utf8($_[0]) }, |
36
|
|
|
|
|
|
|
decode_utf8 => sub { decode_utf8($_[0]) } |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
has last_sql => ''; |
40
|
|
|
|
|
|
|
has models => sub { {} }; |
41
|
|
|
|
|
|
|
has now => sub { |
42
|
|
|
|
|
|
|
sub { |
43
|
|
|
|
|
|
|
my ($sec, $min, $hour, $mday, $mon, $year) = localtime; |
44
|
|
|
|
|
|
|
$mon++; |
45
|
|
|
|
|
|
|
$year += 1900; |
46
|
|
|
|
|
|
|
my $now = sprintf("%04d-%02d-%02d %02d:%02d:%02d", |
47
|
|
|
|
|
|
|
$year, $mon, $mday, $hour, $min, $sec); |
48
|
|
|
|
|
|
|
return $now; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
has result_class => 'DBIx::Custom::Result'; |
52
|
|
|
|
|
|
|
has separator => '.'; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has mytable_symbol => '__MY__'; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
has 'column_name_lc'; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub create_result { |
59
|
0
|
|
|
0
|
0
|
0
|
my ($self, $sth) = @_; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
0
|
return $self->result_class->new(sth => $sth, dbi => $self); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub column { |
65
|
70
|
|
|
70
|
1
|
264
|
my $self = shift; |
66
|
70
|
100
|
|
|
|
218
|
my $option = pop if ref $_[-1] eq 'HASH'; |
67
|
70
|
|
|
|
|
116
|
my $real_table = shift; |
68
|
70
|
|
|
|
|
106
|
my $columns = shift; |
69
|
70
|
|
66
|
|
|
283
|
my $table = $option->{alias} || $real_table; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Columns |
72
|
70
|
100
|
100
|
|
|
326
|
if (!defined $columns || $columns eq '*') { |
73
|
30
|
|
|
|
|
65
|
$columns = $self->model($real_table)->columns; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Separator |
77
|
70
|
|
|
|
|
2028
|
my $separator = $self->separator; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# . is replaced |
80
|
70
|
|
|
|
|
538
|
my $t = $table; |
81
|
70
|
|
|
|
|
172
|
$t =~ s/\./$separator/g; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# Column clause |
84
|
70
|
|
|
|
|
139
|
my @column; |
85
|
70
|
|
50
|
|
|
183
|
$columns ||= []; |
86
|
|
|
|
|
|
|
push @column, $self->_tq($table) . "." . $self->q($_) . |
87
|
|
|
|
|
|
|
" as " . $self->q("${t}${separator}$_") |
88
|
70
|
|
|
|
|
283
|
for @$columns; |
89
|
|
|
|
|
|
|
|
90
|
70
|
|
|
|
|
813
|
return join (', ', @column); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub connect { |
94
|
513
|
|
|
513
|
1
|
879822
|
my $self; |
95
|
|
|
|
|
|
|
|
96
|
513
|
100
|
|
|
|
1518
|
if (ref $_[0]) { |
97
|
3
|
|
|
|
|
7
|
$self = shift; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
510
|
|
|
|
|
1674
|
$self = shift->new(@_); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
513
|
|
|
|
|
13864
|
my $connector = $self->connector; |
104
|
|
|
|
|
|
|
|
105
|
513
|
50
|
33
|
|
|
6224
|
if (!ref $connector && $connector) { |
106
|
513
|
|
|
|
|
9743
|
my $dsn = $self->dsn; |
107
|
513
|
|
|
|
|
12440
|
my $user = $self->user; |
108
|
513
|
|
|
|
|
10735
|
my $password = $self->password; |
109
|
513
|
|
|
|
|
10770
|
my $option = $self->option; |
110
|
|
|
|
|
|
|
my $connector = DBIx::Connector->new($dsn, $user, $password, |
111
|
513
|
|
|
|
|
1028
|
{%{$self->default_option} , %$option}); |
|
513
|
|
|
|
|
8479
|
|
112
|
513
|
|
|
|
|
15687
|
$self->connector($connector); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Connect |
116
|
513
|
|
|
|
|
4270
|
$self->dbh; |
117
|
|
|
|
|
|
|
|
118
|
507
|
|
|
|
|
1487
|
return $self; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub dbh { |
122
|
4070
|
|
|
4070
|
1
|
12842
|
my $self = shift; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# Set |
125
|
4070
|
50
|
|
|
|
8909
|
if (@_) { |
126
|
0
|
|
|
|
|
0
|
$self->{dbh} = $_[0]; |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
0
|
return $self; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Get |
132
|
|
|
|
|
|
|
else { |
133
|
|
|
|
|
|
|
# From Connction manager |
134
|
4070
|
50
|
|
|
|
74984
|
if (my $connector = $self->connector) { |
135
|
4070
|
50
|
33
|
|
|
47094
|
confess "connector must have dbh() method " . _subname |
136
|
|
|
|
|
|
|
unless ref $connector && $connector->can('dbh'); |
137
|
|
|
|
|
|
|
|
138
|
4070
|
|
|
|
|
11693
|
$self->{dbh} = $connector->dbh; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Connect |
142
|
4064
|
|
33
|
|
|
451321
|
$self->{dbh} ||= $self->_connect; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Quote |
145
|
4064
|
100
|
|
|
|
59726
|
if (!defined $self->quote) { |
146
|
331
|
|
|
|
|
2811
|
my $driver = $self->_driver; |
147
|
331
|
50
|
|
|
|
1615
|
my $quote = $driver eq 'odbc' ? '[]' |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
148
|
|
|
|
|
|
|
: $driver eq 'ado' ? '[]' |
149
|
|
|
|
|
|
|
: $driver eq 'mysql' ? '`' |
150
|
|
|
|
|
|
|
: '"'; |
151
|
331
|
|
|
|
|
5861
|
$self->quote($quote); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
4064
|
|
|
|
|
36808
|
return $self->{dbh}; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub delete { |
159
|
74
|
|
|
74
|
1
|
3519
|
my ($self, %opt) = @_; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# Don't allow delete all rows |
162
|
|
|
|
|
|
|
confess qq{delete method where or id option must be specified } . _subname |
163
|
74
|
100
|
100
|
|
|
477
|
if !$opt{where} && !defined $opt{id} && !$opt{allow_delete_all}; |
|
|
|
100
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# Where |
166
|
71
|
|
|
|
|
117
|
my $where; |
167
|
71
|
100
|
|
|
|
185
|
if (defined $opt{id}) { |
168
|
15
|
|
|
|
|
65
|
$where = $self->_id_to_param($opt{id}, $opt{primary_key}, $opt{table}) ; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
else { |
171
|
56
|
|
|
|
|
106
|
$where = $opt{where}; |
172
|
|
|
|
|
|
|
} |
173
|
71
|
|
|
|
|
207
|
my $w = $self->_where_clause_and_param($where); |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# Delete statement |
176
|
71
|
|
|
|
|
147
|
my $sql = "delete "; |
177
|
71
|
100
|
|
|
|
201
|
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
178
|
71
|
|
|
|
|
271
|
$sql .= "from " . $self->_tq($opt{table}) . " $w->{clause} "; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# Execute query |
181
|
71
|
|
|
|
|
276
|
$self->execute($sql, $w->{param}, %opt); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
27
|
|
|
27
|
1
|
18001
|
sub delete_all { shift->delete(@_, allow_delete_all => 1) } |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub create_model { |
187
|
223
|
|
|
223
|
1
|
1337
|
my $self = shift; |
188
|
|
|
|
|
|
|
|
189
|
223
|
|
|
|
|
332
|
my $opt; |
190
|
223
|
100
|
100
|
|
|
1168
|
if (@_ % 2 != 0 && !ref $_[0]) { |
191
|
3
|
|
|
|
|
14
|
$opt = {table => shift, @_}; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
else { |
194
|
220
|
100
|
|
|
|
736
|
$opt = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
# Options |
198
|
223
|
|
|
|
|
458
|
$opt->{dbi} = $self; |
199
|
223
|
|
100
|
|
|
709
|
my $model_class = delete $opt->{model_class} || 'DBIx::Custom::Model'; |
200
|
223
|
|
|
|
|
387
|
my $model_name = delete $opt->{name}; |
201
|
223
|
|
|
|
|
374
|
my $model_table = delete $opt->{table}; |
202
|
223
|
|
66
|
|
|
621
|
$model_name ||= $model_table; |
203
|
223
|
|
|
|
|
341
|
my $column_name_lc = delete $opt->{column_name_lc}; |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# Create model |
206
|
223
|
|
|
|
|
1090
|
my $model = $model_class->new($opt); |
207
|
223
|
|
|
|
|
878
|
weaken $model->{dbi}; |
208
|
223
|
100
|
|
|
|
3746
|
$model->table($model_table) unless $model->table; |
209
|
223
|
|
|
|
|
8999
|
$model->name($model_name); |
210
|
|
|
|
|
|
|
|
211
|
223
|
50
|
33
|
|
|
4712
|
if (!$model->columns || !@{$model->columns}) { |
|
223
|
|
|
|
|
4798
|
|
212
|
223
|
|
|
|
|
4910
|
$model->columns($self->get_columns_from_db($model->table, {column_name_lc => $column_name_lc})); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
# Set model |
216
|
223
|
|
|
|
|
2541
|
$self->model($model_name, $model); |
217
|
|
|
|
|
|
|
|
218
|
223
|
|
|
|
|
3764
|
return $self->model($model->name); |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub execute { |
222
|
3199
|
|
|
3199
|
1
|
189394
|
my $self = shift; |
223
|
3199
|
|
|
|
|
5163
|
my $sql = shift; |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
# Options |
226
|
3199
|
|
|
|
|
4382
|
my $param; |
227
|
3199
|
100
|
|
|
|
8324
|
$param = shift if @_ % 2; |
228
|
3199
|
|
100
|
|
|
10406
|
$param ||= {}; |
229
|
3199
|
|
|
|
|
7838
|
my %opt = @_; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# Append |
232
|
3199
|
100
|
|
|
|
7367
|
$sql .= $opt{append} if defined $opt{append}; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
# Parse named place holder |
235
|
3199
|
|
|
|
|
5225
|
my $safe_char = $self->{safety_character}; |
236
|
3199
|
50
|
|
|
|
12603
|
my $place_holder_re = $safe_char eq 'a-zA-Z0-9_' |
237
|
|
|
|
|
|
|
? qr/(.*?[^\\]):([$safe_char\.]+)(?:\{(.*?)\})?(.*)/so |
238
|
|
|
|
|
|
|
: qr/(.*?[^\\]):([$safe_char\.]+)(?:\{(.*?)\})?(.*)/s; |
239
|
3199
|
|
|
|
|
5538
|
my $source_sql = $sql; |
240
|
3199
|
|
|
|
|
7559
|
$source_sql =~ s/([0-9]):/$1\\:/g; |
241
|
3199
|
|
|
|
|
4703
|
my $parsed_sql = ''; |
242
|
3199
|
|
|
|
|
4269
|
my $columns; |
243
|
3199
|
|
|
|
|
21213
|
while ($source_sql =~ /$place_holder_re/) { |
244
|
2593
|
|
|
|
|
7092
|
push @$columns, $2; |
245
|
2593
|
100
|
|
|
|
17767
|
($parsed_sql, $source_sql) = defined $3 ? |
246
|
|
|
|
|
|
|
($parsed_sql . "$1$2 $3 ?", "$4") : ($parsed_sql . "$1?", "$4"); |
247
|
|
|
|
|
|
|
} |
248
|
3199
|
|
|
|
|
6709
|
$parsed_sql .= $source_sql; |
249
|
3199
|
100
|
|
|
|
7941
|
$parsed_sql =~ s/\\:/:/g if index($parsed_sql, "\\:") != -1; |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
# Edit SQL after building |
252
|
3199
|
|
|
|
|
5202
|
my $after_build_sql = $opt{after_build_sql}; |
253
|
3199
|
100
|
|
|
|
6107
|
$parsed_sql = $after_build_sql->($parsed_sql) if $after_build_sql; |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
# Type rule |
256
|
3199
|
|
|
|
|
4038
|
my $type_filters; |
257
|
3199
|
100
|
|
|
|
7110
|
if ($self->{_type_rule_is_called}) { |
258
|
149
|
|
|
|
|
247
|
$type_filters = {}; |
259
|
149
|
100
|
|
|
|
336
|
unless ($opt{type_rule_off}) { |
260
|
134
|
|
100
|
|
|
318
|
my $tables = $opt{table} || []; |
261
|
134
|
100
|
|
|
|
381
|
$tables = [$tables] unless ref $tables eq 'ARRAY'; |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
# Tables |
264
|
134
|
|
|
|
|
207
|
my $main_table = @{$tables}[-1]; |
|
134
|
|
|
|
|
273
|
|
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
my $type_rule_off_parts = { |
267
|
|
|
|
|
|
|
1 => $opt{type_rule1_off}, |
268
|
|
|
|
|
|
|
2 => $opt{type_rule2_off} |
269
|
134
|
|
|
|
|
401
|
}; |
270
|
134
|
|
|
|
|
252
|
for my $i (1, 2) { |
271
|
268
|
100
|
|
|
|
575
|
unless ($type_rule_off_parts->{$i}) { |
272
|
262
|
|
|
|
|
425
|
$type_filters->{$i} = {}; |
273
|
262
|
|
100
|
|
|
757
|
my $table_alias = $opt{table_alias} || {}; |
274
|
262
|
|
|
|
|
554
|
for my $alias (keys %$table_alias) { |
275
|
12
|
|
|
|
|
22
|
my $table = $table_alias->{$alias}; |
276
|
|
|
|
|
|
|
|
277
|
12
|
100
|
|
|
|
18
|
for my $column (keys %{$self->{"_into$i"}{key}{$table} || {}}) { |
|
12
|
|
|
|
|
71
|
|
278
|
6
|
|
|
|
|
33
|
$type_filters->{$i}->{"$alias.$column"} = $self->{"_into$i"}{key}{$table}{$column}; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
} |
281
|
262
|
100
|
|
|
|
505
|
$type_filters->{$i} = {%{$type_filters->{$i}}, %{$self->{"_into$i"}{key}{$main_table} || {}}} |
|
240
|
100
|
|
|
|
465
|
|
|
240
|
|
|
|
|
1442
|
|
282
|
|
|
|
|
|
|
if $main_table; |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
} |
285
|
|
|
|
|
|
|
} |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
# Replace filter name to code |
289
|
3199
|
|
|
|
|
4770
|
my $filter = $opt{filter}; |
290
|
3199
|
100
|
|
|
|
6113
|
if (defined $filter) { |
291
|
42
|
50
|
|
|
|
136
|
if (ref $opt{filter} eq 'ARRAY') { |
292
|
0
|
|
|
|
|
0
|
$filter = _array_to_hash($filter); |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
42
|
|
|
|
|
142
|
for my $column (keys %$filter) { |
296
|
42
|
|
|
|
|
85
|
my $name = $filter->{$column}; |
297
|
42
|
50
|
|
|
|
174
|
if (!defined $name) { |
|
|
100
|
|
|
|
|
|
298
|
0
|
|
|
|
|
0
|
$filter->{$column} = undef; |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
elsif (ref $name ne 'CODE') { |
301
|
|
|
|
|
|
|
confess qq{Filter "$name" is not registered" } . _subname |
302
|
21
|
100
|
|
|
|
421
|
unless exists $self->filters->{$name}; |
303
|
18
|
|
|
|
|
414
|
$filter->{$column} = $self->filters->{$name}; |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
} |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
# Bind type |
309
|
3196
|
|
|
|
|
4484
|
my $bind_type = $opt{bind_type}; |
310
|
3196
|
100
|
|
|
|
6525
|
$bind_type = _array_to_hash($bind_type) if ref $bind_type eq 'ARRAY'; |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
# Create query |
313
|
3196
|
|
|
|
|
10418
|
my $query = DBIx::Custom::Query->new; |
314
|
3196
|
|
|
|
|
83573
|
$query->param($param); |
315
|
3196
|
|
|
|
|
71588
|
$query->sql($parsed_sql); |
316
|
3196
|
|
|
|
|
68639
|
$query->columns($columns); |
317
|
3196
|
|
|
|
|
67009
|
$query->bind_type($bind_type); |
318
|
|
|
|
|
|
|
|
319
|
3196
|
|
|
|
|
20686
|
$query->{_filter} = $filter; |
320
|
3196
|
|
|
|
|
4835
|
$query->{_type_filters} = $type_filters; |
321
|
3196
|
|
|
|
|
5392
|
$query->{_into1} = $self->{_into1}; |
322
|
3196
|
|
|
|
|
8421
|
$query->{_into2} = $self->{_into2}; |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
# Has filter |
325
|
3196
|
|
100
|
|
|
13008
|
$query->{_f} = defined $filter || defined $type_filters; |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
# Build bind values |
328
|
3196
|
|
|
|
|
10183
|
$query->build; |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
# Statement handle |
331
|
3196
|
|
|
|
|
5529
|
my $sth; |
332
|
|
|
|
|
|
|
my $reuse_sth; |
333
|
3196
|
100
|
|
|
|
6366
|
$reuse_sth = $opt{reuse}->{$parsed_sql} if $opt{reuse}; |
334
|
3196
|
100
|
|
|
|
5400
|
if ($reuse_sth) { |
335
|
18
|
|
|
|
|
31
|
$sth = $reuse_sth; |
336
|
|
|
|
|
|
|
} |
337
|
|
|
|
|
|
|
else { |
338
|
|
|
|
|
|
|
# Prepare statement handle |
339
|
3178
|
|
|
|
|
4688
|
eval { $sth = $self->dbh->prepare($parsed_sql) }; |
|
3178
|
|
|
|
|
6627
|
|
340
|
3178
|
100
|
|
|
|
261217
|
if ($@) { |
341
|
582
|
|
|
|
|
3047
|
$self->_confess($@, qq{. Following SQL is executed.\n} |
342
|
|
|
|
|
|
|
. qq{$parsed_sql\n} . _subname); |
343
|
|
|
|
|
|
|
} |
344
|
2596
|
100
|
|
|
|
7067
|
$opt{reuse}->{$parsed_sql} = $sth if $opt{reuse}; |
345
|
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
# Execute |
348
|
2614
|
|
|
|
|
3679
|
my $affected; |
349
|
2614
|
|
|
|
|
4140
|
eval { |
350
|
2614
|
|
|
|
|
64455
|
my $bind_values = $query->bind_values; |
351
|
2614
|
100
|
|
|
|
18434
|
if ($bind_type) { |
352
|
4
|
|
|
|
|
67
|
my $bind_value_types = $query->bind_value_types; |
353
|
|
|
|
|
|
|
$sth->bind_param($_ + 1, $bind_values->[$_], |
354
|
|
|
|
|
|
|
$bind_value_types->[$_] ? $bind_value_types->[$_] : ()) |
355
|
4
|
100
|
|
|
|
70
|
for (0 .. @$bind_values - 1); |
356
|
4
|
|
|
|
|
91
|
$affected = $sth->execute; |
357
|
|
|
|
|
|
|
} |
358
|
2610
|
|
|
|
|
93583
|
else { $affected = $sth->execute(@$bind_values) } |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
# Save sql |
361
|
2611
|
|
|
|
|
10463
|
$self->{last_sql} = $parsed_sql; |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
# DEBUG message |
364
|
2611
|
100
|
|
|
|
7970
|
if ($ENV{DBIX_CUSTOM_DEBUG}) { |
365
|
2
|
|
|
|
|
33
|
warn "SQL:\n" . $parsed_sql . "\n"; |
366
|
2
|
|
|
|
|
16
|
my @output; |
367
|
2
|
|
|
|
|
8
|
for my $value (@$bind_values) { |
368
|
0
|
0
|
|
|
|
0
|
$value = 'undef' unless defined $value; |
369
|
0
|
0
|
0
|
|
|
0
|
$value = encode($ENV{DBIX_CUSTOM_DEBUG_ENCODING} || 'UTF-8', $value) |
370
|
|
|
|
|
|
|
if utf8::is_utf8($value); |
371
|
0
|
|
|
|
|
0
|
push @output, $value; |
372
|
|
|
|
|
|
|
} |
373
|
2
|
|
|
|
|
14
|
warn "Bind values: " . join(', ', @output) . "\n\n"; |
374
|
|
|
|
|
|
|
} |
375
|
|
|
|
|
|
|
}; |
376
|
|
|
|
|
|
|
|
377
|
2614
|
100
|
|
|
|
5514
|
$self->_confess($@, qq{. Following SQL is executed.\n} |
378
|
|
|
|
|
|
|
. qq{$parsed_sql\n} . _subname) if $@; |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
# Reulst of select statement |
381
|
2611
|
100
|
|
|
|
21870
|
if ($sth->{NUM_OF_FIELDS}) { |
382
|
|
|
|
|
|
|
# Result |
383
|
921
|
|
|
|
|
19641
|
my $result = $self->result_class->new( |
384
|
|
|
|
|
|
|
sth => $sth, |
385
|
|
|
|
|
|
|
dbi => $self, |
386
|
|
|
|
|
|
|
); |
387
|
|
|
|
|
|
|
|
388
|
921
|
100
|
|
|
|
16157
|
if ($self->{_type_rule_is_called}) { |
389
|
|
|
|
|
|
|
$result->type_rule({ |
390
|
|
|
|
|
|
|
from1 => $self->type_rule->{from1}, |
391
|
|
|
|
|
|
|
from2 => $self->type_rule->{from2} |
392
|
94
|
|
|
|
|
264
|
}); |
393
|
94
|
|
|
|
|
163
|
$result->{_has_filter} = 1; |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
921
|
|
|
|
|
10018
|
return $result; |
397
|
|
|
|
|
|
|
} |
398
|
|
|
|
|
|
|
# Affected of insert, update, or delete |
399
|
|
|
|
|
|
|
else { |
400
|
1690
|
|
|
|
|
18473
|
return $affected |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
sub include_model { |
405
|
60
|
|
|
60
|
1
|
1888
|
my ($self, $name_space, $model_infos) = @_; |
406
|
|
|
|
|
|
|
|
407
|
|
|
|
|
|
|
# Name space |
408
|
60
|
|
50
|
|
|
191
|
$name_space ||= ''; |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
# Get Model information |
411
|
60
|
100
|
|
|
|
168
|
unless ($model_infos) { |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
# Load name space module |
414
|
30
|
50
|
|
|
|
149
|
confess qq{"$name_space" is invalid class name } . _subname |
415
|
|
|
|
|
|
|
if $name_space =~ /[^\w:]/; |
416
|
30
|
|
|
3
|
|
2484
|
eval "use $name_space"; |
|
3
|
|
|
3
|
|
1145
|
|
|
3
|
|
|
3
|
|
576
|
|
|
3
|
|
|
3
|
|
36
|
|
|
3
|
|
|
3
|
|
1647
|
|
|
3
|
|
|
3
|
|
606
|
|
|
3
|
|
|
3
|
|
35
|
|
|
3
|
|
|
3
|
|
1378
|
|
|
3
|
|
|
3
|
|
530
|
|
|
3
|
|
|
3
|
|
34
|
|
|
3
|
|
|
|
|
25
|
|
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
29
|
|
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
26
|
|
|
3
|
|
|
|
|
28
|
|
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
20
|
|
|
3
|
|
|
|
|
29
|
|
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
22
|
|
|
3
|
|
|
|
|
1779
|
|
|
3
|
|
|
|
|
672
|
|
|
3
|
|
|
|
|
37
|
|
|
3
|
|
|
|
|
25
|
|
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
15
|
|
|
3
|
|
|
|
|
1379
|
|
|
3
|
|
|
|
|
547
|
|
|
3
|
|
|
|
|
33
|
|
417
|
30
|
50
|
|
|
|
558
|
confess qq{Name space module "$name_space.pm" is needed. $@ } . _subname |
418
|
|
|
|
|
|
|
if $@; |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
# Search model modules |
421
|
30
|
|
|
|
|
75
|
my $name_space_dir = $name_space; |
422
|
30
|
|
|
|
|
102
|
$name_space_dir =~ s/::/\//g; |
423
|
30
|
|
|
|
|
103
|
my $path = $INC{"$name_space_dir.pm"}; |
424
|
30
|
|
|
|
|
166
|
$path =~ s/\.pm$//; |
425
|
30
|
50
|
|
|
|
1921
|
opendir my $dh, $path |
426
|
|
|
|
|
|
|
or confess qq{Can't open directory "$path": $! } . _subname |
427
|
|
|
|
|
|
|
my @modules; |
428
|
30
|
|
|
|
|
1010
|
while (my $file = readdir $dh) { |
429
|
132
|
|
|
|
|
369
|
my $file_abs = "$path/$file"; |
430
|
132
|
100
|
|
|
|
2686
|
if (-d $file_abs) { |
|
|
50
|
|
|
|
|
|
431
|
80
|
100
|
100
|
|
|
672
|
next if $file eq '.' || $file eq '..'; |
432
|
20
|
50
|
|
|
|
551
|
opendir my $fq_dh, $file_abs |
433
|
|
|
|
|
|
|
or confess qq{Can't open directory "$file_abs": $! } . _subname; |
434
|
20
|
|
|
|
|
344
|
while (my $fq_file = readdir $fq_dh) { |
435
|
92
|
|
|
|
|
260
|
my $fq_file_abs = "$file_abs/$fq_file"; |
436
|
92
|
100
|
|
|
|
1701
|
push @modules, "${file}::$fq_file" if -f $fq_file_abs; |
437
|
|
|
|
|
|
|
} |
438
|
20
|
|
|
|
|
377
|
close $fq_dh; |
439
|
|
|
|
|
|
|
} |
440
|
52
|
|
|
|
|
448
|
elsif(-f $file_abs) { push @modules, $file } |
441
|
|
|
|
|
|
|
} |
442
|
30
|
|
|
|
|
111
|
close $dh; |
443
|
|
|
|
|
|
|
|
444
|
30
|
|
|
|
|
76
|
$model_infos = []; |
445
|
30
|
|
|
|
|
91
|
for my $module (@modules) { |
446
|
600
|
100
|
|
|
|
1416
|
if ($module =~ s/\.pm$//) { push @$model_infos, $module } |
|
104
|
|
|
|
|
663
|
|
447
|
|
|
|
|
|
|
} |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
# Include models |
451
|
60
|
|
|
|
|
187
|
for my $model_info (@$model_infos) { |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
# Load model |
454
|
164
|
|
|
|
|
428
|
my $model_class; |
455
|
|
|
|
|
|
|
my $model_name; |
456
|
164
|
|
|
|
|
0
|
my $model_table; |
457
|
164
|
100
|
|
|
|
412
|
if (ref $model_info eq 'HASH') { |
458
|
9
|
|
|
|
|
18
|
$model_class = $model_info->{class}; |
459
|
9
|
|
|
|
|
23
|
$model_name = $model_info->{name}; |
460
|
9
|
|
|
|
|
14
|
$model_table = $model_info->{table}; |
461
|
|
|
|
|
|
|
|
462
|
9
|
|
33
|
|
|
25
|
$model_name ||= $model_class; |
463
|
9
|
|
33
|
|
|
47
|
$model_table ||= $model_name; |
464
|
|
|
|
|
|
|
} |
465
|
|
|
|
|
|
|
else { |
466
|
155
|
|
|
|
|
320
|
$model_class = $model_name = $model_table = $model_info; |
467
|
|
|
|
|
|
|
} |
468
|
|
|
|
|
|
|
|
469
|
164
|
|
|
|
|
379
|
$model_class =~ s/\./::/g; |
470
|
164
|
|
|
|
|
365
|
$model_name =~ s/::/./; |
471
|
164
|
|
|
|
|
369
|
$model_table =~ s/::/./; |
472
|
|
|
|
|
|
|
|
473
|
164
|
|
|
|
|
452
|
my $mclass = "${name_space}::$model_class"; |
474
|
164
|
50
|
|
|
|
569
|
confess qq{"$mclass" is invalid class name } . _subname |
475
|
|
|
|
|
|
|
if $mclass =~ /[^\w:]/; |
476
|
164
|
100
|
|
|
|
1293
|
unless ($mclass->can('new')) { |
477
|
54
|
|
|
|
|
3468
|
eval "require $mclass"; |
478
|
54
|
50
|
|
|
|
23144
|
confess "$@ " . _subname if $@; |
479
|
|
|
|
|
|
|
} |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
# Create model |
482
|
164
|
|
|
|
|
372
|
my $opt = {}; |
483
|
164
|
50
|
|
|
|
541
|
$opt->{model_class} = $mclass if $mclass; |
484
|
164
|
50
|
|
|
|
426
|
$opt->{name} = $model_name if $model_name; |
485
|
164
|
50
|
|
|
|
376
|
$opt->{table} = $model_table if $model_table; |
486
|
|
|
|
|
|
|
|
487
|
164
|
|
|
|
|
525
|
$self->create_model($opt); |
488
|
164
|
|
|
|
|
1370
|
1; |
489
|
|
|
|
|
|
|
} |
490
|
|
|
|
|
|
|
|
491
|
60
|
|
|
|
|
201
|
return $self; |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
|
494
|
3
|
|
|
3
|
1
|
19
|
sub like_value { sub { "%$_[0]%" } } |
|
3
|
|
|
3
|
|
43
|
|
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
sub mapper { |
497
|
81
|
|
|
81
|
1
|
17761
|
my $self = shift; |
498
|
81
|
|
|
|
|
329
|
return DBIx::Custom::Mapper->new(@_); |
499
|
|
|
|
|
|
|
} |
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
sub merge_param { |
502
|
818
|
|
|
818
|
1
|
1784
|
my ($self, $param1, $param2) = @_; |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
# Merge parameters |
505
|
818
|
|
|
|
|
1732
|
my $merged_param = {%$param1}; |
506
|
818
|
|
|
|
|
1846
|
for my $column (keys %$param2) { |
507
|
384
|
100
|
|
|
|
779
|
if (exists $merged_param->{$column}) { |
508
|
|
|
|
|
|
|
$merged_param->{$column} = [$merged_param->{$column}] |
509
|
51
|
100
|
|
|
|
166
|
unless ref $merged_param->{$column} eq 'ARRAY'; |
510
|
51
|
|
|
|
|
196
|
push @{$merged_param->{$column}}, |
511
|
51
|
100
|
|
|
|
80
|
ref $param2->{$column} eq 'ARRAY' ? @{$param2->{$column}} : $param2->{$column}; |
|
6
|
|
|
|
|
15
|
|
512
|
|
|
|
|
|
|
} |
513
|
333
|
|
|
|
|
700
|
else { $merged_param->{$column} = $param2->{$column} } |
514
|
|
|
|
|
|
|
} |
515
|
|
|
|
|
|
|
|
516
|
818
|
|
|
|
|
2018
|
return $merged_param; |
517
|
|
|
|
|
|
|
} |
518
|
|
|
|
|
|
|
|
519
|
|
|
|
|
|
|
sub model { |
520
|
601
|
|
|
601
|
1
|
13342
|
my ($self, $name, $model) = @_; |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
# Set model |
523
|
601
|
100
|
|
|
|
1252
|
if ($model) { |
524
|
223
|
|
|
|
|
4003
|
$self->models->{$name} = $model; |
525
|
223
|
|
|
|
|
1189
|
return $self; |
526
|
|
|
|
|
|
|
} |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
# Check model existence |
529
|
|
|
|
|
|
|
confess qq{Model "$name" is not yet created } . _subname |
530
|
378
|
50
|
|
|
|
6502
|
unless $self->models->{$name}; |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
# Get model |
533
|
378
|
|
|
|
|
8345
|
return $self->models->{$name}; |
534
|
|
|
|
|
|
|
} |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
sub mycolumn { |
537
|
33
|
|
|
33
|
1
|
218
|
my ($self, $table, $columns) = @_; |
538
|
|
|
|
|
|
|
|
539
|
33
|
100
|
66
|
|
|
173
|
if (!$columns || $columns eq '*') { |
540
|
9
|
|
|
|
|
25
|
$columns = $self->model($table)->columns; |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
# Create column clause |
544
|
33
|
|
|
|
|
281
|
my @column; |
545
|
|
|
|
|
|
|
push @column, $self->_tq($table) . "." . $self->q($_) . " as " . $self->q($_) |
546
|
33
|
|
|
|
|
117
|
for @$columns; |
547
|
|
|
|
|
|
|
|
548
|
33
|
|
|
|
|
527
|
return join (', ', @column); |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
sub new { |
552
|
524
|
|
|
524
|
1
|
98607
|
my $self = shift; |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
# Same as DBI connect argument |
555
|
524
|
100
|
66
|
|
|
2195
|
if (@_ > 0 && !ref $_[0] && $_[0] =~ /:/) { |
|
|
|
100
|
|
|
|
|
556
|
3
|
|
|
|
|
7
|
my $dsn = shift; |
557
|
3
|
|
|
|
|
6
|
my $user = shift; |
558
|
3
|
|
|
|
|
6
|
my $password = shift; |
559
|
3
|
|
50
|
|
|
10
|
my $dbi_option = shift || {}; |
560
|
3
|
|
50
|
|
|
9
|
my $attrs = shift || {}; |
561
|
3
|
|
|
|
|
8
|
$attrs->{dsn} = $dsn; |
562
|
3
|
|
|
|
|
7
|
$attrs->{user} = $user; |
563
|
3
|
|
|
|
|
6
|
$attrs->{password} = $password; |
564
|
3
|
|
|
|
|
6
|
$attrs->{option} = $dbi_option; |
565
|
3
|
|
|
|
|
10
|
$self = $self->SUPER::new($attrs); |
566
|
|
|
|
|
|
|
} |
567
|
|
|
|
|
|
|
else { |
568
|
521
|
|
|
|
|
1795
|
$self = $self->SUPER::new(@_); |
569
|
|
|
|
|
|
|
} |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
# Check attributes |
572
|
524
|
|
|
|
|
4334
|
my @attrs = keys %$self; |
573
|
524
|
|
|
|
|
1186
|
for my $attr (@attrs) { |
574
|
34
|
50
|
|
|
|
133
|
confess qq{Invalid attribute: "$attr" } . _subname |
575
|
|
|
|
|
|
|
unless $self->can($attr); |
576
|
|
|
|
|
|
|
} |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
$self->{safety_character} = 'a-zA-Z0-9_' |
579
|
524
|
50
|
|
|
|
1818
|
unless exists $self->{safety_character}; |
580
|
|
|
|
|
|
|
|
581
|
524
|
|
|
|
|
1044
|
return $self; |
582
|
|
|
|
|
|
|
} |
583
|
|
|
|
|
|
|
|
584
|
60
|
|
|
60
|
1
|
3137
|
sub not_exists { DBIx::Custom::NotExists->singleton } |
585
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
sub order { |
587
|
3
|
|
|
3
|
1
|
46
|
my $self = shift; |
588
|
3
|
|
|
|
|
48
|
return DBIx::Custom::Order->new(dbi => $self, @_); |
589
|
|
|
|
|
|
|
} |
590
|
|
|
|
|
|
|
|
591
|
640
|
|
|
640
|
1
|
1706
|
sub q { shift->_tq($_[0], $_[1], whole => 1) } |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
sub _tq { |
594
|
2714
|
|
|
2714
|
|
6872
|
my ($self, $value, $quotemeta, %opt) = @_; |
595
|
|
|
|
|
|
|
|
596
|
2714
|
|
50
|
|
|
8044
|
my $quote = $self->{quote} || $self->quote || ''; |
597
|
|
|
|
|
|
|
|
598
|
2714
|
|
50
|
|
|
9346
|
my $q = substr($quote, 0, 1) || ''; |
599
|
2714
|
|
|
|
|
3441
|
my $p; |
600
|
2714
|
100
|
66
|
|
|
8572
|
if (defined $quote && length $quote > 1) { |
601
|
927
|
|
|
|
|
1584
|
$p = substr($quote, 1, 1); |
602
|
|
|
|
|
|
|
} |
603
|
1787
|
|
|
|
|
2612
|
else { $p = $q } |
604
|
|
|
|
|
|
|
|
605
|
2714
|
50
|
|
|
|
4971
|
if ($quotemeta) { |
606
|
0
|
|
|
|
|
0
|
$q = quotemeta($q); |
607
|
0
|
|
|
|
|
0
|
$p = quotemeta($p); |
608
|
|
|
|
|
|
|
} |
609
|
|
|
|
|
|
|
|
610
|
2714
|
100
|
|
|
|
4956
|
if ($opt{whole}) { return "$q$value$p" } |
|
640
|
|
|
|
|
2401
|
|
611
|
|
|
|
|
|
|
else { |
612
|
2074
|
|
|
|
|
6063
|
my @values = split /\./, $value; |
613
|
2074
|
50
|
|
|
|
4638
|
push @values, '' unless @values; |
614
|
2074
|
|
|
|
|
4009
|
for my $v (@values) { $v = "$q$v$p" } |
|
2717
|
|
|
|
|
5505
|
|
615
|
2074
|
|
|
|
|
8661
|
return join '.', @values; |
616
|
|
|
|
|
|
|
} |
617
|
|
|
|
|
|
|
} |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
sub register_filter { |
620
|
36
|
|
|
36
|
1
|
6516
|
my $self = shift; |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
# Register filter |
623
|
36
|
50
|
|
|
|
183
|
my $filters = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
624
|
36
|
|
|
|
|
74
|
$self->filters({%{$self->filters}, %$filters}); |
|
36
|
|
|
|
|
917
|
|
625
|
|
|
|
|
|
|
|
626
|
36
|
|
|
|
|
353
|
return $self; |
627
|
|
|
|
|
|
|
} |
628
|
|
|
|
|
|
|
|
629
|
|
|
|
|
|
|
sub select { |
630
|
742
|
|
|
742
|
1
|
114169
|
my $self = shift; |
631
|
742
|
100
|
|
|
|
2316
|
my $column = shift if @_ % 2; |
632
|
742
|
|
|
|
|
2617
|
my %opt = @_; |
633
|
742
|
100
|
|
|
|
1883
|
$opt{column} = $column if defined $column; |
634
|
|
|
|
|
|
|
|
635
|
|
|
|
|
|
|
# Table |
636
|
742
|
|
|
|
|
1336
|
my $table = $opt{table}; |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
# Found tables; |
639
|
742
|
|
|
|
|
1352
|
my $found_tables = []; |
640
|
742
|
100
|
|
|
|
2245
|
push @$found_tables, $table if defined $table; |
641
|
|
|
|
|
|
|
|
642
|
742
|
|
100
|
|
|
3053
|
my $param = delete $opt{param} || {}; |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
# Select statement |
645
|
742
|
|
|
|
|
1222
|
my $sql = 'select '; |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
# Prefix |
648
|
742
|
100
|
|
|
|
1703
|
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
649
|
|
|
|
|
|
|
|
650
|
|
|
|
|
|
|
# Column |
651
|
742
|
100
|
|
|
|
1549
|
if (defined $opt{column}) { |
652
|
|
|
|
|
|
|
my $columns |
653
|
176
|
100
|
|
|
|
627
|
= ref $opt{column} eq 'ARRAY' ? $opt{column} : [$opt{column}]; |
654
|
176
|
|
|
|
|
416
|
for my $column (@$columns) { |
655
|
233
|
100
|
|
|
|
576
|
if (ref $column eq 'HASH') { |
656
|
52
|
|
66
|
|
|
1078
|
my $mytable_symbol = $opt{mytable_symbol} || $self->mytable_symbol; |
657
|
52
|
|
|
|
|
553
|
my $table = (keys %$column)[0]; |
658
|
52
|
|
|
|
|
108
|
my $columns = $column->{$table}; |
659
|
|
|
|
|
|
|
|
660
|
52
|
100
|
|
|
|
129
|
if ($table eq $mytable_symbol) { |
661
|
9
|
|
|
|
|
26
|
$column = $self->mycolumn($found_tables->[0] => $columns); |
662
|
|
|
|
|
|
|
} |
663
|
|
|
|
|
|
|
else { |
664
|
43
|
|
|
|
|
132
|
$column = $self->column($table => $columns); |
665
|
|
|
|
|
|
|
} |
666
|
|
|
|
|
|
|
} |
667
|
233
|
100
|
|
|
|
538
|
unshift @$found_tables, @{$self->_search_tables($column)} if $table; |
|
226
|
|
|
|
|
536
|
|
668
|
233
|
|
|
|
|
692
|
$sql .= "$column, "; |
669
|
|
|
|
|
|
|
} |
670
|
176
|
|
|
|
|
876
|
$sql =~ s/, $/ /; |
671
|
|
|
|
|
|
|
} |
672
|
566
|
|
|
|
|
1094
|
else { $sql .= '* ' } |
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
# Execute query without table |
675
|
742
|
100
|
|
|
|
1681
|
return $self->execute($sql, {}, %opt) unless $table; |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
# Table |
678
|
735
|
|
|
|
|
1181
|
$sql .= 'from '; |
679
|
735
|
|
50
|
|
|
2267
|
$sql .= $self->_tq($found_tables->[-1] || '') . ' '; |
680
|
735
|
|
|
|
|
1701
|
$sql =~ s/, $/ /; |
681
|
|
|
|
|
|
|
|
682
|
|
|
|
|
|
|
# Add tables in parameter |
683
|
735
|
|
100
|
|
|
1321
|
unshift @$found_tables, @{$self->_search_tables(join(' ', keys %$param) || '')}; |
|
735
|
|
|
|
|
3622
|
|
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
# Where |
686
|
735
|
|
|
|
|
1290
|
my $where; |
687
|
735
|
100
|
|
|
|
1668
|
if (defined $opt{id}) { |
688
|
49
|
50
|
|
|
|
240
|
$where = $self->_id_to_param($opt{id}, $opt{primary_key}, @$found_tables ? $found_tables->[-1] : undef) ; |
689
|
|
|
|
|
|
|
} |
690
|
|
|
|
|
|
|
else { |
691
|
686
|
|
|
|
|
1094
|
$where = $opt{where}; |
692
|
|
|
|
|
|
|
} |
693
|
735
|
|
|
|
|
2365
|
my $w = $self->_where_clause_and_param($where, $opt{id}); |
694
|
729
|
|
|
|
|
2398
|
$param = $self->merge_param($param, $w->{param}); |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
# Search table names in where clause |
697
|
729
|
|
|
|
|
1151
|
unshift @$found_tables, @{$self->_search_tables($w->{clause})}; |
|
729
|
|
|
|
|
1622
|
|
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
# Search table names in append option |
700
|
729
|
100
|
|
|
|
2198
|
if (defined(my $append = $opt{append})) { |
701
|
20
|
|
|
|
|
45
|
unshift @$found_tables, @{$self->_search_tables($append)}; |
|
20
|
|
|
|
|
53
|
|
702
|
|
|
|
|
|
|
} |
703
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
# Join statement |
705
|
729
|
|
|
|
|
1245
|
my $join = []; |
706
|
729
|
100
|
|
|
|
1691
|
if (defined $opt{join}) { |
707
|
133
|
|
|
|
|
228
|
my $opt_join = $opt{join}; |
708
|
133
|
100
|
|
|
|
376
|
if (ref $opt_join eq 'ARRAY') { |
709
|
127
|
|
|
|
|
319
|
push @$join, @$opt_join; |
710
|
|
|
|
|
|
|
} |
711
|
6
|
|
|
|
|
18
|
else { push @$join, $opt_join } |
712
|
|
|
|
|
|
|
} |
713
|
729
|
100
|
|
|
|
1568
|
if (defined $w->{join}) { |
714
|
18
|
|
|
|
|
44
|
my $where_join = $w->{join}; |
715
|
18
|
50
|
|
|
|
43
|
if (ref $where_join eq 'ARRAY') { |
716
|
18
|
|
|
|
|
35
|
push @$join, @$where_join; |
717
|
|
|
|
|
|
|
} |
718
|
0
|
|
|
|
|
0
|
else { push @$join, $where_join } |
719
|
|
|
|
|
|
|
} |
720
|
729
|
100
|
|
|
|
1819
|
$self->_push_join(\$sql, $join, $found_tables) if @$join; |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
# Add where clause |
723
|
729
|
|
|
|
|
1646
|
$sql .= "$w->{clause} "; |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
# Execute query |
726
|
729
|
|
|
|
|
2502
|
return $self->execute($sql, $param, %opt); |
727
|
|
|
|
|
|
|
} |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
sub setup_model { |
730
|
24
|
|
|
24
|
0
|
297
|
my ($self, %opt) = @_; |
731
|
|
|
|
|
|
|
|
732
|
24
|
|
|
|
|
142
|
_deprecate('0.39', "DBIx::Custom::setup method is DEPRECATED! columns is automatically set when create_model or include_model is called"); |
733
|
|
|
|
|
|
|
|
734
|
24
|
|
|
|
|
60
|
return $self; |
735
|
|
|
|
|
|
|
} |
736
|
|
|
|
|
|
|
|
737
|
|
|
|
|
|
|
sub insert { |
738
|
824
|
|
|
824
|
1
|
19539
|
my $self = shift; |
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
# Options |
741
|
824
|
50
|
|
|
|
2298
|
my $params = @_ % 2 ? shift : undef; |
742
|
824
|
|
|
|
|
2626
|
my %opt = @_; |
743
|
824
|
|
50
|
|
|
2120
|
$params ||= {}; |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
# Insert statement |
746
|
824
|
|
|
|
|
1391
|
my $sql = "insert "; |
747
|
824
|
100
|
|
|
|
1977
|
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
748
|
824
|
|
|
|
|
2193
|
$sql .= "into " . $self->_tq($opt{table}) . " "; |
749
|
|
|
|
|
|
|
|
750
|
824
|
|
|
|
|
1379
|
my $multi; |
751
|
824
|
100
|
|
|
|
1985
|
if (ref $params eq 'ARRAY') { $multi = 1 } |
|
9
|
|
|
|
|
16
|
|
752
|
815
|
|
|
|
|
1610
|
else { $params = [$params] } |
753
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
# Created time and updated time |
755
|
824
|
100
|
100
|
|
|
3357
|
if (defined $opt{ctime} || defined $opt{mtime}) { |
756
|
|
|
|
|
|
|
|
757
|
33
|
|
|
|
|
64
|
for my $param (@$params) { |
758
|
36
|
|
|
|
|
136
|
$param = {%$param}; |
759
|
|
|
|
|
|
|
} |
760
|
33
|
|
|
|
|
727
|
my $now = $self->now; |
761
|
33
|
100
|
|
|
|
183
|
$now = $now->() if ref $now eq 'CODE'; |
762
|
33
|
100
|
|
|
|
108
|
if (defined $opt{ctime}) { |
763
|
23
|
|
|
|
|
104
|
$_->{$opt{ctime}} = $now for @$params; |
764
|
|
|
|
|
|
|
} |
765
|
33
|
100
|
|
|
|
79
|
if (defined $opt{mtime}) { |
766
|
27
|
|
|
|
|
89
|
$_->{$opt{mtime}} = $now for @$params; |
767
|
|
|
|
|
|
|
} |
768
|
|
|
|
|
|
|
} |
769
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
# Merge id to parameter |
771
|
824
|
100
|
66
|
|
|
2086
|
if (defined $opt{id} && !$multi) { |
772
|
|
|
|
|
|
|
|
773
|
38
|
|
|
|
|
149
|
_deprecate('0.39', "DBIx::Custom::insert method's id option is DEPRECATED!"); |
774
|
|
|
|
|
|
|
|
775
|
38
|
|
|
|
|
527
|
for my $param (@$params) { |
776
|
38
|
|
|
|
|
161
|
$param = {%$param}; |
777
|
|
|
|
|
|
|
} |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
confess "insert id option must be specified with primary_key option" |
780
|
38
|
50
|
|
|
|
99
|
unless $opt{primary_key}; |
781
|
38
|
100
|
|
|
|
128
|
$opt{primary_key} = [$opt{primary_key}] unless ref $opt{primary_key} eq 'ARRAY'; |
782
|
38
|
100
|
|
|
|
108
|
$opt{id} = [$opt{id}] unless ref $opt{id} eq 'ARRAY'; |
783
|
38
|
|
|
|
|
72
|
for (my $i = 0; $i < @{$opt{primary_key}}; $i++) { |
|
97
|
|
|
|
|
245
|
|
784
|
59
|
|
|
|
|
102
|
my $key = $opt{primary_key}->[$i]; |
785
|
59
|
100
|
|
|
|
134
|
next if exists $params->[0]->{$key}; |
786
|
56
|
|
|
|
|
124
|
$params->[0]->{$key} = $opt{id}->[$i]; |
787
|
|
|
|
|
|
|
} |
788
|
|
|
|
|
|
|
} |
789
|
|
|
|
|
|
|
|
790
|
824
|
50
|
|
|
|
1590
|
if ($opt{bulk_insert}) { |
791
|
0
|
|
|
|
|
0
|
$sql .= $self->_multi_values_clause($params, {wrap => $opt{wrap}}) . " "; |
792
|
0
|
|
|
|
|
0
|
my $new_param = {}; |
793
|
0
|
|
|
|
|
0
|
$new_param->{$_} = [] for keys %{$params->[0]}; |
|
0
|
|
|
|
|
0
|
|
794
|
0
|
|
|
|
|
0
|
for my $param (@$params) { |
795
|
0
|
|
|
|
|
0
|
push @{$new_param->{$_}}, $param->{$_} for keys %$param; |
|
0
|
|
|
|
|
0
|
|
796
|
|
|
|
|
|
|
} |
797
|
0
|
|
|
|
|
0
|
$params = [$new_param]; |
798
|
|
|
|
|
|
|
} |
799
|
|
|
|
|
|
|
else { |
800
|
824
|
|
|
|
|
3068
|
$sql .= $self->values_clause($params->[0], {wrap => $opt{wrap}}) . " "; |
801
|
|
|
|
|
|
|
} |
802
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
# Execute query |
804
|
821
|
100
|
|
|
|
2248
|
if (@$params > 1) { |
805
|
9
|
|
|
|
|
22
|
for my $param (@$params) { |
806
|
18
|
|
|
|
|
66
|
$self->execute($sql, $param, %opt); |
807
|
|
|
|
|
|
|
} |
808
|
|
|
|
|
|
|
} |
809
|
|
|
|
|
|
|
else { |
810
|
812
|
|
|
|
|
2828
|
$self->execute($sql, $params->[0], %opt); |
811
|
|
|
|
|
|
|
} |
812
|
|
|
|
|
|
|
} |
813
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
sub update { |
815
|
89
|
|
|
89
|
1
|
12234
|
my $self = shift; |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
# Options |
818
|
89
|
100
|
|
|
|
276
|
my $param = @_ % 2 ? shift : undef; |
819
|
89
|
|
|
|
|
336
|
my %opt = @_; |
820
|
89
|
|
100
|
|
|
266
|
$param ||= {}; |
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
# Don't allow update all rows |
823
|
|
|
|
|
|
|
confess qq{update method where option must be specified } . _subname |
824
|
89
|
100
|
100
|
|
|
339
|
if !$opt{where} && !defined $opt{id} && !$opt{allow_update_all}; |
|
|
|
100
|
|
|
|
|
825
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
# Created time and updated time |
827
|
86
|
100
|
|
|
|
219
|
if (defined $opt{mtime}) { |
828
|
9
|
|
|
|
|
32
|
$param = {%$param}; |
829
|
9
|
|
|
|
|
202
|
my $now = $self->now; |
830
|
9
|
50
|
|
|
|
44
|
$now = $now->() if ref $now eq 'CODE'; |
831
|
9
|
|
|
|
|
300
|
$param->{$opt{mtime}} = $self->now->(); |
832
|
|
|
|
|
|
|
} |
833
|
|
|
|
|
|
|
|
834
|
|
|
|
|
|
|
# Assign clause |
835
|
86
|
|
|
|
|
327
|
my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}}); |
836
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
# Where |
838
|
83
|
|
|
|
|
166
|
my $where; |
839
|
83
|
100
|
|
|
|
197
|
if (defined $opt{id}) { |
840
|
15
|
|
|
|
|
68
|
$where = $self->_id_to_param($opt{id}, $opt{primary_key}, $opt{table}) ; |
841
|
|
|
|
|
|
|
} |
842
|
|
|
|
|
|
|
else { |
843
|
68
|
|
|
|
|
121
|
$where = $opt{where}; |
844
|
|
|
|
|
|
|
} |
845
|
|
|
|
|
|
|
|
846
|
83
|
|
|
|
|
231
|
my $w = $self->_where_clause_and_param($where); |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
# Merge update parameter with where parameter |
849
|
83
|
|
|
|
|
282
|
$param = $self->merge_param($param, $w->{param}); |
850
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
# Update statement |
852
|
83
|
|
|
|
|
147
|
my $sql = "update "; |
853
|
83
|
50
|
|
|
|
196
|
$sql .= "$opt{prefix} " if defined $opt{prefix}; |
854
|
83
|
|
|
|
|
197
|
$sql .= $self->_tq($opt{table}) . " set $assign_clause $w->{clause} "; |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
# Execute query |
857
|
83
|
|
|
|
|
290
|
$self->execute($sql, $param, %opt); |
858
|
|
|
|
|
|
|
} |
859
|
|
|
|
|
|
|
|
860
|
3
|
|
|
3
|
1
|
30
|
sub update_all { shift->update(@_, allow_update_all => 1) }; |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
sub values_clause { |
863
|
827
|
|
|
827
|
1
|
1876
|
my ($self, $param, $opts) = @_; |
864
|
|
|
|
|
|
|
|
865
|
827
|
|
100
|
|
|
2727
|
my $wrap = $opts->{wrap} || {}; |
866
|
|
|
|
|
|
|
|
867
|
|
|
|
|
|
|
# Create insert parameter tag |
868
|
827
|
|
|
|
|
1989
|
my ($q, $p) = $self->_qp; |
869
|
|
|
|
|
|
|
|
870
|
827
|
|
|
|
|
20014
|
my $safety_character = $self->safety_character; |
871
|
|
|
|
|
|
|
|
872
|
827
|
|
|
|
|
5766
|
my @columns; |
873
|
|
|
|
|
|
|
my @place_holders; |
874
|
827
|
|
|
|
|
5120
|
for my $column (sort keys %$param) { |
875
|
1949
|
100
|
|
|
|
8850
|
confess qq{"$column" is not safety column name in values clause} . _subname |
876
|
|
|
|
|
|
|
unless $column =~ /^[$safety_character\.]+$/; |
877
|
|
|
|
|
|
|
|
878
|
1946
|
|
|
|
|
4657
|
push @columns, "$q$column$p"; |
879
|
35
|
|
|
|
|
83
|
push @place_holders, ref $param->{$column} eq 'SCALAR' ? ${$param->{$column}} : |
880
|
1946
|
100
|
|
|
|
6569
|
$wrap->{$column} ? $wrap->{$column}->(":$column") : |
|
|
100
|
|
|
|
|
|
881
|
|
|
|
|
|
|
":$column"; |
882
|
|
|
|
|
|
|
} |
883
|
|
|
|
|
|
|
|
884
|
824
|
|
|
|
|
2931
|
my $values_clause = '(' . join(', ', @columns) . ') values (' . join(', ', @place_holders) . ')'; |
885
|
|
|
|
|
|
|
|
886
|
824
|
|
|
|
|
3158
|
return $values_clause; |
887
|
|
|
|
|
|
|
} |
888
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
sub assign_clause { |
890
|
95
|
|
|
95
|
1
|
353
|
my ($self, $param, $opts) = @_; |
891
|
|
|
|
|
|
|
|
892
|
95
|
|
100
|
|
|
366
|
my $wrap = $opts->{wrap} || {}; |
893
|
95
|
|
|
|
|
237
|
my ($q, $p) = $self->_qp; |
894
|
|
|
|
|
|
|
|
895
|
95
|
|
|
|
|
2241
|
my $safety_character = $self->safety_character; |
896
|
|
|
|
|
|
|
|
897
|
95
|
|
|
|
|
653
|
my @set_values; |
898
|
95
|
|
|
|
|
414
|
for my $column (sort keys %$param) { |
899
|
113
|
100
|
|
|
|
767
|
confess qq{"$column" is not safety column name in assign clause} . _subname |
900
|
|
|
|
|
|
|
unless $column =~ /^[$safety_character\.]+$/; |
901
|
|
|
|
|
|
|
|
902
|
3
|
|
|
|
|
65
|
push @set_values, ref $param->{$column} eq 'SCALAR' ? "$q$column$p = " . ${$param->{$column}} |
903
|
110
|
100
|
|
|
|
627
|
: $wrap->{$column} ? "$q$column$p = " . $wrap->{$column}->(":$column") |
|
|
100
|
|
|
|
|
|
904
|
|
|
|
|
|
|
: "$q$column$p = :$column"; |
905
|
|
|
|
|
|
|
} |
906
|
|
|
|
|
|
|
|
907
|
92
|
|
|
|
|
281
|
my $assign_clause = join(', ', @set_values); |
908
|
|
|
|
|
|
|
|
909
|
92
|
|
|
|
|
275
|
return $assign_clause; |
910
|
|
|
|
|
|
|
} |
911
|
|
|
|
|
|
|
|
912
|
186
|
|
|
186
|
1
|
105816
|
sub where { DBIx::Custom::Where->new(dbi => shift, @_) } |
913
|
|
|
|
|
|
|
|
914
|
|
|
|
|
|
|
sub type_rule { |
915
|
262
|
|
|
262
|
1
|
6022
|
my $self = shift; |
916
|
|
|
|
|
|
|
|
917
|
262
|
|
|
|
|
477
|
$self->{_type_rule_is_called} = 1; |
918
|
|
|
|
|
|
|
|
919
|
262
|
100
|
|
|
|
570
|
if (@_) { |
920
|
74
|
50
|
|
|
|
295
|
my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
921
|
|
|
|
|
|
|
|
922
|
|
|
|
|
|
|
# Into |
923
|
74
|
|
|
|
|
207
|
for my $i (1 .. 2) { |
924
|
142
|
|
|
|
|
629
|
my $into = "into$i"; |
925
|
142
|
|
|
|
|
265
|
my $exists_into = exists $type_rule->{$into}; |
926
|
142
|
|
|
|
|
500
|
$type_rule->{$into} = _array_to_hash($type_rule->{$into}); |
927
|
142
|
|
|
|
|
328
|
$self->{type_rule} = $type_rule; |
928
|
142
|
|
|
|
|
359
|
$self->{"_$into"} = {}; |
929
|
142
|
100
|
|
|
|
601
|
for my $type_name (keys %{$type_rule->{$into} || {}}) { |
|
142
|
|
|
|
|
666
|
|
930
|
78
|
100
|
|
|
|
566
|
confess qq{type name of $into section must be lower case} |
931
|
|
|
|
|
|
|
if $type_name =~ /[A-Z]/; |
932
|
|
|
|
|
|
|
} |
933
|
|
|
|
|
|
|
|
934
|
|
|
|
|
|
|
$self->each_column(sub { |
935
|
1909
|
|
|
1909
|
|
3378
|
my ($dbi, $table, $column, $column_info) = @_; |
936
|
|
|
|
|
|
|
|
937
|
1909
|
|
|
|
|
2944
|
my $type_name = lc $column_info->{TYPE_NAME}; |
938
|
1909
|
100
|
100
|
|
|
7765
|
if ($type_rule->{$into} && |
939
|
|
|
|
|
|
|
(my $filter = $type_rule->{$into}->{$type_name})) |
940
|
|
|
|
|
|
|
{ |
941
|
75
|
50
|
|
|
|
179
|
return unless exists $type_rule->{$into}->{$type_name}; |
942
|
75
|
100
|
66
|
|
|
308
|
if (defined $filter && ref $filter ne 'CODE') |
943
|
|
|
|
|
|
|
{ |
944
|
9
|
|
|
|
|
17
|
my $fname = $filter; |
945
|
|
|
|
|
|
|
confess qq{Filter "$fname" is not registered" } . _subname |
946
|
9
|
100
|
|
|
|
189
|
unless exists $self->filters->{$fname}; |
947
|
|
|
|
|
|
|
|
948
|
6
|
|
|
|
|
140
|
$filter = $self->filters->{$fname}; |
949
|
|
|
|
|
|
|
} |
950
|
|
|
|
|
|
|
|
951
|
72
|
|
|
|
|
159
|
my $schema = $column_info->{TABLE_SCHEM}; |
952
|
72
|
|
|
|
|
279
|
$self->{"_$into"}{key}{$table}{$column} = $filter; |
953
|
72
|
|
|
|
|
260
|
$self->{"_$into"}{dot}{"$table.$column"} = $filter; |
954
|
|
|
|
|
|
|
|
955
|
72
|
|
|
|
|
233
|
$self->{"_$into"}{key}{"$schema.$table"}{$column} = $filter; |
956
|
72
|
|
|
|
|
334
|
$self->{"_$into"}{dot}{"$schema.$table.$column"} = $filter; |
957
|
|
|
|
|
|
|
} |
958
|
139
|
|
|
|
|
932
|
}); |
959
|
|
|
|
|
|
|
} |
960
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
# From |
962
|
68
|
|
|
|
|
420
|
for my $i (1 .. 2) { |
963
|
133
|
|
|
|
|
492
|
$type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"}); |
964
|
133
|
100
|
|
|
|
236
|
for my $data_type (keys %{$type_rule->{"from$i"} || {}}) { |
|
133
|
|
|
|
|
591
|
|
965
|
65
|
100
|
|
|
|
574
|
confess qq{data type of from$i section must be lower case or number} |
966
|
|
|
|
|
|
|
if $data_type =~ /[A-Z]/; |
967
|
62
|
|
|
|
|
142
|
my $fname = $type_rule->{"from$i"}{$data_type}; |
968
|
62
|
100
|
66
|
|
|
306
|
if (defined $fname && ref $fname ne 'CODE') { |
969
|
|
|
|
|
|
|
confess qq{Filter "$fname" is not registered" } . _subname |
970
|
3
|
50
|
|
|
|
65
|
unless exists $self->filters->{$fname}; |
971
|
|
|
|
|
|
|
|
972
|
3
|
|
|
|
|
75
|
$type_rule->{"from$i"}{$data_type} = $self->filters->{$fname}; |
973
|
|
|
|
|
|
|
} |
974
|
|
|
|
|
|
|
} |
975
|
|
|
|
|
|
|
} |
976
|
|
|
|
|
|
|
|
977
|
65
|
|
|
|
|
174
|
return $self; |
978
|
|
|
|
|
|
|
} |
979
|
|
|
|
|
|
|
|
980
|
188
|
|
50
|
|
|
781
|
return $self->{type_rule} || {}; |
981
|
|
|
|
|
|
|
} |
982
|
|
|
|
|
|
|
|
983
|
|
|
|
|
|
|
sub get_table_info { |
984
|
3
|
|
|
3
|
1
|
142
|
my ($self, %opt) = @_; |
985
|
|
|
|
|
|
|
|
986
|
3
|
|
|
|
|
12
|
my $exclude = delete $opt{exclude}; |
987
|
3
|
|
|
|
|
16
|
confess qq/"$_" is wrong option/ for keys %opt; |
988
|
|
|
|
|
|
|
|
989
|
3
|
|
|
|
|
9
|
my $table_info = []; |
990
|
|
|
|
|
|
|
$self->each_table( |
991
|
15
|
|
|
15
|
|
239
|
sub { push @$table_info, {table => $_[1], info => $_[2] } }, |
992
|
3
|
|
|
|
|
26
|
exclude => $exclude |
993
|
|
|
|
|
|
|
); |
994
|
|
|
|
|
|
|
|
995
|
3
|
|
|
|
|
50
|
return [sort {$a->{table} cmp $b->{table} } @$table_info]; |
|
24
|
|
|
|
|
62
|
|
996
|
|
|
|
|
|
|
} |
997
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
sub get_column_info { |
999
|
3
|
|
|
3
|
1
|
3603
|
my ($self, %opt) = @_; |
1000
|
|
|
|
|
|
|
|
1001
|
3
|
|
|
|
|
9
|
my $exclude_table = delete $opt{exclude_table}; |
1002
|
3
|
|
|
|
|
12
|
confess qq/"$_" is wrong option/ for keys %opt; |
1003
|
|
|
|
|
|
|
|
1004
|
3
|
|
|
|
|
11
|
my $column_info = []; |
1005
|
|
|
|
|
|
|
$self->each_column( |
1006
|
42
|
|
|
42
|
|
313
|
sub { push @$column_info, {table => $_[1], column => $_[2], info => $_[3] } }, |
1007
|
3
|
|
|
|
|
26
|
exclude_table => $exclude_table |
1008
|
|
|
|
|
|
|
); |
1009
|
|
|
|
|
|
|
|
1010
|
|
|
|
|
|
|
return [ |
1011
|
3
|
50
|
|
|
|
156
|
sort {$a->{table} cmp $b->{table} || $a->{column} cmp $b->{column} } |
|
105
|
|
|
|
|
220
|
|
1012
|
|
|
|
|
|
|
@$column_info]; |
1013
|
|
|
|
|
|
|
} |
1014
|
|
|
|
|
|
|
|
1015
|
|
|
|
|
|
|
sub each_column { |
1016
|
145
|
|
|
145
|
1
|
478
|
my ($self, $cb, %options) = @_; |
1017
|
|
|
|
|
|
|
|
1018
|
145
|
|
|
|
|
3018
|
my $user_column_info = $self->user_column_info; |
1019
|
|
|
|
|
|
|
|
1020
|
145
|
100
|
|
|
|
1040
|
if ($user_column_info) { |
1021
|
129
|
|
|
|
|
481
|
$self->$cb($_->{table}, $_->{column}, $_->{info}) for @$user_column_info; |
1022
|
|
|
|
|
|
|
} |
1023
|
|
|
|
|
|
|
else { |
1024
|
16
|
|
33
|
|
|
291
|
my $re = $self->exclude_table || $options{exclude_table}; |
1025
|
|
|
|
|
|
|
# Tables |
1026
|
16
|
|
|
|
|
161
|
my $tables = {}; |
1027
|
|
|
|
|
|
|
$self->each_table(sub { |
1028
|
53
|
|
|
53
|
|
112
|
my ($dbi, $table, $table_info) = @_; |
1029
|
53
|
|
|
|
|
96
|
my $schema = $table_info->{TABLE_SCHEM}; |
1030
|
53
|
|
|
|
|
707
|
$tables->{$schema}{$table}++; |
1031
|
16
|
|
|
|
|
105
|
}); |
1032
|
|
|
|
|
|
|
|
1033
|
|
|
|
|
|
|
# Iterate all tables |
1034
|
16
|
|
|
|
|
250
|
for my $schema (sort keys %$tables) { |
1035
|
32
|
|
|
|
|
793
|
for my $table (sort keys %{$tables->{$schema}}) { |
|
32
|
|
|
|
|
129
|
|
1036
|
|
|
|
|
|
|
|
1037
|
|
|
|
|
|
|
# Iterate all columns |
1038
|
53
|
|
|
|
|
1088
|
my $sth_columns; |
1039
|
53
|
|
|
|
|
117
|
eval {$sth_columns = $self->dbh->column_info(undef, $schema, $table, '%')}; |
|
53
|
|
|
|
|
157
|
|
1040
|
53
|
50
|
|
|
|
44540
|
next if $@; |
1041
|
53
|
|
|
|
|
470
|
while (my $column_info = $sth_columns->fetchrow_hashref) { |
1042
|
196
|
|
|
|
|
4147
|
my $column = $column_info->{COLUMN_NAME}; |
1043
|
196
|
|
|
|
|
409
|
$self->$cb($table, $column, $column_info); |
1044
|
|
|
|
|
|
|
} |
1045
|
|
|
|
|
|
|
} |
1046
|
|
|
|
|
|
|
} |
1047
|
|
|
|
|
|
|
} |
1048
|
|
|
|
|
|
|
} |
1049
|
|
|
|
|
|
|
|
1050
|
|
|
|
|
|
|
sub get_columns_from_db { |
1051
|
226
|
|
|
226
|
0
|
2123
|
my ($self, $schema_table, $opt) = @_; |
1052
|
|
|
|
|
|
|
|
1053
|
226
|
|
100
|
|
|
534
|
$opt ||= {}; |
1054
|
|
|
|
|
|
|
|
1055
|
226
|
|
|
|
|
382
|
my $column_name_lc = $opt->{column_name_lc}; |
1056
|
|
|
|
|
|
|
|
1057
|
226
|
|
|
|
|
384
|
my $schema; |
1058
|
|
|
|
|
|
|
my $table; |
1059
|
226
|
100
|
|
|
|
801
|
if ($schema_table =~ /^(.+)\.(.*)$/) { |
1060
|
90
|
|
|
|
|
198
|
$schema = $1; |
1061
|
90
|
|
|
|
|
168
|
$table = $2; |
1062
|
|
|
|
|
|
|
} |
1063
|
|
|
|
|
|
|
else { |
1064
|
136
|
|
|
|
|
228
|
$schema = undef; |
1065
|
136
|
|
|
|
|
195
|
$table = $schema_table; |
1066
|
|
|
|
|
|
|
} |
1067
|
|
|
|
|
|
|
|
1068
|
226
|
|
|
|
|
325
|
my $sth_columns; |
1069
|
226
|
|
|
|
|
396
|
eval {$sth_columns = $self->dbh->column_info(undef, $schema, $table, "%") }; |
|
226
|
|
|
|
|
536
|
|
1070
|
226
|
50
|
|
|
|
186385
|
if ($@) { |
1071
|
0
|
|
|
|
|
0
|
return; |
1072
|
|
|
|
|
|
|
} |
1073
|
|
|
|
|
|
|
|
1074
|
226
|
|
|
|
|
405
|
my $columns; |
1075
|
226
|
|
|
|
|
2104
|
while (my $column_info = $sth_columns->fetchrow_hashref) { |
1076
|
355
|
|
100
|
|
|
8305
|
$columns ||= []; |
1077
|
355
|
|
|
|
|
679
|
my $column = $column_info->{COLUMN_NAME}; |
1078
|
355
|
100
|
|
|
|
665
|
if ($column_name_lc) { |
1079
|
4
|
|
|
|
|
11
|
$column = lc $column; |
1080
|
|
|
|
|
|
|
} |
1081
|
355
|
|
|
|
|
2906
|
push @$columns, $column; |
1082
|
|
|
|
|
|
|
} |
1083
|
|
|
|
|
|
|
|
1084
|
226
|
|
|
|
|
15937
|
return $columns; |
1085
|
|
|
|
|
|
|
} |
1086
|
|
|
|
|
|
|
|
1087
|
|
|
|
|
|
|
sub each_table { |
1088
|
25
|
|
|
25
|
1
|
280
|
my ($self, $cb, %option) = @_; |
1089
|
|
|
|
|
|
|
|
1090
|
25
|
|
|
|
|
477
|
my $user_table_infos = $self->user_table_info; |
1091
|
|
|
|
|
|
|
|
1092
|
|
|
|
|
|
|
# Iterate tables |
1093
|
25
|
100
|
|
|
|
193
|
if ($user_table_infos) { |
1094
|
6
|
|
|
|
|
34
|
$self->$cb($_->{table}, $_->{info}) for @$user_table_infos; |
1095
|
|
|
|
|
|
|
} |
1096
|
|
|
|
|
|
|
else { |
1097
|
19
|
|
33
|
|
|
320
|
my $re = $self->exclude_table || $option{exclude}; |
1098
|
19
|
|
|
|
|
199
|
my $sth_tables = $self->dbh->table_info; |
1099
|
19
|
|
|
|
|
6063
|
while (my $table_info = $sth_tables->fetchrow_hashref) { |
1100
|
|
|
|
|
|
|
# Table |
1101
|
65
|
|
|
|
|
359
|
my $table = $table_info->{TABLE_NAME}; |
1102
|
65
|
50
|
33
|
|
|
157
|
next if defined $re && $table =~ /$re/; |
1103
|
65
|
|
|
|
|
176
|
$self->$cb($table, $table_info); |
1104
|
|
|
|
|
|
|
} |
1105
|
|
|
|
|
|
|
} |
1106
|
|
|
|
|
|
|
} |
1107
|
|
|
|
|
|
|
|
1108
|
|
|
|
|
|
|
sub available_datatype { |
1109
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1110
|
|
|
|
|
|
|
|
1111
|
0
|
|
|
|
|
0
|
my $data_types = ''; |
1112
|
0
|
|
|
|
|
0
|
for my $i (-1000 .. 1000) { |
1113
|
0
|
|
|
|
|
0
|
my $type_info = $self->dbh->type_info($i); |
1114
|
0
|
|
|
|
|
0
|
my $data_type = $type_info->{DATA_TYPE}; |
1115
|
0
|
|
|
|
|
0
|
my $type_name = $type_info->{TYPE_NAME}; |
1116
|
0
|
0
|
|
|
|
0
|
$data_types .= "$data_type ($type_name)\n" |
1117
|
|
|
|
|
|
|
if defined $data_type; |
1118
|
|
|
|
|
|
|
} |
1119
|
0
|
0
|
|
|
|
0
|
return "Data Type maybe equal to Type Name" unless $data_types; |
1120
|
0
|
|
|
|
|
0
|
$data_types = "Data Type (Type name)\n" . $data_types; |
1121
|
0
|
|
|
|
|
0
|
return $data_types; |
1122
|
|
|
|
|
|
|
} |
1123
|
|
|
|
|
|
|
|
1124
|
|
|
|
|
|
|
sub available_typename { |
1125
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1126
|
|
|
|
|
|
|
|
1127
|
|
|
|
|
|
|
# Type Names |
1128
|
0
|
|
|
|
|
0
|
my $type_names = {}; |
1129
|
|
|
|
|
|
|
$self->each_column(sub { |
1130
|
0
|
|
|
0
|
|
0
|
my ($self, $table, $column, $column_info) = @_; |
1131
|
|
|
|
|
|
|
$type_names->{$column_info->{TYPE_NAME}} = 1 |
1132
|
0
|
0
|
|
|
|
0
|
if $column_info->{TYPE_NAME}; |
1133
|
0
|
|
|
|
|
0
|
}); |
1134
|
0
|
|
|
|
|
0
|
my @output = sort keys %$type_names; |
1135
|
0
|
|
|
|
|
0
|
unshift @output, "Type Name"; |
1136
|
0
|
|
|
|
|
0
|
return join "\n", @output; |
1137
|
|
|
|
|
|
|
} |
1138
|
|
|
|
|
|
|
|
1139
|
|
|
|
|
|
|
sub show_datatype { |
1140
|
0
|
|
|
0
|
1
|
0
|
my ($self, $table) = @_; |
1141
|
0
|
0
|
|
|
|
0
|
confess "Table name must be specified" unless defined $table; |
1142
|
0
|
|
|
|
|
0
|
print "$table\n"; |
1143
|
|
|
|
|
|
|
|
1144
|
0
|
|
|
|
|
0
|
my $result = $self->select(table => $table, where => "'0' <> '0'"); |
1145
|
0
|
|
|
|
|
0
|
my $sth = $result->sth; |
1146
|
|
|
|
|
|
|
|
1147
|
0
|
|
|
|
|
0
|
my $columns = $sth->{NAME}; |
1148
|
0
|
|
|
|
|
0
|
my $data_types = $sth->{TYPE}; |
1149
|
|
|
|
|
|
|
|
1150
|
0
|
|
|
|
|
0
|
for (my $i = 0; $i < @$columns; $i++) { |
1151
|
0
|
|
|
|
|
0
|
my $column = $columns->[$i]; |
1152
|
0
|
|
|
|
|
0
|
my $data_type = lc $data_types->[$i]; |
1153
|
0
|
|
|
|
|
0
|
print "$column: $data_type\n"; |
1154
|
|
|
|
|
|
|
} |
1155
|
|
|
|
|
|
|
} |
1156
|
|
|
|
|
|
|
|
1157
|
|
|
|
|
|
|
sub show_typename { |
1158
|
0
|
|
|
0
|
1
|
0
|
my ($self, $t) = @_; |
1159
|
0
|
0
|
|
|
|
0
|
confess "Table name must be specified" unless defined $t; |
1160
|
0
|
|
|
|
|
0
|
print "$t\n"; |
1161
|
|
|
|
|
|
|
|
1162
|
|
|
|
|
|
|
$self->each_column(sub { |
1163
|
0
|
|
|
0
|
|
0
|
my ($self, $table, $column, $infos) = @_; |
1164
|
0
|
0
|
|
|
|
0
|
return unless $table eq $t; |
1165
|
0
|
|
|
|
|
0
|
my $typename = lc $infos->{TYPE_NAME}; |
1166
|
0
|
|
|
|
|
0
|
print "$column: $typename\n"; |
1167
|
0
|
|
|
|
|
0
|
}); |
1168
|
|
|
|
|
|
|
|
1169
|
0
|
|
|
|
|
0
|
return $self; |
1170
|
|
|
|
|
|
|
} |
1171
|
|
|
|
|
|
|
|
1172
|
|
|
|
|
|
|
sub show_tables { |
1173
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1174
|
|
|
|
|
|
|
|
1175
|
0
|
|
|
|
|
0
|
my %tables; |
1176
|
0
|
|
|
0
|
|
0
|
$self->each_table(sub { $tables{$_[1]}++ }); |
|
0
|
|
|
|
|
0
|
|
1177
|
0
|
|
|
|
|
0
|
print join("\n", sort keys %tables) . "\n"; |
1178
|
0
|
|
|
|
|
0
|
return $self; |
1179
|
|
|
|
|
|
|
} |
1180
|
|
|
|
|
|
|
|
1181
|
|
|
|
|
|
|
sub _qp { |
1182
|
2635
|
|
|
2635
|
|
7085
|
my ($self, %opt) = @_; |
1183
|
|
|
|
|
|
|
|
1184
|
2635
|
|
50
|
|
|
7356
|
my $quote = $self->{quote} || $self->quote || ''; |
1185
|
|
|
|
|
|
|
|
1186
|
2635
|
|
50
|
|
|
8369
|
my $q = substr($quote, 0, 1) || ''; |
1187
|
2635
|
|
|
|
|
3616
|
my $p; |
1188
|
2635
|
100
|
66
|
|
|
7894
|
if (defined $quote && length $quote > 1) { |
1189
|
904
|
|
|
|
|
1609
|
$p = substr($quote, 1, 1); |
1190
|
|
|
|
|
|
|
} |
1191
|
1731
|
|
|
|
|
2536
|
else { $p = $q } |
1192
|
|
|
|
|
|
|
|
1193
|
2635
|
100
|
|
|
|
5281
|
if ($opt{quotemeta}) { |
1194
|
1710
|
|
|
|
|
2981
|
$q = quotemeta($q); |
1195
|
1710
|
|
|
|
|
2275
|
$p = quotemeta($p); |
1196
|
|
|
|
|
|
|
} |
1197
|
|
|
|
|
|
|
|
1198
|
2635
|
|
|
|
|
8133
|
return ($q, $p); |
1199
|
|
|
|
|
|
|
} |
1200
|
|
|
|
|
|
|
|
1201
|
|
|
|
|
|
|
sub _multi_values_clause { |
1202
|
0
|
|
|
0
|
|
0
|
my ($self, $params, $opts) = @_; |
1203
|
|
|
|
|
|
|
|
1204
|
0
|
|
0
|
|
|
0
|
my $wrap = $opts->{wrap} || {}; |
1205
|
|
|
|
|
|
|
|
1206
|
|
|
|
|
|
|
# Create insert parameter tag |
1207
|
0
|
|
|
|
|
0
|
my ($q, $p) = $self->_qp; |
1208
|
|
|
|
|
|
|
|
1209
|
0
|
|
|
|
|
0
|
my $safety_character = $self->safety_character; |
1210
|
|
|
|
|
|
|
|
1211
|
0
|
|
|
|
|
0
|
my $first_param = $params->[0]; |
1212
|
|
|
|
|
|
|
|
1213
|
0
|
|
|
|
|
0
|
my @columns; |
1214
|
|
|
|
|
|
|
my @columns_quoted; |
1215
|
0
|
|
|
|
|
0
|
for my $column (keys %$first_param) { |
1216
|
0
|
0
|
|
|
|
0
|
confess qq{"$column" is not safety column name in multi values clause} . _subname |
1217
|
|
|
|
|
|
|
unless $column =~ /^[$safety_character\.]+$/; |
1218
|
|
|
|
|
|
|
|
1219
|
0
|
|
|
|
|
0
|
push @columns, $column; |
1220
|
0
|
|
|
|
|
0
|
push @columns_quoted, "$q$column$p"; |
1221
|
|
|
|
|
|
|
} |
1222
|
|
|
|
|
|
|
|
1223
|
|
|
|
|
|
|
# Multi values clause |
1224
|
0
|
|
|
|
|
0
|
my $multi_values_clause = '(' . join(', ', @columns_quoted) . ') values '; |
1225
|
|
|
|
|
|
|
|
1226
|
0
|
|
|
|
|
0
|
for my $param (@$params) { |
1227
|
0
|
|
|
|
|
0
|
my @place_holders; |
1228
|
0
|
|
|
|
|
0
|
for my $column (@columns) { |
1229
|
0
|
|
|
|
|
0
|
push @place_holders, ref $param->{$column} eq 'SCALAR' ? ${$param->{$column}} : |
1230
|
0
|
0
|
|
|
|
0
|
$wrap->{$column} ? $wrap->{$column}->(":$column") : |
|
|
0
|
|
|
|
|
|
1231
|
|
|
|
|
|
|
":$column"; |
1232
|
|
|
|
|
|
|
} |
1233
|
0
|
|
|
|
|
0
|
$multi_values_clause .= '(' . join(', ', @place_holders) . '), '; |
1234
|
|
|
|
|
|
|
} |
1235
|
0
|
|
|
|
|
0
|
$multi_values_clause =~ s/, $//; |
1236
|
|
|
|
|
|
|
|
1237
|
0
|
|
|
|
|
0
|
return $multi_values_clause; |
1238
|
|
|
|
|
|
|
} |
1239
|
|
|
|
|
|
|
|
1240
|
|
|
|
|
|
|
sub _id_to_param { |
1241
|
79
|
|
|
79
|
|
220
|
my ($self, $id, $primary_keys, $table) = @_; |
1242
|
|
|
|
|
|
|
|
1243
|
|
|
|
|
|
|
# Check primary key |
1244
|
79
|
50
|
|
|
|
1248
|
confess "primary_key option " . |
1245
|
|
|
|
|
|
|
"must be specified when id option is used" . _subname |
1246
|
|
|
|
|
|
|
unless defined $primary_keys; |
1247
|
79
|
100
|
|
|
|
271
|
$primary_keys = [$primary_keys] unless ref $primary_keys eq 'ARRAY'; |
1248
|
|
|
|
|
|
|
|
1249
|
79
|
|
|
|
|
324
|
_deprecate('0.39', "DBIx::Custom::select,update,delete method's id and primary_key option is DEPRECATED!"); |
1250
|
|
|
|
|
|
|
|
1251
|
|
|
|
|
|
|
# Create parameter |
1252
|
79
|
|
|
|
|
1033
|
my $param = {}; |
1253
|
79
|
50
|
|
|
|
266
|
if (defined $id) { |
1254
|
79
|
100
|
|
|
|
267
|
$id = [$id] unless ref $id eq 'ARRAY'; |
1255
|
79
|
|
|
|
|
239
|
for(my $i = 0; $i < @$id; $i++) { |
1256
|
106
|
|
|
|
|
201
|
my $key = $primary_keys->[$i]; |
1257
|
106
|
50
|
|
|
|
1329
|
$key = "$table." . $key if $table; |
1258
|
106
|
|
|
|
|
379
|
$param->{$key} = $id->[$i]; |
1259
|
|
|
|
|
|
|
} |
1260
|
|
|
|
|
|
|
} |
1261
|
|
|
|
|
|
|
|
1262
|
79
|
|
|
|
|
192
|
return $param; |
1263
|
|
|
|
|
|
|
} |
1264
|
|
|
|
|
|
|
|
1265
|
|
|
|
|
|
|
sub _connect { |
1266
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
1267
|
|
|
|
|
|
|
|
1268
|
|
|
|
|
|
|
# Attributes |
1269
|
0
|
|
|
|
|
0
|
my $dsn = $self->dsn; |
1270
|
0
|
0
|
|
|
|
0
|
confess qq{"dsn" must be specified } . _subname |
1271
|
|
|
|
|
|
|
unless $dsn; |
1272
|
0
|
|
|
|
|
0
|
my $user = $self->user; |
1273
|
0
|
|
|
|
|
0
|
my $password = $self->password; |
1274
|
0
|
|
|
|
|
0
|
my $option = $self->option; |
1275
|
0
|
|
|
|
|
0
|
$option = {%{$self->default_option}, %$option}; |
|
0
|
|
|
|
|
0
|
|
1276
|
|
|
|
|
|
|
|
1277
|
|
|
|
|
|
|
# Connect |
1278
|
0
|
|
|
|
|
0
|
my $dbh; |
1279
|
0
|
|
|
|
|
0
|
eval { $dbh = DBI->connect($dsn, $user, $password, $option) }; |
|
0
|
|
|
|
|
0
|
|
1280
|
|
|
|
|
|
|
|
1281
|
|
|
|
|
|
|
# Connect error |
1282
|
0
|
0
|
|
|
|
0
|
confess "$@ " . _subname if $@; |
1283
|
|
|
|
|
|
|
|
1284
|
0
|
|
|
|
|
0
|
return $dbh; |
1285
|
|
|
|
|
|
|
} |
1286
|
|
|
|
|
|
|
|
1287
|
|
|
|
|
|
|
sub _confess { |
1288
|
585
|
|
|
585
|
|
1831
|
my ($self, $error, $append) = @_; |
1289
|
|
|
|
|
|
|
|
1290
|
|
|
|
|
|
|
# Append |
1291
|
585
|
|
50
|
|
|
1256
|
$append ||= ""; |
1292
|
|
|
|
|
|
|
|
1293
|
|
|
|
|
|
|
# Verbose |
1294
|
585
|
100
|
|
|
|
1422
|
if ($Carp::Verbose) { confess $error } |
|
6
|
|
|
|
|
713
|
|
1295
|
|
|
|
|
|
|
|
1296
|
|
|
|
|
|
|
# Not verbose |
1297
|
|
|
|
|
|
|
else { |
1298
|
|
|
|
|
|
|
# Remove line and module information |
1299
|
579
|
|
|
|
|
1356
|
my $at_pos = rindex($error, ' at '); |
1300
|
579
|
|
|
|
|
1257
|
$error = substr($error, 0, $at_pos); |
1301
|
579
|
|
|
|
|
3196
|
$error =~ s/\s+$//; |
1302
|
579
|
|
|
|
|
69329
|
confess "$error$append"; |
1303
|
|
|
|
|
|
|
} |
1304
|
|
|
|
|
|
|
} |
1305
|
|
|
|
|
|
|
|
1306
|
331
|
|
|
331
|
|
3006
|
sub _driver { lc shift->{dbh}->{Driver}->{Name} } |
1307
|
|
|
|
|
|
|
|
1308
|
|
|
|
|
|
|
sub _need_tables { |
1309
|
375
|
|
|
375
|
|
730
|
my ($self, $tree, $need_tables, $tables) = @_; |
1310
|
|
|
|
|
|
|
|
1311
|
|
|
|
|
|
|
# Get needed tables |
1312
|
375
|
|
|
|
|
583
|
for my $table (@$tables) { |
1313
|
809
|
100
|
|
|
|
1711
|
if ($tree->{$table}) { |
1314
|
245
|
|
|
|
|
395
|
$need_tables->{$table} = 1; |
1315
|
245
|
|
|
|
|
797
|
$self->_need_tables($tree, $need_tables, [$tree->{$table}{parent}]) |
1316
|
|
|
|
|
|
|
} |
1317
|
|
|
|
|
|
|
} |
1318
|
|
|
|
|
|
|
} |
1319
|
|
|
|
|
|
|
|
1320
|
|
|
|
|
|
|
sub _push_join { |
1321
|
130
|
|
|
130
|
|
326
|
my ($self, $sql, $join, $join_tables) = @_; |
1322
|
|
|
|
|
|
|
|
1323
|
|
|
|
|
|
|
# Push join clause |
1324
|
130
|
|
|
|
|
238
|
my $tree = {}; |
1325
|
130
|
|
|
|
|
406
|
for (my $i = 0; $i < @$join; $i++) { |
1326
|
|
|
|
|
|
|
|
1327
|
|
|
|
|
|
|
# Arrange |
1328
|
157
|
|
|
|
|
269
|
my $join_clause;; |
1329
|
|
|
|
|
|
|
my $option; |
1330
|
157
|
100
|
|
|
|
369
|
if (ref $join->[$i] eq 'HASH') { |
1331
|
6
|
|
|
|
|
17
|
$join_clause = $join->[$i]->{clause}; |
1332
|
6
|
|
|
|
|
20
|
$option = {table => $join->[$i]->{table}}; |
1333
|
|
|
|
|
|
|
} |
1334
|
|
|
|
|
|
|
else { |
1335
|
151
|
|
|
|
|
236
|
$join_clause = $join->[$i]; |
1336
|
151
|
|
|
|
|
239
|
$option = {}; |
1337
|
|
|
|
|
|
|
}; |
1338
|
|
|
|
|
|
|
|
1339
|
|
|
|
|
|
|
# Find tables in join clause |
1340
|
157
|
|
|
|
|
269
|
my $table1; |
1341
|
|
|
|
|
|
|
my $table2; |
1342
|
157
|
100
|
|
|
|
372
|
if (my $table = $option->{table}) { |
1343
|
6
|
|
|
|
|
14
|
$table1 = $table->[0]; |
1344
|
6
|
|
|
|
|
15
|
$table2 = $table->[1]; |
1345
|
|
|
|
|
|
|
} |
1346
|
|
|
|
|
|
|
else { |
1347
|
151
|
|
|
|
|
347
|
my $q = $self->_quote; |
1348
|
151
|
|
|
|
|
1703
|
my $j_clause = (split /\s+on\s+/, $join_clause)[-1]; |
1349
|
151
|
|
|
|
|
399
|
$j_clause =~ s/'.+?'//g; |
1350
|
151
|
|
|
|
|
273
|
my $q_re = quotemeta($q); |
1351
|
151
|
|
|
|
|
538
|
$j_clause =~ s/[$q_re]//g; |
1352
|
|
|
|
|
|
|
|
1353
|
151
|
|
|
|
|
627
|
my @j_clauses = reverse split /\s(and|on)\s/, $j_clause; |
1354
|
151
|
|
|
|
|
313
|
my $c = $self->{safety_character}; |
1355
|
151
|
|
|
|
|
1230
|
my $join_re = qr/((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+[^$c].*?((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+/sm; |
1356
|
151
|
|
|
|
|
354
|
for my $clause (@j_clauses) { |
1357
|
167
|
100
|
|
|
|
1295
|
if ($clause =~ $join_re) { |
1358
|
151
|
|
|
|
|
385
|
$table1 = $1; |
1359
|
151
|
|
|
|
|
245
|
$table2 = $2; |
1360
|
151
|
|
|
|
|
469
|
last; |
1361
|
|
|
|
|
|
|
} |
1362
|
|
|
|
|
|
|
} |
1363
|
|
|
|
|
|
|
} |
1364
|
157
|
50
|
33
|
|
|
639
|
confess qq{join clause must have two table name after "on" keyword. } . |
1365
|
|
|
|
|
|
|
qq{"$join_clause" is passed } . _subname |
1366
|
|
|
|
|
|
|
unless defined $table1 && defined $table2; |
1367
|
|
|
|
|
|
|
confess qq{right side table of "$join_clause" must be unique } . _subname |
1368
|
157
|
50
|
|
|
|
360
|
if exists $tree->{$table2}; |
1369
|
157
|
50
|
|
|
|
324
|
confess qq{Same table "$table1" is specified} . _subname |
1370
|
|
|
|
|
|
|
if $table1 eq $table2; |
1371
|
157
|
|
|
|
|
920
|
$tree->{$table2} |
1372
|
|
|
|
|
|
|
= {position => $i, parent => $table1, join => $join_clause}; |
1373
|
|
|
|
|
|
|
} |
1374
|
|
|
|
|
|
|
|
1375
|
|
|
|
|
|
|
# Search need tables |
1376
|
130
|
|
|
|
|
275
|
my $need_tables = {}; |
1377
|
130
|
|
|
|
|
412
|
$self->_need_tables($tree, $need_tables, $join_tables); |
1378
|
130
|
|
|
|
|
535
|
my @need_tables = sort { $tree->{$a}{position} <=> $tree->{$b}{position} } |
|
18
|
|
|
|
|
83
|
|
1379
|
|
|
|
|
|
|
keys %$need_tables; |
1380
|
|
|
|
|
|
|
|
1381
|
|
|
|
|
|
|
# Add join clause |
1382
|
130
|
|
|
|
|
805
|
$$sql .= $tree->{$_}{join} . ' ' for @need_tables; |
1383
|
|
|
|
|
|
|
} |
1384
|
|
|
|
|
|
|
|
1385
|
|
|
|
|
|
|
sub _quote { |
1386
|
316
|
|
|
316
|
|
1331
|
my $self = shift; |
1387
|
316
|
|
50
|
|
|
3922
|
return $self->quote || ''; |
1388
|
|
|
|
|
|
|
} |
1389
|
|
|
|
|
|
|
|
1390
|
|
|
|
|
|
|
sub _remove_duplicate_table { |
1391
|
0
|
|
|
0
|
|
0
|
my ($self, $tables, $main_table) = @_; |
1392
|
|
|
|
|
|
|
|
1393
|
|
|
|
|
|
|
# Remove duplicate table |
1394
|
0
|
0
|
|
|
|
0
|
my %tables = map {defined $_ ? ($_ => 1) : ()} @$tables; |
|
0
|
|
|
|
|
0
|
|
1395
|
0
|
0
|
|
|
|
0
|
delete $tables{$main_table} if $main_table; |
1396
|
|
|
|
|
|
|
|
1397
|
0
|
0
|
|
|
|
0
|
my $new_tables = [keys %tables, $main_table ? $main_table : ()]; |
1398
|
0
|
0
|
|
|
|
0
|
if (my $q = $self->_quote) { |
1399
|
0
|
|
|
|
|
0
|
$q = quotemeta($q); |
1400
|
0
|
|
|
|
|
0
|
$_ =~ s/[$q]//g for @$new_tables; |
1401
|
|
|
|
|
|
|
} |
1402
|
|
|
|
|
|
|
|
1403
|
0
|
|
|
|
|
0
|
return $new_tables; |
1404
|
|
|
|
|
|
|
} |
1405
|
|
|
|
|
|
|
|
1406
|
|
|
|
|
|
|
sub _search_tables { |
1407
|
1710
|
|
|
1710
|
|
3520
|
my ($self, $source) = @_; |
1408
|
|
|
|
|
|
|
|
1409
|
|
|
|
|
|
|
# Search tables |
1410
|
1710
|
|
|
|
|
2864
|
my $tables = []; |
1411
|
1710
|
|
|
|
|
3856
|
my ($q, $p) = $self->_qp(quotemeta => 1); |
1412
|
1710
|
|
|
|
|
5267
|
$source =~ s/$q//g; |
1413
|
1710
|
|
|
|
|
2922
|
$source =~ s/$p//g; |
1414
|
1710
|
|
|
|
|
35101
|
my $c = $self->safety_character; |
1415
|
|
|
|
|
|
|
|
1416
|
1710
|
|
|
|
|
17754
|
while ($source =~ /((?:[$c]+?\.[$c]+?)|(?:[$c]+?))\.[$c]+/g) { |
1417
|
538
|
|
|
|
|
3675
|
push @$tables, $1; |
1418
|
|
|
|
|
|
|
} |
1419
|
1710
|
|
|
|
|
4487
|
return $tables; |
1420
|
|
|
|
|
|
|
} |
1421
|
|
|
|
|
|
|
|
1422
|
|
|
|
|
|
|
sub _where_clause_and_param { |
1423
|
889
|
|
|
889
|
|
2541
|
my ($self, $where) = @_; |
1424
|
|
|
|
|
|
|
|
1425
|
889
|
|
100
|
|
|
3197
|
$where ||= {}; |
1426
|
889
|
|
|
|
|
1481
|
my $w = {}; |
1427
|
|
|
|
|
|
|
|
1428
|
889
|
100
|
|
|
|
2319
|
if (ref $where eq 'HASH') { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1429
|
766
|
|
|
|
|
14039
|
my $safety_character = $self->safety_character; |
1430
|
|
|
|
|
|
|
|
1431
|
766
|
|
|
|
|
4728
|
my $clause = []; |
1432
|
766
|
|
|
|
|
1230
|
my $column_join = ''; |
1433
|
766
|
|
|
|
|
2617
|
for my $column (sort keys %$where) { |
1434
|
|
|
|
|
|
|
|
1435
|
293
|
100
|
|
|
|
1629
|
confess qq{"$column" is not safety column name in where clause} . _subname |
1436
|
|
|
|
|
|
|
unless $column =~ /^[$safety_character\.]+$/; |
1437
|
|
|
|
|
|
|
|
1438
|
290
|
|
|
|
|
572
|
$column_join .= $column; |
1439
|
290
|
|
|
|
|
480
|
my $table; |
1440
|
|
|
|
|
|
|
my $c; |
1441
|
290
|
50
|
|
|
|
1131
|
if ($column =~ /(?:(.*)\.)?(.*)/) { |
1442
|
290
|
|
|
|
|
621
|
$table = $1; |
1443
|
290
|
|
|
|
|
513
|
$c = $2; |
1444
|
|
|
|
|
|
|
} |
1445
|
|
|
|
|
|
|
|
1446
|
290
|
|
|
|
|
413
|
my $table_quote; |
1447
|
290
|
100
|
|
|
|
803
|
$table_quote = $self->_tq($table) if defined $table; |
1448
|
290
|
|
|
|
|
815
|
my $column_quote = $self->q($c); |
1449
|
290
|
100
|
|
|
|
877
|
$column_quote = $table_quote . '.' . $column_quote |
1450
|
|
|
|
|
|
|
if defined $table_quote; |
1451
|
290
|
100
|
|
|
|
665
|
if (ref $where->{$column} eq 'ARRAY') { |
1452
|
6
|
|
|
|
|
17
|
my $c = join(', ', (":$column") x @{$where->{$column}}); |
|
6
|
|
|
|
|
55
|
|
1453
|
6
|
100
|
|
|
|
11
|
if (@{$where->{$column}}) { |
|
6
|
|
|
|
|
19
|
|
1454
|
3
|
|
|
|
|
18
|
push @$clause, "$column_quote in ( $c )"; |
1455
|
|
|
|
|
|
|
} |
1456
|
3
|
|
|
|
|
13
|
else { push @$clause, '1 <> 1' } |
1457
|
|
|
|
|
|
|
} |
1458
|
284
|
|
|
|
|
983
|
else { push @$clause, "$column_quote = :$column" } |
1459
|
|
|
|
|
|
|
} |
1460
|
|
|
|
|
|
|
|
1461
|
763
|
100
|
|
|
|
2675
|
$w->{clause} = @$clause ? "where ( " . join(' and ', @$clause) . " ) " : '' ; |
1462
|
763
|
|
|
|
|
1793
|
$w->{param} = $where; |
1463
|
|
|
|
|
|
|
} |
1464
|
|
|
|
|
|
|
elsif (ref $where) { |
1465
|
123
|
|
|
|
|
202
|
my $obj; |
1466
|
|
|
|
|
|
|
|
1467
|
123
|
100
|
|
|
|
297
|
if (ref $where eq 'DBIx::Custom::Where') { $obj = $where } |
|
84
|
50
|
|
|
|
123
|
|
1468
|
|
|
|
|
|
|
elsif (ref $where eq 'ARRAY') { |
1469
|
39
|
|
|
|
|
133
|
$obj = $self->where(clause => $where->[0], param => $where->[1], join => $where->[2]); |
1470
|
|
|
|
|
|
|
} |
1471
|
|
|
|
|
|
|
|
1472
|
|
|
|
|
|
|
# Check where argument |
1473
|
123
|
50
|
|
|
|
296
|
confess qq{"where" must be hash reference or DBIx::Custom::Where object} |
1474
|
|
|
|
|
|
|
. qq{or array reference, which contains where clause and parameter} |
1475
|
|
|
|
|
|
|
. _subname |
1476
|
|
|
|
|
|
|
unless ref $obj eq 'DBIx::Custom::Where'; |
1477
|
|
|
|
|
|
|
|
1478
|
123
|
|
|
|
|
302
|
$w->{clause} = $obj->to_string; |
1479
|
120
|
|
|
|
|
2370
|
$w->{param} = $obj->param; |
1480
|
120
|
|
|
|
|
930
|
$w->{join} = $obj->{join}; |
1481
|
|
|
|
|
|
|
} |
1482
|
|
|
|
|
|
|
elsif ($where) { |
1483
|
0
|
|
|
|
|
0
|
$w->{clause} = "where $where"; |
1484
|
|
|
|
|
|
|
} |
1485
|
|
|
|
|
|
|
|
1486
|
883
|
|
|
|
|
1671
|
return $w; |
1487
|
|
|
|
|
|
|
} |
1488
|
|
|
|
|
|
|
|
1489
|
|
|
|
|
|
|
# DEPRECATED |
1490
|
|
|
|
|
|
|
our $AUTOLOAD; |
1491
|
|
|
|
|
|
|
sub AUTOLOAD { |
1492
|
39
|
|
|
39
|
|
7531
|
my $self = shift; |
1493
|
|
|
|
|
|
|
|
1494
|
39
|
|
|
|
|
168
|
_deprecate('0.39', "DBIx::Custom AUTOLOAD feature is DEPRECATED!"); |
1495
|
|
|
|
|
|
|
|
1496
|
|
|
|
|
|
|
# Method name |
1497
|
39
|
|
|
|
|
340
|
my ($package, $mname) = $AUTOLOAD =~ /^([\w\:]+)\:\:(\w+)$/; |
1498
|
|
|
|
|
|
|
|
1499
|
|
|
|
|
|
|
# Call method |
1500
|
39
|
|
100
|
|
|
169
|
$self->{_methods} ||= {}; |
1501
|
39
|
100
|
66
|
|
|
185
|
if (my $method = $self->{_methods}->{$mname}) { |
|
|
100
|
|
|
|
|
|
1502
|
9
|
|
|
|
|
33
|
return $self->$method(@_) |
1503
|
|
|
|
|
|
|
} |
1504
|
|
|
|
|
|
|
elsif ($self->{dbh} && (my $dbh_method = $self->dbh->can($mname))) { |
1505
|
27
|
|
|
|
|
62
|
$self->dbh->$dbh_method(@_); |
1506
|
|
|
|
|
|
|
} |
1507
|
|
|
|
|
|
|
else { |
1508
|
3
|
|
|
|
|
23
|
confess qq{Can't locate object method "$mname" via "$package" } |
1509
|
|
|
|
|
|
|
. _subname; |
1510
|
|
|
|
|
|
|
} |
1511
|
|
|
|
|
|
|
} |
1512
|
|
|
|
0
|
|
|
sub DESTROY {} |
1513
|
|
|
|
|
|
|
|
1514
|
|
|
|
|
|
|
# DEPRECATED |
1515
|
|
|
|
|
|
|
sub helper { |
1516
|
9
|
|
|
9
|
0
|
74
|
my $self = shift; |
1517
|
|
|
|
|
|
|
|
1518
|
9
|
|
|
|
|
28
|
_deprecate('0.39', "DBIx::Custom::helper method is DEPRECATED!"); |
1519
|
|
|
|
|
|
|
|
1520
|
|
|
|
|
|
|
# Register method |
1521
|
9
|
100
|
|
|
|
36
|
my $methods = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
1522
|
9
|
100
|
|
|
|
17
|
$self->{_methods} = {%{$self->{_methods} || {}}, %$methods}; |
|
9
|
|
|
|
|
53
|
|
1523
|
|
|
|
|
|
|
|
1524
|
9
|
|
|
|
|
29
|
return $self; |
1525
|
|
|
|
|
|
|
} |
1526
|
|
|
|
|
|
|
|
1527
|
|
|
|
|
|
|
# DEPRECATED |
1528
|
|
|
|
|
|
|
sub update_or_insert { |
1529
|
|
|
|
|
|
|
|
1530
|
18
|
|
|
18
|
0
|
6205
|
_deprecate('0.39', "DBIx::Custom::update_or_insert method is DEPRECATED!"); |
1531
|
|
|
|
|
|
|
|
1532
|
18
|
|
|
|
|
72
|
my ($self, $param, %opt) = @_; |
1533
|
|
|
|
|
|
|
confess "update_or_insert method need primary_key and id option " |
1534
|
18
|
100
|
66
|
|
|
422
|
unless defined $opt{id} && defined $opt{primary_key}; |
1535
|
15
|
|
50
|
|
|
983
|
my $statement_opt = $opt{option} || {}; |
1536
|
|
|
|
|
|
|
|
1537
|
15
|
50
|
|
|
|
45
|
my $rows = $self->select(%opt, %{$statement_opt->{select} || {}})->all; |
|
15
|
|
|
|
|
81
|
|
1538
|
15
|
100
|
|
|
|
194
|
if (@$rows == 0) { |
|
|
100
|
|
|
|
|
|
1539
|
6
|
50
|
|
|
|
21
|
return $self->insert($param, %opt, %{$statement_opt->{insert} || {}}); |
|
6
|
|
|
|
|
40
|
|
1540
|
|
|
|
|
|
|
} |
1541
|
|
|
|
|
|
|
elsif (@$rows == 1) { |
1542
|
6
|
100
|
|
|
|
37
|
return 0 unless keys %$param; |
1543
|
3
|
50
|
|
|
|
13
|
return $self->update($param, %opt, %{$statement_opt->{update} || {}}); |
|
3
|
|
|
|
|
27
|
|
1544
|
|
|
|
|
|
|
} |
1545
|
3
|
|
|
|
|
16
|
else { confess "selected row must be one " . _subname } |
1546
|
|
|
|
|
|
|
} |
1547
|
|
|
|
|
|
|
|
1548
|
|
|
|
|
|
|
# DEPRECATED |
1549
|
|
|
|
|
|
|
sub count { |
1550
|
15
|
|
|
15
|
0
|
389
|
_deprecate('0.39', "DBIx::Custom::count method is DEPRECATED!"); |
1551
|
15
|
|
|
|
|
53
|
shift->select(column => 'count(*)', @_)->fetch_one->[0] |
1552
|
|
|
|
|
|
|
} |
1553
|
|
|
|
|
|
|
|
1554
|
|
|
|
|
|
|
1; |
1555
|
|
|
|
|
|
|
|
1556
|
|
|
|
|
|
|
=head1 NAME |
1557
|
|
|
|
|
|
|
|
1558
|
|
|
|
|
|
|
DBIx::Custom - DBI extension to execute insert, update, delete, and select easily |
1559
|
|
|
|
|
|
|
|
1560
|
|
|
|
|
|
|
=head1 SYNOPSIS |
1561
|
|
|
|
|
|
|
|
1562
|
|
|
|
|
|
|
use DBIx::Custom; |
1563
|
|
|
|
|
|
|
|
1564
|
|
|
|
|
|
|
# Connect |
1565
|
|
|
|
|
|
|
my $dbi = DBIx::Custom->connect( |
1566
|
|
|
|
|
|
|
"dbi:mysql:database=dbname", |
1567
|
|
|
|
|
|
|
'ken', |
1568
|
|
|
|
|
|
|
'!LFKD%$&', |
1569
|
|
|
|
|
|
|
{mysql_enable_utf8 => 1} |
1570
|
|
|
|
|
|
|
); |
1571
|
|
|
|
|
|
|
|
1572
|
|
|
|
|
|
|
# Create model |
1573
|
|
|
|
|
|
|
$dbi->create_model('book'); |
1574
|
|
|
|
|
|
|
|
1575
|
|
|
|
|
|
|
# Insert |
1576
|
|
|
|
|
|
|
$dbi->model('book')->insert({title => 'Perl', author => 'Ken'}); |
1577
|
|
|
|
|
|
|
|
1578
|
|
|
|
|
|
|
# Update |
1579
|
|
|
|
|
|
|
$dbi->model('book')->update({title => 'Perl', author => 'Ken'}, where => {id => 5}); |
1580
|
|
|
|
|
|
|
|
1581
|
|
|
|
|
|
|
# Delete |
1582
|
|
|
|
|
|
|
$dbi->model('book')->delete(where => {author => 'Ken'}); |
1583
|
|
|
|
|
|
|
|
1584
|
|
|
|
|
|
|
# Select |
1585
|
|
|
|
|
|
|
my $result = $dbi->model('book')->select(['title', 'author'], where => {author => 'Ken'}); |
1586
|
|
|
|
|
|
|
|
1587
|
|
|
|
|
|
|
# Select, more complex |
1588
|
|
|
|
|
|
|
# select book.title as book.title, |
1589
|
|
|
|
|
|
|
# book.author as book.author, |
1590
|
|
|
|
|
|
|
# comnapy.name as company.name |
1591
|
|
|
|
|
|
|
# form book |
1592
|
|
|
|
|
|
|
# left outer join company on book.company_id = company.id |
1593
|
|
|
|
|
|
|
# where book.author = ? |
1594
|
|
|
|
|
|
|
# order by id limit 0, 5 |
1595
|
|
|
|
|
|
|
my $result = $dbi->model('book')->select( |
1596
|
|
|
|
|
|
|
[ |
1597
|
|
|
|
|
|
|
{book => [qw/title author/]}, |
1598
|
|
|
|
|
|
|
{company => ['name']} |
1599
|
|
|
|
|
|
|
], |
1600
|
|
|
|
|
|
|
where => {'book.author' => 'Ken'}, |
1601
|
|
|
|
|
|
|
join => ['left outer join company on book.company_id = company.id'], |
1602
|
|
|
|
|
|
|
append => 'order by id limit 0, 5' |
1603
|
|
|
|
|
|
|
); |
1604
|
|
|
|
|
|
|
|
1605
|
|
|
|
|
|
|
# Get all rows or only one row |
1606
|
|
|
|
|
|
|
my $rows = $result->all; |
1607
|
|
|
|
|
|
|
my $row = $result->one; |
1608
|
|
|
|
|
|
|
|
1609
|
|
|
|
|
|
|
# Execute SQL with named place holder |
1610
|
|
|
|
|
|
|
my $result = $dbi->execute( |
1611
|
|
|
|
|
|
|
"select id from book where author = :author and title like :title", |
1612
|
|
|
|
|
|
|
{author => 'ken', title => '%Perl%'} |
1613
|
|
|
|
|
|
|
); |
1614
|
|
|
|
|
|
|
|
1615
|
|
|
|
|
|
|
=head1 DESCRIPTION |
1616
|
|
|
|
|
|
|
|
1617
|
|
|
|
|
|
|
L is L wrapper module to execute SQL easily. |
1618
|
|
|
|
|
|
|
This module have the following features. |
1619
|
|
|
|
|
|
|
|
1620
|
|
|
|
|
|
|
=over 4 |
1621
|
|
|
|
|
|
|
|
1622
|
|
|
|
|
|
|
=item * |
1623
|
|
|
|
|
|
|
|
1624
|
|
|
|
|
|
|
Execute C, C, C, or C |
1625
|
|
|
|
|
|
|
|
1626
|
|
|
|
|
|
|
=item * |
1627
|
|
|
|
|
|
|
|
1628
|
|
|
|
|
|
|
Create C clause flexibly |
1629
|
|
|
|
|
|
|
|
1630
|
|
|
|
|
|
|
=item * |
1631
|
|
|
|
|
|
|
|
1632
|
|
|
|
|
|
|
Named place holder support |
1633
|
|
|
|
|
|
|
|
1634
|
|
|
|
|
|
|
=item * |
1635
|
|
|
|
|
|
|
|
1636
|
|
|
|
|
|
|
Model support |
1637
|
|
|
|
|
|
|
|
1638
|
|
|
|
|
|
|
=item * |
1639
|
|
|
|
|
|
|
|
1640
|
|
|
|
|
|
|
Connection manager support |
1641
|
|
|
|
|
|
|
|
1642
|
|
|
|
|
|
|
=item * |
1643
|
|
|
|
|
|
|
|
1644
|
|
|
|
|
|
|
Choice your favorite relational database management system, |
1645
|
|
|
|
|
|
|
C, C, C, C, |
1646
|
|
|
|
|
|
|
C, C, C or anything, |
1647
|
|
|
|
|
|
|
|
1648
|
|
|
|
|
|
|
=item * |
1649
|
|
|
|
|
|
|
|
1650
|
|
|
|
|
|
|
Filtering by data type or column name |
1651
|
|
|
|
|
|
|
|
1652
|
|
|
|
|
|
|
=item * |
1653
|
|
|
|
|
|
|
|
1654
|
|
|
|
|
|
|
Create C clause flexibly |
1655
|
|
|
|
|
|
|
|
1656
|
|
|
|
|
|
|
=back |
1657
|
|
|
|
|
|
|
|
1658
|
|
|
|
|
|
|
=head1 WEB SITE |
1659
|
|
|
|
|
|
|
|
1660
|
|
|
|
|
|
|
L |
1661
|
|
|
|
|
|
|
|
1662
|
|
|
|
|
|
|
=head1 DOCUMENTS |
1663
|
|
|
|
|
|
|
|
1664
|
|
|
|
|
|
|
L |
1665
|
|
|
|
|
|
|
|
1666
|
|
|
|
|
|
|
L |
1667
|
|
|
|
|
|
|
|
1668
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
1669
|
|
|
|
|
|
|
|
1670
|
|
|
|
|
|
|
=head2 connector |
1671
|
|
|
|
|
|
|
|
1672
|
|
|
|
|
|
|
my $connector = $dbi->connector; |
1673
|
|
|
|
|
|
|
$dbi = $dbi->connector($connector); |
1674
|
|
|
|
|
|
|
|
1675
|
|
|
|
|
|
|
Connection manager object. if C is set, you can get C |
1676
|
|
|
|
|
|
|
through connection manager. Conection manager object must have C method. |
1677
|
|
|
|
|
|
|
|
1678
|
|
|
|
|
|
|
This is L example. Please pass |
1679
|
|
|
|
|
|
|
C to L C method. |
1680
|
|
|
|
|
|
|
|
1681
|
|
|
|
|
|
|
my $connector = DBIx::Connector->new( |
1682
|
|
|
|
|
|
|
"dbi:mysql:database=$database", |
1683
|
|
|
|
|
|
|
$user, |
1684
|
|
|
|
|
|
|
$password, |
1685
|
|
|
|
|
|
|
DBIx::Custom->new->default_option |
1686
|
|
|
|
|
|
|
); |
1687
|
|
|
|
|
|
|
|
1688
|
|
|
|
|
|
|
my $dbi = DBIx::Custom->connect(connector => $connector); |
1689
|
|
|
|
|
|
|
|
1690
|
|
|
|
|
|
|
If C is set to 1 when connect method is called, |
1691
|
|
|
|
|
|
|
L is automatically set to C |
1692
|
|
|
|
|
|
|
|
1693
|
|
|
|
|
|
|
my $dbi = DBIx::Custom->connect( |
1694
|
|
|
|
|
|
|
dsn => $dsn, user => $user, password => $password, connector => 1); |
1695
|
|
|
|
|
|
|
|
1696
|
|
|
|
|
|
|
my $connector = $dbi->connector; # DBIx::Connector |
1697
|
|
|
|
|
|
|
|
1698
|
|
|
|
|
|
|
Note that L must be installed. |
1699
|
|
|
|
|
|
|
|
1700
|
|
|
|
|
|
|
=head2 dsn |
1701
|
|
|
|
|
|
|
|
1702
|
|
|
|
|
|
|
my $dsn = $dbi->dsn; |
1703
|
|
|
|
|
|
|
$dbi = $dbi->dsn("DBI:mysql:database=dbname"); |
1704
|
|
|
|
|
|
|
|
1705
|
|
|
|
|
|
|
Data source name, used when C method is executed. |
1706
|
|
|
|
|
|
|
|
1707
|
|
|
|
|
|
|
=head2 default_option |
1708
|
|
|
|
|
|
|
|
1709
|
|
|
|
|
|
|
my $default_option = $dbi->default_option; |
1710
|
|
|
|
|
|
|
$dbi = $dbi->default_option($default_option); |
1711
|
|
|
|
|
|
|
|
1712
|
|
|
|
|
|
|
L default option, used when C method is executed, |
1713
|
|
|
|
|
|
|
default to the following values. |
1714
|
|
|
|
|
|
|
|
1715
|
|
|
|
|
|
|
{ |
1716
|
|
|
|
|
|
|
RaiseError => 1, |
1717
|
|
|
|
|
|
|
PrintError => 0, |
1718
|
|
|
|
|
|
|
AutoCommit => 1, |
1719
|
|
|
|
|
|
|
} |
1720
|
|
|
|
|
|
|
|
1721
|
|
|
|
|
|
|
=head2 exclude_table |
1722
|
|
|
|
|
|
|
|
1723
|
|
|
|
|
|
|
my $exclude_table = $dbi->exclude_table; |
1724
|
|
|
|
|
|
|
$dbi = $dbi->exclude_table(qr/pg_/); |
1725
|
|
|
|
|
|
|
|
1726
|
|
|
|
|
|
|
Excluded table regex. |
1727
|
|
|
|
|
|
|
C, C, C |
1728
|
|
|
|
|
|
|
|
1729
|
|
|
|
|
|
|
=head2 filters |
1730
|
|
|
|
|
|
|
|
1731
|
|
|
|
|
|
|
my $filters = $dbi->filters; |
1732
|
|
|
|
|
|
|
$dbi = $dbi->filters(\%filters); |
1733
|
|
|
|
|
|
|
|
1734
|
|
|
|
|
|
|
Filters, registered by C method. |
1735
|
|
|
|
|
|
|
|
1736
|
|
|
|
|
|
|
=head2 last_sql |
1737
|
|
|
|
|
|
|
|
1738
|
|
|
|
|
|
|
my $last_sql = $dbi->last_sql; |
1739
|
|
|
|
|
|
|
$dbi = $dbi->last_sql($last_sql); |
1740
|
|
|
|
|
|
|
|
1741
|
|
|
|
|
|
|
Get last succeeded SQL executed by C method. |
1742
|
|
|
|
|
|
|
|
1743
|
|
|
|
|
|
|
=head2 now |
1744
|
|
|
|
|
|
|
|
1745
|
|
|
|
|
|
|
my $now = $dbi->now; |
1746
|
|
|
|
|
|
|
$dbi = $dbi->now($now); |
1747
|
|
|
|
|
|
|
|
1748
|
|
|
|
|
|
|
Code reference which return current time, default to the following code reference. |
1749
|
|
|
|
|
|
|
|
1750
|
|
|
|
|
|
|
sub { |
1751
|
|
|
|
|
|
|
my ($sec, $min, $hour, $mday, $mon, $year) = localtime; |
1752
|
|
|
|
|
|
|
$mon++; |
1753
|
|
|
|
|
|
|
$year += 1900; |
1754
|
|
|
|
|
|
|
return sprintf("%04d-%02d-%02d %02d:%02d:%02d"); |
1755
|
|
|
|
|
|
|
} |
1756
|
|
|
|
|
|
|
|
1757
|
|
|
|
|
|
|
This return the time like C<2011-10-14 05:05:27>. |
1758
|
|
|
|
|
|
|
|
1759
|
|
|
|
|
|
|
This is used by C method's C option and C option, |
1760
|
|
|
|
|
|
|
and C method's C option. |
1761
|
|
|
|
|
|
|
|
1762
|
|
|
|
|
|
|
=head2 models |
1763
|
|
|
|
|
|
|
|
1764
|
|
|
|
|
|
|
my $models = $dbi->models; |
1765
|
|
|
|
|
|
|
$dbi = $dbi->models(\%models); |
1766
|
|
|
|
|
|
|
|
1767
|
|
|
|
|
|
|
Models, included by C method. |
1768
|
|
|
|
|
|
|
|
1769
|
|
|
|
|
|
|
=head2 mytable_symbol |
1770
|
|
|
|
|
|
|
|
1771
|
|
|
|
|
|
|
Symbol to specify own columns in select method column option, default to '__MY__'. |
1772
|
|
|
|
|
|
|
|
1773
|
|
|
|
|
|
|
$dbi->table('book')->select({__MY__ => '*'}); |
1774
|
|
|
|
|
|
|
|
1775
|
|
|
|
|
|
|
=head2 column_name_lc; |
1776
|
|
|
|
|
|
|
|
1777
|
|
|
|
|
|
|
Set NAME_lc in the Statement Handle Attributes to true when the DBI statement handle is created. |
1778
|
|
|
|
|
|
|
|
1779
|
|
|
|
|
|
|
=head2 option |
1780
|
|
|
|
|
|
|
|
1781
|
|
|
|
|
|
|
my $option = $dbi->option; |
1782
|
|
|
|
|
|
|
$dbi = $dbi->option($option); |
1783
|
|
|
|
|
|
|
|
1784
|
|
|
|
|
|
|
L option, used when C method is executed. |
1785
|
|
|
|
|
|
|
Each value in option override the value of C. |
1786
|
|
|
|
|
|
|
|
1787
|
|
|
|
|
|
|
=head2 password |
1788
|
|
|
|
|
|
|
|
1789
|
|
|
|
|
|
|
my $password = $dbi->password; |
1790
|
|
|
|
|
|
|
$dbi = $dbi->password('lkj&le`@s'); |
1791
|
|
|
|
|
|
|
|
1792
|
|
|
|
|
|
|
Password, used when C method is executed. |
1793
|
|
|
|
|
|
|
|
1794
|
|
|
|
|
|
|
=head2 quote |
1795
|
|
|
|
|
|
|
|
1796
|
|
|
|
|
|
|
my quote = $dbi->quote; |
1797
|
|
|
|
|
|
|
$dbi = $dbi->quote('"'); |
1798
|
|
|
|
|
|
|
|
1799
|
|
|
|
|
|
|
Reserved word quote. |
1800
|
|
|
|
|
|
|
Default to double quote '"' except for mysql. |
1801
|
|
|
|
|
|
|
In mysql, default to back quote '`' |
1802
|
|
|
|
|
|
|
|
1803
|
|
|
|
|
|
|
You can set quote pair. |
1804
|
|
|
|
|
|
|
|
1805
|
|
|
|
|
|
|
$dbi->quote('[]'); |
1806
|
|
|
|
|
|
|
|
1807
|
|
|
|
|
|
|
=head2 result_class |
1808
|
|
|
|
|
|
|
|
1809
|
|
|
|
|
|
|
my $result_class = $dbi->result_class; |
1810
|
|
|
|
|
|
|
$dbi = $dbi->result_class('DBIx::Custom::Result'); |
1811
|
|
|
|
|
|
|
|
1812
|
|
|
|
|
|
|
Result class, default to L. |
1813
|
|
|
|
|
|
|
|
1814
|
|
|
|
|
|
|
=head2 safety_character |
1815
|
|
|
|
|
|
|
|
1816
|
|
|
|
|
|
|
my $safety_character = $dbi->safety_character; |
1817
|
|
|
|
|
|
|
$dbi = $dbi->safety_character($character); |
1818
|
|
|
|
|
|
|
|
1819
|
|
|
|
|
|
|
Regex of safety character for table and column name, default to 'a-zA-Z_'. |
1820
|
|
|
|
|
|
|
Note that you don't have to specify like '[a-zA-Z_]'. |
1821
|
|
|
|
|
|
|
|
1822
|
|
|
|
|
|
|
=head2 separator |
1823
|
|
|
|
|
|
|
|
1824
|
|
|
|
|
|
|
my $separator = $dbi->separator; |
1825
|
|
|
|
|
|
|
$dbi = $dbi->separator('-'); |
1826
|
|
|
|
|
|
|
|
1827
|
|
|
|
|
|
|
Separator which join table name and column name. |
1828
|
|
|
|
|
|
|
This have effect to C and C method, |
1829
|
|
|
|
|
|
|
and C |
1830
|
|
|
|
|
|
|
|
1831
|
|
|
|
|
|
|
Default to C<.>. |
1832
|
|
|
|
|
|
|
|
1833
|
|
|
|
|
|
|
=head2 user |
1834
|
|
|
|
|
|
|
|
1835
|
|
|
|
|
|
|
my $user = $dbi->user; |
1836
|
|
|
|
|
|
|
$dbi = $dbi->user('Ken'); |
1837
|
|
|
|
|
|
|
|
1838
|
|
|
|
|
|
|
User name, used when C method is executed. |
1839
|
|
|
|
|
|
|
|
1840
|
|
|
|
|
|
|
=head2 user_column_info |
1841
|
|
|
|
|
|
|
|
1842
|
|
|
|
|
|
|
my $user_column_info = $dbi->user_column_info; |
1843
|
|
|
|
|
|
|
$dbi = $dbi->user_column_info($user_column_info); |
1844
|
|
|
|
|
|
|
|
1845
|
|
|
|
|
|
|
You can set the date like the following one. |
1846
|
|
|
|
|
|
|
|
1847
|
|
|
|
|
|
|
[ |
1848
|
|
|
|
|
|
|
{table => 'book', column => 'title', info => {...}}, |
1849
|
|
|
|
|
|
|
{table => 'author', column => 'name', info => {...}} |
1850
|
|
|
|
|
|
|
] |
1851
|
|
|
|
|
|
|
|
1852
|
|
|
|
|
|
|
Usually, you set return value of C. |
1853
|
|
|
|
|
|
|
|
1854
|
|
|
|
|
|
|
my $user_column_info |
1855
|
|
|
|
|
|
|
= $dbi->get_column_info(exclude_table => qr/^system/); |
1856
|
|
|
|
|
|
|
$dbi->user_column_info($user_column_info); |
1857
|
|
|
|
|
|
|
|
1858
|
|
|
|
|
|
|
If C is set, C use C |
1859
|
|
|
|
|
|
|
to find column info. this is very fast. |
1860
|
|
|
|
|
|
|
|
1861
|
|
|
|
|
|
|
=head2 user_table_info |
1862
|
|
|
|
|
|
|
|
1863
|
|
|
|
|
|
|
my $user_table_info = $dbi->user_table_info; |
1864
|
|
|
|
|
|
|
$dbi = $dbi->user_table_info($user_table_info); |
1865
|
|
|
|
|
|
|
|
1866
|
|
|
|
|
|
|
You can set the following data. |
1867
|
|
|
|
|
|
|
|
1868
|
|
|
|
|
|
|
[ |
1869
|
|
|
|
|
|
|
{table => 'book', info => {...}}, |
1870
|
|
|
|
|
|
|
{table => 'author', info => {...}} |
1871
|
|
|
|
|
|
|
] |
1872
|
|
|
|
|
|
|
|
1873
|
|
|
|
|
|
|
Usually, you can set return value of C. |
1874
|
|
|
|
|
|
|
|
1875
|
|
|
|
|
|
|
my $user_table_info = $dbi->get_table_info(exclude => qr/^system/); |
1876
|
|
|
|
|
|
|
$dbi->user_table_info($user_table_info); |
1877
|
|
|
|
|
|
|
|
1878
|
|
|
|
|
|
|
If C is set, C use C |
1879
|
|
|
|
|
|
|
to find table info. |
1880
|
|
|
|
|
|
|
|
1881
|
|
|
|
|
|
|
=head1 METHODS |
1882
|
|
|
|
|
|
|
|
1883
|
|
|
|
|
|
|
L inherits all methods from L |
1884
|
|
|
|
|
|
|
and use all methods of L |
1885
|
|
|
|
|
|
|
and implements the following new ones. |
1886
|
|
|
|
|
|
|
|
1887
|
|
|
|
|
|
|
=head2 available_datatype |
1888
|
|
|
|
|
|
|
|
1889
|
|
|
|
|
|
|
print $dbi->available_datatype; |
1890
|
|
|
|
|
|
|
|
1891
|
|
|
|
|
|
|
Get available data types. You can use these data types |
1892
|
|
|
|
|
|
|
in C's C and C section. |
1893
|
|
|
|
|
|
|
|
1894
|
|
|
|
|
|
|
=head2 available_typename |
1895
|
|
|
|
|
|
|
|
1896
|
|
|
|
|
|
|
print $dbi->available_typename; |
1897
|
|
|
|
|
|
|
|
1898
|
|
|
|
|
|
|
Get available type names. You can use these type names in |
1899
|
|
|
|
|
|
|
C's C and C section. |
1900
|
|
|
|
|
|
|
|
1901
|
|
|
|
|
|
|
=head2 assign_clause |
1902
|
|
|
|
|
|
|
|
1903
|
|
|
|
|
|
|
my $assign_clause = $dbi->assign_clause({title => 'a', age => 2}); |
1904
|
|
|
|
|
|
|
|
1905
|
|
|
|
|
|
|
Create assign clause |
1906
|
|
|
|
|
|
|
|
1907
|
|
|
|
|
|
|
title = :title, author = :author |
1908
|
|
|
|
|
|
|
|
1909
|
|
|
|
|
|
|
This is used to create update clause. |
1910
|
|
|
|
|
|
|
|
1911
|
|
|
|
|
|
|
"update book set " . $dbi->assign_clause({title => 'a', age => 2}); |
1912
|
|
|
|
|
|
|
|
1913
|
|
|
|
|
|
|
=head2 column |
1914
|
|
|
|
|
|
|
|
1915
|
|
|
|
|
|
|
my $column = $dbi->column(book => ['author', 'title']); |
1916
|
|
|
|
|
|
|
|
1917
|
|
|
|
|
|
|
Create column clause. The following column clause is created. |
1918
|
|
|
|
|
|
|
|
1919
|
|
|
|
|
|
|
book.author as "book.author", |
1920
|
|
|
|
|
|
|
book.title as "book.title" |
1921
|
|
|
|
|
|
|
|
1922
|
|
|
|
|
|
|
You can change separator by C attribute. |
1923
|
|
|
|
|
|
|
|
1924
|
|
|
|
|
|
|
# Separator is hyphen |
1925
|
|
|
|
|
|
|
$dbi->separator('-'); |
1926
|
|
|
|
|
|
|
|
1927
|
|
|
|
|
|
|
book.author as "book-author", |
1928
|
|
|
|
|
|
|
book.title as "book-title" |
1929
|
|
|
|
|
|
|
|
1930
|
|
|
|
|
|
|
=head2 connect |
1931
|
|
|
|
|
|
|
|
1932
|
|
|
|
|
|
|
# DBI compatible arguments |
1933
|
|
|
|
|
|
|
my $dbi = DBIx::Custom->connect( |
1934
|
|
|
|
|
|
|
"dbi:mysql:database=dbname", |
1935
|
|
|
|
|
|
|
'ken', |
1936
|
|
|
|
|
|
|
'!LFKD%$&', |
1937
|
|
|
|
|
|
|
{mysql_enable_utf8 => 1} |
1938
|
|
|
|
|
|
|
); |
1939
|
|
|
|
|
|
|
|
1940
|
|
|
|
|
|
|
# pass DBIx::Custom attributes |
1941
|
|
|
|
|
|
|
my $dbi = DBIx::Custom->connect( |
1942
|
|
|
|
|
|
|
dsn => "dbi:mysql:database=dbname", |
1943
|
|
|
|
|
|
|
user => 'ken', |
1944
|
|
|
|
|
|
|
password => '!LFKD%$&', |
1945
|
|
|
|
|
|
|
option => {mysql_enable_utf8 => 1} |
1946
|
|
|
|
|
|
|
); |
1947
|
|
|
|
|
|
|
|
1948
|
|
|
|
|
|
|
Connect to the database and create a new L object. |
1949
|
|
|
|
|
|
|
|
1950
|
|
|
|
|
|
|
L is a wrapper of L. |
1951
|
|
|
|
|
|
|
C and C options are true, |
1952
|
|
|
|
|
|
|
and C option is false by default. |
1953
|
|
|
|
|
|
|
|
1954
|
|
|
|
|
|
|
=head2 create_model |
1955
|
|
|
|
|
|
|
|
1956
|
|
|
|
|
|
|
$dbi->create_model('book'); |
1957
|
|
|
|
|
|
|
$dbi->create_model( |
1958
|
|
|
|
|
|
|
'book', |
1959
|
|
|
|
|
|
|
join => [ |
1960
|
|
|
|
|
|
|
'inner join company on book.comparny_id = company.id' |
1961
|
|
|
|
|
|
|
] |
1962
|
|
|
|
|
|
|
); |
1963
|
|
|
|
|
|
|
$dbi->create_model( |
1964
|
|
|
|
|
|
|
table => 'book', |
1965
|
|
|
|
|
|
|
join => [ |
1966
|
|
|
|
|
|
|
'inner join company on book.comparny_id = company.id' |
1967
|
|
|
|
|
|
|
], |
1968
|
|
|
|
|
|
|
); |
1969
|
|
|
|
|
|
|
|
1970
|
|
|
|
|
|
|
Create L object and initialize model. |
1971
|
|
|
|
|
|
|
Model columns attribute is automatically set. |
1972
|
|
|
|
|
|
|
You can use this model by using C method. |
1973
|
|
|
|
|
|
|
|
1974
|
|
|
|
|
|
|
$dbi->model('book')->select(...); |
1975
|
|
|
|
|
|
|
|
1976
|
|
|
|
|
|
|
You can use model name which different from table name |
1977
|
|
|
|
|
|
|
|
1978
|
|
|
|
|
|
|
$dbi->create_model(name => 'book1', table => 'book'); |
1979
|
|
|
|
|
|
|
$dbi->model('book1')->select(...); |
1980
|
|
|
|
|
|
|
|
1981
|
|
|
|
|
|
|
$dbi->create_model( |
1982
|
|
|
|
|
|
|
table => 'book', |
1983
|
|
|
|
|
|
|
join => [ |
1984
|
|
|
|
|
|
|
'inner join company on book.comparny_id = company.id' |
1985
|
|
|
|
|
|
|
], |
1986
|
|
|
|
|
|
|
); |
1987
|
|
|
|
|
|
|
|
1988
|
|
|
|
|
|
|
C option change column names to lower case. |
1989
|
|
|
|
|
|
|
|
1990
|
|
|
|
|
|
|
$dbi->create_model( |
1991
|
|
|
|
|
|
|
table => 'book', |
1992
|
|
|
|
|
|
|
join => [ |
1993
|
|
|
|
|
|
|
'inner join company on book.comparny_id = company.id' |
1994
|
|
|
|
|
|
|
], |
1995
|
|
|
|
|
|
|
column_name_lc => 1, |
1996
|
|
|
|
|
|
|
); |
1997
|
|
|
|
|
|
|
|
1998
|
|
|
|
|
|
|
=head2 dbh |
1999
|
|
|
|
|
|
|
|
2000
|
|
|
|
|
|
|
my $dbh = $dbi->dbh; |
2001
|
|
|
|
|
|
|
|
2002
|
|
|
|
|
|
|
Get L database handle. if C is set, you can get |
2003
|
|
|
|
|
|
|
database handle through C object. |
2004
|
|
|
|
|
|
|
|
2005
|
|
|
|
|
|
|
=head2 delete |
2006
|
|
|
|
|
|
|
|
2007
|
|
|
|
|
|
|
$dbi->delete(table => 'book', where => {title => 'Perl'}); |
2008
|
|
|
|
|
|
|
|
2009
|
|
|
|
|
|
|
Execute delete statement. |
2010
|
|
|
|
|
|
|
|
2011
|
|
|
|
|
|
|
The following options are available. |
2012
|
|
|
|
|
|
|
|
2013
|
|
|
|
|
|
|
B |
2014
|
|
|
|
|
|
|
|
2015
|
|
|
|
|
|
|
C method use all of C method's options, |
2016
|
|
|
|
|
|
|
and use the following new ones. |
2017
|
|
|
|
|
|
|
|
2018
|
|
|
|
|
|
|
=over 4 |
2019
|
|
|
|
|
|
|
|
2020
|
|
|
|
|
|
|
=item prefix |
2021
|
|
|
|
|
|
|
|
2022
|
|
|
|
|
|
|
prefix => 'some' |
2023
|
|
|
|
|
|
|
|
2024
|
|
|
|
|
|
|
prefix before table name section. |
2025
|
|
|
|
|
|
|
|
2026
|
|
|
|
|
|
|
delete some from book |
2027
|
|
|
|
|
|
|
|
2028
|
|
|
|
|
|
|
=item table |
2029
|
|
|
|
|
|
|
|
2030
|
|
|
|
|
|
|
table => 'book' |
2031
|
|
|
|
|
|
|
|
2032
|
|
|
|
|
|
|
Table name. |
2033
|
|
|
|
|
|
|
|
2034
|
|
|
|
|
|
|
=item where |
2035
|
|
|
|
|
|
|
|
2036
|
|
|
|
|
|
|
Same as C |
2037
|
|
|
|
|
|
|
|
2038
|
|
|
|
|
|
|
=back |
2039
|
|
|
|
|
|
|
|
2040
|
|
|
|
|
|
|
=head2 delete_all |
2041
|
|
|
|
|
|
|
|
2042
|
|
|
|
|
|
|
$dbi->delete_all(table => $table); |
2043
|
|
|
|
|
|
|
|
2044
|
|
|
|
|
|
|
Execute delete statement for all rows. |
2045
|
|
|
|
|
|
|
Options is same as C. |
2046
|
|
|
|
|
|
|
|
2047
|
|
|
|
|
|
|
=head2 each_column |
2048
|
|
|
|
|
|
|
|
2049
|
|
|
|
|
|
|
$dbi->each_column( |
2050
|
|
|
|
|
|
|
sub { |
2051
|
|
|
|
|
|
|
my ($dbi, $table, $column, $column_info) = @_; |
2052
|
|
|
|
|
|
|
|
2053
|
|
|
|
|
|
|
my $type = $column_info->{TYPE_NAME}; |
2054
|
|
|
|
|
|
|
|
2055
|
|
|
|
|
|
|
if ($type eq 'DATE') { |
2056
|
|
|
|
|
|
|
# ... |
2057
|
|
|
|
|
|
|
} |
2058
|
|
|
|
|
|
|
} |
2059
|
|
|
|
|
|
|
); |
2060
|
|
|
|
|
|
|
|
2061
|
|
|
|
|
|
|
Iterate all column informations in database. |
2062
|
|
|
|
|
|
|
Argument is callback which is executed when one column is found. |
2063
|
|
|
|
|
|
|
Callback receive four arguments. C, C,
2064
|
|
|
|
|
|
|
C, and C. |
2065
|
|
|
|
|
|
|
|
2066
|
|
|
|
|
|
|
If C is set, C method use C |
2067
|
|
|
|
|
|
|
information, you can improve the performance of C in |
2068
|
|
|
|
|
|
|
the following way. |
2069
|
|
|
|
|
|
|
|
2070
|
|
|
|
|
|
|
my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/); |
2071
|
|
|
|
|
|
|
$dbi->user_column_info($column_info); |
2072
|
|
|
|
|
|
|
$dbi->each_column(sub { ... }); |
2073
|
|
|
|
|
|
|
|
2074
|
|
|
|
|
|
|
=head2 each_table |
2075
|
|
|
|
|
|
|
|
2076
|
|
|
|
|
|
|
$dbi->each_table( |
2077
|
|
|
|
|
|
|
sub { |
2078
|
|
|
|
|
|
|
my ($dbi, $table, $table_info) = @_; |
2079
|
|
|
|
|
|
|
|
2080
|
|
|
|
|
|
|
my $table_name = $table_info->{TABLE_NAME}; |
2081
|
|
|
|
|
|
|
} |
2082
|
|
|
|
|
|
|
); |
2083
|
|
|
|
|
|
|
|
2084
|
|
|
|
|
|
|
Iterate all table information from in database. |
2085
|
|
|
|
|
|
|
Argument is callback which is executed when one table is found. |
2086
|
|
|
|
|
|
|
Callback receive three arguments, C, C,
2087
|
|
|
|
|
|
|
C.
2088
|
|
|
|
|
|
|
|
2089
|
|
|
|
|
|
|
If C is set, C method use C |
2090
|
|
|
|
|
|
|
information, you can improve the performance of C in |
2091
|
|
|
|
|
|
|
the following way. |
2092
|
|
|
|
|
|
|
|
2093
|
|
|
|
|
|
|
my $table_infos = $dbi->get_table_info(exclude => qr/^system_/); |
2094
|
|
|
|
|
|
|
$dbi->user_table_info($table_info); |
2095
|
|
|
|
|
|
|
$dbi->each_table(sub { ... }); |
2096
|
|
|
|
|
|
|
|
2097
|
|
|
|
|
|
|
=head2 execute |
2098
|
|
|
|
|
|
|
|
2099
|
|
|
|
|
|
|
my $result = $dbi->execute( |
2100
|
|
|
|
|
|
|
"select * from book where title = :title and author like :author", |
2101
|
|
|
|
|
|
|
{title => 'Perl', author => '%Ken%'} |
2102
|
|
|
|
|
|
|
); |
2103
|
|
|
|
|
|
|
|
2104
|
|
|
|
|
|
|
my $result = $dbi->execute( |
2105
|
|
|
|
|
|
|
"select * from book where title = :book.title and author like :book.author", |
2106
|
|
|
|
|
|
|
{'book.title' => 'Perl', 'book.author' => '%Ken%'} |
2107
|
|
|
|
|
|
|
); |
2108
|
|
|
|
|
|
|
|
2109
|
|
|
|
|
|
|
Execute SQL. SQL can contain column parameter such as :author and :title. |
2110
|
|
|
|
|
|
|
You can append table name to column name such as :book.title and :book.author. |
2111
|
|
|
|
|
|
|
Second argument is data, embedded into column parameter. |
2112
|
|
|
|
|
|
|
Return value is L object when select statement is executed, |
2113
|
|
|
|
|
|
|
or the count of affected rows when insert, update, delete statement is executed. |
2114
|
|
|
|
|
|
|
|
2115
|
|
|
|
|
|
|
Named placeholder such as C<:title> is replaced by placeholder C>. |
2116
|
|
|
|
|
|
|
|
2117
|
|
|
|
|
|
|
# Original |
2118
|
|
|
|
|
|
|
select * from book where title = :title and author like :author |
2119
|
|
|
|
|
|
|
|
2120
|
|
|
|
|
|
|
# Replaced |
2121
|
|
|
|
|
|
|
select * from where title = ? and author like ?; |
2122
|
|
|
|
|
|
|
|
2123
|
|
|
|
|
|
|
You can specify operator with named placeholder |
2124
|
|
|
|
|
|
|
by C syntax. |
2125
|
|
|
|
|
|
|
|
2126
|
|
|
|
|
|
|
# Original |
2127
|
|
|
|
|
|
|
select * from book where :title{=} and :author{like} |
2128
|
|
|
|
|
|
|
|
2129
|
|
|
|
|
|
|
# Replaced |
2130
|
|
|
|
|
|
|
select * from where title = ? and author like ?; |
2131
|
|
|
|
|
|
|
|
2132
|
|
|
|
|
|
|
Note that colons in time format such as 12:13:15 is an exception, |
2133
|
|
|
|
|
|
|
it is not parsed as named placeholder. |
2134
|
|
|
|
|
|
|
If you want to use colon generally, you must escape it by C<\\> |
2135
|
|
|
|
|
|
|
|
2136
|
|
|
|
|
|
|
select * from where title = "aa\\:bb"; |
2137
|
|
|
|
|
|
|
|
2138
|
|
|
|
|
|
|
B |
2139
|
|
|
|
|
|
|
|
2140
|
|
|
|
|
|
|
The following options are available. |
2141
|
|
|
|
|
|
|
|
2142
|
|
|
|
|
|
|
=over 4 |
2143
|
|
|
|
|
|
|
|
2144
|
|
|
|
|
|
|
=item after_build_sql |
2145
|
|
|
|
|
|
|
|
2146
|
|
|
|
|
|
|
You can filter sql after the sql is build. |
2147
|
|
|
|
|
|
|
|
2148
|
|
|
|
|
|
|
after_build_sql => $code_ref |
2149
|
|
|
|
|
|
|
|
2150
|
|
|
|
|
|
|
The following one is one example. |
2151
|
|
|
|
|
|
|
|
2152
|
|
|
|
|
|
|
$dbi->select( |
2153
|
|
|
|
|
|
|
table => 'book', |
2154
|
|
|
|
|
|
|
column => 'distinct(name)', |
2155
|
|
|
|
|
|
|
after_build_sql => sub { |
2156
|
|
|
|
|
|
|
"select count(*) from ($_[0]) as t1" |
2157
|
|
|
|
|
|
|
} |
2158
|
|
|
|
|
|
|
); |
2159
|
|
|
|
|
|
|
|
2160
|
|
|
|
|
|
|
The following SQL is executed. |
2161
|
|
|
|
|
|
|
|
2162
|
|
|
|
|
|
|
select count(*) from (select distinct(name) from book) as t1; |
2163
|
|
|
|
|
|
|
|
2164
|
|
|
|
|
|
|
=item append |
2165
|
|
|
|
|
|
|
|
2166
|
|
|
|
|
|
|
append => 'order by name' |
2167
|
|
|
|
|
|
|
|
2168
|
|
|
|
|
|
|
Append some statement after SQL. |
2169
|
|
|
|
|
|
|
|
2170
|
|
|
|
|
|
|
=item bind_type |
2171
|
|
|
|
|
|
|
|
2172
|
|
|
|
|
|
|
Specify database bind data type. |
2173
|
|
|
|
|
|
|
|
2174
|
|
|
|
|
|
|
bind_type => {image => DBI::SQL_BLOB} |
2175
|
|
|
|
|
|
|
bind_type => [image => DBI::SQL_BLOB] |
2176
|
|
|
|
|
|
|
bind_type => [[qw/image audio/] => DBI::SQL_BLOB] |
2177
|
|
|
|
|
|
|
|
2178
|
|
|
|
|
|
|
This is used to bind parameter by C of statement handle. |
2179
|
|
|
|
|
|
|
|
2180
|
|
|
|
|
|
|
$sth->bind_param($pos, $value, DBI::SQL_BLOB); |
2181
|
|
|
|
|
|
|
|
2182
|
|
|
|
|
|
|
=item filter |
2183
|
|
|
|
|
|
|
|
2184
|
|
|
|
|
|
|
filter => { |
2185
|
|
|
|
|
|
|
title => sub { uc $_[0] } |
2186
|
|
|
|
|
|
|
author => sub { uc $_[0] } |
2187
|
|
|
|
|
|
|
} |
2188
|
|
|
|
|
|
|
|
2189
|
|
|
|
|
|
|
# Filter name |
2190
|
|
|
|
|
|
|
filter => { |
2191
|
|
|
|
|
|
|
title => 'upper_case', |
2192
|
|
|
|
|
|
|
author => 'upper_case' |
2193
|
|
|
|
|
|
|
} |
2194
|
|
|
|
|
|
|
|
2195
|
|
|
|
|
|
|
# At once |
2196
|
|
|
|
|
|
|
filter => [ |
2197
|
|
|
|
|
|
|
[qw/title author/] => sub { uc $_[0] } |
2198
|
|
|
|
|
|
|
] |
2199
|
|
|
|
|
|
|
|
2200
|
|
|
|
|
|
|
Filter. You can set subroutine or filter name |
2201
|
|
|
|
|
|
|
registered by C. |
2202
|
|
|
|
|
|
|
This filter is executed before data is saved into database. |
2203
|
|
|
|
|
|
|
and before type rule filter is executed. |
2204
|
|
|
|
|
|
|
|
2205
|
|
|
|
|
|
|
=item reuse |
2206
|
|
|
|
|
|
|
|
2207
|
|
|
|
|
|
|
reuse => $hash_ref |
2208
|
|
|
|
|
|
|
|
2209
|
|
|
|
|
|
|
Reuse statement handle in same SQL. |
2210
|
|
|
|
|
|
|
|
2211
|
|
|
|
|
|
|
my $reuse = {}; |
2212
|
|
|
|
|
|
|
$dbi->execute($sql, $param, reuse => $reuse); |
2213
|
|
|
|
|
|
|
|
2214
|
|
|
|
|
|
|
This will improved performance when you want to execute same sql repeatedly. |
2215
|
|
|
|
|
|
|
|
2216
|
|
|
|
|
|
|
=item table |
2217
|
|
|
|
|
|
|
|
2218
|
|
|
|
|
|
|
table => 'author' |
2219
|
|
|
|
|
|
|
|
2220
|
|
|
|
|
|
|
If you want to omit table name in column name |
2221
|
|
|
|
|
|
|
and enable C and C type filter, |
2222
|
|
|
|
|
|
|
You must set C option.
2223
|
|
|
|
|
|
|
|
2224
|
|
|
|
|
|
|
$dbi->execute("select * from book where title = :title and author = :author", |
2225
|
|
|
|
|
|
|
{title => 'Perl', author => 'Ken', table => 'book'); |
2226
|
|
|
|
|
|
|
|
2227
|
|
|
|
|
|
|
# Same |
2228
|
|
|
|
|
|
|
$dbi->execute( |
2229
|
|
|
|
|
|
|
"select * from book where title = :book.title and author = :book.author", |
2230
|
|
|
|
|
|
|
{title => 'Perl', author => 'Ken'); |
2231
|
|
|
|
|
|
|
|
2232
|
|
|
|
|
|
|
=item table_alias |
2233
|
|
|
|
|
|
|
|
2234
|
|
|
|
|
|
|
table_alias => {worker => 'user'} # {ALIAS => TABLE} |
2235
|
|
|
|
|
|
|
|
2236
|
|
|
|
|
|
|
Table alias. Key is alias table name, value is real table name, . |
2237
|
|
|
|
|
|
|
If you set C, you can enable C and C type rule |
2238
|
|
|
|
|
|
|
on alias table name. |
2239
|
|
|
|
|
|
|
|
2240
|
|
|
|
|
|
|
=item type_rule_off |
2241
|
|
|
|
|
|
|
|
2242
|
|
|
|
|
|
|
type_rule_off => 1 |
2243
|
|
|
|
|
|
|
|
2244
|
|
|
|
|
|
|
Turn C and C type rule off. |
2245
|
|
|
|
|
|
|
|
2246
|
|
|
|
|
|
|
=item type_rule1_off |
2247
|
|
|
|
|
|
|
|
2248
|
|
|
|
|
|
|
type_rule1_off => 1 |
2249
|
|
|
|
|
|
|
|
2250
|
|
|
|
|
|
|
Turn C type rule off. |
2251
|
|
|
|
|
|
|
|
2252
|
|
|
|
|
|
|
=item type_rule2_off |
2253
|
|
|
|
|
|
|
|
2254
|
|
|
|
|
|
|
type_rule2_off => 1 |
2255
|
|
|
|
|
|
|
|
2256
|
|
|
|
|
|
|
Turn C type rule off. |
2257
|
|
|
|
|
|
|
|
2258
|
|
|
|
|
|
|
=head2 get_column_info |
2259
|
|
|
|
|
|
|
|
2260
|
|
|
|
|
|
|
my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/); |
2261
|
|
|
|
|
|
|
|
2262
|
|
|
|
|
|
|
get column information except for one which match C pattern. |
2263
|
|
|
|
|
|
|
|
2264
|
|
|
|
|
|
|
[ |
2265
|
|
|
|
|
|
|
{table => 'book', column => 'title', info => {...}}, |
2266
|
|
|
|
|
|
|
{table => 'author', column => 'name' info => {...}} |
2267
|
|
|
|
|
|
|
] |
2268
|
|
|
|
|
|
|
|
2269
|
|
|
|
|
|
|
=head2 get_table_info |
2270
|
|
|
|
|
|
|
|
2271
|
|
|
|
|
|
|
my $table_infos = $dbi->get_table_info(exclude => qr/^system_/); |
2272
|
|
|
|
|
|
|
|
2273
|
|
|
|
|
|
|
get table information except for one which match C pattern. |
2274
|
|
|
|
|
|
|
|
2275
|
|
|
|
|
|
|
[ |
2276
|
|
|
|
|
|
|
{table => 'book', info => {...}}, |
2277
|
|
|
|
|
|
|
{table => 'author', info => {...}} |
2278
|
|
|
|
|
|
|
] |
2279
|
|
|
|
|
|
|
|
2280
|
|
|
|
|
|
|
You can set this value to C. |
2281
|
|
|
|
|
|
|
|
2282
|
|
|
|
|
|
|
=head2 insert |
2283
|
|
|
|
|
|
|
|
2284
|
|
|
|
|
|
|
$dbi->insert({title => 'Perl', author => 'Ken'}, table => 'book'); |
2285
|
|
|
|
|
|
|
|
2286
|
|
|
|
|
|
|
Execute insert statement. First argument is row data. Return value is |
2287
|
|
|
|
|
|
|
affected row count. |
2288
|
|
|
|
|
|
|
|
2289
|
|
|
|
|
|
|
If you want to set constant value to row data, use scalar reference |
2290
|
|
|
|
|
|
|
as parameter value. |
2291
|
|
|
|
|
|
|
|
2292
|
|
|
|
|
|
|
{date => \"NOW()"} |
2293
|
|
|
|
|
|
|
|
2294
|
|
|
|
|
|
|
You can pass multiple parameters, this is very fast. |
2295
|
|
|
|
|
|
|
|
2296
|
|
|
|
|
|
|
$dbi->insert( |
2297
|
|
|
|
|
|
|
[ |
2298
|
|
|
|
|
|
|
{title => 'Perl', author => 'Ken'}, |
2299
|
|
|
|
|
|
|
{title => 'Ruby', author => 'Tom'} |
2300
|
|
|
|
|
|
|
], |
2301
|
|
|
|
|
|
|
table => 'book' |
2302
|
|
|
|
|
|
|
); |
2303
|
|
|
|
|
|
|
|
2304
|
|
|
|
|
|
|
In multiple insert, you can't use C option. |
2305
|
|
|
|
|
|
|
and only first parameter is used to create sql. |
2306
|
|
|
|
|
|
|
|
2307
|
|
|
|
|
|
|
B |
2308
|
|
|
|
|
|
|
|
2309
|
|
|
|
|
|
|
C method use all of C method's options, |
2310
|
|
|
|
|
|
|
and use the following new ones. |
2311
|
|
|
|
|
|
|
|
2312
|
|
|
|
|
|
|
=over 4 |
2313
|
|
|
|
|
|
|
|
2314
|
|
|
|
|
|
|
=item bulk_insert |
2315
|
|
|
|
|
|
|
|
2316
|
|
|
|
|
|
|
bulk_insert => 1 |
2317
|
|
|
|
|
|
|
|
2318
|
|
|
|
|
|
|
bulk insert is executed if database support bulk insert and |
2319
|
|
|
|
|
|
|
multiple parameters is passed to C. |
2320
|
|
|
|
|
|
|
The SQL like the following one is executed. |
2321
|
|
|
|
|
|
|
|
2322
|
|
|
|
|
|
|
insert into book (id, title) values (?, ?), (?, ?); |
2323
|
|
|
|
|
|
|
|
2324
|
|
|
|
|
|
|
=item ctime |
2325
|
|
|
|
|
|
|
|
2326
|
|
|
|
|
|
|
ctime => 'created_time' |
2327
|
|
|
|
|
|
|
|
2328
|
|
|
|
|
|
|
Created time column name. time when row is created is set to the column. |
2329
|
|
|
|
|
|
|
default time format is "YYYY-mm-dd HH:MM:SS", which can be changed by |
2330
|
|
|
|
|
|
|
C attribute. |
2331
|
|
|
|
|
|
|
|
2332
|
|
|
|
|
|
|
=item prefix |
2333
|
|
|
|
|
|
|
|
2334
|
|
|
|
|
|
|
prefix => 'or replace' |
2335
|
|
|
|
|
|
|
|
2336
|
|
|
|
|
|
|
prefix before table name section |
2337
|
|
|
|
|
|
|
|
2338
|
|
|
|
|
|
|
insert or replace into book |
2339
|
|
|
|
|
|
|
|
2340
|
|
|
|
|
|
|
=item table |
2341
|
|
|
|
|
|
|
|
2342
|
|
|
|
|
|
|
table => 'book' |
2343
|
|
|
|
|
|
|
|
2344
|
|
|
|
|
|
|
Table name. |
2345
|
|
|
|
|
|
|
|
2346
|
|
|
|
|
|
|
=item mtime |
2347
|
|
|
|
|
|
|
|
2348
|
|
|
|
|
|
|
This option is same as C method C option. |
2349
|
|
|
|
|
|
|
|
2350
|
|
|
|
|
|
|
=item wrap |
2351
|
|
|
|
|
|
|
|
2352
|
|
|
|
|
|
|
wrap => {price => sub { "max($_[0])" }} |
2353
|
|
|
|
|
|
|
|
2354
|
|
|
|
|
|
|
placeholder wrapped string. |
2355
|
|
|
|
|
|
|
|
2356
|
|
|
|
|
|
|
If the following statement |
2357
|
|
|
|
|
|
|
|
2358
|
|
|
|
|
|
|
$dbi->insert({price => 100}, table => 'book', |
2359
|
|
|
|
|
|
|
{price => sub { "$_[0] + 5" }}); |
2360
|
|
|
|
|
|
|
|
2361
|
|
|
|
|
|
|
is executed, the following SQL is executed. |
2362
|
|
|
|
|
|
|
|
2363
|
|
|
|
|
|
|
insert into book price values ( ? + 5 ); |
2364
|
|
|
|
|
|
|
|
2365
|
|
|
|
|
|
|
=back |
2366
|
|
|
|
|
|
|
|
2367
|
|
|
|
|
|
|
=over 4 |
2368
|
|
|
|
|
|
|
|
2369
|
|
|
|
|
|
|
=head2 include_model |
2370
|
|
|
|
|
|
|
|
2371
|
|
|
|
|
|
|
$dbi->include_model('MyModel'); |
2372
|
|
|
|
|
|
|
|
2373
|
|
|
|
|
|
|
Include models from specified namespace, |
2374
|
|
|
|
|
|
|
the following layout is needed to include models. |
2375
|
|
|
|
|
|
|
|
2376
|
|
|
|
|
|
|
lib / MyModel.pm |
2377
|
|
|
|
|
|
|
/ MyModel / book.pm |
2378
|
|
|
|
|
|
|
/ company.pm |
2379
|
|
|
|
|
|
|
|
2380
|
|
|
|
|
|
|
Name space module, extending L. |
2381
|
|
|
|
|
|
|
|
2382
|
|
|
|
|
|
|
B |
2383
|
|
|
|
|
|
|
|
2384
|
|
|
|
|
|
|
package MyModel; |
2385
|
|
|
|
|
|
|
use DBIx::Custom::Model -base; |
2386
|
|
|
|
|
|
|
|
2387
|
|
|
|
|
|
|
1; |
2388
|
|
|
|
|
|
|
|
2389
|
|
|
|
|
|
|
Model modules, extending name space module. |
2390
|
|
|
|
|
|
|
|
2391
|
|
|
|
|
|
|
B |
2392
|
|
|
|
|
|
|
|
2393
|
|
|
|
|
|
|
package MyModel::book; |
2394
|
|
|
|
|
|
|
use MyModel -base; |
2395
|
|
|
|
|
|
|
|
2396
|
|
|
|
|
|
|
1; |
2397
|
|
|
|
|
|
|
|
2398
|
|
|
|
|
|
|
B |
2399
|
|
|
|
|
|
|
|
2400
|
|
|
|
|
|
|
package MyModel::company; |
2401
|
|
|
|
|
|
|
use MyModel -base; |
2402
|
|
|
|
|
|
|
|
2403
|
|
|
|
|
|
|
1; |
2404
|
|
|
|
|
|
|
|
2405
|
|
|
|
|
|
|
MyModel::book and MyModel::company is included by C. |
2406
|
|
|
|
|
|
|
|
2407
|
|
|
|
|
|
|
You can get model object by C. |
2408
|
|
|
|
|
|
|
|
2409
|
|
|
|
|
|
|
my $book_model = $dbi->model('book'); |
2410
|
|
|
|
|
|
|
my $company_model = $dbi->model('company'); |
2411
|
|
|
|
|
|
|
|
2412
|
|
|
|
|
|
|
You can include full-qualified table name like C |
2413
|
|
|
|
|
|
|
|
2414
|
|
|
|
|
|
|
lib / MyModel.pm |
2415
|
|
|
|
|
|
|
/ MyModel / main / book.pm |
2416
|
|
|
|
|
|
|
/ company.pm |
2417
|
|
|
|
|
|
|
|
2418
|
|
|
|
|
|
|
my $main_book = $self->model('main.book'); |
2419
|
|
|
|
|
|
|
|
2420
|
|
|
|
|
|
|
See L to know model features. |
2421
|
|
|
|
|
|
|
|
2422
|
|
|
|
|
|
|
=head2 like_value |
2423
|
|
|
|
|
|
|
|
2424
|
|
|
|
|
|
|
my $like_value = $dbi->like_value |
2425
|
|
|
|
|
|
|
|
2426
|
|
|
|
|
|
|
Code reference which return a value for the like value. |
2427
|
|
|
|
|
|
|
|
2428
|
|
|
|
|
|
|
sub { "%$_[0]%" } |
2429
|
|
|
|
|
|
|
|
2430
|
|
|
|
|
|
|
=head2 mapper |
2431
|
|
|
|
|
|
|
|
2432
|
|
|
|
|
|
|
my $mapper = $dbi->mapper(param => $param); |
2433
|
|
|
|
|
|
|
|
2434
|
|
|
|
|
|
|
Create a new L object. |
2435
|
|
|
|
|
|
|
|
2436
|
|
|
|
|
|
|
=head2 merge_param |
2437
|
|
|
|
|
|
|
|
2438
|
|
|
|
|
|
|
my $param = $dbi->merge_param({key1 => 1}, {key1 => 1, key2 => 2}); |
2439
|
|
|
|
|
|
|
|
2440
|
|
|
|
|
|
|
Merge parameters. The following new parameter is created. |
2441
|
|
|
|
|
|
|
|
2442
|
|
|
|
|
|
|
{key1 => [1, 1], key2 => 2} |
2443
|
|
|
|
|
|
|
|
2444
|
|
|
|
|
|
|
If same keys contains, the value is converted to array reference. |
2445
|
|
|
|
|
|
|
|
2446
|
|
|
|
|
|
|
=head2 model |
2447
|
|
|
|
|
|
|
|
2448
|
|
|
|
|
|
|
my $model = $dbi->model('book'); |
2449
|
|
|
|
|
|
|
|
2450
|
|
|
|
|
|
|
Get a L object |
2451
|
|
|
|
|
|
|
create by C or C |
2452
|
|
|
|
|
|
|
|
2453
|
|
|
|
|
|
|
=head2 mycolumn |
2454
|
|
|
|
|
|
|
|
2455
|
|
|
|
|
|
|
my $column = $dbi->mycolumn(book => ['author', 'title']); |
2456
|
|
|
|
|
|
|
|
2457
|
|
|
|
|
|
|
Create column clause for myself. The following column clause is created. |
2458
|
|
|
|
|
|
|
|
2459
|
|
|
|
|
|
|
book.author as author, |
2460
|
|
|
|
|
|
|
book.title as title |
2461
|
|
|
|
|
|
|
|
2462
|
|
|
|
|
|
|
=head2 new |
2463
|
|
|
|
|
|
|
|
2464
|
|
|
|
|
|
|
my $dbi = DBIx::Custom->new( |
2465
|
|
|
|
|
|
|
dsn => "dbi:mysql:database=dbname", |
2466
|
|
|
|
|
|
|
user => 'ken', |
2467
|
|
|
|
|
|
|
password => '!LFKD%$&', |
2468
|
|
|
|
|
|
|
option => {mysql_enable_utf8 => 1} |
2469
|
|
|
|
|
|
|
); |
2470
|
|
|
|
|
|
|
|
2471
|
|
|
|
|
|
|
Create a new L object. |
2472
|
|
|
|
|
|
|
|
2473
|
|
|
|
|
|
|
=head2 not_exists |
2474
|
|
|
|
|
|
|
|
2475
|
|
|
|
|
|
|
my $not_exists = $dbi->not_exists; |
2476
|
|
|
|
|
|
|
|
2477
|
|
|
|
|
|
|
DBIx::Custom::NotExists object, indicating the column is not exists. |
2478
|
|
|
|
|
|
|
This is used in C of L . |
2479
|
|
|
|
|
|
|
|
2480
|
|
|
|
|
|
|
=head2 order |
2481
|
|
|
|
|
|
|
|
2482
|
|
|
|
|
|
|
my $order = $dbi->order; |
2483
|
|
|
|
|
|
|
|
2484
|
|
|
|
|
|
|
Create a new L object. |
2485
|
|
|
|
|
|
|
|
2486
|
|
|
|
|
|
|
=head2 q |
2487
|
|
|
|
|
|
|
|
2488
|
|
|
|
|
|
|
my $quooted = $dbi->q("title"); |
2489
|
|
|
|
|
|
|
|
2490
|
|
|
|
|
|
|
Quote string by value of C. |
2491
|
|
|
|
|
|
|
|
2492
|
|
|
|
|
|
|
=head2 register_filter |
2493
|
|
|
|
|
|
|
|
2494
|
|
|
|
|
|
|
$dbi->register_filter( |
2495
|
|
|
|
|
|
|
# Time::Piece object to database DATE format |
2496
|
|
|
|
|
|
|
tp_to_date => sub { |
2497
|
|
|
|
|
|
|
my $tp = shift; |
2498
|
|
|
|
|
|
|
return $tp->strftime('%Y-%m-%d'); |
2499
|
|
|
|
|
|
|
}, |
2500
|
|
|
|
|
|
|
# database DATE format to Time::Piece object |
2501
|
|
|
|
|
|
|
date_to_tp => sub { |
2502
|
|
|
|
|
|
|
my $date = shift; |
2503
|
|
|
|
|
|
|
return Time::Piece->strptime($date, '%Y-%m-%d'); |
2504
|
|
|
|
|
|
|
} |
2505
|
|
|
|
|
|
|
); |
2506
|
|
|
|
|
|
|
|
2507
|
|
|
|
|
|
|
Register filters, used by C option of many methods. |
2508
|
|
|
|
|
|
|
|
2509
|
|
|
|
|
|
|
=head2 select |
2510
|
|
|
|
|
|
|
|
2511
|
|
|
|
|
|
|
my $result = $dbi->select( |
2512
|
|
|
|
|
|
|
column => ['author', 'title'], |
2513
|
|
|
|
|
|
|
table => 'book', |
2514
|
|
|
|
|
|
|
where => {author => 'Ken'}, |
2515
|
|
|
|
|
|
|
); |
2516
|
|
|
|
|
|
|
|
2517
|
|
|
|
|
|
|
Execute select statement. |
2518
|
|
|
|
|
|
|
|
2519
|
|
|
|
|
|
|
You can pass odd number arguments. first argument is C. |
2520
|
|
|
|
|
|
|
|
2521
|
|
|
|
|
|
|
my $result = $dbi->select(['author', 'title'], table => 'book'); |
2522
|
|
|
|
|
|
|
|
2523
|
|
|
|
|
|
|
B |
2524
|
|
|
|
|
|
|
|
2525
|
|
|
|
|
|
|
C |
2526
|
|
|
|
|
|
|
and use the following new ones. |
2527
|
|
|
|
|
|
|
|
2528
|
|
|
|
|
|
|
=over 4 |
2529
|
|
|
|
|
|
|
|
2530
|
|
|
|
|
|
|
=item column |
2531
|
|
|
|
|
|
|
|
2532
|
|
|
|
|
|
|
column => 'author' |
2533
|
|
|
|
|
|
|
column => ['author', 'title'] |
2534
|
|
|
|
|
|
|
|
2535
|
|
|
|
|
|
|
Column clause. |
2536
|
|
|
|
|
|
|
|
2537
|
|
|
|
|
|
|
if C is not specified, '*' is set. |
2538
|
|
|
|
|
|
|
|
2539
|
|
|
|
|
|
|
column => '*' |
2540
|
|
|
|
|
|
|
|
2541
|
|
|
|
|
|
|
You can specify hash of array reference. |
2542
|
|
|
|
|
|
|
|
2543
|
|
|
|
|
|
|
column => [ |
2544
|
|
|
|
|
|
|
{book => [qw/author title/]}, |
2545
|
|
|
|
|
|
|
{person => [qw/name age/]} |
2546
|
|
|
|
|
|
|
] |
2547
|
|
|
|
|
|
|
|
2548
|
|
|
|
|
|
|
This is expanded to the following one by using C method. |
2549
|
|
|
|
|
|
|
|
2550
|
|
|
|
|
|
|
book.author as "book.author", |
2551
|
|
|
|
|
|
|
book.title as "book.title", |
2552
|
|
|
|
|
|
|
person.name as "person.name", |
2553
|
|
|
|
|
|
|
person.age as "person.age" |
2554
|
|
|
|
|
|
|
|
2555
|
|
|
|
|
|
|
You can specify own column by C<__MY__>. |
2556
|
|
|
|
|
|
|
|
2557
|
|
|
|
|
|
|
column => [ |
2558
|
|
|
|
|
|
|
{__MY__ => [qw/author title/]}, |
2559
|
|
|
|
|
|
|
] |
2560
|
|
|
|
|
|
|
|
2561
|
|
|
|
|
|
|
This is expanded to the following one by using C method. |
2562
|
|
|
|
|
|
|
|
2563
|
|
|
|
|
|
|
book.author as "author", |
2564
|
|
|
|
|
|
|
book.title as "title", |
2565
|
|
|
|
|
|
|
|
2566
|
|
|
|
|
|
|
C<__MY__> can be changed by C attribute. |
2567
|
|
|
|
|
|
|
|
2568
|
|
|
|
|
|
|
=item param |
2569
|
|
|
|
|
|
|
|
2570
|
|
|
|
|
|
|
param => {'table2.key3' => 5} |
2571
|
|
|
|
|
|
|
|
2572
|
|
|
|
|
|
|
Parameter shown before where clause. |
2573
|
|
|
|
|
|
|
|
2574
|
|
|
|
|
|
|
For example, if you want to contain named placeholder in join clause, |
2575
|
|
|
|
|
|
|
you can pass parameter by C option. |
2576
|
|
|
|
|
|
|
|
2577
|
|
|
|
|
|
|
join => ['inner join (select * from table2 where table2.key3 = :table2.key3)' . |
2578
|
|
|
|
|
|
|
' as table2 on table1.key1 = table2.key1'] |
2579
|
|
|
|
|
|
|
|
2580
|
|
|
|
|
|
|
=item prefix |
2581
|
|
|
|
|
|
|
|
2582
|
|
|
|
|
|
|
prefix => 'SQL_CALC_FOUND_ROWS' |
2583
|
|
|
|
|
|
|
|
2584
|
|
|
|
|
|
|
Prefix of column clause |
2585
|
|
|
|
|
|
|
|
2586
|
|
|
|
|
|
|
select SQL_CALC_FOUND_ROWS title, author from book; |
2587
|
|
|
|
|
|
|
|
2588
|
|
|
|
|
|
|
=item join |
2589
|
|
|
|
|
|
|
|
2590
|
|
|
|
|
|
|
join => [ |
2591
|
|
|
|
|
|
|
'left outer join company on book.company_id = company_id', |
2592
|
|
|
|
|
|
|
'left outer join location on company.location_id = location.id' |
2593
|
|
|
|
|
|
|
] |
2594
|
|
|
|
|
|
|
|
2595
|
|
|
|
|
|
|
Join clause. If column clause or where clause contain table name like "company.name", |
2596
|
|
|
|
|
|
|
join clauses needed when SQL is created is used automatically. |
2597
|
|
|
|
|
|
|
|
2598
|
|
|
|
|
|
|
$dbi->select( |
2599
|
|
|
|
|
|
|
table => 'book', |
2600
|
|
|
|
|
|
|
column => ['company.location_id as location_id'], |
2601
|
|
|
|
|
|
|
where => {'company.name' => 'Orange'}, |
2602
|
|
|
|
|
|
|
join => [ |
2603
|
|
|
|
|
|
|
'left outer join company on book.company_id = company.id', |
2604
|
|
|
|
|
|
|
'left outer join location on company.location_id = location.id' |
2605
|
|
|
|
|
|
|
] |
2606
|
|
|
|
|
|
|
); |
2607
|
|
|
|
|
|
|
|
2608
|
|
|
|
|
|
|
In above select, column and where clause contain "company" table, |
2609
|
|
|
|
|
|
|
the following SQL is created |
2610
|
|
|
|
|
|
|
|
2611
|
|
|
|
|
|
|
select company.location_id as location_id |
2612
|
|
|
|
|
|
|
from book |
2613
|
|
|
|
|
|
|
left outer join company on book.company_id = company.id |
2614
|
|
|
|
|
|
|
where company.name = ?; |
2615
|
|
|
|
|
|
|
|
2616
|
|
|
|
|
|
|
You can specify two table by yourself. This is useful when join parser can't parse |
2617
|
|
|
|
|
|
|
the join clause correctly. |
2618
|
|
|
|
|
|
|
|
2619
|
|
|
|
|
|
|
$dbi->select( |
2620
|
|
|
|
|
|
|
table => 'book', |
2621
|
|
|
|
|
|
|
column => ['company.location_id as location_id'], |
2622
|
|
|
|
|
|
|
where => {'company.name' => 'Orange'}, |
2623
|
|
|
|
|
|
|
join => [ |
2624
|
|
|
|
|
|
|
{ |
2625
|
|
|
|
|
|
|
clause => 'left outer join location on company.location_id = location.id', |
2626
|
|
|
|
|
|
|
table => ['company', 'location'] |
2627
|
|
|
|
|
|
|
} |
2628
|
|
|
|
|
|
|
] |
2629
|
|
|
|
|
|
|
); |
2630
|
|
|
|
|
|
|
|
2631
|
|
|
|
|
|
|
=item table |
2632
|
|
|
|
|
|
|
|
2633
|
|
|
|
|
|
|
table => 'book' |
2634
|
|
|
|
|
|
|
|
2635
|
|
|
|
|
|
|
Table name. |
2636
|
|
|
|
|
|
|
|
2637
|
|
|
|
|
|
|
=item where |
2638
|
|
|
|
|
|
|
|
2639
|
|
|
|
|
|
|
# (1) Hash reference |
2640
|
|
|
|
|
|
|
where => {author => 'Ken', 'title' => ['Perl', 'Ruby']} |
2641
|
|
|
|
|
|
|
# -> where author = 'Ken' and title in ('Perl', 'Ruby') |
2642
|
|
|
|
|
|
|
|
2643
|
|
|
|
|
|
|
# (2) DBIx::Custom::Where object |
2644
|
|
|
|
|
|
|
where => $dbi->where( |
2645
|
|
|
|
|
|
|
clause => ['and', ':author{=}', ':title{like}'], |
2646
|
|
|
|
|
|
|
param => {author => 'Ken', title => '%Perl%'} |
2647
|
|
|
|
|
|
|
) |
2648
|
|
|
|
|
|
|
# -> where author = 'Ken' and title like '%Perl%' |
2649
|
|
|
|
|
|
|
|
2650
|
|
|
|
|
|
|
# (3) Array reference[where clause, parameters, join(optional)] |
2651
|
|
|
|
|
|
|
where => [ |
2652
|
|
|
|
|
|
|
['and', ':author{=}', ':title{like}'], |
2653
|
|
|
|
|
|
|
{author => 'Ken', title => '%Perl%'}, |
2654
|
|
|
|
|
|
|
["left outer join table2 on table1.key1 = table2.key1"] |
2655
|
|
|
|
|
|
|
] |
2656
|
|
|
|
|
|
|
# -> where author = 'Ken' and title like '%Perl%' |
2657
|
|
|
|
|
|
|
|
2658
|
|
|
|
|
|
|
# (4) Array reference[String, Hash reference] |
2659
|
|
|
|
|
|
|
where => [ |
2660
|
|
|
|
|
|
|
':author{=} and :title{like}', |
2661
|
|
|
|
|
|
|
{author => 'Ken', title => '%Perl%'} |
2662
|
|
|
|
|
|
|
] |
2663
|
|
|
|
|
|
|
# -> where author = 'Ken' and title like '%Perl%' |
2664
|
|
|
|
|
|
|
|
2665
|
|
|
|
|
|
|
# (5) String |
2666
|
|
|
|
|
|
|
where => 'title is null' |
2667
|
|
|
|
|
|
|
# -> where title is null |
2668
|
|
|
|
|
|
|
|
2669
|
|
|
|
|
|
|
Where clause. |
2670
|
|
|
|
|
|
|
See also L to know how to create where clause. |
2671
|
|
|
|
|
|
|
|
2672
|
|
|
|
|
|
|
=back |
2673
|
|
|
|
|
|
|
|
2674
|
|
|
|
|
|
|
=head2 type_rule |
2675
|
|
|
|
|
|
|
|
2676
|
|
|
|
|
|
|
$dbi->type_rule( |
2677
|
|
|
|
|
|
|
into1 => { |
2678
|
|
|
|
|
|
|
date => sub { ... }, |
2679
|
|
|
|
|
|
|
datetime => sub { ... } |
2680
|
|
|
|
|
|
|
}, |
2681
|
|
|
|
|
|
|
into2 => { |
2682
|
|
|
|
|
|
|
date => sub { ... }, |
2683
|
|
|
|
|
|
|
datetime => sub { ... } |
2684
|
|
|
|
|
|
|
}, |
2685
|
|
|
|
|
|
|
from1 => { |
2686
|
|
|
|
|
|
|
# DATE |
2687
|
|
|
|
|
|
|
9 => sub { ... }, |
2688
|
|
|
|
|
|
|
# DATETIME or TIMESTAMP |
2689
|
|
|
|
|
|
|
11 => sub { ... }, |
2690
|
|
|
|
|
|
|
} |
2691
|
|
|
|
|
|
|
from2 => { |
2692
|
|
|
|
|
|
|
# DATE |
2693
|
|
|
|
|
|
|
9 => sub { ... }, |
2694
|
|
|
|
|
|
|
# DATETIME or TIMESTAMP |
2695
|
|
|
|
|
|
|
11 => sub { ... }, |
2696
|
|
|
|
|
|
|
} |
2697
|
|
|
|
|
|
|
); |
2698
|
|
|
|
|
|
|
|
2699
|
|
|
|
|
|
|
Filtering rule when data is send into and get from database. |
2700
|
|
|
|
|
|
|
This has a little complex problem. |
2701
|
|
|
|
|
|
|
|
2702
|
|
|
|
|
|
|
In C and C you can specify |
2703
|
|
|
|
|
|
|
type name as same as type name defined |
2704
|
|
|
|
|
|
|
by create table, such as C or C. |
2705
|
|
|
|
|
|
|
|
2706
|
|
|
|
|
|
|
Note that type name and data type don't contain upper case. |
2707
|
|
|
|
|
|
|
If these contain upper case character, you convert it to lower case. |
2708
|
|
|
|
|
|
|
|
2709
|
|
|
|
|
|
|
C is executed after C. |
2710
|
|
|
|
|
|
|
|
2711
|
|
|
|
|
|
|
Type rule of C and C is enabled on the following |
2712
|
|
|
|
|
|
|
column name. |
2713
|
|
|
|
|
|
|
|
2714
|
|
|
|
|
|
|
=over 4 |
2715
|
|
|
|
|
|
|
|
2716
|
|
|
|
|
|
|
=item 1. column name |
2717
|
|
|
|
|
|
|
|
2718
|
|
|
|
|
|
|
issue_date |
2719
|
|
|
|
|
|
|
issue_datetime |
2720
|
|
|
|
|
|
|
|
2721
|
|
|
|
|
|
|
This need C option in each method.
2722
|
|
|
|
|
|
|
|
2723
|
|
|
|
|
|
|
=item 2. table name and column name, separator is dot |
2724
|
|
|
|
|
|
|
|
2725
|
|
|
|
|
|
|
book.issue_date |
2726
|
|
|
|
|
|
|
book.issue_datetime |
2727
|
|
|
|
|
|
|
|
2728
|
|
|
|
|
|
|
=back |
2729
|
|
|
|
|
|
|
|
2730
|
|
|
|
|
|
|
You get all type name used in database by C. |
2731
|
|
|
|
|
|
|
|
2732
|
|
|
|
|
|
|
print $dbi->available_typename; |
2733
|
|
|
|
|
|
|
|
2734
|
|
|
|
|
|
|
In C and C you specify data type, not type name. |
2735
|
|
|
|
|
|
|
C is executed after C. |
2736
|
|
|
|
|
|
|
You get all data type by C. |
2737
|
|
|
|
|
|
|
|
2738
|
|
|
|
|
|
|
print $dbi->available_datatype; |
2739
|
|
|
|
|
|
|
|
2740
|
|
|
|
|
|
|
You can also specify multiple types at once. |
2741
|
|
|
|
|
|
|
|
2742
|
|
|
|
|
|
|
$dbi->type_rule( |
2743
|
|
|
|
|
|
|
into1 => [ |
2744
|
|
|
|
|
|
|
[qw/DATE DATETIME/] => sub { ... }, |
2745
|
|
|
|
|
|
|
], |
2746
|
|
|
|
|
|
|
); |
2747
|
|
|
|
|
|
|
|
2748
|
|
|
|
|
|
|
=head2 update |
2749
|
|
|
|
|
|
|
|
2750
|
|
|
|
|
|
|
$dbi->update({title => 'Perl'}, table => 'book', where => {id => 4}); |
2751
|
|
|
|
|
|
|
|
2752
|
|
|
|
|
|
|
Execute update statement. First argument is update row data. |
2753
|
|
|
|
|
|
|
|
2754
|
|
|
|
|
|
|
If you want to set constant value to row data, use scalar reference |
2755
|
|
|
|
|
|
|
as parameter value. |
2756
|
|
|
|
|
|
|
|
2757
|
|
|
|
|
|
|
{date => \"NOW()"} |
2758
|
|
|
|
|
|
|
|
2759
|
|
|
|
|
|
|
B |
2760
|
|
|
|
|
|
|
|
2761
|
|
|
|
|
|
|
C method use all of C method's options, |
2762
|
|
|
|
|
|
|
and use the following new ones. |
2763
|
|
|
|
|
|
|
|
2764
|
|
|
|
|
|
|
=over 4 |
2765
|
|
|
|
|
|
|
|
2766
|
|
|
|
|
|
|
=item prefix |
2767
|
|
|
|
|
|
|
|
2768
|
|
|
|
|
|
|
prefix => 'or replace' |
2769
|
|
|
|
|
|
|
|
2770
|
|
|
|
|
|
|
prefix before table name section |
2771
|
|
|
|
|
|
|
|
2772
|
|
|
|
|
|
|
update or replace book |
2773
|
|
|
|
|
|
|
|
2774
|
|
|
|
|
|
|
=item table |
2775
|
|
|
|
|
|
|
|
2776
|
|
|
|
|
|
|
table => 'book' |
2777
|
|
|
|
|
|
|
|
2778
|
|
|
|
|
|
|
Table name. |
2779
|
|
|
|
|
|
|
|
2780
|
|
|
|
|
|
|
=item where |
2781
|
|
|
|
|
|
|
|
2782
|
|
|
|
|
|
|
Same as C |
2783
|
|
|
|
|
|
|
|
2784
|
|
|
|
|
|
|
=item wrap |
2785
|
|
|
|
|
|
|
|
2786
|
|
|
|
|
|
|
wrap => {price => sub { "max($_[0])" }} |
2787
|
|
|
|
|
|
|
|
2788
|
|
|
|
|
|
|
placeholder wrapped string. |
2789
|
|
|
|
|
|
|
|
2790
|
|
|
|
|
|
|
If the following statement |
2791
|
|
|
|
|
|
|
|
2792
|
|
|
|
|
|
|
$dbi->update({price => 100}, table => 'book', |
2793
|
|
|
|
|
|
|
{price => sub { "$_[0] + 5" }}); |
2794
|
|
|
|
|
|
|
|
2795
|
|
|
|
|
|
|
is executed, the following SQL is executed. |
2796
|
|
|
|
|
|
|
|
2797
|
|
|
|
|
|
|
update book set price = ? + 5; |
2798
|
|
|
|
|
|
|
|
2799
|
|
|
|
|
|
|
=item mtime |
2800
|
|
|
|
|
|
|
|
2801
|
|
|
|
|
|
|
mtime => 'modified_time' |
2802
|
|
|
|
|
|
|
|
2803
|
|
|
|
|
|
|
Modified time column name. time row is updated is set to the column. |
2804
|
|
|
|
|
|
|
default time format is C, which can be changed by |
2805
|
|
|
|
|
|
|
C attribute. |
2806
|
|
|
|
|
|
|
|
2807
|
|
|
|
|
|
|
=back |
2808
|
|
|
|
|
|
|
|
2809
|
|
|
|
|
|
|
=head2 update_all |
2810
|
|
|
|
|
|
|
|
2811
|
|
|
|
|
|
|
$dbi->update_all({title => 'Perl'}, table => 'book', ); |
2812
|
|
|
|
|
|
|
|
2813
|
|
|
|
|
|
|
Execute update statement for all rows. |
2814
|
|
|
|
|
|
|
Options is same as C method. |
2815
|
|
|
|
|
|
|
|
2816
|
|
|
|
|
|
|
=over 4 |
2817
|
|
|
|
|
|
|
|
2818
|
|
|
|
|
|
|
=item option |
2819
|
|
|
|
|
|
|
|
2820
|
|
|
|
|
|
|
option => { |
2821
|
|
|
|
|
|
|
select => { |
2822
|
|
|
|
|
|
|
append => '...' |
2823
|
|
|
|
|
|
|
}, |
2824
|
|
|
|
|
|
|
insert => { |
2825
|
|
|
|
|
|
|
prefix => '...' |
2826
|
|
|
|
|
|
|
}, |
2827
|
|
|
|
|
|
|
update => { |
2828
|
|
|
|
|
|
|
filter => {} |
2829
|
|
|
|
|
|
|
} |
2830
|
|
|
|
|
|
|
} |
2831
|
|
|
|
|
|
|
|
2832
|
|
|
|
|
|
|
If you want to pass option to each method, |
2833
|
|
|
|
|
|
|
you can use C |
2834
|
|
|
|
|
|
|
|
2835
|
|
|
|
|
|
|
=over 4 |
2836
|
|
|
|
|
|
|
|
2837
|
|
|
|
|
|
|
=item select_option |
2838
|
|
|
|
|
|
|
|
2839
|
|
|
|
|
|
|
select_option => {append => 'for update'} |
2840
|
|
|
|
|
|
|
|
2841
|
|
|
|
|
|
|
select method option, |
2842
|
|
|
|
|
|
|
select method is used to check the row is already exists. |
2843
|
|
|
|
|
|
|
|
2844
|
|
|
|
|
|
|
=head2 show_datatype |
2845
|
|
|
|
|
|
|
|
2846
|
|
|
|
|
|
|
$dbi->show_datatype($table); |
2847
|
|
|
|
|
|
|
|
2848
|
|
|
|
|
|
|
Show data type of the columns of specified table. |
2849
|
|
|
|
|
|
|
|
2850
|
|
|
|
|
|
|
book |
2851
|
|
|
|
|
|
|
title: 5 |
2852
|
|
|
|
|
|
|
issue_date: 91 |
2853
|
|
|
|
|
|
|
|
2854
|
|
|
|
|
|
|
This data type is used in C's C and C. |
2855
|
|
|
|
|
|
|
|
2856
|
|
|
|
|
|
|
=head2 show_tables |
2857
|
|
|
|
|
|
|
|
2858
|
|
|
|
|
|
|
$dbi->show_tables; |
2859
|
|
|
|
|
|
|
|
2860
|
|
|
|
|
|
|
Show tables. |
2861
|
|
|
|
|
|
|
|
2862
|
|
|
|
|
|
|
=head2 show_typename |
2863
|
|
|
|
|
|
|
|
2864
|
|
|
|
|
|
|
$dbi->show_typename($table); |
2865
|
|
|
|
|
|
|
|
2866
|
|
|
|
|
|
|
Show type name of the columns of specified table. |
2867
|
|
|
|
|
|
|
|
2868
|
|
|
|
|
|
|
book |
2869
|
|
|
|
|
|
|
title: varchar |
2870
|
|
|
|
|
|
|
issue_date: date |
2871
|
|
|
|
|
|
|
|
2872
|
|
|
|
|
|
|
This type name is used in C's C and C. |
2873
|
|
|
|
|
|
|
|
2874
|
|
|
|
|
|
|
=head2 values_clause |
2875
|
|
|
|
|
|
|
|
2876
|
|
|
|
|
|
|
my $values_clause = $dbi->values_clause({title => 'a', age => 2}); |
2877
|
|
|
|
|
|
|
|
2878
|
|
|
|
|
|
|
Create values clause. |
2879
|
|
|
|
|
|
|
|
2880
|
|
|
|
|
|
|
(title, author) values (title = :title, age = :age); |
2881
|
|
|
|
|
|
|
|
2882
|
|
|
|
|
|
|
You can use this in insert statement. |
2883
|
|
|
|
|
|
|
|
2884
|
|
|
|
|
|
|
my $insert_sql = "insert into book $values_clause"; |
2885
|
|
|
|
|
|
|
|
2886
|
|
|
|
|
|
|
=head2 where |
2887
|
|
|
|
|
|
|
|
2888
|
|
|
|
|
|
|
my $where = $dbi->where; |
2889
|
|
|
|
|
|
|
$where->clause(['and', 'title = :title', 'author = :author']); |
2890
|
|
|
|
|
|
|
$where->param({title => 'Perl', author => 'Ken'}); |
2891
|
|
|
|
|
|
|
$where->join(['left join author on book.author = author.id]); |
2892
|
|
|
|
|
|
|
|
2893
|
|
|
|
|
|
|
Create a new L object. |
2894
|
|
|
|
|
|
|
See L to know how to create where clause. |
2895
|
|
|
|
|
|
|
|
2896
|
|
|
|
|
|
|
=head1 ENVIRONMENTAL VARIABLES |
2897
|
|
|
|
|
|
|
|
2898
|
|
|
|
|
|
|
=head2 DBIX_CUSTOM_DEBUG |
2899
|
|
|
|
|
|
|
|
2900
|
|
|
|
|
|
|
If environment variable C is set to true, |
2901
|
|
|
|
|
|
|
executed SQL and bind values are printed to STDERR. |
2902
|
|
|
|
|
|
|
|
2903
|
|
|
|
|
|
|
=head2 DBIX_CUSTOM_DEBUG_ENCODING |
2904
|
|
|
|
|
|
|
|
2905
|
|
|
|
|
|
|
DEBUG output encoding. Default to UTF-8. |
2906
|
|
|
|
|
|
|
|
2907
|
|
|
|
|
|
|
=head2 DBIX_CUSTOM_SUPPRESS_DEPRECATION |
2908
|
|
|
|
|
|
|
|
2909
|
|
|
|
|
|
|
$ENV{DBIX_CUSTOM_SUPPRESS_DEPRECATION} = '0.25'; |
2910
|
|
|
|
|
|
|
|
2911
|
|
|
|
|
|
|
Suppress deprecation warnings before specified version. |
2912
|
|
|
|
|
|
|
|
2913
|
|
|
|
|
|
|
=head1 DEPRECATED FUNCTIONALITY |
2914
|
|
|
|
|
|
|
|
2915
|
|
|
|
|
|
|
L |
2916
|
|
|
|
|
|
|
|
2917
|
|
|
|
|
|
|
# Methods |
2918
|
|
|
|
|
|
|
DBIx::Custom AUTOLOAD feature # will be removed at 2022/5/1 |
2919
|
|
|
|
|
|
|
DBIx::Custom::helper method # will be removed at 2022/5/1 |
2920
|
|
|
|
|
|
|
DBIx::Custom::update_or_insert method is DEPRECATED! # will be removed at 2022/5/1 |
2921
|
|
|
|
|
|
|
DBIx::Custom::count method # will be removed at 2022/5/1 |
2922
|
|
|
|
|
|
|
DBIx::Custom::select,update,delete method's primary_key option is DEPRECATED! # will be removed at 2022/5/1 |
2923
|
|
|
|
|
|
|
DBIx::Custom::select,update,delete method's id option is DEPRECATED! # will be removed at 2022/5/1 |
2924
|
|
|
|
|
|
|
DBIx::Custom::setup method is DEPRECATED! # will be removed at 2022/5/1 |
2925
|
|
|
|
|
|
|
|
2926
|
|
|
|
|
|
|
L |
2927
|
|
|
|
|
|
|
|
2928
|
|
|
|
|
|
|
# Options |
2929
|
|
|
|
|
|
|
kv method's multi option (from 0.28) # will be removed at 2018/3/1 |
2930
|
|
|
|
|
|
|
|
2931
|
|
|
|
|
|
|
L |
2932
|
|
|
|
|
|
|
|
2933
|
|
|
|
|
|
|
DBIx::Custom::Model AUTOLOAD feature # will be removed at 2022/5/1 |
2934
|
|
|
|
|
|
|
DBIx::Custom::Model::helper method is DEPRECATED! # will be removed at 2022/5/1 |
2935
|
|
|
|
|
|
|
DBIx::Custom::Model::update_or_insert method is DEPRECATED! # will be removed at 2022/5/1 |
2936
|
|
|
|
|
|
|
DBIx::Custom::Model::count method # will be removed at 2022/5/1 |
2937
|
|
|
|
|
|
|
DBIx::Custom::Model::primary_key attribute is DEPRECATED! # will be removed at 2022/5/1 |
2938
|
|
|
|
|
|
|
|
2939
|
|
|
|
|
|
|
=head1 BACKWARDS COMPATIBILITY POLICY |
2940
|
|
|
|
|
|
|
|
2941
|
|
|
|
|
|
|
If a feature is DEPRECATED, you can know it by DEPRECATED warnings. |
2942
|
|
|
|
|
|
|
DEPRECATED feature is removed after C, |
2943
|
|
|
|
|
|
|
but if at least one person use the feature and tell me that thing |
2944
|
|
|
|
|
|
|
I extend one year each time he tell me it. |
2945
|
|
|
|
|
|
|
|
2946
|
|
|
|
|
|
|
DEPRECATION warnings can be suppressed by C |
2947
|
|
|
|
|
|
|
environment variable. |
2948
|
|
|
|
|
|
|
|
2949
|
|
|
|
|
|
|
EXPERIMENTAL features will be changed or deleted without warnings. |
2950
|
|
|
|
|
|
|
|
2951
|
|
|
|
|
|
|
=head1 BUGS |
2952
|
|
|
|
|
|
|
|
2953
|
|
|
|
|
|
|
Please tell me bugs if you find bug. |
2954
|
|
|
|
|
|
|
|
2955
|
|
|
|
|
|
|
C<< >> |
2956
|
|
|
|
|
|
|
|
2957
|
|
|
|
|
|
|
L |
2958
|
|
|
|
|
|
|
|
2959
|
|
|
|
|
|
|
=head1 AUTHOR |
2960
|
|
|
|
|
|
|
|
2961
|
|
|
|
|
|
|
Yuki Kimoto, C<< >> |
2962
|
|
|
|
|
|
|
|
2963
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
2964
|
|
|
|
|
|
|
|
2965
|
|
|
|
|
|
|
Copyright 2009-2019 Yuki Kimoto, all rights reserved. |
2966
|
|
|
|
|
|
|
|
2967
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
2968
|
|
|
|
|
|
|
under the same terms as Perl itself. |
2969
|
|
|
|
|
|
|
|
2970
|
|
|
|
|
|
|
=cut |
| | | | |