/
database.go
74 lines (66 loc) · 1.63 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
package client
import (
"fmt"
error2 "github.com/airstrik/gobase/pkg/server/error"
"github.com/airstrik/gobase/pkg/utils"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"log"
"time"
)
type Database struct {
Dialect string
userName string
password string
host string
port string
database string
DB *gorm.DB
}
func (db *Database) getArgs() string {
if db.database == "" {
db.database = "account"
}
switch db.Dialect {
case "postgres":
con := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", db.host, db.port,
db.userName, db.password, db.database)
return con
default:
return ""
}
}
func (db *Database) CreateDB(database string) {
_ = db.DB.Exec("CREATE DATABASE " + database)
log.Print("Create new Database for account ")
}
func (db *Database) Create(v ...interface{}) {
db.DB.Create(v)
db.DB.Model(user).Update("CreatedAt", time.Now())
db.DB.Model(user).Update("UpdatedAt", time.Now())
log.Print("Create new Database for account ")
}
func LoadBaseDatabase() *Database {
if BaseDB == nil {
log.Print("Creating the Database connection from the Application server")
BaseDB = LoadDatabase("account")
}
return BaseDB
}
func LoadDatabase(database string) *Database {
_db := &Database{
Dialect: dialect,
userName: user,
password: password,
host: host,
port: port,
database: database,
}
var DbConnection *gorm.DB
DbConnection , err = gorm.Open(dialect, _db.getArgs())
_db.DB = DbConnection.Begin()
utils.HandleCustomError(err, error2.TenantNotFound)
return _db
}