FMDatabase.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #import <Foundation/Foundation.h>
  2. #import "sqlite3.h"
  3. #import "FMResultSet.h"
  4. #import "FMDatabasePool.h"
  5. #if ! __has_feature(objc_arc)
  6. #define FMDBAutorelease(__v) ([__v autorelease]);
  7. #define FMDBReturnAutoreleased FMDBAutorelease
  8. #define FMDBRetain(__v) ([__v retain]);
  9. #define FMDBReturnRetained FMDBRetain
  10. #define FMDBRelease(__v) ([__v release]);
  11. #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
  12. #else
  13. // -fobjc-arc
  14. #define FMDBAutorelease(__v)
  15. #define FMDBReturnAutoreleased(__v) (__v)
  16. #define FMDBRetain(__v)
  17. #define FMDBReturnRetained(__v) (__v)
  18. #define FMDBRelease(__v)
  19. #if TARGET_OS_IPHONE
  20. // Compiling for iOS
  21. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
  22. // iOS 6.0 or later
  23. #define FMDBDispatchQueueRelease(__v)
  24. #else
  25. // iOS 5.X or earlier
  26. #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
  27. #endif
  28. #else
  29. // Compiling for Mac OS X
  30. #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
  31. // Mac OS X 10.8 or later
  32. #define FMDBDispatchQueueRelease(__v)
  33. #else
  34. // Mac OS X 10.7 or earlier
  35. #define FMDBDispatchQueueRelease(__v) (dispatch_release(__v));
  36. #endif
  37. #endif
  38. #endif
  39. @interface FMDatabase : NSObject {
  40. sqlite3* _db;
  41. NSString* _databasePath;
  42. BOOL _logsErrors;
  43. BOOL _crashOnErrors;
  44. BOOL _traceExecution;
  45. BOOL _checkedOut;
  46. BOOL _shouldCacheStatements;
  47. BOOL _isExecutingStatement;
  48. BOOL _inTransaction;
  49. int _busyRetryTimeout;
  50. NSMutableDictionary *_cachedStatements;
  51. NSMutableSet *_openResultSets;
  52. NSMutableSet *_openFunctions;
  53. NSDateFormatter *_dateFormat;
  54. }
  55. @property (atomic, assign) BOOL traceExecution;
  56. @property (atomic, assign) BOOL checkedOut;
  57. @property (atomic, assign) int busyRetryTimeout;
  58. @property (atomic, assign) BOOL crashOnErrors;
  59. @property (atomic, assign) BOOL logsErrors;
  60. @property (atomic, retain) NSMutableDictionary *cachedStatements;
  61. + (id)databaseWithPath:(NSString*)inPath;
  62. - (id)initWithPath:(NSString*)inPath;
  63. - (BOOL)open;
  64. #if SQLITE_VERSION_NUMBER >= 3005000
  65. - (BOOL)openWithFlags:(int)flags;
  66. #endif
  67. - (BOOL)close;
  68. - (BOOL)goodConnection;
  69. - (void)clearCachedStatements;
  70. - (void)closeOpenResultSets;
  71. - (BOOL)hasOpenResultSets;
  72. // encryption methods. You need to have purchased the sqlite encryption extensions for these to work.
  73. - (BOOL)setKey:(NSString*)key;
  74. - (BOOL)rekey:(NSString*)key;
  75. - (BOOL)setKeyWithData:(NSData *)keyData;
  76. - (BOOL)rekeyWithData:(NSData *)keyData;
  77. - (NSString *)databasePath;
  78. - (NSString*)lastErrorMessage;
  79. - (int)lastErrorCode;
  80. - (BOOL)hadError;
  81. - (NSError*)lastError;
  82. - (sqlite_int64)lastInsertRowId;
  83. - (sqlite3*)sqliteHandle;
  84. - (BOOL)update:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ...;
  85. - (BOOL)executeUpdate:(NSString*)sql, ...;
  86. - (BOOL)executeUpdateWithFormat:(NSString *)format, ...;
  87. - (BOOL)executeUpdate:(NSString*)sql withArgumentsInArray:(NSArray *)arguments;
  88. - (BOOL)executeUpdate:(NSString*)sql withParameterDictionary:(NSDictionary *)arguments;
  89. - (FMResultSet *)executeQuery:(NSString*)sql, ...;
  90. - (FMResultSet *)executeQueryWithFormat:(NSString*)format, ...;
  91. - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments;
  92. - (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDictionary *)arguments;
  93. - (BOOL)rollback;
  94. - (BOOL)commit;
  95. - (BOOL)beginTransaction;
  96. - (BOOL)beginDeferredTransaction;
  97. - (BOOL)inTransaction;
  98. - (BOOL)shouldCacheStatements;
  99. - (void)setShouldCacheStatements:(BOOL)value;
  100. #if SQLITE_VERSION_NUMBER >= 3007000
  101. - (BOOL)startSavePointWithName:(NSString*)name error:(NSError**)outErr;
  102. - (BOOL)releaseSavePointWithName:(NSString*)name error:(NSError**)outErr;
  103. - (BOOL)rollbackToSavePointWithName:(NSString*)name error:(NSError**)outErr;
  104. - (NSError*)inSavePoint:(void (^)(BOOL *rollback))block;
  105. #endif
  106. + (BOOL)isSQLiteThreadSafe;
  107. + (NSString*)sqliteLibVersion;
  108. - (int)changes;
  109. - (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(sqlite3_context *context, int argc, sqlite3_value **argv))block;
  110. /** Generate an NSDateFormat that won't be broken by timezone or locale changes.
  111. Use this method to generate values to set the dateFormat property.
  112. @param dateFormat A valid NSDateFormatter format string.
  113. Example:
  114. myDB.dateFormat = [FMDatabase storeableDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  115. 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.
  116. */
  117. + (NSDateFormatter *)storeableDateFormat:(NSString *)format;
  118. /** Test whether the database has a date formatter assigned.
  119. */
  120. - (BOOL)hasDateFormatter;
  121. /** Set to a date formatter to use string dates with sqlite instead of the default UNIX timestamps.
  122. Set to nil to use UNIX timestamps.
  123. Defaults to nil.
  124. Should be set using a formatter generated using FMDatabase::storeableDateFormat.
  125. 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.
  126. */
  127. - (void)setDateFormat:(NSDateFormatter *)format;
  128. /** Convert the supplied NSString to NSDate, using the current database formatter.
  129. Returns nil if no formatter is set.
  130. */
  131. - (NSDate *)dateFromString:(NSString *)s;
  132. /** Convert the supplied NSDate to NSString, using the current database formatter.
  133. Returns nil if no formatter is set.
  134. */
  135. - (NSString *)stringFromDate:(NSDate *)date;
  136. @end
  137. @interface FMStatement : NSObject {
  138. sqlite3_stmt *_statement;
  139. NSString *_query;
  140. long _useCount;
  141. }
  142. @property (atomic, assign) long useCount;
  143. @property (atomic, retain) NSString *query;
  144. @property (atomic, assign) sqlite3_stmt *statement;
  145. - (void)close;
  146. - (void)reset;
  147. @end