Skip to content

Commit ad8ec58

Browse files
committed
Conventionally mark Objective-C implementation details private
Basically, prefix "_" all the things. Ideally we could hide these implementation details altogether, but it doesn't appear to be possible in a Swift framework (yet). Additionally, by creating and casting our own opaque structs in the "bridging" header, we can avoid exposing the inner sqlite3 module. Signed-off-by: Stephen Celis <[email protected]>
1 parent 550c1df commit ad8ec58

File tree

4 files changed

+96
-92
lines changed

4 files changed

+96
-92
lines changed

SQLite/Database.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,10 @@ public final class Database {
381381
} else {
382382
self.busy = nil
383383
}
384-
return SQLiteBusyHandler(self.handle, self.busy)
384+
return _SQLiteBusyHandler(self.handle, self.busy)
385385
}
386386
}
387-
private var busy: SQLiteBusyHandlerCallback?
387+
private var busy: _SQLiteBusyHandlerCallback?
388388

389389
/// Sets a handler to call when a statement is executed with the compiled
390390
/// SQL.
@@ -398,9 +398,9 @@ public final class Database {
398398
} else {
399399
trace = nil
400400
}
401-
SQLiteTrace(handle, trace)
401+
_SQLiteTrace(handle, trace)
402402
}
403-
private var trace: SQLiteTraceCallback?
403+
private var trace: _SQLiteTraceCallback?
404404

405405
/// An SQL operation passed to update callbacks.
406406
public enum Operation {
@@ -448,9 +448,9 @@ public final class Database {
448448
} else {
449449
updateHook = nil
450450
}
451-
SQLiteUpdateHook(handle, updateHook)
451+
_SQLiteUpdateHook(handle, updateHook)
452452
}
453-
private var updateHook: SQLiteUpdateHookCallback?
453+
private var updateHook: _SQLiteUpdateHookCallback?
454454

455455
/// Registers a callback to be invoked whenever a transaction is committed.
456456
///
@@ -463,18 +463,18 @@ public final class Database {
463463
} else {
464464
commitHook = nil
465465
}
466-
SQLiteCommitHook(handle, commitHook)
466+
_SQLiteCommitHook(handle, commitHook)
467467
}
468-
private var commitHook: SQLiteCommitHookCallback?
468+
private var commitHook: _SQLiteCommitHookCallback?
469469

470470
/// Registers a callback to be invoked whenever a transaction rolls back.
471471
///
472472
/// :param: callback A callback invoked when a transaction is rolled back.
473473
public func rollbackHook(callback: (() -> ())?) {
474474
rollbackHook = callback.map { $0 }
475-
SQLiteRollbackHook(handle, rollbackHook)
475+
_SQLiteRollbackHook(handle, rollbackHook)
476476
}
477-
private var rollbackHook: SQLiteRollbackHookCallback?
477+
private var rollbackHook: _SQLiteRollbackHookCallback?
478478

479479
/// Creates or redefines a custom SQL function.
480480
///
@@ -531,10 +531,10 @@ public final class Database {
531531
assertionFailure("unsupported result type: \(result)")
532532
}
533533
}
534-
return SQLiteCreateFunction(self.handle, function, Int32(argc), deterministic ? 1 : 0, self.functions[function]?[argc])
534+
return _SQLiteCreateFunction(self.handle, function, Int32(argc), deterministic ? 1 : 0, self.functions[function]?[argc])
535535
}
536536
}
537-
private var functions = [String: [Int: SQLiteCreateFunctionCallback]]()
537+
private var functions = [String: [Int: _SQLiteCreateFunctionCallback]]()
538538

539539
/// The return type of a collation comparison function.
540540
public typealias ComparisonResult = NSComparisonResult
@@ -550,10 +550,10 @@ public final class Database {
550550
self.collations[collation] = { lhs, rhs in
551551
return Int32(block(lhs: String.fromCString(lhs)!, rhs: String.fromCString(rhs)!).rawValue)
552552
}
553-
return SQLiteCreateCollation(self.handle, collation, self.collations[collation])
553+
return _SQLiteCreateCollation(self.handle, collation, self.collations[collation])
554554
}
555555
}
556-
private var collations = [String: SQLiteCreateCollationCallback]()
556+
private var collations = [String: _SQLiteCreateCollationCallback]()
557557

