imposm3/database/database.go

116 lines
3.0 KiB
Go
Raw Normal View History

2013-05-15 15:00:42 +04:00
package database
import (
"errors"
"strings"
2014-08-04 17:19:35 +04:00
"github.com/omniscale/imposm3/element"
"github.com/omniscale/imposm3/geom"
2014-08-04 17:19:35 +04:00
"github.com/omniscale/imposm3/mapping"
"github.com/omniscale/imposm3/mapping/config"
2013-05-15 15:00:42 +04:00
)
type Config struct {
ConnectionParams string
Srid int
ImportSchema string
ProductionSchema string
BackupSchema string
2013-05-15 15:00:42 +04:00
}
type DB interface {
Begin() error
End() error
Abort() error
Init() error
Close() error
2013-10-28 11:26:51 +04:00
Inserter
}
2013-06-21 12:33:49 +04:00
type BulkBeginner interface {
BeginBulk() error
}
2013-10-28 11:26:51 +04:00
type Inserter interface {
2013-10-29 19:32:16 +04:00
// InsertXxx inserts element of that type into the database.
// element.Geom is set to that type.
InsertPoint(element.OSMElem, geom.Geometry, []mapping.Match) error
InsertLineString(element.OSMElem, geom.Geometry, []mapping.Match) error
InsertPolygon(element.OSMElem, geom.Geometry, []mapping.Match) error
2016-01-04 13:19:28 +03:00
InsertRelationMember(element.Relation, element.Member, geom.Geometry, []mapping.Match) error
2013-05-15 15:00:42 +04:00
}
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
2013-05-22 13:48:34 +04:00
type Generalizer interface {
Generalize() error
EnableGeneralizeUpdates()
GeneralizeUpdates() error
2013-05-22 13:48:34 +04:00
}
2013-05-22 11:49:03 +04:00
type Finisher interface {
Finish() error
}
2013-06-11 12:29:21 +04:00
type Deleter interface {
Inserter
2017-05-16 17:30:00 +03:00
Delete(int64, []mapping.Match) error
2013-06-11 12:29:21 +04:00
}
2013-07-04 13:26:53 +04:00
type Optimizer interface {
Optimize() error
}
var databases map[string]func(Config, *config.Mapping) (DB, error)
func init() {
databases = make(map[string]func(Config, *config.Mapping) (DB, error))
}
func Register(name string, f func(Config, *config.Mapping) (DB, error)) {
2013-05-15 15:00:42 +04:00
databases[name] = f
}
func Open(conf Config, m *config.Mapping) (DB, error) {
2013-10-29 19:32:16 +04:00
parts := strings.SplitN(conf.ConnectionParams, ":", 2)
connectionType := parts[0]
newFunc, ok := databases[connectionType]
2013-05-15 15:00:42 +04:00
if !ok {
2013-10-29 19:32:16 +04:00
return nil, errors.New("unsupported database type: " + connectionType)
2013-05-15 15:00:42 +04:00
}
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
2013-10-29 19:32:16 +04:00
// nullDb is a dummy database that imports into /dev/null
type nullDb struct{}
func (n *nullDb) Init() error { return nil }
func (n *nullDb) Begin() error { return nil }
func (n *nullDb) End() error { return nil }
func (n *nullDb) Close() error { return nil }
func (n *nullDb) Abort() error { return nil }
func (n *nullDb) InsertPoint(element.OSMElem, geom.Geometry, []mapping.Match) error { return nil }
func (n *nullDb) InsertLineString(element.OSMElem, geom.Geometry, []mapping.Match) error { return nil }
func (n *nullDb) InsertPolygon(element.OSMElem, geom.Geometry, []mapping.Match) error { return nil }
2016-01-04 13:19:28 +03:00
func (n *nullDb) InsertRelationMember(element.Relation, element.Member, geom.Geometry, []mapping.Match) error {
return nil
}
2013-10-29 19:32:16 +04:00
func newNullDb(conf Config, m *config.Mapping) (DB, error) {
2013-10-29 19:32:16 +04:00
return &nullDb{}, nil
2013-05-21 14:14:05 +04:00
}
func init() {
2013-10-29 19:32:16 +04:00
Register("null", newNullDb)
2013-05-21 14:14:05 +04:00
}