From 9a13be1b07f5811030dae544da7f1abb6ed07481 Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Sun, 13 Jun 2021 16:07:36 -0700 Subject: [PATCH 1/5] add RetrieveSPRWithRow; update vendor deps --- go.mod | 4 ++-- go.sum | 4 ++++ spr.go | 14 +++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 36d539b..dc3f68f 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ go 1.16 require ( github.com/sfomuseum/go-edtf v0.2.3 - github.com/whosonfirst/go-whosonfirst-flags v0.4.2 + github.com/whosonfirst/go-whosonfirst-flags v0.4.3 github.com/whosonfirst/go-whosonfirst-spr/v2 v2.0.0 github.com/whosonfirst/go-whosonfirst-sqlite v0.1.7 - github.com/whosonfirst/go-whosonfirst-uri v0.2.0 + github.com/whosonfirst/go-whosonfirst-uri v1.0.1 ) diff --git a/go.sum b/go.sum index 86426e8..ee53284 100644 --- a/go.sum +++ b/go.sum @@ -29,6 +29,8 @@ github.com/whosonfirst/go-whosonfirst-flags v0.4.0 h1:3qEz1v7rALk+TqVstW9DKQOWrM github.com/whosonfirst/go-whosonfirst-flags v0.4.0/go.mod h1:kewFjxBiE00SqjjIanm5DPI81SYvx93wVb3ogwV/PMk= github.com/whosonfirst/go-whosonfirst-flags v0.4.2 h1:HWjy/0MfAQMdCj4M9hi3LAITgK/D+cuDWGHP37mFeZo= github.com/whosonfirst/go-whosonfirst-flags v0.4.2/go.mod h1:kewFjxBiE00SqjjIanm5DPI81SYvx93wVb3ogwV/PMk= +github.com/whosonfirst/go-whosonfirst-flags v0.4.3 h1:ef6IkgvYADL4kc750sl6i5hkReNq0Z6upLcqpK2CHLY= +github.com/whosonfirst/go-whosonfirst-flags v0.4.3/go.mod h1:pL17Ryo60FH8RYaQRgfu5XnxhrNRK3x+rn03TYD6Gc8= github.com/whosonfirst/go-whosonfirst-placetypes v0.1.0/go.mod h1:Jdmug2QQLbrmg+UcYGz8k575GnrOEg63vZVS46e5fMs= github.com/whosonfirst/go-whosonfirst-placetypes v0.2.4/go.mod h1:yl0zZ5tfK80C0kl34pJcPB3mZC5XXR7ybQJ5OJyEcDU= github.com/whosonfirst/go-whosonfirst-placetypes v0.3.0/go.mod h1:ez0VFkGFbgT2/z2oi3PIuW6FewsZ2+5glyfDD79XEHk= @@ -44,6 +46,8 @@ github.com/whosonfirst/go-whosonfirst-sqlite v0.1.7 h1:WZOGRgD2UmQWYOITWNpTWcccd github.com/whosonfirst/go-whosonfirst-sqlite v0.1.7/go.mod h1:Vz7VscOjc7oS99GFGLJMyQj++nWuAQ/F/dCFzEzyYg0= github.com/whosonfirst/go-whosonfirst-uri v0.2.0 h1:iODHdyvW+8IXqHZTixZ/9GEZy1dVKGj6dMRg7fn0d2M= github.com/whosonfirst/go-whosonfirst-uri v0.2.0/go.mod h1:8eaDVcc4v+HHHEDaRbApdmhPwM4/JQllw2PktvZcPVs= +github.com/whosonfirst/go-whosonfirst-uri v1.0.1 h1:hVEDRuW9WhqvTksDi092OO9UecX7PAMDrD47KPEqAg0= +github.com/whosonfirst/go-whosonfirst-uri v1.0.1/go.mod h1:8eaDVcc4v+HHHEDaRbApdmhPwM4/JQllw2PktvZcPVs= github.com/whosonfirst/warning v0.1.0/go.mod h1:cAez7FpC/UEUrbiOXZO15v2JM8eijtFHQlN93AGFy1k= github.com/whosonfirst/warning v0.1.1/go.mod h1:/unEMzhB9YaMeEwTJpzLN3kM5LiSxdJhKEsf/OQhn6s= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/spr.go b/spr.go index 5e7907f..02ba3ea 100644 --- a/spr.go +++ b/spr.go @@ -2,6 +2,7 @@ package spr import ( "context" + "database/sql" "fmt" "github.com/sfomuseum/go-edtf" "github.com/sfomuseum/go-edtf/parser" @@ -198,6 +199,11 @@ func RetrieveSPR(ctx context.Context, spr_db *wof_database.SQLiteDatabase, spr_t row := conn.QueryRowContext(ctx, spr_q, args...) + return RetrieveSPRWithRow(ctx, row) +} + +func RetrieveSPRWithRow(ctx context.Context, row *sql.Row) (wof_spr.StandardPlacesResult, error) { + var spr_id string var parent_id string var name string @@ -233,7 +239,7 @@ func RetrieveSPR(ctx context.Context, spr_db *wof_database.SQLiteDatabase, spr_t // supersedes and superseding need to be added here pending // https://github.com/whosonfirst/go-whosonfirst-sqlite-features/issues/14 - err = row.Scan( + err := row.Scan( &spr_id, &parent_id, &name, &placetype, &country, &repo, &inception, &cessation, &latitude, &longitude, &min_latitude, &max_latitude, &min_longitude, &max_longitude, @@ -246,6 +252,12 @@ func RetrieveSPR(ctx context.Context, spr_db *wof_database.SQLiteDatabase, spr_t return nil, err } + id, err := strconv.ParseInt(spr_id, 10, 64) + + if err != nil { + return nil, err + } + path, err := uri.Id2RelPath(id) if err != nil { From 7c3c6bd104aa25f706b7af63f5426267c7ab9d43 Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Sun, 13 Jun 2021 16:34:35 -0700 Subject: [PATCH 2/5] add RetrieveSPRWithRows method --- spr.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/spr.go b/spr.go index 02ba3ea..39b4743 100644 --- a/spr.go +++ b/spr.go @@ -198,11 +198,34 @@ func RetrieveSPR(ctx context.Context, spr_db *wof_database.SQLiteDatabase, spr_t FROM %s WHERE id = ? AND alt_label = ?`, spr_table.Name()) row := conn.QueryRowContext(ctx, spr_q, args...) - return RetrieveSPRWithRow(ctx, row) } +// See notes below + func RetrieveSPRWithRow(ctx context.Context, row *sql.Row) (wof_spr.StandardPlacesResult, error) { + return retrieveSPRWithScanner(ctx, row) +} + +// See notes below + +func RetrieveSPRWithRows(ctx context.Context, rows *sql.Rows) (wof_spr.StandardPlacesResult, error) { + return retrieveSPRWithScanner(ctx, rows) +} + +// We go to the trouble of all this indirection because neither *sql.Row or *sql.Rows implement +// the sql.Scanner interface. +// The latter expects: Scan(src interface{}) error +// But the former pxpect: Scan(src ...interface{}) error + +func retrieveSPRWithScanner(ctx context.Context, scanner interface{}) (wof_spr.StandardPlacesResult, error) { + + switch scanner.(type) { + case *sql.Row, *sql.Rows: + // pass + default: + return nil, fmt.Errorf("Unsupported scanner") + } var spr_id string var parent_id string @@ -239,17 +262,34 @@ func RetrieveSPRWithRow(ctx context.Context, row *sql.Row) (wof_spr.StandardPlac // supersedes and superseding need to be added here pending // https://github.com/whosonfirst/go-whosonfirst-sqlite-features/issues/14 - err := row.Scan( - &spr_id, &parent_id, &name, &placetype, &country, &repo, - &inception, &cessation, - &latitude, &longitude, &min_latitude, &max_latitude, &min_longitude, &max_longitude, - &is_current, &is_deprecated, &is_ceased, &is_superseded, &is_superseding, - &str_supersedes, &str_superseded_by, &str_belongs_to, - &lastmodified, - ) + var scanner_err error + + switch scanner.(type) { + case *sql.Rows: + + scanner_err = scanner.(*sql.Rows).Scan( + &spr_id, &parent_id, &name, &placetype, &country, &repo, + &inception, &cessation, + &latitude, &longitude, &min_latitude, &max_latitude, &min_longitude, &max_longitude, + &is_current, &is_deprecated, &is_ceased, &is_superseded, &is_superseding, + &str_supersedes, &str_superseded_by, &str_belongs_to, + &lastmodified, + ) + + default: + + scanner_err = scanner.(*sql.Rows).Scan( + &spr_id, &parent_id, &name, &placetype, &country, &repo, + &inception, &cessation, + &latitude, &longitude, &min_latitude, &max_latitude, &min_longitude, &max_longitude, + &is_current, &is_deprecated, &is_ceased, &is_superseded, &is_superseding, + &str_supersedes, &str_superseded_by, &str_belongs_to, + &lastmodified, + ) + } - if err != nil { - return nil, err + if scanner_err != nil { + return nil, scanner_err } id, err := strconv.ParseInt(spr_id, 10, 64) From 8fc454dc1d516ae7a9a2ae36c7ec0e3898a99f31 Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Sun, 13 Jun 2021 21:36:16 -0700 Subject: [PATCH 3/5] Bug fix: Scan all the columns --- spr.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/spr.go b/spr.go index 39b4743..33e4fc0 100644 --- a/spr.go +++ b/spr.go @@ -257,6 +257,9 @@ 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 alt_label string + var lastmodified int64 // supersedes and superseding need to be added here pending @@ -268,22 +271,28 @@ func retrieveSPRWithScanner(ctx context.Context, scanner interface{}) (wof_spr.S case *sql.Rows: scanner_err = scanner.(*sql.Rows).Scan( - &spr_id, &parent_id, &name, &placetype, &country, &repo, + &spr_id, &parent_id, &name, &placetype, &inception, &cessation, - &latitude, &longitude, &min_latitude, &max_latitude, &min_longitude, &max_longitude, + &country, &repo, + &latitude, &longitude, + &min_latitude, &max_latitude, &min_longitude, &max_longitude, &is_current, &is_deprecated, &is_ceased, &is_superseded, &is_superseding, &str_supersedes, &str_superseded_by, &str_belongs_to, + &is_alt, &alt_label, &lastmodified, ) default: scanner_err = scanner.(*sql.Rows).Scan( - &spr_id, &parent_id, &name, &placetype, &country, &repo, + &spr_id, &parent_id, &name, &placetype, &inception, &cessation, - &latitude, &longitude, &min_latitude, &max_latitude, &min_longitude, &max_longitude, + &country, &repo, + &latitude, &longitude, + &min_latitude, &max_latitude, &min_longitude, &max_longitude, &is_current, &is_deprecated, &is_ceased, &is_superseded, &is_superseding, &str_supersedes, &str_superseded_by, &str_belongs_to, + &is_alt, &alt_label, &lastmodified, ) } From 453ef1ad0d58e6c316908501bd7d2bbd8fb617c8 Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Tue, 15 Jun 2021 22:24:53 -0700 Subject: [PATCH 4/5] Bug fix: Ensure correct type casting for sql.Row --- spr.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spr.go b/spr.go index 33e4fc0..6e7854e 100644 --- a/spr.go +++ b/spr.go @@ -284,7 +284,7 @@ func retrieveSPRWithScanner(ctx context.Context, scanner interface{}) (wof_spr.S default: - scanner_err = scanner.(*sql.Rows).Scan( + scanner_err = scanner.(*sql.Row).Scan( &spr_id, &parent_id, &name, &placetype, &inception, &cessation, &country, &repo, From dbcee5ae0abc14c345309476a896544b8c5859ce Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Wed, 16 Jun 2021 22:52:45 -0700 Subject: [PATCH 5/5] Bug fix: include all fields when retrieving SPR --- spr.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spr.go b/spr.go index 6e7854e..de861f6 100644 --- a/spr.go +++ b/spr.go @@ -186,14 +186,14 @@ func RetrieveSPR(ctx context.Context, spr_db *wof_database.SQLiteDatabase, spr_t spr_q := fmt.Sprintf(`SELECT id, parent_id, name, placetype, - country, repo, inception, cessation, + country, repo, latitude, longitude, min_latitude, min_longitude, max_latitude, max_longitude, - is_current, is_deprecated, is_ceased, - is_superseded, is_superseding, + 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())