FMDatabasePool.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. @property (atomic, retain) NSString *path;
  33. @property (atomic, assign) id delegate;
  34. @property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate;
  35. @property (atomic, readonly) int openFlags;
  36. ///---------------------
  37. /// @name Initialization
  38. ///---------------------
  39. /** Create pool using path.
  40. @param aPath The file path of the database.
  41. @return The `FMDatabasePool` object. `nil` on error.
  42. */
  43. + (instancetype)databasePoolWithPath:(NSString*)aPath;
  44. /** Create pool using path and specified flags
  45. @param aPath The file path of the database.
  46. @param openFlags Flags passed to the openWithFlags method of the database
  47. @return The `FMDatabasePool` object. `nil` on error.
  48. */
  49. + (instancetype)databasePoolWithPath:(NSString*)aPath flags:(int)openFlags;
  50. /** Create pool using path.
  51. @param aPath The file path of the database.
  52. @return The `FMDatabasePool` object. `nil` on error.
  53. */
  54. - (instancetype)initWithPath:(NSString*)aPath;
  55. /** Create pool using path and specified flags.
  56. @param aPath The file path of the database.
  57. @param openFlags Flags passed to the openWithFlags method of the database
  58. @return The `FMDatabasePool` object. `nil` on error.
  59. */
  60. - (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags;
  61. ///------------------------------------------------
  62. /// @name Keeping track of checked in/out databases
  63. ///------------------------------------------------
  64. /** Number of checked-in databases in pool
  65. @returns Number of databases
  66. */
  67. - (NSUInteger)countOfCheckedInDatabases;
  68. /** Number of checked-out databases in pool
  69. @returns Number of databases
  70. */
  71. - (NSUInteger)countOfCheckedOutDatabases;
  72. /** Total number of databases in pool
  73. @returns Number of databases
  74. */
  75. - (NSUInteger)countOfOpenDatabases;
  76. /** Release all databases in pool */
  77. - (void)releaseAllDatabases;
  78. ///------------------------------------------
  79. /// @name Perform database operations in pool
  80. ///------------------------------------------
  81. /** Synchronously perform database operations in pool.
  82. @param block The code to be run on the `FMDatabasePool` pool.
  83. */
  84. - (void)inDatabase:(void (^)(FMDatabase *db))block;
  85. /** Synchronously perform database operations in pool using transaction.
  86. @param block The code to be run on the `FMDatabasePool` pool.
  87. */
  88. - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  89. /** Synchronously perform database operations in pool using deferred transaction.
  90. @param block The code to be run on the `FMDatabasePool` pool.
  91. */
  92. - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  93. #if SQLITE_VERSION_NUMBER >= 3007000
  94. /** Synchronously perform database operations in pool using save point.
  95. @param block The code to be run on the `FMDatabasePool` pool.
  96. @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.
  97. */
  98. - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
  99. #endif
  100. @end
  101. @interface NSObject (FMDatabasePoolDelegate)
  102. /** Asks the delegate whether database should be added to the pool. */
  103. - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database;
  104. /** Tells the delegate that database was added to the pool. */
  105. - (void)databasePool:(FMDatabasePool*)pool didAddDatabase:(FMDatabase*)database;
  106. @end