diff --git a/src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php b/src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php index 05d32bc..32c19d1 100644 --- a/src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php +++ b/src/Pecee/Pixie/QueryBuilder/Adapters/BaseAdapter.php @@ -186,11 +186,23 @@ protected function buildCriteria(array $statements, bool $bindValues = true): ar // Get the criteria only query from the nestedCriteria object $queryObject = $nestedCriteria->getQuery('criteriaOnly', true); + // Skip if the closure didn't add any criteria (empty closure) + $sql = $queryObject->getSql(); + if (trim($sql) === '') + { + // Remove the joiner (AND/OR) that was already pushed before the closure check + if ($joiner !== '') + { + array_pop($criteria); + } + continue; + } + // Merge the bindings we get from nestedCriteria object $bindings[] = $queryObject->getBindings(); // Append the sql we get from the nestedCriteria object - $criteria[] = "({$queryObject->getSql()})"; + $criteria[] = "({$sql})"; continue; } @@ -358,7 +370,7 @@ protected function buildAliasedTableName(string $table, array $statements): stri { $this->aliasPrefix = $statements['aliases'][$table] ?? null; if ($this->aliasPrefix !== null) { - return sprintf('%s AS %s', $this->wrapSanitizer($table), $this->wrapSanitizer(strtolower($this->aliasPrefix))); + return sprintf('%s AS %s', $this->wrapSanitizer($table), $this->wrapSanitizer($this->aliasPrefix)); } return sprintf('%s', $this->wrapSanitizer($table)); @@ -375,7 +387,8 @@ protected function buildAliasedTableName(string $table, array $statements): stri */ public function criteriaOnly(array $statements, $bindValues = true): array { - $sql = $bindings = []; + $sql = ''; + $bindings = []; if (isset($statements['criteria']) === false) { return compact('sql', 'bindings'); } @@ -443,7 +456,7 @@ private function doInsert(array $statements, array $data, $type): array $keys[] = $key; if ($value instanceof Raw) { $values[] = (string)$value; - $bindings[] = $value->getBindings(); + $bindings = array_merge($bindings, (array)$value->getBindings()); // ✅ FIXED } else { $values[] = '?'; $bindings[] = $value; @@ -493,7 +506,7 @@ private function getUpdateStatement(array $data): array if ($value instanceof Raw) { $statements[] = $statement . $value; - $bindings += $value->getBindings(); + $bindings = array_merge($bindings, (array)$value->getBindings()); // fixed } else { $statements[] = $statement . '?'; $bindings[] = $value; @@ -807,4 +820,4 @@ public function wrapSanitizer($value) // Join these back with "." and return return implode('.', $valueArr); } -} \ No newline at end of file +} diff --git a/src/Pecee/Pixie/QueryBuilder/Adapters/Sqlserver.php b/src/Pecee/Pixie/QueryBuilder/Adapters/Sqlserver.php index 2d35e5e..972fbac 100644 --- a/src/Pecee/Pixie/QueryBuilder/Adapters/Sqlserver.php +++ b/src/Pecee/Pixie/QueryBuilder/Adapters/Sqlserver.php @@ -75,7 +75,7 @@ public function select(array $statements): array $prefix = $statements['aliases'][$table] ?? null; if ($prefix !== null) { - $t = sprintf('%s AS %s', $table, strtolower($prefix)); + $t = sprintf('%s AS %s', $table, $prefix); } else { $t = sprintf('%s', $table); } @@ -179,4 +179,4 @@ public function wrapSanitizer($value) // Join these back with "." and return return implode('.', $valueArr); } -} \ No newline at end of file +} diff --git a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php index 22be66a..b861ea8 100644 --- a/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php +++ b/src/Pecee/Pixie/QueryBuilder/QueryBuilderHandler.php @@ -698,7 +698,7 @@ public function alias(string $alias, ?string $table = null): self $table = $this->tablePrefix . $table; } - $this->statements['aliases'][$table] = \strtolower($alias); + $this->statements['aliases'][$table] = $alias; return $this; } @@ -1883,4 +1883,4 @@ public function close(): void $this->connection = null; } -} \ No newline at end of file +}