-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
523 lines (462 loc) · 13.3 KB
/
db.go
File metadata and controls
523 lines (462 loc) · 13.3 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package db
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/gomodul/db/builder"
"github.com/gomodul/db/cache"
"github.com/gomodul/db/dialect"
"github.com/gomodul/db/internal/security"
"github.com/gomodul/db/logger"
"github.com/gomodul/db/metrics"
"github.com/gomodul/db/pool"
"github.com/gomodul/db/query"
)
// DB is the main database handle. It wraps a driver connection and provides
// a fluent API for building and executing queries.
//
// Example:
//
// db, err := db.Open(db.Config{DSN: "postgres://user:pass@localhost:5432/mydb"})
// defer db.Close()
//
// // Use the universal driver
// users, err := db.Model(&User{}).Where("status = ?", "active").Find(&users)
type DB struct {
*Config
Driver dialect.Driver // universal driver
Caps *dialect.Capabilities
Logger logger.Logger
QueryCache *cache.QueryCache
PoolMonitor *pool.Monitor
Metrics metrics.Collector
Error error
RowsAffected int64
Statement *Statement
clone int
}
// GetDriver returns the active driver.
func (db *DB) GetDriver() dialect.Driver {
return db.Driver
}
// UseDriver sets the driver and updates capabilities.
func (db *DB) UseDriver(driver dialect.Driver) {
db.Driver = driver
db.Caps = driver.Capabilities()
}
// sqlDB returns the underlying *sql.DB when the driver implements dialect.SQLAccessor.
func (db *DB) sqlDB() *sql.DB {
if a, ok := db.Driver.(dialect.SQLAccessor); ok {
return a.UnderlyingSQL()
}
return nil
}
// Open opens a database connection using the registered driver for the given DSN.
//
// Drivers must be imported for their side effects to register themselves:
//
// import _ "github.com/gomodul/db/driver/postgres"
func Open(cfg Config) (*DB, error) {
// Auto-detect driver from DSN
drivers, err := GetDriverFromConfig(cfg)
if err != nil {
return nil, err
}
// Convert driver config
driverCfg := &dialect.Config{
DSN: cfg.DSN,
MaxOpenConns: cfg.MaxOpenConns,
MaxIdleConns: cfg.MaxIdleConns,
ConnMaxLifetime: cfg.ConnMaxLifetime,
ConnMaxIdleTime: cfg.ConnMaxIdleTime,
RetryMaxRetries: cfg.RetryMaxRetries,
RetryBaseDelay: cfg.RetryBaseDelay,
RetryMaxDelay: cfg.RetryMaxDelay,
}
// Initialize the driver
if err := drivers.Initialize(driverCfg); err != nil {
return nil, fmt.Errorf("failed to initialize database: %w", err)
}
db := &DB{
Config: &cfg,
}
// Initialize logger
if cfg.Logger != nil {
db.Logger = cfg.Logger
} else if !cfg.DisableLogger && cfg.LoggerConfig != nil {
db.Logger = logger.NewSQLQueryLogger(cfg.LoggerConfig)
} else if !cfg.DisableLogger {
// Use default logger config
db.Logger = logger.NewSQLQueryLogger(logger.DefaultConfig())
} else {
db.Logger = logger.NewNullLogger()
}
// Initialize cache
if !cfg.DisableCache && cfg.Cache != nil {
ttl := cfg.CacheTTL
if ttl == 0 {
ttl = 5 * time.Minute
}
db.QueryCache = cache.NewQueryCache(cfg.Cache, ttl)
}
// Wrap driver with retry if configured
if cfg.RetryMaxRetries > 0 {
baseDelay := cfg.RetryBaseDelay
if baseDelay == 0 {
baseDelay = 100 * time.Millisecond
}
maxDelay := cfg.RetryMaxDelay
if maxDelay == 0 {
maxDelay = time.Second
}
db.Driver = dialect.NewRetryableDriver(drivers, cfg.RetryMaxRetries, baseDelay, maxDelay)
} else {
db.Driver = drivers
}
// Get capabilities
db.Caps = db.Driver.Capabilities()
return db, nil
}
// OpenWithDriver opens a database connection with a specific driver
func OpenWithDriver(drvr dialect.Driver, cfg Config) (*DB, error) {
driverCfg := &dialect.Config{
DSN: cfg.DSN,
MaxOpenConns: cfg.MaxOpenConns,
MaxIdleConns: cfg.MaxIdleConns,
ConnMaxLifetime: cfg.ConnMaxLifetime,
ConnMaxIdleTime: cfg.ConnMaxIdleTime,
RetryMaxRetries: cfg.RetryMaxRetries,
RetryBaseDelay: cfg.RetryBaseDelay,
RetryMaxDelay: cfg.RetryMaxDelay,
}
// Initialize the driver
if err := drvr.Initialize(driverCfg); err != nil {
return nil, fmt.Errorf("failed to initialize database: %w", err)
}
db := &DB{
Config: &cfg,
}
// Wrap driver with retry if configured
if cfg.RetryMaxRetries > 0 {
baseDelay := cfg.RetryBaseDelay
if baseDelay == 0 {
baseDelay = 100 * time.Millisecond
}
maxDelay := cfg.RetryMaxDelay
if maxDelay == 0 {
maxDelay = time.Second
}
db.Driver = dialect.NewRetryableDriver(drvr, cfg.RetryMaxRetries, baseDelay, maxDelay)
} else {
db.Driver = drvr
}
db.Caps = db.Driver.Capabilities()
return db, nil
}
// Close closes the database connection.
func (db *DB) Close() error {
if db.Driver != nil {
return db.Driver.Close()
}
return nil
}
// Session creates a new session for the database.
func (db *DB) Session() *DB {
return &DB{
Config: db.Config,
Driver: db.Driver,
Caps: db.Caps,
clone: 1,
}
}
// DB returns the underlying *sql.DB for SQL drivers that expose it.
func (db *DB) DB() (*sql.DB, error) {
if s := db.sqlDB(); s != nil {
return s, nil
}
return nil, ErrNotSupported
}
// DriverType returns the type of the current driver
func (db *DB) DriverType() dialect.DriverType {
if db.Driver != nil {
return db.Driver.Type()
}
return ""
}
// Capabilities returns the driver's capabilities
func (db *DB) Capabilities() *dialect.Capabilities {
return db.Caps
}
// HasCapability checks if the driver has a specific capability
func (db *DB) HasCapability(feature string) bool {
if db.Caps == nil {
return false
}
return db.Caps.HasFeature(feature)
}
// Ping checks if the database is reachable
func (db *DB) Ping(ctx context.Context) error {
if db.Driver == nil {
return ErrNotSupported
}
// Check if driver implements Conn interface (has Ping method)
if conn, ok := db.Driver.(dialect.Conn); ok {
return conn.Ping(ctx)
}
return ErrNotSupported
}
// Health checks the health of the database connection
func (db *DB) Health() (*dialect.HealthStatus, error) {
if db.Driver == nil {
return dialect.NewUnhealthyStatus("no driver configured"), nil
}
// Use the helper function from dialect package
return dialect.Health(db.Driver)
}
// TransactionContext executes fn within a transaction using the provided context.
func (db *DB) Transaction(ctx context.Context, fn func(tx *DB) error) error {
if db.Driver == nil || !db.Caps.Transaction.Supported {
return ErrNotSupported
}
tx, err := dialect.BeginTx(ctx, db.Driver)
if err != nil {
return err
}
txDB := &DB{
Config: db.Config,
Driver: db.Driver,
Caps: db.Caps,
}
defer func() {
if r := recover(); r != nil {
_ = tx.Rollback()
panic(r)
}
}()
if err := fn(txDB); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
// Migrate runs auto-migration (if supported)
func (db *DB) Migrate(models ...interface{}) error {
if db.Driver == nil {
return ErrNotSupported
}
// Use the helper function from dialect package
migrator := dialect.GetMigrator(db.Driver)
return migrator.AutoMigrate(models...)
}
// Execute executes a universal query
func (db *DB) Execute(ctx context.Context, q *query.Query) (*dialect.Result, error) {
if db.Driver == nil {
return nil, ErrNotSupported
}
return db.Driver.Execute(ctx, q)
}
// Legacy methods (for backward compatibility)
// Exec executes a query without returning any rows.
//
// SECURITY WARNING: Always use parameterized queries.
//
// ✅ GOOD: db.Exec(ctx, "UPDATE users SET name = ? WHERE id = ?", "John", 1)
// ❌ BAD: db.Exec(ctx, "UPDATE users SET name = '" + name + "'")
func (db *DB) Exec(ctx context.Context, sql string, values ...interface{}) *DB {
if warnings, err := security.ValidateRawQuery(sql, nil); err != nil {
db.Error = fmt.Errorf("raw query validation failed: %w", err)
return db
} else if len(warnings) > 0 && db.Logger != nil {
for _, w := range warnings {
db.Logger.Log(ctx, logger.Warn, fmt.Sprintf("raw query security warning [%s]: %s — query: %s", w.Severity, w.Message, sql))
}
}
if db.Driver != nil {
q := &query.Query{
Raw: sql,
RawArgs: values,
IsRaw: true,
}
result, err := db.Driver.Execute(ctx, q)
if err != nil {
db.Error = security.AddSecurityWarningToError(err, sql)
return db
}
db.RowsAffected = result.RowsAffected
}
return db
}
// Raw stores a raw SQL query for deferred execution via Scan.
//
// SECURITY WARNING: Always use parameterized queries with placeholders.
//
// ✅ GOOD: db.Raw("SELECT * FROM users WHERE id = ?", userID).Scan(&users)
// ❌ BAD: db.Raw("SELECT * FROM users WHERE id = " + userID)
func (db *DB) Raw(sql string, values ...interface{}) *DB {
if warnings, err := security.ValidateRawQuery(sql, nil); err != nil {
db.Error = fmt.Errorf("raw query validation failed: %w", err)
return db
} else if len(warnings) > 0 && db.Logger != nil {
for _, w := range warnings {
db.Logger.Log(context.Background(), logger.Warn,
fmt.Sprintf("raw query security warning [%s]: %s — query: %s", w.Severity, w.Message, sql))
}
}
clone := db.Session()
stmt := &Statement{}
stmt.SQL.WriteString(sql)
stmt.Vars = values
clone.Statement = stmt
return clone
}
// Scan executes the raw query stored by Raw and scans results into dest.
// For fluent query building use Model().Where(...).Find(&dest) instead.
func (db *DB) Scan(dest interface{}) *DB {
if db.Statement == nil || db.Statement.SQL.Len() == 0 {
return db
}
rawSQL := db.Statement.SQL.String()
if db.Driver != nil {
q := &query.Query{
Operation: query.OpFind,
Raw: rawSQL,
RawArgs: db.Statement.Vars,
IsRaw: true,
}
result, err := db.Driver.Execute(db.Statement.Context, q)
if err != nil {
db.Error = security.AddSecurityWarningToError(err, rawSQL)
return db
}
if result != nil {
db.RowsAffected = result.RowsAffected
}
}
return db
}
// Model starts a query on a model using the fluent builder API
func (db *DB) Model(value interface{}) *builder.QueryBuilder {
b := &builder.DB{
Dialect: db.Driver,
Caps: db.Caps,
Logger: db.Logger,
}
return builder.New(b, value)
}
// Table starts a query on a specific table using the fluent builder API
func (db *DB) Table(name string) *builder.QueryBuilder {
b := &builder.DB{
Dialect: db.Driver,
Caps: db.Caps,
Logger: db.Logger,
}
return builder.New(b, nil).Table(name)
}
// ============ Connection Pool Monitoring Methods ============
// GetPoolStats returns current connection pool statistics.
// Returns ErrNotSupported for drivers that don't expose a *sql.DB.
func (db *DB) GetPoolStats(ctx context.Context) (*pool.Stats, error) {
if db.PoolMonitor != nil {
return db.PoolMonitor.GetStats(ctx)
}
if s := db.sqlDB(); s != nil {
st := s.Stats()
return &pool.Stats{
OpenConnections: st.OpenConnections,
InUse: st.InUse,
Idle: st.Idle,
WaitCount: st.WaitCount,
WaitDuration: st.WaitDuration,
MaxIdleClosed: st.MaxIdleClosed,
MaxLifetimeClosed: st.MaxLifetimeClosed,
MaxOpenConnections: st.MaxOpenConnections,
Timestamp: time.Now(),
}, nil
}
return nil, ErrNotSupported
}
// GetPoolHealth checks the health of the connection pool.
func (db *DB) GetPoolHealth(ctx context.Context) (*pool.HealthStatus, error) {
if db.PoolMonitor != nil {
return db.PoolMonitor.GetHealthStatus(ctx)
}
if s := db.sqlDB(); s != nil {
st := s.Stats()
status := &pool.HealthStatus{Healthy: true, Timestamp: time.Now()}
if st.MaxOpenConnections > 0 {
usage := float64(st.InUse) / float64(st.MaxOpenConnections)
if usage > 0.9 {
status.Healthy = false
status.Warnings = append(status.Warnings,
fmt.Sprintf("High connection usage: %.2f%%", usage*100))
}
}
return status, nil
}
return nil, ErrNotSupported
}
// EnablePoolMonitoring enables connection pool monitoring for SQL drivers.
func (db *DB) EnablePoolMonitoring(cfg *pool.Config) error {
s := db.sqlDB()
if s == nil {
return ErrNotSupported
}
if cfg == nil {
cfg = pool.DefaultConfig()
cfg.Name = "default"
}
db.PoolMonitor = pool.NewMonitor(s, cfg)
db.Metrics = cfg.Metrics
return nil
}
// DisablePoolMonitoring disables connection pool monitoring.
func (db *DB) DisablePoolMonitoring() {
db.PoolMonitor = nil
}
// GetPoolInfo returns connection pool information.
func (db *DB) GetPoolInfo() *pool.PoolInfo {
if db.PoolMonitor != nil {
return db.PoolMonitor.GetPoolInfo()
}
if s := db.sqlDB(); s != nil {
st := s.Stats()
return &pool.PoolInfo{
Name: "default",
MaxOpenConnections: st.MaxOpenConnections,
CurrentOpen: st.OpenConnections,
InUse: st.InUse,
Idle: st.Idle,
WaitCount: st.WaitCount,
TotalWaitDuration: st.WaitDuration,
}
}
return nil
}
// CollectPoolMetrics collects and records pool metrics
func (db *DB) CollectPoolMetrics(ctx context.Context) error {
if db.PoolMonitor != nil {
_, err := db.PoolMonitor.CollectStats(ctx)
return err
}
return ErrNotSupported
}
// ============ Schema Migration Methods ============
// AutoMigrate automatically creates/updates database schema for the given models.
//
// Example:
//
// err := database.AutoMigrate(&User{}, &Order{}, &Product{})
func (db *DB) AutoMigrate(models ...interface{}) error {
return db.Migrator().AutoMigrate(models...)
}
// Migrator returns a dialect.Migrator for advanced schema operations.
//
// Example:
//
// m := db.Migrator()
// err := m.AutoMigrate(&User{})
// err = m.CreateIndex("users", "idx_email", []string{"email"}, true)
func (db *DB) Migrator() dialect.Migrator {
return dialect.GetMigrator(db.Driver)
}