FMDatabasePool.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /*
  11. ***README OR SUFFER***
  12. Before using FMDatabasePool, please consider using FMDatabaseQueue instead.
  13. If you really really really know what you're doing and FMDatabasePool is what
  14. you really really need (ie, you're using a read only database), OK you can use
  15. it. But just be careful not to deadlock!
  16. For an example on deadlocking, search for:
  17. ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS_OTHERWISE_YOULL_DEADLOCK_USE_FMDATABASEQUEUE_INSTEAD
  18. in the main.m file.
  19. */
  20. @class FMDatabase;
  21. @interface FMDatabasePool : NSObject {
  22. NSString *_path;
  23. dispatch_queue_t _lockQueue;
  24. NSMutableArray *_databaseInPool;
  25. NSMutableArray *_databaseOutPool;
  26. __unsafe_unretained id _delegate;
  27. NSUInteger _maximumNumberOfDatabasesToCreate;
  28. }
  29. @property (atomic, retain) NSString *path;
  30. @property (atomic, assign) id delegate;
  31. @property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate;
  32. + (id)databasePoolWithPath:(NSString*)aPath;
  33. - (id)initWithPath:(NSString*)aPath;
  34. - (NSUInteger)countOfCheckedInDatabases;
  35. - (NSUInteger)countOfCheckedOutDatabases;
  36. - (NSUInteger)countOfOpenDatabases;
  37. - (void)releaseAllDatabases;
  38. - (void)inDatabase:(void (^)(FMDatabase *db))block;
  39. - (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  40. - (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block;
  41. #if SQLITE_VERSION_NUMBER >= 3007000
  42. // NOTE: you can not nest these, since calling it will pull another database out of the pool and you'll get a deadlock.
  43. // If you need to nest, use FMDatabase's startSavePointWithName:error: instead.
  44. - (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block;
  45. #endif
  46. @end
  47. @interface NSObject (FMDatabasePoolDelegate)
  48. - (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database;
  49. @end