Skip to content

Commit

Permalink
Random code and documentation cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Feb 15, 2012
1 parent 8831f61 commit 0cfca84
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 100 deletions.
21 changes: 2 additions & 19 deletions src/FMDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@
#import "sqlite3.h"
#import "FMResultSet.h"
#import "FMDatabasePool.h"
/*
#ifndef MAC_OS_X_VERSION_10_7
#define MAC_OS_X_VERSION_10_7 1070
#endif

#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7
#define FMDB_USE_WEAK_POOL 1
#endif
*/

#if ! __has_feature(objc_arc)
#define FMDBAutorelease(__v) ([__v autorelease]);
#define FMDBReturnAutoreleased FMDBAutorelease
Expand Down Expand Up @@ -47,17 +40,7 @@
NSMutableDictionary *_cachedStatements;
NSMutableSet *_openResultSets;
NSMutableSet *_openFunctions;
/*
#ifdef FMDB_USE_WEAK_POOL
__weak FMDatabasePool *_poolAccessViaMethodOnly;
#else
FMDatabasePool *_poolAccessViaMethodOnly;
#endif
NSInteger _poolPopCount;
FMDatabasePool *pool;
*/

}


Expand Down
2 changes: 0 additions & 2 deletions src/FMDatabase.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ - (BOOL)close {
- (void)clearCachedStatements {

for (FMStatement *cachedStmt in [_cachedStatements objectEnumerator]) {
//NSLog(@"cachedStmt: '%@'", cachedStmt);
[cachedStmt close];
}

Expand Down Expand Up @@ -185,7 +184,6 @@ - (FMStatement*)cachedStatementForQuery:(NSString*)query {
}

- (void)setCachedStatement:(FMStatement*)statement forQuery:(NSString*)query {
//NSLog(@"setting query: %@", query);

query = [query copy]; // in case we got handed in a mutable string...

Expand Down
84 changes: 5 additions & 79 deletions src/FMDatabasePool.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,92 +14,18 @@
***README OR SUFFER***
Before using FMDatabasePool, please consider using FMDatabaseQueue instead.
I'm also not 100% sold on this interface. So if you use FMDatabasePool, things like
[[pool db] popFromPool] might go away. In fact, I'm pretty darn sure they will.
If you really really really know what you're doing and FMDatabasePool is what
you really really need, OK you can use it. But just be careful not to deadlock!
First, make your pool.
FMDatabasePool *pool = [FMDatabasePool databasePoolWithPath:aPath];
If you just have a single statement- use it like so:
[[pool db] executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:42]];
The pool's db method will return an instance of FMDatabase that knows it is in a pool. After it is done with the update, it will place itself back into the pool.
Making a query is similar:
FMResultSet *rs = [[pool db] executeQuery:@"SELECT * FROM myTable"];
while ([rs next]) {
//retrieve values for each record
}
When the result set is exhausted or [rs close] is called, the result set will tell the database it was created from to put itself back into the pool for use later on.
If you'd rather use multiple queries without having to call [pool db] each time, you can grab a database instance, tell it to stay out of the pool, and then tell it to go back in the pool when you're done:
FMDatabase *db = [[pool db] popFromPool];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
// put the database back in the pool.
[db pushToPool];
Alternatively, you can use this nifty block based approach:
[dbPool useDatabase: ^(FMDatabase *aDb) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
}];
And it will do the right thing.
you really really need (ie, you're using a read only database), OK you can use
it. But just be careful not to deadlock!
Starting a transaction will keep the db from going back into the pool automatically:
FMDatabase *db = [pool db];
[db beginTransaction];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
[db commit]; // or a rollback here would work as well.
There is also a block based transaction approach:
[dbPool inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
}];
If you check out a database, but never execute a statement or query, **you need to put it back in the pool yourself**.
FMDatabase *db = [pool db];
// lala, don't do anything with the database
// oh look, I BETTER PUT THE DB BACK IN THE POOL OR ELSE IT IS GOING TO LEAK:
[db pushToPool];
For an example on deadlocking, search for:
ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS_OTHERWISE_YOULL_DEADLOCK_USE_FMDATABASEQUEUE_INSTEAD
in the main.m file.
*/









@class FMDatabase;

@interface FMDatabasePool : NSObject {
Expand Down

0 comments on commit 0cfca84

Please sign in to comment.