Skip to content

Commit

Permalink
removed duplicated codes and hard-coded switching (#294)
Browse files Browse the repository at this point in the history
* removed duplicated codes

* remove last hardcoded switch for dialects
  • Loading branch information
sio4 authored and stanislas-m committed Nov 24, 2018
1 parent eab37c3 commit f85fc4c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 40 deletions.
50 changes: 17 additions & 33 deletions connection.go
Expand Up @@ -53,20 +53,15 @@ func NewConnection(deets *ConnectionDetails) (*Connection, error) {
c := &Connection{
ID: randx.String(30),
}
switch deets.Dialect {
case "postgres":
c.Dialect = newPostgreSQL(deets)
case "cockroach":
c.Dialect = newCockroach(deets)
case "mysql":
c.Dialect = newMySQL(deets)
case "sqlite3":
c.Dialect, err = newSQLite(deets)

if nc, ok := newConnection[deets.Dialect]; ok {
c.Dialect, err = nc(deets)
if err != nil {
return c, errors.WithStack(err)
return c, errors.Wrap(err, "could not create new connection")
}
return c, nil
}
return c, nil
return nil, errors.Errorf("could not found connection creator for %v", deets.Dialect)
}

// Connect takes the name of a connection, default is "development", and will
Expand Down Expand Up @@ -134,6 +129,17 @@ func (c *Connection) Transaction(fn func(tx *Connection) error) error {

}

// Rollback will open a new transaction and automatically rollback that transaction
// when the inner function returns, regardless. This can be useful for tests, etc...
func (c *Connection) Rollback(fn func(tx *Connection)) error {
cn, err := c.NewTransaction()
if err != nil {
return err
}
fn(cn)
return cn.TX.Rollback()
}

// NewTransaction starts a new transaction on the connection
func (c *Connection) NewTransaction() (*Connection, error) {
var cn *Connection
Expand Down Expand Up @@ -163,28 +169,6 @@ func (c *Connection) copy() *Connection {
}
}

// Rollback will open a new transaction and automatically rollback that transaction
// when the inner function returns, regardless. This can be useful for tests, etc...
func (c *Connection) Rollback(fn func(tx *Connection)) error {
var cn *Connection
if c.TX == nil {
tx, err := c.Store.Transaction()
if err != nil {
return errors.Wrap(err, "couldn't start a new transaction")
}
cn = &Connection{
ID: randx.String(30),
Store: tx,
Dialect: c.Dialect,
TX: tx,
}
} else {
cn = c
}
fn(cn)
return cn.TX.Rollback()
}

// Q creates a new "empty" query for the current connection.
func (c *Connection) Q() *Query {
return Q(c)
Expand Down
5 changes: 3 additions & 2 deletions dialect_cockroach.go
Expand Up @@ -26,6 +26,7 @@ func init() {
dialectSynonyms["cockroachdb"] = nameCockroach
dialectSynonyms["crdb"] = nameCockroach
finalizer[nameCockroach] = finalizerCockroach
newConnection[nameCockroach] = newCockroach
}

var _ dialect = &cockroach{}
Expand Down Expand Up @@ -271,14 +272,14 @@ func (p *cockroach) TruncateAll(tx *Connection) error {
// return tx3.RawQuery(fmt.Sprintf("truncate %s cascade;", strings.Join(tableNames, ", "))).Exec()
}

func newCockroach(deets *ConnectionDetails) dialect {
func newCockroach(deets *ConnectionDetails) (dialect, error) {
deets.Dialect = "postgres"
cd := &cockroach{
ConnectionDetails: deets,
translateCache: map[string]string{},
mu: sync.Mutex{},
}
return cd
return cd, nil
}

func finalizerCockroach(cd *ConnectionDetails) {
Expand Down
6 changes: 3 additions & 3 deletions dialect_mysql.go
Expand Up @@ -27,6 +27,7 @@ func init() {
AvailableDialects = append(AvailableDialects, nameMySQL)
urlParser[nameMySQL] = urlParserMySQL
finalizer[nameMySQL] = finalizerMySQL
newConnection[nameMySQL] = newMySQL
}

var _ dialect = &mysql{}
Expand Down Expand Up @@ -184,12 +185,11 @@ func (m *mysql) TruncateAll(tx *Connection) error {
return tx.RawQuery(qb.String()).Exec()
}

func newMySQL(deets *ConnectionDetails) dialect {
func newMySQL(deets *ConnectionDetails) (dialect, error) {
cd := &mysql{
ConnectionDetails: deets,
}

return cd
return cd, nil
}

func urlParserMySQL(cd *ConnectionDetails) error {
Expand Down
5 changes: 3 additions & 2 deletions dialect_postgresql.go
Expand Up @@ -24,6 +24,7 @@ func init() {
dialectSynonyms["postgresql"] = namePostgreSQL
dialectSynonyms["pg"] = namePostgreSQL
finalizer[namePostgreSQL] = finalizerPostgreSQL
newConnection[namePostgreSQL] = newPostgreSQL
}

var _ dialect = &postgresql{}
Expand Down Expand Up @@ -198,13 +199,13 @@ func (p *postgresql) TruncateAll(tx *Connection) error {
return tx.RawQuery(fmt.Sprintf(pgTruncate, tx.MigrationTableName())).Exec()
}

func newPostgreSQL(deets *ConnectionDetails) dialect {
func newPostgreSQL(deets *ConnectionDetails) (dialect, error) {
cd := &postgresql{
ConnectionDetails: deets,
translateCache: map[string]string{},
mu: sync.Mutex{},
}
return cd
return cd, nil
}

func finalizerPostgreSQL(cd *ConnectionDetails) {
Expand Down
1 change: 1 addition & 0 deletions dialect_sqlite.go
Expand Up @@ -28,6 +28,7 @@ func init() {
AvailableDialects = append(AvailableDialects, nameSQLite3)
dialectSynonyms["sqlite"] = nameSQLite3
urlParser[nameSQLite3] = urlParserSQLite3
newConnection[nameSQLite3] = newSQLite
}

var _ dialect = &sqlite{}
Expand Down
8 changes: 8 additions & 0 deletions dialect_sqlite_shim.go
Expand Up @@ -6,6 +6,14 @@ import (
"errors"
)

const nameSQLite3 = "sqlite3"

func init() {
AvailableDialects = append(AvailableDialects, nameSQLite3)
dialectSynonyms["sqlite"] = nameSQLite3
newConnection[nameSQLite3] = newSQLite
}

func newSQLite(deets *ConnectionDetails) (dialect, error) {
return nil, errors.New("sqlite3 support was not compiled into the binary")
}
3 changes: 3 additions & 0 deletions pop.go
Expand Up @@ -13,6 +13,9 @@ var urlParser = make(map[string]func(*ConnectionDetails) error)
// map of dialect specific connection details finalizers
var finalizer = make(map[string]func(*ConnectionDetails))

// map of connection creators
var newConnection = make(map[string]func(*ConnectionDetails) (dialect, error))

// DialectSupported checks support for the given database dialect
func DialectSupported(d string) bool {
for _, ad := range AvailableDialects {
Expand Down

0 comments on commit f85fc4c

Please sign in to comment.