db.go 718 B

1234567891011121314151617181920212223242526272829303132
  1. // Copyright (C) 2014-2015 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. package main
  3. import (
  4. "database/sql"
  5. "fmt"
  6. )
  7. type setupFunc func(db *sql.DB) error
  8. type compileFunc func(db *sql.DB) (map[string]*sql.Stmt, error)
  9. var (
  10. setupFuncs = make(map[string]setupFunc)
  11. compileFuncs = make(map[string]compileFunc)
  12. )
  13. func register(name string, setup setupFunc, compile compileFunc) {
  14. setupFuncs[name] = setup
  15. compileFuncs[name] = compile
  16. }
  17. func setup(backend string, db *sql.DB) (map[string]*sql.Stmt, error) {
  18. setup, ok := setupFuncs[backend]
  19. if !ok {
  20. return nil, fmt.Errorf("Unsupported backend")
  21. }
  22. if err := setup(db); err != nil {
  23. return nil, err
  24. }
  25. return compileFuncs[backend](db)
  26. }