Skip to content

Commit f0a6b96

Browse files
authored
Merge pull request #242 from simon-mundy/fix/scalar-specification-parameters
Render int and float clause parameters again (regression from #232)
2 parents 20fb241 + abac8b9 commit f0a6b96

8 files changed

Lines changed: 378 additions & 79 deletions

File tree

‎docs/book/sql/select.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ $select->limit(5);
195195
$select->offset(10);
196196
```
197197

198+
Both take a non-negative integer, as an `int` or a string of digits such as
199+
`'10'`. A digit string is kept as given, so a count larger than `PHP_INT_MAX`
200+
still works. Anything else, including negative numbers, fractions, exponents
201+
and surrounding whitespace, throws
202+
`PhpDb\Sql\Exception\InvalidArgumentException`.
203+
198204
## group()
199205

200206
The `group()` method specifies columns for GROUP BY clauses,

‎src/Sql/AbstractSql.php‎

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpDb\Sql\Argument\Value;
1717
use PhpDb\Sql\Argument\Values;
1818
use PhpDb\Sql\Platform\PlatformDecoratorInterface;
19+
use Stringable;
1920

2021
use function array_key_exists;
2122
use function array_key_first;
@@ -27,6 +28,7 @@
2728
use function preg_match;
2829
use function preg_replace;
2930
use function rtrim;
31+
use function sprintf;
3032
use function str_replace;
3133
use function strtoupper;
3234
use function vsprintf;
@@ -36,7 +38,11 @@
3638
*
3739
* @psalm-type ParameterSpecification = array<int|string, string>|null
3840
* @psalm-type Specification = string|array<string, list<ParameterSpecification>>
39-
* @psalm-type SqlParameters = array<array-key, string|array<array-key, string|array<array-key, string>>>
41+
* @psalm-type SqlParameterValue = string|int|float|bool|Stringable|null
42+
* @psalm-type SqlParameters = array<
43+
* array-key,
44+
* SqlParameterValue|array<array-key, SqlParameterValue|array<array-key, SqlParameterValue>>,
45+
* >
4046
* @psalm-type ColumnValue = Select|ExpressionInterface|string|int|float|bool|null
4147
* @psalm-type ColumnReference = array{
4248
* column: ColumnValue,
@@ -572,27 +578,27 @@ protected function resolveTable(
572578
}
573579

574580
/**
575-
* @param array<array-key, string|array<array-key, mixed>> $values
581+
* @param array<array-key, SqlParameterValue|array<array-key, mixed>> $values
576582
* @return list<string>
577-
* @throws Exception\RuntimeException When a value is a nested list rather than a string.
583+
* @throws Exception\RuntimeException When a value is a nested list rather than a scalar.
578584
*/
579585
private function flattenParameterList(array $values): array
580586
{
581587
$strings = [];
582588
foreach ($values as $value) {
583-
if (! is_string($value)) {
589+
if (is_array($value)) {
584590
throw Exception\RuntimeException::forUnsupportedParameterCountOf(count($value));
585591
}
586592

587-
$strings[] = $value;
593+
$strings[] = $this->stringifyParameter($value);
588594
}
589595

590596
return $strings;
591597
}
592598

593599
/**
594600
* @param array<int|string, string> $parameterSpecification
595-
* @param array<array-key, string|array<array-key, mixed>> $values
601+
* @param array<array-key, SqlParameterValue|array<array-key, mixed>> $values
596602
* @throws Exception\RuntimeException When no format accepts the number of values.
597603
*/
598604
private function formatParameterList(array $parameterSpecification, array $values): string
@@ -633,27 +639,42 @@ private function quoteJoinTableIdentifier(TableIdentifier $identifier, PlatformI
633639

634640
/**
635641
* @param ParameterSpecification $parameterSpecification
636-
* @param string|array<array-key, string|array<array-key, string>> $parameter
642+
* @param SqlParameterValue|array<array-key, SqlParameterValue|array<array-key, SqlParameterValue>> $parameter
637643
* @throws Exception\RuntimeException
638644
*/
639-
private function renderParameter(?array $parameterSpecification, string|array $parameter): string
640-
{
645+
private function renderParameter(
646+
?array $parameterSpecification,
647+
string|int|float|bool|Stringable|array|null $parameter,
648+
): string {
641649
if (null === $parameterSpecification) {
642-
return is_string($parameter)
643-
? $parameter
644-
: throw Exception\RuntimeException::forUnsupportedParameterCountOf(count($parameter));
650+
return is_array($parameter)
651+
? throw Exception\RuntimeException::forUnsupportedParameterCountOf(count($parameter))
652+
: $this->stringifyParameter($parameter);
645653
}
646654

655+
$parameters = is_array($parameter) ? $parameter : [$parameter];
656+
647657
$combinedBy = $parameterSpecification['combinedby'] ?? null;
648658
if (null === $combinedBy) {
649-
return $this->formatParameterList($parameterSpecification, (array) $parameter);
659+
return $this->formatParameterList($parameterSpecification, $parameters);
650660
}
651661

652662
$rendered = [];
653-
foreach ((array) $parameter as $values) {
654-
$rendered[] = $this->formatParameterList($parameterSpecification, (array) $values);
663+
foreach ($parameters as $values) {
664+
$rendered[] = $this->formatParameterList(
665+
$parameterSpecification,
666+
is_array($values) ? $values : [$values],
667+
);
655668
}
656669

657670
return implode($combinedBy, $rendered);
658671
}
672+
673+
/**
674+
* Converts a scalar clause parameter to the text vsprintf's %s conversion would produce.
675+
*/
676+
private function stringifyParameter(string|int|float|bool|Stringable|null $value): string
677+
{
678+
return is_string($value) ? $value : sprintf('%s', $value);
679+
}
659680
}

‎src/Sql/Exception/InvalidArgumentException.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class InvalidArgumentException extends Exception\InvalidArgumentException
6262

6363
final public const string MISSING_VALUES_OR_SELECT = 'values or select should be present';
6464

65-
final public const string NON_NUMERIC_PARAMETER = '%s expects parameter to be numeric, "%s" given';
65+
final public const string NON_NEGATIVE_INTEGER_PARAMETER = '%s expects a non-negative integer, "%s" given';
6666

6767
final public const string NON_STRING_VALUE_KEY = 'set() expects a string for the value key';
6868

@@ -183,9 +183,9 @@ public static function forMissingValuesOrSelect(): self
183183
return new self(self::MISSING_VALUES_OR_SELECT);
184184
}
185185

186-
public static function forNonNumericParameter(string $method, string $givenType): self
186+
public static function forNonNegativeIntegerParameter(string $method, int|string $given): self
187187
{
188-
return new self(sprintf(self::NON_NUMERIC_PARAMETER, $method, $givenType));
188+
return new self(sprintf(self::NON_NEGATIVE_INTEGER_PARAMETER, $method, $given));
189189
}
190190

191191
public static function forNonStringValueKey(): self

‎src/Sql/Select.php‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
use function array_keys;
1717
use function count;
1818
use function explode;
19-
use function gettype;
2019
use function is_array;
2120
use function is_int;
22-
use function is_numeric;
2321
use function is_scalar;
2422
use function is_string;
2523
use function key;
2624
use function method_exists;
25+
use function preg_match;
2726
use function preg_split;
2827
use function strcasecmp;
2928
use function stripos;
@@ -40,6 +39,7 @@
4039
* @psalm-import-type Specification from AbstractSql
4140
* @psalm-import-type ColumnReference from AbstractSql
4241
* @psalm-import-type ColumnValue from AbstractSql
42+
* @psalm-import-type SqlParameterValue from AbstractSql
4343
* @psalm-import-type PredicateValue from Predicate\PredicateSet
4444
* @psalm-import-type JoinName from Join
4545
* @psalm-type TableReference = string|TableIdentifier|array<string, string|TableIdentifier|Select>
@@ -191,6 +191,16 @@ public function __construct(array|string|TableIdentifier|null $table = null)
191191
}
192192
}
193193

194+
/**
195+
* Whether a row count is a non-negative integer, given as an int or as a string of digits
196+
*
197+
* A digit string is kept as given, so a count beyond PHP_INT_MAX is still accepted.
198+
*/
199+
private static function isNonNegativeInteger(int|string $value): bool
200+
{
201+
return is_int($value) ? $value >= 0 : 1 === preg_match('/^\d+\z/', $value);
202+
}
203+
194204
/**
195205
* Specify columns from which to select
196206
* Possible valid states:
@@ -339,8 +349,8 @@ public function join(
339349
*/
340350
public function limit(int|string $limit): static
341351
{
342-
if (! is_numeric($limit)) {
343-
throw Exception\InvalidArgumentException::forNonNumericParameter(__METHOD__, gettype($limit));
352+
if (! self::isNonNegativeInteger($limit)) {
353+
throw Exception\InvalidArgumentException::forNonNegativeIntegerParameter(__METHOD__, $limit);
344354
}
345355

346356
$this->limit = $limit;
@@ -352,8 +362,8 @@ public function limit(int|string $limit): static
352362
*/
353363
public function offset(int|string $offset): static
354364
{
355-
if (! is_numeric($offset)) {
356-
throw Exception\InvalidArgumentException::forNonNumericParameter(__METHOD__, gettype($offset));
365+
if (! self::isNonNegativeInteger($offset)) {
366+
throw Exception\InvalidArgumentException::forNonNegativeIntegerParameter(__METHOD__, $offset);
357367
}
358368

359369
$this->offset = $offset;
@@ -541,7 +551,11 @@ protected function processJoins(
541551
return $this->processJoin($this->joins, $platform, $driver, $parameterContainer);
542552
}
543553

544-
/** @return array{0: string}|null */
554+
/**
555+
* Platform overrides may return the raw limit or offset unquoted; any scalar is rendered as vsprintf's %s would.
556+
*
557+
* @return array{0: SqlParameterValue}|null
558+
*/
545559
protected function processLimit(
546560
PlatformInterface $platform,
547561
?DriverInterface $driver = null,
@@ -560,7 +574,11 @@ protected function processLimit(
560574
return [$platform->quoteValue((string) $this->limit)];
561575
}
562576

563-
/** @return array{0: string}|null */
577+
/**
578+
* Platform overrides may return the raw limit or offset unquoted; any scalar is rendered as vsprintf's %s would.
579+
*
580+
* @return array{0: SqlParameterValue}|null
581+
*/
564582
protected function processOffset(
565583
PlatformInterface $platform,
566584
?DriverInterface $driver = null,

0 commit comments

Comments
 (0)