| 1 | package # hide from PAUSE | ||||||||||||||||||||||||||||
| 2 | CellBIS::SQL::Abstract::Util; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use Mojo::Util 'dumper'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # For "column" with "value" : | ||||||||||||||||||||||||||||
| 8 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 9 | 3 | ▶ | sub col_with_val { | ||||||||||||||||||||||||||
|
Subroutine
col_with_val: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 11 | 3 | my ($column, $value) = @_; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | my @data_col = @{$column}; | |||||||||||||||||||||||||||
| 14 | 3 | my @data_val = @{$value}; | |||||||||||||||||||||||||||
| 15 | my @data | ||||||||||||||||||||||||||||
| 16 | 3 | = map { $data_col[$_] . '=\'' . $data_val[$_] . '\'' } 0 .. $#data_col; | |||||||||||||||||||||||||||
| 17 | 3 | return @data; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # For onjoin clause : | ||||||||||||||||||||||||||||
| 21 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 22 | 3 | ▶ | sub for_onjoin { | ||||||||||||||||||||||||||
|
Subroutine
for_onjoin: called CC=11 SCAR=25.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 24 | 3 | my ($options, $table_name) = @_; | |||||||||||||||||||||||||||
| 25 | 3 | my $data = "FROM " . $table_name->[0]->{name}; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | my %type = %{$options->{typejoin}}; | |||||||||||||||||||||||||||
| 28 | 3 | my $join = $options->{join}; | |||||||||||||||||||||||||||
| 29 | 3 | my $size_join = @{$join}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | my @table_list = @{$table_name}; | |||||||||||||||||||||||||||
| 32 | 3 | my %list_table = map { $_->{name} => $_ } @{$table_name}; | |||||||||||||||||||||||||||
| 33 | 3 | ▶ | my @get_primaryTbl = grep { $_->{primary} && $_->{primary} == 1 } @table_list; | ||||||||||||||||||||||||||
|
Condition66%2/3
$_->{'primary'} && $_->{'primary'} == 1
MC/DC50%1/2
$_->{'primary'} && $_->{'primary'} == 1
$_->{'primary'}
$_->{'primary'} == 1
Truth table66%2/3
A && B
A= $_->{'primary'}B= $_->{'primary'} == 1
| |||||||||||||||||||||||||||||
| 34 | 3 | ▶ | @get_primaryTbl = @get_primaryTbl ? @get_primaryTbl : ($table_list[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Check IF founded primary table : | ||||||||||||||||||||||||||||
| 37 | 3 | ▶ | if (@get_primaryTbl) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 3 | my $tbl_name = ''; | |||||||||||||||||||||||||||
| 39 | 3 | my $tbl_alias = ''; | |||||||||||||||||||||||||||
| 40 | 3 | my $get_table_data = ''; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # For "FROM TABLE" | ||||||||||||||||||||||||||||
| 43 | 3 | $data = "\nFROM $get_primaryTbl[0]->{name}"; | |||||||||||||||||||||||||||
| 44 | 3 | ▶ | if (exists $get_primaryTbl[0]->{alias}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 3 | $data | |||||||||||||||||||||||||||
| 46 | = "\nFROM $get_primaryTbl[0]->{name} AS $get_primaryTbl[0]->{alias}"; | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | my $i = 0; | |||||||||||||||||||||||||||
| 50 | 3 | my $table_join = ''; | |||||||||||||||||||||||||||
| 51 | 3 | my $type_join = ''; | |||||||||||||||||||||||||||
| 52 | 3 | ▶ | while ($i < $size_join) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 3 | my $get_table = $join->[$i]; | |||||||||||||||||||||||||||
| 54 | 3 | $tbl_name = $get_table->{name}; | |||||||||||||||||||||||||||
| 55 | 3 | $table_join = $get_table->{onjoin}; | |||||||||||||||||||||||||||
| 56 | 3 | $get_table_data = $list_table{$tbl_name}; | |||||||||||||||||||||||||||
| 57 | 3 | $type_join = $self->type_join($type{$tbl_name}); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | if (exists $get_table_data->{alias}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 3 | $tbl_alias = $get_table_data->{alias}; | |||||||||||||||||||||||||||
| 61 | 3 | $data .= " $type_join $tbl_name AS $tbl_alias "; | |||||||||||||||||||||||||||
| 62 | 3 | ▶ | $data .= "\nON " if ($i > 1 or $i <= ($size_join - 1)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$i > 1 or $i <= $size_join - 1
MC/DC0%0/2
$i > 1 or $i <= $size_join - 1
$i > 1
$i <= $size_join - 1
Truth table33%1/3
A or B
A= $i > 1B= $i <= $size_join - 1
| |||||||||||||||||||||||||||||
| 63 | 3 | $data .= join " = ", @$table_join; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||
| 66 | 0 | $data .= " $type_join $tbl_name "; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | $data .= 'ON ' if ($i > 1 or $i <= ($size_join - 1)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$i > 1 or $i <= $size_join - 1
MC/DC0%0/2
$i > 1 or $i <= $size_join - 1
$i > 1
$i <= $size_join - 1
Truth table0%0/3
A or B
A= $i > 1B= $i <= $size_join - 1
| |||||||||||||||||||||||||||||
| 68 | 0 | $data .= join " = ", @$table_join; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | $i++; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 3 | return $data; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # For create clause query : | ||||||||||||||||||||||||||||
| 78 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 79 | 22 | ▶ | sub create_clause { | ||||||||||||||||||||||||||
|
Subroutine
create_clause: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 22 | my ($self, $clause) = @_; | |||||||||||||||||||||||||||
| 81 | 22 | my $data = ''; | |||||||||||||||||||||||||||
| 82 | 22 | ▶ | if (exists $clause->{'where'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 17 | $data .= 'WHERE ' . $clause->{'where'}; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 22 | ▶ | if (exists $clause->{'orderby'} and not exists $clause->{'groupby'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $clause->{"orderby"} and not exists $clause->{"groupby"}
MC/DC100%2/2
exists $clause->{"orderby"} and not exists $clause->{"groupby"}
exists $clause->{"orderby"}
not exists $clause->{"groupby"}
Truth table100%3/3
A and not(B)
A= exists $clause->{"orderby"}B= not exists $clause->{"groupby"}
| |||||||||||||||||||||||||||||
| 86 | 11 | $data .= ' ORDER BY ' . $clause->{'orderby'}; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 22 | ▶ | if (exists $clause->{'orderby'} and exists $clause->{'groupby'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $clause->{"orderby"} and exists $clause->{"groupby"}
MC/DC100%2/2
exists $clause->{"orderby"} and exists $clause->{"groupby"}
exists $clause->{"orderby"}
exists $clause->{"groupby"}
Truth table100%3/3
A and B
A= exists $clause->{"orderby"}B= exists $clause->{"groupby"}
| |||||||||||||||||||||||||||||
| 89 | $data | ||||||||||||||||||||||||||||
| 90 | .= ' GROUP BY ' | ||||||||||||||||||||||||||||
| 91 | . $clause->{'groupby'} | ||||||||||||||||||||||||||||
| 92 | . ' ORDER BY ' | ||||||||||||||||||||||||||||
| 93 | 1 | . $clause->{'orderby'}; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 22 | ▶ | if (exists $clause->{'order'} and exists $clause->{orderby}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $clause->{"order"} and exists $clause->{"orderby"}
MC/DC50%1/2
exists $clause->{"order"} and exists $clause->{"orderby"}
exists $clause->{"order"}
exists $clause->{"orderby"}
Truth table66%2/3
A and B
A= exists $clause->{"order"}B= exists $clause->{"orderby"}
| |||||||||||||||||||||||||||||
| 96 | 9 | $data .= ' ' . (uc $clause->{'order'}); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 22 | ▶ | if (exists $clause->{'limit'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 11 | $data .= ' LIMIT ' . $clause->{'limit'}; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 22 | return $data; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # for Type Join : | ||||||||||||||||||||||||||||
| 105 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 106 | 3 | ▶ | sub type_join { | ||||||||||||||||||||||||||
|
Subroutine
type_join: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 3 | my ($self, $type) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | my %data_type = ('left' => "\nLEFT JOIN", 'inner' => "\nINNER JOIN",); | |||||||||||||||||||||||||||
| 110 | 3 | ▶ | return $data_type{$type} if exists $data_type{$type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # For replace data values "insert" : | ||||||||||||||||||||||||||||
| 114 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 115 | 9 | ▶ | sub data_insert_pre_st { | ||||||||||||||||||||||||||
|
Subroutine
data_insert_pre_st: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 117 | 9 | my ($data_value) = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 9 | my @data = @{$data_value}; | |||||||||||||||||||||||||||
| 120 | my @value | ||||||||||||||||||||||||||||
| 121 | 9 | ▶ | = map { $self->is_sql_function($_) ? $_->[0] : $_ } @data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 9 | ▶ | my @result = map { $self->is_sql_function($_) ? $_->[0] : '?' } @data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 9 | @result = grep (defined, @result); | |||||||||||||||||||||||||||
| 124 | 9 | return [\@result, \@value]; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # For replace data values "insert" in no prepare statement : | ||||||||||||||||||||||||||||
| 128 | # ------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 129 | 6 | ▶ | sub data_insert { | ||||||||||||||||||||||||||
|
Subroutine
data_insert: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 6 | my ($data_value) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 6 | my @data = @{$data_value}; | |||||||||||||||||||||||||||
| 134 | my @result = map { | ||||||||||||||||||||||||||||
| 135 | 6 | ▶ | $self->is_sql_function($_) # for match sql function | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | ? $_->[0] # true condition | ||||||||||||||||||||||||||||
| 137 | : "'" . $_ . "'" # if not function. | ||||||||||||||||||||||||||||
| 138 | } @data; | ||||||||||||||||||||||||||||
| 139 | 6 | @result = grep (defined, @result); | |||||||||||||||||||||||||||
| 140 | 6 | return @result; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # For check if function sql | ||||||||||||||||||||||||||||
| 144 | 80 | ▶ | sub is_sql_function { | ||||||||||||||||||||||||||
|
Subroutine
is_sql_function: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 80 | my ($self, $string) = @_; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 80 | ▶ | return ref($string) eq 'ARRAY' ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 17 | 1; | |||||||||||||||||||||||||||