Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@MomentDerek
Copy link
Author

@gqcn 目前有人可以审核pr么

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