@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.
Files changed (207) hide show
  1. package/JosuelmmCapacitorBackgroundGeolocation.podspec +34 -0
  2. package/LICENSE +17 -0
  3. package/NOTICE.md +32 -0
  4. package/Package.swift +45 -0
  5. package/README.md +402 -0
  6. package/android/build.gradle +79 -0
  7. package/android/proguard-rules.pro +1 -0
  8. package/android/src/main/AndroidManifest.xml +83 -0
  9. package/android/src/main/java/com/evgenii/jsevaluator/HandlerWrapper.java +18 -0
  10. package/android/src/main/java/com/evgenii/jsevaluator/JavaScriptInterface.java +22 -0
  11. package/android/src/main/java/com/evgenii/jsevaluator/JsEvaluator.java +133 -0
  12. package/android/src/main/java/com/evgenii/jsevaluator/JsFunctionCallFormatter.java +37 -0
  13. package/android/src/main/java/com/evgenii/jsevaluator/WebViewWrapper.java +71 -0
  14. package/android/src/main/java/com/evgenii/jsevaluator/interfaces/CallJavaResultInterface.java +8 -0
  15. package/android/src/main/java/com/evgenii/jsevaluator/interfaces/HandlerWrapperInterface.java +5 -0
  16. package/android/src/main/java/com/evgenii/jsevaluator/interfaces/JsCallback.java +10 -0
  17. package/android/src/main/java/com/evgenii/jsevaluator/interfaces/JsEvaluatorInterface.java +18 -0
  18. package/android/src/main/java/com/evgenii/jsevaluator/interfaces/WebViewWrapperInterface.java +14 -0
  19. package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/BackgroundGeolocationPlugin.java +898 -0
  20. package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/ConfigMapper.java +303 -0
  21. package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/HeadlessTaskRegistry.java +34 -0
  22. package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/JsEvaluatorTaskRunner.java +63 -0
  23. package/android/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java +699 -0
  24. package/android/src/main/java/com/marianhello/bgloc/BootCompletedReceiver.java +103 -0
  25. package/android/src/main/java/com/marianhello/bgloc/Config.java +1155 -0
  26. package/android/src/main/java/com/marianhello/bgloc/ConnectivityListener.java +5 -0
  27. package/android/src/main/java/com/marianhello/bgloc/HttpPostService.java +362 -0
  28. package/android/src/main/java/com/marianhello/bgloc/LocationManager.java +138 -0
  29. package/android/src/main/java/com/marianhello/bgloc/PluginDelegate.java +45 -0
  30. package/android/src/main/java/com/marianhello/bgloc/PluginException.java +38 -0
  31. package/android/src/main/java/com/marianhello/bgloc/PostLocationTask.java +238 -0
  32. package/android/src/main/java/com/marianhello/bgloc/ResourceResolver.java +55 -0
  33. package/android/src/main/java/com/marianhello/bgloc/data/AbstractLocationTemplate.java +69 -0
  34. package/android/src/main/java/com/marianhello/bgloc/data/ArrayListLocationTemplate.java +88 -0
  35. package/android/src/main/java/com/marianhello/bgloc/data/BackgroundActivity.java +108 -0
  36. package/android/src/main/java/com/marianhello/bgloc/data/BackgroundLocation.java +1088 -0
  37. package/android/src/main/java/com/marianhello/bgloc/data/ConfigJsonMapper.java +211 -0
  38. package/android/src/main/java/com/marianhello/bgloc/data/ConfigurationDAO.java +13 -0
  39. package/android/src/main/java/com/marianhello/bgloc/data/DAOFactory.java +17 -0
  40. package/android/src/main/java/com/marianhello/bgloc/data/HashMapLocationTemplate.java +82 -0
  41. package/android/src/main/java/com/marianhello/bgloc/data/LocationDAO.java +27 -0
  42. package/android/src/main/java/com/marianhello/bgloc/data/LocationTemplate.java +12 -0
  43. package/android/src/main/java/com/marianhello/bgloc/data/LocationTemplateFactory.java +71 -0
  44. package/android/src/main/java/com/marianhello/bgloc/data/LocationTransform.java +19 -0
  45. package/android/src/main/java/com/marianhello/bgloc/data/SessionLocationDAO.java +18 -0
  46. package/android/src/main/java/com/marianhello/bgloc/data/provider/ContentProviderLocationDAO.java +406 -0
  47. package/android/src/main/java/com/marianhello/bgloc/data/provider/LocationContentProvider.java +321 -0
  48. package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationContract.java +94 -0
  49. package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationDAO.java +227 -0
  50. package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationContract.java +122 -0
  51. package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationDAO.java +550 -0
  52. package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteOpenHelper.java +189 -0
  53. package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteSessionContract.java +74 -0
  54. package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteSessionLocationDAO.java +169 -0
  55. package/android/src/main/java/com/marianhello/bgloc/driving/DrivingEventsDetector.java +265 -0
  56. package/android/src/main/java/com/marianhello/bgloc/headless/AbstractTaskRunner.java +15 -0
  57. package/android/src/main/java/com/marianhello/bgloc/headless/ActivityTask.java +48 -0
  58. package/android/src/main/java/com/marianhello/bgloc/headless/JsCallback.java +10 -0
  59. package/android/src/main/java/com/marianhello/bgloc/headless/LocationTask.java +60 -0
  60. package/android/src/main/java/com/marianhello/bgloc/headless/StationaryTask.java +25 -0
  61. package/android/src/main/java/com/marianhello/bgloc/headless/Task.java +8 -0
  62. package/android/src/main/java/com/marianhello/bgloc/headless/TaskRunner.java +5 -0
  63. package/android/src/main/java/com/marianhello/bgloc/headless/TaskRunnerFactory.java +8 -0
  64. package/android/src/main/java/com/marianhello/bgloc/http/UrlTemplateResolver.java +115 -0
  65. package/android/src/main/java/com/marianhello/bgloc/oem/BatteryOemHelper.java +214 -0
  66. package/android/src/main/java/com/marianhello/bgloc/provider/AbstractLocationProvider.java +218 -0
  67. package/android/src/main/java/com/marianhello/bgloc/provider/ActivityRecognitionLocationProvider.java +385 -0
  68. package/android/src/main/java/com/marianhello/bgloc/provider/DistanceFilterLocationProvider.java +685 -0
  69. package/android/src/main/java/com/marianhello/bgloc/provider/LocationProvider.java +32 -0
  70. package/android/src/main/java/com/marianhello/bgloc/provider/LocationProviderFactory.java +47 -0
  71. package/android/src/main/java/com/marianhello/bgloc/provider/ProviderDelegate.java +12 -0
  72. package/android/src/main/java/com/marianhello/bgloc/provider/RawLocationProvider.java +175 -0
  73. package/android/src/main/java/com/marianhello/bgloc/sensor/SensorFusionDetector.java +199 -0
  74. package/android/src/main/java/com/marianhello/bgloc/service/LocationService.java +16 -0
  75. package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceImpl.java +1531 -0
  76. package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceInfo.java +6 -0
  77. package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceInfoImpl.java +41 -0
  78. package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceIntentBuilder.java +203 -0
  79. package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceProxy.java +156 -0
  80. package/android/src/main/java/com/marianhello/bgloc/sync/AccountHelper.java +39 -0
  81. package/android/src/main/java/com/marianhello/bgloc/sync/Authenticator.java +68 -0
  82. package/android/src/main/java/com/marianhello/bgloc/sync/AuthenticatorService.java +28 -0
  83. package/android/src/main/java/com/marianhello/bgloc/sync/BatchManager.java +311 -0
  84. package/android/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java +148 -0
  85. package/android/src/main/java/com/marianhello/bgloc/sync/SyncAdapter.java +301 -0
  86. package/android/src/main/java/com/marianhello/bgloc/sync/SyncService.java +68 -0
  87. package/android/src/main/java/com/marianhello/logging/DBLogReader.java +208 -0
  88. package/android/src/main/java/com/marianhello/logging/LogEntry.java +99 -0
  89. package/android/src/main/java/com/marianhello/logging/LoggerManager.java +70 -0
  90. package/android/src/main/java/com/marianhello/logging/UncaughtExceptionLogger.java +36 -0
  91. package/android/src/main/java/com/marianhello/utils/CloneHelper.java +22 -0
  92. package/android/src/main/java/com/marianhello/utils/Convert.java +56 -0
  93. package/android/src/main/java/com/marianhello/utils/TextUtils.java +72 -0
  94. package/android/src/main/java/com/marianhello/utils/ToneGenerator.java +68 -0
  95. package/android/src/main/java/org/apache/commons/io/Charsets.java +153 -0
  96. package/android/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java +344 -0
  97. package/android/src/main/java/org/chromium/content/browser/ThreadUtils.java +134 -0
  98. package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlExpression.java +398 -0
  99. package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlSelectStatement.java +671 -0
  100. package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlStatement.java +29 -0
  101. package/android/src/main/java/ru/andremoniy/utils/TextUtils.java +61 -0
  102. package/android/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
  103. package/android/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
  104. package/android/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
  105. package/android/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
  106. package/android/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
  107. package/android/src/main/res/values/strings.xml +15 -0
  108. package/android/src/main/res/xml/authenticator.xml +7 -0
  109. package/android/src/main/res/xml/syncadapter.xml +9 -0
  110. package/dist/esm/definitions.d.ts +1052 -0
  111. package/dist/esm/definitions.js +142 -0
  112. package/dist/esm/definitions.js.map +1 -0
  113. package/dist/esm/index.d.ts +8 -0
  114. package/dist/esm/index.js +23 -0
  115. package/dist/esm/index.js.map +1 -0
  116. package/dist/esm/web.d.ts +92 -0
  117. package/dist/esm/web.js +242 -0
  118. package/dist/esm/web.js.map +1 -0
  119. package/dist/plugin.cjs.js +415 -0
  120. package/dist/plugin.cjs.js.map +1 -0
  121. package/dist/plugin.js +418 -0
  122. package/dist/plugin.js.map +1 -0
  123. package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin-Bridging-Header.h +18 -0
  124. package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin.m +52 -0
  125. package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin.swift +750 -0
  126. package/ios/Tests/BackgroundGeolocationPluginTests/BackgroundGeolocationPluginTests.swift +12 -0
  127. package/ios/common/BackgroundGeolocation/CocoaLumberjack.h +1945 -0
  128. package/ios/common/BackgroundGeolocation/CocoaLumberjack.m +5255 -0
  129. package/ios/common/BackgroundGeolocation/FMDB.h +2357 -0
  130. package/ios/common/BackgroundGeolocation/FMDB.m +2672 -0
  131. package/ios/common/BackgroundGeolocation/FMDBLogger.h +42 -0
  132. package/ios/common/BackgroundGeolocation/FMDBLogger.m +264 -0
  133. package/ios/common/BackgroundGeolocation/INTULocationManager/INTUHeadingRequest.h +41 -0
  134. package/ios/common/BackgroundGeolocation/INTULocationManager/INTUHeadingRequest.m +68 -0
  135. package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager+Internal.h +33 -0
  136. package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager.h +178 -0
  137. package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager.m +1025 -0
  138. package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequest.h +103 -0
  139. package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequest.m +238 -0
  140. package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequestDefines.h +163 -0
  141. package/ios/common/BackgroundGeolocation/INTULocationManager/INTURequestIDGenerator.h +39 -0
  142. package/ios/common/BackgroundGeolocation/INTULocationManager/INTURequestIDGenerator.m +37 -0
  143. package/ios/common/BackgroundGeolocation/MAURAbstractLocationProvider.h +51 -0
  144. package/ios/common/BackgroundGeolocation/MAURAbstractLocationProvider.m +53 -0
  145. package/ios/common/BackgroundGeolocation/MAURActivity.h +23 -0
  146. package/ios/common/BackgroundGeolocation/MAURActivity.m +52 -0
  147. package/ios/common/BackgroundGeolocation/MAURActivityLocationProvider.h +18 -0
  148. package/ios/common/BackgroundGeolocation/MAURActivityLocationProvider.m +340 -0
  149. package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.h +88 -0
  150. package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.m +1193 -0
  151. package/ios/common/BackgroundGeolocation/MAURBackgroundSync.h +46 -0
  152. package/ios/common/BackgroundGeolocation/MAURBackgroundSync.m +283 -0
  153. package/ios/common/BackgroundGeolocation/MAURBackgroundTaskManager.h +25 -0
  154. package/ios/common/BackgroundGeolocation/MAURBackgroundTaskManager.m +105 -0
  155. package/ios/common/BackgroundGeolocation/MAURConfig.h +99 -0
  156. package/ios/common/BackgroundGeolocation/MAURConfig.m +636 -0
  157. package/ios/common/BackgroundGeolocation/MAURConfigurationContract.h +53 -0
  158. package/ios/common/BackgroundGeolocation/MAURConfigurationContract.m +54 -0
  159. package/ios/common/BackgroundGeolocation/MAURDistanceFilterLocationProvider.h +20 -0
  160. package/ios/common/BackgroundGeolocation/MAURDistanceFilterLocationProvider.m +550 -0
  161. package/ios/common/BackgroundGeolocation/MAURGeolocationOpenHelper.h +17 -0
  162. package/ios/common/BackgroundGeolocation/MAURGeolocationOpenHelper.m +124 -0
  163. package/ios/common/BackgroundGeolocation/MAURLocation.h +73 -0
  164. package/ios/common/BackgroundGeolocation/MAURLocation.m +392 -0
  165. package/ios/common/BackgroundGeolocation/MAURLocationContract.h +38 -0
  166. package/ios/common/BackgroundGeolocation/MAURLocationContract.m +39 -0
  167. package/ios/common/BackgroundGeolocation/MAURLocationManager.h +53 -0
  168. package/ios/common/BackgroundGeolocation/MAURLocationManager.m +305 -0
  169. package/ios/common/BackgroundGeolocation/MAURLogReader.h +26 -0
  170. package/ios/common/BackgroundGeolocation/MAURLogReader.m +122 -0
  171. package/ios/common/BackgroundGeolocation/MAURLogging.h +19 -0
  172. package/ios/common/BackgroundGeolocation/MAURPostLocationTask.h +53 -0
  173. package/ios/common/BackgroundGeolocation/MAURPostLocationTask.m +367 -0
  174. package/ios/common/BackgroundGeolocation/MAURProviderDelegate.h +52 -0
  175. package/ios/common/BackgroundGeolocation/MAURRawLocationProvider.h +18 -0
  176. package/ios/common/BackgroundGeolocation/MAURRawLocationProvider.m +138 -0
  177. package/ios/common/BackgroundGeolocation/MAURSQLiteConfigurationDAO.h +26 -0
  178. package/ios/common/BackgroundGeolocation/MAURSQLiteConfigurationDAO.m +335 -0
  179. package/ios/common/BackgroundGeolocation/MAURSQLiteHelper.h +57 -0
  180. package/ios/common/BackgroundGeolocation/MAURSQLiteHelper.m +93 -0
  181. package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.h +52 -0
  182. package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.m +520 -0
  183. package/ios/common/BackgroundGeolocation/MAURSQLiteOpenHelper.h +32 -0
  184. package/ios/common/BackgroundGeolocation/MAURSQLiteOpenHelper.m +276 -0
  185. package/ios/common/BackgroundGeolocation/MAURSensorFusionDetector.h +41 -0
  186. package/ios/common/BackgroundGeolocation/MAURSensorFusionDetector.m +137 -0
  187. package/ios/common/BackgroundGeolocation/MAURSessionLocationContract.h +29 -0
  188. package/ios/common/BackgroundGeolocation/MAURSessionLocationContract.m +31 -0
  189. package/ios/common/BackgroundGeolocation/MAURSessionLocationDAO.h +25 -0
  190. package/ios/common/BackgroundGeolocation/MAURSessionLocationDAO.m +153 -0
  191. package/ios/common/BackgroundGeolocation/MAURUncaughtExceptionLogger.h +20 -0
  192. package/ios/common/BackgroundGeolocation/MAURUncaughtExceptionLogger.m +62 -0
  193. package/ios/common/BackgroundGeolocation/MAURUrlTemplateResolver.h +31 -0
  194. package/ios/common/BackgroundGeolocation/MAURUrlTemplateResolver.m +107 -0
  195. package/ios/common/BackgroundGeolocation/Reachability.h +102 -0
  196. package/ios/common/BackgroundGeolocation/Reachability.m +475 -0
  197. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/README.md +170 -0
  198. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/ext/NSString+ZIMString.h +55 -0
  199. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/ext/NSString+ZIMString.m +47 -0
  200. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlDataManipulationCommand.h +27 -0
  201. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlExpression.h +250 -0
  202. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlExpression.m +259 -0
  203. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlSelectStatement.h +360 -0
  204. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlSelectStatement.m +427 -0
  205. package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlStatement.h +37 -0
  206. package/ios/common/BackgroundGeolocation/module.modulemap +16 -0
  207. package/package.json +82 -0
