refactor(store): unify SQLite and Postgres into dialect-based SQLStore
Extract shared SQLStore with Dialect interface for the ~5% that differs between backends (DDL, placeholders, sequence resets). - New dialect.go: Dialect interface + placeholder rewriter (? → $N) - New sqlstore.go: single implementation of all 19 Store methods - sqlite.go: reduced from 286 to 83 lines (SQLiteDialect only) - postgres.go: reduced from 266 to 78 lines (PostgresDialect only) - main.go: use NewSQLiteStore/NewPostgresStore constructors Zero CRUD logic duplication. Every future schema change written once.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package store
|
||||
|
||||
import "database/sql"
|
||||
|
||||
type Dialect interface {
|
||||
DriverName() string
|
||||
CreateTablesSQL() []string
|
||||
MigrationsSQL() []string
|
||||
BoolFalse() string
|
||||
ResetSequenceOnEmpty(db *sql.DB, table string)
|
||||
ImportWipe(tx *sql.Tx)
|
||||
ImportResetSequences(tx *sql.Tx)
|
||||
}
|
||||
|
||||
// rewritePlaceholders converts ? markers to $1, $2, etc. for Postgres.
|
||||
// For SQLite (or any dialect not needing rewrite), returns the input unchanged.
|
||||
func rewritePlaceholders(query string, dollarStyle bool) string {
|
||||
if !dollarStyle {
|
||||
return query
|
||||
}
|
||||
buf := make([]byte, 0, len(query)+32)
|
||||
n := 0
|
||||
for i := 0; i < len(query); i++ {
|
||||
if query[i] == '?' {
|
||||
n++
|
||||
buf = append(buf, '$')
|
||||
if n >= 10 {
|
||||
buf = append(buf, byte('0'+n/10))
|
||||
}
|
||||
buf = append(buf, byte('0'+n%10))
|
||||
} else {
|
||||
buf = append(buf, query[i])
|
||||
}
|
||||
}
|
||||
return string(buf)
|
||||
}
|
||||
Reference in New Issue
Block a user