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