@@ -0,0 +1,2357 @@
1
+ /*
2
+ If you are using FMDB in your project, I'd love to hear about it. Let Gus know
3
+ by sending an email to gus@flyingmeat.com.
4
+
5
+ And if you happen to come across either Gus Mueller or Rob Ryan in a bar, you
6
+ might consider purchasing a drink of their choosing if FMDB has been useful to
7
+ you.
8
+
9
+ Finally, and shortly, this is the MIT License.
10
+
11
+ Copyright (c) 2008-2014 Flying Meat Inc.
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in
21
+ all copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
+ THE SOFTWARE.
30
+ */
31
+
32
+ #import <Foundation/Foundation.h>
33
+
34
+ #ifndef __has_feature // Optional.
35
+ #define __has_feature(x) 0 // Compatibility with non-clang compilers.
36
+ #endif
37
+
38
+ #ifndef NS_RETURNS_NOT_RETAINED
39
+ #if __has_feature(attribute_ns_returns_not_retained)
40
+ #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
41
+ #else
42
+ #define NS_RETURNS_NOT_RETAINED
43
+ #endif
44
+ #endif
45
+
46
+ @class FMDatabase;
47
+ @class FMStatement;
48
+
49
+ /** Represents the results of executing a query on an `<FMDatabase>`.
50
+
51
+ ### See also
52
+
53
+ - `<FMDatabase>`
54
+ */
55
+
56
+ @interface FMResultSet : NSObject {
57
+ FMDatabase *_parentDB;
58
+ FMStatement *_statement;
59
+
60
+ NSString *_query;
61
+ NSMutableDictionary *_columnNameToIndexMap;
62
+ }
63
+
64
+ ///-----------------
65
+ /// @name Properties
66
+ ///-----------------
67
+
68
+ /** Executed query */
69
+
70
+ @property (atomic, retain) NSString *query;
71
+
72
+ /** `NSMutableDictionary` mapping column names to numeric index */
73
+
74
+ @property (readonly) NSMutableDictionary *columnNameToIndexMap;
75
+
76
+ /** `FMStatement` used by result set. */
77
+
78
+ @property (atomic, retain) FMStatement *statement;
79
+
80
+ ///------------------------------------
81
+ /// @name Creating and closing database
82
+ ///------------------------------------
83
+
84
+ /** Create result set from `<FMStatement>`
85
+
86
+ @param statement A `<FMStatement>` to be performed
87
+
88
+ @param aDB A `<FMDatabase>` to be used
89
+
90
+ @return A `FMResultSet` on success; `nil` on failure
91
+ */
92
+
93
+ + (instancetype)resultSetWithStatement:(FMStatement *)statement usingParentDatabase:(FMDatabase*)aDB;
94
+
95
+ /** Close result set */
96
+
97
+ - (void)close;
98
+
99
+ - (void)setParentDB:(FMDatabase *)newDb;
100
+
101
+ ///---------------------------------------
102
+ /// @name Iterating through the result set
103
+ ///---------------------------------------
104
+
105
+ /** Retrieve next row for result set.
106
+
107
+ You must always invoke `next` or `nextWithError` before attempting to access the values returned in a query, even if you're only expecting one.
108
+
109
+ @return `YES` if row successfully retrieved; `NO` if end of result set reached
110
+
111
+ @see hasAnotherRow
112
+ */
113
+
114
+ - (BOOL)next;
115
+
116
+ /** Retrieve next row for result set.
117
+
118
+ You must always invoke `next` or `nextWithError` before attempting to access the values returned in a query, even if you're only expecting one.
119
+
120
+ @param outErr A 'NSError' object to receive any error object (if any).
121
+
122
+ @return 'YES' if row successfully retrieved; 'NO' if end of result set reached
123
+
124
+ @see hasAnotherRow
125
+ */
126
+
127
+ - (BOOL)nextWithError:(NSError **)outErr;
128
+
129
+ /** Did the last call to `<next>` succeed in retrieving another row?
130
+
131
+ @return `YES` if the last call to `<next>` succeeded in retrieving another record; `NO` if not.
132
+
133
+ @see next
134
+
135
+ @warning The `hasAnotherRow` method must follow a call to `<next>`. If the previous database interaction was something other than a call to `next`, then this method may return `NO`, whether there is another row of data or not.
136
+ */
137
+
138
+ - (BOOL)hasAnotherRow;
139
+
140
+ ///---------------------------------------------
141
+ /// @name Retrieving information from result set
142
+ ///---------------------------------------------
143
+
144
+ /** How many columns in result set
145
+
146
+ @return Integer value of the number of columns.
147
+ */
148
+
149
+ - (int)columnCount;
150
+
151
+ /** Column index for column name
152
+
153
+ @param columnName `NSString` value of the name of the column.
154
+
155
+ @return Zero-based index for column.
156
+ */
157
+
158
+ - (int)columnIndexForName:(NSString*)columnName;
159
+
160
+ /** Column name for column index
161
+
162
+ @param columnIdx Zero-based index for column.
163
+
164
+ @return columnName `NSString` value of the name of the column.
165
+ */
166
+
167
+ - (NSString*)columnNameForIndex:(int)columnIdx;
168
+
169
+ /** Result set integer value for column.
170
+
171
+ @param columnName `NSString` value of the name of the column.
172
+
173
+ @return `int` value of the result set's column.
174
+ */
175
+
176
+ - (int)intForColumn:(NSString*)columnName;
177
+
178
+ /** Result set integer value for column.
179
+
180
+ @param columnIdx Zero-based index for column.
181
+
182
+ @return `int` value of the result set's column.
183
+ */
184
+
185
+ - (int)intForColumnIndex:(int)columnIdx;
186
+
187
+ /** Result set `long` value for column.
188
+
189
+ @param columnName `NSString` value of the name of the column.
190
+
191
+ @return `long` value of the result set's column.
192
+ */
193
+
194
+ - (long)longForColumn:(NSString*)columnName;
195
+
196
+ /** Result set long value for column.
197
+
198
+ @param columnIdx Zero-based index for column.
199
+
200
+ @return `long` value of the result set's column.
201
+ */
202
+
203
+ - (long)longForColumnIndex:(int)columnIdx;
204
+
205
+ /** Result set `long long int` value for column.
206
+
207
+ @param columnName `NSString` value of the name of the column.
208
+
209
+ @return `long long int` value of the result set's column.
210
+ */
211
+
212
+ - (long long int)longLongIntForColumn:(NSString*)columnName;
213
+
214
+ /** Result set `long long int` value for column.
215
+
216
+ @param columnIdx Zero-based index for column.
217
+
218
+ @return `long long int` value of the result set's column.
219
+ */
220
+
221
+ - (long long int)longLongIntForColumnIndex:(int)columnIdx;
222
+
223
+ /** Result set `unsigned long long int` value for column.
224
+
225
+ @param columnName `NSString` value of the name of the column.
226
+
227
+ @return `unsigned long long int` value of the result set's column.
228
+ */
229
+
230
+ - (unsigned long long int)unsignedLongLongIntForColumn:(NSString*)columnName;
231
+
232
+ /** Result set `unsigned long long int` value for column.
233
+
234
+ @param columnIdx Zero-based index for column.
235
+
236
+ @return `unsigned long long int` value of the result set's column.
237
+ */
238
+
239
+ - (unsigned long long int)unsignedLongLongIntForColumnIndex:(int)columnIdx;
240
+
241
+ /** Result set `BOOL` value for column.
242
+
243
+ @param columnName `NSString` value of the name of the column.
244
+
245
+ @return `BOOL` value of the result set's column.
246
+ */
247
+
248
+ - (BOOL)boolForColumn:(NSString*)columnName;
249
+
250
+ /** Result set `BOOL` value for column.
251
+
252
+ @param columnIdx Zero-based index for column.
253
+
254
+ @return `BOOL` value of the result set's column.
255
+ */
256
+
257
+ - (BOOL)boolForColumnIndex:(int)columnIdx;
258
+
259
+ /** Result set `double` value for column.
260
+
261
+ @param columnName `NSString` value of the name of the column.
262
+
263
+ @return `double` value of the result set's column.
264
+
265
+ */
266
+
267
+ - (double)doubleForColumn:(NSString*)columnName;
268
+
269
+ /** Result set `double` value for column.
270
+
271
+ @param columnIdx Zero-based index for column.
272
+
273
+ @return `double` value of the result set's column.
274
+
275
+ */
276
+
277
+ - (double)doubleForColumnIndex:(int)columnIdx;
278
+
279
+ /** Result set `NSString` value for column.
280
+
281
+ @param columnName `NSString` value of the name of the column.
282
+
283
+ @return `NSString` value of the result set's column.
284
+
285
+ */
286
+
287
+ - (NSString*)stringForColumn:(NSString*)columnName;
288
+
289
+ /** Result set `NSString` value for column.
290
+
291
+ @param columnIdx Zero-based index for column.
292
+
293
+ @return `NSString` value of the result set's column.
294
+ */
295
+
296
+ - (NSString*)stringForColumnIndex:(int)columnIdx;
297
+
298
+ /** Result set `NSDate` value for column.
299
+
300
+ @param columnName `NSString` value of the name of the column.
301
+
302
+ @return `NSDate` value of the result set's column.
303
+ */
304
+
305
+ - (NSDate*)dateForColumn:(NSString*)columnName;
306
+
307
+ /** Result set `NSDate` value for column.
308
+
309
+ @param columnIdx Zero-based index for column.
310
+
311
+ @return `NSDate` value of the result set's column.
312
+
313
+ */
314
+
315
+ - (NSDate*)dateForColumnIndex:(int)columnIdx;
316
+
317
+ /** Result set `NSData` value for column.
318
+
319
+ This is useful when storing binary data in table (such as image or the like).
320
+
321
+ @param columnName `NSString` value of the name of the column.
322
+
323
+ @return `NSData` value of the result set's column.
324
+
325
+ */
326
+
327
+ - (NSData*)dataForColumn:(NSString*)columnName;
328
+
329
+ /** Result set `NSData` value for column.
330
+
331
+ @param columnIdx Zero-based index for column.
332
+
333
+ @return `NSData` value of the result set's column.
334
+ */
335
+
336
+ - (NSData*)dataForColumnIndex:(int)columnIdx;
337
+
338
+ /** Result set `(const unsigned char *)` value for column.
339
+
340
+ @param columnName `NSString` value of the name of the column.
341
+
342
+ @return `(const unsigned char *)` value of the result set's column.
343
+ */
344
+
345
+ - (const unsigned char *)UTF8StringForColumnName:(NSString*)columnName;
346
+
347
+ /** Result set `(const unsigned char *)` value for column.
348
+
349
+ @param columnIdx Zero-based index for column.
350
+
351
+ @return `(const unsigned char *)` value of the result set's column.
352
+ */
353
+
354
+ - (const unsigned char *)UTF8StringForColumnIndex:(int)columnIdx;
355
+
356
+ /** Result set object for column.
357
+
358
+ @param columnName `NSString` value of the name of the column.
359
+
360
+ @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
361
+
362
+ @see objectForKeyedSubscript:
363
+ */
364
+
365
+ - (id)objectForColumnName:(NSString*)columnName;
366
+
367
+ /** Result set object for column.
368
+
369
+ @param columnIdx Zero-based index for column.
370
+
371
+ @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
372
+
373
+ @see objectAtIndexedSubscript:
374
+ */
375
+
376
+ - (id)objectForColumnIndex:(int)columnIdx;
377
+
378
+ /** Result set object for column.
379
+
380
+ This method allows the use of the "boxed" syntax supported in Modern Objective-C. For example, by defining this method, the following syntax is now supported:
381
+
382
+ id result = rs[@"employee_name"];
383
+
384
+ This simplified syntax is equivalent to calling:
385
+
386
+ id result = [rs objectForKeyedSubscript:@"employee_name"];
387
+
388
+ which is, it turns out, equivalent to calling:
389
+
390
+ id result = [rs objectForColumnName:@"employee_name"];
391
+
392
+ @param columnName `NSString` value of the name of the column.
393
+
394
+ @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
395
+ */
396
+
397
+ - (id)objectForKeyedSubscript:(NSString *)columnName;
398
+
399
+ /** Result set object for column.
400
+
401
+ This method allows the use of the "boxed" syntax supported in Modern Objective-C. For example, by defining this method, the following syntax is now supported:
402
+
403
+ id result = rs[0];
404
+
405
+ This simplified syntax is equivalent to calling:
406
+
407
+ id result = [rs objectForKeyedSubscript:0];
408
+
409
+ which is, it turns out, equivalent to calling:
410
+
411
+ id result = [rs objectForColumnName:0];
412
+
413
+ @param columnIdx Zero-based index for column.
414
+
415
+ @return Either `NSNumber`, `NSString`, `NSData`, or `NSNull`. If the column was `NULL`, this returns `[NSNull null]` object.
416
+ */
417
+
418
+ - (id)objectAtIndexedSubscript:(int)columnIdx;
419
+
420
+ /** Result set `NSData` value for column.
421
+
422
+ @param columnName `NSString` value of the name of the column.
423
+
424
+ @return `NSData` value of the result set's column.
425
+
426
+ @warning If you are going to use this data after you iterate over the next row, or after you close the
427
+ result set, make sure to make a copy of the data first (or just use `<dataForColumn:>`/`<dataForColumnIndex:>`)
428
+ If you don't, you're going to be in a world of hurt when you try and use the data.
429
+
430
+ */
431
+
432
+ - (NSData*)dataNoCopyForColumn:(NSString*)columnName NS_RETURNS_NOT_RETAINED;
433
+
434
+ /** Result set `NSData` value for column.
435
+
436
+ @param columnIdx Zero-based index for column.
437
+
438
+ @return `NSData` value of the result set's column.
439
+
440
+ @warning If you are going to use this data after you iterate over the next row, or after you close the
441
+ result set, make sure to make a copy of the data first (or just use `<dataForColumn:>`/`<dataForColumnIndex:>`)
442
+ If you don't, you're going to be in a world of hurt when you try and use the data.
443
+
444
+ */
445
+
446
+ - (NSData*)dataNoCopyForColumnIndex:(int)columnIdx NS_RETURNS_NOT_RETAINED;
447
+
448
+ /** Is the column `NULL`?
449
+
450
+ @param columnIdx Zero-based index for column.
451
+
452
+ @return `YES` if column is `NULL`; `NO` if not `NULL`.
453
+ */
454
+
455
+ - (BOOL)columnIndexIsNull:(int)columnIdx;
456
+
457
+ /** Is the column `NULL`?
458
+
459
+ @param columnName `NSString` value of the name of the column.
460
+
461
+ @return `YES` if column is `NULL`; `NO` if not `NULL`.
462
+ */
463
+
464
+ - (BOOL)columnIsNull:(NSString*)columnName;
465
+
466
+
467
+ /** Returns a dictionary of the row results mapped to case sensitive keys of the column names.
468
+
469
+ @returns `NSDictionary` of the row results.
470
+
471
+ @warning The keys to the dictionary are case sensitive of the column names.
472
+ */
473
+
474
+ - (NSDictionary*)resultDictionary;
475
+
476
+ /** Returns a dictionary of the row results
477
+
478
+ @see resultDictionary
479
+
480
+ @warning **Deprecated**: Please use `<resultDictionary>` instead. Also, beware that `<resultDictionary>` is case sensitive!
481
+ */
482
+
483
+ - (NSDictionary*)resultDict __attribute__ ((deprecated));
484
+
485
+ ///-----------------------------
486
+ /// @name Key value coding magic
487
+ ///-----------------------------
488
+
489
+ /** Performs `setValue` to yield support for key value observing.
490
+
491
+ @param object The object for which the values will be set. This is the key-value-coding compliant object that you might, for example, observe.
492
+
493
+ */
494
+
495
+ - (void)kvcMagic:(id)object;
496
+
497
+
498
+ @end
499
+
500
+
501
+
502
+ #if ! __has_feature(objc_arc)
503
+ #define FMDBAutorelease(__v) ([__v autorelease]);
504
+ #define FMDBReturnAutoreleased FMDBAutorelease
505
+
506
+ #define FMDBRetain(__v) ([__v retain]);
507
+ #define FMDBReturnRetained FMDBRetain
508
+
509
+ #define FMDBRelease(__v) ([__v release]);
510
+
511
+ #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
512
+ #else
513
+ // -fobjc-arc
514
+ #define FMDBAutorelease(__v)
515
+ #define FMDBReturnAutoreleased(__v) (__v)
516
+
517
+ #define FMDBRetain(__v)
518
+ #define FMDBReturnRetained(__v) (__v)
519
+
520
+ #define FMDBRelease(__v)
521
+
522
+ // If OS_OBJECT_USE_OBJC=1, then the dispatch objects will be treated like ObjC objects
523
+ // and will participate in ARC.
524
+ // See the section on "Dispatch Queues and Automatic Reference Counting" in "Grand Central Dispatch (GCD) Reference" for details.
525
+ #if OS_OBJECT_USE_OBJC
526
+ #define FMDBDispatchQueueRelease(__v)
527
+ #else
528
+ #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
529
+ #endif
530
+ #endif
531
+
532
+ #if !__has_feature(objc_instancetype)
533
+ #define instancetype id
534
+ #endif
535
+
536
+
537
+ typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary);
538
+
539
+
540
+ /** A SQLite ([http://sqlite.org/](http://sqlite.org/)) Objective-C wrapper.
541
+
542
+ ### Usage
543
+ The three main classes in FMDB are:
544
+
545
+ - `FMDatabase` - Represents a single SQLite database. Used for executing SQL statements.
546
+ - `<FMResultSet>` - Represents the results of executing a query on an `FMDatabase`.
547
+ - `<FMDatabaseQueue>` - If you want to perform queries and updates on multiple threads, you'll want to use this class.
548
+
549
+ ### See also
550
+
551
+ - `<FMDatabasePool>` - A pool of `FMDatabase` objects.
552
+ - `<FMStatement>` - A wrapper for `sqlite_stmt`.
553
+
554
+ ### External links
555
+
556
+ - [FMDB on GitHub](https://github.com/ccgus/fmdb) including introductory documentation
557
+ - [SQLite web site](http://sqlite.org/)
558
+ - [FMDB mailing list](http://groups.google.com/group/fmdb)
559
+ - [SQLite FAQ](http://www.sqlite.org/faq.html)
560
+
561
+ @warning Do not instantiate a single `FMDatabase` object and use it across multiple threads. Instead, use `<FMDatabaseQueue>`.
562
+
563
+ */
564
+
565
+ #pragma clang diagnostic push
566
+ #pragma clang diagnostic ignored "-Wobjc-interface-ivars"
567
+
568
+
569
+ @interface FMDatabase : NSObject {
570
+
571
+ void* _db;
572
+ NSString* _databasePath;
573
+ BOOL _logsErrors;
574
+ BOOL _crashOnErrors;
575
+ BOOL _traceExecution;
576
+ BOOL _checkedOut;
577
+ BOOL _shouldCacheStatements;
578
+ BOOL _isExecutingStatement;
579
+ BOOL _inTransaction;
580
+ NSTimeInterval _maxBusyRetryTimeInterval;
581
+ NSTimeInterval _startBusyRetryTime;
582
+
583
+ NSMutableDictionary *_cachedStatements;
584
+ NSMutableSet *_openResultSets;
585
+ NSMutableSet *_openFunctions;
586
+
587
+ NSDateFormatter *_dateFormat;
588
+ }
589
+
590
+ ///-----------------
591
+ /// @name Properties
592
+ ///-----------------
593
+
594
+ /** Whether should trace execution */
595
+
596
+ @property (atomic, assign) BOOL traceExecution;
597
+
598
+ /** Whether checked out or not */
599
+
600
+ @property (atomic, assign) BOOL checkedOut;
601
+
602
+ /** Crash on errors */
603
+
604
+ @property (atomic, assign) BOOL crashOnErrors;
605
+
606
+ /** Logs errors */
607
+
608
+ @property (atomic, assign) BOOL logsErrors;
609
+
610
+ /** Dictionary of cached statements */
611
+
612
+ @property (atomic, retain) NSMutableDictionary *cachedStatements;
613
+
614
+ ///---------------------
615
+ /// @name Initialization
616
+ ///---------------------
617
+
618
+ /** Create a `FMDatabase` object.
619
+
620
+ An `FMDatabase` is created with a path to a SQLite database file. This path can be one of these three:
621
+
622
+ 1. A file system path. The file does not have to exist on disk. If it does not exist, it is created for you.
623
+ 2. An empty string (`@""`). An empty database is created at a temporary location. This database is deleted with the `FMDatabase` connection is closed.
624
+ 3. `nil`. An in-memory database is created. This database will be destroyed with the `FMDatabase` connection is closed.
625
+
626
+ For example, to create/open a database in your Mac OS X `tmp` folder:
627
+
628
+ FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
629
+
630
+ Or, in iOS, you might open a database in the app's `Documents` directory:
631
+
632
+ NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
633
+ NSString *dbPath = [docsPath stringByAppendingPathComponent:@"test.db"];
634
+ FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
635
+
636
+ (For more information on temporary and in-memory databases, read the sqlite documentation on the subject: [http://www.sqlite.org/inmemorydb.html](http://www.sqlite.org/inmemorydb.html))
637
+
638
+ @param inPath Path of database file
639
+
640
+ @return `FMDatabase` object if successful; `nil` if failure.
641
+
642
+ */
643
+
644
+ + (instancetype)databaseWithPath:(NSString*)inPath;
645
+
646
+ /** Initialize a `FMDatabase` object.
647
+
648
+ An `FMDatabase` is created with a path to a SQLite database file. This path can be one of these three:
649
+
650
+ 1. A file system path. The file does not have to exist on disk. If it does not exist, it is created for you.
651
+ 2. An empty string (`@""`). An empty database is created at a temporary location. This database is deleted with the `FMDatabase` connection is closed.
652
+ 3. `nil`. An in-memory database is created. This database will be destroyed with the `FMDatabase` connection is closed.
653
+
654
+ For example, to create/open a database in your Mac OS X `tmp` folder:
655
+
656
+ FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
657
+
658
+ Or, in iOS, you might open a database in the app's `Documents` directory:
659
+
660
+ NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
661
+ NSString *dbPath = [docsPath stringByAppendingPathComponent:@"test.db"];
662
+ FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
663
+
664
+ (For more information on temporary and in-memory databases, read the sqlite documentation on the subject: [http://www.sqlite.org/inmemorydb.html](http://www.sqlite.org/inmemorydb.html))
665
+
666
+ @param inPath Path of database file
667
+
668
+ @return `FMDatabase` object if successful; `nil` if failure.
669
+
670
+ */
671
+
672
+ - (instancetype)initWithPath:(NSString*)inPath;
673
+
674
+
675
+ ///-----------------------------------
676
+ /// @name Opening and closing database
677
+ ///-----------------------------------
678
+
679
+ /** Opening a new database connection
680
+
681
+ The database is opened for reading and writing, and is created if it does not already exist.
682
+
683
+ @return `YES` if successful, `NO` on error.
684
+
685
+ @see [sqlite3_open()](http://sqlite.org/c3ref/open.html)
686
+ @see openWithFlags:
687
+ @see close
688
+ */
689
+
690
+ - (BOOL)open;
691
+
692
+ /** Opening a new database connection with flags and an optional virtual file system (VFS)
693
+
694
+ @param flags one of the following three values, optionally combined with the `SQLITE_OPEN_NOMUTEX`, `SQLITE_OPEN_FULLMUTEX`, `SQLITE_OPEN_SHAREDCACHE`, `SQLITE_OPEN_PRIVATECACHE`, and/or `SQLITE_OPEN_URI` flags:
695
+
696
+ `SQLITE_OPEN_READONLY`
697
+
698
+ The database is opened in read-only mode. If the database does not already exist, an error is returned.
699
+
700
+ `SQLITE_OPEN_READWRITE`
701
+
702
+ The database is opened for reading and writing if possible, or reading only if the file is write protected by the operating system. In either case the database must already exist, otherwise an error is returned.
703
+
704
+ `SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE`
705
+
706
+ The database is opened for reading and writing, and is created if it does not already exist. This is the behavior that is always used for `open` method.
707
+
708
+ @return `YES` if successful, `NO` on error.
709
+
710
+ @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
711
+ @see open
712
+ @see close
713
+ */
714
+
715
+ - (BOOL)openWithFlags:(int)flags;
716
+
717
+ /** Opening a new database connection with flags and an optional virtual file system (VFS)
718
+
719
+ @param flags one of the following three values, optionally combined with the `SQLITE_OPEN_NOMUTEX`, `SQLITE_OPEN_FULLMUTEX`, `SQLITE_OPEN_SHAREDCACHE`, `SQLITE_OPEN_PRIVATECACHE`, and/or `SQLITE_OPEN_URI` flags:
720
+
721
+ `SQLITE_OPEN_READONLY`
722
+
723
+ The database is opened in read-only mode. If the database does not already exist, an error is returned.
724
+
725
+ `SQLITE_OPEN_READWRITE`
726
+
727
+ The database is opened for reading and writing if possible, or reading only if the file is write protected by the operating system. In either case the database must already exist, otherwise an error is returned.
728
+
729
+ `SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE`
730
+
731
+ The database is opened for reading and writing, and is created if it does not already exist. This is the behavior that is always used for `open` method.
732
+
733
+ @param vfsName If vfs is given the value is passed to the vfs parameter of sqlite3_open_v2.
734
+
735
+ @return `YES` if successful, `NO` on error.
736
+
737
+ @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
738
+ @see open
739
+ @see close
740
+ */
741
+
742
+ - (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName;
743
+
744
+ /** Closing a database connection
745
+
746
+ @return `YES` if success, `NO` on error.
747
+
748
+ @see [sqlite3_close()](http://sqlite.org/c3ref/close.html)
749
+ @see open
750
+ @see openWithFlags:
751
+ */
752
+
753
+ - (BOOL)close;
754
+
755
+ /** Test to see if we have a good connection to the database.
756
+
757
+ This will confirm whether:
758
+
759
+ - is database open
760
+ - if open, it will try a simple SELECT statement and confirm that it succeeds.
761
+
762
+ @return `YES` if everything succeeds, `NO` on failure.
763
+ */
764
+
765
+ - (BOOL)goodConnection;
766
+
767
+
768
+ ///----------------------
769
+ /// @name Perform updates
770
+ ///----------------------
771
+
772
+ /** Execute single update statement
773
+
774
+ This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html), [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) to bind values to `?` placeholders in the SQL with the optional list of parameters, and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update.
775
+
776
+ The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
777
+
778
+ @param sql The SQL to be performed, with optional `?` placeholders.
779
+
780
+ @param outErr A reference to the `NSError` pointer to be updated with an auto released `NSError` object if an error if an error occurs. If `nil`, no `NSError` object will be returned.
781
+
782
+ @param ... Optional parameters to bind to `?` placeholders in the SQL statement. These should be Objective-C objects (e.g. `NSString`, `NSNumber`, etc.), not fundamental C data types (e.g. `int`, `char *`, etc.).
783
+
784
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
785
+
786
+ @see lastError
787
+ @see lastErrorCode
788
+ @see lastErrorMessage
789
+ @see [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html)
790
+ */
791
+
792
+ - (BOOL)executeUpdate:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ...;
793
+
794
+ /** Execute single update statement
795
+
796
+ @see executeUpdate:withErrorAndBindings:
797
+
798
+ @warning **Deprecated**: Please use `<executeUpdate:withErrorAndBindings>` instead.
799
+ */
800
+
801
+ - (BOOL)update:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ... __attribute__ ((deprecated));
802
+
803
+ /** Execute single update statement
804
+
805
+ This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html), [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) to bind values to `?` placeholders in the SQL with the optional list of parameters, and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update.
806
+
807
+ The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
808
+
809
+ @param sql The SQL to be performed, with optional `?` placeholders.
810
+
811
+ @param ... Optional parameters to bind to `?` placeholders in the SQL statement. These should be Objective-C objects (e.g. `NSString`, `NSNumber`, etc.), not fundamental C data types (e.g. `int`, `char *`, etc.).
812
+
813
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
814
+
815
+ @see lastError
816
+ @see lastErrorCode
817
+ @see lastErrorMessage
818
+ @see [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html)
819
+
820
+ @note This technique supports the use of `?` placeholders in the SQL, automatically binding any supplied value parameters to those placeholders. This approach is more robust than techniques that entail using `stringWithFormat` to manually build SQL statements, which can be problematic if the values happened to include any characters that needed to be quoted.
821
+
822
+ @note If you want to use this from Swift, please note that you must include `FMDatabaseVariadic.swift` in your project. Without that, you cannot use this method directly, and instead have to use methods such as `<executeUpdate:withArgumentsInArray:>`.
823
+ */
824
+
825
+ - (BOOL)executeUpdate:(NSString*)sql, ...;
826
+
827
+ /** Execute single update statement
828
+
829
+ This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update. Unlike the other `executeUpdate` methods, this uses printf-style formatters (e.g. `%s`, `%d`, etc.) to build the SQL. Do not use `?` placeholders in the SQL if you use this method.
830
+
831
+ @param format The SQL to be performed, with `printf`-style escape sequences.
832
+
833
+ @param ... Optional parameters to bind to use in conjunction with the `printf`-style escape sequences in the SQL statement.
834
+
835
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
836
+
837
+ @see executeUpdate:
838
+ @see lastError
839
+ @see lastErrorCode
840
+ @see lastErrorMessage
841
+
842
+ @note This method does not technically perform a traditional printf-style replacement. What this method actually does is replace the printf-style percent sequences with a SQLite `?` placeholder, and then bind values to that placeholder. Thus the following command
843
+
844
+ [db executeUpdateWithFormat:@"INSERT INTO test (name) VALUES (%@)", @"Gus"];
845
+
846
+ is actually replacing the `%@` with `?` placeholder, and then performing something equivalent to `<executeUpdate:>`
847
+
848
+ [db executeUpdate:@"INSERT INTO test (name) VALUES (?)", @"Gus"];
849
+
850
+ There are two reasons why this distinction is important. First, the printf-style escape sequences can only be used where it is permissible to use a SQLite `?` placeholder. You can use it only for values in SQL statements, but not for table names or column names or any other non-value context. This method also cannot be used in conjunction with `pragma` statements and the like. Second, note the lack of quotation marks in the SQL. The `VALUES` clause was _not_ `VALUES ('%@')` (like you might have to do if you built a SQL statement using `NSString` method `stringWithFormat`), but rather simply `VALUES (%@)`.
851
+ */
852
+
853
+ - (BOOL)executeUpdateWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
854
+
855
+ /** Execute single update statement
856
+
857
+ This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) binding any `?` placeholders in the SQL with the optional list of parameters.
858
+
859
+ The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
860
+
861
+ @param sql The SQL to be performed, with optional `?` placeholders.
862
+
863
+ @param arguments A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
864
+
865
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
866
+
867
+ @see executeUpdate:values:error:
868
+ @see lastError
869
+ @see lastErrorCode
870
+ @see lastErrorMessage
871
+ */
872
+
873
+ - (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
874
+
875
+ /** Execute single update statement
876
+
877
+ This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) binding any `?` placeholders in the SQL with the optional list of parameters.
878
+
879
+ The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
880
+
881
+ This is similar to `<executeUpdate:withArgumentsInArray:>`, except that this also accepts a pointer to a `NSError` pointer, so that errors can be returned.
882
+
883
+ In Swift 2, this throws errors, as if it were defined as follows:
884
+
885
+ `func executeUpdate(sql: String!, values: [AnyObject]!) throws -> Bool`
886
+
887
+ @param sql The SQL to be performed, with optional `?` placeholders.
888
+
889
+ @param values A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
890
+
891
+ @param error A `NSError` object to receive any error object (if any).
892
+
893
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
894
+
895
+ @see lastError
896
+ @see lastErrorCode
897
+ @see lastErrorMessage
898
+
899
+ */
900
+
901
+ - (BOOL)executeUpdate:(NSString*)sql values:(NSArray *)values error:(NSError * __autoreleasing *)error;
902
+
903
+ /** Execute single update statement
904
+
905
+ This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update. Unlike the other `executeUpdate` methods, this uses printf-style formatters (e.g. `%s`, `%d`, etc.) to build the SQL.
906
+
907
+ The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
908
+
909
+ @param sql The SQL to be performed, with optional `?` placeholders.
910
+
911
+ @param arguments A `NSDictionary` of objects keyed by column names that will be used when binding values to the `?` placeholders in the SQL statement.
912
+
913
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
914
+
915
+ @see lastError
916
+ @see lastErrorCode
917
+ @see lastErrorMessage
918
+ */
919
+
920
+ - (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;
921
+
922
+
923
+ /** Execute single update statement
924
+
925
+ This method executes a single SQL update statement (i.e. any SQL that does not return results, such as `UPDATE`, `INSERT`, or `DELETE`. This method employs [`sqlite3_prepare_v2`](http://sqlite.org/c3ref/prepare.html) and [`sqlite_step`](http://sqlite.org/c3ref/step.html) to perform the update. Unlike the other `executeUpdate` methods, this uses printf-style formatters (e.g. `%s`, `%d`, etc.) to build the SQL.
926
+
927
+ The optional values provided to this method should be objects (e.g. `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects), not fundamental data types (e.g. `int`, `long`, `NSInteger`, etc.). This method automatically handles the aforementioned object types, and all other object types will be interpreted as text values using the object's `description` method.
928
+
929
+ @param sql The SQL to be performed, with optional `?` placeholders.
930
+
931
+ @param args A `va_list` of arguments.
932
+
933
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
934
+
935
+ @see lastError
936
+ @see lastErrorCode
937
+ @see lastErrorMessage
938
+ */
939
+
940
+ - (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;
941
+
942
+ /** Execute multiple SQL statements
943
+
944
+ This executes a series of SQL statements that are combined in a single string (e.g. the SQL generated by the `sqlite3` command line `.dump` command). This accepts no value parameters, but rather simply expects a single string with multiple SQL statements, each terminated with a semicolon. This uses `sqlite3_exec`.
945
+
946
+ @param sql The SQL to be performed
947
+
948
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
949
+
950
+ @see executeStatements:withResultBlock:
951
+ @see [sqlite3_exec()](http://sqlite.org/c3ref/exec.html)
952
+
953
+ */
954
+
955
+ - (BOOL)executeStatements:(NSString *)sql;
956
+
957
+ /** Execute multiple SQL statements with callback handler
958
+
959
+ This executes a series of SQL statements that are combined in a single string (e.g. the SQL generated by the `sqlite3` command line `.dump` command). This accepts no value parameters, but rather simply expects a single string with multiple SQL statements, each terminated with a semicolon. This uses `sqlite3_exec`.
960
+
961
+ @param sql The SQL to be performed.
962
+ @param block A block that will be called for any result sets returned by any SQL statements.
963
+ Note, if you supply this block, it must return integer value, zero upon success (this would be a good opportunity to use SQLITE_OK),
964
+ non-zero value upon failure (which will stop the bulk execution of the SQL). If a statement returns values, the block will be called with the results from the query in NSDictionary *resultsDictionary.
965
+ This may be `nil` if you don't care to receive any results.
966
+
967
+ @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`,
968
+ `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
969
+
970
+ @see executeStatements:
971
+ @see [sqlite3_exec()](http://sqlite.org/c3ref/exec.html)
972
+
973
+ */
974
+
975
+ - (BOOL)executeStatements:(NSString *)sql withResultBlock:(FMDBExecuteStatementsCallbackBlock)block;
976
+
977
+ /** Last insert rowid
978
+
979
+ Each entry in an SQLite table has a unique 64-bit signed integer key called the "rowid". The rowid is always available as an undeclared column named `ROWID`, `OID`, or `_ROWID_` as long as those names are not also used by explicitly declared columns. If the table has a column of type `INTEGER PRIMARY KEY` then that column is another alias for the rowid.
980
+
981
+ This routine returns the rowid of the most recent successful `INSERT` into the database from the database connection in the first argument. As of SQLite version 3.7.7, this routines records the last insert rowid of both ordinary tables and virtual tables. If no successful `INSERT`s have ever occurred on that database connection, zero is returned.
982
+
983
+ @return The rowid of the last inserted row.
984
+
985
+ @see [sqlite3_last_insert_rowid()](http://sqlite.org/c3ref/last_insert_rowid.html)
986
+
987
+ */
988
+
989
+ - (int64_t)lastInsertRowId;
990
+
991
+ /** The number of rows changed by prior SQL statement.
992
+
993
+ This function returns the number of database rows that were changed or inserted or deleted by the most recently completed SQL statement on the database connection specified by the first parameter. Only changes that are directly specified by the INSERT, UPDATE, or DELETE statement are counted.
994
+
995
+ @return The number of rows changed by prior SQL statement.
996
+
997
+ @see [sqlite3_changes()](http://sqlite.org/c3ref/changes.html)
998
+
999
+ */
1000
+
1001
+ - (int)changes;
1002
+
1003
+
1004
+ ///-------------------------
1005
+ /// @name Retrieving results
1006
+ ///-------------------------
1007
+
1008
+ /** Execute select statement
1009
+
1010
+ Executing queries returns an `<FMResultSet>` object if successful, and `nil` upon failure. Like executing updates, there is a variant that accepts an `NSError **` parameter. Otherwise you should use the `<lastErrorMessage>` and `<lastErrorMessage>` methods to determine why a query failed.
1011
+
1012
+ In order to iterate through the results of your query, you use a `while()` loop. You also need to "step" (via `<[FMResultSet next]>`) from one record to the other.
1013
+
1014
+ This method employs [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html) for any optional value parameters. This properly escapes any characters that need escape sequences (e.g. quotation marks), which eliminates simple SQL errors as well as protects against SQL injection attacks. This method natively handles `NSString`, `NSNumber`, `NSNull`, `NSDate`, and `NSData` objects. All other object types will be interpreted as text values using the object's `description` method.
1015
+
1016
+ @param sql The SELECT statement to be performed, with optional `?` placeholders.
1017
+
1018
+ @param ... Optional parameters to bind to `?` placeholders in the SQL statement. These should be Objective-C objects (e.g. `NSString`, `NSNumber`, etc.), not fundamental C data types (e.g. `int`, `char *`, etc.).
1019
+
1020
+ @return A `<FMResultSet>` for the result set upon success; `nil` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1021
+
1022
+ @see FMResultSet
1023
+ @see [`FMResultSet next`](<[FMResultSet next]>)
1024
+ @see [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html)
1025
+
1026
+ @note If you want to use this from Swift, please note that you must include `FMDatabaseVariadic.swift` in your project. Without that, you cannot use this method directly, and instead have to use methods such as `<executeQuery:withArgumentsInArray:>`.
1027
+ */
1028
+
1029
+ - (FMResultSet *)executeQuery:(NSString*)sql, ...;
1030
+
1031
+ /** Execute select statement
1032
+
1033
+ Executing queries returns an `<FMResultSet>` object if successful, and `nil` upon failure. Like executing updates, there is a variant that accepts an `NSError **` parameter. Otherwise you should use the `<lastErrorMessage>` and `<lastErrorMessage>` methods to determine why a query failed.
1034
+
1035
+ In order to iterate through the results of your query, you use a `while()` loop. You also need to "step" (via `<[FMResultSet next]>`) from one record to the other.
1036
+
1037
+ @param format The SQL to be performed, with `printf`-style escape sequences.
1038
+
1039
+ @param ... Optional parameters to bind to use in conjunction with the `printf`-style escape sequences in the SQL statement.
1040
+
1041
+ @return A `<FMResultSet>` for the result set upon success; `nil` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1042
+
1043
+ @see executeQuery:
1044
+ @see FMResultSet
1045
+ @see [`FMResultSet next`](<[FMResultSet next]>)
1046
+
1047
+ @note This method does not technically perform a traditional printf-style replacement. What this method actually does is replace the printf-style percent sequences with a SQLite `?` placeholder, and then bind values to that placeholder. Thus the following command
1048
+
1049
+ [db executeQueryWithFormat:@"SELECT * FROM test WHERE name=%@", @"Gus"];
1050
+
1051
+ is actually replacing the `%@` with `?` placeholder, and then performing something equivalent to `<executeQuery:>`
1052
+
1053
+ [db executeQuery:@"SELECT * FROM test WHERE name=?", @"Gus"];
1054
+
1055
+ There are two reasons why this distinction is important. First, the printf-style escape sequences can only be used where it is permissible to use a SQLite `?` placeholder. You can use it only for values in SQL statements, but not for table names or column names or any other non-value context. This method also cannot be used in conjunction with `pragma` statements and the like. Second, note the lack of quotation marks in the SQL. The `WHERE` clause was _not_ `WHERE name='%@'` (like you might have to do if you built a SQL statement using `NSString` method `stringWithFormat`), but rather simply `WHERE name=%@`.
1056
+
1057
+ */
1058
+
1059
+ - (FMResultSet *)executeQueryWithFormat:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
1060
+
1061
+ /** Execute select statement
1062
+
1063
+ Executing queries returns an `<FMResultSet>` object if successful, and `nil` upon failure. Like executing updates, there is a variant that accepts an `NSError **` parameter. Otherwise you should use the `<lastErrorMessage>` and `<lastErrorMessage>` methods to determine why a query failed.
1064
+
1065
+ In order to iterate through the results of your query, you use a `while()` loop. You also need to "step" (via `<[FMResultSet next]>`) from one record to the other.
1066
+
1067
+ @param sql The SELECT statement to be performed, with optional `?` placeholders.
1068
+
1069
+ @param arguments A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
1070
+
1071
+ @return A `<FMResultSet>` for the result set upon success; `nil` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1072
+
1073
+ @see -executeQuery:values:error:
1074
+ @see FMResultSet
1075
+ @see [`FMResultSet next`](<[FMResultSet next]>)
1076
+ */
1077
+
1078
+ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments;
1079
+
1080
+ /** Execute select statement
1081
+
1082
+ Executing queries returns an `<FMResultSet>` object if successful, and `nil` upon failure. Like executing updates, there is a variant that accepts an `NSError **` parameter. Otherwise you should use the `<lastErrorMessage>` and `<lastErrorMessage>` methods to determine why a query failed.
1083
+
1084
+ In order to iterate through the results of your query, you use a `while()` loop. You also need to "step" (via `<[FMResultSet next]>`) from one record to the other.
1085
+
1086
+ This is similar to `<executeQuery:withArgumentsInArray:>`, except that this also accepts a pointer to a `NSError` pointer, so that errors can be returned.
1087
+
1088
+ In Swift 2, this throws errors, as if it were defined as follows:
1089
+
1090
+ `func executeQuery(sql: String!, values: [AnyObject]!) throws -> FMResultSet!`
1091
+
1092
+ @param sql The SELECT statement to be performed, with optional `?` placeholders.
1093
+
1094
+ @param values A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
1095
+
1096
+ @param error A `NSError` object to receive any error object (if any).
1097
+
1098
+ @return A `<FMResultSet>` for the result set upon success; `nil` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1099
+
1100
+ @see FMResultSet
1101
+ @see [`FMResultSet next`](<[FMResultSet next]>)
1102
+
1103
+ @note When called from Swift, only use the first two parameters, `sql` and `values`. This but throws the error.
1104
+
1105
+ */
1106
+
1107
+ - (FMResultSet *)executeQuery:(NSString *)sql values:(NSArray *)values error:(NSError * __autoreleasing *)error;
1108
+
1109
+ /** Execute select statement
1110
+
1111
+ Executing queries returns an `<FMResultSet>` object if successful, and `nil` upon failure. Like executing updates, there is a variant that accepts an `NSError **` parameter. Otherwise you should use the `<lastErrorMessage>` and `<lastErrorMessage>` methods to determine why a query failed.
1112
+
1113
+ In order to iterate through the results of your query, you use a `while()` loop. You also need to "step" (via `<[FMResultSet next]>`) from one record to the other.
1114
+
1115
+ @param sql The SELECT statement to be performed, with optional `?` placeholders.
1116
+
1117
+ @param arguments A `NSDictionary` of objects keyed by column names that will be used when binding values to the `?` placeholders in the SQL statement.
1118
+
1119
+ @return A `<FMResultSet>` for the result set upon success; `nil` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1120
+
1121
+ @see FMResultSet
1122
+ @see [`FMResultSet next`](<[FMResultSet next]>)
1123
+ */
1124
+
1125
+ - (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary *)arguments;
1126
+
1127
+
1128
+ // Documentation forthcoming.
1129
+ - (FMResultSet *)executeQuery:(NSString*)sql withVAList: (va_list)args;
1130
+
1131
+ ///-------------------
1132
+ /// @name Transactions
1133
+ ///-------------------
1134
+
1135
+ /** Begin a transaction
1136
+
1137
+ @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1138
+
1139
+ @see commit
1140
+ @see rollback
1141
+ @see beginDeferredTransaction
1142
+ @see inTransaction
1143
+ */
1144
+
1145
+ - (BOOL)beginTransaction;
1146
+
1147
+ /** Begin a deferred transaction
1148
+
1149
+ @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1150
+
1151
+ @see commit
1152
+ @see rollback
1153
+ @see beginTransaction
1154
+ @see inTransaction
1155
+ */
1156
+
1157
+ - (BOOL)beginDeferredTransaction;
1158
+
1159
+ /** Commit a transaction
1160
+
1161
+ Commit a transaction that was initiated with either `<beginTransaction>` or with `<beginDeferredTransaction>`.
1162
+
1163
+ @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1164
+
1165
+ @see beginTransaction
1166
+ @see beginDeferredTransaction
1167
+ @see rollback
1168
+ @see inTransaction
1169
+ */
1170
+
1171
+ - (BOOL)commit;
1172
+
1173
+ /** Rollback a transaction
1174
+
1175
+ Rollback a transaction that was initiated with either `<beginTransaction>` or with `<beginDeferredTransaction>`.
1176
+
1177
+ @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1178
+
1179
+ @see beginTransaction
1180
+ @see beginDeferredTransaction
1181
+ @see commit
1182
+ @see inTransaction
1183
+ */
1184
+
1185
+ - (BOOL)rollback;
1186
+
1187
+ /** Identify whether currently in a transaction or not
1188
+
1189
+ @return `YES` if currently within transaction; `NO` if not.
1190
+
1191
+ @see beginTransaction
1192
+ @see beginDeferredTransaction
1193
+ @see commit
1194
+ @see rollback
1195
+ */
1196
+
1197
+ - (BOOL)inTransaction;
1198
+
1199
+
1200
+ ///----------------------------------------
1201
+ /// @name Cached statements and result sets
1202
+ ///----------------------------------------
1203
+
1204
+ /** Clear cached statements */
1205
+
1206
+ - (void)clearCachedStatements;
1207
+
1208
+ /** Close all open result sets */
1209
+
1210
+ - (void)closeOpenResultSets;
1211
+
1212
+ /** Whether database has any open result sets
1213
+
1214
+ @return `YES` if there are open result sets; `NO` if not.
1215
+ */
1216
+
1217
+ - (BOOL)hasOpenResultSets;
1218
+
1219
+ /** Return whether should cache statements or not
1220
+
1221
+ @return `YES` if should cache statements; `NO` if not.
1222
+ */
1223
+
1224
+ - (BOOL)shouldCacheStatements;
1225
+
1226
+ /** Set whether should cache statements or not
1227
+
1228
+ @param value `YES` if should cache statements; `NO` if not.
1229
+ */
1230
+
1231
+ - (void)setShouldCacheStatements:(BOOL)value;
1232
+
1233
+ /** Interupt pending database operation
1234
+
1235
+ This method causes any pending database operation to abort and return at its earliest opportunity
1236
+
1237
+ @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1238
+
1239
+ */
1240
+
1241
+ - (BOOL)interrupt;
1242
+
1243
+ ///-------------------------
1244
+ /// @name Encryption methods
1245
+ ///-------------------------
1246
+
1247
+ /** Set encryption key.
1248
+
1249
+ @param key The key to be used.
1250
+
1251
+ @return `YES` if success, `NO` on error.
1252
+
1253
+ @see https://www.zetetic.net/sqlcipher/
1254
+
1255
+ @warning You need to have purchased the sqlite encryption extensions for this method to work.
1256
+ */
1257
+
1258
+ - (BOOL)setKey:(NSString*)key;
1259
+
1260
+ /** Reset encryption key
1261
+
1262
+ @param key The key to be used.
1263
+
1264
+ @return `YES` if success, `NO` on error.
1265
+
1266
+ @see https://www.zetetic.net/sqlcipher/
1267
+
1268
+ @warning You need to have purchased the sqlite encryption extensions for this method to work.
1269
+ */
1270
+
1271
+ - (BOOL)rekey:(NSString*)key;
1272
+
1273
+ /** Set encryption key using `keyData`.
1274
+
1275
+ @param keyData The `NSData` to be used.
1276
+
1277
+ @return `YES` if success, `NO` on error.
1278
+
1279
+ @see https://www.zetetic.net/sqlcipher/
1280
+
1281
+ @warning You need to have purchased the sqlite encryption extensions for this method to work.
1282
+ */
1283
+
1284
+ - (BOOL)setKeyWithData:(NSData *)keyData;
1285
+
1286
+ /** Reset encryption key using `keyData`.
1287
+
1288
+ @param keyData The `NSData` to be used.
1289
+
1290
+ @return `YES` if success, `NO` on error.
1291
+
1292
+ @see https://www.zetetic.net/sqlcipher/
1293
+
1294
+ @warning You need to have purchased the sqlite encryption extensions for this method to work.
1295
+ */
1296
+
1297
+ - (BOOL)rekeyWithData:(NSData *)keyData;
1298
+
1299
+
1300
+ ///------------------------------
1301
+ /// @name General inquiry methods
1302
+ ///------------------------------
1303
+
1304
+ /** The path of the database file
1305
+
1306
+ @return path of database.
1307
+
1308
+ */
1309
+
1310
+ - (NSString *)databasePath;
1311
+
1312
+ /** The underlying SQLite handle
1313
+
1314
+ @return The `sqlite3` pointer.
1315
+
1316
+ */
1317
+
1318
+ - (void*)sqliteHandle;
1319
+
1320
+
1321
+ ///-----------------------------
1322
+ /// @name Retrieving error codes
1323
+ ///-----------------------------
1324
+
1325
+ /** Last error message
1326
+
1327
+ Returns the English-language text that describes the most recent failed SQLite API call associated with a database connection. If a prior API call failed but the most recent API call succeeded, this return value is undefined.
1328
+
1329
+ @return `NSString` of the last error message.
1330
+
1331
+ @see [sqlite3_errmsg()](http://sqlite.org/c3ref/errcode.html)
1332
+ @see lastErrorCode
1333
+ @see lastError
1334
+
1335
+ */
1336
+
1337
+ - (NSString*)lastErrorMessage;
1338
+
1339
+ /** Last error code
1340
+
1341
+ Returns the numeric result code or extended result code for the most recent failed SQLite API call associated with a database connection. If a prior API call failed but the most recent API call succeeded, this return value is undefined.
1342
+
1343
+ @return Integer value of the last error code.
1344
+
1345
+ @see [sqlite3_errcode()](http://sqlite.org/c3ref/errcode.html)
1346
+ @see lastErrorMessage
1347
+ @see lastError
1348
+
1349
+ */
1350
+
1351
+ - (int)lastErrorCode;
1352
+
1353
+ /** Had error
1354
+
1355
+ @return `YES` if there was an error, `NO` if no error.
1356
+
1357
+ @see lastError
1358
+ @see lastErrorCode
1359
+ @see lastErrorMessage
1360
+
1361
+ */
1362
+
1363
+ - (BOOL)hadError;
1364
+
1365
+ /** Last error
1366
+
1367
+ @return `NSError` representing the last error.
1368
+
1369
+ @see lastErrorCode
1370
+ @see lastErrorMessage
1371
+
1372
+ */
1373
+
1374
+ - (NSError*)lastError;
1375
+
1376
+
1377
+ // description forthcoming
1378
+ - (void)setMaxBusyRetryTimeInterval:(NSTimeInterval)timeoutInSeconds;
1379
+ - (NSTimeInterval)maxBusyRetryTimeInterval;
1380
+
1381
+
1382
+ ///------------------
1383
+ /// @name Save points
1384
+ ///------------------
1385
+
1386
+ /** Start save point
1387
+
1388
+ @param name Name of save point.
1389
+
1390
+ @param outErr A `NSError` object to receive any error object (if any).
1391
+
1392
+ @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1393
+
1394
+ @see releaseSavePointWithName:error:
1395
+ @see rollbackToSavePointWithName:error:
1396
+ */
1397
+
1398
+ - (BOOL)startSavePointWithName:(NSString*)name error:(NSError**)outErr;
1399
+
1400
+ /** Release save point
1401
+
1402
+ @param name Name of save point.
1403
+
1404
+ @param outErr A `NSError` object to receive any error object (if any).
1405
+
1406
+ @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1407
+
1408
+ @see startSavePointWithName:error:
1409
+ @see rollbackToSavePointWithName:error:
1410
+
1411
+ */
1412
+
1413
+ - (BOOL)releaseSavePointWithName:(NSString*)name error:(NSError**)outErr;
1414
+
1415
+ /** Roll back to save point
1416
+
1417
+ @param name Name of save point.
1418
+ @param outErr A `NSError` object to receive any error object (if any).
1419
+
1420
+ @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
1421
+
1422
+ @see startSavePointWithName:error:
1423
+ @see releaseSavePointWithName:error:
1424
+
1425
+ */
1426
+
1427
+ - (BOOL)rollbackToSavePointWithName:(NSString*)name error:(NSError**)outErr;
1428
+
1429
+ /** Start save point
1430
+
1431
+ @param block Block of code to perform from within save point.
1432
+
1433
+ @return The NSError corresponding to the error, if any. If no error, returns `nil`.
1434
+
1435
+ @see startSavePointWithName:error:
1436
+ @see releaseSavePointWithName:error:
1437
+ @see rollbackToSavePointWithName:error:
1438
+
1439
+ */
1440
+
1441
+ - (NSError*)inSavePoint:(void (^)(BOOL *rollback))block;
1442
+
1443
+ ///----------------------------
1444
+ /// @name SQLite library status
1445
+ ///----------------------------
1446
+
1447
+ /** Test to see if the library is threadsafe
1448
+
1449
+ @return `NO` if and only if SQLite was compiled with mutexing code omitted due to the SQLITE_THREADSAFE compile-time option being set to 0.
1450
+
1451
+ @see [sqlite3_threadsafe()](http://sqlite.org/c3ref/threadsafe.html)
1452
+ */
1453
+
1454
+ + (BOOL)isSQLiteThreadSafe;
1455
+
1456
+ /** Run-time library version numbers
1457
+
1458
+ @return The sqlite library version string.
1459
+
1460
+ @see [sqlite3_libversion()](http://sqlite.org/c3ref/libversion.html)
1461
+ */
1462
+
1463
+ + (NSString*)sqliteLibVersion;
1464
+
1465
+
1466
+ + (NSString*)FMDBUserVersion;
1467
+
1468
+ + (SInt32)FMDBVersion;
1469
+
1470
+
1471
+ ///------------------------
1472
+ /// @name Make SQL function
1473
+ ///------------------------
1474
+
1475
+ /** Adds SQL functions or aggregates or to redefine the behavior of existing SQL functions or aggregates.
1476
+
1477
+ For example:
1478
+
1479
+ [queue inDatabase:^(FMDatabase *adb) {
1480
+
1481
+ [adb executeUpdate:@"create table ftest (foo text)"];
1482
+ [adb executeUpdate:@"insert into ftest values ('hello')"];
1483
+ [adb executeUpdate:@"insert into ftest values ('hi')"];
1484
+ [adb executeUpdate:@"insert into ftest values ('not h!')"];
1485
+ [adb executeUpdate:@"insert into ftest values ('definitely not h!')"];
1486
+
1487
+ [adb makeFunctionNamed:@"StringStartsWithH" maximumArguments:1 withBlock:^(sqlite3_context *context, int aargc, sqlite3_value **aargv) {
1488
+ if (sqlite3_value_type(aargv[0]) == SQLITE_TEXT) {
1489
+ @autoreleasepool {
1490
+ const char *c = (const char *)sqlite3_value_text(aargv[0]);
1491
+ NSString *s = [NSString stringWithUTF8String:c];
1492
+ sqlite3_result_int(context, [s hasPrefix:@"h"]);
1493
+ }
1494
+ }
1495
+ else {
1496
+ NSLog(@"Unknown formart for StringStartsWithH (%d) %s:%d", sqlite3_value_type(aargv[0]), __FUNCTION__, __LINE__);
1497
+ sqlite3_result_null(context);
1498
+ }
1499
+ }];
1500
+
1501
+ int rowCount = 0;
1502
+ FMResultSet *ars = [adb executeQuery:@"select * from ftest where StringStartsWithH(foo)"];
1503
+ while ([ars next]) {
1504
+ rowCount++;
1505
+ NSLog(@"Does %@ start with 'h'?", [rs stringForColumnIndex:0]);
1506
+ }
1507
+ FMDBQuickCheck(rowCount == 2);
1508
+ }];
1509
+
1510
+ @param name Name of function
1511
+
1512
+ @param count Maximum number of parameters
1513
+
1514
+ @param block The block of code for the function
1515
+
1516
+ @see [sqlite3_create_function()](http://sqlite.org/c3ref/create_function.html)
1517
+ */
1518
+
1519
+ - (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(void *context, int argc, void **argv))block;
1520
+
1521
+
1522
+ ///---------------------
1523
+ /// @name Date formatter
1524
+ ///---------------------
1525
+
1526
+ /** Generate an `NSDateFormatter` that won't be broken by permutations of timezones or locales.
1527
+
1528
+ Use this method to generate values to set the dateFormat property.
1529
+
1530
+ Example:
1531
+
1532
+ myDB.dateFormat = [FMDatabase storeableDateFormat:@"yyyy-MM-dd HH:mm:ss"];
1533
+
1534
+ @param format A valid NSDateFormatter format string.
1535
+
1536
+ @return A `NSDateFormatter` that can be used for converting dates to strings and vice versa.
1537
+
1538
+ @see hasDateFormatter
1539
+ @see setDateFormat:
1540
+ @see dateFromString:
1541
+ @see stringFromDate:
1542
+ @see storeableDateFormat:
1543
+
1544
+ @warning Note that `NSDateFormatter` is not thread-safe, so the formatter generated by this method should be assigned to only one FMDB instance and should not be used for other purposes.
1545
+
1546
+ */
1547
+
1548
+ + (NSDateFormatter *)storeableDateFormat:(NSString *)format;
1549
+
1550
+ /** Test whether the database has a date formatter assigned.
1551
+
1552
+ @return `YES` if there is a date formatter; `NO` if not.
1553
+
1554
+ @see hasDateFormatter
1555
+ @see setDateFormat:
1556
+ @see dateFromString:
1557
+ @see stringFromDate:
1558
+ @see storeableDateFormat:
1559
+ */
1560
+
1561
+ - (BOOL)hasDateFormatter;
1562
+
1563
+ /** Set to a date formatter to use string dates with sqlite instead of the default UNIX timestamps.
1564
+
1565
+ @param format Set to nil to use UNIX timestamps. Defaults to nil. Should be set using a formatter generated using FMDatabase::storeableDateFormat.
1566
+
1567
+ @see hasDateFormatter
1568
+ @see setDateFormat:
1569
+ @see dateFromString:
1570
+ @see stringFromDate:
1571
+ @see storeableDateFormat:
1572
+
1573
+ @warning Note there is no direct getter for the `NSDateFormatter`, and you should not use the formatter you pass to FMDB for other purposes, as `NSDateFormatter` is not thread-safe.
1574
+ */
1575
+
1576
+ - (void)setDateFormat:(NSDateFormatter *)format;
1577
+
1578
+ /** Convert the supplied NSString to NSDate, using the current database formatter.
1579
+
1580
+ @param s `NSString` to convert to `NSDate`.
1581
+
1582
+ @return The `NSDate` object; or `nil` if no formatter is set.
1583
+
1584
+ @see hasDateFormatter
1585
+ @see setDateFormat:
1586
+ @see dateFromString:
1587
+ @see stringFromDate:
1588
+ @see storeableDateFormat:
1589
+ */
1590
+
1591
+ - (NSDate *)dateFromString:(NSString *)s;
1592
+
1593
+ /** Convert the supplied NSDate to NSString, using the current database formatter.
1594
+
1595
+ @param date `NSDate` of date to convert to `NSString`.
1596
+
1597
+ @return The `NSString` representation of the date; `nil` if no formatter is set.
1598
+
1599
+ @see hasDateFormatter
1600
+ @see setDateFormat:
1601
+ @see dateFromString:
1602
+ @see stringFromDate:
1603
+ @see storeableDateFormat:
1604
+ */
1605
+
1606
+ - (NSString *)stringFromDate:(NSDate *)date;
1607
+
1608
+ @end
1609
+
1610
+
1611
+ /** Objective-C wrapper for `sqlite3_stmt`
1612
+
1613
+ This is a wrapper for a SQLite `sqlite3_stmt`. Generally when using FMDB you will not need to interact directly with `FMStatement`, but rather with `<FMDatabase>` and `<FMResultSet>` only.
1614
+
1615
+ ### See also
1616
+
1617
+ - `<FMDatabase>`
1618
+ - `<FMResultSet>`
1619
+ - [`sqlite3_stmt`](http://www.sqlite.org/c3ref/stmt.html)
1620
+ */
1621
+
1622
+ @interface FMStatement : NSObject {
1623
+ void *_statement;
1624
+ NSString *_query;
1625
+ long _useCount;
1626
+ BOOL _inUse;
1627
+ }
1628
+
1629
+ ///-----------------
1630
+ /// @name Properties
1631
+ ///-----------------
1632
+
1633
+ /** Usage count */
1634
+
1635
+ @property (atomic, assign) long useCount;
1636
+
1637
+ /** SQL statement */
1638
+
1639
+ @property (atomic, retain) NSString *query;
1640
+
1641
+ /** SQLite sqlite3_stmt
1642
+
1643
+ @see [`sqlite3_stmt`](http://www.sqlite.org/c3ref/stmt.html)
1644
+ */
1645
+
1646
+ @property (atomic, assign) void *statement;
1647
+
1648
+ /** Indication of whether the statement is in use */
1649
+
1650
+ @property (atomic, assign) BOOL inUse;
1651
+
1652
+ ///----------------------------
1653
+ /// @name Closing and Resetting
1654
+ ///----------------------------
1655
+
1656
+ /** Close statement */
1657
+
1658
+ - (void)close;
1659
+
1660
+ /** Reset statement */
1661
+
1662
+ - (void)reset;
1663
+
1664
+ @end
1665
+
1666
+ #pragma clang diagnostic pop
1667
+
1668
+ //
1669
+ // FMDatabaseAdditions.h
1670
+ // fmdb
1671
+ //
1672
+ // Created by August Mueller on 10/30/05.
1673
+ // Copyright 2005 Flying Meat Inc.. All rights reserved.
1674
+ //
1675
+
1676
+
1677
+
1678
+ /** Category of additions for `<FMDatabase>` class.
1679
+
1680
+ ### See also
1681
+
1682
+ - `<FMDatabase>`
1683
+ */
1684
+
1685
+ @interface FMDatabase (FMDatabaseAdditions)
1686
+
1687
+ ///----------------------------------------
1688
+ /// @name Return results of SQL to variable
1689
+ ///----------------------------------------
1690
+
1691
+ /** Return `int` value for query
1692
+
1693
+ @param query The SQL query to be performed.
1694
+ @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query.
1695
+
1696
+ @return `int` value.
1697
+
1698
+ @note To use this method from Swift, you must include `FMDatabaseAdditionsVariadic.swift` in your project.
1699
+ */
1700
+
1701
+ - (int)intForQuery:(NSString*)query, ...;
1702
+
1703
+ /** Return `long` value for query
1704
+
1705
+ @param query The SQL query to be performed.
1706
+ @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query.
1707
+
1708
+ @return `long` value.
1709
+
1710
+ @note To use this method from Swift, you must include `FMDatabaseAdditionsVariadic.swift` in your project.
1711
+ */
1712
+
1713
+ - (long)longForQuery:(NSString*)query, ...;
1714
+
1715
+ /** Return `BOOL` value for query
1716
+
1717
+ @param query The SQL query to be performed.
1718
+ @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query.
1719
+
1720
+ @return `BOOL` value.
1721
+
1722
+ @note To use this method from Swift, you must include `FMDatabaseAdditionsVariadic.swift` in your project.
1723
+ */
1724
+
1725
+ - (BOOL)boolForQuery:(NSString*)query, ...;
1726
+
1727
+ /** Return `double` value for query
1728
+
1729
+ @param query The SQL query to be performed.
1730
+ @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query.
1731
+
1732
+ @return `double` value.
1733
+
1734
+ @note To use this method from Swift, you must include `FMDatabaseAdditionsVariadic.swift` in your project.
1735
+ */
1736
+
1737
+ - (double)doubleForQuery:(NSString*)query, ...;
1738
+
1739
+ /** Return `NSString` value for query
1740
+
1741
+ @param query The SQL query to be performed.
1742
+ @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query.
1743
+
1744
+ @return `NSString` value.
1745
+
1746
+ @note To use this method from Swift, you must include `FMDatabaseAdditionsVariadic.swift` in your project.
1747
+ */
1748
+
1749
+ - (NSString*)stringForQuery:(NSString*)query, ...;
1750
+
1751
+ /** Return `NSData` value for query
1752
+
1753
+ @param query The SQL query to be performed.
1754
+ @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query.
1755
+
1756
+ @return `NSData` value.
1757
+
1758
+ @note To use this method from Swift, you must include `FMDatabaseAdditionsVariadic.swift` in your project.
1759
+ */
1760
+
1761
+ - (NSData*)dataForQuery:(NSString*)query, ...;
1762
+
1763
+ /** Return `NSDate` value for query
1764
+
1765
+ @param query The SQL query to be performed.
1766
+ @param ... A list of parameters that will be bound to the `?` placeholders in the SQL query.
1767
+
1768
+ @return `NSDate` value.
1769
+
1770
+ @note To use this method from Swift, you must include `FMDatabaseAdditionsVariadic.swift` in your project.
1771
+ */
1772
+
1773
+ - (NSDate*)dateForQuery:(NSString*)query, ...;
1774
+
1775
+
1776
+ // Notice that there's no dataNoCopyForQuery:.
1777
+ // That would be a bad idea, because we close out the result set, and then what
1778
+ // happens to the data that we just didn't copy? Who knows, not I.
1779
+
1780
+
1781
+ ///--------------------------------
1782
+ /// @name Schema related operations
1783
+ ///--------------------------------
1784
+
1785
+ /** Does table exist in database?
1786
+
1787
+ @param tableName The name of the table being looked for.
1788
+
1789
+ @return `YES` if table found; `NO` if not found.
1790
+ */
1791
+
1792
+ - (BOOL)tableExists:(NSString*)tableName;
1793
+
1794
+ /** The schema of the database.
1795
+
1796
+ This will be the schema for the entire database. For each entity, each row of the result set will include the following fields:
1797
+
1798
+ - `type` - The type of entity (e.g. table, index, view, or trigger)
1799
+ - `name` - The name of the object
1800
+ - `tbl_name` - The name of the table to which the object references
1801
+ - `rootpage` - The page number of the root b-tree page for tables and indices
1802
+ - `sql` - The SQL that created the entity
1803
+
1804
+ @return `FMResultSet` of schema; `nil` on error.
1805
+
1806
+ @see [SQLite File Format](http://www.sqlite.org/fileformat.html)
1807
+ */
1808
+
1809
+ - (FMResultSet*)getSchema;
1810
+
1811
+ /** The schema of the database.
1812
+
1813
+ This will be the schema for a particular table as report by SQLite `PRAGMA`, for example:
1814
+
1815
+ PRAGMA table_info('employees')
1816
+
1817
+ This will report:
1818
+
1819
+ - `cid` - The column ID number
1820
+ - `name` - The name of the column
1821
+ - `type` - The data type specified for the column
1822
+ - `notnull` - whether the field is defined as NOT NULL (i.e. values required)
1823
+ - `dflt_value` - The default value for the column
1824
+ - `pk` - Whether the field is part of the primary key of the table
1825
+
1826
+ @param tableName The name of the table for whom the schema will be returned.
1827
+
1828
+ @return `FMResultSet` of schema; `nil` on error.
1829
+
1830
+ @see [table_info](http://www.sqlite.org/pragma.html#pragma_table_info)
1831
+ */
1832
+
1833
+ - (FMResultSet*)getTableSchema:(NSString*)tableName;
1834
+
1835
+ /** Test to see if particular column exists for particular table in database
1836
+
1837
+ @param columnName The name of the column.
1838
+
1839
+ @param tableName The name of the table.
1840
+
1841
+ @return `YES` if column exists in table in question; `NO` otherwise.
1842
+ */
1843
+
1844
+ - (BOOL)columnExists:(NSString*)columnName inTableWithName:(NSString*)tableName;
1845
+
1846
+ /** Test to see if particular column exists for particular table in database
1847
+
1848
+ @param columnName The name of the column.
1849
+
1850
+ @param tableName The name of the table.
1851
+
1852
+ @return `YES` if column exists in table in question; `NO` otherwise.
1853
+
1854
+ @see columnExists:inTableWithName:
1855
+
1856
+ @warning Deprecated - use `<columnExists:inTableWithName:>` instead.
1857
+ */
1858
+
1859
+ - (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName __attribute__ ((deprecated));
1860
+
1861
+
1862
+ /** Validate SQL statement
1863
+
1864
+ This validates SQL statement by performing `sqlite3_prepare_v2`, but not returning the results, but instead immediately calling `sqlite3_finalize`.
1865
+
1866
+ @param sql The SQL statement being validated.
1867
+
1868
+ @param error This is a pointer to a `NSError` object that will receive the autoreleased `NSError` object if there was any error. If this is `nil`, no `NSError` result will be returned.
1869
+
1870
+ @return `YES` if validation succeeded without incident; `NO` otherwise.
1871
+
1872
+ */
1873
+
1874
+ - (BOOL)validateSQL:(NSString*)sql error:(NSError**)error;
1875
+
1876
+
1877
+ ///-----------------------------------
1878
+ /// @name Application identifier tasks
1879
+ ///-----------------------------------
1880
+
1881
+ /** Retrieve application ID
1882
+
1883
+ @return The `uint32_t` numeric value of the application ID.
1884
+
1885
+ @see setApplicationID:
1886
+ */
1887
+
1888
+ - (uint32_t)applicationID;
1889
+
1890
+ /** Set the application ID
1891
+
1892
+ @param appID The `uint32_t` numeric value of the application ID.
1893
+
1894
+ @see applicationID
1895
+ */
1896
+
1897
+ - (void)setApplicationID:(uint32_t)appID;
1898
+
1899
+ #if TARGET_OS_MAC && !TARGET_OS_IPHONE
1900
+ /** Retrieve application ID string
1901
+
1902
+ @return The `NSString` value of the application ID.
1903
+
1904
+ @see setApplicationIDString:
1905
+ */
1906
+
1907
+
1908
+ - (NSString*)applicationIDString;
1909
+
1910
+ /** Set the application ID string
1911
+
1912
+ @param string The `NSString` value of the application ID.
1913
+
1914
+ @see applicationIDString
1915
+ */
1916
+
1917
+ - (void)setApplicationIDString:(NSString*)string;
1918
+
1919
+ #endif
1920
+
1921
+ ///-----------------------------------
1922
+ /// @name user version identifier tasks
1923
+ ///-----------------------------------
1924
+
1925
+ /** Retrieve user version
1926
+
1927
+ @return The `uint32_t` numeric value of the user version.
1928
+
1929
+ @see setUserVersion:
1930
+ */
1931
+
1932
+ - (uint32_t)userVersion;
1933
+
1934
+ /** Set the user-version
1935
+
1936
+ @param version The `uint32_t` numeric value of the user version.
1937
+
1938
+ @see userVersion
1939
+ */
1940
+
1941
+ - (void)setUserVersion:(uint32_t)version;
1942
+
1943
+ @end
1944
+ //
1945
+ // FMDatabaseQueue.h
1946
+ // fmdb
1947
+ //
1948
+ // Created by August Mueller on 6/22/11.
1949
+ // Copyright 2011 Flying Meat Inc. All rights reserved.
1950
+ //
1951
+
1952
+
1953
+ @class FMDatabase;
1954
+
1955
+ /** To perform queries and updates on multiple threads, you'll want to use `FMDatabaseQueue`.
1956
+
1957
+ Using a single instance of `<FMDatabase>` from multiple threads at once is a bad idea. It has always been OK to make a `<FMDatabase>` object *per thread*. Just don't share a single instance across threads, and definitely not across multiple threads at the same time.
1958
+
1959
+ Instead, use `FMDatabaseQueue`. Here's how to use it:
1960
+
1961
+ First, make your queue.
1962
+
1963
+ FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
1964
+
1965
+ Then use it like so:
1966
+
1967
+ [queue inDatabase:^(FMDatabase *db) {
1968
+ [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
1969
+ [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
1970
+ [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
1971
+
1972
+ FMResultSet *rs = [db executeQuery:@"select * from foo"];
1973
+ while ([rs next]) {
1974
+ //…
1975
+ }
1976
+ }];
1977
+
1978
+ An easy way to wrap things up in a transaction can be done like this:
1979
+
1980
+ [queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
1981
+ [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
1982
+ [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
1983
+ [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
1984
+
1985
+ if (whoopsSomethingWrongHappened) {
1986
+ *rollback = YES;
1987
+ return;
1988
+ }
1989
+ // etc…
1990
+ [db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]];
1991
+ }];
1992
+
1993
+ `FMDatabaseQueue` will run the blocks on a serialized queue (hence the name of the class). So if you call `FMDatabaseQueue`'s methods from multiple threads at the same time, they will be executed in the order they are received. This way queries and updates won't step on each other's toes, and every one is happy.
1994
+
1995
+ ### See also
1996
+
1997
+ - `<FMDatabase>`
1998
+
1999
+ @warning Do not instantiate a single `<FMDatabase>` object and use it across multiple threads. Use `FMDatabaseQueue` instead.
2000
+
2001
+ @warning The calls to `FMDatabaseQueue`'s methods are blocking. So even though you are passing along blocks, they will **not** be run on another thread.
2002
+
2003
+ */
2004
+
2005
+ @interface FMDatabaseQueue : NSObject {
2006
+ NSString *_path;
2007
+ dispatch_queue_t _queue;
2008
+ FMDatabase *_db;
2009
+ int _openFlags;
2010
+ NSString *_vfsName;
2011
+ }
2012
+
2013
+ /** Path of database */
2014
+
2015
+ @property (atomic, retain) NSString *path;
2016
+
2017
+ /** Open flags */
2018
+
2019
+ @property (atomic, readonly) int openFlags;
2020
+
2021
+ /** Custom virtual file system name */
2022
+
2023
+ @property (atomic, copy) NSString *vfsName;
2024
+
2025
+ ///----------------------------------------------------
2026
+ /// @name Initialization, opening, and closing of queue
2027
+ ///----------------------------------------------------
2028
+
2029
+ /** Create queue using path.
2030
+
2031
+ @param aPath The file path of the database.
2032
+
2033
+ @return The `FMDatabaseQueue` object. `nil` on error.
2034
+ */
2035
+
2036
+ + (instancetype)databaseQueueWithPath:(NSString*)aPath;
2037
+
2038
+ /** Create queue using path and specified flags.
2039
+
2040
+ @param aPath The file path of the database.
2041
+ @param openFlags Flags passed to the openWithFlags method of the database
2042
+
2043
+ @return The `FMDatabaseQueue` object. `nil` on error.
2044
+ */
2045
+ + (instancetype)databaseQueueWithPath:(NSString*)aPath flags:(int)openFlags;
2046
+
2047
+ /** Create queue using path.
2048
+
2049
+ @param aPath The file path of the database.
2050
+
2051
+ @return The `FMDatabaseQueue` object. `nil` on error.
2052
+ */
2053
+
2054
+ - (instancetype)initWithPath:(NSString*)aPath;
2055
+
2056
+ /** Create queue using path and specified flags.
2057
+
2058
+ @param aPath The file path of the database.
2059
+ @param openFlags Flags passed to the openWithFlags method of the database
2060
+
2061
+ @return The `FMDatabaseQueue` object. `nil` on error.
2062
+ */
2063
+
2064
+ - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
2065
+
2066
+ /** Create queue using path and specified flags.
2067
+
2068
+ @param aPath The file path of the database.
2069
+ @param openFlags Flags passed to the openWithFlags method of the database
2070
+ @param vfsName The name of a custom virtual file system
2071
+
2072
+ @return The `FMDatabaseQueue` object. `nil` on error.
2073
+ */
2074
+
2075
+ - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName;
2076
+
2077
+ /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object.
2078
+
2079
+ Subclasses can override this method to return specified Class of 'FMDatabase' subclass.
2080
+
2081
+ @return The Class of 'FMDatabase' subclass, that will be used to instantiate database object.
2082
+ */
2083
+
2084
+ + (Class)databaseClass;
2085
+
2086
+ /** Close database used by queue. */
2087
+
2088
+ - (void)close;
2089
+
2090
+ /** Interupt pending database operation. */
2091
+
2092
+ - (void)interrupt;
2093
+
2094
+ ///-----------------------------------------------
2095
+ /// @name Dispatching database operations to queue
2096
+ ///-----------------------------------------------
2097
+
2098
+ /** Synchronously perform database operations on queue.
2099
+
2100
+ @param block The code to be run on the queue of `FMDatabaseQueue`
2101
+ */
2102
+
2103
+ - (void)inDatabase:(void (^)(FMDatabase *db))block;
2104
+
2105
+ /** Synchronously perform database operations on queue, using transactions.
2106
+
2107
+ @param block The code to be run on the queue of `FMDatabaseQueue`
2108
+ */
2109
+
2110
+ - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
2111
+
2112
+ /** Synchronously perform database operations on queue, using deferred transactions.
2113
+
2114
+ @param block The code to be run on the queue of `FMDatabaseQueue`
2115
+ */
2116
+
2117
+ - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
2118
+
2119
+ ///-----------------------------------------------
2120
+ /// @name Dispatching database operations to queue
2121
+ ///-----------------------------------------------
2122
+
2123
+ /** Synchronously perform database operations using save point.
2124
+
2125
+ @param block The code to be run on the queue of `FMDatabaseQueue`
2126
+ */
2127
+
2128
+ // NOTE: you can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock.
2129
+ // If you need to nest, use FMDatabase's startSavePointWithName:error: instead.
2130
+ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
2131
+
2132
+ @end
2133
+
2134
+ //
2135
+ // FMDatabasePool.h
2136
+ // fmdb
2137
+ //
2138
+ // Created by August Mueller on 6/22/11.
2139
+ // Copyright 2011 Flying Meat Inc. All rights reserved.
2140
+ //
2141
+
2142
+
2143
+ @class FMDatabase;
2144
+
2145
+ /** Pool of `<FMDatabase>` objects.
2146
+
2147
+ ### See also
2148
+
2149
+ - `<FMDatabaseQueue>`
2150
+ - `<FMDatabase>`
2151
+
2152
+ @warning Before using `FMDatabasePool`, please consider using `<FMDatabaseQueue>` instead.
2153
+
2154
+ If you really really really know what you're doing and `FMDatabasePool` is what
2155
+ you really really need (ie, you're using a read only database), OK you can use
2156
+ it. But just be careful not to deadlock!
2157
+
2158
+ For an example on deadlocking, search for:
2159
+ `ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS_OTHERWISE_YOULL_DEADLOCK_USE_FMDATABASEQUEUE_INSTEAD`
2160
+ in the main.m file.
2161
+ */
2162
+
2163
+ @interface FMDatabasePool : NSObject {
2164
+ NSString *_path;
2165
+
2166
+ dispatch_queue_t _lockQueue;
2167
+
2168
+ NSMutableArray *_databaseInPool;
2169
+ NSMutableArray *_databaseOutPool;
2170
+
2171
+ __unsafe_unretained id _delegate;
2172
+
2173
+ NSUInteger _maximumNumberOfDatabasesToCreate;
2174
+ int _openFlags;
2175
+ NSString *_vfsName;
2176
+ }
2177
+
2178
+ /** Database path */
2179
+
2180
+ @property (atomic, retain) NSString *path;
2181
+
2182
+ /** Delegate object */
2183
+
2184
+ @property (atomic, assign) id delegate;
2185
+
2186
+ /** Maximum number of databases to create */
2187
+
2188
+ @property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate;
2189
+
2190
+ /** Open flags */
2191
+
2192
+ @property (atomic, readonly) int openFlags;
2193
+
2194
+ /** Custom virtual file system name */
2195
+
2196
+ @property (atomic, copy) NSString *vfsName;
2197
+
2198
+
2199
+ ///---------------------
2200
+ /// @name Initialization
2201
+ ///---------------------
2202
+
2203
+ /** Create pool using path.
2204
+
2205
+ @param aPath The file path of the database.
2206
+
2207
+ @return The `FMDatabasePool` object. `nil` on error.
2208
+ */
2209
+
2210
+ + (instancetype)databasePoolWithPath:(NSString*)aPath;
2211
+
2212
+ /** Create pool using path and specified flags
2213
+
2214
+ @param aPath The file path of the database.
2215
+ @param openFlags Flags passed to the openWithFlags method of the database
2216
+
2217
+ @return The `FMDatabasePool` object. `nil` on error.
2218
+ */
2219
+
2220
+ + (instancetype)databasePoolWithPath:(NSString*)aPath flags:(int)openFlags;
2221
+
2222
+ /** Create pool using path.
2223
+
2224
+ @param aPath The file path of the database.
2225
+
2226
+ @return The `FMDatabasePool` object. `nil` on error.
2227
+ */
2228
+
2229
+ - (instancetype)initWithPath:(NSString*)aPath;
2230
+
2231
+ /** Create pool using path and specified flags.
2232
+
2233
+ @param aPath The file path of the database.
2234
+ @param openFlags Flags passed to the openWithFlags method of the database
2235
+
2236
+ @return The `FMDatabasePool` object. `nil` on error.
2237
+ */
2238
+
2239
+ - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
2240
+
2241
+ /** Create pool using path and specified flags.
2242
+
2243
+ @param aPath The file path of the database.
2244
+ @param openFlags Flags passed to the openWithFlags method of the database
2245
+ @param vfsName The name of a custom virtual file system
2246
+
2247
+ @return The `FMDatabasePool` object. `nil` on error.
2248
+ */
2249
+
2250
+ - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName;
2251
+
2252
+ /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object.
2253
+
2254
+ Subclasses can override this method to return specified Class of 'FMDatabase' subclass.
2255
+
2256
+ @return The Class of 'FMDatabase' subclass, that will be used to instantiate database object.
2257
+ */
2258
+
2259
+ + (Class)databaseClass;
2260
+
2261
+ ///------------------------------------------------
2262
+ /// @name Keeping track of checked in/out databases
2263
+ ///------------------------------------------------
2264
+
2265
+ /** Number of checked-in databases in pool
2266
+
2267
+ @returns Number of databases
2268
+ */
2269
+
2270
+ - (NSUInteger)countOfCheckedInDatabases;
2271
+
2272
+ /** Number of checked-out databases in pool
2273
+
2274
+ @returns Number of databases
2275
+ */
2276
+
2277
+ - (NSUInteger)countOfCheckedOutDatabases;
2278
+
2279
+ /** Total number of databases in pool
2280
+
2281
+ @returns Number of databases
2282
+ */
2283
+
2284
+ - (NSUInteger)countOfOpenDatabases;
2285
+
2286
+ /** Release all databases in pool */
2287
+
2288
+ - (void)releaseAllDatabases;
2289
+
2290
+ ///------------------------------------------
2291
+ /// @name Perform database operations in pool
2292
+ ///------------------------------------------
2293
+
2294
+ /** Synchronously perform database operations in pool.
2295
+
2296
+ @param block The code to be run on the `FMDatabasePool` pool.
2297
+ */
2298
+
2299
+ - (void)inDatabase:(void (^)(FMDatabase *db))block;
2300
+
2301
+ /** Synchronously perform database operations in pool using transaction.
2302
+
2303
+ @param block The code to be run on the `FMDatabasePool` pool.
2304
+ */
2305
+
2306
+ - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
2307
+
2308
+ /** Synchronously perform database operations in pool using deferred transaction.
2309
+
2310
+ @param block The code to be run on the `FMDatabasePool` pool.
2311
+ */
2312
+
2313
+ - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
2314
+
2315
+ /** Synchronously perform database operations in pool using save point.
2316
+
2317
+ @param block The code to be run on the `FMDatabasePool` pool.
2318
+
2319
+ @return `NSError` object if error; `nil` if successful.
2320
+
2321
+ @warning You can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock. If you need to nest, use `<[FMDatabase startSavePointWithName:error:]>` instead.
2322
+ */
2323
+
2324
+ - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
2325
+
2326
+ @end
2327
+
2328
+
2329
+ /** FMDatabasePool delegate category
2330
+
2331
+ This is a category that defines the protocol for the FMDatabasePool delegate
2332
+ */
2333
+
2334
+ @interface NSObject (FMDatabasePoolDelegate)
2335
+
2336
+ /** Asks the delegate whether database should be added to the pool.
2337
+
2338
+ @param pool The `FMDatabasePool` object.
2339
+ @param database The `FMDatabase` object.
2340
+
2341
+ @return `YES` if it should add database to pool; `NO` if not.
2342
+
2343
+ */
2344
+
2345
+ - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database;
2346
+
2347
+ /** Tells the delegate that database was added to the pool.
2348
+
2349
+ @param pool The `FMDatabasePool` object.
2350
+ @param database The `FMDatabase` object.
2351
+
2352
+ */
2353
+
2354
+ - (void)databasePool:(FMDatabasePool*)pool didAddDatabase:(FMDatabase*)database;
2355
+
2356
+ @end
2357
+