-
Notifications
You must be signed in to change notification settings - Fork 82
/
config.go
75 lines (62 loc) · 1.58 KB
/
config.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
package gorm
import (
"errors"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/database/support"
"github.com/goravel/framework/facades"
)
func config(connection string) (gorm.Dialector, error) {
driver := facades.Config.GetString(fmt.Sprintf("database.connections.%s.driver", connection))
switch orm.Driver(driver) {
case orm.DriverMysql:
return mysqlConfig(connection), nil
case orm.DriverPostgresql:
return postgresqlConfig(connection), nil
case orm.DriverSqlite:
return sqliteConfig(connection), nil
case orm.DriverSqlserver:
return sqlserverConfig(connection), nil
default:
return nil, errors.New(fmt.Sprintf("err database driver: %s, only support mysql, postgresql, sqlite and sqlserver", driver))
}
}
func mysqlConfig(connection string) gorm.Dialector {
dsn := support.GetMysqlDsn(connection)
if dsn == "" {
return nil
}
return mysql.New(mysql.Config{
DSN: dsn,
})
}
func postgresqlConfig(connection string) gorm.Dialector {
dsn := support.GetPostgresqlDsn(connection)
if dsn == "" {
return nil
}
return postgres.New(postgres.Config{
DSN: dsn,
})
}
func sqliteConfig(connection string) gorm.Dialector {
dsn := support.GetSqliteDsn(connection)
if dsn == "" {
return nil
}
return sqlite.Open(dsn)
}
func sqlserverConfig(connection string) gorm.Dialector {
dsn := support.GetSqlserverDsn(connection)
if dsn == "" {
return nil
}
return sqlserver.New(sqlserver.Config{
DSN: dsn,
})
}