@interface NSData (ITFoundationCategory)
++ (id)dataWithBase64:(NSString *)base64;
+- (id)initWithBase64:(NSString *)base64;
+
- (NSString *)hexadecimalRepresentation;
+- (NSString *)base64;
- (NSData *)MD5;
- (NSData *)SHA1;
#import "ITCategory-NSData.h"
+#import <openssl/bio.h>
+#import <openssl/evp.h>
#import <openssl/md5.h>
#import <openssl/sha.h>
@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;
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];
- (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
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 {
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);
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