FMDatabasePool.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #import "sqlite3.h"
  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. NSString *_path;
  25. dispatch_queue_t _lockQueue;
  26. NSMutableArray *_databaseInPool;
  27. NSMutableArray *_databaseOutPool;
  28. __unsafe_unretained id _delegate;
  29. NSUInteger _maximumNumberOfDatabasesToCreate;
  30. int _openFlags;
  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. ///---------------------
  41. /// @name Initialization
  42. ///---------------------
  43. /** Create pool using path.
  44. @param aPath The file path of the database.
  45. @return The `FMDatabasePool` object. `nil` on error.
  46. */
  47. + (instancetype)databasePoolWithPath:(NSString*)aPath;
  48. /** Create pool using path and specified flags
  49. @param aPath The file path of the database.
  50. @param openFlags Flags passed to the openWithFlags method of the database
  51. @return The `FMDatabasePool` object. `nil` on error.
  52. */
  53. + (instancetype)databasePoolWithPath:(NSString*)aPath flags:(int)openFlags;
  54. /** Create pool using path.
  55. @param aPath The file path of the database.
  56. @return The `FMDatabasePool` object. `nil` on error.
  57. */
  58. - (instancetype)initWithPath:(NSString*)aPath;
  59. /** Create pool using path and specified flags.
  60. @param aPath The file path of the database.
  61. @param openFlags Flags passed to the openWithFlags method of the database
  62. @return The `FMDatabasePool` object. `nil` on error.
  63. */
  64. - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
  65. ///------------------------------------------------
  66. /// @name Keeping track of checked in/out databases
  67. ///------------------------------------------------
  68. /** Number of checked-in databases in pool
  69. @returns Number of databases
  70. */
  71. - (NSUInteger)countOfCheckedInDatabases;
  72. /** Number of checked-out databases in pool
  73. @returns Number of databases
  74. */
  75. - (NSUInteger)countOfCheckedOutDatabases;
  76. /** Total number of databases in pool
  77. @returns Number of databases
  78. */
  79. - (NSUInteger)countOfOpenDatabases;
  80. /** Release all databases in pool */
  81. - (void)releaseAllDatabases;
  82. ///------------------------------------------
  83. /// @name Perform database operations in pool
  84. ///------------------------------------------
  85. /** Synchronously perform database operations in pool.
  86. @param block The code to be run on the `FMDatabasePool` pool.
  87. */
  88. - (void)inDatabase:(void (^)(FMDatabase *db))block;
  89. /** Synchronously perform database operations in pool using transaction.
  90. @param block The code to be run on the `FMDatabasePool` pool.
  91. */
  92. - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  93. /** Synchronously perform database operations in pool using deferred transaction.
  94. @param block The code to be run on the `FMDatabasePool` pool.
  95. */
  96. - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  97. #if SQLITE_VERSION_NUMBER >= 3007000
  98. /** Synchronously perform database operations in pool using save point.
  99. @param block The code to be run on the `FMDatabasePool` pool.
  100. @return `NSError` object if error; `nil` if successful.
  101. @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.
  102. */
  103. - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
  104. #endif
  105. @end
  106. /** FMDatabasePool delegate category
  107. This is a category that defines the protocol for the FMDatabasePool delegate
  108. */
  109. @interface NSObject (FMDatabasePoolDelegate)
  110. /** Asks the delegate whether database should be added to the pool.
  111. @param pool The `FMDatabasePool` object.
  112. @param database The `FMDatabase` object.
  113. @return `YES` if it should add database to pool; `NO` if not.
  114. */
  115. - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database;
  116. /** Tells the delegate that database was added to the pool.
  117. @param pool The `FMDatabasePool` object.
  118. @param database The `FMDatabase` object.
  119. */
  120. - (void)databasePool:(FMDatabasePool*)pool didAddDatabase:(FMDatabase*)database;
  121. @end