Permalink
Browse files

support multiple or no logger

  • Loading branch information...
Fs02 committed Apr 17, 2018
1 parent a2d65ad commit e170eb2d5821b6666d827e0345d59a21ff99c25e
@@ -2,12 +2,12 @@ package grimoire

// Adapter interface
type Adapter interface {
Count(Query, Logger) (int, error)
All(Query, interface{}, Logger) (int, error)
Delete(Query, Logger) error
Insert(Query, map[string]interface{}, Logger) (interface{}, error)
InsertAll(Query, []string, []map[string]interface{}, Logger) ([]interface{}, error)
Update(Query, map[string]interface{}, Logger) error
Count(Query, ...Logger) (int, error)
All(Query, interface{}, ...Logger) (int, error)
Delete(Query, ...Logger) error
Insert(Query, map[string]interface{}, ...Logger) (interface{}, error)
InsertAll(Query, []string, []map[string]interface{}, ...Logger) ([]interface{}, error)
Update(Query, map[string]interface{}, ...Logger) error

Begin() (Adapter, error)
Commit() error
@@ -3,7 +3,6 @@ package mysql
import (
"os"
"testing"
"time"

"github.com/Fs02/go-paranoid"
"github.com/Fs02/grimoire"
@@ -13,18 +12,16 @@ import (
"github.com/stretchr/testify/assert"
)

func logger(string, time.Duration, error) {}

func init() {
adapter, err := Open(dsn())
if err != nil {
panic(err)
}
defer adapter.Close()

_, _, err = adapter.Exec(`DROP TABLE IF EXISTS addresses;`, []interface{}{}, logger)
_, _, err = adapter.Exec(`DROP TABLE IF EXISTS addresses;`, nil)
paranoid.Panic(err)
_, _, err = adapter.Exec(`DROP TABLE IF EXISTS users;`, []interface{}{}, logger)
_, _, err = adapter.Exec(`DROP TABLE IF EXISTS users;`, nil)
paranoid.Panic(err)

_, _, err = adapter.Exec(`CREATE TABLE users (
@@ -35,7 +32,7 @@ func init() {
note varchar(50),
created_at DATETIME,
updated_at DATETIME
);`, []interface{}{}, logger)
);`, nil)
paranoid.Panic(err)

_, _, err = adapter.Exec(`CREATE TABLE addresses (
@@ -45,7 +42,7 @@ func init() {
created_at DATETIME,
updated_at DATETIME,
FOREIGN KEY (user_id) REFERENCES users(id)
);`, []interface{}{}, logger)
);`, nil)
paranoid.Panic(err)
}

@@ -108,7 +105,7 @@ func TestAdapterInsertAllError(t *testing.T) {
{"notexist": "13"},
}

_, err = adapter.InsertAll(grimoire.Repo{}.From("users"), fields, allchanges, logger)
_, err = adapter.InsertAll(grimoire.Repo{}.From("users"), fields, allchanges)

assert.NotNil(t, err)
}
@@ -142,7 +139,7 @@ func TestAdapterQueryError(t *testing.T) {

out := struct{}{}

_, err = adapter.Query(&out, "error", []interface{}{}, logger)
_, err = adapter.Query(&out, "error", nil)
assert.NotNil(t, err)
}

@@ -153,7 +150,7 @@ func TestAdapterExecError(t *testing.T) {
}
defer adapter.Close()

_, _, err = adapter.Exec("error", []interface{}{}, logger)
_, _, err = adapter.Exec("error", nil)
assert.NotNil(t, err)
}

@@ -27,7 +27,7 @@ func Open(dsn string) (*Adapter, error) {
}

