refactored database package structure
parent
0d2be27981
commit
32373dec32
|
@ -0,0 +1,38 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"goposm/mapping"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package db
|
||||
package postgis
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -1,27 +1,16 @@
|
|||
package db
|
||||
package postgis
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/bmizerany/pq"
|
||||
"goposm/database"
|
||||
"goposm/mapping"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Type string
|
||||
ConnectionParams string
|
||||
Srid int
|
||||
Schema string
|
||||
}
|
||||
|
||||
type DB interface {
|
||||
Init(*mapping.Mapping) error
|
||||
InsertBatch(string, [][]interface{}) error
|
||||
}
|
||||
|
||||
type ColumnSpec struct {
|
||||
Name string
|
||||
Type ColumnType
|
||||
|
@ -78,7 +67,7 @@ func (spec *TableSpec) InsertSQL() string {
|
|||
)
|
||||
}
|
||||
|
||||
func NewTableSpec(conf *Config, t *mapping.Table, schema string) *TableSpec {
|
||||
func NewTableSpec(conf *database.Config, t *mapping.Table, schema string) *TableSpec {
|
||||
spec := TableSpec{
|
||||
Name: t.Name,
|
||||
Schema: schema,
|
||||
|
@ -145,12 +134,12 @@ func (pg *PostGIS) createSchema() error {
|
|||
var sql string
|
||||
var err error
|
||||
|
||||
if pg.Config.Schema == "public" {
|
||||
if pg.Schema == "public" {
|
||||
return nil
|
||||
}
|
||||
|
||||
sql = fmt.Sprintf("SELECT EXISTS(SELECT schema_name FROM information_schema.schemata WHERE schema_name = '%s');",
|
||||
pg.Config.Schema)
|
||||
pg.Schema)
|
||||
row := pg.Db.QueryRow(sql)
|
||||
var exists bool
|
||||
err = row.Scan(&exists)
|
||||
|
@ -161,7 +150,7 @@ func (pg *PostGIS) createSchema() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
sql = fmt.Sprintf("CREATE SCHEMA \"%s\"", pg.Config.Schema)
|
||||
sql = fmt.Sprintf("CREATE SCHEMA \"%s\"", pg.Schema)
|
||||
_, err = pg.Db.Exec(sql)
|
||||
if err != nil {
|
||||
return &SQLError{sql, err}
|
||||
|
@ -172,7 +161,7 @@ func (pg *PostGIS) createSchema() error {
|
|||
type PostGIS struct {
|
||||
Db *sql.DB
|
||||
Schema string
|
||||
Config Config
|
||||
Config database.Config
|
||||
Tables map[string]*TableSpec
|
||||
}
|
||||
|
||||
|
@ -271,10 +260,7 @@ func (pg *PostGIS) Init(m *mapping.Mapping) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func Open(conf Config) (DB, error) {
|
||||
if conf.Type != "postgres" {
|
||||
panic("unsupported database type: " + conf.Type)
|
||||
}
|
||||
func New(conf database.Config) (database.DB, error) {
|
||||
db := &PostGIS{}
|
||||
db.Tables = make(map[string]*TableSpec)
|
||||
db.Config = conf
|
||||
|
@ -284,3 +270,8 @@ func Open(conf Config) (DB, error) {
|
|||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
database.Register("postgres", New)
|
||||
database.Register("postgis", New)
|
||||
}
|
10
goposm.go
10
goposm.go
|
@ -3,7 +3,8 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"goposm/cache"
|
||||
"goposm/db"
|
||||
"goposm/database"
|
||||
_ "goposm/database/postgis"
|
||||
"goposm/element"
|
||||
"goposm/geom"
|
||||
"goposm/geom/geos"
|
||||
|
@ -249,13 +250,12 @@ func main() {
|
|||
waitFill := sync.WaitGroup{}
|
||||
wayChan := make(chan []element.Way)
|
||||
waitDb := &sync.WaitGroup{}
|
||||
conf := db.Config{
|
||||
Type: "postgres",
|
||||
conf := database.Config{
|
||||
Type: "postgis",
|
||||
ConnectionParams: *connection,
|
||||
Srid: 3857,
|
||||
Schema: "public",
|
||||
}
|
||||
pg, err := db.Open(conf)
|
||||
pg, err := database.Open(conf)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package writer
|
||||
|
||||
import (
|
||||
"goposm/db"
|
||||
"goposm/database"
|
||||
"log"
|
||||
)
|
||||
|
||||
const batchSize = 1024
|
||||
|
||||
func DBWriter(db db.DB, in chan InsertBatch) {
|
||||
func DBWriter(db database.DB, in chan InsertBatch) {
|
||||
for batch := range in {
|
||||
err := db.InsertBatch(batch.Table, batch.Rows)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue