This repository has been archived by the owner on Mar 24, 2020. It is now read-only.
/
provider.go
78 lines (66 loc) · 1.8 KB
/
provider.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
package provider
import (
"errors"
"github.com/jinzhu/gorm"
"github.com/krostar/nebulo-golib/log"
"github.com/krostar/nebulo-golib/provider/orm"
)
// DefaultConfig is the configuration needed for running every database
type DefaultConfig struct {
CreateTablesIfNotExists bool `json:"-"`
DropTablesIfExists bool `json:"-"`
}
// SQLiteConfig is the configuration needed for running an SQLite database
type SQLiteConfig struct {
DefaultConfig
Filepath string `json:"file" validate:"file=omitempty+readable:createifmissing"`
}
// MySQLConfig is the configuration needed for running an MySQL database
type MySQLConfig struct {
DefaultConfig
Username string `json:"username"`
Password string `json:"password"`
Address string `json:"address"`
Database string `json:"database"`
}
// DatabaseType represent a database type
type DatabaseType int
const (
// Unknown represent a non-handled database
Unknown DatabaseType = iota
// SQLite represent a SQLite database
SQLite
// MySQL represent a MySQL database
MySQL
)
// TablesManagement contains all the required tables needed
// to manage tables
type TablesManagement interface {
CreateTables() (err error)
DropTables() (err error)
CreateIndexes() (err error)
}
// RootProvider contains all the methods and properties needed
// to manage a database
type RootProvider struct {
Type DatabaseType
DB *gorm.DB
Config interface{}
}
var (
// RP is the selected database
RP *RootProvider
// ErrRPIsNil is the error throwed when RP is nil
ErrRPIsNil = errors.New("database is nil")
)
// InitializeDatabase define the user table properties
func InitializeDatabase(p *RootProvider) {
// in debug mode we want to see the sql requests
if log.Verbosity == log.DEBUG {
p.DB.SetLogger(&orm.Logger{})
p.DB.LogMode(true)
} else {
p.DB.LogMode(false)
}
RP = p
}