diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-11 19:35:06 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-11 19:35:06 -0400 |
| commit | 591e74c57cdfb9cd380e401ed9c43fd8dfe6f47d (patch) | |
| tree | 50a772b399b4b77f65b81ae1844827ddc3f54641 /internal/storage/sqlite3.go | |
| parent | Fix driver name in tutorial (diff) | |
| download | cli-591e74c57cdfb9cd380e401ed9c43fd8dfe6f47d.zip | |
Add support for sqlite3
Diffstat (limited to 'internal/storage/sqlite3.go')
| -rw-r--r-- | internal/storage/sqlite3.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/internal/storage/sqlite3.go b/internal/storage/sqlite3.go new file mode 100644 index 0000000..877dc7e --- /dev/null +++ b/internal/storage/sqlite3.go @@ -0,0 +1,48 @@ +package storage + +import ( + "fmt" + + "github.com/eugenetriguba/bolt/internal/configloader" +) + +type SqliteAdapter struct{} + +func (s SqliteAdapter) ConvertGenericPlaceholders( + query string, + argsCount int, +) string { + // The sqlite driver uses ? as a generic placeholder, + // so no translation is necessary. + return query +} + +func (s SqliteAdapter) TableExists( + executor sqlExecutor, + tableName string, +) (bool, error) { + var count int + err := executor.QueryRow(` + SELECT COUNT(*) + FROM sqlite_master + WHERE type='table' AND name=?; + `, tableName).Scan(&count) + if err != nil { + return false, fmt.Errorf( + "unable to check if %s exists: %w", + tableName, + err, + ) + } + + return count > 0, nil +} + +func (s SqliteAdapter) DatabaseName(executor sqlExecutor) (string, error) { + return "main", nil +} + +func (s SqliteAdapter) CreateDSN(cfg configloader.ConnectionConfig) string { + // Note: Use the dbname as the sqlite db name/path + return cfg.DBName +} |