imposm3/database/postgis/fields.go

63 lines
1.3 KiB
Go
Raw Normal View History

2013-05-15 15:00:42 +04:00
package postgis
2013-05-15 10:15:33 +04:00
2013-05-15 13:05:02 +04:00
import (
"fmt"
)
type ColumnType interface {
Name() string
PrepareInsertSql(i int,
spec *TableSpec) string
2013-05-22 13:48:34 +04:00
GeneralizeSql(colSpec *ColumnSpec, spec *GeneralizedTableSpec) string
2013-05-15 13:05:02 +04:00
}
type simpleColumnType struct {
name string
}
func (t *simpleColumnType) Name() string {
return t.name
}
func (t *simpleColumnType) PrepareInsertSql(i int, spec *TableSpec) string {
return fmt.Sprintf("$%d", i)
}
2013-05-22 13:48:34 +04:00
func (t *simpleColumnType) GeneralizeSql(colSpec *ColumnSpec, spec *GeneralizedTableSpec) string {
return colSpec.Name
}
2013-05-15 13:05:02 +04:00
type geometryType struct {
name string
}
func (t *geometryType) Name() string {
return t.name
}
func (t *geometryType) PrepareInsertSql(i int, spec *TableSpec) string {
2013-06-21 12:33:49 +04:00
return fmt.Sprintf("$%d::Geometry",
i,
2013-05-15 13:05:02 +04:00
)
2013-05-15 10:15:33 +04:00
}
2013-05-22 13:48:34 +04:00
func (t *geometryType) GeneralizeSql(colSpec *ColumnSpec, spec *GeneralizedTableSpec) string {
return fmt.Sprintf(`ST_SimplifyPreserveTopology("%s", %f) as "%s"`,
colSpec.Name, spec.Tolerance, colSpec.Name,
)
}
2013-05-15 10:15:33 +04:00
var pgTypes map[string]ColumnType
func init() {
pgTypes = map[string]ColumnType{
2013-05-31 16:48:16 +04:00
"string": &simpleColumnType{"VARCHAR"},
"bool": &simpleColumnType{"BOOL"},
"int8": &simpleColumnType{"SMALLINT"},
"int32": &simpleColumnType{"INT"},
"int64": &simpleColumnType{"BIGINT"},
"float32": &simpleColumnType{"REAL"},
"geometry": &geometryType{"GEOMETRY"},
2013-05-15 10:15:33 +04:00
}
}