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(*mapping.Mapping) error
|
|
|
|
InsertBatch(string, [][]interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var databases map[string]func(Config) (DB, error)
|
|
|
|
|
|
|
|
func Register(name string, f func(Config) (DB, error)) {
|
|
|
|
if databases == nil {
|
|
|
|
databases = make(map[string]func(Config) (DB, error))
|
|
|
|
}
|
|
|
|
databases[name] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
func Open(conf Config) (DB, error) {
|
|
|
|
newFunc, ok := databases[conf.Type]
|
|
|
|
if !ok {
|
|
|
|
panic("unsupported database type: " + conf.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := newFunc(conf)
|
|
|
|
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(*mapping.Mapping) error { return nil }
|
|
|
|
func (n *NullDb) InsertBatch(string, [][]interface{}) error { return nil }
|
|
|
|
|
|
|
|
func NewNullDb(conf Config) (DB, error) {
|
|
|
|
return &NullDb{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("null", NewNullDb)
|
|
|
|
}
|