From b38a83ff363acb26c933e24ac62f9e13ad247f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Garcia?= Date: Thu, 4 Mar 2021 22:58:27 -0300 Subject: [PATCH] Improve the signature of the New() function so it`s more readable --- benchmark_test.go | 10 +++++++-- examples/crud/crud.go | 5 ++++- kiss_orm.go | 47 ++++++++++++++++++++++++++++++++++--------- kiss_orm_test.go | 5 ++++- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/benchmark_test.go b/benchmark_test.go index 3ce4f42..ffa66e3 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -18,7 +18,10 @@ func BenchmarkInsert(b *testing.B) { driver := "postgres" connStr := "host=localhost port=5432 user=postgres password=postgres dbname=kissorm sslmode=disable" - kissormDB, err := kissorm.New(driver, connStr, 1, "users") + kissormDB, err := kissorm.New(driver, connStr, kissorm.Config{ + MaxOpenConns: 1, + TableName: "users", + }) if err != nil { b.FailNow() } @@ -87,7 +90,10 @@ func BenchmarkQuery(b *testing.B) { driver := "postgres" connStr := "host=localhost port=5432 user=postgres password=postgres dbname=kissorm sslmode=disable" - kissormDB, err := kissorm.New(driver, connStr, 1, "users") + kissormDB, err := kissorm.New(driver, connStr, kissorm.Config{ + MaxOpenConns: 1, + TableName: "users", + }) if err != nil { b.FailNow() } diff --git a/examples/crud/crud.go b/examples/crud/crud.go index 9cec059..0eb740f 100644 --- a/examples/crud/crud.go +++ b/examples/crud/crud.go @@ -35,7 +35,10 @@ type Address struct { func main() { ctx := context.Background() - db, err := kissorm.New("sqlite3", "/tmp/hello.sqlite", 1, "users") + db, err := kissorm.New("sqlite3", "/tmp/hello.sqlite", kissorm.Config{ + MaxOpenConns: 1, + TableName: "users", + }) if err != nil { panic(err.Error()) } diff --git a/kiss_orm.go b/kiss_orm.go index 678e1fc..27b66b4 100644 --- a/kiss_orm.go +++ b/kiss_orm.go @@ -40,13 +40,26 @@ const ( insertWithNoIDRetrieval ) +// Config describes the optional arguments accepted +// by the kissorm.New() function. +type Config struct { + // MaxOpenCons defaults to 1 if not set + MaxOpenConns int + + // TableName must be set in order to use the Insert, Delete and Update helper + // functions. If you only intend to make queries or to use the Exec function + // it is safe to leave this field unset. + TableName string + + // IDColumns defaults to []string{"id"} if unset + IDColumns []string +} + // New instantiates a new Kissorm client func New( dbDriver string, connectionString string, - maxOpenConns int, - tableName string, - idCols ...string, + config Config, ) (DB, error) { db, err := sql.Open(dbDriver, connectionString) if err != nil { @@ -56,22 +69,26 @@ func New( return DB{}, err } - db.SetMaxOpenConns(maxOpenConns) + if config.MaxOpenConns == 0 { + config.MaxOpenConns = 1 + } + + db.SetMaxOpenConns(config.MaxOpenConns) dialect := getDriverDialect(dbDriver) if dialect == nil { return DB{}, fmt.Errorf("unsupported driver `%s`", dbDriver) } - if len(idCols) == 0 { - idCols = append(idCols, "id") + if len(config.IDColumns) == 0 { + config.IDColumns = []string{"id"} } var insertMethod insertMethod switch dbDriver { case "sqlite3": insertMethod = insertWithLastInsertID - if len(idCols) > 1 { + if len(config.IDColumns) > 1 { insertMethod = insertWithNoIDRetrieval } case "postgres": @@ -84,9 +101,9 @@ func New( dialect: dialect, driver: dbDriver, db: db, - tableName: tableName, + tableName: config.TableName, - idCols: idCols, + idCols: config.IDColumns, insertMethod: insertMethod, }, nil } @@ -315,6 +332,10 @@ func (c DB) Insert( ctx context.Context, records ...interface{}, ) error { + if c.tableName == "" { + return fmt.Errorf("the optional TableName argument was not provided to New(), can't use the Insert method") + } + for _, record := range records { query, params, err := buildInsertQuery(c.dialect, c.tableName, record, c.idCols...) if err != nil { @@ -460,6 +481,10 @@ func (c DB) Delete( ctx context.Context, ids ...interface{}, ) error { + if c.tableName == "" { + return fmt.Errorf("the optional TableName argument was not provided to New(), can't use the Delete method") + } + if len(ids) == 0 { return nil } @@ -528,6 +553,10 @@ func (c DB) Update( ctx context.Context, records ...interface{}, ) error { + if c.tableName == "" { + return fmt.Errorf("the optional TableName argument was not provided to New(), can't use the Update method") + } + for _, record := range records { query, params, err := buildUpdateQuery(c.dialect, c.tableName, record, c.idCols...) if err != nil { diff --git a/kiss_orm_test.go b/kiss_orm_test.go index 1aaf31e..570085a 100644 --- a/kiss_orm_test.go +++ b/kiss_orm_test.go @@ -373,7 +373,10 @@ func TestInsert(t *testing.T) { ctx := context.Background() // Using columns "id" and "name" as IDs: - c, err := New(driver, connectionString[driver], 1, "users", "id", "name") + c, err := New(driver, connectionString[driver], Config{ + TableName: "users", + IDColumns: []string{"id", "name"}, + }) assert.Equal(t, nil, err) u := User{