Permalink
Browse files

Got rid of the pop and pull from the database pool- too dangerous! Up…

…dated readme and tests.
  • Loading branch information...
1 parent 58c3e70 commit ab3b78cf02fea1092128d51e0607cf51075be1fa @ccgus committed Feb 10, 2012
Showing with 243 additions and 251 deletions.
  1. +6 −0 CHANGES_AND_TODO_LIST.txt
  2. +4 −0 README.markdown
  3. +2 −0 fmdb.xcodeproj/project.pbxproj
  4. +4 −9 src/FMDatabase.h
  5. +0 −89 src/FMDatabase.m
  6. +1 −4 src/FMDatabasePool.h
  7. +19 −6 src/FMDatabasePool.m
  8. +207 −143 src/fmdb.m
@@ -3,6 +3,12 @@ Zip, nada, zilch. Got any ideas?
If you would like to contribute some code- awesome! I just ask that you make it conform to the coding conventions already set in here, and to add a couple of tests for your new code to fmdb.m. And of course, the code should be of general use to more than just a couple of folks. Send your patches to gus@flyingmeat.com.
+2012.02.10:
+ Changed up FMDatabasePool so that you can't "pop" it from a pool anymore. I just consider this too risky- use the block based functions instead.
+ Also provided a good reason in main.m for why you should use FMDatabaseQueue instead. Search for "ONLY_USE_THE_POOL_IF_YOU_ARE_DOING_READS".
+ I consider this branch 2.0 at this point- I'll let it bake for a couple of days, then push it to the main repo.
+
+
2012.01.06:
Added a new method to FMDatabase to make custom functions out of a block:
- (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(sqlite3_context *context, int argc, sqlite3_value **argv))block
View
@@ -174,6 +174,10 @@ An easy way to wrap things up in a transaction can be done like this:
FMDatabaseQueue will make a serialized GCD queue in the background and execute the blocks you pass to the GCD queue. This means if you call your FMDatabaseQueue's methods from multiple threads at the same time GDC will execute them in the order they are received. This means queries and updates won't step on each other's toes, and every one is happy.
+## Making custom sqlite functions, based on blocks.
+
+You can do this! For an example, look for "makeFunctionNamed:" in main.m
+
## History
The history and changes are availbe on its [GitHub page](https://github.com/ccgus/fmdb) and are summarized in the "CHANGES_AND_TODO_LIST.txt" file.
@@ -273,6 +273,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_64_BIT)";
+ CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_OBJCPP_ARC_ABI = YES;
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
@@ -289,6 +290,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+ CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_OBJCPP_ARC_ABI = YES;
GCC_GENERATE_DEBUGGING_SYMBOLS = NO;
GCC_MODEL_TUNING = G5;
View
@@ -2,15 +2,15 @@
#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
@@ -47,7 +47,7 @@
NSMutableDictionary *_cachedStatements;
NSMutableSet *_openResultSets;
NSMutableSet *_openFunctions;
-
+ /*
#ifdef FMDB_USE_WEAK_POOL
__weak FMDatabasePool *_poolAccessViaMethodOnly;
#else
@@ -57,6 +57,7 @@
NSInteger _poolPopCount;
FMDatabasePool *pool;
+ */
}
@@ -128,12 +129,6 @@
- (int)changes;
-- (FMDatabase*)popFromPool;
-- (void)pushToPool;
-
-- (FMDatabasePool *)pool;
-- (void)setPool:(FMDatabasePool *)value;
-
- (void)makeFunctionNamed:(NSString*)name maximumArguments:(int)count withBlock:(void (^)(sqlite3_context *context, int argc, sqlite3_value **argv))block;
@end
View
@@ -4,7 +4,6 @@
@interface FMDatabase ()
-- (void)checkPoolPushBack;
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
- (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args;
@end
@@ -60,8 +59,6 @@ - (void)dealloc {
FMDBRelease(_databasePath);
FMDBRelease(_openFunctions);
- [self setPool:0x00];
-
#if ! __has_feature(objc_arc)
[super dealloc];
#endif
@@ -181,8 +178,6 @@ - (void)resultSetDidClose:(FMResultSet *)resultSet {
NSValue *setValue = [NSValue valueWithNonretainedObject:resultSet];
[_openResultSets removeObject:setValue];
-
- [self checkPoolPushBack];
}
- (FMStatement*)cachedStatementForQuery:(NSString*)query {
@@ -489,14 +484,11 @@ - (FMResultSet *)executeQuery:(NSString *)sql withParameterDictionary:(NSDiction
- (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args {
if (![self databaseExists]) {
- //Pushing the FMDatabase instance back to the pool if error occurs
- [self checkPoolPushBack];
return 0x00;
}
if (_isExecutingStatement) {
[self warnInUse];
- [self checkPoolPushBack];
return 0x00;
}
@@ -533,7 +525,6 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arr
NSLog(@"Database busy");
sqlite3_finalize(pStmt);
_isExecutingStatement = NO;
- [self checkPoolPushBack];
return nil;
}
}
@@ -553,7 +544,6 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arr
sqlite3_finalize(pStmt);
_isExecutingStatement = NO;
- [self checkPoolPushBack];
return nil;
}
}
@@ -614,7 +604,6 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray*)arr
NSLog(@"Error: the bind count is not correct for the # of variables (executeQuery)");
sqlite3_finalize(pStmt);
_isExecutingStatement = NO;
- [self checkPoolPushBack];
return nil;
}
@@ -675,12 +664,10 @@ - (FMResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)ar
- (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArray:(NSArray*)arrayArgs orDictionary:(NSDictionary *)dictionaryArgs orVAList:(va_list)args {
if (![self databaseExists]) {
- [self checkPoolPushBack];
return NO;
}
if (_isExecutingStatement) {
- [self checkPoolPushBack];
[self warnInUse];
return NO;
}
@@ -717,7 +704,6 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra
NSLog(@"Database busy");
sqlite3_finalize(pStmt);
_isExecutingStatement = NO;
- [self checkPoolPushBack];
return NO;
}
}
@@ -742,7 +728,6 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra
}
_isExecutingStatement = NO;
- [self checkPoolPushBack];
return NO;
}
}
@@ -804,7 +789,6 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra
NSLog(@"Error: the bind count is not correct for the # of variables (%@) (executeUpdate)", sql);
sqlite3_finalize(pStmt);
_isExecutingStatement = NO;
- [self checkPoolPushBack];
return NO;
}
@@ -888,7 +872,6 @@ - (BOOL)executeUpdate:(NSString*)sql error:(NSError**)outErr withArgumentsInArra
}
_isExecutingStatement = NO;
- [self checkPoolPushBack];
return (rc == SQLITE_DONE || rc == SQLITE_OK);
}
@@ -938,8 +921,6 @@ - (BOOL)update:(NSString*)sql withErrorAndBindings:(NSError**)outErr, ... {
- (BOOL)rollback {
BOOL b = [self executeUpdate:@"rollback transaction"];
- [self pushToPool];
-
if (b) {
_inTransaction = NO;
}
@@ -950,8 +931,6 @@ - (BOOL)rollback {
- (BOOL)commit {
BOOL b = [self executeUpdate:@"commit transaction"];
- [self pushToPool];
-
if (b) {
_inTransaction = NO;
}
@@ -961,10 +940,6 @@ - (BOOL)commit {
- (BOOL)beginDeferredTransaction {
- if ([self pool]) {
- [self popFromPool];
- }
-
BOOL b = [self executeUpdate:@"begin deferred transaction"];
if (b) {
_inTransaction = YES;
@@ -975,10 +950,6 @@ - (BOOL)beginDeferredTransaction {
- (BOOL)beginTransaction {
- if ([self pool]) {
- [self popFromPool];
- }
-
BOOL b = [self executeUpdate:@"begin exclusive transaction"];
if (b) {
_inTransaction = YES;
@@ -999,10 +970,6 @@ - (BOOL)startSavePointWithName:(NSString*)name error:(NSError**)outErr {
NSParameterAssert(name);
- if ([self pool]) {
- [self popFromPool];
- }
-
if (![self executeUpdate:[NSString stringWithFormat:@"savepoint '%@';", name]]) {
if (*outErr) {
@@ -1025,10 +992,6 @@ - (BOOL)releaseSavePointWithName:(NSString*)name error:(NSError**)outErr {
*outErr = [self lastError];
}
- if ([self pool]) {
- [self pushToPool];
- }
-
return worked;
}
@@ -1042,10 +1005,6 @@ - (BOOL)rollbackToSavePointWithName:(NSString*)name error:(NSError**)outErr {
*outErr = [self lastError];
}
- if ([self pool]) {
- [self pushToPool];
- }
-
return worked;
}
@@ -1094,54 +1053,6 @@ - (void)setShouldCacheStatements:(BOOL)value {
}
}
-
-- (FMDatabase*)popFromPool {
-
- if (![self pool]) {
- NSLog(@"No FMDatabasePool in place for %@", self);
- return 0x00;
- }
-
- _poolPopCount++;
-
- return self;
-}
-
-- (void)pushToPool {
- _poolPopCount--;
-
- [self checkPoolPushBack];
-}
-
-- (void)checkPoolPushBack {
-
- if (_poolPopCount <= 0) {
- [[self pool] pushDatabaseBackInPool:self];
-
- if (_poolPopCount < 0) {
- _poolPopCount = 0;
- }
- }
-}
-
-// #if __has_feature(objc_arc_weak)
-
-- (FMDatabasePool *)pool {
-#if FMDB_USE_WEAK_POOL && (!__has_feature(objc_arc_weak))
- return objc_loadWeak(&_poolAccessViaMethodOnly);
-#else
- return _poolAccessViaMethodOnly;
-#endif
-}
-
-- (void)setPool:(FMDatabasePool *)value {
-#if FMDB_USE_WEAK_POOL && (!__has_feature(objc_arc_weak))
- objc_storeWeak(&_poolAccessViaMethodOnly, value);
-#else
- _poolAccessViaMethodOnly = value;
-#endif
-}
-
void FMDBBlockSQLiteCallBackFunction(sqlite3_context *context, int argc, sqlite3_value **argv);
void FMDBBlockSQLiteCallBackFunction(sqlite3_context *context, int argc, sqlite3_value **argv) {
#if ! __has_feature(objc_arc)
View
@@ -15,7 +15,7 @@
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.
+[[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!
@@ -122,9 +122,6 @@ If you check out a database, but never execute a statement or query, **you need
+ (id)databasePoolWithPath:(NSString*)aPath;
- (id)initWithPath:(NSString*)aPath;
-- (void)pushDatabaseBackInPool:(FMDatabase*)db;
-- (FMDatabase*)db;
-
- (NSUInteger)countOfCheckedInDatabases;
- (NSUInteger)countOfCheckedOutDatabases;
- (NSUInteger)countOfOpenDatabases;
Oops, something went wrong.

0 comments on commit ab3b78c

Please sign in to comment.