@@ -17,7 +17,7 @@ import TSCUtility
17
17
public final class SQLiteBackedCache < Value: Codable > : Closable {
18
18
public typealias Key = String
19
19
20
- public let name : String
20
+ public let tableName : String
21
21
public let fileSystem : TSCBasic . FileSystem
22
22
public let location : SQLite . Location
23
23
public let configuration : SQLiteBackedCacheConfiguration
@@ -29,8 +29,8 @@ public final class SQLiteBackedCache<Value: Codable>: Closable {
29
29
private let jsonEncoder : JSONEncoder
30
30
private let jsonDecoder : JSONDecoder
31
31
32
- public init ( name : String , location: SQLite . Location , configuration: SQLiteBackedCacheConfiguration = . init( ) , diagnosticsEngine: DiagnosticsEngine ? = nil ) {
33
- self . name = name
32
+ public init ( tableName : String , location: SQLite . Location , configuration: SQLiteBackedCacheConfiguration = . init( ) , diagnosticsEngine: DiagnosticsEngine ? = nil ) {
33
+ self . tableName = tableName
34
34
self . location = location
35
35
switch self . location {
36
36
case . path, . temporary:
@@ -44,8 +44,8 @@ public final class SQLiteBackedCache<Value: Codable>: Closable {
44
44
self . jsonDecoder = JSONDecoder . makeWithDefaults ( )
45
45
}
46
46
47
- public convenience init ( name : String , path: AbsolutePath , configuration: SQLiteBackedCacheConfiguration = . init( ) , diagnosticsEngine: DiagnosticsEngine ? = nil ) {
48
- self . init ( name : name , location: . path( path) , configuration: configuration, diagnosticsEngine: diagnosticsEngine)
47
+ public convenience init ( tableName : String , path: AbsolutePath , configuration: SQLiteBackedCacheConfiguration = . init( ) , diagnosticsEngine: DiagnosticsEngine ? = nil ) {
48
+ self . init ( tableName : tableName , location: . path( path) , configuration: configuration, diagnosticsEngine: diagnosticsEngine)
49
49
}
50
50
51
51
deinit {
@@ -69,7 +69,7 @@ public final class SQLiteBackedCache<Value: Codable>: Closable {
69
69
70
70
public func put( key: Key , value: Value , replace: Bool = false ) throws {
71
71
do {
72
- let query = " INSERT OR \( replace ? " REPLACE " : " IGNORE " ) INTO \( self . name ) VALUES (?, ?); "
72
+ let query = " INSERT OR \( replace ? " REPLACE " : " IGNORE " ) INTO \( self . tableName ) VALUES (?, ?); "
73
73
try self . executeStatement ( query) { statement -> Void in
74
74
let data = try self . jsonEncoder. encode ( value)
75
75
let bindings : [ SQLite . SQLiteValue ] = [
@@ -83,8 +83,8 @@ public final class SQLiteBackedCache<Value: Codable>: Closable {
83
83
if !self . configuration. truncateWhenFull {
84
84
throw error
85
85
}
86
- self . diagnosticsEngine? . emit ( . warning( " truncating \( self . name ) cache database since it reached max size of \( self . configuration. maxSizeInBytes ?? 0 ) bytes " ) )
87
- try self . executeStatement ( " DELETE FROM \( self . name ) ; " ) { statement -> Void in
86
+ self . diagnosticsEngine? . emit ( . warning( " truncating \( self . tableName ) cache database since it reached max size of \( self . configuration. maxSizeInBytes ?? 0 ) bytes " ) )
87
+ try self . executeStatement ( " DELETE FROM \( self . tableName ) ; " ) { statement -> Void in
88
88
try statement. step ( )
89
89
}
90
90
try self . put ( key: key, value: value, replace: replace)
@@ -94,7 +94,7 @@ public final class SQLiteBackedCache<Value: Codable>: Closable {
94
94
}
95
95
96
96
public func get( key: Key ) throws -> Value ? {
97
- let query = " SELECT value FROM \( self . name ) WHERE key = ? LIMIT 1; "
97
+ let query = " SELECT value FROM \( self . tableName ) WHERE key = ? LIMIT 1; "
98
98
return try self . executeStatement ( query) { statement -> Value ? in
99
99
try statement. bind ( [ . string( key) ] )
100
100
let data = try statement. step ( ) ? . blob ( at: 0 )
@@ -105,7 +105,7 @@ public final class SQLiteBackedCache<Value: Codable>: Closable {
105
105
}
106
106
107
107
public func remove( key: Key ) throws {
108
- let query = " DELETE FROM \( self . name ) WHERE key = ? LIMIT 1; "
108
+ let query = " DELETE FROM \( self . tableName ) WHERE key = ? LIMIT 1; "
109
109
try self . executeStatement ( query) { statement in
110
110
try statement. bind ( [ . string( key) ] )
111
111
try statement. step ( )
@@ -175,7 +175,7 @@ public final class SQLiteBackedCache<Value: Codable>: Closable {
175
175
176
176
private func createSchemaIfNecessary( db: SQLite ) throws {
177
177
let table = """
178
- CREATE TABLE IF NOT EXISTS \( self . name ) (
178
+ CREATE TABLE IF NOT EXISTS \( self . tableName ) (
179
179
key STRING PRIMARY KEY NOT NULL,
180
180
value BLOB NOT NULL
181
181
);
0 commit comments