imposm3/database/database.go

65 lines
1.2 KiB
Go
Raw Normal View History

2013-05-15 15:00:42 +04:00
package database
import (
"goposm/mapping"
2013-05-21 14:14:05 +04:00
"strings"
2013-05-15 15:00:42 +04:00
)
type Config struct {
Type string
ConnectionParams string
Srid int
}
type DB interface {
Init() error
2013-05-15 15:00:42 +04:00
InsertBatch(string, [][]interface{}) error
}
type Deployer interface {
2013-05-22 10:46:39 +04:00
Deploy() error
RevertDeploy() error
2013-05-22 10:46:39 +04:00
RemoveBackup() error
}
2013-05-15 15:00:42 +04:00
var databases map[string]func(Config, *mapping.Mapping) (DB, error)
func init() {
databases = make(map[string]func(Config, *mapping.Mapping) (DB, error))
}
func Register(name string, f func(Config, *mapping.Mapping) (DB, error)) {
2013-05-15 15:00:42 +04:00
databases[name] = f
}
func Open(conf Config, m *mapping.Mapping) (DB, error) {
2013-05-15 15:00:42 +04:00
newFunc, ok := databases[conf.Type]
if !ok {
panic("unsupported database type: " + conf.Type)
}
db, err := newFunc(conf, m)
2013-05-15 15:00:42 +04:00
if err != nil {
return nil, err
}
return db, nil
}
2013-05-21 14:14:05 +04:00
func ConnectionType(param string) string {
parts := strings.SplitN(param, ":", 2)
return parts[0]
}
type NullDb struct{}
func (n *NullDb) Init() error { return nil }
2013-05-21 14:14:05 +04:00
func (n *NullDb) InsertBatch(string, [][]interface{}) error { return nil }
func NewNullDb(conf Config, m *mapping.Mapping) (DB, error) {
2013-05-21 14:14:05 +04:00
return &NullDb{}, nil
}
func init() {
Register("null", NewNullDb)
}