-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
126 lines (113 loc) · 3 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package config
import (
"encoding/json"
"errors"
"fmt"
"github.com/udovin/gosql"
// Register SQL drivers.
_ "github.com/jackc/pgx/v4/stdlib"
_ "github.com/mattn/go-sqlite3"
)
// DBDriver represents database driver name.
type DBDriver string
const (
// SQLiteDriver represents SQLite driver.
SQLiteDriver DBDriver = "sqlite"
// PostgresDriver represents Postgres driver.
PostgresDriver DBDriver = "postgres"
)
// SQLiteOptions stores SQLite connection options.
type SQLiteOptions struct {
// Path contains path to SQLite database file.
Path string `json:"path"`
}
// PostgresOptions stores Postgres connection options.
type PostgresOptions struct {
// Hosts contains list of hosts.
Hosts []string `json:"hosts"`
// User contains username of user.
User string `json:"user"`
// Password contains password of user.
Password string `json:"password"`
// Name contains name of database.
Name string `json:"name"`
// SSLMode contains sslmode configuration.
SSLMode string `json:"sslmode"`
}
// DB stores configuration for database connection.
type DB struct {
// Options contains options for database driver.
//
// For SQLiteDriver field should contains SQLiteOptions.
// For PostgresDriver field should contains PostgresOptions.
Options any
}
// UnmarshalJSON parses JSON to create appropriate connection configuration.
func (c *DB) UnmarshalJSON(bytes []byte) error {
var cfg struct {
Driver DBDriver `json:"driver"`
Options json.RawMessage `json:"options"`
}
if err := json.Unmarshal(bytes, &cfg); err != nil {
return err
}
switch cfg.Driver {
case SQLiteDriver:
var options SQLiteOptions
if err := json.Unmarshal(cfg.Options, &options); err != nil {
return err
}
c.Options = options
case PostgresDriver:
var options PostgresOptions
if err := json.Unmarshal(cfg.Options, &options); err != nil {
return err
}
c.Options = options
default:
return fmt.Errorf("driver %q is not supported", cfg.Driver)
}
return nil
}
func (c DB) MarshalJSON() ([]byte, error) {
cfg := struct {
Driver DBDriver `json:"driver"`
Options any `json:"options"`
}{
Options: c.Options,
}
switch t := c.Options.(type) {
case SQLiteOptions:
cfg.Driver = SQLiteDriver
case PostgresOptions:
cfg.Driver = PostgresDriver
default:
return nil, fmt.Errorf("options of type %T is not supported", t)
}
return json.Marshal(cfg)
}
func createSQLiteDB(opts SQLiteOptions) (*gosql.DB, error) {
return (gosql.SQLiteConfig{
Path: opts.Path,
}).NewDB()
}
func createPostgresDB(opts PostgresOptions) (*gosql.DB, error) {
return (gosql.PostgresConfig{
Hosts: opts.Hosts,
User: opts.User,
Password: opts.Password,
Name: opts.Name,
SSLMode: opts.SSLMode,
}).NewDB()
}
// Create creates database connection using current configuration.
func (c *DB) Create() (*gosql.DB, error) {
switch v := c.Options.(type) {
case SQLiteOptions:
return createSQLiteDB(v)
case PostgresOptions:
return createPostgresDB(v)
default:
return nil, errors.New("unsupported database config type")
}
}