Skip to content

Commit

Permalink
Move sql connection interface to plugin level (cadence-workflow#3539)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjdawson2016 authored Sep 22, 2020
1 parent 31bd469 commit 8bd97e5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
7 changes: 0 additions & 7 deletions common/persistence/sql/sqlplugin/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,4 @@ type (
PluginName() string
Close() error
}
// Conn defines the API for a single database connection
Conn interface {
Exec(query string, args ...interface{}) (sql.Result, error)
NamedExec(query string, arg interface{}) (sql.Result, error)
Get(dest interface{}, query string, args ...interface{}) error
Select(dest interface{}, query string, args ...interface{}) error
}
)
37 changes: 25 additions & 12 deletions common/persistence/sql/sqlplugin/mysql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,29 @@
package mysql

import (
"database/sql"

"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"

"github.com/uber/cadence/common/persistence/sql/sqlplugin"
)

// db represents a logical connection to mysql database
type db struct {
db *sqlx.DB
tx *sqlx.Tx
conn sqlplugin.Conn
converter DataConverter
}
type (
db struct {
db *sqlx.DB
tx *sqlx.Tx
converter DataConverter
conn conn
}

conn interface {
Exec(query string, args ...interface{}) (sql.Result, error)
NamedExec(query string, arg interface{}) (sql.Result, error)
Get(dest interface{}, query string, args ...interface{}) error
Select(dest interface{}, query string, args ...interface{}) error
}
)

var _ sqlplugin.AdminDB = (*db)(nil)
var _ sqlplugin.DB = (*db)(nil)
Expand All @@ -51,13 +61,16 @@ func (mdb *db) IsDupEntryError(err error) bool {
// newDB returns an instance of DB, which is a logical
// connection to the underlying mysql database
func newDB(xdb *sqlx.DB, tx *sqlx.Tx) *db {
mdb := &db{db: xdb, tx: tx}
mdb.conn = xdb
db := &db{
db: xdb,
tx: tx,
converter: &converter{},
conn: xdb,
}
if tx != nil {
mdb.conn = tx
db.conn = tx
}
mdb.converter = &converter{}
return mdb
return db
}

// BeginTx starts a new transaction and returns a reference to the Tx object
Expand Down
37 changes: 25 additions & 12 deletions common/persistence/sql/sqlplugin/postgres/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,29 @@
package postgres

import (
"database/sql"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"

"github.com/uber/cadence/common/persistence/sql/sqlplugin"
)

// db represents a logical connection to mysql database
type db struct {
db *sqlx.DB
tx *sqlx.Tx
conn sqlplugin.Conn
converter DataConverter
}
type (
conn interface {
Exec(query string, args ...interface{}) (sql.Result, error)
NamedExec(query string, arg interface{}) (sql.Result, error)
Get(dest interface{}, query string, args ...interface{}) error
Select(dest interface{}, query string, args ...interface{}) error
}

db struct {
db *sqlx.DB
tx *sqlx.Tx
conn conn
converter DataConverter
}
)

var _ sqlplugin.DB = (*db)(nil)
var _ sqlplugin.Tx = (*db)(nil)
Expand All @@ -50,13 +60,16 @@ func (pdb *db) IsDupEntryError(err error) bool {
// newDB returns an instance of DB, which is a logical
// connection to the underlying postgres database
func newDB(xdb *sqlx.DB, tx *sqlx.Tx) *db {
mdb := &db{db: xdb, tx: tx}
mdb.conn = xdb
db := &db{
db: xdb,
tx: tx,
converter: &converter{},
conn: xdb,
}
if tx != nil {
mdb.conn = tx
db.conn = tx
}
mdb.converter = &converter{}
return mdb
return db
}

// BeginTx starts a new transaction and returns a reference to the Tx object
Expand Down

0 comments on commit 8bd97e5

Please sign in to comment.