|
| 1 | +package CellBIS::SQL::Abstract; |
| 2 | +use Mojo::Base -base; |
| 3 | + |
| 4 | +use Scalar::Util qw(blessed); |
| 5 | +use Mojo::Util qw(trim); |
| 6 | +use CellBIS::SQL::Abstract::Utils; |
| 7 | + |
| 8 | +# ABSTRACT: SQL Abstract |
| 9 | +our $VERSION = '0.1'; |
| 10 | + |
| 11 | +# For Query Insert : |
| 12 | +# ------------------------------------------------------------------------ |
| 13 | +sub insert { |
| 14 | + my $self = shift; |
| 15 | + my $arg_len = scalar @_; |
| 16 | + my $data = ''; |
| 17 | + my ($table_name, $column, $col_val, $type); |
| 18 | + if ($arg_len == 3) { |
| 19 | + ($table_name, $column, $col_val) = @_; |
| 20 | + } |
| 21 | + if ($arg_len >= 4) { |
| 22 | + ($table_name, $column, $col_val, $type) = @_; |
| 23 | + } |
| 24 | + my @table_field = @{$column}; |
| 25 | + my @table_data = @{$col_val}; |
| 26 | + my @get_data_value = (); |
| 27 | + my $field_col = join ', ', @table_field; |
| 28 | + my $value_col = ''; |
| 29 | + |
| 30 | + if ((scalar @table_field) == (scalar @table_data)) { |
| 31 | + |
| 32 | + if ($type and $type ne '' and $type eq 'no-pre-st') { |
| 33 | + $value_col = join ', ', @table_data; |
| 34 | + } |
| 35 | + elsif ($type and $type ne '' and $type eq 'pre-st') { |
| 36 | + @get_data_value = CellBIS::SQL::Abstract::Utils->replace_data_value_insert(\@table_data); |
| 37 | + $value_col = join ', ', @get_data_value; |
| 38 | + } |
| 39 | + else { |
| 40 | + @get_data_value = CellBIS::SQL::Abstract::Utils->replace_data_value_insert(\@table_data); |
| 41 | + $value_col = join ', ', @get_data_value; |
| 42 | + } |
| 43 | + |
| 44 | + $field_col = trim($field_col); |
| 45 | + $value_col = trim($value_col); |
| 46 | + $value_col =~ s/\,$//g; |
| 47 | + $value_col =~ s/\s\,//g; |
| 48 | + |
| 49 | + $data = "INSERT INTO $table_name($field_col) VALUES($value_col)"; |
| 50 | + } |
| 51 | + return $data; |
| 52 | +} |
| 53 | + |
| 54 | +# For Query Update : |
| 55 | +# ------------------------------------------------------------------------ |
| 56 | +sub update { |
| 57 | + my $self = shift; |
| 58 | + my $arg_len = scalar @_; |
| 59 | + my ($table_name, $column, $value, $clause, $type); |
| 60 | + |
| 61 | + if ($arg_len == 4) { |
| 62 | + ($table_name, $column, $value, $clause) = @_; |
| 63 | + } |
| 64 | + if ($arg_len == 5) { |
| 65 | + ($table_name, $column, $value, $clause, $type) = @_; |
| 66 | + } |
| 67 | + my $data = ''; |
| 68 | + |
| 69 | + my @table_field = @{$column}; |
| 70 | + my $field_change = ''; |
| 71 | + my $where_clause = ''; |
| 72 | + |
| 73 | + if ($type and $type ne '' and $type eq 'no-pre-st') { |
| 74 | + my @get_value = CellBIS::SQL::Abstract::Utils->col_with_val(@table_field, @{$value}); |
| 75 | + $field_change = join ', ', @get_value; |
| 76 | + |
| 77 | + if (exists $clause->{where}) { |
| 78 | + $where_clause = CellBIS::SQL::Abstract::Utils->create_clause($clause); |
| 79 | + $data = "UPDATE $table_name SET $field_change" . $where_clause; |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + elsif ($type and $type ne '' and $type eq 'pre-st') { |
| 84 | + $field_change = join '=?, ', @table_field; |
| 85 | + $field_change .= '=?'; |
| 86 | + |
| 87 | + if (exists $clause->{where}) { |
| 88 | + $where_clause = CellBIS::SQL::Abstract::Utils->create_clause($clause); |
| 89 | + $data = "UPDATE $table_name SET $field_change" . $where_clause; |
| 90 | + } |
| 91 | + } |
| 92 | + else { |
| 93 | + $field_change = join '=?, ', @table_field; |
| 94 | + $field_change .= '=?'; |
| 95 | + |
| 96 | + if (exists $clause->{where}) { |
| 97 | + $where_clause = CellBIS::SQL::Abstract::Utils->create_clause($clause); |
| 98 | + $data = "UPDATE $table_name SET $field_change" . $where_clause; |
| 99 | + } |
| 100 | + } |
| 101 | + return $data; |
| 102 | +} |
| 103 | + |
| 104 | +# For Query Delete : |
| 105 | +# ------------------------------------------------------------------------ |
| 106 | +sub delete { |
| 107 | + my $self = shift; |
| 108 | + my ($table_name, $clause) = @_; |
| 109 | + my $data = ''; |
| 110 | + |
| 111 | + if (ref($clause) eq "HASH") { |
| 112 | + # my $size_clause = scalar keys %{$clause}; |
| 113 | + if (exists $clause->{where}) { |
| 114 | + my $where_clause = CellBIS::SQL::Abstract::Utils->create_clause($clause); |
| 115 | + $data = "DELETE FROM $table_name" . $where_clause; |
| 116 | + } |
| 117 | + } |
| 118 | + return $data; |
| 119 | +} |
| 120 | + |
| 121 | +# For Query Select : |
| 122 | +# ------------------------------------------------------------------------ |
| 123 | +sub select { |
| 124 | + my $self = shift; |
| 125 | + my $arg_len = scalar @_; |
| 126 | + my $data; |
| 127 | + |
| 128 | + $data = $self->_qSelect_arg3(@_) unless ($arg_len < 2); |
| 129 | + return $data; |
| 130 | +} |
| 131 | + |
| 132 | +# For Query Select Join : |
| 133 | +# ------------------------------------------------------------------------ |
| 134 | +sub select_join { |
| 135 | + my $self = shift; |
| 136 | + my $arg_len = scalar @_; |
| 137 | + my $data = ''; |
| 138 | + |
| 139 | + $data = $self->_qSelectJoin_arg3(@_) unless ($arg_len < 3); |
| 140 | + return $data; |
| 141 | +} |
| 142 | +# For Action Query String - "select" - arg3 : |
| 143 | +# ------------------------------------------------------------------------ |
| 144 | +sub _qSelect_arg3 { |
| 145 | + my $self = shift; |
| 146 | + my ($table_name, $column, $clause) = @_; |
| 147 | + my $data = ''; |
| 148 | + my @col = @{$column}; |
| 149 | + my $size_col = scalar @col; |
| 150 | + my $field_change = ''; |
| 151 | + my $where_clause = ''; |
| 152 | + |
| 153 | + if (ref($clause) eq "HASH") { |
| 154 | + my $size_clause = scalar keys %{$clause}; |
| 155 | + |
| 156 | + unless ($size_clause == 0) { |
| 157 | + $where_clause = CellBIS::SQL::Abstract::Utils->create_clause($clause); |
| 158 | + if ($size_col == 0) { |
| 159 | + $field_change = '*'; |
| 160 | + } |
| 161 | + |
| 162 | + if ($size_col == 1) { |
| 163 | + $field_change = join ', ', @col if (ref($column) eq 'ARRAY'); |
| 164 | + $field_change = '*'; |
| 165 | + } |
| 166 | + |
| 167 | + $data = "SELECT $field_change FROM $table_name" . $where_clause; |
| 168 | + } |
| 169 | + else { |
| 170 | + if ($size_col == 0) { |
| 171 | + $data = "SELECT * FROM $table_name"; |
| 172 | + } |
| 173 | + |
| 174 | + if ($size_col >= 1) { |
| 175 | + $field_change = join ', ', @col; |
| 176 | + $data = "SELECT $field_change FROM $table_name"; |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + else { |
| 181 | + if ($size_col == 0) { |
| 182 | + $data = "SELECT * FROM $table_name"; |
| 183 | + } |
| 184 | + if ($size_col >= 1) { |
| 185 | + $field_change = join ', ', @col; |
| 186 | + $data = "SELECT $field_change FROM $table_name"; |
| 187 | + } |
| 188 | + } |
| 189 | + return $data; |
| 190 | +} |
| 191 | + |
| 192 | +# For Action Query String - "select_join" - arg3 : |
| 193 | +# ------------------------------------------------------------------------ |
| 194 | +sub _qSelectJoin_arg3 { |
| 195 | + my $self = shift; |
| 196 | + my ($table_name, $column, $clause) = @_; |
| 197 | + my $data = ''; |
| 198 | + |
| 199 | + my $size_col = scalar @{$column}; |
| 200 | + my $field_change = ''; |
| 201 | + $field_change = '*' if $size_col == 0; |
| 202 | + $field_change = join ', ', @{$column} if $size_col >= 1; |
| 203 | + my $where_clause = ''; |
| 204 | + my $join_clause = ''; |
| 205 | + |
| 206 | + if (ref($clause) eq "HASH") { |
| 207 | + if (exists $clause->{join}) { |
| 208 | + $join_clause = CellBIS::SQL::Abstract::Utils->for_onjoin($clause, $table_name); |
| 209 | + $where_clause = CellBIS::SQL::Abstract::Utils->create_clause($clause); |
| 210 | + $data = "SELECT $field_change $join_clause" . $where_clause; |
| 211 | + } |
| 212 | + else { |
| 213 | + $where_clause = CellBIS::SQL::Abstract::Utils->create_clause($clause); |
| 214 | + $data = "SELECT $field_change FROM $table_name"; |
| 215 | + } |
| 216 | + } |
| 217 | + else { |
| 218 | + $data = "SELECT $field_change FROM $table_name"; |
| 219 | + } |
| 220 | + return $data; |
| 221 | +} |
| 222 | + |
| 223 | +1; |
| 224 | + |
| 225 | +=encoding utf8 |
| 226 | +
|
| 227 | +=head1 NAME |
| 228 | +
|
| 229 | +CellBIS::SQL::Abstract - SQL Abstract |
| 230 | +
|
| 231 | +=head1 DESCRIPTION |
| 232 | +
|
| 233 | +The purpose of this module is to support SQL abstraction in L<Mojo::mysql>. |
| 234 | +This module inherits from L<Mojo::Base> |
| 235 | +
|
| 236 | +=head1 AUTHOR |
| 237 | +
|
| 238 | +Achmad Yusri Afandi, E<lt>[email protected]E<gt> |
| 239 | +
|
| 240 | +=head1 COPYRIGHT AND LICENSE |
| 241 | +
|
| 242 | +Copyright (C) 2018 by Achmad Yusri Afandi |
| 243 | +
|
| 244 | +=cut |
0 commit comments