FMDatabase.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. #import <Foundation/Foundation.h>
  2. #import "FMResultSet.h"
  3. #import "FMDatabasePool.h"
  4. #if ! __has_feature(objc_arc)
  5. #define FMDBAutorelease(__v) ([__v autorelease]);
  6. #define FMDBReturnAutoreleased FMDBAutorelease
  7. #define FMDBRetain(__v) ([__v retain]);
  8. #define FMDBReturnRetained FMDBRetain
  9. #define FMDBRelease(__v) ([__v release]);
  10. #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
  11. #else
  12. // -fobjc-arc
  13. #define FMDBAutorelease(__v)
  14. #define FMDBReturnAutoreleased(__v) (__v)
  15. #define FMDBRetain(__v)
  16. #define FMDBReturnRetained(__v) (__v)
  17. #define FMDBRelease(__v)
  18. // If OS_OBJECT_USE_OBJC=1, then the dispatch objects will be treated like ObjC objects
  19. // and will participate in ARC.
  20. // See the section on "Dispatch Queues and Automatic Reference Counting" in "Grand Central Dispatch (GCD) Reference" for details.
  21. #if OS_OBJECT_USE_OBJC
  22. #define FMDBDispatchQueueRelease(__v)
  23. #else
  24. #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
  25. #endif
  26. #endif
  27. #if !__has_feature(objc_instancetype)
  28. #define instancetype id
  29. #endif
  30. typedef int(^FMDBExecuteStatementsCallbackBlock)(NSDictionary *resultsDictionary);
  31. /** A SQLite ([http://sqlite.org/](http://sqlite.org/)) Objective-C wrapper.
  32. ### Usage
  33. The three main classes in FMDB are:
  34. - `FMDatabase` - Represents a single SQLite database. Used for executing SQL statements.
  35. - `<FMResultSet>` - Represents the results of executing a query on an `FMDatabase`.
  36. - `<FMDatabaseQueue>` - If you want to perform queries and updates on multiple threads, you'll want to use this class.
  37. ### See also
  38. - `<FMDatabasePool>` - A pool of `FMDatabase` objects.
  39. - `<FMStatement>` - A wrapper for `sqlite_stmt`.
  40. ### External links
  41. - [FMDB on GitHub](https://github.com/ccgus/fmdb) including introductory documentation
  42. - [SQLite web site](http://sqlite.org/)
  43. - [FMDB mailing list](http://groups.google.com/group/fmdb)
  44. - [SQLite FAQ](http://www.sqlite.org/faq.html)
  45. @warning Do not instantiate a single `FMDatabase` object and use it across multiple threads. Instead, use `<FMDatabaseQueue>`.
  46. */
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wobjc-interface-ivars"
  49. @interface FMDatabase : NSObject {
  50. void* _db;
  51. NSString* _databasePath;
  52. BOOL _logsErrors;
  53. BOOL _crashOnErrors;
  54. BOOL _traceExecution;
  55. BOOL _checkedOut;
  56. BOOL _shouldCacheStatements;
  57. BOOL _isExecutingStatement;
  58. BOOL _inTransaction;
  59. NSTimeInterval _maxBusyRetryTimeInterval;
  60. NSTimeInterval _startBusyRetryTime;
  61. NSMutableDictionary *_cachedStatements;
  62. NSMutableSet *_openResultSets;
  63. NSMutableSet *_openFunctions;
  64. NSDateFormatter *_dateFormat;
  65. }
  66. ///-----------------
  67. /// @name Properties
  68. ///-----------------
  69. /** Whether should trace execution */
  70. @property (atomic, assign) BOOL traceExecution;
  71. /** Whether checked out or not */
  72. @property (atomic, assign) BOOL checkedOut;
  73. /** Crash on errors */
  74. @property (atomic, assign) BOOL crashOnErrors;
  75. /** Logs errors */
  76. @property (atomic, assign) BOOL logsErrors;
  77. /** Dictionary of cached statements */
  78. @property (atomic, retain) NSMutableDictionary *cachedStatements;
  79. ///---------------------
  80. /// @name Initialization
  81. ///---------------------
  82. /** Create a `FMDatabase` object.
  83. An `FMDatabase` is created with a path to a SQLite database file. This path can be one of these three:
  84. 1. A file system path. The file does not have to exist on disk. If it does not exist, it is created for you.
  85. 2. An empty string (`@""`). An empty database is created at a temporary location. This database is deleted with the `FMDatabase` connection is closed.
  86. 3. `nil`. An in-memory database is created. This database will be destroyed with the `FMDatabase` connection is closed.
  87. For example, to create/open a database in your Mac OS X `tmp` folder:
  88. FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
  89. Or, in iOS, you might open a database in the app's `Documents` directory:
  90. NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  91. NSString *dbPath = [docsPath stringByAppendingPathComponent:@"test.db"];
  92. FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
  93. (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))
  94. @param inPath Path of database file
  95. @return `FMDatabase` object if successful; `nil` if failure.
  96. */
  97. + (instancetype)databaseWithPath:(NSString*)inPath;
  98. /** Initialize a `FMDatabase` object.
  99. An `FMDatabase` is created with a path to a SQLite database file. This path can be one of these three:
  100. 1. A file system path. The file does not have to exist on disk. If it does not exist, it is created for you.
  101. 2. An empty string (`@""`). An empty database is created at a temporary location. This database is deleted with the `FMDatabase` connection is closed.
  102. 3. `nil`. An in-memory database is created. This database will be destroyed with the `FMDatabase` connection is closed.
  103. For example, to create/open a database in your Mac OS X `tmp` folder:
  104. FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
  105. Or, in iOS, you might open a database in the app's `Documents` directory:
  106. NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  107. NSString *dbPath = [docsPath stringByAppendingPathComponent:@"test.db"];
  108. FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
  109. (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))
  110. @param inPath Path of database file
  111. @return `FMDatabase` object if successful; `nil` if failure.
  112. */
  113. - (instancetype)initWithPath:(NSString*)inPath;
  114. ///-----------------------------------
  115. /// @name Opening and closing database
  116. ///-----------------------------------
  117. /** Opening a new database connection
  118. The database is opened for reading and writing, and is created if it does not already exist.
  119. @return `YES` if successful, `NO` on error.
  120. @see [sqlite3_open()](http://sqlite.org/c3ref/open.html)
  121. @see openWithFlags:
  122. @see close
  123. */
  124. - (BOOL)open;
  125. /** Opening a new database connection with flags and an optional virtual file system (VFS)
  126. @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:
  127. `SQLITE_OPEN_READONLY`
  128. The database is opened in read-only mode. If the database does not already exist, an error is returned.
  129. `SQLITE_OPEN_READWRITE`
  130. 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.
  131. `SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE`
  132. 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.
  133. @return `YES` if successful, `NO` on error.
  134. @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
  135. @see open
  136. @see close
  137. */
  138. - (BOOL)openWithFlags:(int)flags;
  139. /** Opening a new database connection with flags and an optional virtual file system (VFS)
  140. @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:
  141. `SQLITE_OPEN_READONLY`
  142. The database is opened in read-only mode. If the database does not already exist, an error is returned.
  143. `SQLITE_OPEN_READWRITE`
  144. 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.
  145. `SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE`
  146. 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.
  147. @param vfsName If vfs is given the value is passed to the vfs parameter of sqlite3_open_v2.
  148. @return `YES` if successful, `NO` on error.
  149. @see [sqlite3_open_v2()](http://sqlite.org/c3ref/open.html)
  150. @see open
  151. @see close
  152. */
  153. - (BOOL)openWithFlags:(int)flags vfs:(NSString *)vfsName;
  154. /** Closing a database connection
  155. @return `YES` if success, `NO` on error.
  156. @see [sqlite3_close()](http://sqlite.org/c3ref/close.html)
  157. @see open
  158. @see openWithFlags:
  159. */
  160. - (BOOL)close;
  161. /** Test to see if we have a good connection to the database.
  162. This will confirm whether:
  163. - is database open
  164. - if open, it will try a simple SELECT statement and confirm that it succeeds.
  165. @return `YES` if everything succeeds, `NO` on failure.
  166. */
  167. - (BOOL)goodConnection;
  168. ///----------------------
  169. /// @name Perform updates
  170. ///----------------------
  171. /** Execute single update statement
  172. 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.
  173. 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.
  174. @param sql The SQL to be performed, with optional `?` placeholders.
  175. @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.
  176. @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.).
  177. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  178. @see lastError
  179. @see lastErrorCode
  180. @see lastErrorMessage
  181. @see [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html)
  182. */
  183. - (BOOL)executeUpdate:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ...;
  184. /** Execute single update statement
  185. @see executeUpdate:withErrorAndBindings:
  186. @warning **Deprecated**: Please use `<executeUpdate:withErrorAndBindings>` instead.
  187. */
  188. - (BOOL)update:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ... __attribute__ ((deprecated));
  189. /** Execute single update statement
  190. 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.
  191. 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.
  192. @param sql The SQL to be performed, with optional `?` placeholders.
  193. @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.).
  194. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  195. @see lastError
  196. @see lastErrorCode
  197. @see lastErrorMessage
  198. @see [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html)
  199. @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.
  200. @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:>`.
  201. */
  202. - (BOOL)executeUpdate:(NSString*)sql, ...;
  203. /** Execute single update statement
  204. 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.
  205. @param format The SQL to be performed, with `printf`-style escape sequences.
  206. @param ... Optional parameters to bind to use in conjunction with the `printf`-style escape sequences in the SQL statement.
  207. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  208. @see executeUpdate:
  209. @see lastError
  210. @see lastErrorCode
  211. @see lastErrorMessage
  212. @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
  213. [db executeUpdateWithFormat:@"INSERT INTO test (name) VALUES (%@)", @"Gus"];
  214. is actually replacing the `%@` with `?` placeholder, and then performing something equivalent to `<executeUpdate:>`
  215. [db executeUpdate:@"INSERT INTO test (name) VALUES (?)", @"Gus"];
  216. 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 (%@)`.
  217. */
  218. - (BOOL)executeUpdateWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
  219. /** Execute single update statement
  220. 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.
  221. 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.
  222. @param sql The SQL to be performed, with optional `?` placeholders.
  223. @param arguments A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
  224. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  225. @see executeUpdate:values:error:
  226. @see lastError
  227. @see lastErrorCode
  228. @see lastErrorMessage
  229. */
  230. - (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
  231. /** Execute single update statement
  232. 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.
  233. 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.
  234. This is similar to `<executeUpdate:withArgumentsInArray:>`, except that this also accepts a pointer to a `NSError` pointer, so that errors can be returned.
  235. In Swift 2, this throws errors, as if it were defined as follows:
  236. `func executeUpdate(sql: String!, values: [AnyObject]!) throws -> Bool`
  237. @param sql The SQL to be performed, with optional `?` placeholders.
  238. @param values A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
  239. @param error A `NSError` object to receive any error object (if any).
  240. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  241. @see lastError
  242. @see lastErrorCode
  243. @see lastErrorMessage
  244. */
  245. - (BOOL)executeUpdate:(NSString*)sql values:(NSArray *)values error:(NSError * __autoreleasing *)error;
  246. /** Execute single update statement
  247. 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.
  248. 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.
  249. @param sql The SQL to be performed, with optional `?` placeholders.
  250. @param arguments A `NSDictionary` of objects keyed by column names that will be used when binding values to the `?` placeholders in the SQL statement.
  251. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  252. @see lastError
  253. @see lastErrorCode
  254. @see lastErrorMessage
  255. */
  256. - (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;
  257. /** Execute single update statement
  258. 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.
  259. 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.
  260. @param sql The SQL to be performed, with optional `?` placeholders.
  261. @param args A `va_list` of arguments.
  262. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  263. @see lastError
  264. @see lastErrorCode
  265. @see lastErrorMessage
  266. */
  267. - (BOOL)executeUpdate:(NSString*)sql withVAList: (va_list)args;
  268. /** Execute multiple SQL statements
  269. 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`.
  270. @param sql The SQL to be performed
  271. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  272. @see executeStatements:withResultBlock:
  273. @see [sqlite3_exec()](http://sqlite.org/c3ref/exec.html)
  274. */
  275. - (BOOL)executeStatements:(NSString *)sql;
  276. /** Execute multiple SQL statements with callback handler
  277. 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`.
  278. @param sql The SQL to be performed.
  279. @param block A block that will be called for any result sets returned by any SQL statements.
  280. Note, if you supply this block, it must return integer value, zero upon success (this would be a good opportunity to use SQLITE_OK),
  281. 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.
  282. This may be `nil` if you don't care to receive any results.
  283. @return `YES` upon success; `NO` upon failure. If failed, you can call `<lastError>`,
  284. `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  285. @see executeStatements:
  286. @see [sqlite3_exec()](http://sqlite.org/c3ref/exec.html)
  287. */
  288. - (BOOL)executeStatements:(NSString *)sql withResultBlock:(FMDBExecuteStatementsCallbackBlock)block;
  289. /** Last insert rowid
  290. 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.
  291. 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.
  292. @return The rowid of the last inserted row.
  293. @see [sqlite3_last_insert_rowid()](http://sqlite.org/c3ref/last_insert_rowid.html)
  294. */
  295. - (int64_t)lastInsertRowId;
  296. /** The number of rows changed by prior SQL statement.
  297. 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.
  298. @return The number of rows changed by prior SQL statement.
  299. @see [sqlite3_changes()](http://sqlite.org/c3ref/changes.html)
  300. */
  301. - (int)changes;
  302. ///-------------------------
  303. /// @name Retrieving results
  304. ///-------------------------
  305. /** Execute select statement
  306. 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.
  307. 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.
  308. 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.
  309. @param sql The SELECT statement to be performed, with optional `?` placeholders.
  310. @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.).
  311. @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.
  312. @see FMResultSet
  313. @see [`FMResultSet next`](<[FMResultSet next]>)
  314. @see [`sqlite3_bind`](http://sqlite.org/c3ref/bind_blob.html)
  315. @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:>`.
  316. */
  317. - (FMResultSet *)executeQuery:(NSString*)sql, ...;
  318. /** Execute select statement
  319. 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.
  320. 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.
  321. @param format The SQL to be performed, with `printf`-style escape sequences.
  322. @param ... Optional parameters to bind to use in conjunction with the `printf`-style escape sequences in the SQL statement.
  323. @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.
  324. @see executeQuery:
  325. @see FMResultSet
  326. @see [`FMResultSet next`](<[FMResultSet next]>)
  327. @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
  328. [db executeQueryWithFormat:@"SELECT * FROM test WHERE name=%@", @"Gus"];
  329. is actually replacing the `%@` with `?` placeholder, and then performing something equivalent to `<executeQuery:>`
  330. [db executeQuery:@"SELECT * FROM test WHERE name=?", @"Gus"];
  331. 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=%@`.
  332. */
  333. - (FMResultSet *)executeQueryWithFormat:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  334. /** Execute select statement
  335. 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.
  336. 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.
  337. @param sql The SELECT statement to be performed, with optional `?` placeholders.
  338. @param arguments A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
  339. @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.
  340. @see -executeQuery:values:error:
  341. @see FMResultSet
  342. @see [`FMResultSet next`](<[FMResultSet next]>)
  343. */
  344. - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments;
  345. /** Execute select statement
  346. 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.
  347. 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.
  348. This is similar to `<executeQuery:withArgumentsInArray:>`, except that this also accepts a pointer to a `NSError` pointer, so that errors can be returned.
  349. In Swift 2, this throws errors, as if it were defined as follows:
  350. `func executeQuery(sql: String!, values: [AnyObject]!) throws -> FMResultSet!`
  351. @param sql The SELECT statement to be performed, with optional `?` placeholders.
  352. @param values A `NSArray` of objects to be used when binding values to the `?` placeholders in the SQL statement.
  353. @param error A `NSError` object to receive any error object (if any).
  354. @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.
  355. @see FMResultSet
  356. @see [`FMResultSet next`](<[FMResultSet next]>)
  357. @note When called from Swift, only use the first two parameters, `sql` and `values`. This but throws the error.
  358. */
  359. - (FMResultSet *)executeQuery:(NSString *)sql values:(NSArray *)values error:(NSError * __autoreleasing *)error;
  360. /** Execute select statement
  361. 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.
  362. 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.
  363. @param sql The SELECT statement to be performed, with optional `?` placeholders.
  364. @param arguments A `NSDictionary` of objects keyed by column names that will be used when binding values to the `?` placeholders in the SQL statement.
  365. @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.
  366. @see FMResultSet
  367. @see [`FMResultSet next`](<[FMResultSet next]>)
  368. */
  369. - (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary *)arguments;
  370. // Documentation forthcoming.
  371. - (FMResultSet *)executeQuery:(NSString*)sql withVAList: (va_list)args;
  372. ///-------------------
  373. /// @name Transactions
  374. ///-------------------
  375. /** Begin a transaction
  376. @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  377. @see commit
  378. @see rollback
  379. @see beginDeferredTransaction
  380. @see inTransaction
  381. */
  382. - (BOOL)beginTransaction;
  383. /** Begin a deferred transaction
  384. @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  385. @see commit
  386. @see rollback
  387. @see beginTransaction
  388. @see inTransaction
  389. */
  390. - (BOOL)beginDeferredTransaction;
  391. /** Commit a transaction
  392. Commit a transaction that was initiated with either `<beginTransaction>` or with `<beginDeferredTransaction>`.
  393. @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  394. @see beginTransaction
  395. @see beginDeferredTransaction
  396. @see rollback
  397. @see inTransaction
  398. */
  399. - (BOOL)commit;
  400. /** Rollback a transaction
  401. Rollback a transaction that was initiated with either `<beginTransaction>` or with `<beginDeferredTransaction>`.
  402. @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  403. @see beginTransaction
  404. @see beginDeferredTransaction
  405. @see commit
  406. @see inTransaction
  407. */
  408. - (BOOL)rollback;
  409. /** Identify whether currently in a transaction or not
  410. @return `YES` if currently within transaction; `NO` if not.
  411. @see beginTransaction
  412. @see beginDeferredTransaction
  413. @see commit
  414. @see rollback
  415. */
  416. - (BOOL)inTransaction;
  417. ///----------------------------------------
  418. /// @name Cached statements and result sets
  419. ///----------------------------------------
  420. /** Clear cached statements */
  421. - (void)clearCachedStatements;
  422. /** Close all open result sets */
  423. - (void)closeOpenResultSets;
  424. /** Whether database has any open result sets
  425. @return `YES` if there are open result sets; `NO` if not.
  426. */
  427. - (BOOL)hasOpenResultSets;
  428. /** Return whether should cache statements or not
  429. @return `YES` if should cache statements; `NO` if not.
  430. */
  431. - (BOOL)shouldCacheStatements;
  432. /** Set whether should cache statements or not
  433. @param value `YES` if should cache statements; `NO` if not.
  434. */
  435. - (void)setShouldCacheStatements:(BOOL)value;
  436. /** Interupt pending database operation
  437. This method causes any pending database operation to abort and return at its earliest opportunity
  438. @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  439. */
  440. - (BOOL)interrupt;
  441. ///-------------------------
  442. /// @name Encryption methods
  443. ///-------------------------
  444. /** Set encryption key.
  445. @param key The key to be used.
  446. @return `YES` if success, `NO` on error.
  447. @see https://www.zetetic.net/sqlcipher/
  448. @warning You need to have purchased the sqlite encryption extensions for this method to work.
  449. */
  450. - (BOOL)setKey:(NSString*)key;
  451. /** Reset encryption key
  452. @param key The key to be used.
  453. @return `YES` if success, `NO` on error.
  454. @see https://www.zetetic.net/sqlcipher/
  455. @warning You need to have purchased the sqlite encryption extensions for this method to work.
  456. */
  457. - (BOOL)rekey:(NSString*)key;
  458. /** Set encryption key using `keyData`.
  459. @param keyData The `NSData` to be used.
  460. @return `YES` if success, `NO` on error.
  461. @see https://www.zetetic.net/sqlcipher/
  462. @warning You need to have purchased the sqlite encryption extensions for this method to work.
  463. */
  464. - (BOOL)setKeyWithData:(NSData *)keyData;
  465. /** Reset encryption key using `keyData`.
  466. @param keyData The `NSData` to be used.
  467. @return `YES` if success, `NO` on error.
  468. @see https://www.zetetic.net/sqlcipher/
  469. @warning You need to have purchased the sqlite encryption extensions for this method to work.
  470. */
  471. - (BOOL)rekeyWithData:(NSData *)keyData;
  472. ///------------------------------
  473. /// @name General inquiry methods
  474. ///------------------------------
  475. /** The path of the database file
  476. @return path of database.
  477. */
  478. - (NSString *)databasePath;
  479. /** The underlying SQLite handle
  480. @return The `sqlite3` pointer.
  481. */
  482. - (void*)sqliteHandle;
  483. ///-----------------------------
  484. /// @name Retrieving error codes
  485. ///-----------------------------
  486. /** Last error message
  487. 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.
  488. @return `NSString` of the last error message.
  489. @see [sqlite3_errmsg()](http://sqlite.org/c3ref/errcode.html)
  490. @see lastErrorCode
  491. @see lastError
  492. */
  493. - (NSString*)lastErrorMessage;
  494. /** Last error code
  495. 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.
  496. @return Integer value of the last error code.
  497. @see [sqlite3_errcode()](http://sqlite.org/c3ref/errcode.html)
  498. @see lastErrorMessage
  499. @see lastError
  500. */
  501. - (int)lastErrorCode;
  502. /** Had error
  503. @return `YES` if there was an error, `NO` if no error.
  504. @see lastError
  505. @see lastErrorCode
  506. @see lastErrorMessage
  507. */
  508. - (BOOL)hadError;
  509. /** Last error
  510. @return `NSError` representing the last error.
  511. @see lastErrorCode
  512. @see lastErrorMessage
  513. */
  514. - (NSError*)lastError;
  515. // description forthcoming
  516. - (void)setMaxBusyRetryTimeInterval:(NSTimeInterval)timeoutInSeconds;
  517. - (NSTimeInterval)maxBusyRetryTimeInterval;
  518. ///------------------
  519. /// @name Save points
  520. ///------------------
  521. /** Start save point
  522. @param name Name of save point.
  523. @param outErr A `NSError` object to receive any error object (if any).
  524. @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  525. @see releaseSavePointWithName:error:
  526. @see rollbackToSavePointWithName:error:
  527. */
  528. - (BOOL)startSavePointWithName:(NSString*)name error:(NSError**)outErr;
  529. /** Release save point
  530. @param name Name of save point.
  531. @param outErr A `NSError` object to receive any error object (if any).
  532. @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  533. @see startSavePointWithName:error:
  534. @see rollbackToSavePointWithName:error:
  535. */
  536. - (BOOL)releaseSavePointWithName:(NSString*)name error:(NSError**)outErr;
  537. /** Roll back to save point
  538. @param name Name of save point.
  539. @param outErr A `NSError` object to receive any error object (if any).
  540. @return `YES` on success; `NO` on failure. If failed, you can call `<lastError>`, `<lastErrorCode>`, or `<lastErrorMessage>` for diagnostic information regarding the failure.
  541. @see startSavePointWithName:error:
  542. @see releaseSavePointWithName:error:
  543. */
  544. - (BOOL)rollbackToSavePointWithName:(NSString*)name error:(NSError**)outErr;
  545. /** Start save point
  546. @param block Block of code to perform from within save point.
  547. @return The NSError corresponding to the error, if any. If no error, returns `nil`.
  548. @see startSavePointWithName:error:
  549. @see releaseSavePointWithName:error:
  550. @see rollbackToSavePointWithName:error:
  551. */
  552. - (NSError*)inSavePoint:(void (^)(BOOL *rollback))block;
  553. ///----------------------------
  554. /// @name SQLite library status
  555. ///----------------------------
  556. /** Test to see if the library is threadsafe
  557. @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.
  558. @see [sqlite3_threadsafe()](http://sqlite.org/c3ref/threadsafe.html)
  559. */
  560. + (BOOL)isSQLiteThreadSafe;
  561. /** Run-time library version numbers
  562. @return The sqlite library version string.
  563. @see [sqlite3_libversion()](http://sqlite.org/c3ref/libversion.html)
  564. */
  565. + (NSString*)sqliteLibVersion;
  566. + (NSString*)FMDBUserVersion;
  567. + (SInt32)FMDBVersion;
  568. ///------------------------
  569. /// @name Make SQL function
  570. ///------------------------
  571. /** Adds SQL functions or aggregates or to redefine the behavior of existing SQL functions or aggregates.
  572. For example:
  573. [queue inDatabase:^(FMDatabase *adb) {
  574. [adb executeUpdate:@"create table ftest (foo text)"];
  575. [adb executeUpdate:@"insert into ftest values ('hello')"];
  576. [adb executeUpdate:@"insert into ftest values ('hi')"];
  577. [adb executeUpdate:@"insert into ftest values ('not h!')"];
  578. [adb executeUpdate:@"insert into ftest values ('definitely not h!')"];
  579. [adb makeFunctionNamed:@"StringStartsWithH" maximumArguments:1 withBlock:^(sqlite3_context *context, int aargc, sqlite3_value **aargv) {
  580. if (sqlite3_value_type(aargv[0]) == SQLITE_TEXT) {
  581. @autoreleasepool {
  582. const char *c = (const char *)sqlite3_value_text(aargv[0]);
  583. NSString *s = [NSString stringWithUTF8String:c];
  584. sqlite3_result_int(context, [s hasPrefix:@"h"]);
  585. }
  586. }
  587. else {
  588. NSLog(@"Unknown formart for StringStartsWithH (%d) %s:%d", sqlite3_value_type(aargv[0]), __FUNCTION__, __LINE__);
  589. sqlite3_result_null(context);
  590. }
  591. }];
  592. int rowCount = 0;
  593. FMResultSet *ars = [adb executeQuery:@"select * from ftest where StringStartsWithH(foo)"];
  594. while ([ars next]) {
  595. rowCount++;
  596. NSLog(@"Does %@ start with 'h'?", [rs stringForColumnIndex:0]);
  597. }
  598. FMDBQuickCheck(rowCount == 2);
  599. }];
  600. @param name Name of function
  601. @param count Maximum number of parameters
  602. @param block The block of code for the function
  603. @see [sqlite3_create_function()](http://sqlite.org/c3ref/create_function.html)
  604. */
  605. - (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(void *context, int argc, void **argv))block;
  606. ///---------------------
  607. /// @name Date formatter
  608. ///---------------------
  609. /** Generate an `NSDateFormatter` that won't be broken by permutations of timezones or locales.
  610. Use this method to generate values to set the dateFormat property.
  611. Example:
  612. myDB.dateFormat = [FMDatabase storeableDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  613. @param format A valid NSDateFormatter format string.
  614. @return A `NSDateFormatter` that can be used for converting dates to strings and vice versa.
  615. @see hasDateFormatter
  616. @see setDateFormat:
  617. @see dateFromString:
  618. @see stringFromDate:
  619. @see storeableDateFormat:
  620. @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.
  621. */
  622. + (NSDateFormatter *)storeableDateFormat:(NSString *)format;
  623. /** Test whether the database has a date formatter assigned.
  624. @return `YES` if there is a date formatter; `NO` if not.
  625. @see hasDateFormatter
  626. @see setDateFormat:
  627. @see dateFromString:
  628. @see stringFromDate:
  629. @see storeableDateFormat:
  630. */
  631. - (BOOL)hasDateFormatter;
  632. /** Set to a date formatter to use string dates with sqlite instead of the default UNIX timestamps.
  633. @param format Set to nil to use UNIX timestamps. Defaults to nil. Should be set using a formatter generated using FMDatabase::storeableDateFormat.
  634. @see hasDateFormatter
  635. @see setDateFormat:
  636. @see dateFromString:
  637. @see stringFromDate:
  638. @see storeableDateFormat:
  639. @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.
  640. */
  641. - (void)setDateFormat:(NSDateFormatter *)format;
  642. /** Convert the supplied NSString to NSDate, using the current database formatter.
  643. @param s `NSString` to convert to `NSDate`.
  644. @return The `NSDate` object; or `nil` if no formatter is set.
  645. @see hasDateFormatter
  646. @see setDateFormat:
  647. @see dateFromString:
  648. @see stringFromDate:
  649. @see storeableDateFormat:
  650. */
  651. - (NSDate *)dateFromString:(NSString *)s;
  652. /** Convert the supplied NSDate to NSString, using the current database formatter.
  653. @param date `NSDate` of date to convert to `NSString`.
  654. @return The `NSString` representation of the date; `nil` if no formatter is set.
  655. @see hasDateFormatter
  656. @see setDateFormat:
  657. @see dateFromString:
  658. @see stringFromDate:
  659. @see storeableDateFormat:
  660. */
  661. - (NSString *)stringFromDate:(NSDate *)date;
  662. @end
  663. /** Objective-C wrapper for `sqlite3_stmt`
  664. 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.
  665. ### See also
  666. - `<FMDatabase>`
  667. - `<FMResultSet>`
  668. - [`sqlite3_stmt`](http://www.sqlite.org/c3ref/stmt.html)
  669. */
  670. @interface FMStatement : NSObject {
  671. void *_statement;
  672. NSString *_query;
  673. long _useCount;
  674. BOOL _inUse;
  675. }
  676. ///-----------------
  677. /// @name Properties
  678. ///-----------------
  679. /** Usage count */
  680. @property (atomic, assign) long useCount;
  681. /** SQL statement */
  682. @property (atomic, retain) NSString *query;
  683. /** SQLite sqlite3_stmt
  684. @see [`sqlite3_stmt`](http://www.sqlite.org/c3ref/stmt.html)
  685. */
  686. @property (atomic, assign) void *statement;
  687. /** Indication of whether the statement is in use */
  688. @property (atomic, assign) BOOL inUse;
  689. ///----------------------------
  690. /// @name Closing and Resetting
  691. ///----------------------------
  692. /** Close statement */
  693. - (void)close;
  694. /** Reset statement */
  695. - (void)reset;
  696. @end
  697. #pragma clang diagnostic pop