Skip to content

Add bUInt() and bUIntArray() for unsigned integers - #19

Merged
ellisgl merged 1 commit into
mainfrom
unsigned-int
Nov 25, 2026
Merged

ellisgl merged 1 commit into
mainfrom
unsigned-int

Conversation

@ellisgl

@ellisgl ellisgl commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Summary

There was no way to bind unsigned integers. bInt() accepts negative numbers and stops at PHP's integer maximum, 9223372036854775807 (2⁶³−1), so the top half of MySQL's BIGINT UNSIGNED couldn't be bound.

  • New Statement::bUInt($value, $null = false) and bUIntArray(array $values)
    • Range is 0 to 18446744073709551615 (2⁶⁴−1). Values above PHP_INT_MAX are passed as integer strings.
    • Negative, non-numeric and out-of-range values throw InvalidArgumentException. Floats are truncated toward zero like bInt(), but floats at 2⁶³ or above are rejected because a float can't hold them exactly.
    • bUIntArray() validates each value instead of casting it the way bIntArray() does. It removes duplicates.
  • Uses %%, not ?. A value above PHP_INT_MAX can only be bound as a string. With PDO's default emulated prepares, MySQL compares a string with a number as a float in expressions and IN (...) lists, so 18446744073709551615 matched 18446744073709551614 in testing. Putting validated digits into the SQL text, as bIntArray() and bFloat() already do, is exact. Only digits pass validation, so this is safe for user input.
  • No break for custom bindings. The methods are in a new UnsignedNumericBindingInterface that extends NumericBindingInterface, and MySQLNumericBindings implements it. If custom numeric bindings only implement the old interface, Bindings falls back to the MySQL versions.
  • PostgreSQL has no unsigned types. There, bUInt() checks that a value isn't negative, and values above 2⁶³−1 need a numeric column.

Testing

  • Unit tests in MySQLNumericBindingsTest cover valid and invalid values, arrays, getComputed() output and the fallback for custom numeric bindings.
  • GLPDO2MySQLTest::testUnsignedBigint uses a real BIGINT UNSIGNED column. It checks exact storage and that neighbouring values don't match in id + 0 = %% and IN (%%).
  • GLPDO2PostgreSQLTest::testUnsignedValuesAboveBigint uses a numeric(20) column.
  • Full suite passed locally against MySQL 8.4 and PostgreSQL 17 on PHP 8.5 (185 tests). PHPStan level 8 and PSR-12 are clean.

🤖 Generated with Claude Code

- Bind 0 to 18446744073709551615 (BIGINT UNSIGNED). Negative,
  non-numeric and out-of-range values throw; values above PHP_INT_MAX
  are accepted as integer strings.
- Both use '%%' and put validated digits into the SQL. A string
  parameter is compared as a float in MySQL expressions and IN() lists,
  so large neighbouring values would match.
- New UnsignedNumericBindingInterface extends NumericBindingInterface;
  numeric bindings that don't implement it fall back to the MySQL ones.
- Unit tests, plus MySQL (BIGINT UNSIGNED) and PostgreSQL (numeric(20))
  integration tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ellisgl
ellisgl merged commit 8642738 into main Nov 25, 2026
5 checks passed
@ellisgl
ellisgl deleted the unsigned-int branch November 25, 2026 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant