Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Named parameters for MySQL inserts / updates #3038

Answered by kyleconroy
SebastienMelki asked this question in Q&A
Discussion options

What do you want to change?

I'm trying to generate code based on queries like this

-- name: InsertPurchaseGuid :exec INSERT INTO purchase_guids (external_id, guid, created_at, updated_at, partner_id, payment_method_id) VALUES (:external_id, :guid, :created_at, :updated_at, :partner_id, :payment_method_id);

So that I can use the NamedExec function of the mysql driver. But whenever I run sqlc generate I get this error

db/queries/subs/insert_purchase_guid.sql:3:10: syntax error near ":external_id, :guid, :created_at, :updated_at, :partner_id, :payment_method_id);"

Is this not supported at all? Or am I doing it wrong?

Thank you!

What database engines need to be changed?

MySQL

What programming language backends need to be changed?

Go

You must be logged in to vote

The named parameter syntax you're using is not native to MySQL. It's an extension added by sqlx. To use named parameters with sqlc, you need to use the sqlc.arg function, like so:

/* name: InsertPurchaseGuid :exec */
INSERT INTO purchase_guids (
 external_id,
 guid,
 created_at,
 updated_at,
 partner_id,
 payment_method_id
) VALUES (
 sqlc.arg('external_id'),
 sqlc.arg('guid'),
 sqlc.arg('created_at'),
 sqlc.arg('updated_at'),
 sqlc.arg('partner_id'),
 sqlc.arg('payment_method_id')
 );

Replies: 3 comments 3 replies

Comment options

MySQL does not support NamedParameter

You must be logged in to vote
0 replies
Comment options

MySQL does not support NamedParameter

Hello, do you mean it's not supported in sqlc? Because MySQL itself does support named parameters just fine as far as my testing goes. I used the .NamedExec of the sql driver in go, and got the expected results

You must be logged in to vote
0 replies
Comment options

The named parameter syntax you're using is not native to MySQL. It's an extension added by sqlx. To use named parameters with sqlc, you need to use the sqlc.arg function, like so:

/* name: InsertPurchaseGuid :exec */
INSERT INTO purchase_guids (
 external_id,
 guid,
 created_at,
 updated_at,
 partner_id,
 payment_method_id
) VALUES (
 sqlc.arg('external_id'),
 sqlc.arg('guid'),
 sqlc.arg('created_at'),
 sqlc.arg('updated_at'),
 sqlc.arg('partner_id'),
 sqlc.arg('payment_method_id')
 );
You must be logged in to vote
3 replies
Comment options

Hi @kyleconroy thank you for the answer. I actually use sqlc only to generate the structs for the relevant tables / queries, but I don't use the generated function to run those queries, I have my own custom ones that include instrumentation and logging. Is there a way to force sqlc to still generate the structs with the sqlx extension syntax? Or absolutely no way?

Thanks for the feedback!

Comment options

There isn't a way to generate the structs. The SQL isn't valid MySQL syntax, so sqlc can't parse the queries.

Comment options

Alright thanks a lot @kyleconroy ! Appreciate it

Answer selected by kyleconroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #3037 on December 05, 2023 16:57.

AltStyle によって変換されたページ (->オリジナル) /