FMDatabasePool.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // FMDatabasePool.h
  3. // fmdb
  4. //
  5. // Created by August Mueller on 6/22/11.
  6. // Copyright 2011 Flying Meat Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. @class FMDatabase;
  11. /** Pool of @c FMDatabase objects.
  12. See also
  13. - @c FMDatabaseQueue
  14. - @c FMDatabase
  15. @warning Before using @c FMDatabasePool , please consider using @c FMDatabaseQueue instead.
  16. If you really really really know what you're doing and @c FMDatabasePool is what
  17. you really really need (ie, you're using a read only database), OK you can use
  18. it. But just be careful not to deadlock!
  19. For an example on deadlocking, search for:
  20. `ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS_OTHERWISE_YOULL_DEADLOCK_USE_FMDATABASEQUEUE_INSTEAD`
  21. in the main.m file.
  22. */
  23. @interface FMDatabasePool : NSObject
  24. /** Database path */
  25. @property (atomic, copy, nullable) NSString *path;
  26. /** Delegate object */
  27. @property (atomic, unsafe_unretained, nullable) id delegate;
  28. /** Maximum number of databases to create */
  29. @property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate;
  30. /** Open flags */
  31. @property (atomic, readonly) int openFlags;
  32. /** Custom virtual file system name */
  33. @property (atomic, copy, nullable) NSString *vfsName;
  34. ///---------------------
  35. /// @name Initialization
  36. ///---------------------
  37. /** Create pool using path.
  38. @param aPath The file path of the database.
  39. @return The @c FMDatabasePool object. @c nil on error.
  40. */
  41. + (instancetype)databasePoolWithPath:(NSString * _Nullable)aPath;
  42. /** Create pool using file URL.
  43. @param url The file @c NSURL of the database.
  44. @return The @c FMDatabasePool object. @c nil on error.
  45. */
  46. + (instancetype)databasePoolWithURL:(NSURL * _Nullable)url;
  47. /** Create pool using path and specified flags
  48. @param aPath The file path of the database.
  49. @param openFlags Flags passed to the openWithFlags method of the database.
  50. @return The @c FMDatabasePool object. @c nil on error.
  51. */
  52. + (instancetype)databasePoolWithPath:(NSString * _Nullable)aPath flags:(int)openFlags;
  53. /** Create pool using file URL and specified flags
  54. @param url The file @c NSURL of the database.
  55. @param openFlags Flags passed to the openWithFlags method of the database.
  56. @return The @c FMDatabasePool object. @c nil on error.
  57. */
  58. + (instancetype)databasePoolWithURL:(NSURL * _Nullable)url flags:(int)openFlags;
  59. /** Create pool using path.
  60. @param aPath The file path of the database.
  61. @return The @c FMDatabasePool object. @c nil on error.
  62. */
  63. - (instancetype)initWithPath:(NSString * _Nullable)aPath;
  64. /** Create pool using file URL.
  65. @param url The file `NSURL of the database.
  66. @return The @c FMDatabasePool object. @c nil on error.
  67. */
  68. - (instancetype)initWithURL:(NSURL * _Nullable)url;
  69. /** Create pool using path and specified flags.
  70. @param aPath The file path of the database.
  71. @param openFlags Flags passed to the openWithFlags method of the database
  72. @return The @c FMDatabasePool object. @c nil on error.
  73. */
  74. - (instancetype)initWithPath:(NSString * _Nullable)aPath flags:(int)openFlags;
  75. /** Create pool using file URL and specified flags.
  76. @param url The file @c NSURL of the database.
  77. @param openFlags Flags passed to the openWithFlags method of the database
  78. @return The @c FMDatabasePool object. @c nil on error.
  79. */
  80. - (instancetype)initWithURL:(NSURL * _Nullable)url flags:(int)openFlags;
  81. /** Create pool using path and specified flags.
  82. @param aPath The file path of the database.
  83. @param openFlags Flags passed to the openWithFlags method of the database
  84. @param vfsName The name of a custom virtual file system
  85. @return The @c FMDatabasePool object. @c nil on error.
  86. */
  87. - (instancetype)initWithPath:(NSString * _Nullable)aPath flags:(int)openFlags vfs:(NSString * _Nullable)vfsName;
  88. /** Create pool using file URL and specified flags.
  89. @param url The file @c NSURL of the database.
  90. @param openFlags Flags passed to the openWithFlags method of the database
  91. @param vfsName The name of a custom virtual file system
  92. @return The @c FMDatabasePool object. @c nil on error.
  93. */
  94. - (instancetype)initWithURL:(NSURL * _Nullable)url flags:(int)openFlags vfs:(NSString * _Nullable)vfsName;
  95. /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  96. Subclasses can override this method to return specified Class of 'FMDatabase' subclass.
  97. @return The Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  98. */
  99. + (Class)databaseClass;
  100. ///------------------------------------------------
  101. /// @name Keeping track of checked in/out databases
  102. ///------------------------------------------------
  103. /** Number of checked-in databases in pool
  104. */
  105. @property (nonatomic, readonly) NSUInteger countOfCheckedInDatabases;
  106. /** Number of checked-out databases in pool
  107. */
  108. @property (nonatomic, readonly) NSUInteger countOfCheckedOutDatabases;
  109. /** Total number of databases in pool
  110. */
  111. @property (nonatomic, readonly) NSUInteger countOfOpenDatabases;
  112. /** Release all databases in pool */
  113. - (void)releaseAllDatabases;
  114. ///------------------------------------------
  115. /// @name Perform database operations in pool
  116. ///------------------------------------------
  117. /** Synchronously perform database operations in pool.
  118. @param block The code to be run on the @c FMDatabasePool pool.
  119. */
  120. - (void)inDatabase:(__attribute__((noescape)) void (^)(FMDatabase *db))block;
  121. /** Synchronously perform database operations in pool using transaction.
  122. @param block The code to be run on the @c FMDatabasePool pool.
  123. @warning Unlike SQLite's `BEGIN TRANSACTION`, this method currently performs
  124. an exclusive transaction, not a deferred transaction. This behavior
  125. is likely to change in future versions of FMDB, whereby this method
  126. will likely eventually adopt standard SQLite behavior and perform
  127. deferred transactions. If you really need exclusive tranaction, it is
  128. recommended that you use `inExclusiveTransaction`, instead, not only
  129. to make your intent explicit, but also to future-proof your code.
  130. */
  131. - (void)inTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  132. /** Synchronously perform database operations in pool using exclusive transaction.
  133. @param block The code to be run on the @c FMDatabasePool pool.
  134. */
  135. - (void)inExclusiveTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  136. /** Synchronously perform database operations in pool using deferred transaction.
  137. @param block The code to be run on the @c FMDatabasePool pool.
  138. */
  139. - (void)inDeferredTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  140. /** Synchronously perform database operations on queue, using immediate transactions.
  141. @param block The code to be run on the queue of @c FMDatabaseQueue
  142. */
  143. - (void)inImmediateTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  144. /** Synchronously perform database operations in pool using save point.
  145. @param block The code to be run on the @c FMDatabasePool pool.
  146. @return @c NSError object if error; @c nil if successful.
  147. @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 @c startSavePointWithName:error: instead.
  148. */
  149. - (NSError * _Nullable)inSavePoint:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  150. @end
  151. /** FMDatabasePool delegate category
  152. This is a category that defines the protocol for the FMDatabasePool delegate
  153. */
  154. @interface NSObject (FMDatabasePoolDelegate)
  155. /** Asks the delegate whether database should be added to the pool.
  156. @param pool The @c FMDatabasePool object.
  157. @param database The @c FMDatabase object.
  158. @return @c YES if it should add database to pool; @c NO if not.
  159. */
  160. - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database;
  161. /** Tells the delegate that database was added to the pool.
  162. @param pool The @c FMDatabasePool object.
  163. @param database The @c FMDatabase object.
  164. */
  165. - (void)databasePool:(FMDatabasePool*)pool didAddDatabase:(FMDatabase*)database;
  166. @end
  167. NS_ASSUME_NONNULL_END