commit | author | age
|
6e1425
|
1 |
// |
H |
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 |
|
|
9 |
#import <Foundation/Foundation.h> |
|
10 |
|
|
11 |
@class FMDatabase; |
|
12 |
|
|
13 |
/** Pool of `<FMDatabase>` objects. |
|
14 |
|
|
15 |
### See also |
|
16 |
|
|
17 |
- `<FMDatabaseQueue>` |
|
18 |
- `<FMDatabase>` |
|
19 |
|
|
20 |
@warning Before using `FMDatabasePool`, please consider using `<FMDatabaseQueue>` instead. |
|
21 |
|
|
22 |
If you really really really know what you're doing and `FMDatabasePool` is what |
|
23 |
you really really need (ie, you're using a read only database), OK you can use |
|
24 |
it. But just be careful not to deadlock! |
|
25 |
|
|
26 |
For an example on deadlocking, search for: |
|
27 |
`ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS_OTHERWISE_YOULL_DEADLOCK_USE_FMDATABASEQUEUE_INSTEAD` |
|
28 |
in the main.m file. |
|
29 |
*/ |
|
30 |
|
|
31 |
@interface FMDatabasePool : NSObject { |
|
32 |
NSString *_path; |
|
33 |
|
|
34 |
dispatch_queue_t _lockQueue; |
|
35 |
|
|
36 |
NSMutableArray *_databaseInPool; |
|
37 |
NSMutableArray *_databaseOutPool; |
|
38 |
|
|
39 |
__unsafe_unretained id _delegate; |
|
40 |
|
|
41 |
NSUInteger _maximumNumberOfDatabasesToCreate; |
|
42 |
int _openFlags; |
|
43 |
} |
|
44 |
|
|
45 |
/** Database path */ |
|
46 |
|
|
47 |
@property (atomic, retain) NSString *path; |
|
48 |
|
|
49 |
/** Delegate object */ |
|
50 |
|
|
51 |
@property (atomic, assign) id delegate; |
|
52 |
|
|
53 |
/** Maximum number of databases to create */ |
|
54 |
|
|
55 |
@property (atomic, assign) NSUInteger maximumNumberOfDatabasesToCreate; |
|
56 |
|
|
57 |
/** Open flags */ |
|
58 |
|
|
59 |
@property (atomic, readonly) int openFlags; |
|
60 |
|
|
61 |
|
|
62 |
///--------------------- |
|
63 |
/// @name Initialization |
|
64 |
///--------------------- |
|
65 |
|
|
66 |
/** Create pool using path. |
|
67 |
|
|
68 |
@param aPath The file path of the database. |
|
69 |
|
|
70 |
@return The `FMDatabasePool` object. `nil` on error. |
|
71 |
*/ |
|
72 |
|
|
73 |
+ (instancetype)databasePoolWithPath:(NSString*)aPath; |
|
74 |
|
|
75 |
/** Create pool using path and specified flags |
|
76 |
|
|
77 |
@param aPath The file path of the database. |
|
78 |
@param openFlags Flags passed to the openWithFlags method of the database |
|
79 |
|
|
80 |
@return The `FMDatabasePool` object. `nil` on error. |
|
81 |
*/ |
|
82 |
|
|
83 |
+ (instancetype)databasePoolWithPath:(NSString*)aPath flags:(int)openFlags; |
|
84 |
|
|
85 |
/** Create pool using path. |
|
86 |
|
|
87 |
@param aPath The file path of the database. |
|
88 |
|
|
89 |
@return The `FMDatabasePool` object. `nil` on error. |
|
90 |
*/ |
|
91 |
|
|
92 |
- (instancetype)initWithPath:(NSString*)aPath; |
|
93 |
|
|
94 |
/** Create pool using path and specified flags. |
|
95 |
|
|
96 |
@param aPath The file path of the database. |
|
97 |
@param openFlags Flags passed to the openWithFlags method of the database |
|
98 |
|
|
99 |
@return The `FMDatabasePool` object. `nil` on error. |
|
100 |
*/ |
|
101 |
|
|
102 |
- (instancetype)initWithPath:(NSString*)aPath flags:(int)openFlags; |
|
103 |
|
|
104 |
///------------------------------------------------ |
|
105 |
/// @name Keeping track of checked in/out databases |
|
106 |
///------------------------------------------------ |
|
107 |
|
|
108 |
/** Number of checked-in databases in pool |
|
109 |
|
|
110 |
@returns Number of databases |
|
111 |
*/ |
|
112 |
|
|
113 |
- (NSUInteger)countOfCheckedInDatabases; |
|
114 |
|
|
115 |
/** Number of checked-out databases in pool |
|
116 |
|
|
117 |
@returns Number of databases |
|
118 |
*/ |
|
119 |
|
|
120 |
- (NSUInteger)countOfCheckedOutDatabases; |
|
121 |
|
|
122 |
/** Total number of databases in pool |
|
123 |
|
|
124 |
@returns Number of databases |
|
125 |
*/ |
|
126 |
|
|
127 |
- (NSUInteger)countOfOpenDatabases; |
|
128 |
|
|
129 |
/** Release all databases in pool */ |
|
130 |
|
|
131 |
- (void)releaseAllDatabases; |
|
132 |
|
|
133 |
///------------------------------------------ |
|
134 |
/// @name Perform database operations in pool |
|
135 |
///------------------------------------------ |
|
136 |
|
|
137 |
/** Synchronously perform database operations in pool. |
|
138 |
|
|
139 |
@param block The code to be run on the `FMDatabasePool` pool. |
|
140 |
*/ |
|
141 |
|
|
142 |
- (void)inDatabase:(void (^)(FMDatabase *db))block; |
|
143 |
|
|
144 |
/** Synchronously perform database operations in pool using transaction. |
|
145 |
|
|
146 |
@param block The code to be run on the `FMDatabasePool` pool. |
|
147 |
*/ |
|
148 |
|
|
149 |
- (void)inTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block; |
|
150 |
|
|
151 |
/** Synchronously perform database operations in pool using deferred transaction. |
|
152 |
|
|
153 |
@param block The code to be run on the `FMDatabasePool` pool. |
|
154 |
*/ |
|
155 |
|
|
156 |
- (void)inDeferredTransaction:(void (^)(FMDatabase *db, BOOL *rollback))block; |
|
157 |
|
|
158 |
/** Synchronously perform database operations in pool using save point. |
|
159 |
|
|
160 |
@param block The code to be run on the `FMDatabasePool` pool. |
|
161 |
|
|
162 |
@return `NSError` object if error; `nil` if successful. |
|
163 |
|
|
164 |
@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. |
|
165 |
*/ |
|
166 |
|
|
167 |
- (NSError*)inSavePoint:(void (^)(FMDatabase *db, BOOL *rollback))block; |
|
168 |
|
|
169 |
@end |
|
170 |
|
|
171 |
|
|
172 |
/** FMDatabasePool delegate category |
|
173 |
|
|
174 |
This is a category that defines the protocol for the FMDatabasePool delegate |
|
175 |
*/ |
|
176 |
|
|
177 |
@interface NSObject (FMDatabasePoolDelegate) |
|
178 |
|
|
179 |
/** Asks the delegate whether database should be added to the pool. |
|
180 |
|
|
181 |
@param pool The `FMDatabasePool` object. |
|
182 |
@param database The `FMDatabase` object. |
|
183 |
|
|
184 |
@return `YES` if it should add database to pool; `NO` if not. |
|
185 |
|
|
186 |
*/ |
|
187 |
|
|
188 |
- (BOOL)databasePool:(FMDatabasePool*)pool shouldAddDatabaseToPool:(FMDatabase*)database; |
|
189 |
|
|
190 |
/** Tells the delegate that database was added to the pool. |
|
191 |
|
|
192 |
@param pool The `FMDatabasePool` object. |
|
193 |
@param database The `FMDatabase` object. |
|
194 |
|
|
195 |
*/ |
|
196 |
|
|
197 |
- (void)databasePool:(FMDatabasePool*)pool didAddDatabase:(FMDatabase*)database; |
|
198 |
|
|
199 |
@end |
|
200 |
|