forked from AnalogRepublic/go-mysql-queue
/
connection.go
160 lines (124 loc) · 2.77 KB
/
connection.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package msq
import (
"errors"
"fmt"
"strconv"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type ConnectionConfig struct {
Type string
Host string
Proto string
Port int
Username string
Password string
Database string
Charset string
Locale string
Logging bool
}
type Connection struct {
db *gorm.DB
Config ConnectionConfig
}
func (c *Connection) Attempt() error {
if c.Config.Locale == "" {
c.Config.Locale = "Local"
}
if c.Config.Charset == "" {
c.Config.Charset = "utf8mb4"
}
if c.Config.Proto == "" {
c.Config.Proto = "tcp"
}
connectionString := c.getConnectionString()
if c.Config.Logging {
fmt.Println("Connecting to " + connectionString)
}
db, err := gorm.Open(c.getType(), connectionString)
if err != nil {
return err
}
db.LogMode(c.Config.Logging)
c.db = db
return nil
}
func (c *Connection) Close() error {
return c.db.Close()
}
func (c *Connection) Database() *gorm.DB {
return c.db
}
func (c *Connection) SetupDatabase() error {
dbScope := c.db
if c.getType() != "sqlite3" {
tableOptions := fmt.Sprintf("ENGINE=InnoDB DEFAULT CHARSET=%s", c.Config.Charset)
dbScope = dbScope.Set("gorm:table_options", tableOptions)
}
dbScope = dbScope.AutoMigrate(&MessengerMessage{})
hasTable := dbScope.HasTable(&MessengerMessage{})
if !hasTable {
return errors.New("Events table was not created")
}
return nil
}
func (c *Connection) getType() string {
if c.Config.Type == "sqlite" {
return "sqlite3"
}
if c.Config.Type == "postgres" {
return "postgres"
}
return c.Config.Type
}
func (c *Connection) getConnectionString() string {
dbType := c.getType()
port := strconv.Itoa(c.Config.Port)
hostname := c.Config.Host
if port != "" && port != "0" {
hostname = fmt.Sprintf("%s:%s", c.Config.Host, port)
}
if dbType == "mysql" {
return fmt.Sprintf(
"%s:%s@%s(%s)/%s?charset=%s&parseTime=True&loc=%s",
c.Config.Username,
c.Config.Password,
c.Config.Proto,
hostname,
c.Config.Database,
c.Config.Charset,
c.Config.Locale,
)
} else if dbType == "sqlite3" {
return c.Config.Database
} else if dbType == "postgres" {
return fmt.Sprintf(
"postgres://%s:%s@%s/%s?sslmode=disable",
c.Config.Username,
c.Config.Password,
hostname,
c.Config.Database,
)
}
panic(
fmt.Sprintf(
"Invalid database type \"%s\" provided, must be 'postgres' or 'myqsl' or 'sqlite3'/'sqlite'",
dbType))
}
func Connect(config ConnectionConfig) (*Queue, error) {
connection := &Connection{
Config: config,
}
err := connection.Attempt()
if err != nil {
return &Queue{}, err
}
connection.SetupDatabase()
queue := &Queue{
Connection: connection,
}
return queue, nil
}