generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sql.go
408 lines (353 loc) · 12.3 KB
/
sql.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package datastore
import (
"fmt"
"log"
"os"
"time"
"github.com/mrz1836/go-datastore/nrgorm"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
glogger "gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"gorm.io/plugin/dbresolver"
)
/*
// Load the NewRelic capable drivers
// _ "github.com/newrelic/go-agent/v3/integrations/nrmysql"
// _ "github.com/newrelic/go-agent/v3/integrations/nrpgx"
// _ "github.com/newrelic/go-agent/v3/integrations/nrsqlite3"
*/
// SQL related default settings
// todo: make this configurable for the end-user?
const (
defaultDatetimePrecision = true // disable datetime precision, which not supported before MySQL 5.6
defaultDontSupportRenameColumn = true // `change` when rename column, rename column not supported before MySQL 8, MariaDB
defaultDontSupportRenameIndex = true // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
defaultFieldStringSize uint = 256 // default size for string fields
dsnDefault = "file::memory:" // DSN for connection (file or memory, default is memory)
defaultPreparedStatements = false // Flag for prepared statements for SQL
)
// openSQLDatabase will open a new SQL database
func openSQLDatabase(optionalLogger glogger.Interface, configs ...*SQLConfig) (db *gorm.DB, err error) {
// Try to find a source
var sourceConfig *SQLConfig
if sourceConfig, configs = getSourceDatabase(configs); sourceConfig == nil {
return nil, ErrNoSourceFound
}
// Not a valid driver?
if sourceConfig.Driver != MySQL.String() && sourceConfig.Driver != PostgreSQL.String() {
return nil, ErrUnsupportedDriver
}
// Switch on driver
sourceDialector := getDialector(sourceConfig)
// Create a new source connection
// todo: make this configurable? (PrepareStmt)
if db, err = gorm.Open(
sourceDialector, getGormConfig(
sourceConfig.TablePrefix, defaultPreparedStatements,
sourceConfig.Debug, optionalLogger,
),
); err != nil {
return
}
// Start the resolver (default is source and replica are the same)
resolverConfig := dbresolver.Config{
Policy: dbresolver.RandomPolicy{},
Replicas: []gorm.Dialector{sourceDialector},
Sources: []gorm.Dialector{sourceDialector},
}
// Do we have additional
if len(configs) > 0 {
// Clear the existing replica
resolverConfig.Replicas = nil
// Loop configs
for _, config := range configs {
// Get the dialector
dialector := getDialector(config)
// Set based on replica
if config.Replica {
resolverConfig.Replicas = append(resolverConfig.Replicas, dialector)
} else {
resolverConfig.Sources = append(resolverConfig.Sources, dialector)
}
}
// No replica?
if len(resolverConfig.Replicas) == 0 {
resolverConfig.Replicas = append(resolverConfig.Replicas, sourceDialector)
}
}
// Create the register and set the configuration
//
// See: https://gorm.io/docs/dbresolver.html
// var register *dbresolver.DBResolver
register := new(dbresolver.DBResolver)
register.Register(resolverConfig)
if sourceConfig.MaxConnectionIdleTime.String() != emptyTimeDuration {
register = register.SetConnMaxIdleTime(sourceConfig.MaxConnectionIdleTime)
}
if sourceConfig.MaxConnectionTime.String() != emptyTimeDuration {
register = register.SetConnMaxLifetime(sourceConfig.MaxConnectionTime)
}
if sourceConfig.MaxOpenConnections > 0 {
register = register.SetMaxOpenConns(sourceConfig.MaxOpenConnections)
}
if sourceConfig.MaxIdleConnections > 0 {
register = register.SetMaxIdleConns(sourceConfig.MaxIdleConnections)
}
// Use the register
if err = db.Use(register); err != nil {
return
}
// Register the callbacks with NewRelic
nrgorm.AddGormCallbacks(db)
// Return the connection
return
}
// openSQLiteDatabase will open a SQLite database connection
func openSQLiteDatabase(optionalLogger glogger.Interface, config *SQLiteConfig) (db *gorm.DB, err error) {
// Check for an existing connection
var dialector gorm.Dialector
if config.ExistingConnection != nil {
dialector = sqlite.Dialector{Conn: config.ExistingConnection}
} else {
dialector = sqlite.Open(getDNS(config.DatabasePath, config.Shared))
}
/*
// todo: implement this functionality (name spaced in-memory tables)
NOTE: https://www.sqlite.org/inmemorydb.html
If two or more distinct but shareable in-memory databases are needed in a single process, then the mode=memory
query parameter can be used with a URI filename to create a named in-memory database:
rc = sqlite3_open("file:memdb1?mode=memory&cache=shared", &db);
*/
// Create a new connection
if db, err = gorm.Open(
dialector, getGormConfig(
config.TablePrefix, defaultPreparedStatements,
config.Debug, optionalLogger,
),
); err != nil {
return
}
// @mrz: turned off, unsure if it's really needed or not
// Get the SQL DB
// var sqlDB *sql.DB
// sqlDB, err = db.DB()
// sqlDB.SetMaxIdleConns(config.MaxIdleConnections)
// sqlDB.SetMaxOpenConns(config.MaxOpenConnections)
// sqlDB.SetConnMaxLifetime(config.MaxConnectionTime)
// sqlDB.SetConnMaxIdleTime(config.MaxConnectionIdleTime)
// Register the callbacks with NewRelic
nrgorm.AddGormCallbacks(db)
// Return the connection
return
}
// getDNS will return the DNS string
func getDNS(databasePath string, shared bool) (dsn string) {
// Use a file based path?
if len(databasePath) > 0 {
dsn = databasePath
} else { // Default is in-memory
dsn = dsnDefault
}
// Shared?
if shared {
dsn += "?cache=shared"
}
return
}
// getDialector will return a new gorm.Dialector based on driver
func getDialector(config *SQLConfig) gorm.Dialector {
if config.Driver == MySQL.String() {
return mySQLDialector(config)
}
return postgreSQLDialector(config)
}
// mySQLDialector will return a gorm.Dialector
func mySQLDialector(config *SQLConfig) gorm.Dialector {
// Create the default MySQL configuration
cfg := mysql.Config{
// DriverName: "nrmysql",
// todo: make all params customizable via config
DSN: config.User + ":" + config.Password +
"@tcp(" + config.Host + ":" + config.Port + ")/" +
config.Name + "?charset=utf8&parseTime=True&loc=Local", // data source name (connection string)
DefaultStringSize: defaultFieldStringSize, // default size for string fields
DisableDatetimePrecision: defaultDatetimePrecision, // disable datetime precision, which not supported before MySQL 5.6
DontSupportRenameIndex: defaultDontSupportRenameIndex, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
DontSupportRenameColumn: defaultDontSupportRenameColumn, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
SkipInitializeWithVersion: config.SkipInitializeWithVersion, // autoconfigure based on currently MySQL version
}
// Do we have an existing connection
if config.ExistingConnection != nil {
cfg.DSN = ""
cfg.Conn = config.ExistingConnection
}
return mysql.New(cfg)
}
// postgreSQLDialector will return a gorm.Dialector
func postgreSQLDialector(config *SQLConfig) gorm.Dialector {
// Create the default PostgreSQL configuration
cfg := postgres.Config{
// DriverName: "nrpgx",
PreferSimpleProtocol: true, // turn to TRUE to disable implicit prepared statement usage
WithoutReturning: false,
}
// Do we have an existing connection
if config.ExistingConnection != nil {
cfg.Conn = config.ExistingConnection
} else {
cfg.DSN = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s",
config.Host, config.User, config.Password, config.Name, config.Port, config.SslMode, config.TimeZone)
}
return postgres.New(cfg)
}
// getSourceDatabase will loop all configs and get the first source
//
// todo: this will grab ANY source (create a better way to seed the source database)
func getSourceDatabase(configs []*SQLConfig) (*SQLConfig, []*SQLConfig) {
for index, config := range configs {
if !config.Replica {
if len(configs) > 1 {
var processed []*SQLConfig
for i, c := range configs {
if i != index {
processed = append(processed, c)
}
}
return configs[index], processed
}
return configs[index], nil
}
}
return nil, configs
}
// getGormSessionConfig returns the gorm session config
func getGormSessionConfig(preparedStatement, debug bool, optionalLogger glogger.Interface) *gorm.Session {
config := &gorm.Session{
AllowGlobalUpdate: false,
CreateBatchSize: 0,
DisableNestedTransaction: false,
DryRun: false,
FullSaveAssociations: false,
Logger: optionalLogger,
NewDB: false,
NowFunc: nil,
PrepareStmt: preparedStatement,
QueryFields: false,
SkipDefaultTransaction: false,
SkipHooks: true,
}
// Optional logger vs basic
if optionalLogger == nil {
logLevel := glogger.Silent
if debug {
logLevel = glogger.Info
}
config.Logger = glogger.New(
log.New(os.Stdout, "\r\n ", log.LstdFlags), // io writer
glogger.Config{
SlowThreshold: 5 * time.Second, // Slow SQL threshold
LogLevel: logLevel, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)
}
return config
}
// getGormConfig will return a valid gorm.Config
//
// See: https://gorm.io/docs/gorm_config.html
func getGormConfig(tablePrefix string, preparedStatement, debug bool, optionalLogger glogger.Interface) *gorm.Config {
// Set the prefix
if len(tablePrefix) > 0 {
tablePrefix = tablePrefix + "_"
}
// Create the configuration
config := &gorm.Config{
AllowGlobalUpdate: false,
ClauseBuilders: nil,
ConnPool: nil,
CreateBatchSize: 0,
Dialector: nil,
DisableAutomaticPing: false,
DisableForeignKeyConstraintWhenMigrating: true,
DisableNestedTransaction: false,
DryRun: false, // toggle for extreme debugging
FullSaveAssociations: false,
Logger: optionalLogger,
NamingStrategy: schema.NamingStrategy{
TablePrefix: tablePrefix, // table name prefix, table for `User` would be `t_users`
SingularTable: false, // use singular table name, table for `User` would be `user` with this option enabled
},
NowFunc: nil,
Plugins: nil,
PrepareStmt: preparedStatement, // default is: false
QueryFields: false,
SkipDefaultTransaction: false,
}
// Optional logger vs basic
if optionalLogger == nil {
logLevel := glogger.Silent
if debug {
logLevel = glogger.Info
}
config.Logger = glogger.New(
log.New(os.Stdout, "\r\n ", log.LstdFlags), // io writer
glogger.Config{
SlowThreshold: 5 * time.Second, // Slow SQL threshold
LogLevel: logLevel, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)
}
return config
}
// closeSQLDatabase will close an SQL connection safely
func closeSQLDatabase(gormDB *gorm.DB) error {
if gormDB == nil {
return nil
}
sqlDB, err := gormDB.DB()
if err != nil {
return err
}
return sqlDB.Close()
}
// sqlDefaults will set the default values if missing
func (s *SQLConfig) sqlDefaults(engine Engine) *SQLConfig {
// Set the default(s)
if s.TxTimeout.String() == emptyTimeDuration {
s.TxTimeout = defaultDatabaseTxTimeout
}
if s.MaxConnectionTime.String() == emptyTimeDuration {
s.MaxConnectionTime = defaultDatabaseMaxTimeout
}
if s.MaxConnectionIdleTime.String() == emptyTimeDuration {
s.MaxConnectionIdleTime = defaultDatabaseMaxIdleTime
}
if len(s.Port) == 0 {
if engine == MySQL {
s.Port = defaultMySQLPort
} else {
s.Port = defaultPostgreSQLPort
}
}
if len(s.Host) == 0 {
if engine == MySQL {
s.Host = defaultMySQLHost
} else {
s.Host = defaultPostgreSQLHost
}
}
if len(s.TimeZone) == 0 {
s.TimeZone = defaultTimeZone
}
if len(s.SslMode) == 0 {
s.SslMode = defaultPostgreSQLSslMode
}
return s
}