|
| 1 | +// The MIT License (MIT) |
| 2 | + |
| 3 | +// Copyright (c) 2017-2020 Uber Technologies Inc. |
| 4 | + |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +package sqlite |
| 24 | + |
| 25 | +import ( |
| 26 | + "bytes" |
| 27 | + "fmt" |
| 28 | + "sort" |
| 29 | + "strings" |
| 30 | + |
| 31 | + "github.com/uber/cadence/common/config" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + // if journal mode is not provided, we set it to WAL by default |
| 36 | + // WAL mode allows readers and writers from different processes |
| 37 | + // to access the database concurrently by default |
| 38 | + // https://sqlite.org/pragma.html#pragma_journal_mode |
| 39 | + // https://www.sqlite.org/wal.html |
| 40 | + pragmaJournalModeAttrName = "_pragma.journal_mode" |
| 41 | + pragmaJournalModeDefaultValue = "WAL" |
| 42 | + |
| 43 | + // if busy_timeout is not provided, we set it to 60 seconds by default |
| 44 | + // https://sqlite.org/pragma.html#pragma_busy_timeout |
| 45 | + pragmaBusyTimeoutAttrName = "_pragma.busy_timeout" |
| 46 | + pragmaBusyTimeoutDefault = "60000" |
| 47 | +) |
| 48 | + |
| 49 | +const ( |
| 50 | + // pragmaKey is the key is used to set pragma arguments in the DSN |
| 51 | + pragmaKey = "_pragma" |
| 52 | + |
| 53 | + // pragmaPrefix is the prefix used to identify pragma arguments in the config |
| 54 | + pragmaPrefix = "_pragma." |
| 55 | +) |
| 56 | + |
| 57 | +// buildDSN builds the data source name for sqlite from config.SQL |
| 58 | +// If DatabaseName is not provided, then sqlite will use in-memory database, otherwise it will use the file as the database |
| 59 | +// All dsn attributes can be set up in the ConnectAttributes field of the config.SQL |
| 60 | +// Available attributes can be found here: https://github.com/ncruces/go-sqlite3/blob/main/driver/driver.go |
| 61 | +// PRAGMA attributes should start with "_pragma." prefix |
| 62 | +// example: "_pragma.journal_mode":"wal" will be transformed to "_pragma=journal_mode(wal)" |
| 63 | +// More about PRAGMA attributes: https://sqlite.org/pragma.html |
| 64 | +// Default PRAGMA values if not provided: |
| 65 | +// - journal_mode: WAL (file only) - https://sqlite.org/pragma.html#pragma_journal_mode |
| 66 | +// - busy_timeout: 60 seconds - https://sqlite.org/pragma.html#pragma_busy_timeout |
| 67 | +func buildDSN(cfg *config.SQL) string { |
| 68 | + |
| 69 | + // by default, we use in-memory database if no database name is provided |
| 70 | + var dsn = "file::memory:" |
| 71 | + |
| 72 | + // if database name is provided, then sqlite will use the file as the database |
| 73 | + if cfg.DatabaseName != "" { |
| 74 | + dsn = fmt.Sprintf("file:%s", cfg.DatabaseName) |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + if dsnAttrs := buildDSNAttrs(cfg); dsnAttrs != "" { |
| 79 | + dsn += "?" + dsnAttrs |
| 80 | + } |
| 81 | + |
| 82 | + return dsn |
| 83 | +} |
| 84 | + |
| 85 | +// buildDSNAttrs builds the data source name attributes for sqlite from config.SQL |
| 86 | +func buildDSNAttrs(cfg *config.SQL) string { |
| 87 | + |
| 88 | + sanitizedAttrs := sanitizeDSNAttrs(cfg.ConnectAttributes) |
| 89 | + |
| 90 | + if cfg.DatabaseName != "" { |
| 91 | + defaultIfEmpty(sanitizedAttrs, pragmaJournalModeAttrName, pragmaJournalModeDefaultValue) |
| 92 | + } |
| 93 | + |
| 94 | + defaultIfEmpty(sanitizedAttrs, pragmaBusyTimeoutAttrName, pragmaBusyTimeoutDefault) |
| 95 | + return joinDSNAttrs(sanitizedAttrs) |
| 96 | +} |
| 97 | + |
| 98 | +// defaultIfEmpty sets the value to the key if the key is not present in the attributes |
| 99 | +func defaultIfEmpty(attrs map[string]string, key, value string) { |
| 100 | + if hasAttr(attrs, key) { |
| 101 | + return |
| 102 | + } |
| 103 | + attrs[key] = value |
| 104 | +} |
| 105 | + |
| 106 | +// hasAttr checks if the attributes map has any of the keys |
| 107 | +func hasAttr(attrs map[string]string, keys ...string) bool { |
| 108 | + for key := range attrs { |
| 109 | + for _, k := range keys { |
| 110 | + if key == k { |
| 111 | + return true |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return false |
| 116 | +} |
| 117 | + |
| 118 | +// sanitizeDSNAttrs sanitizes the attributes by trimming the keys and values |
| 119 | +func sanitizeDSNAttrs(attrs map[string]string) map[string]string { |
| 120 | + sanitized := make(map[string]string, len(attrs)) |
| 121 | + |
| 122 | + for k, v := range attrs { |
| 123 | + k, v = sanitizeDSNAttrKey(k), sanitizeDSNAttrValue(v) |
| 124 | + sanitized[k] = v |
| 125 | + } |
| 126 | + |
| 127 | + return sanitized |
| 128 | +} |
| 129 | + |
| 130 | +// isPragmaKey checks if the key is a pragma key |
| 131 | +func isPragmaKey(key string) bool { |
| 132 | + return strings.HasPrefix(key, pragmaPrefix) |
| 133 | +} |
| 134 | + |
| 135 | +// transformPragmaArgument transforms the pragma argument to the format that can be used in the DSN |
| 136 | +// example: "_pragma.journal_mode":"wal" -> "_pragma=journal_mode(wal)" |
| 137 | +func transformPragmaArgument(key, value string) (newKey, newValue string) { |
| 138 | + return pragmaKey, fmt.Sprintf("%s(%s)", strings.TrimPrefix(key, pragmaPrefix), value) |
| 139 | +} |
| 140 | + |
| 141 | +// sanitizeDSNAttrElem trims the value, lowercases it |
| 142 | +func sanitizeDSNAttrKey(v string) string { |
| 143 | + return strings.TrimSpace(strings.ToLower(v)) |
| 144 | +} |
| 145 | + |
| 146 | +// sanitizeDSNAttrElem trims the value, lowercases it |
| 147 | +func sanitizeDSNAttrValue(v string) string { |
| 148 | + return strings.TrimSpace(v) |
| 149 | +} |
| 150 | + |
| 151 | +// sortedKeys returns the sorted keys of the map |
| 152 | +func sortMapKeys(m map[string]string) []string { |
| 153 | + keys := make([]string, 0, len(m)) |
| 154 | + for k := range m { |
| 155 | + keys = append(keys, k) |
| 156 | + } |
| 157 | + |
| 158 | + sort.Strings(keys) |
| 159 | + return keys |
| 160 | +} |
| 161 | + |
| 162 | +// joinDSNAttrs joins the attributes into a single string |
| 163 | +// with key=value pairs separated by & and escaped |
| 164 | +func joinDSNAttrs(attrs map[string]string) string { |
| 165 | + first := true |
| 166 | + var buf bytes.Buffer |
| 167 | + |
| 168 | + // sort the keys to make the order of the attributes deterministic |
| 169 | + sortedKeys := sortMapKeys(attrs) |
| 170 | + |
| 171 | + for _, k := range sortedKeys { |
| 172 | + v := attrs[k] |
| 173 | + |
| 174 | + // pragma arguments should be transformed to the format that can be used in the DSN |
| 175 | + if isPragmaKey(k) { |
| 176 | + k, v = transformPragmaArgument(k, v) |
| 177 | + } |
| 178 | + |
| 179 | + if !first { |
| 180 | + buf.WriteString("&") |
| 181 | + } |
| 182 | + first = false |
| 183 | + buf.WriteString(k) |
| 184 | + buf.WriteString("=") |
| 185 | + buf.WriteString(v) |
| 186 | + } |
| 187 | + return buf.String() |
| 188 | +} |
0 commit comments