// Insert inserts a record to database and returns its id.
func (adapter *Adapter) Insert(query grimoire.Query, changes map[string]interface{}, logger grimoire.Logger) (interface{}, error) {
func (adapter *Adapter) Insert(query grimoire.Query, changes map[string]interface{}, loggers ...grimoire.Logger) (interface{}, error) {
statement, args := sql.NewBuilder(adapter.Placeholder, adapter.Ordinal).
Returning("id").
Insert(query.Collection, changes)
@@ -36,19 +36,19 @@ func (adapter *Adapter) Insert(query grimoire.Query, changes map[string]interfac
ID int64
}

_, err := adapter.Query(&result, statement, args, logger)
_, err := adapter.Query(&result, statement, args, loggers...)
return result.ID, err
}

// InsertAll inserts all record to database and returns its ids.
func (adapter *Adapter) InsertAll(query grimoire.Query, fields []string, allchanges []map[string]interface{}, logger grimoire.Logger) ([]interface{}, error) {
func (adapter *Adapter) InsertAll(query grimoire.Query, fields []string, allchanges []map[string]interface{}, loggers ...grimoire.Logger) ([]interface{}, error) {
statement, args := sql.NewBuilder(adapter.Placeholder, adapter.Ordinal).Returning("id").InsertAll(query.Collection, fields, allchanges)

var result []struct {
ID int64
}

_, err := adapter.Query(&result, statement, args, logger)
_, err := adapter.Query(&result, statement, args, loggers...)

ids := make([]interface{}, 0, len(result))
for _, r := range result {
@@ -3,7 +3,6 @@ package postgres
import (
"os"
"testing"
"time"

"github.com/Fs02/go-paranoid"
"github.com/Fs02/grimoire"
@@ -13,18 +12,16 @@ import (
"github.com/stretchr/testify/assert"
)

func logger(string, time.Duration, error) {}

func init() {
adapter, err := Open(dsn())
if err != nil {
panic(err)
}
defer adapter.Close()

_, _, err = adapter.Exec(`DROP TABLE IF EXISTS addresses;`, []interface{}{}, logger)
_, _, err = adapter.Exec(`DROP TABLE IF EXISTS addresses;`, nil)
paranoid.Panic(err)
_, _, err = adapter.Exec(`DROP TABLE IF EXISTS users;`, []interface{}{}, logger)
_, _, err = adapter.Exec(`DROP TABLE IF EXISTS users;`, nil)
paranoid.Panic(err)

_, _, err = adapter.Exec(`CREATE TABLE users (
@@ -35,7 +32,7 @@ func init() {
note varchar(50),
created_at TIMESTAMP,
updated_at TIMESTAMP
);`, []interface{}{}, logger)
);`, nil)
paranoid.Panic(err)

_, _, err = adapter.Exec(`CREATE TABLE addresses (
@@ -44,7 +41,7 @@ func init() {
address VARCHAR(60) NOT NULL DEFAULT '',
created_at TIMESTAMP,
updated_at TIMESTAMP
);`, []interface{}{}, logger)
);`, nil)
paranoid.Panic(err)
}

@@ -107,7 +104,7 @@ func TestAdapterInsertAllError(t *testing.T) {
{"notexist": "13"},
}

_, err = adapter.InsertAll(grimoire.Repo{}.From("users"), fields, allchanges, logger)
_, err = adapter.InsertAll(grimoire.Repo{}.From("users"), fields, allchanges)

assert.NotNil(t, err)
}
@@ -141,7 +138,7 @@ func TestAdapterQueryError(t *testing.T) {

out := struct{}{}

_, err = adapter.Query(&out, "error", []interface{}{}, logger)
_, err = adapter.Query(&out, "error", nil)
assert.NotNil(t, err)
}

@@ -152,7 +149,7 @@ func TestAdapterExecError(t *testing.T) {
}
defer adapter.Close()

_, _, err = adapter.Exec("error", []interface{}{}, logger)
_, _, err = adapter.Exec("error", nil)
assert.NotNil(t, err)
}

@@ -36,35 +36,35 @@ func (adapter *Adapter) Close() error {
}

// Count retrieves count of record that match the query.
func (adapter *Adapter) Count(query grimoire.Query, logger grimoire.Logger) (int, error) {
func (adapter *Adapter) Count(query grimoire.Query, loggers ...grimoire.Logger) (int, error) {
var doc struct {
Count int
}

query.Fields = []string{"COUNT(*) AS count"}
statement, args := NewBuilder(adapter.Placeholder, adapter.Ordinal).Find(query)
_, err := adapter.Query(&doc, statement, args, logger)
_, err := adapter.Query(&doc, statement, args, loggers...)
return doc.Count, err
}

// All retrieves all record that match the query.
func (adapter *Adapter) All(query grimoire.Query, doc interface{}, logger grimoire.Logger) (int, error) {
func (adapter *Adapter) All(query grimoire.Query, doc interface{}, loggers ...grimoire.Logger) (int, error) {
statement, args := NewBuilder(adapter.Placeholder, adapter.Ordinal).Find(query)
count, err := adapter.Query(doc, statement, args, logger)
count, err := adapter.Query(doc, statement, args, loggers...)
return int(count), err
}

// Insert inserts a record to database and returns its id.
func (adapter *Adapter) Insert(query grimoire.Query, changes map[string]interface{}, logger grimoire.Logger) (interface{}, error) {
func (adapter *Adapter) Insert(query grimoire.Query, changes map[string]interface{}, loggers ...grimoire.Logger) (interface{}, error) {
statement, args := NewBuilder(adapter.Placeholder, adapter.Ordinal).Insert(query.Collection, changes)
id, _, err := adapter.Exec(statement, args, logger)
id, _, err := adapter.Exec(statement, args, loggers...)
return id, err
}

// InsertAll inserts all record to database and returns its ids.
func (adapter *Adapter) InsertAll(query grimoire.Query, fields []string, allchanges []map[string]interface{}, logger grimoire.Logger) ([]interface{}, error) {
func (adapter *Adapter) InsertAll(query grimoire.Query, fields []string, allchanges []map[string]interface{}, loggers ...grimoire.Logger) ([]interface{}, error) {
statement, args := NewBuilder(adapter.Placeholder, adapter.Ordinal).InsertAll(query.Collection, fields, allchanges)
id, _, err := adapter.Exec(statement, args, logger)
id, _, err := adapter.Exec(statement, args, loggers...)
if err != nil {
return nil, err
}
@@ -84,16 +84,16 @@ func (adapter *Adapter) InsertAll(query grimoire.Query, fields []string, allchan
}

// Update updates a record in database.
func (adapter *Adapter) Update(query grimoire.Query, changes map[string]interface{}, logger grimoire.Logger) error {
func (adapter *Adapter) Update(query grimoire.Query, changes map[string]interface{}, loggers ...grimoire.Logger) error {
statement, args := NewBuilder(adapter.Placeholder, adapter.Ordinal).Update(query.Collection, changes, query.Condition)
_, _, err := adapter.Exec(statement, args, logger)
_, _, err := adapter.Exec(statement, args, loggers...)
return err
}

// Delete deletes all results that match the query.
func (adapter *Adapter) Delete(query grimoire.Query, logger grimoire.Logger) error {
func (adapter *Adapter) Delete(query grimoire.Query, loggers ...grimoire.Logger) error {
statement, args := NewBuilder(adapter.Placeholder, adapter.Ordinal).Delete(query.Collection, query.Condition)
_, _, err := adapter.Exec(statement, args, logger)
_, _, err := adapter.Exec(statement, args, loggers...)
return err
}

@@ -131,7 +131,7 @@ func (adapter *Adapter) Rollback() error {
}

// Query performs query operation.
func (adapter *Adapter) Query(out interface{}, statement string, args []interface{}, logger grimoire.Logger) (int64, error) {
func (adapter *Adapter) Query(out interface{}, statement string, args []interface{}, loggers ...grimoire.Logger) (int64, error) {
var rows *sql.Rows
var err error

@@ -141,7 +141,7 @@ func (adapter *Adapter) Query(out interface{}, statement string, args []interfac
} else {
rows, err = adapter.DB.Query(statement, args...)
}
logger(statement, time.Since(start), err)
go grimoire.Log(loggers, statement, time.Since(start), err)

if err != nil {
return 0, adapter.ErrorFunc(err)
@@ -153,7 +153,7 @@ func (adapter *Adapter) Query(out interface{}, statement string, args []interfac
}

// Exec performs exec operation.
func (adapter *Adapter) Exec(statement string, args []interface{}, logger grimoire.Logger) (int64, int64, error) {
func (adapter *Adapter) Exec(statement string, args []interface{}, loggers ...grimoire.Logger) (int64, int64, error) {
var res sql.Result
var err error

@@ -163,7 +163,7 @@ func (adapter *Adapter) Exec(statement string, args []interface{}, logger grimoi
} else {
res, err = adapter.DB.Exec(statement, args...)
}
logger(statement, time.Since(start), err)
go grimoire.Log(loggers, statement, time.Since(start), err)

if err != nil {
return 0, 0, adapter.ErrorFunc(err)
@@ -3,7 +3,6 @@ package sql
import (
db "database/sql"
"testing"
"time"

paranoid "github.com/Fs02/go-paranoid"
"github.com/Fs02/grimoire"
@@ -13,8 +12,6 @@ import (
"github.com/stretchr/testify/assert"
)

func logger(string, time.Duration, error) {}

func open() (*Adapter, error) {
var err error
adapter := &Adapter{
@@ -30,7 +27,7 @@ func open() (*Adapter, error) {
_, _, execerr := adapter.Exec(`CREATE TABLE test (
id INTEGER PRIMARY KEY,
name STRING
);`, []interface{}{}, logger)
);`, nil)
paranoid.Panic(execerr)

return adapter, err
@@ -149,7 +146,7 @@ func TestAdapterInsertAllError(t *testing.T) {
{"notexist": "13"},
}

_, err = adapter.InsertAll(grimoire.Repo{}.From("test"), fields, allchanges, logger)
_, err = adapter.InsertAll(grimoire.Repo{}.From("test"), fields, allchanges)

assert.NotNil(t, err)
}
@@ -183,7 +180,7 @@ func TestAdapterQueryError(t *testing.T) {

out := struct{}{}

_, err = adapter.Query(&out, "error", []interface{}{}, logger)
_, err = adapter.Query(&out, "error", nil)
assert.NotNil(t, err)
}

@@ -194,6 +191,6 @@ func TestAdapterExecError(t *testing.T) {
}
defer adapter.Close()

_, _, err = adapter.Exec("error", []interface{}{}, logger)
_, _, err = adapter.Exec("error", nil)
assert.NotNil(t, err)
}
Oops, something went wrong.

0 comments on commit e170eb2

Please sign in to comment.