Skip to content

Fix PostgreSQL boolean handling for emulated prepares and raw SQL#7669

Merged
limingxinleo merged 5 commits intohyperf:masterfrom
binaryfire:pgsql
Jan 25, 2026
Merged

Fix PostgreSQL boolean handling for emulated prepares and raw SQL#7669
limingxinleo merged 5 commits intohyperf:masterfrom
binaryfire:pgsql

Conversation

@binaryfire
Copy link
Contributor

This PR fixes two issues with PostgreSQL boolean handling:

  1. Emulated prepares bug: When PDO::ATTR_EMULATE_PREPARES is enabled, boolean values cause errors
  2. Raw SQL bug: Methods like toRawSql() output 1/0 instead of true/false

Problem

When using PostgreSQL with PDO::ATTR_EMULATE_PREPARES => true, queries with boolean columns fail:

  SQLSTATE[42804]: Datatype mismatch: 7 ERROR:
  column "is_active" is of type boolean but expression is of type integer

This happens because:

  • Base Connection::prepareBindings() converts booleans to 1/0
  • PostgreSQL requires true/false literals, not integers
  • With emulated prepares, PDO embeds values directly into SQL string

This is a known Laravel bug that affects Hyperf too.

Solution

PostgreSqlConnection.php:

  • Override prepareBindings() to convert booleans to 'true'/'false' when emulated prepares enabled
  • Override escapeBool() to return 'true'/'false' for raw SQL methods

PostgreSqlSwooleExtConnection.php:

  • Override escapeBool() to return 'true'/'false' for raw SQL methods

References:

Laravel's PostgresConnection has the same escapeBool() fix:
https://github.com/laravel/framework/blob/12.x/src/Illuminate/Database/PostgresConnection.php#L42-L45

Tests Added

New file: PostgreSqlConnectionTest.php

  • testPrepareBindingsConvertsBooleanToStringWhenEmulatedPreparesEnabled
  • testPrepareBindingsConvertsBooleanToIntegerWhenEmulatedPreparesDisabled
  • testEscapeBoolReturnsPostgresLiterals

Added to: PostgreSqlSwooleExtConnectionTest.php

  • testEscapeBoolReturnsPostgresLiterals

@limingxinleo limingxinleo merged commit 45d6c52 into hyperf:master Jan 25, 2026
77 checks passed
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.

2 participants