From 0c5ed9573fec93cbf31e4778c981c61d7decdf4a Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 17 Dec 2020 21:31:11 +0700 Subject: sql: change the new client function parameter into struct of options While at it, rename the function from "New" to "NewClient" for clarity. --- lib/sql/client.go | 15 ++++++++++----- lib/sql/client_options.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 lib/sql/client_options.go (limited to 'lib/sql') diff --git a/lib/sql/client.go b/lib/sql/client.go index 819d2c3b..ac88cb23 100644 --- a/lib/sql/client.go +++ b/lib/sql/client.go @@ -27,17 +27,22 @@ const ( // type Client struct { *sql.DB - DriverName string + ClientOptions TableNames []string // List of tables in database. } // -// New wrap a database client to provide additional methods. +// NewClient create and initialize new database client. // -func New(driverName string, db *sql.DB) (cl *Client, err error) { +func NewClient(opts ClientOptions) (cl *Client, err error) { + db, err := sql.Open(opts.DriverName, opts.DSN) + if err != nil { + return nil, fmt.Errorf("sql.NewClient: %w", err) + } + cl = &Client{ - DB: db, - DriverName: driverName, + DB: db, + ClientOptions: opts, } return cl, nil diff --git a/lib/sql/client_options.go b/lib/sql/client_options.go new file mode 100644 index 00000000..0214f78c --- /dev/null +++ b/lib/sql/client_options.go @@ -0,0 +1,15 @@ +// Copyright 2020, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package sql + +// +// ClientOptions contains options to connect to database server, including the +// migration directory. +// +type ClientOptions struct { + DriverName string + DSN string + MigrationDir string +} -- cgit v1.3-6-g1900