fix(backtest): resolve PostgreSQL placeholder syntax error in storage queries #1313
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request - Backend | 后端 PR
📝 Description | 描述
English:
Fixed PostgreSQL syntax error when starting backtest runs. The issue was caused by using
?placeholders in raw SQL queries, which are incompatible with PostgreSQL drivers that require$1, $2...format.中文:
修复了启动回测运行时的 PostgreSQL 语法错误。问题是由于在原生 SQL 查询中使用了
?占位符,而 PostgreSQL 驱动需要$1, $2...格式的占位符。🎯 Type of Change | 变更类型
🔗 Related Issues | 相关 Issue
Error message:
Error occurs when:
📋 Changes Made | 具体变更
English:
convertQuery()function to convert?to$Nplaceholders for PostgreSQLSetDatabaseType()to detect and store database typebacktest/storage_db_impl.goto useconvertQuery()中文:
convertQuery()函数,将?转换为 PostgreSQL 的$N占位符SetDatabaseType()检测并存储数据库类型backtest/storage_db_impl.go中的所有原生 SQL 查询以使用convertQuery()Files changed:
backtest/persistence_db.go: Added query conversion logicbacktest/storage_db_impl.go: Wrapped all SQL queries withconvertQuery()main.go: AddedSetDatabaseType()call during initializationTechnical details:
$1, $2, $3...placeholders?placeholdersconvertQuery()function detects database type and applies appropriate conversion🧪 Testing | 测试
Test Environment | 测试环境
Manual Testing | 手动测试
Test Results | 测试结果
Before fix:
After fix:
Backtest queries should execute successfully with proper placeholder conversion.
🔒 Security Considerations | 安全考虑
Security note:
The fix uses parameterized queries with proper placeholder conversion, maintaining the same SQL injection protection as before.
⚡ Performance Impact | 性能影响
If impacted, explain | 如果受影响,请说明:
The
convertQuery()function adds minimal string processing overhead only for PostgreSQL. The conversion happens once per query (not per execution), so the impact is negligible.✅ Checklist | 检查清单
Code Quality | 代码质量
go build)go fmt| 已运行go fmtDocumentation | 文档
Git
devbranch | 已 rebase 到最新dev分支📚 Additional Notes | 补充说明
English:
This fix resolves a critical issue preventing backtest runs on PostgreSQL 17. The solution is backward compatible with SQLite and maintains the same security guarantees. All SQL queries in
storage_db_impl.go(25+ queries) have been updated to use the conversion function.中文:
此修复解决了阻止在 PostgreSQL 17 上运行回测的关键问题。该解决方案向后兼容 SQLite,并保持相同的安全保证。
storage_db_impl.go中的所有 SQL 查询(25+ 个查询)都已更新为使用转换函数。Database compatibility:
Related commits:
efcdd788- Main fix: PostgreSQL placeholder conversion30c9aadd- Code formatting withgo fmtBy submitting this PR, I confirm | 提交此 PR,我确认:
🌟 Thank you for your contribution! | 感谢你的贡献!