|
22 | 22 |
|
23 | 23 | package sqlite
|
24 | 24 |
|
25 |
| -import ( |
26 |
| - "bytes" |
27 |
| - "fmt" |
28 |
| - "strings" |
29 |
| - |
30 |
| - "github.com/iancoleman/strcase" |
31 |
| - "github.com/jmoiron/sqlx" |
32 |
| - |
33 |
| - "github.com/uber/cadence/common/config" |
34 |
| - "github.com/uber/cadence/common/persistence/sql/sqldriver" |
35 |
| - "github.com/uber/cadence/common/persistence/sql/sqlplugin" |
36 |
| -) |
37 |
| - |
38 | 25 | const (
|
39 | 26 | PluginName = "sqlite"
|
40 | 27 | )
|
41 |
| - |
42 |
| -// SQLite plugin provides an sql persistence storage implementation for sqlite database |
43 |
| -// Mostly the implementation reuses the mysql implementation |
44 |
| -// The plugin supports only in-memory sqlite database for now |
45 |
| -type plugin struct{} |
46 |
| - |
47 |
| -var _ sqlplugin.Plugin = (*plugin)(nil) |
48 |
| - |
49 |
| -// CreateDB wraps createDB to return an instance of sqlplugin.DB |
50 |
| -func (p *plugin) CreateDB(cfg *config.SQL) (sqlplugin.DB, error) { |
51 |
| - return p.createDB(cfg) |
52 |
| -} |
53 |
| - |
54 |
| -// CreateAdminDB wraps createDB to return an instance of sqlplugin.AdminDB |
55 |
| -func (p *plugin) CreateAdminDB(cfg *config.SQL) (sqlplugin.AdminDB, error) { |
56 |
| - return p.createDB(cfg) |
57 |
| -} |
58 |
| - |
59 |
| -// createDB create a new instance of DB |
60 |
| -func (p *plugin) createDB(cfg *config.SQL) (*DB, error) { |
61 |
| - conns, err := sqldriver.CreateDBConnections(cfg, func(cfg *config.SQL) (*sqlx.DB, error) { |
62 |
| - return p.createSingleDBConn(cfg) |
63 |
| - }) |
64 |
| - if err != nil { |
65 |
| - return nil, err |
66 |
| - } |
67 |
| - return NewDB(conns, nil, sqlplugin.DbShardUndefined, cfg.NumShards) |
68 |
| -} |
69 |
| - |
70 |
| -// createSingleDBConn creates a single database connection for sqlite |
71 |
| -func (p *plugin) createSingleDBConn(cfg *config.SQL) (*sqlx.DB, error) { |
72 |
| - db, err := sqlx.Connect("sqlite3", buildDSN(cfg)) |
73 |
| - if err != nil { |
74 |
| - return nil, err |
75 |
| - } |
76 |
| - |
77 |
| - if cfg.MaxConns > 0 { |
78 |
| - db.SetMaxOpenConns(cfg.MaxConns) |
79 |
| - } |
80 |
| - if cfg.MaxIdleConns > 0 { |
81 |
| - db.SetMaxIdleConns(cfg.MaxIdleConns) |
82 |
| - } |
83 |
| - if cfg.MaxConnLifetime > 0 { |
84 |
| - db.SetConnMaxLifetime(cfg.MaxConnLifetime) |
85 |
| - } |
86 |
| - |
87 |
| - // Maps struct names in CamelCase to snake without need for DB struct tags. |
88 |
| - db.MapperFunc(strcase.ToSnake) |
89 |
| - return db, nil |
90 |
| -} |
91 |
| - |
92 |
| -// buildDSN builds the data source name for sqlite from config.SQL |
93 |
| -func buildDSN(cfg *config.SQL) string { |
94 |
| - |
95 |
| - // by default, we use in-memory database if no database name is provided |
96 |
| - var dsn = ":memory:" |
97 |
| - |
98 |
| - // if database name is provided, then sqlite will use the file as the database |
99 |
| - if cfg.DatabaseName != "" { |
100 |
| - dsn = fmt.Sprintf("file:%s", cfg.DatabaseName) |
101 |
| - |
102 |
| - } |
103 |
| - |
104 |
| - if dsnAttrs := buildDSNAttrs(cfg); dsnAttrs != "" { |
105 |
| - dsn += "?" + dsnAttrs |
106 |
| - } |
107 |
| - |
108 |
| - return dsn |
109 |
| -} |
110 |
| - |
111 |
| -const ( |
112 |
| - // if journal mode is not provided, we set it to WAL by default |
113 |
| - // WAL mode allows readers and writers from different processes |
114 |
| - // to access the database concurrently by default |
115 |
| - // https://www.sqlite.org/wal.html |
116 |
| - journalModeAttrName = "_journal_mode" |
117 |
| - journalModeAttrShortName = "_journal" |
118 |
| - journalModeDefaultValue = "WAL" |
119 |
| -) |
120 |
| - |
121 |
| -var ( |
122 |
| - journalModeAttrNames = []string{journalModeAttrName, journalModeAttrShortName} |
123 |
| -) |
124 |
| - |
125 |
| -// buildDSNAttrs builds the data source name attributes for sqlite from config.SQL |
126 |
| -// available attributes can be found here |
127 |
| -// https://github.com/mattn/go-sqlite3?tab=readme-ov-file#connection-string |
128 |
| -func buildDSNAttrs(cfg *config.SQL) string { |
129 |
| - |
130 |
| - sanitizedAttrs := sanitizeDSNAttrs(cfg.ConnectAttributes) |
131 |
| - |
132 |
| - if cfg.DatabaseName != "" { |
133 |
| - if !hasAttr(sanitizedAttrs, journalModeAttrNames...) { |
134 |
| - sanitizedAttrs[journalModeAttrName] = journalModeDefaultValue |
135 |
| - } |
136 |
| - } |
137 |
| - |
138 |
| - return joinDSNAttrs(sanitizedAttrs) |
139 |
| -} |
140 |
| - |
141 |
| -// hasAttr checks if the attributes map has any of the keys |
142 |
| -func hasAttr(attrs map[string]string, keys ...string) bool { |
143 |
| - for key := range attrs { |
144 |
| - for _, k := range keys { |
145 |
| - if key == k { |
146 |
| - return true |
147 |
| - } |
148 |
| - } |
149 |
| - } |
150 |
| - return false |
151 |
| -} |
152 |
| - |
153 |
| -// sanitizeDSNAttrs sanitizes the attributes by trimming the keys and values |
154 |
| -func sanitizeDSNAttrs(attrs map[string]string) map[string]string { |
155 |
| - sanitized := make(map[string]string, len(attrs)) |
156 |
| - |
157 |
| - for k, v := range attrs { |
158 |
| - k, v = sanitizeDSNAttrElem(k), sanitizeDSNAttrElem(v) |
159 |
| - sanitized[k] = v |
160 |
| - } |
161 |
| - |
162 |
| - return sanitized |
163 |
| -} |
164 |
| - |
165 |
| -// sanitizeDSNAttrElem trims the value, lowercases it |
166 |
| -func sanitizeDSNAttrElem(v string) string { |
167 |
| - return strings.TrimSpace(strings.ToLower(v)) |
168 |
| -} |
169 |
| - |
170 |
| -// joinDSNAttrs joins the attributes into a single string |
171 |
| -// with key=value pairs separated by & and escaped |
172 |
| -func joinDSNAttrs(attrs map[string]string) string { |
173 |
| - first := true |
174 |
| - var buf bytes.Buffer |
175 |
| - for k, v := range attrs { |
176 |
| - if !first { |
177 |
| - buf.WriteString("&") |
178 |
| - } |
179 |
| - first = false |
180 |
| - buf.WriteString(k) |
181 |
| - buf.WriteString("=") |
182 |
| - buf.WriteString(v) |
183 |
| - } |
184 |
| - return buf.String() |
185 |
| -} |
0 commit comments