Skip to content

feat(database/gdb): support PostgreSQL's returning syntax #4292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

MomentDerek
Copy link

feat(database/gdb): support PostgreSQL's returning syntax

This PR adds comprehensive support for PostgreSQL's RETURNING syntax to the GoFrame database module, allowing users to retrieve data from INSERT, UPDATE, and DELETE operations.

Changes

Core Features

  • RETURNING clause support: Added Returning(), ReturningAll(), and ReturningExcept() methods to the Model
  • PostgreSQL driver enhancement: Modified pgsql driver to handle RETURNING clauses properly
  • Result handling: Enhanced result processing to capture and return data from RETURNING operations

Key Components

  • database/gdb/gdb_model_returning.go: New file containing RETURNING-related methods
  • contrib/drivers/pgsql/pgsql_do_exec.go: Enhanced to support custom RETURNING clauses
  • contrib/drivers/pgsql/pgsql_result.go: Updated result handling for RETURNING data
  • Comprehensive test coverage with 247+ new test cases

API Usage Examples

// Return specific fields
result, err := db.Model("users").Data(data).Returning("id", "name").Insert()

// Return all fields
result, err := db.Model("users").Data(data).ReturningAll().Insert()

// Return all fields except specified ones
result, err := db.Model("users").Data(data).ReturningExcept("password", "secret").Update()

Benefits

  • Eliminates need for separate SELECT queries after INSERT/UPDATE/DELETE
  • Improves performance by reducing database round trips
  • Provides atomic operations with immediate result feedback
  • Maintains compatibility with existing code

Testing

  • Added comprehensive unit tests in pgsql_z_unit_returning_test.go
  • Added internal tests in gdb_z_pgsql_internal_test.go
  • All existing tests continue to pass

Feat #4291

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