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