From: Joseph Spiros Date: Sat, 10 Jan 2009 22:24:48 +0000 (+0000) Subject: Update to ITSQLite3Database interface, and NSData additions to work with Base64 encod... X-Git-Url: http://git.ithinksw.org/ITFoundation.git/commitdiff_plain/0238d96a35ba0c9d948e03672cce0506f8a57b46 Update to ITSQLite3Database interface, and NSData additions to work with Base64 encoded data. --- diff --git a/ITCategory-NSData.h b/ITCategory-NSData.h index 68af6e3..938859d 100644 --- a/ITCategory-NSData.h +++ b/ITCategory-NSData.h @@ -13,8 +13,12 @@ @interface NSData (ITFoundationCategory) ++ (id)dataWithBase64:(NSString *)base64; +- (id)initWithBase64:(NSString *)base64; + - (NSString *)hexadecimalRepresentation; +- (NSString *)base64; - (NSData *)MD5; - (NSData *)SHA1; diff --git a/ITCategory-NSData.m b/ITCategory-NSData.m index 0f45f68..e35fa45 100644 --- a/ITCategory-NSData.m +++ b/ITCategory-NSData.m @@ -1,9 +1,32 @@ #import "ITCategory-NSData.h" +#import +#import #import #import @implementation NSData (ITFoundationCategory) ++ (id)dataWithBase64:(NSString *)base64 { + return [[[self alloc] initWithBase64:base64] autorelease]; +} + +- (id)initWithBase64:(NSString *)base64 { + BIO *mem = BIO_new_mem_buf((void *)[base64 cString], [base64 cStringLength]); + BIO *b64 = BIO_new(BIO_f_base64()); + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + mem = BIO_push(b64, mem); + + NSMutableData *data = [NSMutableData data]; + char inbuf[512]; + int inlen; + while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0) { + [data appendBytes:inbuf length:inlen]; + } + + BIO_free_all(mem); + return [self initWithData:data]; +} + - (NSString *)hexadecimalRepresentation { int dataLength = [self length]; int stringLength = dataLength * 2; @@ -19,6 +42,23 @@ return [NSString stringWithCString:hexString length:stringLength]; } +- (NSString *)base64 { + BIO *mem = BIO_new(BIO_s_mem()); + BIO *b64 = BIO_new(BIO_f_base64()); + BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); + mem = BIO_push(b64, mem); + + BIO_write(mem, [self bytes], [self length]); + BIO_flush(mem); + + char *base64Char; + long base64Length = BIO_get_mem_data(mem, &base64Char); + NSString *base64String = [NSString stringWithCString:base64Char length:base64Length]; + + BIO_free_all(mem); + return base64String; +} + - (NSData *)MD5 { int length = 16; unsigned char digest[length]; diff --git a/ITSQLite3Database.h b/ITSQLite3Database.h index 803c49d..ee9163c 100644 --- a/ITSQLite3Database.h +++ b/ITSQLite3Database.h @@ -30,13 +30,18 @@ extern id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex); - (BOOL)rollback; - (BOOL)rollbackTransaction; -- (BOOL)executeQuery:(NSString *)query va_args:(va_list)args; - (BOOL)executeQuery:(NSString *)query, ...; -- (NSDictionary *)fetchRow:(NSString *)query va_args:(va_list)args; +// returns a dictionary with column names as keys, or nil - (NSDictionary *)fetchRow:(NSString *)query, ...; -- (NSArray *)fetchTable:(NSString *)query va_args:(va_list)args; +// returns an array of dictionaries with column names as keys, or an empty array - (NSArray *)fetchTable:(NSString *)query, ...; +// returns a single column value, or nil +- (id)fetchRowColumn:(NSString *)query, ...; + +// returns an array of single column values, or an empty array +- (NSArray *)fetchTableColumn:(NSString *)query, ...; + @end diff --git a/ITSQLite3Database.m b/ITSQLite3Database.m index 9de25de..66f086c 100644 --- a/ITSQLite3Database.m +++ b/ITSQLite3Database.m @@ -55,6 +55,12 @@ id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) { return retval; } +@interface ITSQLite3Database (Internals) +- (BOOL)executeQuery:(NSString *)query va_args:(va_list)args; +- (NSDictionary *)fetchRow:(NSString *)query va_args:(va_list)args; +- (NSArray *)fetchTable:(NSString *)query va_args:(va_list)args; +@end + @implementation ITSQLite3Database - (id)initWithPath:(NSString *)path { @@ -159,7 +165,7 @@ id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) { va_start(args, query); [dbLock lock]; - id result = [self fetchRow:query va_args:args]; + NSDictionary *result = [self fetchRow:query va_args:args]; [dbLock unlock]; va_end(args); @@ -213,11 +219,53 @@ id sqlite3_column_objc_object(sqlite3_stmt *statement, int columnIndex) { va_start(args, query); [dbLock lock]; - id result = [self fetchTable:query va_args:args]; + NSArray *result = [self fetchTable:query va_args:args]; [dbLock unlock]; va_end(args); return result; } +- (id)fetchRowColumn:(NSString *)query, ... { + va_list args; + va_start(args, query); + + [dbLock lock]; + NSDictionary *result = [self fetchRow:query va_args:args]; + [dbLock unlock]; + + va_end(args); + + if (result) { + if ([[result allKeys] count] >= 1) { + return [result objectForKey:[[result allKeys] objectAtIndex:0]]; + } + } + + return nil; +} + +- (NSArray *)fetchTableColumn:(NSString *)query, ... { + va_list args; + va_start(args, query); + + [dbLock lock]; + NSArray *result = [self fetchTable:query va_args:args]; + [dbLock unlock]; + + va_end(args); + + if (result) { + NSMutableArray *columnArray = [[NSMutableArray alloc] init]; + NSEnumerator *enumerator = [result objectEnumerator]; + NSDictionary *row; + while (row = (NSDictionary *)[enumerator nextObject]) { + [columnArray addObject:[row objectForKey:[[row allKeys] objectAtIndex:0]]]; + } + return [columnArray autorelease]; + } + + return nil; +} + @end