mirror of https://github.com/VinGarcia/ksql.git
Improve the signature of the New() function so it`s more readable
parent
d91c7cfcfd
commit
b38a83ff36
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
47
kiss_orm.go
47
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 {
|
||||
|
|
|
@ -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{
|
||||
|
|
Loading…
Reference in New Issue