558558
// MARK: - Error Handling
559559

SQLite/FTS.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ extension Database {
7676

7777
public func register(tokenizer submoduleName: String, next: String -> (String, Range<String.Index>)?) {
7878
try {
79-
SQLiteRegisterTokenizer(self.handle, Tokenizer.moduleName, submoduleName) { input, offset, length in
79+
_SQLiteRegisterTokenizer(self.handle, Tokenizer.moduleName, submoduleName) { input, offset, length in
8080
let string = String.fromCString(input)!
8181
if var (token, range) = next(string) {
8282
let view = string.utf8

SQLite/SQLite-Bridging.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,32 @@
2222
// THE SOFTWARE.
2323
//
2424

25-
@import sqlite3;
2625
@import Foundation;
2726

28-
typedef int (^SQLiteBusyHandlerCallback)(int times);
29-
int SQLiteBusyHandler(sqlite3 * handle, SQLiteBusyHandlerCallback callback);
27+
typedef struct SQLiteHandle SQLiteHandle;
28+
typedef struct SQLiteContext SQLiteContext;
29+
typedef struct SQLiteValue SQLiteValue;
3030

31-
typedef void (^SQLiteTraceCallback)(const char * SQL);
32-
void SQLiteTrace(sqlite3 * handle, SQLiteTraceCallback callback);
31+
typedef int (^_SQLiteBusyHandlerCallback)(int times);
32+
int _SQLiteBusyHandler(SQLiteHandle * handle, _SQLiteBusyHandlerCallback callback);
3333

34-
typedef void (^SQLiteUpdateHookCallback)(int operation, const char * db, const char * table, sqlite3_int64 rowid);
35-
void SQLiteUpdateHook(sqlite3 * handle, SQLiteUpdateHookCallback callback);
34+
typedef void (^_SQLiteTraceCallback)(const char * SQL);
35+
void _SQLiteTrace(SQLiteHandle * handle, _SQLiteTraceCallback callback);
3636

37-
typedef int (^SQLiteCommitHookCallback)();
38-
void SQLiteCommitHook(sqlite3 * handle, SQLiteCommitHookCallback callback);
37+
typedef void (^_SQLiteUpdateHookCallback)(int operation, const char * db, const char * table, long long rowid);
38+
void _SQLiteUpdateHook(SQLiteHandle * handle, _SQLiteUpdateHookCallback callback);
3939

40-
typedef void (^SQLiteRollbackHookCallback)();
41-
void SQLiteRollbackHook(sqlite3 * handle, SQLiteRollbackHookCallback callback);
40+
typedef int (^_SQLiteCommitHookCallback)();
41+
void _SQLiteCommitHook(SQLiteHandle * handle, _SQLiteCommitHookCallback callback);
4242

43-
typedef void (^SQLiteCreateFunctionCallback)(sqlite3_context * context, int argc, sqlite3_value ** argv);
44-
int SQLiteCreateFunction(sqlite3 * handle, const char * name, int argc, int deterministic, SQLiteCreateFunctionCallback callback);
43+
typedef void (^_SQLiteRollbackHookCallback)();
44+
void _SQLiteRollbackHook(SQLiteHandle * handle, _SQLiteRollbackHookCallback callback);
4545

46-
typedef int (^SQLiteCreateCollationCallback)(const char * lhs, const char * rhs);
47-
int SQLiteCreateCollation(sqlite3 * handle, const char * name, SQLiteCreateCollationCallback callback);
46+
typedef void (^_SQLiteCreateFunctionCallback)(SQLiteContext * context, int argc, SQLiteValue ** argv);
47+
int _SQLiteCreateFunction(SQLiteHandle * handle, const char * name, int argc, int deterministic, _SQLiteCreateFunctionCallback callback);
4848

49-
typedef NSString * (^SQLiteTokenizerNextCallback)(const char * input, int * inputOffset, int * inputLength);
50-
int SQLiteRegisterTokenizer(sqlite3 * db, const char * module, const char * tokenizer, SQLiteTokenizerNextCallback callback);
49+
typedef int (^_SQLiteCreateCollationCallback)(const char * lhs, const char * rhs);
50+
int _SQLiteCreateCollation(SQLiteHandle * handle, const char * name, _SQLiteCreateCollationCallback callback);
51+
52+
typedef NSString * (^_SQLiteTokenizerNextCallback)(const char * input, int * inputOffset, int * inputLength);
53+
int _SQLiteRegisterTokenizer(SQLiteHandle * db, const char * module, const char * tokenizer, _SQLiteTokenizerNextCallback callback);

SQLite/SQLite-Bridging.m

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,113 +24,114 @@
2424

2525
#import "SQLite-Bridging.h"
2626

27+
@import sqlite3;
28+
2729
#import "fts3_tokenizer.h"
2830

29-
static int _SQLiteBusyHandler(void * context, int tries) {
30-
return ((__bridge SQLiteBusyHandlerCallback)context)(tries);
31+
static int __SQLiteBusyHandler(void * context, int tries) {
32+
return ((__bridge _SQLiteBusyHandlerCallback)context)(tries);
3133
}
3234

33-
int SQLiteBusyHandler(sqlite3 * handle, SQLiteBusyHandlerCallback callback) {
35+
int _SQLiteBusyHandler(SQLiteHandle * handle, _SQLiteBusyHandlerCallback callback) {
3436
if (callback) {
35-
return sqlite3_busy_handler(handle, _SQLiteBusyHandler, (__bridge void *)callback);
37+
return sqlite3_busy_handler((sqlite3 *)handle, __SQLiteBusyHandler, (__bridge void *)callback);
3638
} else {
37-
return sqlite3_busy_handler(handle, 0, 0);
39+
return sqlite3_busy_handler((sqlite3 *)handle, 0, 0);
3840
}
3941
}
4042

41-
static void _SQLiteTrace(void * context, const char * SQL) {
42-
((__bridge SQLiteTraceCallback)context)(SQL);
43+
static void __SQLiteTrace(void * context, const char * SQL) {
44+
((__bridge _SQLiteTraceCallback)context)(SQL);
4345
}
4446

45-
void SQLiteTrace(sqlite3 * handle, SQLiteTraceCallback callback) {
47+
void _SQLiteTrace(SQLiteHandle * handle, _SQLiteTraceCallback callback) {
4648
if (callback) {
47-
sqlite3_trace(handle, _SQLiteTrace, (__bridge void *)callback);
49+
sqlite3_trace((sqlite3 *)handle, __SQLiteTrace, (__bridge void *)callback);
4850
} else {
49-
sqlite3_trace(handle, 0, 0);
51+
sqlite3_trace((sqlite3 *)handle, 0, 0);
5052
}
5153
}
5254

53-
static void _SQLiteUpdateHook(void * context, int operation, const char * db, const char * table, sqlite3_int64 rowid) {
54-
((__bridge SQLiteUpdateHookCallback)context)(operation, db, table, rowid);
55+
static void __SQLiteUpdateHook(void * context, int operation, const char * db, const char * table, long long rowid) {
56+
((__bridge _SQLiteUpdateHookCallback)context)(operation, db, table, rowid);
5557
}
5658

57-
void SQLiteUpdateHook(sqlite3 * handle, SQLiteUpdateHookCallback callback) {
58-
sqlite3_update_hook(handle, _SQLiteUpdateHook, (__bridge void *)callback);
59+
void _SQLiteUpdateHook(SQLiteHandle * handle, _SQLiteUpdateHookCallback callback) {
60+
sqlite3_update_hook((sqlite3 *)handle, __SQLiteUpdateHook, (__bridge void *)callback);
5961
}
6062

61-
static int _SQLiteCommitHook(void * context) {
62-
return ((__bridge SQLiteCommitHookCallback)context)();
63+
static int __SQLiteCommitHook(void * context) {
64+
return ((__bridge _SQLiteCommitHookCallback)context)();
6365
}
6466

65-
void SQLiteCommitHook(sqlite3 * handle, SQLiteCommitHookCallback callback) {
66-
sqlite3_commit_hook(handle, _SQLiteCommitHook, (__bridge void *)callback);
67+
void _SQLiteCommitHook(SQLiteHandle * handle, _SQLiteCommitHookCallback callback) {
68+
sqlite3_commit_hook((sqlite3 *)handle, __SQLiteCommitHook, (__bridge void *)callback);
6769
}
6870

69-
static void _SQLiteRollbackHook(void * context) {
70-
((__bridge SQLiteRollbackHookCallback)context)();
71+
static void __SQLiteRollbackHook(void * context) {
72+
((__bridge _SQLiteRollbackHookCallback)context)();
7173
}
7274

73-
void SQLiteRollbackHook(sqlite3 * handle, SQLiteRollbackHookCallback callback) {
74-
sqlite3_rollback_hook(handle, _SQLiteRollbackHook, (__bridge void *)callback);
75+
void _SQLiteRollbackHook(SQLiteHandle * handle, _SQLiteRollbackHookCallback callback) {
76+
sqlite3_rollback_hook((sqlite3 *)handle, __SQLiteRollbackHook, (__bridge void *)callback);
7577
}
7678

77-
static void _SQLiteCreateFunction(sqlite3_context * context, int argc, sqlite3_value ** argv) {
78-
((__bridge SQLiteCreateFunctionCallback)sqlite3_user_data(context))(context, argc, argv);
79+
static void __SQLiteCreateFunction(sqlite3_context * context, int argc, sqlite3_value ** argv) {
80+
((__bridge _SQLiteCreateFunctionCallback)sqlite3_user_data(context))((SQLiteContext *)context, argc, (SQLiteValue **)argv);
7981
}
8082

81-
int SQLiteCreateFunction(sqlite3 * handle, const char * name, int argc, int deterministic, SQLiteCreateFunctionCallback callback) {
83+
int _SQLiteCreateFunction(SQLiteHandle * handle, const char * name, int argc, int deterministic, _SQLiteCreateFunctionCallback callback) {
8284
if (callback) {
8385
int flags = SQLITE_UTF8;
8486
if (deterministic) {
8587
#ifdef SQLITE_DETERMINISTIC
8688
flags |= SQLITE_DETERMINISTIC;
8789
#endif
8890
}
89-
return sqlite3_create_function_v2(handle, name, -1, flags, (__bridge void *)callback, &_SQLiteCreateFunction, 0, 0, 0);
91+
return sqlite3_create_function_v2((sqlite3 *)handle, name, -1, flags, (__bridge void *)callback, &__SQLiteCreateFunction, 0, 0, 0);
9092
} else {
91-
return sqlite3_create_function_v2(handle, name, 0, 0, 0, 0, 0, 0, 0);
93+
return sqlite3_create_function_v2((sqlite3 *)handle, name, 0, 0, 0, 0, 0, 0, 0);
9294
}
9395
}
9496

95-
static int _SQLiteCreateCollation(void * context, int len_lhs, const void * lhs, int len_rhs, const void * rhs) {
96-
return ((__bridge SQLiteCreateCollationCallback)context)(lhs, rhs);
97+
static int __SQLiteCreateCollation(void * context, int len_lhs, const void * lhs, int len_rhs, const void * rhs) {
98+
return ((__bridge _SQLiteCreateCollationCallback)context)(lhs, rhs);
9799
}
98100

99-
int SQLiteCreateCollation(sqlite3 * handle, const char * name, SQLiteCreateCollationCallback callback) {
101+
int _SQLiteCreateCollation(SQLiteHandle * handle, const char * name, _SQLiteCreateCollationCallback callback) {
100102
if (callback) {
101-
return sqlite3_create_collation_v2(handle, name, SQLITE_UTF8, (__bridge void *)callback, &_SQLiteCreateCollation, 0);
103+
return sqlite3_create_collation_v2((sqlite3 *)handle, name, SQLITE_UTF8, (__bridge void *)callback, &__SQLiteCreateCollation, 0);
102104
} else {
103-
return sqlite3_create_collation_v2(handle, name, 0, 0, 0, 0);
105+
return sqlite3_create_collation_v2((sqlite3 *)handle, name, 0, 0, 0, 0);
104106
}
105107
}
106108

107109
#pragma mark - FTS
108110

109-
typedef struct _SQLiteTokenizer {
111+
typedef struct __SQLiteTokenizer {
110112
sqlite3_tokenizer base;
111-
__unsafe_unretained SQLiteTokenizerNextCallback callback;
112-
} _SQLiteTokenizer;
113+
__unsafe_unretained _SQLiteTokenizerNextCallback callback;
114+
} __SQLiteTokenizer;
113115

114-
typedef struct _SQLiteTokenizerCursor {
116+
typedef struct __SQLiteTokenizerCursor {
115117
void * base;
116118
const char * input;
117119
int inputOffset;
118120
int inputLength;
119121
int idx;
120-
} _SQLiteTokenizerCursor;
121-
122+
} __SQLiteTokenizerCursor;
122123

123-
static NSMutableDictionary * _SQLiteTokenizerMap;
124+
static NSMutableDictionary * __SQLiteTokenizerMap;
124125

125-
static int _SQLiteTokenizerCreate(int argc, const char * const * argv, sqlite3_tokenizer ** ppTokenizer) {
126-
_SQLiteTokenizer * tokenizer = (_SQLiteTokenizer *)sqlite3_malloc(sizeof(_SQLiteTokenizer));
126+
static int __SQLiteTokenizerCreate(int argc, const char * const * argv, sqlite3_tokenizer ** ppTokenizer) {
127+
__SQLiteTokenizer * tokenizer = (__SQLiteTokenizer *)sqlite3_malloc(sizeof(__SQLiteTokenizer));
127128
if (!tokenizer) {
128129
return SQLITE_NOMEM;
129130
}
130131
memset(tokenizer, 0, sizeof(* tokenizer)); // FIXME: needed?
131132

132133
NSString * key = [NSString stringWithUTF8String:argv[0]];
133-
tokenizer->callback = [_SQLiteTokenizerMap objectForKey:key];
134+
tokenizer->callback = [__SQLiteTokenizerMap objectForKey:key];
134135
if (!tokenizer->callback) {
135136
return SQLITE_ERROR;
136137
}
@@ -139,13 +140,13 @@ static int _SQLiteTokenizerCreate(int argc, const char * const * argv, sqlite3_t
139140
return SQLITE_OK;
140141
}
141142

142-
static int _SQLiteTokenizerDestroy(sqlite3_tokenizer * pTokenizer) {
143+
static int __SQLiteTokenizerDestroy(sqlite3_tokenizer * pTokenizer) {
143144
sqlite3_free(pTokenizer);
144145
return SQLITE_OK;
145146
}
146147

147-
static int _SQLiteTokenizerOpen(sqlite3_tokenizer * pTokenizer, const char * pInput, int nBytes, sqlite3_tokenizer_cursor ** ppCursor) {
148-
_SQLiteTokenizerCursor * cursor = (_SQLiteTokenizerCursor *)sqlite3_malloc(sizeof(_SQLiteTokenizerCursor));
148+
static int __SQLiteTokenizerOpen(sqlite3_tokenizer * pTokenizer, const char * pInput, int nBytes, sqlite3_tokenizer_cursor ** ppCursor) {
149+
__SQLiteTokenizerCursor * cursor = (__SQLiteTokenizerCursor *)sqlite3_malloc(sizeof(__SQLiteTokenizerCursor));
149150
if (!cursor) {
150151
return SQLITE_NOMEM;
151152
}
@@ -159,14 +160,14 @@ static int _SQLiteTokenizerOpen(sqlite3_tokenizer * pTokenizer, const char * pIn
159160
return SQLITE_OK;
160161
}
161162

162-
static int _SQLiteTokenizerClose(sqlite3_tokenizer_cursor * pCursor) {
163+
static int __SQLiteTokenizerClose(sqlite3_tokenizer_cursor * pCursor) {
163164
sqlite3_free(pCursor);
164165
return SQLITE_OK;
165166
}
166167

167-
static int _SQLiteTokenizerNext(sqlite3_tokenizer_cursor * pCursor, const char ** ppToken, int * pnBytes, int * piStartOffset, int * piEndOffset, int * piPosition) {
168-
_SQLiteTokenizerCursor * cursor = (_SQLiteTokenizerCursor *)pCursor;
169-
_SQLiteTokenizer * tokenizer = (_SQLiteTokenizer *)cursor->base;
168+
static int __SQLiteTokenizerNext(sqlite3_tokenizer_cursor * pCursor, const char ** ppToken, int * pnBytes, int * piStartOffset, int * piEndOffset, int * piPosition) {
169+
__SQLiteTokenizerCursor * cursor = (__SQLiteTokenizerCursor *)pCursor;
170+
__SQLiteTokenizer * tokenizer = (__SQLiteTokenizer *)cursor->base;
170171

171172
cursor->inputOffset += cursor->inputLength;
172173
const char * input = cursor->input + cursor->inputOffset;
@@ -183,27 +184,27 @@ static int _SQLiteTokenizerNext(sqlite3_tokenizer_cursor * pCursor, const char *
183184
return SQLITE_OK;
184185
}
185186

186-
static const sqlite3_tokenizer_module _SQLiteTokenizerModule = {
187+
static const sqlite3_tokenizer_module __SQLiteTokenizerModule = {
187188
0,
188-
_SQLiteTokenizerCreate,
189-
_SQLiteTokenizerDestroy,
190-
_SQLiteTokenizerOpen,
191-
_SQLiteTokenizerClose,
192-
_SQLiteTokenizerNext
189+
__SQLiteTokenizerCreate,
190+
__SQLiteTokenizerDestroy,
191+
__SQLiteTokenizerOpen,
192+
__SQLiteTokenizerClose,
193+
__SQLiteTokenizerNext
193194
};
194195

195-
int SQLiteRegisterTokenizer(sqlite3 * db, const char * moduleName, const char * submoduleName, SQLiteTokenizerNextCallback callback) {
196+
int _SQLiteRegisterTokenizer(SQLiteHandle * db, const char * moduleName, const char * submoduleName, _SQLiteTokenizerNextCallback callback) {
196197
static dispatch_once_t onceToken;
197198
dispatch_once(&onceToken, ^{
198-
_SQLiteTokenizerMap = [NSMutableDictionary new];
199+
__SQLiteTokenizerMap = [NSMutableDictionary new];
199200
});
200201

201202
sqlite3_stmt * stmt;
202-
int status = sqlite3_prepare_v2(db, "SELECT fts3_tokenizer(?, ?)", -1, &stmt, 0);
203+
int status = sqlite3_prepare_v2((sqlite3 *)db, "SELECT fts3_tokenizer(?, ?)", -1, &stmt, 0);
203204
if (status != SQLITE_OK ){
204205
return status;
205206
}
206-
const sqlite3_tokenizer_module * pModule = &_SQLiteTokenizerModule;
207+
const sqlite3_tokenizer_module * pModule = &__SQLiteTokenizerModule;
207208
sqlite3_bind_text(stmt, 1, moduleName, -1, SQLITE_STATIC);
208209
sqlite3_bind_blob(stmt, 2, &pModule, sizeof(pModule), SQLITE_STATIC);
209210
sqlite3_step(stmt);
@@ -212,7 +213,7 @@ int SQLiteRegisterTokenizer(sqlite3 * db, const char * moduleName, const char *
212213
return status;
213214
}
214215

215-
[_SQLiteTokenizerMap setObject:[callback copy] forKey:[NSString stringWithUTF8String:submoduleName]];
216+
[__SQLiteTokenizerMap setObject:[callback copy] forKey:[NSString stringWithUTF8String:submoduleName]];
216217

217218
return SQLITE_OK;
218219
}

0 commit comments

Comments
 (0)