aboutsummaryrefslogtreecommitdiff
path: root/lib/sql
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-12-17 21:31:11 +0700
committerShulhan <m.shulhan@gmail.com>2020-12-17 21:31:11 +0700
commit0c5ed9573fec93cbf31e4778c981c61d7decdf4a (patch)
tree6b48cb9215a8dbe7a67303c208260c93eb7f4fa6 /lib/sql
parenta4ed4ab1df3b20d922315d1e821478bdcacefc5b (diff)
downloadpakakeh.go-0c5ed9573fec93cbf31e4778c981c61d7decdf4a.tar.xz
sql: change the new client function parameter into struct of options
While at it, rename the function from "New" to "NewClient" for clarity.
Diffstat (limited to 'lib/sql')
-rw-r--r--lib/sql/client.go15
-rw-r--r--lib/sql/client_options.go15
2 files changed, 25 insertions, 5 deletions
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 <ms@kilabit.info>. 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
+}