lib/CellBIS/SQL/Abstract.pm

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