This commit is contained in:
2021-06-30 11:40:15 +08:00
parent a3f02701ca
commit f8a28967d5
+29 -6
View File
@@ -74,7 +74,7 @@ class SqlBuilder extends Component
* @return bool|array * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function mathematics(array $attributes, $opera = '+'): bool|array public function mathematics(array $attributes, string $opera = '+'): bool|array
{ {
$string = $array = []; $string = $array = [];
@@ -104,7 +104,7 @@ class SqlBuilder extends Component
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function insert(array $attributes, $isBatch = false): array public function insert(array $attributes, bool $isBatch = false): array
{ {
$update = sprintf('INSERT INTO %s', $this->tableName()); $update = sprintf('INSERT INTO %s', $this->tableName());
if ($isBatch === false) { if ($isBatch === false) {
@@ -156,16 +156,39 @@ class SqlBuilder extends Component
* @return array[] * @return array[]
* a=:b, * a=:b,
*/ */
private function builderParams(array $attributes, bool $isInsert = false, $params = [], $order = 0): array #[Pure] private function builderParams(array $attributes, bool $isInsert = false, array $params = [], int $order = 0): array
{ {
$keys = []; $keys = [];
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
if ($isInsert === true) { if ($isInsert === true) {
$keys[] = ':' . $key . $order; $keys[] = ':' . $key . $order;
$params[$key . $order] = $value;
} else { } else {
$keys[] = $key . '=:' . $key . $order; [$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys);
} }
}
return [$keys, $params];
}
/**
* @param string $key
* @param mixed $value
* @param int $order
* @param array $params
* @param array $keys
* @return array
*/
private function resolveParams(string $key, mixed $value, int $order, array $params, array $keys): array
{
if (
str_starts_with($value, '+') ||
str_starts_with($value, '-')
) {
$keys[] = $key . '=' . $key . $value;
} else {
$params[$key . $order] = $value; $params[$key . $order] = $value;
$keys[] = $key . '=:' . $key . $order;
} }
return [$keys, $params]; return [$keys, $params];
} }
@@ -226,7 +249,7 @@ class SqlBuilder extends Component
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _prefix($hasOrder = false): string private function _prefix(bool $hasOrder = false): string
{ {
$select = ''; $select = '';
if (!empty($this->query->from)) { if (!empty($this->query->from)) {
@@ -286,7 +309,7 @@ class SqlBuilder extends Component
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function get($isCount = false): string public function get(bool $isCount = false): string
{ {
if ($isCount === false) { if ($isCount === false) {
return $this->all(); return $this->all();