Skip to content

Commit c55aa98

Browse files
committed
Fixed bugs on "select" method and update "02-select.t"
1 parent d12853b commit c55aa98

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

lib/CellBIS/SQL/Abstract.pm

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,37 +147,41 @@ sub _qSelect_arg3 {
147147
my $data = '';
148148
my @col = @{$column};
149149
my $size_col = scalar @col;
150-
my $field_change = '';
151-
my $where_clause = '';
152150

153151
if (ref($clause) eq "HASH") {
152+
my $field_change;
153+
my $where_clause;
154+
154155
my $size_clause = scalar keys %{$clause};
155156

156-
unless ($size_clause == 0) {
157+
if ($size_clause != 0) {
157158
$where_clause = CellBIS::SQL::Abstract::Utils->create_clause($clause);
158-
if ($size_col == 0) {
159-
$field_change = '*';
159+
if (scalar @col == 0) {
160+
$data = 'SELECT * FROM '.$table_name . $where_clause;
160161
}
161162

162-
if ($size_col == 1) {
163-
$field_change = join ', ', @col if (ref($column) eq 'ARRAY');
164-
$field_change = '*';
163+
elsif (scalar @col => 1) {
164+
$field_change = ref($column) eq "ARRAY" ? (join ', ', @col) : '*';
165+
$data = 'SELECT '. $field_change . ' FROM '. $table_name . $where_clause;
165166
}
166167

167-
$data = "SELECT $field_change FROM $table_name" . $where_clause;
168168
}
169169
else {
170170
if ($size_col == 0) {
171+
say 'size_col', $size_col, 'true';
171172
$data = "SELECT * FROM $table_name";
172173
}
173174

174175
if ($size_col >= 1) {
176+
say 'size_col', $size_col, 'true';
175177
$field_change = join ', ', @col;
176178
$data = "SELECT $field_change FROM $table_name";
177179
}
178180
}
179181
}
180182
else {
183+
my $field_change = '';
184+
181185
if ($size_col == 0) {
182186
$data = "SELECT * FROM $table_name";
183187
}

t/02-select.t

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ use CellBIS::SQL::Abstract;
1010

1111
my $sql_abstract = CellBIS::SQL::Abstract->new();
1212

13-
my $select1 = $sql_abstract->select('table_test', []);
14-
ok($select1 eq 'SELECT * FROM table_test', "SQL Query [$select1] is true");
13+
my $select = $sql_abstract->select('table_test', []);
14+
ok($select eq 'SELECT * FROM table_test', "SQL Query [$select] is true");
1515

16-
my $select2 = $sql_abstract->select('table_test', [], {
16+
$select = $sql_abstract->select('table_test', [], {
1717
'orderby' => 'id_test',
1818
'order' => 'asc',
1919
'limit' => '5'
2020
});
21-
ok($select2 eq 'SELECT * FROM table_test ORDER BY id_test ASC LIMIT 5', "SQL Query [$select2] is true");
21+
ok($select eq 'SELECT * FROM table_test ORDER BY id_test ASC LIMIT 5', "SQL Query [$select] is true");
22+
23+
$select = $sql_abstract->select('table_test', ['data'], {
24+
'groupby' => 'data',
25+
'orderby' => 'id_test',
26+
'order' => 'asc',
27+
'limit' => '5'
28+
});
29+
ok($select eq 'SELECT data FROM table_test GROUP BY data ORDER BY id_test ASC LIMIT 5', "SQL Query [$select] is true");
2230

2331
done_testing();

0 commit comments

Comments
 (0)