| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package CellBIS::SQL::Abstract; | 
| 2 | 10 |  |  | 10 |  | 515075 | use Mojo::Base -base; | 
|  | 10 |  |  |  |  | 1059827 |  | 
|  | 10 |  |  |  |  | 91 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 10 |  |  | 10 |  | 2321 | use Scalar::Util qw(blessed); | 
|  | 10 |  |  |  |  | 16 |  | 
|  | 10 |  |  |  |  | 556 |  | 
| 5 | 10 |  |  | 10 |  | 43 | use Carp (); | 
|  | 10 |  |  |  |  | 15 |  | 
|  | 10 |  |  |  |  | 138 |  | 
| 6 | 10 |  |  | 10 |  | 35 | use Mojo::Util qw(trim); | 
|  | 10 |  |  |  |  | 13 |  | 
|  | 10 |  |  |  |  | 616 |  | 
| 7 | 10 |  |  | 10 |  | 4765 | use CellBIS::SQL::Abstract::Util; | 
|  | 10 |  |  |  |  | 21 |  | 
|  | 10 |  |  |  |  | 76 |  | 
| 8 | 10 |  |  | 10 |  | 3595 | use CellBIS::SQL::Abstract::Table; | 
|  | 10 |  |  |  |  | 28 |  | 
|  | 10 |  |  |  |  | 84 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # ABSTRACT: SQL Query Generator | 
| 11 |  |  |  |  |  |  | our $VERSION = '1.1'; | 
| 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 | 2 |  |  | 2 | 1 | 863 | my $self    = shift; | 
| 20 | 2 |  |  |  |  | 3 | my $arg_len = scalar @_; | 
| 21 | 2 |  |  |  |  | 3 | my $data    = ''; | 
| 22 | 2 |  |  |  |  | 4 | my ($table_name, $column, $col_val, $type); | 
| 23 | 2 | 100 |  |  |  | 6 | if ($arg_len == 3) { | 
| 24 | 1 |  |  |  |  | 2 | ($table_name, $column, $col_val) = @_; | 
| 25 |  |  |  |  |  |  | } | 
| 26 | 2 | 100 |  |  |  | 6 | if ($arg_len >= 4) { | 
| 27 | 1 |  |  |  |  | 4 | ($table_name, $column, $col_val, $type) = @_; | 
| 28 |  |  |  |  |  |  | } | 
| 29 | 2 |  |  |  |  | 3 | my @table_field    = @{$column}; | 
|  | 2 |  |  |  |  | 4 |  | 
| 30 | 2 |  |  |  |  | 3 | my @table_data     = @{$col_val}; | 
|  | 2 |  |  |  |  | 3 |  | 
| 31 | 2 |  |  |  |  | 3 | my @get_data_value = (); | 
| 32 | 2 |  |  |  |  | 6 | my $field_col      = join ', ', @table_field; | 
| 33 | 2 |  |  |  |  | 3 | my $value_col      = ''; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 2 | 50 |  |  |  | 5 | if ((scalar @table_field) == (scalar @table_data)) { | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 2 | 50 | 66 |  |  | 13 | 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 |  |  |  |  | 6 | = $self->QueryUtil->replace_data_value_insert(\@table_data); | 
| 44 | 1 |  |  |  |  | 4 | $value_col = join ', ', @get_data_value; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  | else { | 
| 47 | 1 |  |  |  |  | 4 | $value_col = join ', ', | 
| 48 |  |  |  |  |  |  | $self->QueryUtil->replace_data_value_insert_no_pre_st(\@table_data); | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 2 |  |  |  |  | 9 | $field_col = trim($field_col); | 
| 52 | 2 |  |  |  |  | 19 | $value_col = trim($value_col); | 
| 53 | 2 |  |  |  |  | 13 | $value_col =~ s/\,$//g; | 
| 54 | 2 |  |  |  |  | 4 | $value_col =~ s/\s\,//g; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 2 |  |  |  |  | 6 | $data = "INSERT INTO $table_name($field_col) VALUES($value_col)"; | 
| 57 |  |  |  |  |  |  | } | 
| 58 | 2 |  |  |  |  | 7 | return $data; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | # For Query Update : | 
| 62 |  |  |  |  |  |  | # ------------------------------------------------------------------------ | 
| 63 |  |  |  |  |  |  | sub update { | 
| 64 | 7 |  |  | 7 | 1 | 111 | my $self    = shift; | 
| 65 | 7 |  |  |  |  | 15 | my $arg_len = scalar @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 7 | 50 | 33 |  |  | 27 | if ($arg_len > 2 || $arg_len >= 5) { | 
| 68 | 7 |  |  |  |  | 20 | my $method_name = '_qUpdate_arg' . $arg_len; | 
| 69 | 7 | 50 |  |  |  | 36 | if ($self->can($method_name)) { | 
| 70 | 7 |  |  |  |  | 27 | return $self->$method_name(@_); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 0 |  |  |  |  | 0 | return ''; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | # For Query Delete : | 
| 77 |  |  |  |  |  |  | # ------------------------------------------------------------------------ | 
| 78 |  |  |  |  |  |  | sub delete { | 
| 79 | 2 |  |  | 2 | 1 | 93 | my $self = shift; | 
| 80 | 2 |  |  |  |  | 5 | my ($table_name, $clause) = @_; | 
| 81 | 2 |  |  |  |  | 4 | my $data = ''; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 2 | 50 |  |  |  | 6 | if (ref($clause) eq "HASH") { | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | #    my $size_clause = scalar keys %{$clause}; | 
| 86 | 2 | 50 |  |  |  | 5 | if (exists $clause->{where}) { | 
| 87 | 2 |  |  |  |  | 7 | my $where_clause = $self->QueryUtil->create_clause($clause); | 
| 88 | 2 |  |  |  |  | 6 | $data = "DELETE FROM $table_name \n$where_clause"; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  | } | 
| 91 | 2 |  |  |  |  | 5 | return $data; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | # For Query Select : | 
| 95 |  |  |  |  |  |  | # ------------------------------------------------------------------------ | 
| 96 |  |  |  |  |  |  | sub select { | 
| 97 | 6 |  |  | 6 | 1 | 99 | my $self    = shift; | 
| 98 | 6 |  |  |  |  | 9 | my $arg_len = scalar @_; | 
| 99 | 6 |  |  |  |  | 7 | my $data; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 6 | 50 |  |  |  | 20 | $data = $self->_qSelect_arg3(@_) unless ($arg_len < 2); | 
| 102 | 6 |  |  |  |  | 10 | return $data; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | # For Query Select Join : | 
| 106 |  |  |  |  |  |  | # ------------------------------------------------------------------------ | 
| 107 |  |  |  |  |  |  | sub select_join { | 
| 108 | 3 |  |  | 3 | 1 | 100 | my $self    = shift; | 
| 109 | 3 |  |  |  |  | 4 | my $arg_len = scalar @_; | 
| 110 | 3 |  |  |  |  | 4 | my $data    = ''; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 3 | 50 |  |  |  | 11 | $data = $self->_qSelectJoin_arg3(@_) unless ($arg_len < 3); | 
| 113 | 3 |  |  |  |  | 6 | return $data; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # For Create Table : | 
| 117 |  |  |  |  |  |  | # ------------------------------------------------------------------------ | 
| 118 |  |  |  |  |  |  | sub create_table { | 
| 119 | 4 |  |  | 4 | 1 | 445 | my $self    = shift; | 
| 120 | 4 |  |  |  |  | 10 | my $arg_len = scalar @_; | 
| 121 | 4 |  |  |  |  | 8 | my $result  = ''; | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 4 | 50 |  |  |  | 13 | if ($arg_len >= 3) { | 
| 124 | 4 |  | 100 |  |  | 20 | my $tables = CellBIS::SQL::Abstract::Table->new(db_type => $self->db_type | 
| 125 |  |  |  |  |  |  | // 'mysql'); | 
| 126 | 4 |  |  |  |  | 98 | $result = $tables->create_query_table(@_); | 
| 127 |  |  |  |  |  |  | } | 
| 128 | 4 |  |  |  |  | 12 | return $result; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | sub _qUpdate_arg3 { | 
| 132 | 1 |  |  | 1 |  | 4 | my $self = shift; | 
| 133 | 1 |  |  |  |  | 4 | my ($table_name, $col_val, $clause) = @_; | 
| 134 | 1 |  |  |  |  | 5 | my $data = ''; | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 1 | 50 |  |  |  | 6 | Carp::croak '$col_val is must be hashref datatype' | 
| 137 |  |  |  |  |  |  | unless ref $col_val eq "HASH"; | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 1 | 50 |  |  |  | 6 | if (exists $clause->{where}) { | 
| 140 |  |  |  |  |  |  | my @field = map { | 
| 141 |  |  |  |  |  |  | $col_val->{$_} =~ qr/date|datetime|now|NOW/ | 
| 142 |  |  |  |  |  |  | ? $_ . ' = ' . $col_val->{$_} | 
| 143 |  |  |  |  |  |  | : $_ . ' = ' . "'" | 
| 144 | 3 | 50 |  |  |  | 47 | . $col_val->{$_} . "'" | 
| 145 | 1 |  |  |  |  | 2 | } keys %{$col_val}; | 
|  | 1 |  |  |  |  | 6 |  | 
| 146 | 1 |  |  |  |  | 5 | my $field_change = join ', ', @field; | 
| 147 | 1 |  |  |  |  | 7 | my $where_clause = $self->QueryUtil->create_clause($clause); | 
| 148 | 1 |  |  |  |  | 8 | $data = "UPDATE $table_name \nSET $field_change \n$where_clause"; | 
| 149 |  |  |  |  |  |  | } | 
| 150 | 1 |  |  |  |  | 7 | return $data; | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | sub _qUpdate_arg4 { | 
| 154 | 3 |  |  | 3 |  | 7 | my $self = shift; | 
| 155 | 3 |  |  |  |  | 10 | my ($table_name, $column, $value, $clause) = @_; | 
| 156 | 3 |  |  |  |  | 9 | my $data = ''; | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 3 | 50 |  |  |  | 10 | if (exists $clause->{where}) { | 
| 159 | 3 |  |  |  |  | 13 | my @get_value = $self->QueryUtil->col_with_val($column, $value); | 
| 160 | 3 |  |  |  |  | 13 | my $field_change = join ', ', @get_value; | 
| 161 | 3 |  |  |  |  | 10 | my $where_clause = $self->QueryUtil->create_clause($clause); | 
| 162 | 3 |  |  |  |  | 16 | $data = "UPDATE $table_name \nSET $field_change \n$where_clause"; | 
| 163 |  |  |  |  |  |  | } | 
| 164 | 3 |  |  |  |  | 13 | return $data; | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | sub _qUpdate_arg5 { | 
| 168 | 3 |  |  | 3 |  | 5 | my $self = shift; | 
| 169 | 3 |  |  |  |  | 11 | my ($table_name, $column, $value, $clause, $type) = @_; | 
| 170 | 3 |  |  |  |  | 8 | my $data = ''; | 
| 171 |  |  |  |  |  |  |  | 
| 172 | 3 |  |  |  |  | 5 | my @table_field  = @{$column}; | 
|  | 3 |  |  |  |  | 10 |  | 
| 173 | 3 |  |  |  |  | 6 | my $field_change = ''; | 
| 174 | 3 |  |  |  |  | 6 | my $where_clause = ''; | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 3 | 50 | 33 |  |  | 18 | 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 |  |  |  | 10 | if (exists $clause->{where}) { | 
| 189 | 3 |  |  |  |  | 8 | $field_change = join '=?, ', @table_field; | 
| 190 | 3 |  |  |  |  | 7 | $field_change .= '=?'; | 
| 191 | 3 |  |  |  |  | 13 | $where_clause = $self->QueryUtil->create_clause($clause); | 
| 192 | 3 |  |  |  |  | 14 | $data         = "UPDATE $table_name \nSET $field_change \n$where_clause"; | 
| 193 |  |  |  |  |  |  | } | 
| 194 |  |  |  |  |  |  | } | 
| 195 | 3 |  |  |  |  | 13 | 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 |  |  |  |  | 6 | my @col      = @{$column}; | 
|  | 6 |  |  |  |  | 10 |  | 
| 205 | 6 |  |  |  |  | 7 | my $size_col = scalar @col; | 
| 206 |  |  |  |  |  |  |  | 
| 207 | 6 | 100 |  |  |  | 14 | if (ref($clause) eq "HASH") { | 
| 208 | 5 |  |  |  |  | 6 | my $field_change; | 
| 209 |  |  |  |  |  |  | my $where_clause; | 
| 210 |  |  |  |  |  |  |  | 
| 211 | 5 |  |  |  |  | 7 | my $size_clause = scalar keys %{$clause}; | 
|  | 5 |  |  |  |  | 10 |  | 
| 212 |  |  |  |  |  |  |  | 
| 213 | 5 | 50 |  |  |  | 8 | if ($size_clause != 0) { | 
| 214 | 5 |  |  |  |  | 13 | $where_clause = $self->QueryUtil->create_clause($clause); | 
| 215 | 5 | 100 |  |  |  | 10 | if (scalar @col == 0) { | 
| 216 | 4 |  |  |  |  | 9 | $data = 'SELECT * FROM ' . $table_name . "\n" . $where_clause; | 
| 217 |  |  |  |  |  |  | } | 
| 218 |  |  |  |  |  |  | else { | 
| 219 | 1 | 50 |  |  |  | 5 | $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 |  |  |  |  | 2 | my $field_change = ''; | 
| 241 |  |  |  |  |  |  |  | 
| 242 | 1 | 50 |  |  |  | 3 | if ($size_col == 0) { | 
| 243 | 1 |  |  |  |  | 3 | $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 |  |  |  |  | 11 | 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 |  |  |  |  | 6 | my ($table_name, $column, $clause) = @_; | 
| 258 | 3 |  |  |  |  | 4 | my $data = ''; | 
| 259 |  |  |  |  |  |  |  | 
| 260 | 3 |  |  |  |  | 4 | my $size_col     = scalar @{$column}; | 
|  | 3 |  |  |  |  | 4 |  | 
| 261 | 3 |  |  |  |  | 4 | my $field_change = ''; | 
| 262 | 3 | 50 |  |  |  | 8 | $field_change = '*' if $size_col == 0; | 
| 263 | 3 | 50 |  |  |  | 5 | $field_change = join ', ', @{$column} if $size_col >= 1; | 
|  | 3 |  |  |  |  | 7 |  | 
| 264 | 3 |  |  |  |  | 5 | my $where_clause = ''; | 
| 265 | 3 |  |  |  |  | 4 | my $join_clause  = ''; | 
| 266 |  |  |  |  |  |  |  | 
| 267 | 3 | 50 |  |  |  | 7 | if (ref($clause) eq "HASH") { | 
| 268 | 3 | 50 |  |  |  | 6 | if (exists $clause->{join}) { | 
| 269 | 3 |  |  |  |  | 9 | $join_clause = $self->QueryUtil->for_onjoin($clause, $table_name); | 
| 270 | 3 |  |  |  |  | 7 | $where_clause = $self->QueryUtil->create_clause($clause); | 
| 271 | 3 |  |  |  |  | 8 | $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 |  |  |  |  | 6 | return $data; | 
| 282 |  |  |  |  |  |  | } | 
| 283 |  |  |  |  |  |  |  | 
| 284 |  |  |  |  |  |  | sub to_one_liner { | 
| 285 | 22 |  |  | 22 | 0 | 135 | my ($self, $result) = @_; | 
| 286 |  |  |  |  |  |  |  | 
| 287 | 22 |  |  |  |  | 71 | $result =~ s/\t+//g; | 
| 288 | 22 |  |  |  |  | 102 | $result =~ s/\,\s+/\, /g; | 
| 289 | 22 |  |  |  |  | 243 | $result =~ s/\s+/ /g; | 
| 290 | 22 |  |  |  |  | 144 | return $result; | 
| 291 |  |  |  |  |  |  | } | 
| 292 |  |  |  |  |  |  |  | 
| 293 |  |  |  |  |  |  | 1; |