FMDatabasePool.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 `<FMDatabase>` objects.
  12. ### See also
  13. - `<FMDatabaseQueue>`
  14. - `<FMDatabase>`
  15. @warning Before using `FMDatabasePool`, please consider using `<FMDatabaseQueue>` instead.
  16. If you really really really know what you're doing and `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, assign, 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 `FMDatabasePool` object. `nil` on error.
  40. */
  41. + (instancetype)databasePoolWithPath:(NSString * _Nullable)aPath;
  42. /** Create pool using file URL.
  43. @param url The file `NSURL` of the database.
  44. @return The `FMDatabasePool` object. `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 `FMDatabasePool` object. `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 `NSURL` of the database.
  55. @param openFlags Flags passed to the openWithFlags method of the database.
  56. @return The `FMDatabasePool` object. `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 `FMDatabasePool` object. `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 `FMDatabasePool` object. `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 `FMDatabasePool` object. `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 `NSURL` of the database.
  77. @param openFlags Flags passed to the openWithFlags method of the database
  78. @return The `FMDatabasePool` object. `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 `FMDatabasePool` object. `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 `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 `FMDatabasePool` object. `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 `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 `FMDatabasePool` pool.
  123. */
  124. - (void)inTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  125. /** Synchronously perform database operations in pool using deferred transaction.
  126. @param block The code to be run on the `FMDatabasePool` pool.
  127. */
  128. - (void)inDeferredTransaction:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  129. /** Synchronously perform database operations in pool using save point.
  130. @param block The code to be run on the `FMDatabasePool` pool.
  131. @return `NSError` object if error; `nil` if successful.
  132. @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.
  133. */
  134. - (NSError * _Nullable)inSavePoint:(__attribute__((noescape)) void (^)(FMDatabase *db, BOOL *rollback))block;
  135. @end
  136. /** FMDatabasePool delegate category
  137. This is a category that defines the protocol for the FMDatabasePool delegate
  138. */
  139. @interface NSObject (FMDatabasePoolDelegate)
  140. /** Asks the delegate whether database should be added to the pool.
  141. @param pool The `FMDatabasePool` object.
  142. @param database The `FMDatabase` object.
  143. @return `YES` if it should add database to pool; `NO` if not.
  144. */
  145. - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database;
  146. /** Tells the delegate that database was added to the pool.
  147. @param pool The `FMDatabasePool` object.
  148. @param database The `FMDatabase` object.
  149. */
  150. - (void)databasePool:(FMDatabasePool*)pool didAddDatabase:(FMDatabase*)database;
  151. @end
  152. NS_ASSUME_NONNULL_END