Replies: 2 comments
-
I believe #112 would solve this problem without having to extend the configuration file. Your queries would look something like this: -- name: Subscription :template
SELECT
id,
title,
description,
ST_AsBinary(geometry)::geometry as geometry,
start_time,
end_time,
owner,
created
FROM subscriptions;
-- name: GetSubscription :one
SELECT sqlc.tmpl('Subscription') FROM subscriptions
WHERE id = $1 LIMIT 1;
-- name: CreateSubscription :one
INSERT INTO subscriptions (
id, title, description, geometry, start_time, end_time, owner, created
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)
RETURNING sqlc.tmpl('Subscription'); The above syntax is just an example; we haven't decided on a final syntax. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the response, @kyleconroy. Maybe a template would help - but I'm not sure it would completely solve it. I think there may be multiple issues. Here is a somewhat stripped down example: CREATE TABLE places (
id UUID PRIMARY KEY,
geometry GEOMETRY NOT NULL
);
-- name: CreatePlace :one
INSERT INTO places (
id, geometry
) VALUES (
$1, ST_GeomFromWKB($2)::geometry -- This is issue #1
)
RETURNING id, ST_AsBinary(geometry::geometry) as geometry; -- This is issue #2 This generates the expected type Place struct {
ID uuid.UUID `json:"id"`
Geometry *geo.Geometry `json:"geometry"`
} However, these types were a surprise to me: type CreatePlaceParams struct {
ID uuid.UUID `json:"id"`
StGeomfromwkb interface{} `json:"st_geomfromwkb"` // this is issue #1
}
type CreatePlaceRow struct {
ID uuid.UUID `json:"id"`
Geometry interface{} `json:"geometry"` // this is issue #2
} The first surprise (issue 1) was that the The second surprise (issue 2) was that the The underlying problem is that the driver I'm using doesn't seem to let me configure both the result and parameter format as binary. So I need to use |
Beta Was this translation helpful? Give feedback.
-
I have a table created like this:
To handle the
geometry
column, I have an override in mysqlc.yaml
like this:The
*example.com/pkg/geo.Geometry
type implementssql.Scanner
anddriver.Valuer
, but when selecting a geometry, I need to call theST_AsBinary
function first. I've written a select query like this:I was hoping this would make use of the already generated
Subscription
type (which is the behavior withselect * from subscriptions where id = $1 limit 1
), but it creates a new typeGetSubscriptionRow
. Having these types is not that big a hassle, but if I have more queries that select subscriptions (e.g. by time, by area, etc.), it looks like I'll be getting a new type for each. I'm wondering if there is a way to use theST_AsBinary
function in a select without generating an additional type for the returned record.A related problem comes up for insert/update. My insert query looks something like this:
This generates types like this:
Ideally,
StGeomfromwkb
would be namedGeometry
, the type would be*geo.Geometry
instead ofinterface{}
, and the tag would bejson:"geometry"
.And instead of the new
CreateSubscriptionRow
type, I could use the existingSubscription
type (already inmodels.go
). With theGeometry
field beinginterface{}
, my scanner is not getting called for the returned row.I recognize these should probably be separate issues, but I'm wondering if it would be reasonable to support additional override properties to handle this extra SQL on encode and decode. For example, I'm imagining that I might be able configure an override like this:
Then my queries would look like this:
And the generated SQL would look like this:
Basically, I'd like to use the magic of
select * from ...
and... returning *
and avoid repeating column names and having to use the additional encoding/decoding functions everywhere.Beta Was this translation helpful? Give feedback.
All reactions