-
Notifications
You must be signed in to change notification settings - Fork 923
support for mutiple db connections #908
-
Hi,
Nice work!
2 quick questions:
I have an application that must connect to multiple databases. Could be mysql and/or PG
- Does sqlc support multiple connections to different instances of mysql (just mysql)?
- How about multiple connections to a mix of mysql and PG?
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Beta Was this translation helpful? Give feedback.