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

support for mutiple db connections #908

Unanswered
jzendle asked this question in Issue Triage
Discussion options

Hi,

Nice work!

2 quick questions:
I have an application that must connect to multiple databases. Could be mysql and/or PG

  1. Does sqlc support multiple connections to different instances of mysql (just mysql)?
  2. How about multiple connections to a mix of mysql and PG?

Thanks in advance.

You must be logged in to vote

Replies: 2 comments

Comment options

Does sqlc support multiple connections to different instances of mysql (just mysql)?

sqlc will generate a constructor that takes a *sql.DB struct and returns a *Queries struct.

type DBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
	return &Queries{db: db}
}

You would need to call this constructor with both of your database connections.

How about multiple connections to a mix of mysql and PG?

sqlc supports this, but it may not work as you expect. sqlc isn't doing any translation of the SQL you right, so you have to make sure that your SQL queries work with MySQL and PostgreSQL, which can be challenging.

You must be logged in to vote
0 replies
Comment options

Got it. Thanks for the explanation. Sent from my Galaxy -------- Original message --------From: Kyle Conroy <notifications@github.com> Date: 2/23/21 10:25 PM (GMT-07:00) To: kyleconroy/sqlc <sqlc@noreply.github.com> Cc: jzendle <joe_zendle@yahoo.com>, Author <author@noreply.github.com> Subject: Re: [kyleconroy/sqlc] support for mutiple db connections (#908) Does sqlc support multiple connections to different instances of mysql (just mysql)? sqlc will generate a constructor that takes a *sql.DB struct and returns a *Queries struct. type DBTX interface { ExecContext(context.Context, string, ...interface{}) (sql.Result, error) PrepareContext(context.Context, string) (*sql.Stmt, error) QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) QueryRowContext(context.Context, string, ...interface{}) *sql.Row } func New(db DBTX) *Queries { return &Queries{db: db} } You would need to call this constructor with both of your database connections. How about multiple connections to a mix of mysql and PG? sqlc supports this, but it may not work as you expect. sqlc isn't doing any translation of the SQL you right, so you have to make sure that your SQL queries work with MySQL and PostgreSQL, which can be challenging. —You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or unsubscribe.
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

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