Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added method to validate a SQL statement

  • Loading branch information...
commit d8f93d80e128237a65611ee2f56799671d36a067 1 parent b4bcbd9
@davedelong davedelong authored
Showing with 36 additions and 0 deletions.
  1. +2 −0  src/FMDatabaseAdditions.h
  2. +34 −0 src/FMDatabaseAdditions.m
View
2  src/FMDatabaseAdditions.h
@@ -28,4 +28,6 @@
- (FMResultSet*)getTableSchema:(NSString*)tableName;
- (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName;
+- (BOOL)validateSQL:(NSString*)sql error:(NSError**)error;
+
@end
View
34 src/FMDatabaseAdditions.m
@@ -111,4 +111,38 @@ - (BOOL)columnExists:(NSString*)tableName columnName:(NSString*)columnName {
return returnBool;
}
+- (BOOL)validateSQL:(NSString*)sql error:(NSError**)error {
+ sqlite3_stmt *pStmt = NULL;
+ BOOL validationSucceeded = YES;
+ BOOL keepTrying = YES;
+ int numberOfRetries = 0;
+
+ [self setInUse:YES];
+ while (keepTrying == YES) {
+ keepTrying = NO;
+ int rc = sqlite3_prepare_v2(db, [sql UTF8String], -1, &pStmt, 0);
+ if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
+ keepTrying = YES;
+ usleep(20);
+
+ if (busyRetryTimeout && (numberOfRetries++ > busyRetryTimeout)) {
+ NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [self databasePath]);
+ NSLog(@"Database busy");
+ }
+ } else if (rc != SQLITE_OK) {
+ validationSucceeded = NO;
+ if (error) {
+ *error = [NSError errorWithDomain:NSCocoaErrorDomain
+ code:[self lastErrorCode]
+ userInfo:[NSDictionary dictionaryWithObject:[self lastErrorMessage]
+ forKey:NSLocalizedDescriptionKey]];
+ }
+ }
+ }
+ [self setInUse:NO];
+ sqlite3_finalize(pStmt);
+
+ return validationSucceeded;
+}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.