syncthing/cmd/discosrv/db.go

33 lines
718 B
Go
Raw Normal View History

2015-03-25 07:16:52 +00:00
// Copyright (C) 2014-2015 Jakob Borg and Contributors (see the CONTRIBUTORS file).
package main
2015-07-21 22:56:27 +00:00
import (
"database/sql"
"fmt"
)
type setupFunc func(db *sql.DB) error
type compileFunc func(db *sql.DB) (map[string]*sql.Stmt, error)
var (
setupFuncs = make(map[string]setupFunc)
compileFuncs = make(map[string]compileFunc)
)
func register(name string, setup setupFunc, compile compileFunc) {
setupFuncs[name] = setup
compileFuncs[name] = compile
2015-03-25 07:16:52 +00:00
}
2015-07-21 22:56:27 +00:00
func setup(backend string, db *sql.DB) (map[string]*sql.Stmt, error) {
setup, ok := setupFuncs[backend]
if !ok {
return nil, fmt.Errorf("Unsupported backend")
2015-03-25 07:16:52 +00:00
}
2015-07-21 22:56:27 +00:00
if err := setup(db); err != nil {
return nil, err
2015-03-25 07:16:52 +00:00
}
2015-07-21 22:56:27 +00:00
return compileFuncs[backend](db)
2015-03-25 07:16:52 +00:00
}