FMDatabasePool.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  31. @property (atomic, retain) NSString *path;
  32. @property (atomic, assign) id delegate;
  33. @property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate;
  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*)aPath;
  42. /** Create pool using path.
  43. @param aPath The file path of the database.
  44. @return The `FMDatabasePool` object. `nil` on error.
  45. */
  46. - (instancetype)initWithPath:(NSString*)aPath;
  47. ///------------------------------------------------
  48. /// @name Keeping track of checked in/out databases
  49. ///------------------------------------------------
  50. /** Number of checked-in databases in pool
  51. @returns Number of databases
  52. */
  53. - (NSUInteger)countOfCheckedInDatabases;
  54. /** Number of checked-out databases in pool
  55. @returns Number of databases
  56. */
  57. - (NSUInteger)countOfCheckedOutDatabases;
  58. /** Total number of databases in pool
  59. @returns Number of databases
  60. */
  61. - (NSUInteger)countOfOpenDatabases;
  62. /** Release all databases in pool */
  63. - (void)releaseAllDatabases;
  64. ///------------------------------------------
  65. /// @name Perform database operations in pool
  66. ///------------------------------------------
  67. /** Synchronously perform database operations in pool.
  68. @param block The code to be run on the `FMDatabasePool` pool.
  69. */
  70. - (void)inDatabase:(void (^)(FMDatabase *db))block;
  71. /** Synchronously perform database operations in pool using transaction.
  72. @param block The code to be run on the `FMDatabasePool` pool.
  73. */
  74. - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  75. /** Synchronously perform database operations in pool using deferred transaction.
  76. @param block The code to be run on the `FMDatabasePool` pool.
  77. */
  78. - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  79. #if SQLITE_VERSION_NUMBER >= 3007000
  80. /** Synchronously perform database operations in pool using save point.
  81. @param block The code to be run on the `FMDatabasePool` pool.
  82. @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.
  83. */
  84. - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
  85. #endif
  86. @end
  87. @interface NSObject (FMDatabasePoolDelegate)
  88. - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database;
  89. @end