Skip to content

Commit 5a473d8

Browse files
committed
refactor select() method
1 parent 2fcf600 commit 5a473d8

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/QueryBuilder.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ private function prepareConditions($where): array
457457
private function searchForSpecChars(string $str): bool
458458
{
459459
foreach (self::FIELD_SPEC_CHARS as $char) {
460-
if (strpos($char, $str) !== false) {
460+
if (mb_strpos($str, $char) !== false) {
461461
return true;
462462
}
463463
}
@@ -568,23 +568,29 @@ public function select($table, $fields = '*', $dist = false): QueryBuilder
568568
return $this;
569569
}
570570

571-
$this->reset();
571+
$preparedTable = $this->prepareTables($table);
572+
$preparedFields = $this->prepareAliases($fields);
572573

573-
$this->sql = "SELECT ";
574-
$this->sql .= $dist ? "DISTINCT " : '';
574+
if (!$this->concat) {
575+
$this->reset();
576+
}
575577

576-
if (is_array($fields) || is_string($fields)) {
577-
$this->sql .= $this->prepareAliases($fields);
578-
} else {
579-
$this->setError('Incorrect type of $fields in ' . __METHOD__ . '. $fields must be a string or an array');
580-
return $this;
581-
}
578+
$sql = "SELECT ";
579+
$sql .= $dist ? "DISTINCT " : '';
582580

583-
if (is_array($table) || is_string($table)) {
584-
$this->sql .= " FROM {$this->prepareAliases($table)}";
585-
} else {
586-
$this->setError('Incorrect type of $table in ' . __METHOD__ . '. $table must be a string or an array');
587-
}
581+
if (is_string($table) && $this->searchForSpecChars($table) && $fields == '*') {
582+
$sql .= $preparedTable;
583+
$this->fields = $this->prepareAliases($table);
584+
} else {
585+
$this->fields = $fields;
586+
$sql .= "{$preparedFields} FROM {$preparedTable}";
587+
}
588+
589+
if ($this->concat) {
590+
$this->sql .= $sql;
591+
} else {
592+
$this->sql = $sql;
593+
}
588594

589595
return $this;
590596
}
@@ -650,9 +656,9 @@ public function like($field, string $value = ''): QueryBuilder
650656
return $this;
651657
}
652658

653-
if (is_string($field) && !empty($field) && is_string($value) && !empty($value)) {
654-
$this->where([[$field, 'LIKE', $value]]);
655-
} else if (is_string($field) && empty($value)) {
659+
if (is_string($field) && !empty($field) && !empty($value)) {
660+
$this->where([[$field, 'LIKE', $value]]);
661+
} else if (is_string($field) && empty($value)) {
656662
$this->where($field);
657663
} else if (is_array($field)) {
658664
$this->where([[$field[0], 'LIKE', $field[1]]]);

0 commit comments

Comments
 (0)