-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_sqlite.go
More file actions
116 lines (96 loc) · 2.62 KB
/
storage_sqlite.go
File metadata and controls
116 lines (96 loc) · 2.62 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
package module
import (
"context"
"database/sql"
"fmt"
"os"
"path/filepath"
"github.com/GoCodeAlone/modular"
_ "modernc.org/sqlite"
)
// SQLiteStorage is a module that provides a SQLite database connection as a
// service. Other modules can depend on it for local SQL storage.
type SQLiteStorage struct {
name string
dbPath string
maxConnections int
walMode bool
db *sql.DB
logger modular.Logger
}
// NewSQLiteStorage creates a new SQLite storage module.
func NewSQLiteStorage(name, dbPath string) *SQLiteStorage {
return &SQLiteStorage{
name: name,
dbPath: dbPath,
maxConnections: 1,
walMode: true,
logger: &noopLogger{},
}
}
// SetMaxConnections sets the maximum number of database connections.
func (s *SQLiteStorage) SetMaxConnections(n int) {
if n > 0 {
s.maxConnections = n
}
}
// SetWALMode enables or disables WAL journal mode.
func (s *SQLiteStorage) SetWALMode(enabled bool) {
s.walMode = enabled
}
func (s *SQLiteStorage) Name() string { return s.name }
func (s *SQLiteStorage) Init(app modular.Application) error {
s.logger = app.Logger()
return nil
}
// Start opens the SQLite database connection.
// Idempotent: if the DB is already open, this is a no-op.
func (s *SQLiteStorage) Start(_ context.Context) error {
if s.db != nil {
return nil
}
dir := filepath.Dir(s.dbPath)
if err := os.MkdirAll(dir, 0o750); err != nil {
return fmt.Errorf("create data directory %s: %w", dir, err)
}
dsn := s.dbPath
if s.walMode {
dsn += "?_journal_mode=WAL&_busy_timeout=5000"
}
db, err := sql.Open("sqlite", dsn)
if err != nil {
return fmt.Errorf("open sqlite %s: %w", s.dbPath, err)
}
db.SetMaxOpenConns(s.maxConnections)
if _, err := db.Exec("PRAGMA foreign_keys = ON"); err != nil {
db.Close()
return fmt.Errorf("enable foreign keys: %w", err)
}
s.db = db
s.logger.Info("SQLite storage started", "path", s.dbPath)
return nil
}
// Stop closes the database connection.
func (s *SQLiteStorage) Stop(_ context.Context) error {
if s.db != nil {
s.logger.Info("SQLite storage stopped")
return s.db.Close()
}
return nil
}
// DB returns the underlying *sql.DB connection.
func (s *SQLiteStorage) DB() *sql.DB {
return s.db
}
// DriverName returns "sqlite3" for placeholder normalization.
func (s *SQLiteStorage) DriverName() string {
return "sqlite3"
}
func (s *SQLiteStorage) ProvidesServices() []modular.ServiceProvider {
return []modular.ServiceProvider{
{Name: s.name, Description: "SQLite database connection", Instance: s},
}
}
func (s *SQLiteStorage) RequiresServices() []modular.ServiceDependency {
return nil
}