// // MGMLiteConnection.m // MGMDB // // Created by Mr. Gecko on 8/13/10. // Copyright (c) 2011 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/ // // Permission to use, copy, modify, and/or distribute this software for any purpose // with or without fee is hereby granted, provided that the above copyright notice // and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, // OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS // ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // #import "MGMLiteConnection.h" #import "MGMLiteResult.h" @implementation MGMLiteConnection + (id)memoryConnection { return [[[self alloc] initWithPath:@":memory:"] autorelease]; } + (id)connectionWithPath:(NSString *)thePath { return [[[self alloc] initWithPath:thePath] autorelease]; } - (id)initWithPath:(NSString *)thePath { if (self = [super init]) { logQuery = NO; path = [thePath copy]; int result = SQLITE_INTERNAL; result = sqlite3_open([path UTF8String], &SQLiteConnection); if (result!=SQLITE_OK) { [self release]; self = nil; } else { escapeSet = [[NSCharacterSet characterSetWithCharactersInString:@"'"] retain]; } } return self; } - (void)dealloc { if (SQLiteConnection!=NULL) { int result = sqlite3_close(SQLiteConnection); if (result!=SQLITE_OK) NSLog(@"Unable to close the SQLite Database %@", path); } if (path!=nil) [path release]; if (escapeSet!=nil) [escapeSet release]; [super dealloc]; } - (sqlite3 *)SQLiteConnection { return SQLiteConnection; } - (NSString *)path { return path; } - (NSString *)errorMessage { return [NSString stringWithCString:sqlite3_errmsg(SQLiteConnection) encoding:NSUTF8StringEncoding]; } - (int)errorID { return sqlite3_errcode(SQLiteConnection); } - (NSString *)escapeData:(NSData *)theData { static const char hexdigits[] = "0123456789ABCDEF"; const size_t length = [theData length]; const char *bytes = [theData bytes]; char *stringBuffer = (char *)malloc(length * 2 + 1); char *hexBuffer = stringBuffer; for (int i=0; i> 4) & 0xF]; *hexBuffer++ = hexdigits[*bytes & 0xF]; bytes++; } *hexBuffer = '\0'; NSString *hex = [NSString stringWithUTF8String:stringBuffer]; free(stringBuffer); return hex; } - (NSString *)escapeString:(NSString *)theString { if (theString==nil) return nil; NSRange range = [theString rangeOfCharacterFromSet:escapeSet]; NSMutableString *string = [NSMutableString string]; if (range.location==NSNotFound) { [string appendString:theString]; } else { unsigned long len = [theString length]; for (unsigned long i=0; i