Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added availability create adapters with already opened db #69

Merged
merged 2 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions adapter/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,23 @@ type Adapter struct {

var _ rel.Adapter = (*Adapter)(nil)

// Open mysql connection using dsn.
func Open(dsn string) (*Adapter, error) {
var err error
// New is mysql adapter constructor.
func New(database *db.DB) *Adapter {
return &Adapter{
Adapter: &sql.Adapter{
Config: &sql.Config{
Placeholder: "?",
EscapeChar: "`",
IncrementFunc: incrementFunc,
ErrorFunc: errorFunc,
},
DB: database,
},
}
}

// Open mysql connection using dsn.
func Open(dsn string) (_ *Adapter, err error) {
snovichkov marked this conversation as resolved.
Show resolved Hide resolved
// force clientFoundRows=true
// this allows not found record check when updating a record.
if strings.ContainsRune(dsn, '?') {
Expand All @@ -39,19 +52,12 @@ func Open(dsn string) (*Adapter, error) {
dsn += "?clientFoundRows=true"
}

adapter := &Adapter{
Adapter: &sql.Adapter{
Config: &sql.Config{
Placeholder: "?",
EscapeChar: "`",
IncrementFunc: incrementFunc,
ErrorFunc: errorFunc,
},
},
var database *db.DB
if database, err = db.Open("mysql", dsn); err != nil {
return nil, err
}
adapter.DB, err = db.Open("mysql", dsn)

return adapter, err
return New(database), nil
}

func incrementFunc(adapter sql.Adapter) int {
Expand Down
22 changes: 14 additions & 8 deletions adapter/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ import (
"github.com/Fs02/rel/adapter/sql"
)

// Adapter definition for postgrees database.
// Adapter definition for postgres database.
type Adapter struct {
*sql.Adapter
}

var _ rel.Adapter = (*Adapter)(nil)

// Open postgrees connection using dsn.
func Open(dsn string) (*Adapter, error) {
var err error

adapter := &Adapter{
// New is postgres adapter constructor.
func New(database *db.DB) *Adapter {
return &Adapter{
Adapter: &sql.Adapter{
Config: &sql.Config{
Placeholder: "$",
Expand All @@ -40,11 +38,19 @@ func Open(dsn string) (*Adapter, error) {
InsertDefaultValues: true,
ErrorFunc: errorFunc,
},
DB: database,
},
}
adapter.DB, err = db.Open("postgres", dsn)
}

// Open postgres connection using dsn.
func Open(dsn string) (_ *Adapter, err error) {
var database *db.DB
if database, err = db.Open("postgres", dsn); err != nil {
return nil, err
}

return adapter, err
return New(database), nil
snovichkov marked this conversation as resolved.
Show resolved Hide resolved
}

// Insert inserts a record to database and returns its id.
Expand Down
20 changes: 13 additions & 7 deletions adapter/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ type Adapter struct {

var _ rel.Adapter = (*Adapter)(nil)

// Open mysql connection using dsn.
func Open(dsn string) (*Adapter, error) {
var err error

adapter := &Adapter{
// New is mysql adapter constructor.
func New(database *db.DB) *Adapter {
return &Adapter{
Adapter: &sql.Adapter{
Config: &sql.Config{
Placeholder: "?",
Expand All @@ -40,11 +38,19 @@ func Open(dsn string) (*Adapter, error) {
IncrementFunc: incrementFunc,
ErrorFunc: errorFunc,
},
DB: database,
},
}
adapter.DB, err = db.Open("sqlite3", dsn)
}

// Open mysql connection using dsn.
func Open(dsn string) (_ *Adapter, err error) {
var database *db.DB
if database, err = db.Open("sqlite3", dsn); err != nil {
return nil, err
}

return adapter, err
return New(database), nil
}

func incrementFunc(adapter sql.Adapter) int {
Expand Down