@josuelmm/capacitor-background-geolocation 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/JosuelmmCapacitorBackgroundGeolocation.podspec +34 -0
- package/LICENSE +17 -0
- package/NOTICE.md +32 -0
- package/Package.swift +45 -0
- package/README.md +402 -0
- package/android/build.gradle +79 -0
- package/android/proguard-rules.pro +1 -0
- package/android/src/main/AndroidManifest.xml +83 -0
- package/android/src/main/java/com/evgenii/jsevaluator/HandlerWrapper.java +18 -0
- package/android/src/main/java/com/evgenii/jsevaluator/JavaScriptInterface.java +22 -0
- package/android/src/main/java/com/evgenii/jsevaluator/JsEvaluator.java +133 -0
- package/android/src/main/java/com/evgenii/jsevaluator/JsFunctionCallFormatter.java +37 -0
- package/android/src/main/java/com/evgenii/jsevaluator/WebViewWrapper.java +71 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/CallJavaResultInterface.java +8 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/HandlerWrapperInterface.java +5 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/JsCallback.java +10 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/JsEvaluatorInterface.java +18 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/WebViewWrapperInterface.java +14 -0
- package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/BackgroundGeolocationPlugin.java +898 -0
- package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/ConfigMapper.java +303 -0
- package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/HeadlessTaskRegistry.java +34 -0
- package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/JsEvaluatorTaskRunner.java +63 -0
- package/android/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java +699 -0
- package/android/src/main/java/com/marianhello/bgloc/BootCompletedReceiver.java +103 -0
- package/android/src/main/java/com/marianhello/bgloc/Config.java +1155 -0
- package/android/src/main/java/com/marianhello/bgloc/ConnectivityListener.java +5 -0
- package/android/src/main/java/com/marianhello/bgloc/HttpPostService.java +362 -0
- package/android/src/main/java/com/marianhello/bgloc/LocationManager.java +138 -0
- package/android/src/main/java/com/marianhello/bgloc/PluginDelegate.java +45 -0
- package/android/src/main/java/com/marianhello/bgloc/PluginException.java +38 -0
- package/android/src/main/java/com/marianhello/bgloc/PostLocationTask.java +238 -0
- package/android/src/main/java/com/marianhello/bgloc/ResourceResolver.java +55 -0
- package/android/src/main/java/com/marianhello/bgloc/data/AbstractLocationTemplate.java +69 -0
- package/android/src/main/java/com/marianhello/bgloc/data/ArrayListLocationTemplate.java +88 -0
- package/android/src/main/java/com/marianhello/bgloc/data/BackgroundActivity.java +108 -0
- package/android/src/main/java/com/marianhello/bgloc/data/BackgroundLocation.java +1088 -0
- package/android/src/main/java/com/marianhello/bgloc/data/ConfigJsonMapper.java +211 -0
- package/android/src/main/java/com/marianhello/bgloc/data/ConfigurationDAO.java +13 -0
- package/android/src/main/java/com/marianhello/bgloc/data/DAOFactory.java +17 -0
- package/android/src/main/java/com/marianhello/bgloc/data/HashMapLocationTemplate.java +82 -0
- package/android/src/main/java/com/marianhello/bgloc/data/LocationDAO.java +27 -0
- package/android/src/main/java/com/marianhello/bgloc/data/LocationTemplate.java +12 -0
- package/android/src/main/java/com/marianhello/bgloc/data/LocationTemplateFactory.java +71 -0
- package/android/src/main/java/com/marianhello/bgloc/data/LocationTransform.java +19 -0
- package/android/src/main/java/com/marianhello/bgloc/data/SessionLocationDAO.java +18 -0
- package/android/src/main/java/com/marianhello/bgloc/data/provider/ContentProviderLocationDAO.java +406 -0
- package/android/src/main/java/com/marianhello/bgloc/data/provider/LocationContentProvider.java +321 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationContract.java +94 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationDAO.java +227 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationContract.java +122 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationDAO.java +550 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteOpenHelper.java +189 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteSessionContract.java +74 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteSessionLocationDAO.java +169 -0
- package/android/src/main/java/com/marianhello/bgloc/driving/DrivingEventsDetector.java +265 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/AbstractTaskRunner.java +15 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/ActivityTask.java +48 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/JsCallback.java +10 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/LocationTask.java +60 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/StationaryTask.java +25 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/Task.java +8 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/TaskRunner.java +5 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/TaskRunnerFactory.java +8 -0
- package/android/src/main/java/com/marianhello/bgloc/http/UrlTemplateResolver.java +115 -0
- package/android/src/main/java/com/marianhello/bgloc/oem/BatteryOemHelper.java +214 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/AbstractLocationProvider.java +218 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/ActivityRecognitionLocationProvider.java +385 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/DistanceFilterLocationProvider.java +685 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/LocationProvider.java +32 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/LocationProviderFactory.java +47 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/ProviderDelegate.java +12 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/RawLocationProvider.java +175 -0
- package/android/src/main/java/com/marianhello/bgloc/sensor/SensorFusionDetector.java +199 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationService.java +16 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceImpl.java +1531 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceInfo.java +6 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceInfoImpl.java +41 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceIntentBuilder.java +203 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceProxy.java +156 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/AccountHelper.java +39 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/Authenticator.java +68 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/AuthenticatorService.java +28 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/BatchManager.java +311 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java +148 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/SyncAdapter.java +301 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/SyncService.java +68 -0
- package/android/src/main/java/com/marianhello/logging/DBLogReader.java +208 -0
- package/android/src/main/java/com/marianhello/logging/LogEntry.java +99 -0
- package/android/src/main/java/com/marianhello/logging/LoggerManager.java +70 -0
- package/android/src/main/java/com/marianhello/logging/UncaughtExceptionLogger.java +36 -0
- package/android/src/main/java/com/marianhello/utils/CloneHelper.java +22 -0
- package/android/src/main/java/com/marianhello/utils/Convert.java +56 -0
- package/android/src/main/java/com/marianhello/utils/TextUtils.java +72 -0
- package/android/src/main/java/com/marianhello/utils/ToneGenerator.java +68 -0
- package/android/src/main/java/org/apache/commons/io/Charsets.java +153 -0
- package/android/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java +344 -0
- package/android/src/main/java/org/chromium/content/browser/ThreadUtils.java +134 -0
- package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlExpression.java +398 -0
- package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlSelectStatement.java +671 -0
- package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlStatement.java +29 -0
- package/android/src/main/java/ru/andremoniy/utils/TextUtils.java +61 -0
- package/android/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
- package/android/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
- package/android/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
- package/android/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
- package/android/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/android/src/main/res/values/strings.xml +15 -0
- package/android/src/main/res/xml/authenticator.xml +7 -0
- package/android/src/main/res/xml/syncadapter.xml +9 -0
- package/dist/esm/definitions.d.ts +1052 -0
- package/dist/esm/definitions.js +142 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.js +23 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +92 -0
- package/dist/esm/web.js +242 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +415 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +418 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin-Bridging-Header.h +18 -0
- package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin.m +52 -0
- package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin.swift +750 -0
- package/ios/Tests/BackgroundGeolocationPluginTests/BackgroundGeolocationPluginTests.swift +12 -0
- package/ios/common/BackgroundGeolocation/CocoaLumberjack.h +1945 -0
- package/ios/common/BackgroundGeolocation/CocoaLumberjack.m +5255 -0
- package/ios/common/BackgroundGeolocation/FMDB.h +2357 -0
- package/ios/common/BackgroundGeolocation/FMDB.m +2672 -0
- package/ios/common/BackgroundGeolocation/FMDBLogger.h +42 -0
- package/ios/common/BackgroundGeolocation/FMDBLogger.m +264 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTUHeadingRequest.h +41 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTUHeadingRequest.m +68 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager+Internal.h +33 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager.h +178 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager.m +1025 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequest.h +103 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequest.m +238 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequestDefines.h +163 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTURequestIDGenerator.h +39 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTURequestIDGenerator.m +37 -0
- package/ios/common/BackgroundGeolocation/MAURAbstractLocationProvider.h +51 -0
- package/ios/common/BackgroundGeolocation/MAURAbstractLocationProvider.m +53 -0
- package/ios/common/BackgroundGeolocation/MAURActivity.h +23 -0
- package/ios/common/BackgroundGeolocation/MAURActivity.m +52 -0
- package/ios/common/BackgroundGeolocation/MAURActivityLocationProvider.h +18 -0
- package/ios/common/BackgroundGeolocation/MAURActivityLocationProvider.m +340 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.h +88 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.m +1193 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundSync.h +46 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundSync.m +283 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundTaskManager.h +25 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundTaskManager.m +105 -0
- package/ios/common/BackgroundGeolocation/MAURConfig.h +99 -0
- package/ios/common/BackgroundGeolocation/MAURConfig.m +636 -0
- package/ios/common/BackgroundGeolocation/MAURConfigurationContract.h +53 -0
- package/ios/common/BackgroundGeolocation/MAURConfigurationContract.m +54 -0
- package/ios/common/BackgroundGeolocation/MAURDistanceFilterLocationProvider.h +20 -0
- package/ios/common/BackgroundGeolocation/MAURDistanceFilterLocationProvider.m +550 -0
- package/ios/common/BackgroundGeolocation/MAURGeolocationOpenHelper.h +17 -0
- package/ios/common/BackgroundGeolocation/MAURGeolocationOpenHelper.m +124 -0
- package/ios/common/BackgroundGeolocation/MAURLocation.h +73 -0
- package/ios/common/BackgroundGeolocation/MAURLocation.m +392 -0
- package/ios/common/BackgroundGeolocation/MAURLocationContract.h +38 -0
- package/ios/common/BackgroundGeolocation/MAURLocationContract.m +39 -0
- package/ios/common/BackgroundGeolocation/MAURLocationManager.h +53 -0
- package/ios/common/BackgroundGeolocation/MAURLocationManager.m +305 -0
- package/ios/common/BackgroundGeolocation/MAURLogReader.h +26 -0
- package/ios/common/BackgroundGeolocation/MAURLogReader.m +122 -0
- package/ios/common/BackgroundGeolocation/MAURLogging.h +19 -0
- package/ios/common/BackgroundGeolocation/MAURPostLocationTask.h +53 -0
- package/ios/common/BackgroundGeolocation/MAURPostLocationTask.m +367 -0
- package/ios/common/BackgroundGeolocation/MAURProviderDelegate.h +52 -0
- package/ios/common/BackgroundGeolocation/MAURRawLocationProvider.h +18 -0
- package/ios/common/BackgroundGeolocation/MAURRawLocationProvider.m +138 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteConfigurationDAO.h +26 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteConfigurationDAO.m +335 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteHelper.h +57 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteHelper.m +93 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.h +52 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.m +520 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteOpenHelper.h +32 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteOpenHelper.m +276 -0
- package/ios/common/BackgroundGeolocation/MAURSensorFusionDetector.h +41 -0
- package/ios/common/BackgroundGeolocation/MAURSensorFusionDetector.m +137 -0
- package/ios/common/BackgroundGeolocation/MAURSessionLocationContract.h +29 -0
- package/ios/common/BackgroundGeolocation/MAURSessionLocationContract.m +31 -0
- package/ios/common/BackgroundGeolocation/MAURSessionLocationDAO.h +25 -0
- package/ios/common/BackgroundGeolocation/MAURSessionLocationDAO.m +153 -0
- package/ios/common/BackgroundGeolocation/MAURUncaughtExceptionLogger.h +20 -0
- package/ios/common/BackgroundGeolocation/MAURUncaughtExceptionLogger.m +62 -0
- package/ios/common/BackgroundGeolocation/MAURUrlTemplateResolver.h +31 -0
- package/ios/common/BackgroundGeolocation/MAURUrlTemplateResolver.m +107 -0
- package/ios/common/BackgroundGeolocation/Reachability.h +102 -0
- package/ios/common/BackgroundGeolocation/Reachability.m +475 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/README.md +170 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/ext/NSString+ZIMString.h +55 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/ext/NSString+ZIMString.m +47 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlDataManipulationCommand.h +27 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlExpression.h +250 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlExpression.m +259 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlSelectStatement.h +360 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlSelectStatement.m +427 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlStatement.h +37 -0
- package/ios/common/BackgroundGeolocation/module.modulemap +16 -0
- package/package.json +82 -0
|
@@ -0,0 +1,2672 @@
|
|
|
1
|
+
#import "FMDB.h"
|
|
2
|
+
#import "unistd.h"
|
|
3
|
+
#import <objc/runtime.h>
|
|
4
|
+
|
|
5
|
+
#if FMDB_SQLITE_STANDALONE
|
|
6
|
+
#import <sqlite3/sqlite3.h>
|
|
7
|
+
#else
|
|
8
|
+
#import <sqlite3.h>
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
@interface FMDatabase ()
|
|
12
|
+
- (void)resultSetDidClose:(FMResultSet *)resultSet;
|
|
13
|
+
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
|
|
14
|
+
- (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
|
|
15
|
+
|
|
16
|
+
@end
|
|
17
|
+
|
|
18
|
+
@implementation FMDatabase
|
|
19
|
+
@synthesize cachedStatements=_cachedStatements;
|
|
20
|
+
@synthesize logsErrors=_logsErrors;
|
|
21
|
+
@synthesize crashOnErrors=_crashOnErrors;
|
|
22
|
+
@synthesize checkedOut=_checkedOut;
|
|
23
|
+
@synthesize traceExecution=_traceExecution;
|
|
24
|
+
|
|
25
|
+
#pragma mark FMDatabase instantiation and deallocation
|
|
26
|
+
|
|
27
|
+
+ (instancetype)databaseWithPath:(NSString*)aPath {
|
|
28
|
+
return FMDBReturnAutoreleased([[self alloc] initWithPath:aPath]);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
- (instancetype)init {
|
|
32
|
+
return [self initWithPath:nil];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
- (instancetype)initWithPath:(NSString*)aPath {
|
|
36
|
+
|
|
37
|
+
assert(sqlite3_threadsafe()); // whoa there big boy- gotta make sure sqlite it happy with what we're going to do.
|
|
38
|
+
|
|
39
|
+
self = [super init];
|
|
40
|
+
|
|
41
|
+
if (self) {
|
|
42
|
+
_databasePath = [aPath copy];
|
|
43
|
+
_openResultSets = [[NSMutableSet alloc] init];
|
|
44
|
+
_db = nil;
|
|
45
|
+
_logsErrors = YES;
|
|
46
|
+
_crashOnErrors = NO;
|
|
47
|
+
_maxBusyRetryTimeInterval = 2;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return self;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
- (void)finalize {
|
|
54
|
+
[self close];
|
|
55
|
+
[super finalize];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
- (void)dealloc {
|
|
59
|
+
[self close];
|
|
60
|
+
FMDBRelease(_openResultSets);
|
|
61
|
+
FMDBRelease(_cachedStatements);
|
|
62
|
+
FMDBRelease(_dateFormat);
|
|
63
|
+
FMDBRelease(_databasePath);
|
|
64
|
+
FMDBRelease(_openFunctions);
|
|
65
|
+
|
|
66
|
+
#if ! __has_feature(objc_arc)
|
|
67
|
+
[super dealloc];
|
|
68
|
+
#endif
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
- (NSString *)databasePath {
|
|
72
|
+
return _databasePath;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
+ (NSString*)FMDBUserVersion {
|
|
76
|
+
return @"2.6.2";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// returns 0x0240 for version 2.4. This makes it super easy to do things like:
|
|
80
|
+
// /* need to make sure to do X with FMDB version 2.4 or later */
|
|
81
|
+
// if ([FMDatabase FMDBVersion] >= 0x0240) { … }
|
|
82
|
+
|
|
83
|
+
+ (SInt32)FMDBVersion {
|
|
84
|
+
|
|
85
|
+
// we go through these hoops so that we only have to change the version number in a single spot.
|
|
86
|
+
static dispatch_once_t once;
|
|
87
|
+
static SInt32 FMDBVersionVal = 0;
|
|
88
|
+
|
|
89
|
+
dispatch_once(&once, ^{
|
|
90
|
+
NSString *prodVersion = [self FMDBUserVersion];
|
|
91
|
+
|
|
92
|
+
if ([[prodVersion componentsSeparatedByString:@"."] count] < 3) {
|
|
93
|
+
prodVersion = [prodVersion stringByAppendingString:@".0"];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
NSString *junk = [prodVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
|
|
97
|
+
|
|
98
|
+
char *e = nil;
|
|
99
|
+
FMDBVersionVal = (int) strtoul([junk UTF8String], &e, 16);
|
|
100
|
+
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return FMDBVersionVal;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#pragma mark SQLite information
|
|
107
|
+
|
|
108
|
+
+ (NSString*)sqliteLibVersion {
|
|
109
|
+
return [NSString stringWithFormat:@"%s", sqlite3_libversion()];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
+ (BOOL)isSQLiteThreadSafe {
|
|
113
|
+
// make sure to read the sqlite headers on this guy!
|
|
114
|
+
return sqlite3_threadsafe() != 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
- (void*)sqliteHandle {
|
|
118
|
+
return _db;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
- (const char*)sqlitePath {
|
|
122
|
+
|
|
123
|
+
if (!_databasePath) {
|
|
124
|
+
return ":memory:";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if ([_databasePath length] == 0) {
|
|
128
|
+
return ""; // this creates a temporary database (it's an sqlite thing).
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return [_databasePath fileSystemRepresentation];
|
|
132
|
+
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
#pragma mark Open and close database
|
|
136
|
+
|
|
137
|
+
- (BOOL)open {
|
|
138
|
+
if (_db) {
|
|
139
|
+
return YES;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
int err = sqlite3_open([self sqlitePath], (sqlite3**)&_db );
|
|
143
|
+
if(err != SQLITE_OK) {
|
|
144
|
+
NSLog(@"error opening!: %d", err);
|
|
145
|
+
return NO;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (_maxBusyRetryTimeInterval > 0.0) {
|
|
149
|
+
// set the handler
|
|
150
|
+
[self setMaxBusyRetryTimeInterval:_maxBusyRetryTimeInterval];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
return YES;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
- (BOOL)openWithFlags:(int)flags {
|
|
158
|
+
return [self openWithFlags:flags vfs:nil];
|
|
159
|
+
}
|
|
160
|
+
- (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName {
|
|
161
|
+
#if SQLITE_VERSION_NUMBER >= 3005000
|
|
162
|
+
if (_db) {
|
|
163
|
+
return YES;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
int err = sqlite3_open_v2([self sqlitePath], (sqlite3**)&_db, flags, [vfsName UTF8String]);
|
|
167
|
+
if(err != SQLITE_OK) {
|
|
168
|
+
NSLog(@"error opening!: %d", err);
|
|
169
|
+
return NO;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (_maxBusyRetryTimeInterval > 0.0) {
|
|
173
|
+
// set the handler
|
|
174
|
+
[self setMaxBusyRetryTimeInterval:_maxBusyRetryTimeInterval];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return YES;
|
|
178
|
+
#else
|
|
179
|
+
NSLog(@"openWithFlags requires SQLite 3.5");
|
|
180
|
+
return NO;
|
|
181
|
+
#endif
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
- (BOOL)close {
|
|
186
|
+
|
|
187
|
+
[self clearCachedStatements];
|
|
188
|
+
[self closeOpenResultSets];
|
|
189
|
+
|
|
190
|
+
if (!_db) {
|
|
191
|
+
return YES;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
int rc;
|
|
195
|
+
BOOL retry;
|
|
196
|
+
BOOL triedFinalizingOpenStatements = NO;
|
|
197
|
+
|
|
198
|
+
do {
|
|
199
|
+
retry = NO;
|
|
200
|
+
rc = sqlite3_close(_db);
|
|
201
|
+
if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
|
|
202
|
+
if (!triedFinalizingOpenStatements) {
|
|
203
|
+
triedFinalizingOpenStatements = YES;
|
|
204
|
+
sqlite3_stmt *pStmt;
|
|
205
|
+
while ((pStmt = sqlite3_next_stmt(_db, nil)) !=0) {
|
|
206
|
+
NSLog(@"Closing leaked statement");
|
|
207
|
+
sqlite3_finalize(pStmt);
|
|
208
|
+
retry = YES;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else if (SQLITE_OK != rc) {
|
|
213
|
+
NSLog(@"error closing!: %d", rc);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
while (retry);
|
|
217
|
+
|
|
218
|
+
_db = nil;
|
|
219
|
+
return YES;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
#pragma mark Busy handler routines
|
|
223
|
+
|
|
224
|
+
// NOTE: appledoc seems to choke on this function for some reason;
|
|
225
|
+
// so when generating documentation, you might want to ignore the
|
|
226
|
+
// .m files so that it only documents the public interfaces outlined
|
|
227
|
+
// in the .h files.
|
|
228
|
+
//
|
|
229
|
+
// This is a known appledoc bug that it has problems with C functions
|
|
230
|
+
// within a class implementation, but for some reason, only this
|
|
231
|
+
// C function causes problems; the rest don't. Anyway, ignoring the .m
|
|
232
|
+
// files with appledoc will prevent this problem from occurring.
|
|
233
|
+
|
|
234
|
+
static int FMDBDatabaseBusyHandler(void *f, int count) {
|
|
235
|
+
FMDatabase *self = (__bridge FMDatabase*)f;
|
|
236
|
+
|
|
237
|
+
if (count == 0) {
|
|
238
|
+
self->_startBusyRetryTime = [NSDate timeIntervalSinceReferenceDate];
|
|
239
|
+
return 1;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
NSTimeInterval delta = [NSDate timeIntervalSinceReferenceDate] - (self->_startBusyRetryTime);
|
|
243
|
+
|
|
244
|
+
if (delta < [self maxBusyRetryTimeInterval]) {
|
|
245
|
+
int requestedSleepInMillseconds = (int) arc4random_uniform(50) + 50;
|
|
246
|
+
int actualSleepInMilliseconds = sqlite3_sleep(requestedSleepInMillseconds);
|
|
247
|
+
if (actualSleepInMilliseconds != requestedSleepInMillseconds) {
|
|
248
|
+
NSLog(@"WARNING: Requested sleep of %i milliseconds, but SQLite returned %i. Maybe SQLite wasn't built with HAVE_USLEEP=1?", requestedSleepInMillseconds, actualSleepInMilliseconds);
|
|
249
|
+
}
|
|
250
|
+
return 1;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return 0;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
- (void)setMaxBusyRetryTimeInterval:(NSTimeInterval)timeout {
|
|
257
|
+
|
|
258
|
+
_maxBusyRetryTimeInterval = timeout;
|
|
259
|
+
|
|
260
|
+
if (!_db) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (timeout > 0) {
|
|
265
|
+
sqlite3_busy_handler(_db, &FMDBDatabaseBusyHandler, (__bridge void *)(self));
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
// turn it off otherwise
|
|
269
|
+
sqlite3_busy_handler(_db, nil, nil);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
- (NSTimeInterval)maxBusyRetryTimeInterval {
|
|
274
|
+
return _maxBusyRetryTimeInterval;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
// we no longer make busyRetryTimeout public
|
|
279
|
+
// but for folks who don't bother noticing that the interface to FMDatabase changed,
|
|
280
|
+
// we'll still implement the method so they don't get suprise crashes
|
|
281
|
+
- (int)busyRetryTimeout {
|
|
282
|
+
NSLog(@"%s:%d", __FUNCTION__, __LINE__);
|
|
283
|
+
NSLog(@"FMDB: busyRetryTimeout no longer works, please use maxBusyRetryTimeInterval");
|
|
284
|
+
return -1;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
- (void)setBusyRetryTimeout:(int)i {
|
|
288
|
+
#pragma unused(i)
|
|
289
|
+
NSLog(@"%s:%d", __FUNCTION__, __LINE__);
|
|
290
|
+
NSLog(@"FMDB: setBusyRetryTimeout does nothing, please use setMaxBusyRetryTimeInterval:");
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
#pragma mark Result set functions
|
|
294
|
+
|
|
295
|
+
- (BOOL)hasOpenResultSets {
|
|
296
|
+
return [_openResultSets count] > 0;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
- (void)closeOpenResultSets {
|
|
300
|
+
|
|
301
|
+
//Copy the set so we don't get mutation errors
|
|
302
|
+
NSSet *openSetCopy = FMDBReturnAutoreleased([_openResultSets copy]);
|
|
303
|
+
for (NSValue *rsInWrappedInATastyValueMeal in openSetCopy) {
|
|
304
|
+
FMResultSet *rs = (FMResultSet *)[rsInWrappedInATastyValueMeal pointerValue];
|
|
305
|
+
|
|
306
|
+
[rs setParentDB:nil];
|
|
307
|
+
[rs close];
|
|
308
|
+
|
|
309
|
+
[_openResultSets removeObject:rsInWrappedInATastyValueMeal];
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
- (void)resultSetDidClose:(FMResultSet *)resultSet {
|
|
314
|
+
NSValue *setValue = [NSValue valueWithNonretainedObject:resultSet];
|
|
315
|
+
|
|
316
|
+
[_openResultSets removeObject:setValue];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
#pragma mark Cached statements
|
|
320
|
+
|
|
321
|
+
- (void)clearCachedStatements {
|
|
322
|
+
|
|
323
|
+
for (NSMutableSet *statements in [_cachedStatements objectEnumerator]) {
|
|
324
|
+
for (FMStatement *statement in [statements allObjects]) {
|
|
325
|
+
[statement close];
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
[_cachedStatements removeAllObjects];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
- (FMStatement*)cachedStatementForQuery:(NSString*)query {
|
|
333
|
+
|
|
334
|
+
NSMutableSet* statements = [_cachedStatements objectForKey:query];
|
|
335
|
+
|
|
336
|
+
return [[statements objectsPassingTest:^BOOL(FMStatement* statement, BOOL *stop) {
|
|
337
|
+
|
|
338
|
+
*stop = ![statement inUse];
|
|
339
|
+
return *stop;
|
|
340
|
+
|
|
341
|
+
}] anyObject];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
- (void)setCachedStatement:(FMStatement*)statement forQuery:(NSString*)query {
|
|
346
|
+
|
|
347
|
+
query = [query copy]; // in case we got handed in a mutable string...
|
|
348
|
+
[statement setQuery:query];
|
|
349
|
+
|
|
350
|
+
NSMutableSet* statements = [_cachedStatements objectForKey:query];
|
|
351
|
+
if (!statements) {
|
|
352
|
+
statements = [NSMutableSet set];
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
[statements addObject:statement];
|
|
356
|
+
|
|
357
|
+
[_cachedStatements setObject:statements forKey:query];
|
|
358
|
+
|
|
359
|
+
FMDBRelease(query);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
#pragma mark Key routines
|
|
363
|
+
|
|
364
|
+
- (BOOL)rekey:(NSString*)key {
|
|
365
|
+
NSData *keyData = [NSData dataWithBytes:(void *)[key UTF8String] length:(NSUInteger)strlen([key UTF8String])];
|
|
366
|
+
|
|
367
|
+
return [self rekeyWithData:keyData];
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
- (BOOL)rekeyWithData:(NSData *)keyData {
|
|
371
|
+
#ifdef SQLITE_HAS_CODEC
|
|
372
|
+
if (!keyData) {
|
|
373
|
+
return NO;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
int rc = sqlite3_rekey(_db, [keyData bytes], (int)[keyData length]);
|
|
377
|
+
|
|
378
|
+
if (rc != SQLITE_OK) {
|
|
379
|
+
NSLog(@"error on rekey: %d", rc);
|
|
380
|
+
NSLog(@"%@", [self lastErrorMessage]);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return (rc == SQLITE_OK);
|
|
384
|
+
#else
|
|
385
|
+
#pragma unused(keyData)
|
|
386
|
+
return NO;
|
|
387
|
+
#endif
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
- (BOOL)setKey:(NSString*)key {
|
|
391
|
+
NSData *keyData = [NSData dataWithBytes:[key UTF8String] length:(NSUInteger)strlen([key UTF8String])];
|
|
392
|
+
|
|
393
|
+
return [self setKeyWithData:keyData];
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
- (BOOL)setKeyWithData:(NSData *)keyData {
|
|
397
|
+
#ifdef SQLITE_HAS_CODEC
|
|
398
|
+
if (!keyData) {
|
|
399
|
+
return NO;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
int rc = sqlite3_key(_db, [keyData bytes], (int)[keyData length]);
|
|
403
|
+
|
|
404
|
+
return (rc == SQLITE_OK);
|
|
405
|
+
#else
|
|
406
|
+
#pragma unused(keyData)
|
|
407
|
+
return NO;
|
|
408
|
+
#endif
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
#pragma mark Date routines
|
|
412
|
+
|
|
413
|
+
+ (NSDateFormatter *)storeableDateFormat:(NSString *)format {
|
|
414
|
+
|
|
415
|
+
NSDateFormatter *result = FMDBReturnAutoreleased([[NSDateFormatter alloc] init]);
|
|
416
|
+
result.dateFormat = format;
|
|
417
|
+
result.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
|
|
418
|
+
result.locale = FMDBReturnAutoreleased([[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]);
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
- (BOOL)hasDateFormatter {
|
|
424
|
+
return _dateFormat != nil;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
- (void)setDateFormat:(NSDateFormatter *)format {
|
|
428
|
+
FMDBAutorelease(_dateFormat);
|
|
429
|
+
_dateFormat = FMDBReturnRetained(format);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
- (NSDate *)dateFromString:(NSString *)s {
|
|
433
|
+
return [_dateFormat dateFromString:s];
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
- (NSString *)stringFromDate:(NSDate *)date {
|
|
437
|
+
return [_dateFormat stringFromDate:date];
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
#pragma mark State of database
|
|
441
|
+
|
|
442
|
+
- (BOOL)goodConnection {
|
|
443
|
+
|
|
444
|
+
if (!_db) {
|
|
445
|
+
return NO;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
FMResultSet *rs = [self executeQuery:@"select name from sqlite_master where type='table'"];
|
|
449
|
+
|
|
450
|
+
if (rs) {
|
|
451
|
+
[rs close];
|
|
452
|
+
return YES;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
return NO;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
- (void)warnInUse {
|
|
459
|
+
NSLog(@"The FMDatabase %@ is currently in use.", self);
|
|
460
|
+
|
|
461
|
+
#ifndef NS_BLOCK_ASSERTIONS
|
|
462
|
+
if (_crashOnErrors) {
|
|
463
|
+
NSAssert(false, @"The FMDatabase %@ is currently in use.", self);
|
|
464
|
+
abort();
|
|
465
|
+
}
|
|
466
|
+
#endif
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
- (BOOL)databaseExists {
|
|
470
|
+
|
|
471
|
+
if (!_db) {
|
|
472
|
+
|
|
473
|
+
NSLog(@"The FMDatabase %@ is not open.", self);
|
|
474
|
+
|
|
475
|
+
#ifndef NS_BLOCK_ASSERTIONS
|
|
476
|
+
if (_crashOnErrors) {
|
|
477
|
+
NSAssert(false, @"The FMDatabase %@ is not open.", self);
|
|
478
|
+
abort();
|
|
479
|
+
}
|
|
480
|
+
#endif
|
|
481
|
+
|
|
482
|
+
return NO;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return YES;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
#pragma mark Error routines
|
|
489
|
+
|
|
490
|
+
- (NSString*)lastErrorMessage {
|
|
491
|
+
return [NSString stringWithUTF8String:sqlite3_errmsg(_db)];
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
- (BOOL)hadError {
|
|
495
|
+
int lastErrCode = [self lastErrorCode];
|
|
496
|
+
|
|
497
|
+
return (lastErrCode > SQLITE_OK && lastErrCode < SQLITE_ROW);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
- (int)lastErrorCode {
|
|
501
|
+
return sqlite3_errcode(_db);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
- (NSError*)errorWithMessage:(NSString*)message {
|
|
505
|
+
NSDictionary* errorMessage = [NSDictionary dictionaryWithObject:message forKey:NSLocalizedDescriptionKey];
|
|
506
|
+
|
|
507
|
+
return [NSError errorWithDomain:@"FMDatabase" code:sqlite3_errcode(_db) userInfo:errorMessage];
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
- (NSError*)lastError {
|
|
511
|
+
return [self errorWithMessage:[self lastErrorMessage]];
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
#pragma mark Update information routines
|
|
515
|
+
|
|
516
|
+
- (sqlite_int64)lastInsertRowId {
|
|
517
|
+
|
|
518
|
+
if (_isExecutingStatement) {
|
|
519
|
+
[self warnInUse];
|
|
520
|
+
return NO;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
_isExecutingStatement = YES;
|
|
524
|
+
|
|
525
|
+
sqlite_int64 ret = sqlite3_last_insert_rowid(_db);
|
|
526
|
+
|
|
527
|
+
_isExecutingStatement = NO;
|
|
528
|
+
|
|
529
|
+
return ret;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
- (int)changes {
|
|
533
|
+
if (_isExecutingStatement) {
|
|
534
|
+
[self warnInUse];
|
|
535
|
+
return 0;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
_isExecutingStatement = YES;
|
|
539
|
+
|
|
540
|
+
int ret = sqlite3_changes(_db);
|
|
541
|
+
|
|
542
|
+
_isExecutingStatement = NO;
|
|
543
|
+
|
|
544
|
+
return ret;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
#pragma mark SQL manipulation
|
|
548
|
+
|
|
549
|
+
- (void)bindObject:(id)obj toColumn:(int)idx inStatement:(sqlite3_stmt*)pStmt {
|
|
550
|
+
|
|
551
|
+
if ((!obj) || ((NSNull *)obj == [NSNull null])) {
|
|
552
|
+
sqlite3_bind_null(pStmt, idx);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// FIXME - someday check the return codes on these binds.
|
|
556
|
+
else if ([obj isKindOfClass:[NSData class]]) {
|
|
557
|
+
const void *bytes = [obj bytes];
|
|
558
|
+
if (!bytes) {
|
|
559
|
+
// it's an empty NSData object, aka [NSData data].
|
|
560
|
+
// Don't pass a NULL pointer, or sqlite will bind a SQL null instead of a blob.
|
|
561
|
+
bytes = "";
|
|
562
|
+
}
|
|
563
|
+
sqlite3_bind_blob(pStmt, idx, bytes, (int)[obj length], SQLITE_STATIC);
|
|
564
|
+
}
|
|
565
|
+
else if ([obj isKindOfClass:[NSDate class]]) {
|
|
566
|
+
if (self.hasDateFormatter)
|
|
567
|
+
sqlite3_bind_text(pStmt, idx, [[self stringFromDate:obj] UTF8String], -1, SQLITE_STATIC);
|
|
568
|
+
else
|
|
569
|
+
sqlite3_bind_double(pStmt, idx, [obj timeIntervalSince1970]);
|
|
570
|
+
}
|
|
571
|
+
else if ([obj isKindOfClass:[NSNumber class]]) {
|
|
572
|
+
|
|
573
|
+
if (strcmp([obj objCType], @encode(char)) == 0) {
|
|
574
|
+
sqlite3_bind_int(pStmt, idx, [obj charValue]);
|
|
575
|
+
}
|
|
576
|
+
else if (strcmp([obj objCType], @encode(unsigned char)) == 0) {
|
|
577
|
+
sqlite3_bind_int(pStmt, idx, [obj unsignedCharValue]);
|
|
578
|
+
}
|
|
579
|
+
else if (strcmp([obj objCType], @encode(short)) == 0) {
|
|
580
|
+
sqlite3_bind_int(pStmt, idx, [obj shortValue]);
|
|
581
|
+
}
|
|
582
|
+
else if (strcmp([obj objCType], @encode(unsigned short)) == 0) {
|
|
583
|
+
sqlite3_bind_int(pStmt, idx, [obj unsignedShortValue]);
|
|
584
|
+
}
|
|
585
|
+
else if (strcmp([obj objCType], @encode(int)) == 0) {
|
|
586
|
+
sqlite3_bind_int(pStmt, idx, [obj intValue]);
|
|
587
|
+
}
|
|
588
|
+
else if (strcmp([obj objCType], @encode(unsigned int)) == 0) {
|
|
589
|
+
sqlite3_bind_int64(pStmt, idx, (long long)[obj unsignedIntValue]);
|
|
590
|
+
}
|
|
591
|
+
else if (strcmp([obj objCType], @encode(long)) == 0) {
|
|
592
|
+
sqlite3_bind_int64(pStmt, idx, [obj longValue]);
|
|
593
|
+
}
|
|
594
|
+
else if (strcmp([obj objCType], @encode(unsigned long)) == 0) {
|
|
595
|
+
sqlite3_bind_int64(pStmt, idx, (long long)[obj unsignedLongValue]);
|
|
596
|
+
}
|
|
597
|
+
else if (strcmp([obj objCType], @encode(long long)) == 0) {
|
|
598
|
+
sqlite3_bind_int64(pStmt, idx, [obj longLongValue]);
|
|
599
|
+
}
|
|
600
|
+
else if (strcmp([obj objCType], @encode(unsigned long long)) == 0) {
|
|
601
|
+
sqlite3_bind_int64(pStmt, idx, (long long)[obj unsignedLongLongValue]);
|
|
602
|
+
}
|
|
603
|
+
else if (strcmp([obj objCType], @encode(float)) == 0) {
|
|
604
|
+
sqlite3_bind_double(pStmt, idx, [obj floatValue]);
|
|
605
|
+
}
|
|
606
|
+
else if (strcmp([obj objCType], @encode(double)) == 0) {
|
|
607
|
+
sqlite3_bind_double(pStmt, idx, [obj doubleValue]);
|
|
608
|
+
}
|
|
609
|
+
else if (strcmp([obj objCType], @encode(BOOL)) == 0) {
|
|
610
|
+
sqlite3_bind_int(pStmt, idx, ([obj boolValue] ? 1 : 0));
|
|
611
|
+
}
|
|
612
|
+
else {
|
|
613
|
+
sqlite3_bind_text(pStmt, idx, [[obj description] UTF8String], -1, SQLITE_STATIC);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
else {
|
|
617
|
+
sqlite3_bind_text(pStmt, idx, [[obj description] UTF8String], -1, SQLITE_STATIC);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
- (void)extractSQL:(NSString *)sql argumentsList:(va_list)args intoString:(NSMutableString *)cleanedSQL arguments:(NSMutableArray *)arguments {
|
|
622
|
+
|
|
623
|
+
NSUInteger length = [sql length];
|
|
624
|
+
unichar last = '\0';
|
|
625
|
+
for (NSUInteger i = 0; i < length; ++i) {
|
|
626
|
+
id arg = nil;
|
|
627
|
+
unichar current = [sql characterAtIndex:i];
|
|
628
|
+
unichar add = current;
|
|
629
|
+
if (last == '%') {
|
|
630
|
+
switch (current) {
|
|
631
|
+
case '@':
|
|
632
|
+
arg = va_arg(args, id);
|
|
633
|
+
break;
|
|
634
|
+
case 'c':
|
|
635
|
+
// warning: second argument to 'va_arg' is of promotable type 'char'; this va_arg has undefined behavior because arguments will be promoted to 'int'
|
|
636
|
+
arg = [NSString stringWithFormat:@"%c", va_arg(args, int)];
|
|
637
|
+
break;
|
|
638
|
+
case 's':
|
|
639
|
+
arg = [NSString stringWithUTF8String:va_arg(args, char*)];
|
|
640
|
+
break;
|
|
641
|
+
case 'd':
|
|
642
|
+
case 'D':
|
|
643
|
+
case 'i':
|
|
644
|
+
arg = [NSNumber numberWithInt:va_arg(args, int)];
|
|
645
|
+
break;
|
|
646
|
+
case 'u':
|
|
647
|
+
case 'U':
|
|
648
|
+
arg = [NSNumber numberWithUnsignedInt:va_arg(args, unsigned int)];
|
|
649
|
+
break;
|
|
650
|
+
case 'h':
|
|
651
|
+
i++;
|
|
652
|
+
if (i < length && [sql characterAtIndex:i] == 'i') {
|
|
653
|
+
// warning: second argument to 'va_arg' is of promotable type 'short'; this va_arg has undefined behavior because arguments will be promoted to 'int'
|
|
654
|
+
arg = [NSNumber numberWithShort:(short)(va_arg(args, int))];
|
|
655
|
+
}
|
|
656
|
+
else if (i < length && [sql characterAtIndex:i] == 'u') {
|
|
657
|
+
// warning: second argument to 'va_arg' is of promotable type 'unsigned short'; this va_arg has undefined behavior because arguments will be promoted to 'int'
|
|
658
|
+
arg = [NSNumber numberWithUnsignedShort:(unsigned short)(va_arg(args, uint))];
|
|
659
|
+
}
|
|
660
|
+
else {
|
|
661
|
+
i--;
|
|
662
|
+
}
|
|
663
|
+
break;
|
|
664
|
+
case 'q':
|
|
665
|
+
i++;
|
|
666
|
+
if (i < length && [sql characterAtIndex:i] == 'i') {
|
|
667
|
+
arg = [NSNumber numberWithLongLong:va_arg(args, long long)];
|
|
668
|
+
}
|
|
669
|
+
else if (i < length && [sql characterAtIndex:i] == 'u') {
|
|
670
|
+
arg = [NSNumber numberWithUnsignedLongLong:va_arg(args, unsigned long long)];
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
i--;
|
|
674
|
+
}
|
|
675
|
+
break;
|
|
676
|
+
case 'f':
|
|
677
|
+
arg = [NSNumber numberWithDouble:va_arg(args, double)];
|
|
678
|
+
break;
|
|
679
|
+
case 'g':
|
|
680
|
+
// warning: second argument to 'va_arg' is of promotable type 'float'; this va_arg has undefined behavior because arguments will be promoted to 'double'
|
|
681
|
+
arg = [NSNumber numberWithFloat:(float)(va_arg(args, double))];
|
|
682
|
+
break;
|
|
683
|
+
case 'l':
|
|
684
|
+
i++;
|
|
685
|
+
if (i < length) {
|
|
686
|
+
unichar next = [sql characterAtIndex:i];
|
|
687
|
+
if (next == 'l') {
|
|
688
|
+
i++;
|
|
689
|
+
if (i < length && [sql characterAtIndex:i] == 'd') {
|
|
690
|
+
//%lld
|
|
691
|
+
arg = [NSNumber numberWithLongLong:va_arg(args, long long)];
|
|
692
|
+
}
|
|
693
|
+
else if (i < length && [sql characterAtIndex:i] == 'u') {
|
|
694
|
+
//%llu
|
|
695
|
+
arg = [NSNumber numberWithUnsignedLongLong:va_arg(args, unsigned long long)];
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
i--;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
else if (next == 'd') {
|
|
702
|
+
//%ld
|
|
703
|
+
arg = [NSNumber numberWithLong:va_arg(args, long)];
|
|
704
|
+
}
|
|
705
|
+
else if (next == 'u') {
|
|
706
|
+
//%lu
|
|
707
|
+
arg = [NSNumber numberWithUnsignedLong:va_arg(args, unsigned long)];
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
i--;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
else {
|
|
714
|
+
i--;
|
|
715
|
+
}
|
|
716
|
+
break;
|
|
717
|
+
default:
|
|
718
|
+
// something else that we can't interpret. just pass it on through like normal
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
else if (current == '%') {
|
|
723
|
+
// percent sign; skip this character
|
|
724
|
+
add = '\0';
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
if (arg != nil) {
|
|
728
|
+
[cleanedSQL appendString:@"?"];
|
|
729
|
+
[arguments addObject:arg];
|
|
730
|
+
}
|
|
731
|
+
else if (add == (unichar)'@' && last == (unichar) '%') {
|
|
732
|
+
[cleanedSQL appendFormat:@"NULL"];
|
|
733
|
+
}
|
|
734
|
+
else if (add != '\0') {
|
|
735
|
+
[cleanedSQL appendFormat:@"%C", add];
|
|
736
|
+
}
|
|
737
|
+
last = current;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
#pragma mark Execute queries
|
|
742
|
+
|
|
743
|
+
- (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary *)arguments {
|
|
744
|
+
return [self executeQuery:sql withArgumentsInArray:nil orDictionary:arguments orVAList:nil];
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args {
|
|
748
|
+
|
|
749
|
+
if (![self databaseExists]) {
|
|
750
|
+
return 0x00;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
if (_isExecutingStatement) {
|
|
754
|
+
[self warnInUse];
|
|
755
|
+
return 0x00;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
_isExecutingStatement = YES;
|
|
759
|
+
|
|
760
|
+
int rc = 0x00;
|
|
761
|
+
sqlite3_stmt *pStmt = 0x00;
|
|
762
|
+
FMStatement *statement = 0x00;
|
|
763
|
+
FMResultSet *rs = 0x00;
|
|
764
|
+
|
|
765
|
+
if (_traceExecution && sql) {
|
|
766
|
+
NSLog(@"%@ executeQuery: %@", self, sql);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
if (_shouldCacheStatements) {
|
|
770
|
+
statement = [self cachedStatementForQuery:sql];
|
|
771
|
+
pStmt = statement ? [statement statement] : 0x00;
|
|
772
|
+
[statement reset];
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
if (!pStmt) {
|
|
776
|
+
|
|
777
|
+
rc = sqlite3_prepare_v2(_db, [sql UTF8String], -1, &pStmt, 0);
|
|
778
|
+
|
|
779
|
+
if (SQLITE_OK != rc) {
|
|
780
|
+
if (_logsErrors) {
|
|
781
|
+
NSLog(@"DB Error: %d \"%@\"", [self lastErrorCode], [self lastErrorMessage]);
|
|
782
|
+
NSLog(@"DB Query: %@", sql);
|
|
783
|
+
NSLog(@"DB Path: %@", _databasePath);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
if (_crashOnErrors) {
|
|
787
|
+
NSAssert(false, @"DB Error: %d \"%@\"", [self lastErrorCode], [self lastErrorMessage]);
|
|
788
|
+
abort();
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
sqlite3_finalize(pStmt);
|
|
792
|
+
_isExecutingStatement = NO;
|
|
793
|
+
return nil;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
id obj;
|
|
798
|
+
int idx = 0;
|
|
799
|
+
int queryCount = sqlite3_bind_parameter_count(pStmt); // pointed out by Dominic Yu (thanks!)
|
|
800
|
+
|
|
801
|
+
// If dictionaryArgs is passed in, that means we are using sqlite's named parameter support
|
|
802
|
+
if (dictionaryArgs) {
|
|
803
|
+
|
|
804
|
+
for (NSString *dictionaryKey in [dictionaryArgs allKeys]) {
|
|
805
|
+
|
|
806
|
+
// Prefix the key with a colon.
|
|
807
|
+
NSString *parameterName = [[NSString alloc] initWithFormat:@":%@", dictionaryKey];
|
|
808
|
+
|
|
809
|
+
if (_traceExecution) {
|
|
810
|
+
NSLog(@"%@ = %@", parameterName, [dictionaryArgs objectForKey:dictionaryKey]);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
// Get the index for the parameter name.
|
|
814
|
+
int namedIdx = sqlite3_bind_parameter_index(pStmt, [parameterName UTF8String]);
|
|
815
|
+
|
|
816
|
+
FMDBRelease(parameterName);
|
|
817
|
+
|
|
818
|
+
if (namedIdx > 0) {
|
|
819
|
+
// Standard binding from here.
|
|
820
|
+
[self bindObject:[dictionaryArgs objectForKey:dictionaryKey] toColumn:namedIdx inStatement:pStmt];
|
|
821
|
+
// increment the binding count, so our check below works out
|
|
822
|
+
idx++;
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
NSLog(@"Could not find index for %@", dictionaryKey);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
else {
|
|
830
|
+
|
|
831
|
+
while (idx < queryCount) {
|
|
832
|
+
|
|
833
|
+
if (arrayArgs && idx < (int)[arrayArgs count]) {
|
|
834
|
+
obj = [arrayArgs objectAtIndex:(NSUInteger)idx];
|
|
835
|
+
}
|
|
836
|
+
else if (args) {
|
|
837
|
+
obj = va_arg(args, id);
|
|
838
|
+
}
|
|
839
|
+
else {
|
|
840
|
+
//We ran out of arguments
|
|
841
|
+
break;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
if (_traceExecution) {
|
|
845
|
+
if ([obj isKindOfClass:[NSData class]]) {
|
|
846
|
+
NSLog(@"data: %ld bytes", (unsigned long)[(NSData*)obj length]);
|
|
847
|
+
}
|
|
848
|
+
else {
|
|
849
|
+
NSLog(@"obj: %@", obj);
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
idx++;
|
|
854
|
+
|
|
855
|
+
[self bindObject:obj toColumn:idx inStatement:pStmt];
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
if (idx != queryCount) {
|
|
860
|
+
NSLog(@"Error: the bind count is not correct for the # of variables (executeQuery)");
|
|
861
|
+
sqlite3_finalize(pStmt);
|
|
862
|
+
_isExecutingStatement = NO;
|
|
863
|
+
return nil;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
FMDBRetain(statement); // to balance the release below
|
|
867
|
+
|
|
868
|
+
if (!statement) {
|
|
869
|
+
statement = [[FMStatement alloc] init];
|
|
870
|
+
[statement setStatement:pStmt];
|
|
871
|
+
|
|
872
|
+
if (_shouldCacheStatements && sql) {
|
|
873
|
+
[self setCachedStatement:statement forQuery:sql];
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// the statement gets closed in rs's dealloc or [rs close];
|
|
878
|
+
rs = [FMResultSet resultSetWithStatement:statement usingParentDatabase:self];
|
|
879
|
+
[rs setQuery:sql];
|
|
880
|
+
|
|
881
|
+
NSValue *openResultSet = [NSValue valueWithNonretainedObject:rs];
|
|
882
|
+
[_openResultSets addObject:openResultSet];
|
|
883
|
+
|
|
884
|
+
[statement setUseCount:[statement useCount] + 1];
|
|
885
|
+
|
|
886
|
+
FMDBRelease(statement);
|
|
887
|
+
|
|
888
|
+
_isExecutingStatement = NO;
|
|
889
|
+
|
|
890
|
+
return rs;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
- (FMResultSet *)executeQuery:(NSString*)sql, ... {
|
|
894
|
+
va_list args;
|
|
895
|
+
va_start(args, sql);
|
|
896
|
+
|
|
897
|
+
id result = [self executeQuery:sql withArgumentsInArray:nil orDictionary:nil orVAList:args];
|
|
898
|
+
|
|
899
|
+
va_end(args);
|
|
900
|
+
return result;
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
- (FMResultSet *)executeQueryWithFormat:(NSString*)format, ... {
|
|
904
|
+
va_list args;
|
|
905
|
+
va_start(args, format);
|
|
906
|
+
|
|
907
|
+
NSMutableString *sql = [NSMutableString stringWithCapacity:[format length]];
|
|
908
|
+
NSMutableArray *arguments = [NSMutableArray array];
|
|
909
|
+
[self extractSQL:format argumentsList:args intoString:sql arguments:arguments];
|
|
910
|
+
|
|
911
|
+
va_end(args);
|
|
912
|
+
|
|
913
|
+
return [self executeQuery:sql withArgumentsInArray:arguments];
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments {
|
|
917
|
+
return [self executeQuery:sql withArgumentsInArray:arguments orDictionary:nil orVAList:nil];
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
- (FMResultSet *)executeQuery:(NSString *)sql values:(NSArray *)values error:(NSError * __autoreleasing *)error {
|
|
921
|
+
FMResultSet *rs = [self executeQuery:sql withArgumentsInArray:values orDictionary:nil orVAList:nil];
|
|
922
|
+
if (!rs && error) {
|
|
923
|
+
*error = [self lastError];
|
|
924
|
+
}
|
|
925
|
+
return rs;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
- (FMResultSet *)executeQuery:(NSString*)sql withVAList:(va_list)args {
|
|
929
|
+
return [self executeQuery:sql withArgumentsInArray:nil orDictionary:nil orVAList:args];
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
#pragma mark Execute updates
|
|
933
|
+
|
|
934
|
+
- (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args {
|
|
935
|
+
|
|
936
|
+
if (![self databaseExists]) {
|
|
937
|
+
return NO;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
if (_isExecutingStatement) {
|
|
941
|
+
[self warnInUse];
|
|
942
|
+
return NO;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
_isExecutingStatement = YES;
|
|
946
|
+
|
|
947
|
+
int rc = 0x00;
|
|
948
|
+
sqlite3_stmt *pStmt = 0x00;
|
|
949
|
+
FMStatement *cachedStmt = 0x00;
|
|
950
|
+
|
|
951
|
+
if (_traceExecution && sql) {
|
|
952
|
+
NSLog(@"%@ executeUpdate: %@", self, sql);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
if (_shouldCacheStatements) {
|
|
956
|
+
cachedStmt = [self cachedStatementForQuery:sql];
|
|
957
|
+
pStmt = cachedStmt ? [cachedStmt statement] : 0x00;
|
|
958
|
+
[cachedStmt reset];
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
if (!pStmt) {
|
|
962
|
+
rc = sqlite3_prepare_v2(_db, [sql UTF8String], -1, &pStmt, 0);
|
|
963
|
+
|
|
964
|
+
if (SQLITE_OK != rc) {
|
|
965
|
+
if (_logsErrors) {
|
|
966
|
+
NSLog(@"DB Error: %d \"%@\"", [self lastErrorCode], [self lastErrorMessage]);
|
|
967
|
+
NSLog(@"DB Query: %@", sql);
|
|
968
|
+
NSLog(@"DB Path: %@", _databasePath);
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
if (_crashOnErrors) {
|
|
972
|
+
NSAssert(false, @"DB Error: %d \"%@\"", [self lastErrorCode], [self lastErrorMessage]);
|
|
973
|
+
abort();
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
if (outErr) {
|
|
977
|
+
*outErr = [self errorWithMessage:[NSString stringWithUTF8String:sqlite3_errmsg(_db)]];
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
sqlite3_finalize(pStmt);
|
|
981
|
+
|
|
982
|
+
_isExecutingStatement = NO;
|
|
983
|
+
return NO;
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
id obj;
|
|
988
|
+
int idx = 0;
|
|
989
|
+
int queryCount = sqlite3_bind_parameter_count(pStmt);
|
|
990
|
+
|
|
991
|
+
// If dictionaryArgs is passed in, that means we are using sqlite's named parameter support
|
|
992
|
+
if (dictionaryArgs) {
|
|
993
|
+
|
|
994
|
+
for (NSString *dictionaryKey in [dictionaryArgs allKeys]) {
|
|
995
|
+
|
|
996
|
+
// Prefix the key with a colon.
|
|
997
|
+
NSString *parameterName = [[NSString alloc] initWithFormat:@":%@", dictionaryKey];
|
|
998
|
+
|
|
999
|
+
if (_traceExecution) {
|
|
1000
|
+
NSLog(@"%@ = %@", parameterName, [dictionaryArgs objectForKey:dictionaryKey]);
|
|
1001
|
+
}
|
|
1002
|
+
// Get the index for the parameter name.
|
|
1003
|
+
int namedIdx = sqlite3_bind_parameter_index(pStmt, [parameterName UTF8String]);
|
|
1004
|
+
|
|
1005
|
+
FMDBRelease(parameterName);
|
|
1006
|
+
|
|
1007
|
+
if (namedIdx > 0) {
|
|
1008
|
+
// Standard binding from here.
|
|
1009
|
+
[self bindObject:[dictionaryArgs objectForKey:dictionaryKey] toColumn:namedIdx inStatement:pStmt];
|
|
1010
|
+
|
|
1011
|
+
// increment the binding count, so our check below works out
|
|
1012
|
+
idx++;
|
|
1013
|
+
}
|
|
1014
|
+
else {
|
|
1015
|
+
NSString *message = [NSString stringWithFormat:@"Could not find index for %@", dictionaryKey];
|
|
1016
|
+
|
|
1017
|
+
if (_logsErrors) {
|
|
1018
|
+
NSLog(@"%@", message);
|
|
1019
|
+
}
|
|
1020
|
+
if (outErr) {
|
|
1021
|
+
*outErr = [self errorWithMessage:message];
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
else {
|
|
1027
|
+
|
|
1028
|
+
while (idx < queryCount) {
|
|
1029
|
+
|
|
1030
|
+
if (arrayArgs && idx < (int)[arrayArgs count]) {
|
|
1031
|
+
obj = [arrayArgs objectAtIndex:(NSUInteger)idx];
|
|
1032
|
+
}
|
|
1033
|
+
else if (args) {
|
|
1034
|
+
obj = va_arg(args, id);
|
|
1035
|
+
}
|
|
1036
|
+
else {
|
|
1037
|
+
//We ran out of arguments
|
|
1038
|
+
break;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
if (_traceExecution) {
|
|
1042
|
+
if ([obj isKindOfClass:[NSData class]]) {
|
|
1043
|
+
NSLog(@"data: %ld bytes", (unsigned long)[(NSData*)obj length]);
|
|
1044
|
+
}
|
|
1045
|
+
else {
|
|
1046
|
+
NSLog(@"obj: %@", obj);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
idx++;
|
|
1051
|
+
|
|
1052
|
+
[self bindObject:obj toColumn:idx inStatement:pStmt];
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
|
|
1057
|
+
if (idx != queryCount) {
|
|
1058
|
+
NSString *message = [NSString stringWithFormat:@"Error: the bind count (%d) is not correct for the # of variables in the query (%d) (%@) (executeUpdate)", idx, queryCount, sql];
|
|
1059
|
+
if (_logsErrors) {
|
|
1060
|
+
NSLog(@"%@", message);
|
|
1061
|
+
}
|
|
1062
|
+
if (outErr) {
|
|
1063
|
+
*outErr = [self errorWithMessage:message];
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
sqlite3_finalize(pStmt);
|
|
1067
|
+
_isExecutingStatement = NO;
|
|
1068
|
+
return NO;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
/* Call sqlite3_step() to run the virtual machine. Since the SQL being
|
|
1072
|
+
** executed is not a SELECT statement, we assume no data will be returned.
|
|
1073
|
+
*/
|
|
1074
|
+
|
|
1075
|
+
rc = sqlite3_step(pStmt);
|
|
1076
|
+
|
|
1077
|
+
if (SQLITE_DONE == rc) {
|
|
1078
|
+
// all is well, let's return.
|
|
1079
|
+
}
|
|
1080
|
+
else if (SQLITE_INTERRUPT == rc) {
|
|
1081
|
+
if (_logsErrors) {
|
|
1082
|
+
NSLog(@"Error calling sqlite3_step. Query was interrupted (%d: %s) SQLITE_INTERRUPT", rc, sqlite3_errmsg(_db));
|
|
1083
|
+
NSLog(@"DB Query: %@", sql);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
else if (rc == SQLITE_ROW) {
|
|
1087
|
+
NSString *message = [NSString stringWithFormat:@"A executeUpdate is being called with a query string '%@'", sql];
|
|
1088
|
+
if (_logsErrors) {
|
|
1089
|
+
NSLog(@"%@", message);
|
|
1090
|
+
NSLog(@"DB Query: %@", sql);
|
|
1091
|
+
}
|
|
1092
|
+
if (outErr) {
|
|
1093
|
+
*outErr = [self errorWithMessage:message];
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
else {
|
|
1097
|
+
if (outErr) {
|
|
1098
|
+
*outErr = [self errorWithMessage:[NSString stringWithUTF8String:sqlite3_errmsg(_db)]];
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
if (SQLITE_ERROR == rc) {
|
|
1102
|
+
if (_logsErrors) {
|
|
1103
|
+
NSLog(@"Error calling sqlite3_step (%d: %s) SQLITE_ERROR", rc, sqlite3_errmsg(_db));
|
|
1104
|
+
NSLog(@"DB Query: %@", sql);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
else if (SQLITE_MISUSE == rc) {
|
|
1108
|
+
// uh oh.
|
|
1109
|
+
if (_logsErrors) {
|
|
1110
|
+
NSLog(@"Error calling sqlite3_step (%d: %s) SQLITE_MISUSE", rc, sqlite3_errmsg(_db));
|
|
1111
|
+
NSLog(@"DB Query: %@", sql);
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
else {
|
|
1115
|
+
// wtf?
|
|
1116
|
+
if (_logsErrors) {
|
|
1117
|
+
NSLog(@"Unknown error calling sqlite3_step (%d: %s) eu", rc, sqlite3_errmsg(_db));
|
|
1118
|
+
NSLog(@"DB Query: %@", sql);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
if (_shouldCacheStatements && !cachedStmt) {
|
|
1124
|
+
cachedStmt = [[FMStatement alloc] init];
|
|
1125
|
+
|
|
1126
|
+
[cachedStmt setStatement:pStmt];
|
|
1127
|
+
|
|
1128
|
+
[self setCachedStatement:cachedStmt forQuery:sql];
|
|
1129
|
+
|
|
1130
|
+
FMDBRelease(cachedStmt);
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
int closeErrorCode;
|
|
1134
|
+
|
|
1135
|
+
if (cachedStmt) {
|
|
1136
|
+
[cachedStmt setUseCount:[cachedStmt useCount] + 1];
|
|
1137
|
+
closeErrorCode = sqlite3_reset(pStmt);
|
|
1138
|
+
}
|
|
1139
|
+
else {
|
|
1140
|
+
/* Finalize the virtual machine. This releases all memory and other
|
|
1141
|
+
** resources allocated by the sqlite3_prepare() call above.
|
|
1142
|
+
*/
|
|
1143
|
+
closeErrorCode = sqlite3_finalize(pStmt);
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
if (closeErrorCode != SQLITE_OK) {
|
|
1147
|
+
if (_logsErrors) {
|
|
1148
|
+
NSLog(@"Unknown error finalizing or resetting statement (%d: %s)", closeErrorCode, sqlite3_errmsg(_db));
|
|
1149
|
+
NSLog(@"DB Query: %@", sql);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
_isExecutingStatement = NO;
|
|
1154
|
+
return (rc == SQLITE_DONE || rc == SQLITE_OK);
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
- (BOOL)executeUpdate:(NSString*)sql, ... {
|
|
1159
|
+
va_list args;
|
|
1160
|
+
va_start(args, sql);
|
|
1161
|
+
|
|
1162
|
+
BOOL result = [self executeUpdate:sql error:nil withArgumentsInArray:nil orDictionary:nil orVAList:args];
|
|
1163
|
+
|
|
1164
|
+
va_end(args);
|
|
1165
|
+
return result;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
- (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments {
|
|
1169
|
+
return [self executeUpdate:sql error:nil withArgumentsInArray:arguments orDictionary:nil orVAList:nil];
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
- (BOOL)executeUpdate:(NSString*)sql values:(NSArray *)values error:(NSError * __autoreleasing *)error {
|
|
1173
|
+
return [self executeUpdate:sql error:error withArgumentsInArray:values orDictionary:nil orVAList:nil];
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
- (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments {
|
|
1177
|
+
return [self executeUpdate:sql error:nil withArgumentsInArray:nil orDictionary:arguments orVAList:nil];
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
- (BOOL)executeUpdate:(NSString*)sql withVAList:(va_list)args {
|
|
1181
|
+
return [self executeUpdate:sql error:nil withArgumentsInArray:nil orDictionary:nil orVAList:args];
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
- (BOOL)executeUpdateWithFormat:(NSString*)format, ... {
|
|
1185
|
+
va_list args;
|
|
1186
|
+
va_start(args, format);
|
|
1187
|
+
|
|
1188
|
+
NSMutableString *sql = [NSMutableString stringWithCapacity:[format length]];
|
|
1189
|
+
NSMutableArray *arguments = [NSMutableArray array];
|
|
1190
|
+
|
|
1191
|
+
[self extractSQL:format argumentsList:args intoString:sql arguments:arguments];
|
|
1192
|
+
|
|
1193
|
+
va_end(args);
|
|
1194
|
+
|
|
1195
|
+
return [self executeUpdate:sql withArgumentsInArray:arguments];
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, char **names); // shhh clang.
|
|
1200
|
+
int FMDBExecuteBulkSQLCallback(void *theBlockAsVoid, int columns, char **values, char **names) {
|
|
1201
|
+
|
|
1202
|
+
if (!theBlockAsVoid) {
|
|
1203
|
+
return SQLITE_OK;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
int (^execCallbackBlock)(NSDictionary *resultsDictionary) = (__bridge int (^)(NSDictionary *__strong))(theBlockAsVoid);
|
|
1207
|
+
|
|
1208
|
+
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:(NSUInteger)columns];
|
|
1209
|
+
|
|
1210
|
+
for (NSInteger i = 0; i < columns; i++) {
|
|
1211
|
+
NSString *key = [NSString stringWithUTF8String:names[i]];
|
|
1212
|
+
id value = values[i] ? [NSString stringWithUTF8String:values[i]] : [NSNull null];
|
|
1213
|
+
[dictionary setObject:value forKey:key];
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
return execCallbackBlock(dictionary);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
- (BOOL)executeStatements:(NSString *)sql {
|
|
1220
|
+
return [self executeStatements:sql withResultBlock:nil];
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
- (BOOL)executeStatements:(NSString *)sql withResultBlock:(FMDBExecuteStatementsCallbackBlock)block {
|
|
1224
|
+
|
|
1225
|
+
int rc;
|
|
1226
|
+
char *errmsg = nil;
|
|
1227
|
+
|
|
1228
|
+
rc = sqlite3_exec([self sqliteHandle], [sql UTF8String], block ? FMDBExecuteBulkSQLCallback : nil, (__bridge void *)(block), &errmsg);
|
|
1229
|
+
|
|
1230
|
+
if (errmsg && [self logsErrors]) {
|
|
1231
|
+
NSLog(@"Error inserting batch: %s", errmsg);
|
|
1232
|
+
sqlite3_free(errmsg);
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
return (rc == SQLITE_OK);
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
- (BOOL)executeUpdate:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ... {
|
|
1239
|
+
|
|
1240
|
+
va_list args;
|
|
1241
|
+
va_start(args, outErr);
|
|
1242
|
+
|
|
1243
|
+
BOOL result = [self executeUpdate:sql error:outErr withArgumentsInArray:nil orDictionary:nil orVAList:args];
|
|
1244
|
+
|
|
1245
|
+
va_end(args);
|
|
1246
|
+
return result;
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
#pragma clang diagnostic push
|
|
1251
|
+
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
|
|
1252
|
+
- (BOOL)update:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ... {
|
|
1253
|
+
va_list args;
|
|
1254
|
+
va_start(args, outErr);
|
|
1255
|
+
|
|
1256
|
+
BOOL result = [self executeUpdate:sql error:outErr withArgumentsInArray:nil orDictionary:nil orVAList:args];
|
|
1257
|
+
|
|
1258
|
+
va_end(args);
|
|
1259
|
+
return result;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
#pragma clang diagnostic pop
|
|
1263
|
+
|
|
1264
|
+
#pragma mark Transactions
|
|
1265
|
+
|
|
1266
|
+
- (BOOL)rollback {
|
|
1267
|
+
BOOL b = [self executeUpdate:@"rollback transaction"];
|
|
1268
|
+
|
|
1269
|
+
if (b) {
|
|
1270
|
+
_inTransaction = NO;
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
return b;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
- (BOOL)commit {
|
|
1277
|
+
BOOL b = [self executeUpdate:@"commit transaction"];
|
|
1278
|
+
|
|
1279
|
+
if (b) {
|
|
1280
|
+
_inTransaction = NO;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
return b;
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
- (BOOL)beginDeferredTransaction {
|
|
1287
|
+
|
|
1288
|
+
BOOL b = [self executeUpdate:@"begin deferred transaction"];
|
|
1289
|
+
if (b) {
|
|
1290
|
+
_inTransaction = YES;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
return b;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
- (BOOL)beginTransaction {
|
|
1297
|
+
|
|
1298
|
+
BOOL b = [self executeUpdate:@"begin exclusive transaction"];
|
|
1299
|
+
if (b) {
|
|
1300
|
+
_inTransaction = YES;
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
return b;
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
- (BOOL)inTransaction {
|
|
1307
|
+
return _inTransaction;
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
- (BOOL)interrupt
|
|
1311
|
+
{
|
|
1312
|
+
if (_db) {
|
|
1313
|
+
sqlite3_interrupt([self sqliteHandle]);
|
|
1314
|
+
return YES;
|
|
1315
|
+
}
|
|
1316
|
+
return NO;
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
static NSString *FMDBEscapeSavePointName(NSString *savepointName) {
|
|
1320
|
+
return [savepointName stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
- (BOOL)startSavePointWithName:(NSString*)name error:(NSError**)outErr {
|
|
1324
|
+
#if SQLITE_VERSION_NUMBER >= 3007000
|
|
1325
|
+
NSParameterAssert(name);
|
|
1326
|
+
|
|
1327
|
+
NSString *sql = [NSString stringWithFormat:@"savepoint '%@';", FMDBEscapeSavePointName(name)];
|
|
1328
|
+
|
|
1329
|
+
return [self executeUpdate:sql error:outErr withArgumentsInArray:nil orDictionary:nil orVAList:nil];
|
|
1330
|
+
#else
|
|
1331
|
+
NSString *errorMessage = NSLocalizedString(@"Save point functions require SQLite 3.7", nil);
|
|
1332
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
1333
|
+
return NO;
|
|
1334
|
+
#endif
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
- (BOOL)releaseSavePointWithName:(NSString*)name error:(NSError**)outErr {
|
|
1338
|
+
#if SQLITE_VERSION_NUMBER >= 3007000
|
|
1339
|
+
NSParameterAssert(name);
|
|
1340
|
+
|
|
1341
|
+
NSString *sql = [NSString stringWithFormat:@"release savepoint '%@';", FMDBEscapeSavePointName(name)];
|
|
1342
|
+
|
|
1343
|
+
return [self executeUpdate:sql error:outErr withArgumentsInArray:nil orDictionary:nil orVAList:nil];
|
|
1344
|
+
#else
|
|
1345
|
+
NSString *errorMessage = NSLocalizedString(@"Save point functions require SQLite 3.7", nil);
|
|
1346
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
1347
|
+
return NO;
|
|
1348
|
+
#endif
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
- (BOOL)rollbackToSavePointWithName:(NSString*)name error:(NSError**)outErr {
|
|
1352
|
+
#if SQLITE_VERSION_NUMBER >= 3007000
|
|
1353
|
+
NSParameterAssert(name);
|
|
1354
|
+
|
|
1355
|
+
NSString *sql = [NSString stringWithFormat:@"rollback transaction to savepoint '%@';", FMDBEscapeSavePointName(name)];
|
|
1356
|
+
|
|
1357
|
+
return [self executeUpdate:sql error:outErr withArgumentsInArray:nil orDictionary:nil orVAList:nil];
|
|
1358
|
+
#else
|
|
1359
|
+
NSString *errorMessage = NSLocalizedString(@"Save point functions require SQLite 3.7", nil);
|
|
1360
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
1361
|
+
return NO;
|
|
1362
|
+
#endif
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
- (NSError*)inSavePoint:(void (^)(BOOL *rollback))block {
|
|
1366
|
+
#if SQLITE_VERSION_NUMBER >= 3007000
|
|
1367
|
+
static unsigned long savePointIdx = 0;
|
|
1368
|
+
|
|
1369
|
+
NSString *name = [NSString stringWithFormat:@"dbSavePoint%ld", savePointIdx++];
|
|
1370
|
+
|
|
1371
|
+
BOOL shouldRollback = NO;
|
|
1372
|
+
|
|
1373
|
+
NSError *err = 0x00;
|
|
1374
|
+
|
|
1375
|
+
if (![self startSavePointWithName:name error:&err]) {
|
|
1376
|
+
return err;
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
if (block) {
|
|
1380
|
+
block(&shouldRollback);
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
if (shouldRollback) {
|
|
1384
|
+
// We need to rollback and release this savepoint to remove it
|
|
1385
|
+
[self rollbackToSavePointWithName:name error:&err];
|
|
1386
|
+
}
|
|
1387
|
+
[self releaseSavePointWithName:name error:&err];
|
|
1388
|
+
|
|
1389
|
+
return err;
|
|
1390
|
+
#else
|
|
1391
|
+
NSString *errorMessage = NSLocalizedString(@"Save point functions require SQLite 3.7", nil);
|
|
1392
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
1393
|
+
return [NSError errorWithDomain:@"FMDatabase" code:0 userInfo:@{NSLocalizedDescriptionKey : errorMessage}];
|
|
1394
|
+
#endif
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
|
|
1398
|
+
#pragma mark Cache statements
|
|
1399
|
+
|
|
1400
|
+
- (BOOL)shouldCacheStatements {
|
|
1401
|
+
return _shouldCacheStatements;
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
- (void)setShouldCacheStatements:(BOOL)value {
|
|
1405
|
+
|
|
1406
|
+
_shouldCacheStatements = value;
|
|
1407
|
+
|
|
1408
|
+
if (_shouldCacheStatements && !_cachedStatements) {
|
|
1409
|
+
[self setCachedStatements:[NSMutableDictionary dictionary]];
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
if (!_shouldCacheStatements) {
|
|
1413
|
+
[self setCachedStatements:nil];
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
#pragma mark Callback function
|
|
1418
|
+
|
|
1419
|
+
void FMDBBlockSQLiteCallBackFunction(sqlite3_context *context, int argc, sqlite3_value **argv); // -Wmissing-prototypes
|
|
1420
|
+
void FMDBBlockSQLiteCallBackFunction(sqlite3_context *context, int argc, sqlite3_value **argv) {
|
|
1421
|
+
#if ! __has_feature(objc_arc)
|
|
1422
|
+
void (^block)(sqlite3_context *context, int argc, sqlite3_value **argv) = (id)sqlite3_user_data(context);
|
|
1423
|
+
#else
|
|
1424
|
+
void (^block)(sqlite3_context *context, int argc, sqlite3_value **argv) = (__bridge id)sqlite3_user_data(context);
|
|
1425
|
+
#endif
|
|
1426
|
+
if (block) {
|
|
1427
|
+
block(context, argc, argv);
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
- (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(void *context, int argc, void **argv))block {
|
|
1433
|
+
|
|
1434
|
+
if (!_openFunctions) {
|
|
1435
|
+
_openFunctions = [NSMutableSet new];
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
id b = FMDBReturnAutoreleased([block copy]);
|
|
1439
|
+
|
|
1440
|
+
[_openFunctions addObject:b];
|
|
1441
|
+
|
|
1442
|
+
/* I tried adding custom functions to release the block when the connection is destroyed- but they seemed to never be called, so we use _openFunctions to store the values instead. */
|
|
1443
|
+
#if ! __has_feature(objc_arc)
|
|
1444
|
+
sqlite3_create_function([self sqliteHandle], [name UTF8String], count, SQLITE_UTF8, (void*)b, &FMDBBlockSQLiteCallBackFunction, 0x00, 0x00);
|
|
1445
|
+
#else
|
|
1446
|
+
sqlite3_create_function([self sqliteHandle], [name UTF8String], count, SQLITE_UTF8, (__bridge void*)b, &FMDBBlockSQLiteCallBackFunction, 0x00, 0x00);
|
|
1447
|
+
#endif
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
@end
|
|
1451
|
+
|
|
1452
|
+
|
|
1453
|
+
|
|
1454
|
+
@implementation FMStatement
|
|
1455
|
+
@synthesize statement=_statement;
|
|
1456
|
+
@synthesize query=_query;
|
|
1457
|
+
@synthesize useCount=_useCount;
|
|
1458
|
+
@synthesize inUse=_inUse;
|
|
1459
|
+
|
|
1460
|
+
- (void)finalize {
|
|
1461
|
+
[self close];
|
|
1462
|
+
[super finalize];
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
- (void)dealloc {
|
|
1466
|
+
[self close];
|
|
1467
|
+
FMDBRelease(_query);
|
|
1468
|
+
#if ! __has_feature(objc_arc)
|
|
1469
|
+
[super dealloc];
|
|
1470
|
+
#endif
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
- (void)close {
|
|
1474
|
+
if (_statement) {
|
|
1475
|
+
sqlite3_finalize(_statement);
|
|
1476
|
+
_statement = 0x00;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
_inUse = NO;
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
- (void)reset {
|
|
1483
|
+
if (_statement) {
|
|
1484
|
+
sqlite3_reset(_statement);
|
|
1485
|
+
}
|
|
1486
|
+
|
|
1487
|
+
_inUse = NO;
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
- (NSString*)description {
|
|
1491
|
+
return [NSString stringWithFormat:@"%@ %ld hit(s) for query %@", [super description], _useCount, _query];
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
|
|
1495
|
+
@end
|
|
1496
|
+
|
|
1497
|
+
//
|
|
1498
|
+
// FMDatabaseAdditions.m
|
|
1499
|
+
// fmdb
|
|
1500
|
+
//
|
|
1501
|
+
// Created by August Mueller on 10/30/05.
|
|
1502
|
+
// Copyright 2005 Flying Meat Inc.. All rights reserved.
|
|
1503
|
+
//
|
|
1504
|
+
|
|
1505
|
+
#import "TargetConditionals.h"
|
|
1506
|
+
|
|
1507
|
+
@interface FMDatabase (PrivateStuff)
|
|
1508
|
+
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
|
|
1509
|
+
@end
|
|
1510
|
+
|
|
1511
|
+
@implementation FMDatabase (FMDatabaseAdditions)
|
|
1512
|
+
|
|
1513
|
+
#define RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(type, sel) \
|
|
1514
|
+
va_list args; \
|
|
1515
|
+
va_start(args, query); \
|
|
1516
|
+
FMResultSet *resultSet = [self executeQuery:query withArgumentsInArray:0x00 orDictionary:0x00 orVAList:args]; \
|
|
1517
|
+
va_end(args); \
|
|
1518
|
+
if (![resultSet next]) { return (type)0; } \
|
|
1519
|
+
type ret = [resultSet sel:0]; \
|
|
1520
|
+
[resultSet close]; \
|
|
1521
|
+
[resultSet setParentDB:nil]; \
|
|
1522
|
+
return ret;
|
|
1523
|
+
|
|
1524
|
+
|
|
1525
|
+
- (NSString*)stringForQuery:(NSString*)query, ... {
|
|
1526
|
+
RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSString *, stringForColumnIndex);
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
- (int)intForQuery:(NSString*)query, ... {
|
|
1530
|
+
RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(int, intForColumnIndex);
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
- (long)longForQuery:(NSString*)query, ... {
|
|
1534
|
+
RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(long, longForColumnIndex);
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
- (BOOL)boolForQuery:(NSString*)query, ... {
|
|
1538
|
+
RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(BOOL, boolForColumnIndex);
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
- (double)doubleForQuery:(NSString*)query, ... {
|
|
1542
|
+
RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(double, doubleForColumnIndex);
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
- (NSData*)dataForQuery:(NSString*)query, ... {
|
|
1546
|
+
RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSData *, dataForColumnIndex);
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
- (NSDate*)dateForQuery:(NSString*)query, ... {
|
|
1550
|
+
RETURN_RESULT_FOR_QUERY_WITH_SELECTOR(NSDate *, dateForColumnIndex);
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
|
|
1554
|
+
- (BOOL)tableExists:(NSString*)tableName {
|
|
1555
|
+
|
|
1556
|
+
tableName = [tableName lowercaseString];
|
|
1557
|
+
|
|
1558
|
+
FMResultSet *rs = [self executeQuery:@"select [sql] from sqlite_master where [type] = 'table' and lower(name) = ?", tableName];
|
|
1559
|
+
|
|
1560
|
+
//if at least one next exists, table exists
|
|
1561
|
+
BOOL returnBool = [rs next];
|
|
1562
|
+
|
|
1563
|
+
//close and free object
|
|
1564
|
+
[rs close];
|
|
1565
|
+
|
|
1566
|
+
return returnBool;
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
/*
|
|
1570
|
+
get table with list of tables: result colums: type[STRING], name[STRING],tbl_name[STRING],rootpage[INTEGER],sql[STRING]
|
|
1571
|
+
check if table exist in database (patch from OZLB)
|
|
1572
|
+
*/
|
|
1573
|
+
- (FMResultSet*)getSchema {
|
|
1574
|
+
|
|
1575
|
+
//result colums: type[STRING], name[STRING],tbl_name[STRING],rootpage[INTEGER],sql[STRING]
|
|
1576
|
+
FMResultSet *rs = [self executeQuery:@"SELECT type, name, tbl_name, rootpage, sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type != 'meta' AND name NOT LIKE 'sqlite_%' ORDER BY tbl_name, type DESC, name"];
|
|
1577
|
+
|
|
1578
|
+
return rs;
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
/*
|
|
1582
|
+
get table schema: result colums: cid[INTEGER], name,type [STRING], notnull[INTEGER], dflt_value[],pk[INTEGER]
|
|
1583
|
+
*/
|
|
1584
|
+
- (FMResultSet*)getTableSchema:(NSString*)tableName {
|
|
1585
|
+
|
|
1586
|
+
//result colums: cid[INTEGER], name,type [STRING], notnull[INTEGER], dflt_value[],pk[INTEGER]
|
|
1587
|
+
FMResultSet *rs = [self executeQuery:[NSString stringWithFormat: @"pragma table_info('%@')", tableName]];
|
|
1588
|
+
|
|
1589
|
+
return rs;
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
- (BOOL)columnExists:(NSString*)columnName inTableWithName:(NSString*)tableName {
|
|
1593
|
+
|
|
1594
|
+
BOOL returnBool = NO;
|
|
1595
|
+
|
|
1596
|
+
tableName = [tableName lowercaseString];
|
|
1597
|
+
columnName = [columnName lowercaseString];
|
|
1598
|
+
|
|
1599
|
+
FMResultSet *rs = [self getTableSchema:tableName];
|
|
1600
|
+
|
|
1601
|
+
//check if column is present in table schema
|
|
1602
|
+
while ([rs next]) {
|
|
1603
|
+
if ([[[rs stringForColumn:@"name"] lowercaseString] isEqualToString:columnName]) {
|
|
1604
|
+
returnBool = YES;
|
|
1605
|
+
break;
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
//If this is not done FMDatabase instance stays out of pool
|
|
1610
|
+
[rs close];
|
|
1611
|
+
|
|
1612
|
+
return returnBool;
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
|
|
1616
|
+
|
|
1617
|
+
- (uint32_t)applicationID {
|
|
1618
|
+
#if SQLITE_VERSION_NUMBER >= 3007017
|
|
1619
|
+
uint32_t r = 0;
|
|
1620
|
+
|
|
1621
|
+
FMResultSet *rs = [self executeQuery:@"pragma application_id"];
|
|
1622
|
+
|
|
1623
|
+
if ([rs next]) {
|
|
1624
|
+
r = (uint32_t)[rs longLongIntForColumnIndex:0];
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
[rs close];
|
|
1628
|
+
|
|
1629
|
+
return r;
|
|
1630
|
+
#else
|
|
1631
|
+
NSString *errorMessage = NSLocalizedString(@"Application ID functions require SQLite 3.7.17", nil);
|
|
1632
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
1633
|
+
return 0;
|
|
1634
|
+
#endif
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
- (void)setApplicationID:(uint32_t)appID {
|
|
1638
|
+
#if SQLITE_VERSION_NUMBER >= 3007017
|
|
1639
|
+
NSString *query = [NSString stringWithFormat:@"pragma application_id=%d", appID];
|
|
1640
|
+
FMResultSet *rs = [self executeQuery:query];
|
|
1641
|
+
[rs next];
|
|
1642
|
+
[rs close];
|
|
1643
|
+
#else
|
|
1644
|
+
NSString *errorMessage = NSLocalizedString(@"Application ID functions require SQLite 3.7.17", nil);
|
|
1645
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
1646
|
+
#endif
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
|
|
1650
|
+
#if TARGET_OS_MAC && !TARGET_OS_IPHONE
|
|
1651
|
+
|
|
1652
|
+
- (NSString*)applicationIDString {
|
|
1653
|
+
#if SQLITE_VERSION_NUMBER >= 3007017
|
|
1654
|
+
NSString *s = NSFileTypeForHFSTypeCode([self applicationID]);
|
|
1655
|
+
|
|
1656
|
+
assert([s length] == 6);
|
|
1657
|
+
|
|
1658
|
+
s = [s substringWithRange:NSMakeRange(1, 4)];
|
|
1659
|
+
|
|
1660
|
+
|
|
1661
|
+
return s;
|
|
1662
|
+
#else
|
|
1663
|
+
NSString *errorMessage = NSLocalizedString(@"Application ID functions require SQLite 3.7.17", nil);
|
|
1664
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
1665
|
+
return nil;
|
|
1666
|
+
#endif
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
- (void)setApplicationIDString:(NSString*)s {
|
|
1670
|
+
#if SQLITE_VERSION_NUMBER >= 3007017
|
|
1671
|
+
if ([s length] != 4) {
|
|
1672
|
+
NSLog(@"setApplicationIDString: string passed is not exactly 4 chars long. (was %ld)", [s length]);
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
[self setApplicationID:NSHFSTypeCodeFromFileType([NSString stringWithFormat:@"'%@'", s])];
|
|
1676
|
+
#else
|
|
1677
|
+
NSString *errorMessage = NSLocalizedString(@"Application ID functions require SQLite 3.7.17", nil);
|
|
1678
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
1679
|
+
#endif
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
#endif
|
|
1683
|
+
|
|
1684
|
+
- (uint32_t)userVersion {
|
|
1685
|
+
uint32_t r = 0;
|
|
1686
|
+
|
|
1687
|
+
FMResultSet *rs = [self executeQuery:@"pragma user_version"];
|
|
1688
|
+
|
|
1689
|
+
if ([rs next]) {
|
|
1690
|
+
r = (uint32_t)[rs longLongIntForColumnIndex:0];
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
[rs close];
|
|
1694
|
+
return r;
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
- (void)setUserVersion:(uint32_t)version {
|
|
1698
|
+
NSString *query = [NSString stringWithFormat:@"pragma user_version = %d", version];
|
|
1699
|
+
FMResultSet *rs = [self executeQuery:query];
|
|
1700
|
+
[rs next];
|
|
1701
|
+
[rs close];
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
#pragma clang diagnostic push
|
|
1705
|
+
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
|
|
1706
|
+
|
|
1707
|
+
- (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName __attribute__ ((deprecated)) {
|
|
1708
|
+
return [self columnExists:columnName inTableWithName:tableName];
|
|
1709
|
+
}
|
|
1710
|
+
|
|
1711
|
+
#pragma clang diagnostic pop
|
|
1712
|
+
|
|
1713
|
+
|
|
1714
|
+
- (BOOL)validateSQL:(NSString*)sql error:(NSError**)error {
|
|
1715
|
+
sqlite3_stmt *pStmt = NULL;
|
|
1716
|
+
BOOL validationSucceeded = YES;
|
|
1717
|
+
|
|
1718
|
+
int rc = sqlite3_prepare_v2(_db, [sql UTF8String], -1, &pStmt, 0);
|
|
1719
|
+
if (rc != SQLITE_OK) {
|
|
1720
|
+
validationSucceeded = NO;
|
|
1721
|
+
if (error) {
|
|
1722
|
+
*error = [NSError errorWithDomain:NSCocoaErrorDomain
|
|
1723
|
+
code:[self lastErrorCode]
|
|
1724
|
+
userInfo:[NSDictionary dictionaryWithObject:[self lastErrorMessage]
|
|
1725
|
+
forKey:NSLocalizedDescriptionKey]];
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
sqlite3_finalize(pStmt);
|
|
1730
|
+
|
|
1731
|
+
return validationSucceeded;
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
@end
|
|
1735
|
+
//
|
|
1736
|
+
// FMDatabasePool.m
|
|
1737
|
+
// fmdb
|
|
1738
|
+
//
|
|
1739
|
+
// Created by August Mueller on 6/22/11.
|
|
1740
|
+
// Copyright 2011 Flying Meat Inc. All rights reserved.
|
|
1741
|
+
//
|
|
1742
|
+
|
|
1743
|
+
@interface FMDatabasePool()
|
|
1744
|
+
|
|
1745
|
+
- (void)pushDatabaseBackInPool:(FMDatabase*)db;
|
|
1746
|
+
- (FMDatabase*)db;
|
|
1747
|
+
|
|
1748
|
+
@end
|
|
1749
|
+
|
|
1750
|
+
|
|
1751
|
+
@implementation FMDatabasePool
|
|
1752
|
+
@synthesize path=_path;
|
|
1753
|
+
@synthesize delegate=_delegate;
|
|
1754
|
+
@synthesize maximumNumberOfDatabasesToCreate=_maximumNumberOfDatabasesToCreate;
|
|
1755
|
+
@synthesize openFlags=_openFlags;
|
|
1756
|
+
|
|
1757
|
+
|
|
1758
|
+
+ (instancetype)databasePoolWithPath:(NSString*)aPath {
|
|
1759
|
+
return FMDBReturnAutoreleased([[self alloc] initWithPath:aPath]);
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
+ (instancetype)databasePoolWithPath:(NSString*)aPath flags:(int)openFlags {
|
|
1763
|
+
return FMDBReturnAutoreleased([[self alloc] initWithPath:aPath flags:openFlags]);
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName {
|
|
1767
|
+
|
|
1768
|
+
self = [super init];
|
|
1769
|
+
|
|
1770
|
+
if (self != nil) {
|
|
1771
|
+
_path = [aPath copy];
|
|
1772
|
+
_lockQueue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], NULL);
|
|
1773
|
+
_databaseInPool = FMDBReturnRetained([NSMutableArray array]);
|
|
1774
|
+
_databaseOutPool = FMDBReturnRetained([NSMutableArray array]);
|
|
1775
|
+
_openFlags = openFlags;
|
|
1776
|
+
_vfsName = [vfsName copy];
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
return self;
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
|
|
1783
|
+
return [self initWithPath:aPath flags:openFlags vfs:nil];
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
- (instancetype)initWithPath:(NSString*)aPath
|
|
1787
|
+
{
|
|
1788
|
+
// default flags for sqlite3_open
|
|
1789
|
+
return [self initWithPath:aPath flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE];
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
- (instancetype)init {
|
|
1793
|
+
return [self initWithPath:nil];
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
+ (Class)databaseClass {
|
|
1797
|
+
return [FMDatabase class];
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
- (void)dealloc {
|
|
1801
|
+
|
|
1802
|
+
_delegate = 0x00;
|
|
1803
|
+
FMDBRelease(_path);
|
|
1804
|
+
FMDBRelease(_databaseInPool);
|
|
1805
|
+
FMDBRelease(_databaseOutPool);
|
|
1806
|
+
|
|
1807
|
+
if (_lockQueue) {
|
|
1808
|
+
FMDBDispatchQueueRelease(_lockQueue);
|
|
1809
|
+
_lockQueue = 0x00;
|
|
1810
|
+
}
|
|
1811
|
+
#if ! __has_feature(objc_arc)
|
|
1812
|
+
[super dealloc];
|
|
1813
|
+
#endif
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
|
|
1817
|
+
- (void)executeLocked:(void (^)(void))aBlock {
|
|
1818
|
+
dispatch_sync(_lockQueue, aBlock);
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
- (void)pushDatabaseBackInPool:(FMDatabase*)db {
|
|
1822
|
+
|
|
1823
|
+
if (!db) { // db can be null if we set an upper bound on the # of databases to create.
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
[self executeLocked:^() {
|
|
1828
|
+
|
|
1829
|
+
if ([self->_databaseInPool containsObject:db]) {
|
|
1830
|
+
[[NSException exceptionWithName:@"Database already in pool" reason:@"The FMDatabase being put back into the pool is already present in the pool" userInfo:nil] raise];
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
[self->_databaseInPool addObject:db];
|
|
1834
|
+
[self->_databaseOutPool removeObject:db];
|
|
1835
|
+
|
|
1836
|
+
}];
|
|
1837
|
+
}
|
|
1838
|
+
|
|
1839
|
+
- (FMDatabase*)db {
|
|
1840
|
+
|
|
1841
|
+
__block FMDatabase *db;
|
|
1842
|
+
|
|
1843
|
+
|
|
1844
|
+
[self executeLocked:^() {
|
|
1845
|
+
db = [self->_databaseInPool lastObject];
|
|
1846
|
+
|
|
1847
|
+
BOOL shouldNotifyDelegate = NO;
|
|
1848
|
+
|
|
1849
|
+
if (db) {
|
|
1850
|
+
[self->_databaseOutPool addObject:db];
|
|
1851
|
+
[self->_databaseInPool removeLastObject];
|
|
1852
|
+
}
|
|
1853
|
+
else {
|
|
1854
|
+
|
|
1855
|
+
if (self->_maximumNumberOfDatabasesToCreate) {
|
|
1856
|
+
NSUInteger currentCount = [self->_databaseOutPool count] + [self->_databaseInPool count];
|
|
1857
|
+
|
|
1858
|
+
if (currentCount >= self->_maximumNumberOfDatabasesToCreate) {
|
|
1859
|
+
NSLog(@"Maximum number of databases (%ld) has already been reached!", (long)currentCount);
|
|
1860
|
+
return;
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
db = [[[self class] databaseClass] databaseWithPath:self->_path];
|
|
1865
|
+
shouldNotifyDelegate = YES;
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
//This ensures that the db is opened before returning
|
|
1869
|
+
#if SQLITE_VERSION_NUMBER >= 3005000
|
|
1870
|
+
BOOL success = [db openWithFlags:self->_openFlags vfs:self->_vfsName];
|
|
1871
|
+
#else
|
|
1872
|
+
BOOL success = [db open];
|
|
1873
|
+
#endif
|
|
1874
|
+
if (success) {
|
|
1875
|
+
if ([self->_delegate respondsToSelector:@selector(databasePool:shouldAddDatabaseToPool:)] && ![self->_delegate databasePool:self shouldAddDatabaseToPool:db]) {
|
|
1876
|
+
[db close];
|
|
1877
|
+
db = 0x00;
|
|
1878
|
+
}
|
|
1879
|
+
else {
|
|
1880
|
+
//It should not get added in the pool twice if lastObject was found
|
|
1881
|
+
if (![self->_databaseOutPool containsObject:db]) {
|
|
1882
|
+
[self->_databaseOutPool addObject:db];
|
|
1883
|
+
|
|
1884
|
+
if (shouldNotifyDelegate && [self->_delegate respondsToSelector:@selector(databasePool:didAddDatabase:)]) {
|
|
1885
|
+
[self->_delegate databasePool:self didAddDatabase:db];
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
else {
|
|
1891
|
+
NSLog(@"Could not open up the database at path %@", self->_path);
|
|
1892
|
+
db = 0x00;
|
|
1893
|
+
}
|
|
1894
|
+
}];
|
|
1895
|
+
|
|
1896
|
+
return db;
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
- (NSUInteger)countOfCheckedInDatabases {
|
|
1900
|
+
|
|
1901
|
+
__block NSUInteger count;
|
|
1902
|
+
|
|
1903
|
+
[self executeLocked:^() {
|
|
1904
|
+
count = [self->_databaseInPool count];
|
|
1905
|
+
}];
|
|
1906
|
+
|
|
1907
|
+
return count;
|
|
1908
|
+
}
|
|
1909
|
+
|
|
1910
|
+
- (NSUInteger)countOfCheckedOutDatabases {
|
|
1911
|
+
|
|
1912
|
+
__block NSUInteger count;
|
|
1913
|
+
|
|
1914
|
+
[self executeLocked:^() {
|
|
1915
|
+
count = [self->_databaseOutPool count];
|
|
1916
|
+
}];
|
|
1917
|
+
|
|
1918
|
+
return count;
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
- (NSUInteger)countOfOpenDatabases {
|
|
1922
|
+
__block NSUInteger count;
|
|
1923
|
+
|
|
1924
|
+
[self executeLocked:^() {
|
|
1925
|
+
count = [self->_databaseOutPool count] + [self->_databaseInPool count];
|
|
1926
|
+
}];
|
|
1927
|
+
|
|
1928
|
+
return count;
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
- (void)releaseAllDatabases {
|
|
1932
|
+
[self executeLocked:^() {
|
|
1933
|
+
[self->_databaseOutPool removeAllObjects];
|
|
1934
|
+
[self->_databaseInPool removeAllObjects];
|
|
1935
|
+
}];
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
- (void)inDatabase:(void (^)(FMDatabase *db))block {
|
|
1939
|
+
|
|
1940
|
+
FMDatabase *db = [self db];
|
|
1941
|
+
|
|
1942
|
+
block(db);
|
|
1943
|
+
|
|
1944
|
+
[self pushDatabaseBackInPool:db];
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
- (void)beginTransaction:(BOOL)useDeferred withBlock:(void (^)(FMDatabase *db, BOOL *rollback))block {
|
|
1948
|
+
|
|
1949
|
+
BOOL shouldRollback = NO;
|
|
1950
|
+
|
|
1951
|
+
FMDatabase *db = [self db];
|
|
1952
|
+
|
|
1953
|
+
if (useDeferred) {
|
|
1954
|
+
[db beginDeferredTransaction];
|
|
1955
|
+
}
|
|
1956
|
+
else {
|
|
1957
|
+
[db beginTransaction];
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
|
|
1961
|
+
block(db, &shouldRollback);
|
|
1962
|
+
|
|
1963
|
+
if (shouldRollback) {
|
|
1964
|
+
[db rollback];
|
|
1965
|
+
}
|
|
1966
|
+
else {
|
|
1967
|
+
[db commit];
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
[self pushDatabaseBackInPool:db];
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
- (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
|
|
1974
|
+
[self beginTransaction:YES withBlock:block];
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
- (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
|
|
1978
|
+
[self beginTransaction:NO withBlock:block];
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
- (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
|
|
1982
|
+
#if SQLITE_VERSION_NUMBER >= 3007000
|
|
1983
|
+
static unsigned long savePointIdx = 0;
|
|
1984
|
+
|
|
1985
|
+
NSString *name = [NSString stringWithFormat:@"savePoint%ld", savePointIdx++];
|
|
1986
|
+
|
|
1987
|
+
BOOL shouldRollback = NO;
|
|
1988
|
+
|
|
1989
|
+
FMDatabase *db = [self db];
|
|
1990
|
+
|
|
1991
|
+
NSError *err = 0x00;
|
|
1992
|
+
|
|
1993
|
+
if (![db startSavePointWithName:name error:&err]) {
|
|
1994
|
+
[self pushDatabaseBackInPool:db];
|
|
1995
|
+
return err;
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
block(db, &shouldRollback);
|
|
1999
|
+
|
|
2000
|
+
if (shouldRollback) {
|
|
2001
|
+
// We need to rollback and release this savepoint to remove it
|
|
2002
|
+
[db rollbackToSavePointWithName:name error:&err];
|
|
2003
|
+
}
|
|
2004
|
+
[db releaseSavePointWithName:name error:&err];
|
|
2005
|
+
|
|
2006
|
+
[self pushDatabaseBackInPool:db];
|
|
2007
|
+
|
|
2008
|
+
return err;
|
|
2009
|
+
#else
|
|
2010
|
+
NSString *errorMessage = NSLocalizedString(@"Save point functions require SQLite 3.7", nil);
|
|
2011
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
2012
|
+
return [NSError errorWithDomain:@"FMDatabase" code:0 userInfo:@{NSLocalizedDescriptionKey : errorMessage}];
|
|
2013
|
+
#endif
|
|
2014
|
+
}
|
|
2015
|
+
|
|
2016
|
+
@end
|
|
2017
|
+
//
|
|
2018
|
+
// FMDatabaseQueue.m
|
|
2019
|
+
// fmdb
|
|
2020
|
+
//
|
|
2021
|
+
// Created by August Mueller on 6/22/11.
|
|
2022
|
+
// Copyright 2011 Flying Meat Inc. All rights reserved.
|
|
2023
|
+
//
|
|
2024
|
+
|
|
2025
|
+
/*
|
|
2026
|
+
|
|
2027
|
+
Note: we call [self retain]; before using dispatch_sync, just incase
|
|
2028
|
+
FMDatabaseQueue is released on another thread and we're in the middle of doing
|
|
2029
|
+
something in dispatch_sync
|
|
2030
|
+
|
|
2031
|
+
*/
|
|
2032
|
+
|
|
2033
|
+
/*
|
|
2034
|
+
* A key used to associate the FMDatabaseQueue object with the dispatch_queue_t it uses.
|
|
2035
|
+
* This in turn is used for deadlock detection by seeing if inDatabase: is called on
|
|
2036
|
+
* the queue's dispatch queue, which should not happen and causes a deadlock.
|
|
2037
|
+
*/
|
|
2038
|
+
static const void * const kDispatchQueueSpecificKey = &kDispatchQueueSpecificKey;
|
|
2039
|
+
|
|
2040
|
+
@implementation FMDatabaseQueue
|
|
2041
|
+
|
|
2042
|
+
@synthesize path = _path;
|
|
2043
|
+
@synthesize openFlags = _openFlags;
|
|
2044
|
+
@synthesize vfsName = _vfsName;
|
|
2045
|
+
|
|
2046
|
+
+ (instancetype)databaseQueueWithPath:(NSString*)aPath {
|
|
2047
|
+
|
|
2048
|
+
FMDatabaseQueue *q = [[self alloc] initWithPath:aPath];
|
|
2049
|
+
|
|
2050
|
+
FMDBAutorelease(q);
|
|
2051
|
+
|
|
2052
|
+
return q;
|
|
2053
|
+
}
|
|
2054
|
+
|
|
2055
|
+
+ (instancetype)databaseQueueWithPath:(NSString*)aPath flags:(int)openFlags {
|
|
2056
|
+
|
|
2057
|
+
FMDatabaseQueue *q = [[self alloc] initWithPath:aPath flags:openFlags];
|
|
2058
|
+
|
|
2059
|
+
FMDBAutorelease(q);
|
|
2060
|
+
|
|
2061
|
+
return q;
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
+ (Class)databaseClass {
|
|
2065
|
+
return [FMDatabase class];
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName {
|
|
2069
|
+
|
|
2070
|
+
self = [super init];
|
|
2071
|
+
|
|
2072
|
+
if (self != nil) {
|
|
2073
|
+
|
|
2074
|
+
_db = [[[self class] databaseClass] databaseWithPath:aPath];
|
|
2075
|
+
FMDBRetain(_db);
|
|
2076
|
+
|
|
2077
|
+
#if SQLITE_VERSION_NUMBER >= 3005000
|
|
2078
|
+
BOOL success = [_db openWithFlags:openFlags vfs:vfsName];
|
|
2079
|
+
#else
|
|
2080
|
+
BOOL success = [_db open];
|
|
2081
|
+
#endif
|
|
2082
|
+
if (!success) {
|
|
2083
|
+
NSLog(@"Could not create database queue for path %@", aPath);
|
|
2084
|
+
FMDBRelease(self);
|
|
2085
|
+
return 0x00;
|
|
2086
|
+
}
|
|
2087
|
+
|
|
2088
|
+
_path = FMDBReturnRetained(aPath);
|
|
2089
|
+
|
|
2090
|
+
_queue = dispatch_queue_create([[NSString stringWithFormat:@"fmdb.%@", self] UTF8String], NULL);
|
|
2091
|
+
dispatch_queue_set_specific(_queue, kDispatchQueueSpecificKey, (__bridge void *)self, NULL);
|
|
2092
|
+
_openFlags = openFlags;
|
|
2093
|
+
_vfsName = [vfsName copy];
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
return self;
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags {
|
|
2100
|
+
return [self initWithPath:aPath flags:openFlags vfs:nil];
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
- (instancetype)initWithPath:(NSString*)aPath {
|
|
2104
|
+
|
|
2105
|
+
// default flags for sqlite3_open
|
|
2106
|
+
return [self initWithPath:aPath flags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE vfs:nil];
|
|
2107
|
+
}
|
|
2108
|
+
|
|
2109
|
+
- (instancetype)init {
|
|
2110
|
+
return [self initWithPath:nil];
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2113
|
+
|
|
2114
|
+
- (void)dealloc {
|
|
2115
|
+
|
|
2116
|
+
FMDBRelease(_db);
|
|
2117
|
+
FMDBRelease(_path);
|
|
2118
|
+
|
|
2119
|
+
if (_queue) {
|
|
2120
|
+
FMDBDispatchQueueRelease(_queue);
|
|
2121
|
+
_queue = 0x00;
|
|
2122
|
+
}
|
|
2123
|
+
#if ! __has_feature(objc_arc)
|
|
2124
|
+
[super dealloc];
|
|
2125
|
+
#endif
|
|
2126
|
+
}
|
|
2127
|
+
|
|
2128
|
+
- (void)close {
|
|
2129
|
+
FMDBRetain(self);
|
|
2130
|
+
dispatch_sync(_queue, ^() {
|
|
2131
|
+
[self->_db close];
|
|
2132
|
+
FMDBRelease(_db);
|
|
2133
|
+
self->_db = 0x00;
|
|
2134
|
+
});
|
|
2135
|
+
FMDBRelease(self);
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
- (void)interrupt
|
|
2139
|
+
{
|
|
2140
|
+
[[self database] interrupt];
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
- (FMDatabase*)database {
|
|
2144
|
+
if (!_db) {
|
|
2145
|
+
_db = FMDBReturnRetained([[[self class] databaseClass] databaseWithPath:_path]);
|
|
2146
|
+
|
|
2147
|
+
#if SQLITE_VERSION_NUMBER >= 3005000
|
|
2148
|
+
BOOL success = [_db openWithFlags:_openFlags vfs:_vfsName];
|
|
2149
|
+
#else
|
|
2150
|
+
BOOL success = [_db open];
|
|
2151
|
+
#endif
|
|
2152
|
+
if (!success) {
|
|
2153
|
+
NSLog(@"FMDatabaseQueue could not reopen database for path %@", _path);
|
|
2154
|
+
FMDBRelease(_db);
|
|
2155
|
+
_db = 0x00;
|
|
2156
|
+
return 0x00;
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
return _db;
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
- (void)inDatabase:(void (^)(FMDatabase *db))block {
|
|
2164
|
+
/* Get the currently executing queue (which should probably be nil, but in theory could be another DB queue
|
|
2165
|
+
* and then check it against self to make sure we're not about to deadlock. */
|
|
2166
|
+
FMDatabaseQueue *currentSyncQueue = (__bridge id)dispatch_get_specific(kDispatchQueueSpecificKey);
|
|
2167
|
+
assert(currentSyncQueue != self && "inDatabase: was called reentrantly on the same queue, which would lead to a deadlock");
|
|
2168
|
+
|
|
2169
|
+
FMDBRetain(self);
|
|
2170
|
+
|
|
2171
|
+
dispatch_sync(_queue, ^() {
|
|
2172
|
+
|
|
2173
|
+
FMDatabase *db = [self database];
|
|
2174
|
+
block(db);
|
|
2175
|
+
|
|
2176
|
+
if ([db hasOpenResultSets]) {
|
|
2177
|
+
NSLog(@"Warning: there is at least one open result set around after performing [FMDatabaseQueue inDatabase:]");
|
|
2178
|
+
|
|
2179
|
+
#if defined(DEBUG) && DEBUG
|
|
2180
|
+
NSSet *openSetCopy = FMDBReturnAutoreleased([[db valueForKey:@"_openResultSets"] copy]);
|
|
2181
|
+
for (NSValue *rsInWrappedInATastyValueMeal in openSetCopy) {
|
|
2182
|
+
FMResultSet *rs = (FMResultSet *)[rsInWrappedInATastyValueMeal pointerValue];
|
|
2183
|
+
NSLog(@"query: '%@'", [rs query]);
|
|
2184
|
+
}
|
|
2185
|
+
#endif
|
|
2186
|
+
}
|
|
2187
|
+
});
|
|
2188
|
+
|
|
2189
|
+
FMDBRelease(self);
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
|
|
2193
|
+
- (void)beginTransaction:(BOOL)useDeferred withBlock:(void (^)(FMDatabase *db, BOOL *rollback))block {
|
|
2194
|
+
FMDBRetain(self);
|
|
2195
|
+
dispatch_sync(_queue, ^() {
|
|
2196
|
+
|
|
2197
|
+
BOOL shouldRollback = NO;
|
|
2198
|
+
|
|
2199
|
+
if (useDeferred) {
|
|
2200
|
+
[[self database] beginDeferredTransaction];
|
|
2201
|
+
}
|
|
2202
|
+
else {
|
|
2203
|
+
[[self database] beginTransaction];
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
block([self database], &shouldRollback);
|
|
2207
|
+
|
|
2208
|
+
if (shouldRollback) {
|
|
2209
|
+
[[self database] rollback];
|
|
2210
|
+
}
|
|
2211
|
+
else {
|
|
2212
|
+
[[self database] commit];
|
|
2213
|
+
}
|
|
2214
|
+
});
|
|
2215
|
+
|
|
2216
|
+
FMDBRelease(self);
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
- (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
|
|
2220
|
+
[self beginTransaction:YES withBlock:block];
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
- (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block {
|
|
2224
|
+
[self beginTransaction:NO withBlock:block];
|
|
2225
|
+
}
|
|
2226
|
+
|
|
2227
|
+
- (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block {
|
|
2228
|
+
#if SQLITE_VERSION_NUMBER >= 3007000
|
|
2229
|
+
static unsigned long savePointIdx = 0;
|
|
2230
|
+
__block NSError *err = 0x00;
|
|
2231
|
+
FMDBRetain(self);
|
|
2232
|
+
dispatch_sync(_queue, ^() {
|
|
2233
|
+
|
|
2234
|
+
NSString *name = [NSString stringWithFormat:@"savePoint%ld", savePointIdx++];
|
|
2235
|
+
|
|
2236
|
+
BOOL shouldRollback = NO;
|
|
2237
|
+
|
|
2238
|
+
if ([[self database] startSavePointWithName:name error:&err]) {
|
|
2239
|
+
|
|
2240
|
+
block([self database], &shouldRollback);
|
|
2241
|
+
|
|
2242
|
+
if (shouldRollback) {
|
|
2243
|
+
// We need to rollback and release this savepoint to remove it
|
|
2244
|
+
[[self database] rollbackToSavePointWithName:name error:&err];
|
|
2245
|
+
}
|
|
2246
|
+
[[self database] releaseSavePointWithName:name error:&err];
|
|
2247
|
+
|
|
2248
|
+
}
|
|
2249
|
+
});
|
|
2250
|
+
FMDBRelease(self);
|
|
2251
|
+
return err;
|
|
2252
|
+
#else
|
|
2253
|
+
NSString *errorMessage = NSLocalizedString(@"Save point functions require SQLite 3.7", nil);
|
|
2254
|
+
if (self.logsErrors) NSLog(@"%@", errorMessage);
|
|
2255
|
+
return [NSError errorWithDomain:@"FMDatabase" code:0 userInfo:@{NSLocalizedDescriptionKey : errorMessage}];
|
|
2256
|
+
#endif
|
|
2257
|
+
}
|
|
2258
|
+
|
|
2259
|
+
@end
|
|
2260
|
+
|
|
2261
|
+
//@interface FMDatabase ()
|
|
2262
|
+
//- (void)resultSetDidClose:(FMResultSet *)resultSet;
|
|
2263
|
+
//@end
|
|
2264
|
+
|
|
2265
|
+
|
|
2266
|
+
@implementation FMResultSet
|
|
2267
|
+
@synthesize query=_query;
|
|
2268
|
+
@synthesize statement=_statement;
|
|
2269
|
+
|
|
2270
|
+
+ (instancetype)resultSetWithStatement:(FMStatement *)statement usingParentDatabase:(FMDatabase*)aDB {
|
|
2271
|
+
|
|
2272
|
+
FMResultSet *rs = [[FMResultSet alloc] init];
|
|
2273
|
+
|
|
2274
|
+
[rs setStatement:statement];
|
|
2275
|
+
[rs setParentDB:aDB];
|
|
2276
|
+
|
|
2277
|
+
NSParameterAssert(![statement inUse]);
|
|
2278
|
+
[statement setInUse:YES]; // weak reference
|
|
2279
|
+
|
|
2280
|
+
return FMDBReturnAutoreleased(rs);
|
|
2281
|
+
}
|
|
2282
|
+
|
|
2283
|
+
- (void)finalize {
|
|
2284
|
+
[self close];
|
|
2285
|
+
[super finalize];
|
|
2286
|
+
}
|
|
2287
|
+
|
|
2288
|
+
- (void)dealloc {
|
|
2289
|
+
[self close];
|
|
2290
|
+
|
|
2291
|
+
FMDBRelease(_query);
|
|
2292
|
+
_query = nil;
|
|
2293
|
+
|
|
2294
|
+
FMDBRelease(_columnNameToIndexMap);
|
|
2295
|
+
_columnNameToIndexMap = nil;
|
|
2296
|
+
|
|
2297
|
+
#if ! __has_feature(objc_arc)
|
|
2298
|
+
[super dealloc];
|
|
2299
|
+
#endif
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
- (void)close {
|
|
2303
|
+
[_statement reset];
|
|
2304
|
+
FMDBRelease(_statement);
|
|
2305
|
+
_statement = nil;
|
|
2306
|
+
|
|
2307
|
+
// we don't need this anymore... (i think)
|
|
2308
|
+
//[_parentDB setInUse:NO];
|
|
2309
|
+
[_parentDB resultSetDidClose:self];
|
|
2310
|
+
_parentDB = nil;
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
- (int)columnCount {
|
|
2314
|
+
return sqlite3_column_count([_statement statement]);
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
- (NSMutableDictionary *)columnNameToIndexMap {
|
|
2318
|
+
if (!_columnNameToIndexMap) {
|
|
2319
|
+
int columnCount = sqlite3_column_count([_statement statement]);
|
|
2320
|
+
_columnNameToIndexMap = [[NSMutableDictionary alloc] initWithCapacity:(NSUInteger)columnCount];
|
|
2321
|
+
int columnIdx = 0;
|
|
2322
|
+
for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
|
|
2323
|
+
[_columnNameToIndexMap setObject:[NSNumber numberWithInt:columnIdx]
|
|
2324
|
+
forKey:[[NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)] lowercaseString]];
|
|
2325
|
+
}
|
|
2326
|
+
}
|
|
2327
|
+
return _columnNameToIndexMap;
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
- (void)kvcMagic:(id)object {
|
|
2331
|
+
|
|
2332
|
+
int columnCount = sqlite3_column_count([_statement statement]);
|
|
2333
|
+
|
|
2334
|
+
int columnIdx = 0;
|
|
2335
|
+
for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
|
|
2336
|
+
|
|
2337
|
+
const char *c = (const char *)sqlite3_column_text([_statement statement], columnIdx);
|
|
2338
|
+
|
|
2339
|
+
// check for a null row
|
|
2340
|
+
if (c) {
|
|
2341
|
+
NSString *s = [NSString stringWithUTF8String:c];
|
|
2342
|
+
|
|
2343
|
+
[object setValue:s forKey:[NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)]];
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
}
|
|
2347
|
+
|
|
2348
|
+
#pragma clang diagnostic push
|
|
2349
|
+
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
|
|
2350
|
+
|
|
2351
|
+
- (NSDictionary*)resultDict {
|
|
2352
|
+
|
|
2353
|
+
NSUInteger num_cols = (NSUInteger)sqlite3_data_count([_statement statement]);
|
|
2354
|
+
|
|
2355
|
+
if (num_cols > 0) {
|
|
2356
|
+
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:num_cols];
|
|
2357
|
+
|
|
2358
|
+
NSEnumerator *columnNames = [[self columnNameToIndexMap] keyEnumerator];
|
|
2359
|
+
NSString *columnName = nil;
|
|
2360
|
+
while ((columnName = [columnNames nextObject])) {
|
|
2361
|
+
id objectValue = [self objectForColumnName:columnName];
|
|
2362
|
+
[dict setObject:objectValue forKey:columnName];
|
|
2363
|
+
}
|
|
2364
|
+
|
|
2365
|
+
return FMDBReturnAutoreleased([dict copy]);
|
|
2366
|
+
}
|
|
2367
|
+
else {
|
|
2368
|
+
NSLog(@"Warning: There seem to be no columns in this set.");
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
return nil;
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
#pragma clang diagnostic pop
|
|
2375
|
+
|
|
2376
|
+
- (NSDictionary*)resultDictionary {
|
|
2377
|
+
|
|
2378
|
+
NSUInteger num_cols = (NSUInteger)sqlite3_data_count([_statement statement]);
|
|
2379
|
+
|
|
2380
|
+
if (num_cols > 0) {
|
|
2381
|
+
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:num_cols];
|
|
2382
|
+
|
|
2383
|
+
int columnCount = sqlite3_column_count([_statement statement]);
|
|
2384
|
+
|
|
2385
|
+
int columnIdx = 0;
|
|
2386
|
+
for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
|
|
2387
|
+
|
|
2388
|
+
NSString *columnName = [NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)];
|
|
2389
|
+
id objectValue = [self objectForColumnIndex:columnIdx];
|
|
2390
|
+
[dict setObject:objectValue forKey:columnName];
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
return dict;
|
|
2394
|
+
}
|
|
2395
|
+
else {
|
|
2396
|
+
NSLog(@"Warning: There seem to be no columns in this set.");
|
|
2397
|
+
}
|
|
2398
|
+
|
|
2399
|
+
return nil;
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
|
|
2403
|
+
|
|
2404
|
+
|
|
2405
|
+
- (BOOL)next {
|
|
2406
|
+
return [self nextWithError:nil];
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
- (BOOL)nextWithError:(NSError **)outErr {
|
|
2410
|
+
|
|
2411
|
+
int rc = sqlite3_step([_statement statement]);
|
|
2412
|
+
|
|
2413
|
+
if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
|
|
2414
|
+
NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [_parentDB databasePath]);
|
|
2415
|
+
NSLog(@"Database busy");
|
|
2416
|
+
if (outErr) {
|
|
2417
|
+
*outErr = [_parentDB lastError];
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
else if (SQLITE_DONE == rc || SQLITE_ROW == rc) {
|
|
2421
|
+
// all is well, let's return.
|
|
2422
|
+
}
|
|
2423
|
+
else if (SQLITE_ERROR == rc) {
|
|
2424
|
+
NSLog(@"Error calling sqlite3_step (%d: %s) rs", rc, sqlite3_errmsg([_parentDB sqliteHandle]));
|
|
2425
|
+
if (outErr) {
|
|
2426
|
+
*outErr = [_parentDB lastError];
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
else if (SQLITE_MISUSE == rc) {
|
|
2430
|
+
// uh oh.
|
|
2431
|
+
NSLog(@"Error calling sqlite3_step (%d: %s) rs", rc, sqlite3_errmsg([_parentDB sqliteHandle]));
|
|
2432
|
+
if (outErr) {
|
|
2433
|
+
if (_parentDB) {
|
|
2434
|
+
*outErr = [_parentDB lastError];
|
|
2435
|
+
}
|
|
2436
|
+
else {
|
|
2437
|
+
// If 'next' or 'nextWithError' is called after the result set is closed,
|
|
2438
|
+
// we need to return the appropriate error.
|
|
2439
|
+
NSDictionary* errorMessage = [NSDictionary dictionaryWithObject:@"parentDB does not exist" forKey:NSLocalizedDescriptionKey];
|
|
2440
|
+
*outErr = [NSError errorWithDomain:@"FMDatabase" code:SQLITE_MISUSE userInfo:errorMessage];
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
}
|
|
2444
|
+
}
|
|
2445
|
+
else {
|
|
2446
|
+
// wtf?
|
|
2447
|
+
NSLog(@"Unknown error calling sqlite3_step (%d: %s) rs", rc, sqlite3_errmsg([_parentDB sqliteHandle]));
|
|
2448
|
+
if (outErr) {
|
|
2449
|
+
*outErr = [_parentDB lastError];
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
|
|
2453
|
+
|
|
2454
|
+
if (rc != SQLITE_ROW) {
|
|
2455
|
+
[self close];
|
|
2456
|
+
}
|
|
2457
|
+
|
|
2458
|
+
return (rc == SQLITE_ROW);
|
|
2459
|
+
}
|
|
2460
|
+
|
|
2461
|
+
- (BOOL)hasAnotherRow {
|
|
2462
|
+
return sqlite3_errcode([_parentDB sqliteHandle]) == SQLITE_ROW;
|
|
2463
|
+
}
|
|
2464
|
+
|
|
2465
|
+
- (int)columnIndexForName:(NSString*)columnName {
|
|
2466
|
+
columnName = [columnName lowercaseString];
|
|
2467
|
+
|
|
2468
|
+
NSNumber *n = [[self columnNameToIndexMap] objectForKey:columnName];
|
|
2469
|
+
|
|
2470
|
+
if (n) {
|
|
2471
|
+
return [n intValue];
|
|
2472
|
+
}
|
|
2473
|
+
|
|
2474
|
+
NSLog(@"Warning: I could not find the column named '%@'.", columnName);
|
|
2475
|
+
|
|
2476
|
+
return -1;
|
|
2477
|
+
}
|
|
2478
|
+
|
|
2479
|
+
|
|
2480
|
+
|
|
2481
|
+
- (int)intForColumn:(NSString*)columnName {
|
|
2482
|
+
return [self intForColumnIndex:[self columnIndexForName:columnName]];
|
|
2483
|
+
}
|
|
2484
|
+
|
|
2485
|
+
- (int)intForColumnIndex:(int)columnIdx {
|
|
2486
|
+
return sqlite3_column_int([_statement statement], columnIdx);
|
|
2487
|
+
}
|
|
2488
|
+
|
|
2489
|
+
- (long)longForColumn:(NSString*)columnName {
|
|
2490
|
+
return [self longForColumnIndex:[self columnIndexForName:columnName]];
|
|
2491
|
+
}
|
|
2492
|
+
|
|
2493
|
+
- (long)longForColumnIndex:(int)columnIdx {
|
|
2494
|
+
return (long)sqlite3_column_int64([_statement statement], columnIdx);
|
|
2495
|
+
}
|
|
2496
|
+
|
|
2497
|
+
- (long long int)longLongIntForColumn:(NSString*)columnName {
|
|
2498
|
+
return [self longLongIntForColumnIndex:[self columnIndexForName:columnName]];
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
- (long long int)longLongIntForColumnIndex:(int)columnIdx {
|
|
2502
|
+
return sqlite3_column_int64([_statement statement], columnIdx);
|
|
2503
|
+
}
|
|
2504
|
+
|
|
2505
|
+
- (unsigned long long int)unsignedLongLongIntForColumn:(NSString*)columnName {
|
|
2506
|
+
return [self unsignedLongLongIntForColumnIndex:[self columnIndexForName:columnName]];
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
- (unsigned long long int)unsignedLongLongIntForColumnIndex:(int)columnIdx {
|
|
2510
|
+
return (unsigned long long int)[self longLongIntForColumnIndex:columnIdx];
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
- (BOOL)boolForColumn:(NSString*)columnName {
|
|
2514
|
+
return [self boolForColumnIndex:[self columnIndexForName:columnName]];
|
|
2515
|
+
}
|
|
2516
|
+
|
|
2517
|
+
- (BOOL)boolForColumnIndex:(int)columnIdx {
|
|
2518
|
+
return ([self intForColumnIndex:columnIdx] != 0);
|
|
2519
|
+
}
|
|
2520
|
+
|
|
2521
|
+
- (double)doubleForColumn:(NSString*)columnName {
|
|
2522
|
+
return [self doubleForColumnIndex:[self columnIndexForName:columnName]];
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
- (double)doubleForColumnIndex:(int)columnIdx {
|
|
2526
|
+
return sqlite3_column_double([_statement statement], columnIdx);
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
- (NSString*)stringForColumnIndex:(int)columnIdx {
|
|
2530
|
+
|
|
2531
|
+
if (sqlite3_column_type([_statement statement], columnIdx) == SQLITE_NULL || (columnIdx < 0)) {
|
|
2532
|
+
return nil;
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
const char *c = (const char *)sqlite3_column_text([_statement statement], columnIdx);
|
|
2536
|
+
|
|
2537
|
+
if (!c) {
|
|
2538
|
+
// null row.
|
|
2539
|
+
return nil;
|
|
2540
|
+
}
|
|
2541
|
+
|
|
2542
|
+
return [NSString stringWithUTF8String:c];
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
- (NSString*)stringForColumn:(NSString*)columnName {
|
|
2546
|
+
return [self stringForColumnIndex:[self columnIndexForName:columnName]];
|
|
2547
|
+
}
|
|
2548
|
+
|
|
2549
|
+
- (NSDate*)dateForColumn:(NSString*)columnName {
|
|
2550
|
+
return [self dateForColumnIndex:[self columnIndexForName:columnName]];
|
|
2551
|
+
}
|
|
2552
|
+
|
|
2553
|
+
- (NSDate*)dateForColumnIndex:(int)columnIdx {
|
|
2554
|
+
|
|
2555
|
+
if (sqlite3_column_type([_statement statement], columnIdx) == SQLITE_NULL || (columnIdx < 0)) {
|
|
2556
|
+
return nil;
|
|
2557
|
+
}
|
|
2558
|
+
|
|
2559
|
+
return [_parentDB hasDateFormatter] ? [_parentDB dateFromString:[self stringForColumnIndex:columnIdx]] : [NSDate dateWithTimeIntervalSince1970:[self doubleForColumnIndex:columnIdx]];
|
|
2560
|
+
}
|
|
2561
|
+
|
|
2562
|
+
|
|
2563
|
+
- (NSData*)dataForColumn:(NSString*)columnName {
|
|
2564
|
+
return [self dataForColumnIndex:[self columnIndexForName:columnName]];
|
|
2565
|
+
}
|
|
2566
|
+
|
|
2567
|
+
- (NSData*)dataForColumnIndex:(int)columnIdx {
|
|
2568
|
+
|
|
2569
|
+
if (sqlite3_column_type([_statement statement], columnIdx) == SQLITE_NULL || (columnIdx < 0)) {
|
|
2570
|
+
return nil;
|
|
2571
|
+
}
|
|
2572
|
+
|
|
2573
|
+
const char *dataBuffer = sqlite3_column_blob([_statement statement], columnIdx);
|
|
2574
|
+
int dataSize = sqlite3_column_bytes([_statement statement], columnIdx);
|
|
2575
|
+
|
|
2576
|
+
if (dataBuffer == NULL) {
|
|
2577
|
+
return nil;
|
|
2578
|
+
}
|
|
2579
|
+
|
|
2580
|
+
return [NSData dataWithBytes:(const void *)dataBuffer length:(NSUInteger)dataSize];
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
|
|
2584
|
+
- (NSData*)dataNoCopyForColumn:(NSString*)columnName {
|
|
2585
|
+
return [self dataNoCopyForColumnIndex:[self columnIndexForName:columnName]];
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
- (NSData*)dataNoCopyForColumnIndex:(int)columnIdx {
|
|
2589
|
+
|
|
2590
|
+
if (sqlite3_column_type([_statement statement], columnIdx) == SQLITE_NULL || (columnIdx < 0)) {
|
|
2591
|
+
return nil;
|
|
2592
|
+
}
|
|
2593
|
+
|
|
2594
|
+
const char *dataBuffer = sqlite3_column_blob([_statement statement], columnIdx);
|
|
2595
|
+
int dataSize = sqlite3_column_bytes([_statement statement], columnIdx);
|
|
2596
|
+
|
|
2597
|
+
NSData *data = [NSData dataWithBytesNoCopy:(void *)dataBuffer length:(NSUInteger)dataSize freeWhenDone:NO];
|
|
2598
|
+
|
|
2599
|
+
return data;
|
|
2600
|
+
}
|
|
2601
|
+
|
|
2602
|
+
|
|
2603
|
+
- (BOOL)columnIndexIsNull:(int)columnIdx {
|
|
2604
|
+
return sqlite3_column_type([_statement statement], columnIdx) == SQLITE_NULL;
|
|
2605
|
+
}
|
|
2606
|
+
|
|
2607
|
+
- (BOOL)columnIsNull:(NSString*)columnName {
|
|
2608
|
+
return [self columnIndexIsNull:[self columnIndexForName:columnName]];
|
|
2609
|
+
}
|
|
2610
|
+
|
|
2611
|
+
- (const unsigned char *)UTF8StringForColumnIndex:(int)columnIdx {
|
|
2612
|
+
|
|
2613
|
+
if (sqlite3_column_type([_statement statement], columnIdx) == SQLITE_NULL || (columnIdx < 0)) {
|
|
2614
|
+
return nil;
|
|
2615
|
+
}
|
|
2616
|
+
|
|
2617
|
+
return sqlite3_column_text([_statement statement], columnIdx);
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
- (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName {
|
|
2621
|
+
return [self UTF8StringForColumnIndex:[self columnIndexForName:columnName]];
|
|
2622
|
+
}
|
|
2623
|
+
|
|
2624
|
+
- (id)objectForColumnIndex:(int)columnIdx {
|
|
2625
|
+
int columnType = sqlite3_column_type([_statement statement], columnIdx);
|
|
2626
|
+
|
|
2627
|
+
id returnValue = nil;
|
|
2628
|
+
|
|
2629
|
+
if (columnType == SQLITE_INTEGER) {
|
|
2630
|
+
returnValue = [NSNumber numberWithLongLong:[self longLongIntForColumnIndex:columnIdx]];
|
|
2631
|
+
}
|
|
2632
|
+
else if (columnType == SQLITE_FLOAT) {
|
|
2633
|
+
returnValue = [NSNumber numberWithDouble:[self doubleForColumnIndex:columnIdx]];
|
|
2634
|
+
}
|
|
2635
|
+
else if (columnType == SQLITE_BLOB) {
|
|
2636
|
+
returnValue = [self dataForColumnIndex:columnIdx];
|
|
2637
|
+
}
|
|
2638
|
+
else {
|
|
2639
|
+
//default to a string for everything else
|
|
2640
|
+
returnValue = [self stringForColumnIndex:columnIdx];
|
|
2641
|
+
}
|
|
2642
|
+
|
|
2643
|
+
if (returnValue == nil) {
|
|
2644
|
+
returnValue = [NSNull null];
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
return returnValue;
|
|
2648
|
+
}
|
|
2649
|
+
|
|
2650
|
+
- (id)objectForColumnName:(NSString*)columnName {
|
|
2651
|
+
return [self objectForColumnIndex:[self columnIndexForName:columnName]];
|
|
2652
|
+
}
|
|
2653
|
+
|
|
2654
|
+
// returns autoreleased NSString containing the name of the column in the result set
|
|
2655
|
+
- (NSString*)columnNameForIndex:(int)columnIdx {
|
|
2656
|
+
return [NSString stringWithUTF8String: sqlite3_column_name([_statement statement], columnIdx)];
|
|
2657
|
+
}
|
|
2658
|
+
|
|
2659
|
+
- (void)setParentDB:(FMDatabase *)newDb {
|
|
2660
|
+
_parentDB = newDb;
|
|
2661
|
+
}
|
|
2662
|
+
|
|
2663
|
+
- (id)objectAtIndexedSubscript:(int)columnIdx {
|
|
2664
|
+
return [self objectForColumnIndex:columnIdx];
|
|
2665
|
+
}
|
|
2666
|
+
|
|
2667
|
+
- (id)objectForKeyedSubscript:(NSString *)columnName {
|
|
2668
|
+
return [self objectForColumnName:columnName];
|
|
2669
|
+
}
|
|
2670
|
+
|
|
2671
|
+
|
|
2672
|
+
@end
|