Skip to content

Commit

Permalink
Cleanup header dependencies; cleanup whitespace; replace tabs; move c…
Browse files Browse the repository at this point in the history
…onditionally-included SQL binding code (ref: PR #170)
  • Loading branch information
Chris Brody committed Mar 1, 2015
1 parent 6d0e0dc commit c29e10b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 26 deletions.
11 changes: 3 additions & 8 deletions src/ios/SQLitePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
* See http://opensource.org/licenses/alphabetical for full text.
*/

#import <Foundation/Foundation.h>

#import "sqlite3.h"

#import <Cordova/CDVPlugin.h>
#import <Cordova/CDVJSON.h>
#import <Cordova/NSData+Base64.h>

#import "AppDelegate.h"
// Used to remove dependency on sqlite3.h in this header:
struct sqlite3;

enum WebSQLError {
UNKNOWN_ERR = 0,
Expand Down Expand Up @@ -53,7 +48,7 @@ typedef int WebSQLError;

-(id) getDBPath:(NSString *)dbFile at:(NSString *)atkey;

+(NSDictionary *)captureSQLiteErrorFromDb:(sqlite3 *)db;
+(NSDictionary *)captureSQLiteErrorFromDb:(struct sqlite3 *)db;

+(int)mapSQLiteErrorCode:(int)code;

Expand Down
39 changes: 21 additions & 18 deletions src/ios/SQLitePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
*/

#import "SQLitePlugin.h"

#import "sqlite3.h"

#include <regex.h>

#import <Cordova/NSData+Base64.h>

static void sqlite_regexp(sqlite3_context* context, int argc, sqlite3_value** values) {
if ( argc < 2 ) {
sqlite3_result_error(context, "SQL function regexp() called with missing arguments.", -1);
Expand Down Expand Up @@ -137,14 +142,14 @@ -(void)open: (CDVInvokedUrlCommand*)command
if (![[NSFileManager defaultManager] fileExistsAtPath:dbname]) {
NSString *createFromResource = [options objectForKey:@"createFromResource"];
if (createFromResource != NULL)
[self createFromResource:dbfilename withDbname:dbname];
[self createFromResource:dbfilename withDbname:dbname];
}

if (sqlite3_open(name, &db) != SQLITE_OK) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Unable to open DB"];
return;
} else {
sqlite3_create_function(db, "regexp", 2, SQLITE_ANY, NULL, &sqlite_regexp, NULL, NULL);
sqlite3_create_function(db, "regexp", 2, SQLITE_ANY, NULL, &sqlite_regexp, NULL, NULL);

// for SQLCipher version:
// NSString *dbkey = [options objectForKey:@"key"];
Expand Down Expand Up @@ -188,7 +193,7 @@ -(void)createFromResource:(NSString *)dbfile withDbname:(NSString *)dbname {
NSLog(@"Found prepopulated DB: %@", prepopulatedDb);
NSError *error;
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:prepopulatedDb toPath:dbname error:&error];

if(success)
NSLog(@"Copied prepopulated DB content to: %@", dbname);
else
Expand Down Expand Up @@ -240,7 +245,7 @@ -(void) delete: (CDVInvokedUrlCommand*)command

if (dbFileName==NULL) {
// Should not happen:
NSLog(@"No db name specified for delete");
NSLog(@"No db name specified for delete");
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"You must specify database path"];
} else {
NSString *dbPath = [self getDBPath:dbFileName at:dblocation];
Expand Down Expand Up @@ -379,11 +384,11 @@ -(CDVPluginResult*) executeSqlWithDict: (NSMutableDictionary*)options andArgs: (
i = 0;
entry = [NSMutableDictionary dictionaryWithCapacity:0];
count = sqlite3_column_count(statement);

while (i < count) {
columnValue = nil;
columnName = [NSString stringWithFormat:@"%s", sqlite3_column_name(statement, i)];

column_type = sqlite3_column_type(statement, i);
switch (column_type) {
case SQLITE_INTEGER:
Expand All @@ -400,6 +405,9 @@ -(CDVPluginResult*) executeSqlWithDict: (NSMutableDictionary*)options andArgs: (
case SQLITE_BLOB:
columnValue = [SQLitePlugin getBlobAsBase64String: sqlite3_column_blob(statement, i)
withLength: sqlite3_column_bytes(statement, i)];
#ifdef INCLUDE_SQL_BLOB_BINDING // TBD subjet to change:
columnValue = [@"sqlblob:;base64," stringByAppendingString:columnValue];
#endif
break;
case SQLITE_FLOAT:
columnValue = [NSNumber numberWithFloat: sqlite3_column_double(statement, i)];
Expand All @@ -408,13 +416,12 @@ -(CDVPluginResult*) executeSqlWithDict: (NSMutableDictionary*)options andArgs: (
columnValue = [NSNull null];
break;
}

if (columnValue) {
[entry setObject:columnValue forKey:columnName];
}

i++;

i++;
}
[resultRows addObject:entry];
break;
Expand Down Expand Up @@ -469,7 +476,7 @@ -(void)bindStatement:(sqlite3_stmt *)statement withArg:(NSObject *)arg atIndex:(
}
} else { // NSString
NSString *stringArg;

if ([arg isKindOfClass:[NSString class]]) {
stringArg = (NSString *)arg;
} else {
Expand Down Expand Up @@ -523,7 +530,7 @@ -(void)dealloc
#endif
}

+(NSDictionary *)captureSQLiteErrorFromDb:(sqlite3 *)db
+(NSDictionary *)captureSQLiteErrorFromDb:(struct sqlite3 *)db
{
int code = sqlite3_errcode(db);
int webSQLCode = [SQLitePlugin mapSQLiteErrorCode:code];
Expand Down Expand Up @@ -567,20 +574,16 @@ +(NSString*)getBlobAsBase64String:(const char*)blob_chars
{
size_t outputLength = 0;
char* outputBuffer = CDVNewBase64Encode(blob_chars, blob_length, true, &outputLength);

NSString* result = [[NSString alloc] initWithBytesNoCopy:outputBuffer
length:outputLength
encoding:NSASCIIStringEncoding
freeWhenDone:YES];
#if !__has_feature(objc_arc)
[result autorelease];
#endif

#ifdef INCLUDE_SQL_BLOB_BINDING // TBD subjet to change:
return [@"sqlblob:;base64," stringByAppendingString:result];
#else
return result;
#endif

return result;
}

@end

0 comments on commit c29e10b

Please sign in to comment.