FMDatabasePool.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. @class FMDatabase;
  10. /** Pool of `<FMDatabase>` objects.
  11. ### See also
  12. - `<FMDatabaseQueue>`
  13. - `<FMDatabase>`
  14. @warning Before using `FMDatabasePool`, please consider using `<FMDatabaseQueue>` instead.
  15. If you really really really know what you're doing and `FMDatabasePool` is what
  16. you really really need (ie, you're using a read only database), OK you can use
  17. it. But just be careful not to deadlock!
  18. For an example on deadlocking, search for:
  19. `ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS_OTHERWISE_YOULL_DEADLOCK_USE_FMDATABASEQUEUE_INSTEAD`
  20. in the main.m file.
  21. */
  22. @interface FMDatabasePool : NSObject {
  23. NSString *_path;
  24. dispatch_queue_t _lockQueue;
  25. NSMutableArray *_databaseInPool;
  26. NSMutableArray *_databaseOutPool;
  27. __unsafe_unretained id _delegate;
  28. NSUInteger _maximumNumberOfDatabasesToCreate;
  29. int _openFlags;
  30. NSString *_vfsName;
  31. }
  32. /** Database path */
  33. @property (atomic, retain) NSString *path;
  34. /** Delegate object */
  35. @property (atomic, assign) id delegate;
  36. /** Maximum number of databases to create */
  37. @property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate;
  38. /** Open flags */
  39. @property (atomic, readonly) int openFlags;
  40. /** Custom virtual file system name */
  41. @property (atomic, copy) NSString *vfsName;
  42. ///---------------------
  43. /// @name Initialization
  44. ///---------------------
  45. /** Create pool using path.
  46. @param aPath The file path of the database.
  47. @return The `FMDatabasePool` object. `nil` on error.
  48. */
  49. + (instancetype)databasePoolWithPath:(NSString*)aPath;
  50. /** Create pool using path and specified flags
  51. @param aPath The file path of the database.
  52. @param openFlags Flags passed to the openWithFlags method of the database
  53. @return The `FMDatabasePool` object. `nil` on error.
  54. */
  55. + (instancetype)databasePoolWithPath:(NSString*)aPath flags:(int)openFlags;
  56. /** Create pool using path.
  57. @param aPath The file path of the database.
  58. @return The `FMDatabasePool` object. `nil` on error.
  59. */
  60. - (instancetype)initWithPath:(NSString*)aPath;
  61. /** Create pool using path and specified flags.
  62. @param aPath The file path of the database.
  63. @param openFlags Flags passed to the openWithFlags method of the database
  64. @return The `FMDatabasePool` object. `nil` on error.
  65. */
  66. - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
  67. /** Create pool using path and specified flags.
  68. @param aPath The file path of the database.
  69. @param openFlags Flags passed to the openWithFlags method of the database
  70. @param vfsName The name of a custom virtual file system
  71. @return The `FMDatabasePool` object. `nil` on error.
  72. */
  73. - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags vfs:(NSString *)vfsName;
  74. /** Returns the Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  75. Subclasses can override this method to return specified Class of 'FMDatabase' subclass.
  76. @return The Class of 'FMDatabase' subclass, that will be used to instantiate database object.
  77. */
  78. + (Class)databaseClass;
  79. ///------------------------------------------------
  80. /// @name Keeping track of checked in/out databases
  81. ///------------------------------------------------
  82. /** Number of checked-in databases in pool
  83. @returns Number of databases
  84. */
  85. - (NSUInteger)countOfCheckedInDatabases;
  86. /** Number of checked-out databases in pool
  87. @returns Number of databases
  88. */
  89. - (NSUInteger)countOfCheckedOutDatabases;
  90. /** Total number of databases in pool
  91. @returns Number of databases
  92. */
  93. - (NSUInteger)countOfOpenDatabases;
  94. /** Release all databases in pool */
  95. - (void)releaseAllDatabases;
  96. ///------------------------------------------
  97. /// @name Perform database operations in pool
  98. ///------------------------------------------
  99. /** Synchronously perform database operations in pool.
  100. @param block The code to be run on the `FMDatabasePool` pool.
  101. */
  102. - (void)inDatabase:(void (^)(FMDatabase *db))block;
  103. /** Synchronously perform database operations in pool using transaction.
  104. @param block The code to be run on the `FMDatabasePool` pool.
  105. */
  106. - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  107. /** Synchronously perform database operations in pool using deferred transaction.
  108. @param block The code to be run on the `FMDatabasePool` pool.
  109. */
  110. - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  111. /** Synchronously perform database operations in pool using save point.
  112. @param block The code to be run on the `FMDatabasePool` pool.
  113. @return `NSError` object if error; `nil` if successful.
  114. @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.
  115. */
  116. - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
  117. @end
  118. /** FMDatabasePool delegate category
  119. This is a category that defines the protocol for the FMDatabasePool delegate
  120. */
  121. @interface NSObject (FMDatabasePoolDelegate)
  122. /** Asks the delegate whether database should be added to the pool.
  123. @param pool The `FMDatabasePool` object.
  124. @param database The `FMDatabase` object.
  125. @return `YES` if it should add database to pool; `NO` if not.
  126. */
  127. - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database;
  128. /** Tells the delegate that database was added to the pool.
  129. @param pool The `FMDatabasePool` object.
  130. @param database The `FMDatabase` object.
  131. */
  132. - (void)databasePool:(FMDatabasePool*)pool didAddDatabase:(FMDatabase*)database;
  133. @end