ab75f61c6b
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.
37 lines
828 B
Go
37 lines
828 B
Go
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)
|
|
}
|