simple-sql/simplesql.go

69 lines
1.6 KiB
Go
Raw Normal View History

2020-07-14 21:59:39 +00:00
package simplesql
import (
"context"
2022-01-03 03:46:54 +00:00
"embed"
2020-07-14 21:59:39 +00:00
"fmt"
2022-01-03 03:46:54 +00:00
"path/filepath"
"strings"
2020-07-14 21:59:39 +00:00
2022-01-03 03:46:54 +00:00
"github.com/jmoiron/sqlx"
)
2020-07-14 21:59:39 +00:00
2022-01-03 03:46:54 +00:00
// Stmts is a map of named statements
type Stmts map[string]*sqlx.NamedStmt
2020-07-14 21:59:39 +00:00
// Close will close all prepared statements.
2022-01-03 03:46:54 +00:00
// This should be called after all statements are no longer needed.
2020-07-14 21:59:39 +00:00
func (s *Stmts) Close() {
for _, stmt := range *s {
_ = stmt.Close()
}
}
2022-01-03 03:46:54 +00:00
// Prepare will create a Stmts from an embed.FS. The file name is the key.
func Prepare(ctx context.Context, db *sqlx.DB, queries embed.FS) (Stmts, error) {
// Read entries from sqlq dir
entries, err := queries.ReadDir("sqlq")
if err != nil {
return nil, fmt.Errorf("reading sqlq dir: %w", err)
}
// Get file content per entry and map to file name as prepared statement
2020-07-14 22:09:12 +00:00
s := Stmts{}
2022-01-03 03:46:54 +00:00
for _, entry := range entries {
// Get file content
fp := filepath.Join("sqlq", entry.Name())
qb, err := queries.ReadFile(fp)
if err != nil {
return nil, fmt.Errorf("reading file %q from sqlq: %w", fp, err)
}
2020-07-14 21:59:39 +00:00
2022-01-03 03:46:54 +00:00
// Cleanse input query
qs := cleanseQuery(string(qb))
// Prepare statement
stmt, err := db.PrepareNamedContext(ctx, qs)
2020-07-14 21:59:39 +00:00
if err != nil {
2022-01-03 03:46:54 +00:00
return nil, fmt.Errorf("preparing statement %q: %w", qs, err)
2020-07-14 21:59:39 +00:00
}
2022-01-03 03:46:54 +00:00
// Map to file name, without the extension, which is assumed to be .sql
s[strings.TrimSuffix(entry.Name(), ".sql")] = stmt
2020-07-14 21:59:39 +00:00
}
return s, nil
}
2022-01-03 03:46:54 +00:00
func cleanseQuery(s string) string {
var cleansed []string
for _, line := range strings.Split(s, "\n") {
line := strings.TrimSpace(line)
if !strings.HasPrefix(line, "--") {
cleansed = append(cleansed, line)
}
}
return strings.Join(cleansed, "\n")
}