From b8bd646ba740506206ae39b6834e15ad56dec0d7 Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Sat, 14 Dec 2024 17:40:13 -0800 Subject: [PATCH] update spr.postgres.schema, various fixes in spr package to work with postgres --- app/sql/{ => tables}/index/app.go | 0 app/sql/{ => tables}/index/flags.go | 0 cmd/wof-sql-index/main.go | 2 +- sql/spr/query.go | 2 +- sql/spr/spr.go | 62 ++++++++++++++++++++--------- sql/tables/spr.postgres.schema | 10 ++--- 6 files changed, 50 insertions(+), 26 deletions(-) rename app/sql/{ => tables}/index/app.go (100%) rename app/sql/{ => tables}/index/flags.go (100%) diff --git a/app/sql/index/app.go b/app/sql/tables/index/app.go similarity index 100% rename from app/sql/index/app.go rename to app/sql/tables/index/app.go diff --git a/app/sql/index/flags.go b/app/sql/tables/index/flags.go similarity index 100% rename from app/sql/index/flags.go rename to app/sql/tables/index/flags.go diff --git a/cmd/wof-sql-index/main.go b/cmd/wof-sql-index/main.go index 03c4099..597dd1d 100755 --- a/cmd/wof-sql-index/main.go +++ b/cmd/wof-sql-index/main.go @@ -6,7 +6,7 @@ import ( "context" "log" - "github.com/whosonfirst/go-whosonfirst-database/app/sql/index" + "github.com/whosonfirst/go-whosonfirst-database/app/sql/tables/index" ) func main() { diff --git a/sql/spr/query.go b/sql/spr/query.go index 6b716cb..0a9b7ae 100644 --- a/sql/spr/query.go +++ b/sql/spr/query.go @@ -4,7 +4,7 @@ import ( "context" "database/sql" "fmt" - + "github.com/aaronland/go-pagination" sql_pagination "github.com/aaronland/go-pagination-sql" "github.com/whosonfirst/go-whosonfirst-spr/v2" diff --git a/sql/spr/spr.go b/sql/spr/spr.go index db9d86a..efd049b 100644 --- a/sql/spr/spr.go +++ b/sql/spr/spr.go @@ -7,13 +7,13 @@ import ( "strconv" "strings" + database_sql "github.com/sfomuseum/go-database/sql" "github.com/sfomuseum/go-edtf" "github.com/sfomuseum/go-edtf/parser" "github.com/whosonfirst/go-whosonfirst-flags" "github.com/whosonfirst/go-whosonfirst-flags/existential" wof_spr "github.com/whosonfirst/go-whosonfirst-spr/v2" "github.com/whosonfirst/go-whosonfirst-uri" - database_sql "github.com/sfomuseum/go-database/sql" ) // Results is a struct that implements the `whosonfirst/go-whosonfirst-spr.StandardPlacesResults` @@ -21,7 +21,7 @@ import ( type Results struct { wof_spr.StandardPlacesResults `json:",omitempty"` // Places are the list of `StandardPlacesResults.StandardPlacesResults` instances contained by the struct. - Places []wof_spr.StandardPlacesResult `json:"places"` + Places []wof_spr.StandardPlacesResult `json:"places"` } // Results returns a list of `StandardPlacesResults.StandardPlacesResults` instances. @@ -184,20 +184,43 @@ func RetrieveSPR(ctx context.Context, spr_db *sql.DB, spr_table database_sql.Tab // supersedes and superseding need to be added here pending // https://github.com/whosonfirst/go-whosonfirst-sqlite-features/issues/14 - spr_q := fmt.Sprintf(`SELECT - id, parent_id, name, placetype, - inception, cessation, - country, repo, - latitude, longitude, - min_latitude, min_longitude, - max_latitude, max_longitude, - is_current, is_deprecated, is_ceased,is_superseded, is_superseding, - supersedes, superseded_by, belongsto, - is_alt, alt_label, - lastmodified - FROM %s WHERE id = ? AND alt_label = ?`, spr_table.Name()) - - row := spr_db.QueryRowContext(ctx, spr_q, args...) + db_driver := database_sql.Driver(spr_db) + + var q string + + switch db_driver { + case database_sql.POSTGRES_DRIVER: + + q = fmt.Sprintf(`SELECT + id, parent_id, name, placetype, + inception, cessation, + country, repo, + latitude, longitude, + min_latitude, min_longitude, + max_latitude, max_longitude, + is_current, is_deprecated, is_ceased,is_superseded, is_superseding, + supersedes, superseded_by, belongsto, + is_alt, alt_label, + lastmodified + FROM %s WHERE id = $1 AND alt_label = $2`, spr_table.Name()) + + default: + + q = fmt.Sprintf(`SELECT + id, parent_id, name, placetype, + inception, cessation, + country, repo, + latitude, longitude, + min_latitude, min_longitude, + max_latitude, max_longitude, + is_current, is_deprecated, is_ceased,is_superseded, is_superseding, + supersedes, superseded_by, belongsto, + is_alt, alt_label, + lastmodified + FROM %s WHERE id = ? AND alt_label = ?`, spr_table.Name()) + } + + row := spr_db.QueryRowContext(ctx, q, args...) return RetrieveSPRWithRow(ctx, row) } @@ -231,12 +254,13 @@ func retrieveSPRWithScanner(ctx context.Context, scanner interface{}) (wof_spr.S var parent_id string var name string var placetype string - var country string - var repo string var inception string var cessation string + var country string + var repo string + var latitude float64 var longitude float64 var min_latitude float64 @@ -257,7 +281,7 @@ func retrieveSPRWithScanner(ctx context.Context, scanner interface{}) (wof_spr.S var str_superseded_by string var str_belongs_to string - var is_alt int64 + var is_alt bool var alt_label string var lastmodified int64 diff --git a/sql/tables/spr.postgres.schema b/sql/tables/spr.postgres.schema index 209192d..b2862e8 100644 --- a/sql/tables/spr.postgres.schema +++ b/sql/tables/spr.postgres.schema @@ -13,11 +13,11 @@ CREATE TABLE {{ .Name }} ( min_longitude REAL, max_latitude REAL, max_longitude REAL, - is_current BIGINT, - is_deprecated BIGINT, - is_ceased BIGINT, - is_superseded BIGINT, - is_superseding BIGINT, + is_current SMALLINT, + is_deprecated SMALLINT, + is_ceased SMALLINT, + is_superseded SMALLINT, + is_superseding SMALLINT, superseded_by TEXT, supersedes TEXT, belongsto TEXT,