Skip to content

Enable bound parameters for PostgreSQL - #774

Open
josetduarte wants to merge 1 commit into
web2py:masterfrom
josetduarte:feat/postgres-parameters
Open

Enable bound parameters for PostgreSQL#774
josetduarte wants to merge 1 commit into
web2py:masterfrom
josetduarte:feat/postgres-parameters

Conversation

@josetduarte

Copy link
Copy Markdown
Contributor

This pull request introduces significant improvements to parameterized SQL compilation and execution, especially for PostgreSQL support in pyDAL. The changes ensure that the PostgreSQL backend and its compiler use the correct placeholder style (%s), safely handle percent signs in values, and support parameterized queries across all statement types. Extensive tests are added to verify parameter binding and round-trip execution with real PostgreSQL databases.

PostgreSQL Parameterization Enhancements:

  • The PostgresCompiler now defaults to parameterized queries with the "format" placeholder style (%s), and appends RETURNING id for inserts when appropriate. This ensures compatibility with PostgreSQL drivers and enables safe query execution.
  • The _insert method in Postgres is simplified to rely on the superclass implementation, leveraging the improved compiler logic.

Core SQL Compiler Improvements:

  • The SQL compiler now uses unique placeholder tokens internally (e.g., \x00pydal_param_1\x00) and replaces them with %s at the end of compilation, ensuring percent signs in data do not interfere with placeholders. All statement entry points (compile_select, compile_insert, compile_update, compile_delete, compile_count) now consistently return ParamSQL objects with parameters when appropriate. [1] [2] [3] [4] [5] [6] [7] [8]
  • String, text, and password fields are now explicitly coerced to strings when adapting values for binding, improving type safety for parameterized queries.

Testing and Validation:

  • The parameterization tests in tests/ast_params.py are expanded to cover all statement types, edge cases with percent signs, type coercion, and round-trip execution against a live PostgreSQL database. This includes a new test class for real PostgreSQL round-trips, ensuring correctness and robustness of the parameterization logic. [1] [2]
  • Additional test fields and scenarios are added, such as lists, booleans, and dates, to verify handling of diverse data types.

Test and Documentation Updates:

  • Test descriptions and documentation are updated to clarify the default parameterization behavior and backend-specific support. [1] [2]

These changes collectively make parameterized SQL generation safer, more reliable, and fully compatible with PostgreSQL, while maintaining backward compatibility and improving test coverage.

@josetduarte
josetduarte force-pushed the feat/postgres-parameters branch 2 times, most recently from e75ec41 to bbd2910 Compare July 21, 2026 14:41
@josetduarte
josetduarte force-pushed the feat/postgres-parameters branch from bbd2910 to 2f43eec Compare July 21, 2026 15:13
@leonelcamara

Copy link
Copy Markdown
Contributor

@mdipierro this looks very good.

@josetduarte when/if this is merged a nice follow up would be to add the json and geo functions, so postgres can be completely modernized to the new way of doing things in pydal.

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