diff --git a/db/prepared.go b/db/prepared.go index 4cdc8e09..937352ae 100644 --- a/db/prepared.go +++ b/db/prepared.go @@ -2,14 +2,11 @@ package db import ( "context" - "embed" "encoding/json" "fmt" "strings" "time" - fslayer "github.com/Gleipnir-Technology/arcgis-go/fieldseeker/layer" - "github.com/Gleipnir-Technology/arcgis-go/response" "github.com/Gleipnir-Technology/bob" "github.com/Gleipnir-Technology/bob/dialect/psql" "github.com/google/uuid" @@ -17,9 +14,6 @@ import ( "github.com/stephenafamo/scan" ) -//go:embed prepared_functions/*.sql -var sqlFiles embed.FS - func TestPreparedQueryOld(ctx context.Context) error { type Skn struct { Result int @@ -34,52 +28,6 @@ func TestPreparedQueryOld(ctx context.Context) error { return nil } -func TestPreparedQuery(ctx context.Context, row *fslayer.RodentLocation) error { - /* - q := queryStoredProcedure("fieldseeker.insert_rodentlocation", - Uint("p_objectid", row.ObjectID), - String("p_locationname", row.LocationName), - String("p_zone", row.Zone), - String("p_zone2", row.Zone2), - String("p_habitat", row.Habitat), - String("p_priority", row.Priority), - String("p_usetype", row.Usetype), - Int16("p_active", row.Active), - String("p_description", row.Description), - String("p_accessdesc", row.Accessdesc), - String("p_comments", row.Comments), - String("p_symbology", row.Symbology), - String("p_externalid", row.ExternalID), - Timestamp("p_nextactiondatescheduled", row.Nextactiondatescheduled), - Int32("p_locationnumber", row.Locationnumber), - Timestamp("p_lastinspectdate", row.LastInspectionDate), - String("p_lastinspectspecies", row.LastInspectionSpecies), - String("p_lastinspectaction", row.LastInspectionAction), - String("p_lastinspectconditions", row.LastInspectionConditions), - String("p_lastinspectrodentevidence", row.LastInspectionRodentEvidence), - UUID("p_globalid", row.GlobalID), - String("p_created_user", row.CreatedUser), - Timestamp("p_created_date", row.CreatedDate), - String("p_last_edited_user", row.LastEditedUser), - Timestamp("p_last_edited_date", row.LastEditedDate), - Timestamp("p_creationdate", row.CreationDate), - String("p_creator", row.Creator), - Timestamp("p_editdate", row.EditDate), - String("p_editor", row.Editor), - String("p_jurisdiction", row.Jurisdiction), - ) - query := psql.RawQuery(q) - log.Info().Str("query", q).Msg("querying") - result, err := bob.One[InsertResultRow](ctx, PGInstance.BobDB, query, scan.StructMapper[InsertResultRow]()) - if err != nil { - return fmt.Errorf("Failed to execute test function: %w", err) - } - //log.Info().Int("version", result.NextVersion).Msg("got result") - //log.Info().Bool("added", result.Row.Added).Int("version", result.Row.Version).Msg("done") - log.Info().Bool("inserted", result.Inserted).Int("version", result.Version).Msg("done") - */ - return nil -} // SqlParam is a generic struct that wraps a parameter with its SQL representation type SqlParam interface { @@ -414,84 +362,3 @@ func queryStoredProcedure(procedure string, params ...SqlParam) string { // Join parameters and return the execute statement return fmt.Sprintf("SELECT * FROM %s(%s)", procedure, strings.Join(paramStrings, ", ")) } - -func executeFunction(functionName string, params ...SqlParam) string { - if len(params) == 0 { - return fmt.Sprintf("EXECUTE %s()", functionName) - } - - // Convert each parameter to its SQL representation - paramStrings := make([]string, len(params)) - for i, param := range params { - paramStrings[i] = param.ToSql() - } - - // Join parameters and return the execute statement - return fmt.Sprintf("EXECUTE %s(%s)", functionName, strings.Join(paramStrings, ", ")) -} - -func parseLine(msg json.RawMessage) (result GeometryLine, err error) { - err = json.Unmarshal(msg, &result) - if err != nil { - return result, fmt.Errorf("Failed to parse line from '%s': %w", string(msg), err) - } - return result, nil -} - -func parsePoint(msg json.RawMessage) (result GeometryPoint, err error) { - err = json.Unmarshal(msg, &result) - if err != nil { - return result, fmt.Errorf("Failed to parse point from '%s': %w", string(msg), err) - } - return result, nil -} - -func parsePolygon(msg json.RawMessage) (result GeometryPolygon, err error) { - err = json.Unmarshal(msg, &result) - if err != nil { - return result, fmt.Errorf("Failed to parse polygon from '%s': %w", string(msg), err) - } - return result, nil -} - -func lineOrNull(msg json.RawMessage) (SqlParam, error) { - // Surprisingly some geos are actually empty - if len(msg) == 0 { - return NullParam{"p_geospatial"}, nil - } - geo, err := parseLine(msg) - if err != nil { - return NullParam{"p_geospatial"}, fmt.Errorf("Failed to pepare GISLine: %w", err) - } - return GISLine("p_geospatial", geo, 3857), nil -} - -func pointOrNull(geo response.Geometry) (SqlParam, error) { - switch geo.Type() { - case "esriGeometryPoint": - p, ok := geo.(response.Point) - if !ok { - return nil, fmt.Errorf("point that isn't a point") - } - return GISPoint("p_geospatial", GeometryPoint{ - X: p.X, - Y: p.Y, - }, 3857), nil - default: - log.Warn().Str("type", geo.Type()).Msg("expected point, got something else") - return nil, nil - } - -} - -func polygonOrNull(msg json.RawMessage) (SqlParam, error) { - // Surprisingly some geos are actually empty - if len(msg) == 0 { - return NullParam{"p_geospatial"}, nil - } - geo, err := parsePolygon(msg) - if err != nil { - return NullParam{"p_geospatial"}, fmt.Errorf("Failed to pepare GISPolygon: %w", err) - } - return GISPolygon("p_geospatial", geo, 3857), nil -}