line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CellBIS::SQL::Abstract; |
2
|
17
|
|
|
17
|
|
944834
|
use Mojo::Base -base; |
|
17
|
|
|
|
|
2058564
|
|
|
17
|
|
|
|
|
130
|
|
3
|
|
|
|
|
|
|
|
4
|
17
|
|
|
17
|
|
3575
|
use Scalar::Util qw(blessed); |
|
17
|
|
|
|
|
42
|
|
|
17
|
|
|
|
|
1032
|
|
5
|
17
|
|
|
17
|
|
98
|
use Carp (); |
|
17
|
|
|
|
|
30
|
|
|
17
|
|
|
|
|
331
|
|
6
|
17
|
|
|
17
|
|
74
|
use Mojo::Util qw(trim dumper); |
|
17
|
|
|
|
|
25
|
|
|
17
|
|
|
|
|
1153
|
|
7
|
17
|
|
|
17
|
|
8349
|
use CellBIS::SQL::Abstract::Util; |
|
17
|
|
|
|
|
49
|
|
|
17
|
|
|
|
|
164
|
|
8
|
17
|
|
|
17
|
|
7593
|
use CellBIS::SQL::Abstract::Table; |
|
17
|
|
|
|
|
54
|
|
|
17
|
|
|
|
|
243
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ABSTRACT: SQL Query Generator |
11
|
|
|
|
|
|
|
our $VERSION = '1.5'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has 'QueryUtil' => sub { state $qu = CellBIS::SQL::Abstract::Util->new }; |
14
|
|
|
|
|
|
|
has 'db_type' => 'mariadb'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub insert { |
17
|
3
|
|
|
3
|
1
|
1329
|
my $self = shift; |
18
|
3
|
|
|
|
|
5
|
my $arg_len = scalar @_; |
19
|
3
|
|
|
|
|
5
|
my $data = ''; |
20
|
3
|
|
|
|
|
5
|
my ($table_name, $column, $col_val, $type); |
21
|
3
|
100
|
|
|
|
8
|
if ($arg_len == 3) { |
22
|
2
|
|
|
|
|
5
|
($table_name, $column, $col_val) = @_; |
23
|
|
|
|
|
|
|
} |
24
|
3
|
100
|
|
|
|
8
|
if ($arg_len >= 4) { |
25
|
1
|
|
|
|
|
3
|
($table_name, $column, $col_val, $type) = @_; |
26
|
|
|
|
|
|
|
} |
27
|
3
|
|
|
|
|
4
|
my @table_field = @{$column}; |
|
3
|
|
|
|
|
7
|
|
28
|
3
|
|
|
|
|
3
|
my @table_data = @{$col_val}; |
|
3
|
|
|
|
|
6
|
|
29
|
3
|
|
|
|
|
5
|
my @get_data_value = (); |
30
|
3
|
|
|
|
|
8
|
my $field_col = join ', ', @table_field; |
31
|
3
|
|
|
|
|
5
|
my $value_col = ''; |
32
|
|
|
|
|
|
|
|
33
|
3
|
50
|
|
|
|
8
|
if ((scalar @table_field) == (scalar @table_data)) { |
34
|
|
|
|
|
|
|
|
35
|
3
|
50
|
66
|
|
|
16
|
if ($type && $type eq 'no-pre-st') { |
|
|
100
|
66
|
|
|
|
|
36
|
0
|
|
|
|
|
0
|
$value_col = join ', ', $self->QueryUtil->data_insert(\@table_data); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
elsif ($type && $type eq 'pre-st') { |
39
|
|
|
|
|
|
|
@get_data_value |
40
|
1
|
|
|
|
|
2
|
= @{$self->QueryUtil->data_insert_pre_st(\@table_data)->[0]}; |
|
1
|
|
|
|
|
5
|
|
41
|
1
|
|
|
|
|
5
|
$value_col = join ', ', @get_data_value; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
else { |
44
|
2
|
|
|
|
|
7
|
$value_col = join ', ', $self->QueryUtil->data_insert(\@table_data); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
3
|
|
|
|
|
13
|
$field_col = trim($field_col); |
48
|
3
|
|
|
|
|
30
|
$value_col = trim($value_col); |
49
|
3
|
|
|
|
|
20
|
$value_col =~ s/\,$//g; |
50
|
3
|
|
|
|
|
6
|
$value_col =~ s/\s\,//g; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
9
|
$data = "INSERT INTO $table_name($field_col) VALUES($value_col)"; |
53
|
|
|
|
|
|
|
} |
54
|
3
|
|
|
|
|
10
|
return $data; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub insert_bulk { |
58
|
2
|
|
|
2
|
1
|
1158
|
my ($self, $table_name, $column, $col_val, $type) = @_; |
59
|
|
|
|
|
|
|
|
60
|
2
|
|
100
|
|
|
12
|
$type //= ''; |
61
|
2
|
|
|
|
|
2
|
my @table_field = @{$column}; |
|
2
|
|
|
|
|
5
|
|
62
|
2
|
|
|
|
|
3
|
my @table_data = (); |
63
|
2
|
|
|
|
|
3
|
my @value_data = (); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# for field column |
66
|
2
|
|
|
|
|
6
|
my $field_col = join ', ', @table_field; |
67
|
2
|
|
|
|
|
3
|
my $value_col = ''; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# for data column |
70
|
2
|
|
|
|
|
3
|
for my $value (@{$col_val}) { |
|
2
|
|
|
|
|
5
|
|
71
|
8
|
100
|
|
|
|
17
|
if ($type eq 'pre-st') { |
72
|
4
|
|
|
|
|
5
|
push @value_data, @{$self->QueryUtil->data_insert_pre_st($value)->[1]}; |
|
4
|
|
|
|
|
9
|
|
73
|
|
|
|
|
|
|
push @table_data, |
74
|
|
|
|
|
|
|
'(' |
75
|
4
|
|
|
|
|
7
|
. (join ', ', @{$self->QueryUtil->data_insert_pre_st($value)->[0]}) |
|
4
|
|
|
|
|
7
|
|
76
|
|
|
|
|
|
|
. ')'; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
else { |
79
|
4
|
|
|
|
|
11
|
push @table_data, |
80
|
|
|
|
|
|
|
'(' . (join ', ', $self->QueryUtil->data_insert($value)) . ')'; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
2
|
|
|
|
|
9
|
$field_col = trim($field_col); |
85
|
2
|
|
|
|
|
24
|
$value_col = trim(join(', ', @table_data)); |
86
|
2
|
|
|
|
|
15
|
$value_col =~ s/\,$//g; |
87
|
2
|
|
|
|
|
6
|
$value_col =~ s/\s\,//g; |
88
|
|
|
|
|
|
|
|
89
|
2
|
|
|
|
|
11
|
return ["INSERT INTO ${table_name}($field_col) VALUES $value_col", |
90
|
|
|
|
|
|
|
@value_data]; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub update { |
94
|
8
|
|
|
8
|
1
|
122
|
my $self = shift; |
95
|
8
|
|
|
|
|
11
|
my $arg_len = scalar @_; |
96
|
|
|
|
|
|
|
|
97
|
8
|
50
|
33
|
|
|
21
|
if ($arg_len > 2 || $arg_len >= 5) { |
98
|
8
|
|
|
|
|
16
|
my $method_name = '_qUpdate_arg' . $arg_len; |
99
|
8
|
50
|
|
|
|
30
|
if ($self->can($method_name)) { |
100
|
8
|
|
|
|
|
24
|
return $self->$method_name(@_); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
0
|
|
|
|
|
0
|
return ''; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub delete { |
107
|
2
|
|
|
2
|
1
|
99
|
my $self = shift; |
108
|
2
|
|
|
|
|
5
|
my ($table_name, $clause) = @_; |
109
|
2
|
|
|
|
|
3
|
my $data = ''; |
110
|
|
|
|
|
|
|
|
111
|
2
|
50
|
|
|
|
8
|
if (ref($clause) eq "HASH") { |
112
|
|
|
|
|
|
|
|
113
|
2
|
50
|
|
|
|
4
|
if (exists $clause->{where}) { |
114
|
2
|
|
|
|
|
7
|
my $where_clause = $self->QueryUtil->create_clause($clause); |
115
|
2
|
|
|
|
|
6
|
$data = "DELETE FROM $table_name \n$where_clause"; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
2
|
|
|
|
|
5
|
return $data; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub select { |
122
|
10
|
|
|
10
|
1
|
151
|
my $self = shift; |
123
|
10
|
|
|
|
|
19
|
my $arg_len = scalar @_; |
124
|
10
|
|
|
|
|
15
|
my $data; |
125
|
|
|
|
|
|
|
|
126
|
10
|
50
|
|
|
|
44
|
$data = $self->_qSelect_arg3(@_) unless ($arg_len < 2); |
127
|
10
|
|
|
|
|
27
|
return $data; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub select_join { |
131
|
3
|
|
|
3
|
1
|
130
|
my $self = shift; |
132
|
3
|
|
|
|
|
5
|
my $arg_len = scalar @_; |
133
|
3
|
|
|
|
|
6
|
my $data = ''; |
134
|
|
|
|
|
|
|
|
135
|
3
|
50
|
|
|
|
16
|
$data = $self->_qSelectJoin_arg3(@_) unless ($arg_len < 3); |
136
|
3
|
|
|
|
|
8
|
return $data; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub create_table { |
140
|
10
|
|
|
10
|
1
|
829
|
my $self = shift; |
141
|
10
|
|
|
|
|
24
|
my $arg_len = scalar @_; |
142
|
10
|
|
|
|
|
25
|
my $result = ''; |
143
|
|
|
|
|
|
|
|
144
|
10
|
50
|
|
|
|
35
|
if ($arg_len >= 3) { |
145
|
10
|
|
|
|
|
44
|
my $tables |
146
|
|
|
|
|
|
|
= CellBIS::SQL::Abstract::Table->new(db_type => $self->db_type); |
147
|
10
|
|
|
|
|
226
|
$result = $tables->create_query_table(@_); |
148
|
|
|
|
|
|
|
} |
149
|
10
|
|
|
|
|
37
|
return $result; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# For Action Query String - "update" |
153
|
|
|
|
|
|
|
# -------------------------------------------------------------- |
154
|
|
|
|
|
|
|
sub _qUpdate_arg3 { |
155
|
2
|
|
|
2
|
|
4
|
my $self = shift; |
156
|
2
|
|
|
|
|
4
|
my ($table_name, $col_val, $clause) = @_; |
157
|
|
|
|
|
|
|
|
158
|
2
|
|
|
|
|
3
|
my $data = ''; |
159
|
|
|
|
|
|
|
|
160
|
2
|
50
|
|
|
|
6
|
Carp::croak '$col_val is must be hashref datatype' |
161
|
|
|
|
|
|
|
unless ref $col_val eq "HASH"; |
162
|
|
|
|
|
|
|
|
163
|
2
|
50
|
|
|
|
6
|
if (exists $clause->{where}) { |
164
|
|
|
|
|
|
|
my @field = map { |
165
|
|
|
|
|
|
|
$self->QueryUtil->is_sql_function($col_val->{$_}) |
166
|
|
|
|
|
|
|
? $_ . ' = ' . $col_val->{$_}->[0] |
167
|
|
|
|
|
|
|
: $_ . ' = ' . "'" |
168
|
7
|
100
|
|
|
|
14
|
. $col_val->{$_} . "'" |
169
|
2
|
|
|
|
|
3
|
} keys %{$col_val}; |
|
2
|
|
|
|
|
7
|
|
170
|
2
|
|
|
|
|
7
|
my $field_change = join ', ', @field; |
171
|
2
|
|
|
|
|
4
|
my $where_clause = $self->QueryUtil->create_clause($clause); |
172
|
2
|
|
|
|
|
8
|
$data = "UPDATE $table_name \nSET $field_change \n$where_clause"; |
173
|
|
|
|
|
|
|
} |
174
|
2
|
|
|
|
|
6
|
return $data; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub _qUpdate_arg4 { |
178
|
3
|
|
|
3
|
|
5
|
my $self = shift; |
179
|
3
|
|
|
|
|
6
|
my ($table_name, $column, $value, $clause) = @_; |
180
|
3
|
|
|
|
|
5
|
my $data = ''; |
181
|
|
|
|
|
|
|
|
182
|
3
|
50
|
|
|
|
7
|
if (exists $clause->{where}) { |
183
|
3
|
|
|
|
|
9
|
my @get_value = $self->QueryUtil->col_with_val($column, $value); |
184
|
3
|
|
|
|
|
8
|
my $field_change = join ', ', @get_value; |
185
|
3
|
|
|
|
|
6
|
my $where_clause = $self->QueryUtil->create_clause($clause); |
186
|
3
|
|
|
|
|
11
|
$data = "UPDATE $table_name \nSET $field_change \n$where_clause"; |
187
|
|
|
|
|
|
|
} |
188
|
3
|
|
|
|
|
9
|
return $data; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub _qUpdate_arg5 { |
192
|
3
|
|
|
3
|
|
6
|
my $self = shift; |
193
|
3
|
|
|
|
|
6
|
my ($table_name, $column, $value, $clause, $type) = @_; |
194
|
3
|
|
|
|
|
5
|
my $data = ''; |
195
|
|
|
|
|
|
|
|
196
|
3
|
|
|
|
|
4
|
my @table_field = @{$column}; |
|
3
|
|
|
|
|
6
|
|
197
|
3
|
|
|
|
|
4
|
my $field_change = ''; |
198
|
3
|
|
|
|
|
5
|
my $where_clause = ''; |
199
|
|
|
|
|
|
|
|
200
|
3
|
50
|
33
|
|
|
13
|
if ($type && $type eq 'no-pre-st') { |
201
|
|
|
|
|
|
|
|
202
|
0
|
0
|
|
|
|
0
|
if (exists $clause->{where}) { |
203
|
0
|
|
|
|
|
0
|
my @get_value = $self->QueryUtil->col_with_val($column, $value); |
204
|
0
|
|
|
|
|
0
|
$field_change = join ', ', @get_value; |
205
|
0
|
|
|
|
|
0
|
$where_clause = $self->QueryUtil->create_clause($clause); |
206
|
0
|
|
|
|
|
0
|
$data = "UPDATE $table_name \nSET $field_change \n$where_clause"; |
207
|
|
|
|
|
|
|
} |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
else { |
211
|
|
|
|
|
|
|
|
212
|
3
|
50
|
|
|
|
7
|
if (exists $clause->{where}) { |
213
|
3
|
|
|
|
|
8
|
$field_change = join '=?, ', @table_field; |
214
|
3
|
|
|
|
|
6
|
$field_change .= '=?'; |
215
|
3
|
|
|
|
|
9
|
$where_clause = $self->QueryUtil->create_clause($clause); |
216
|
3
|
|
|
|
|
8
|
$data = "UPDATE $table_name \nSET $field_change \n$where_clause"; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
} |
219
|
3
|
|
|
|
|
10
|
return $data; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# For Action Query String - "select" : |
223
|
|
|
|
|
|
|
# -------------------------------------------------------------- |
224
|
|
|
|
|
|
|
sub _qSelect_arg3 { |
225
|
10
|
|
|
10
|
|
18
|
my $self = shift; |
226
|
10
|
|
|
|
|
26
|
my ($table_name, $column, $clause) = @_; |
227
|
10
|
|
|
|
|
20
|
my $data = ''; |
228
|
10
|
|
|
|
|
13
|
my @col = @{$column}; |
|
10
|
|
|
|
|
20
|
|
229
|
10
|
|
|
|
|
17
|
my $size_col = scalar @col; |
230
|
|
|
|
|
|
|
|
231
|
10
|
100
|
|
|
|
32
|
if (ref($clause) eq "HASH") { |
232
|
9
|
|
|
|
|
32
|
my $field_change; |
233
|
|
|
|
|
|
|
my $where_clause; |
234
|
|
|
|
|
|
|
|
235
|
9
|
|
|
|
|
17
|
my $size_clause = scalar keys %{$clause}; |
|
9
|
|
|
|
|
20
|
|
236
|
|
|
|
|
|
|
|
237
|
9
|
50
|
|
|
|
24
|
if ($size_clause != 0) { |
238
|
9
|
|
|
|
|
32
|
$where_clause = $self->QueryUtil->create_clause($clause); |
239
|
9
|
100
|
|
|
|
25
|
if (scalar @col == 0) { |
240
|
4
|
|
|
|
|
10
|
$data = 'SELECT * FROM ' . $table_name . "\n" . $where_clause; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
else { |
243
|
5
|
50
|
|
|
|
26
|
$field_change = ref($column) eq "ARRAY" ? (join ', ', @col) : '*'; |
244
|
5
|
|
|
|
|
23
|
$data |
245
|
|
|
|
|
|
|
= 'SELECT ' |
246
|
|
|
|
|
|
|
. $field_change |
247
|
|
|
|
|
|
|
. " \nFROM " |
248
|
|
|
|
|
|
|
. $table_name . "\n" |
249
|
|
|
|
|
|
|
. $where_clause; |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
else { |
254
|
0
|
0
|
|
|
|
0
|
if ($size_col == 0) { |
255
|
0
|
|
|
|
|
0
|
$data = "SELECT * FROM $table_name"; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
else { |
258
|
0
|
|
|
|
|
0
|
$field_change = join ', ', @col; |
259
|
0
|
|
|
|
|
0
|
$data = "SELECT $field_change FROM $table_name"; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
else { |
264
|
1
|
|
|
|
|
2
|
my $field_change = ''; |
265
|
|
|
|
|
|
|
|
266
|
1
|
50
|
|
|
|
3
|
if ($size_col == 0) { |
267
|
1
|
|
|
|
|
3
|
$data = "SELECT * FROM $table_name"; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
else { |
270
|
0
|
|
|
|
|
0
|
$field_change = join ', ', @col; |
271
|
0
|
|
|
|
|
0
|
$data = "SELECT $field_change FROM $table_name"; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
} |
274
|
10
|
|
|
|
|
38
|
return $data; |
275
|
|
|
|
|
|
|
} |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
# For Action Query String - "select_join" : |
278
|
|
|
|
|
|
|
# -------------------------------------------------------------- |
279
|
|
|
|
|
|
|
sub _qSelectJoin_arg3 { |
280
|
3
|
|
|
3
|
|
7
|
my $self = shift; |
281
|
3
|
|
|
|
|
7
|
my ($table_name, $column, $clause) = @_; |
282
|
3
|
|
|
|
|
6
|
my $data = ''; |
283
|
|
|
|
|
|
|
|
284
|
3
|
|
|
|
|
4
|
my $size_col = scalar @{$column}; |
|
3
|
|
|
|
|
5
|
|
285
|
3
|
|
|
|
|
6
|
my $field_change = ''; |
286
|
3
|
50
|
|
|
|
8
|
$field_change = '*' if $size_col == 0; |
287
|
3
|
50
|
|
|
|
9
|
$field_change = join ', ', @{$column} if $size_col >= 1; |
|
3
|
|
|
|
|
8
|
|
288
|
3
|
|
|
|
|
17
|
my $where_clause = ''; |
289
|
3
|
|
|
|
|
5
|
my $join_clause = ''; |
290
|
|
|
|
|
|
|
|
291
|
3
|
50
|
|
|
|
12
|
if (ref($clause) eq "HASH") { |
292
|
3
|
50
|
|
|
|
8
|
if (exists $clause->{join}) { |
293
|
3
|
|
|
|
|
13
|
$join_clause = $self->QueryUtil->for_onjoin($clause, $table_name); |
294
|
3
|
|
|
|
|
9
|
$where_clause = $self->QueryUtil->create_clause($clause); |
295
|
3
|
|
|
|
|
12
|
$data = "SELECT $field_change $join_clause" . "\n" . $where_clause; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
else { |
298
|
0
|
|
|
|
|
0
|
$where_clause = $self->QueryUtil->create_clause($clause); |
299
|
0
|
|
|
|
|
0
|
$data = "SELECT $field_change FROM $table_name"; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
else { |
303
|
0
|
|
|
|
|
0
|
$data = "SELECT $field_change FROM $table_name"; |
304
|
|
|
|
|
|
|
} |
305
|
3
|
|
|
|
|
7
|
return $data; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
sub to_one_liner { |
309
|
25
|
|
|
25
|
0
|
141
|
my ($self, $result) = @_; |
310
|
|
|
|
|
|
|
|
311
|
25
|
|
|
|
|
113
|
$result =~ s/\t+//g; |
312
|
25
|
|
|
|
|
138
|
$result =~ s/\,\s+/\, /g; |
313
|
25
|
|
|
|
|
268
|
$result =~ s/\s+/ /g; |
314
|
25
|
|
|
|
|
163
|
return $result; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
1; |