Movatterモバイル変換


[0]ホーム

URL:


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

Postgres Store Procedure#3160

Unanswered
skedee asked this question inIssue Triage
Jan 26, 2024· 3 comments
Discussion options

Does sqlc have support to generate code for migrations that contain stored procedures?

You must be logged in to vote

Replies: 3 comments

Comment options

@skedee Did you find the answer?

You must be logged in to vote
0 replies
Comment options

@skedee I don't think I can answer your question without more information. Can you provide a full example?

You must be logged in to vote
0 replies
Comment options

@kyleconroy Hi, let me give an example.

migration.sql

-- Table: accountCREATETABLEaccount (    account_idSERIALPRIMARY KEY,    usernameVARCHAR(50)NOT NULL UNIQUE,    password_hashTEXTNOT NULL,    emailVARCHAR(100),    created_atTIMESTAMP DEFAULTCURRENT_TIMESTAMP,    account_typeVARCHAR(20),-- can be 'user', 'admin', or 'superadmin'    is_activeBOOLEANNOT NULL DEFAULT TRUE,    admin_idINT,FOREIGN KEY (admin_id)REFERENCES account (account_id)ON DELETESETNULL);-- For bulk updateCREATETYPEaccount_updateAS (    account_idINT,    usernameVARCHAR(50),    password_hashTEXT,    emailVARCHAR(100),    account_typeVARCHAR(20),    is_activeBOOLEAN,    admin_idINT);-- For bulk updateCREATE OR REPLACEFUNCTIONbulk_update_accounts(admin_idINT, accounts account_update[])RETURNS VOIDAS $$BEGINUPDATE accountAS aSET        username= COALESCE(u.username,a.username),        password_hash= COALESCE(u.password_hash,a.password_hash),        email= COALESCE(u.email,a.email),        account_type= COALESCE(u.account_type,a.account_type),        is_active= COALESCE(u.is_active,a.is_active)FROM UNNEST(accounts)AS uWHEREa.account_id=u.account_idANDa.admin_id= admin_id;END;$$ LANGUAGE plpgsql;

Now in account.sql repo for sqlc:

-- name: BulkUpdateAccounts :execSELECT bulk_update_accounts($1, $2);

This generates this code-

constbulkUpdateAccounts=`-- name: BulkUpdateAccounts :execSELECT bulk_update_accounts($1, $2)`typeBulkUpdateAccountsParamsstruct {AdminIDint32`json:"adminId"`Accountsstring`json:"accounts"`// Account is of string type instead of a slice of struct}func (q*Queries)BulkUpdateAccounts(ctx context.Context,argBulkUpdateAccountsParams)error {_,err:=q.db.Exec(ctx,bulkUpdateAccounts,arg.AdminID,arg.Accounts)returnerr}

See, the Accounts is of string type instead of a slice of struct.
Am I doing something wrong in my SQL definition, or does sqlc not yet support generating the correct Go type for a function parameter that is an array of a composite type?

Before using SP, I have tried this in the account.sql as well but it also has the same issue with generated type.

-- name: BulkUpdateAccounts :execUPDATE accountAS aSETusername= COALESCE(u.username,a.username),password_hash= COALESCE(u.password_hash,a.password_hash),email= COALESCE(u.email,a.email),account_type= COALESCE(u.account_type,a.account_type),is_active= COALESCE(u.is_active,a.is_active),admin_id= COALESCE(u.admin_id,a.admin_id)FROMUNNEST(sqlc.arg('accounts')::account_update[])AS uWHEREa.account_id=u.account_idANDa.admin_id=sqlc.arg('admin_id');
You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
3 participants
@skedee@kyleconroy@5hubham5ingh

[8]ページ先頭

©2009-2025 Movatter.jp