-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.go
50 lines (43 loc) · 995 Bytes
/
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
package olapsql
import (
"fmt"
"github.com/awatercolorpen/olap-sql/api/types"
"gorm.io/driver/clickhouse"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type DBOption struct {
Debug bool `json:"debug"`
DSN string `json:"dsn"`
Type types.DBType `json:"type"`
}
func (o *DBOption) NewDB() (*gorm.DB, error) {
dialect, err := getDialect(o.Type, o.DSN)
if err != nil {
return nil, err
}
db, err := gorm.Open(dialect, &gorm.Config{})
if err != nil {
return nil, err
}
if o.Debug {
db = db.Debug()
}
return db, nil
}
func getDialect(ty types.DBType, dsn string) (gorm.Dialector, error) {
switch ty {
case types.DBTypeSQLite:
return sqlite.Open(dsn), nil
case types.DBTypeMySQL:
return mysql.Open(dsn), nil
case types.DBTypePostgre:
return postgres.Open(dsn), nil
case types.DBTypeClickHouse:
return clickhouse.Open(dsn), nil
default:
return nil, fmt.Errorf("unsupported db type: %v", ty)
}
}