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