Skip to content

Conversation

@tomtwinkle
Copy link
Owner

Overview

Fixed support for PostgreSQL's RETURNING clause in bulk insert queries. The enhancement ensures that bulk insert query generation correctly preserves the RETURNING clause when transforming single-row insert statements into multi-row batch inserts.

Changes

  • Modified templates/template.go: Added logic to detect and preserve the RETURNING keyword as a suffix clause (similar to existing handling for ON DUPLICATE KEY UPDATE and ON CONFLICT)
  • Added templates/template_test.go: Introduced 4 new test cases covering:
    • Basic RETURNING clause functionality
    • RETURNING clause combined with ON CONFLICT (PostgreSQL upsert pattern)
    • Additional test coverage for ON DUPLICATE KEY UPDATE (MySQL upsert) including case-insensitive handling

Impact

  • PostgreSQL compatibility: Users can now generate bulk insert queries that return inserted row data (e.g., auto-generated IDs)
  • Database feature parity: Brings RETURNING clause support in line with existing upsert clause handling
  • No breaking changes: The modification extends existing suffix detection logic without affecting current functionality
  • Improved test coverage: 52 new lines of test code ensure the feature works correctly in isolation and combined with other SQL clauses

- Add RETURNING clause detection to suffix boundary logic
- Preserve RETURNING clause when building bulk insert queries
- Add test cases for RETURNING clause with and without ON CONFLICT
- Add additional test cases for ON DUPLICATE KEY UPDATE with case variations
- Ensures RETURNING clause is properly handled similar to ON DUPLICATE KEY UPDATE and ON CONFLICT
Copilot AI review requested due to automatic review settings November 12, 2025 04:53
@tomtwinkle tomtwinkle changed the title Fix template suffix logic 2 Add support for RETURNING clause in bulk insert queries Nov 12, 2025
@tomtwinkle tomtwinkle self-assigned this Nov 12, 2025
@tomtwinkle tomtwinkle force-pushed the fix-template-suffix-logic-2 branch from 63bf771 to b047593 Compare November 12, 2025 04:54
Copilot finished reviewing on behalf of tomtwinkle November 12, 2025 04:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for PostgreSQL's RETURNING clause in bulk insert queries by extending the suffix detection logic to recognize and preserve RETURNING alongside existing upsert clauses (ON DUPLICATE KEY UPDATE and ON CONFLICT).

Key changes:

  • Enhanced suffix detection in buildBulkInsertQuery to identify the earliest occurrence of suffix keywords (ON DUPLICATE KEY UPDATE, ON CONFLICT, RETURNING)
  • Refactored query parsing to extract suffixes before searching for the VALUES clause, preventing false matches within suffix functions
  • Added comprehensive test coverage for RETURNING clause (standalone and combined with ON CONFLICT), plus additional upsert test cases

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
templates/template.go Refactored suffix detection logic to support RETURNING clause alongside existing upsert keywords; simplified iteration syntax
templates/template_test.go Added 8 new test cases covering RETURNING clause, upsert patterns, and case-insensitive handling
Comments suppressed due to low confidence (1)

templates/template.go:103

  • The queryPrefixStr should use queryWithoutSuffix instead of trimmedQuery to avoid including the suffix in the prefix. If a suffix exists, the current code will include it in both the prefix and suffix parts of the final query.
	queryPrefixStr := strings.TrimSpace(trimmedQuery[:valuesUpperIndex]) + " VALUES "

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tomtwinkle tomtwinkle merged commit 1937d8e into main Nov 12, 2025
1 check passed
@tomtwinkle tomtwinkle deleted the fix-template-suffix-logic-2 branch November 12, 2025 04:55
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