Skip to content

Commit 6759992

Browse files
committed
Improve method "update" on "Abstract.pm"
for support use argument with hashref datatype for column and value
1 parent 2ec57da commit 6759992

File tree

2 files changed

+93
-36
lines changed

2 files changed

+93
-36
lines changed

lib/CellBIS/SQL/Abstract.pm

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package CellBIS::SQL::Abstract;
22
use Mojo::Base -base;
33

44
use Scalar::Util qw(blessed);
5+
use Carp ();
56
use Mojo::Util qw(trim);
67
use CellBIS::SQL::Abstract::Util;
78
use CellBIS::SQL::Abstract::Table;
@@ -62,45 +63,14 @@ sub insert {
6263
sub update {
6364
my $self = shift;
6465
my $arg_len = scalar @_;
65-
my ($table_name, $column, $value, $clause, $type);
66-
my $data = '';
67-
68-
($table_name, $column, $value, $clause) = @_ if $arg_len == 4;
69-
($table_name, $column, $value, $clause, $type) = @_ if $arg_len >= 5;
70-
71-
my @table_field = @{$column};
72-
my $field_change = '';
73-
my $where_clause = '';
74-
75-
if ($type && $type eq 'no-pre-st') {
76-
my @get_value = $self->QueryUtil->col_with_val($column, $value);
77-
$field_change = join ', ', @get_value;
78-
79-
if (exists $clause->{where}) {
80-
$where_clause = $self->QueryUtil->create_clause($clause);
81-
$data = "UPDATE $table_name \nSET $field_change \n$where_clause";
82-
}
83-
84-
}
85-
elsif ($type && $type eq 'pre-st') {
86-
$field_change = join '=?, ', @table_field;
87-
$field_change .= '=?';
88-
89-
if (exists $clause->{where}) {
90-
$where_clause = $self->QueryUtil->create_clause($clause);
91-
$data = "UPDATE $table_name \nSET $field_change \n$where_clause";
92-
}
93-
}
94-
else {
95-
my @get_value = $self->QueryUtil->col_with_val($column, $value);
96-
$field_change = join ', ', @get_value;
9766

98-
if (exists $clause->{where}) {
99-
$where_clause = $self->QueryUtil->create_clause($clause);
100-
$data = "UPDATE $table_name \nSET $field_change \n$where_clause";
67+
if ($arg_len > 2 || $arg_len >= 5) {
68+
my $method_name = '_qUpdate_arg' . $arg_len;
69+
if ($self->can($method_name)) {
70+
return $self->$method_name(@_);
10171
}
10272
}
103-
return $data;
73+
return '';
10474
}
10575

10676
# For Query Delete :
@@ -158,6 +128,73 @@ sub create_table {
158128
return $result;
159129
}
160130

131+
sub _qUpdate_arg3 {
132+
my $self = shift;
133+
my ($table_name, $col_val, $clause) = @_;
134+
my $data = '';
135+
136+
Carp::croak '$col_val is must be hashref datatype'
137+
unless ref $col_val eq "HASH";
138+
139+
if (exists $clause->{where}) {
140+
my @field = map {
141+
$col_val->{$_} =~ qr/date|datetime|now|NOW/
142+
? $_ . ' = ' . $col_val->{$_}
143+
: $_ . ' = ' . "'"
144+
. $col_val->{$_} . "'"
145+
} keys %{$col_val};
146+
my $field_change = join ', ', @field;
147+
my $where_clause = $self->QueryUtil->create_clause($clause);
148+
$data = "UPDATE $table_name \nSET $field_change \n$where_clause";
149+
}
150+
return $data;
151+
}
152+
153+
sub _qUpdate_arg4 {
154+
my $self = shift;
155+
my ($table_name, $column, $value, $clause) = @_;
156+
my $data = '';
157+
158+
if (exists $clause->{where}) {
159+
my @get_value = $self->QueryUtil->col_with_val($column, $value);
160+
my $field_change = join ', ', @get_value;
161+
my $where_clause = $self->QueryUtil->create_clause($clause);
162+
$data = "UPDATE $table_name \nSET $field_change \n$where_clause";
163+
}
164+
return $data;
165+
}
166+
167+
sub _qUpdate_arg5 {
168+
my $self = shift;
169+
my ($table_name, $column, $value, $clause, $type) = @_;
170+
my $data = '';
171+
172+
my @table_field = @{$column};
173+
my $field_change = '';
174+
my $where_clause = '';
175+
176+
if ($type && $type eq 'no-pre-st') {
177+
178+
if (exists $clause->{where}) {
179+
my @get_value = $self->QueryUtil->col_with_val($column, $value);
180+
$field_change = join ', ', @get_value;
181+
$where_clause = $self->QueryUtil->create_clause($clause);
182+
$data = "UPDATE $table_name \nSET $field_change \n$where_clause";
183+
}
184+
185+
}
186+
else {
187+
188+
if (exists $clause->{where}) {
189+
$field_change = join '=?, ', @table_field;
190+
$field_change .= '=?';
191+
$where_clause = $self->QueryUtil->create_clause($clause);
192+
$data = "UPDATE $table_name \nSET $field_change \n$where_clause";
193+
}
194+
}
195+
return $data;
196+
}
197+
161198
# For Action Query String - "select" - arg3 :
162199
# ------------------------------------------------------------------------
163200
sub _qSelect_arg3 {

lib/CellBIS/SQL/Abstract.pod

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,26 @@ SQL equivalent :
344344
# No Prepare Statement :
345345
UPDATE my_users SET first_name='Achmad Yusri', last_name='Afandi' WHERE id = 2;
346346

347+
=head2 update - with simple
348+
349+
use CellBIS::SQL::Abstract
350+
my $sql_abstract = CellBIS::SQL::Abstract->new;
351+
352+
my $table_name = 'my_users'; # Table name.
353+
my $col_val = {
354+
'first_name' => 'Achmad Yusri',
355+
'last_name' => 'Afandi'
356+
};
357+
my $clause = {
358+
where => 'id = 2'
359+
};
360+
my $update = $sql_abstract->update($table_name, $col_val, $clause);
361+
362+
SQL equivalent :
363+
364+
# No Prepare Statement :
365+
UPDATE my_users SET first_name='Achmad Yusri', last_name='Afandi' WHERE id = 2;
366+
347367
=head2 delete
348368

349369
use CellBIS::SQL::Abstract

0 commit comments

Comments
 (0)