Advertisement
Guest User

Untitled

a guest
Feb 7th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #import "SQLiteAccess.h"
  3. #import <sqlite3.h>
  4.  
  5. @implementation SQLiteAccess
  6.  
  7. static int singleRowCallback(void *queryValuesVP, int columnCount, char **values, char **columnNames) {
  8.     NSMutableDictionary *queryValues = (NSMutableDictionary *)queryValuesVP;
  9.     int i;
  10.     for(i=0; i<columnCount; i++) {
  11.         [queryValues setObject:values[i] ? [NSString stringWithUTF8String:values[i]] : [NSNull null]
  12.                         forKey:[NSString stringWithUTF8String:columnNames[i]]];
  13.     }
  14.     return 0;
  15. }
  16.  
  17. static int multipleRowCallback(void *queryValuesVP, int columnCount, char **values, char **columnNames) {
  18.     NSMutableArray *queryValues = (NSMutableArray *)queryValuesVP;
  19.     NSMutableDictionary *individualQueryValues = [NSMutableDictionary dictionary];
  20.     int i;
  21.     for(i=0; i<columnCount; i++) {
  22.         [individualQueryValues setObject:values[i] ? [NSString stringWithUTF8String:values[i]] : [NSNull null]
  23.                                   forKey:[NSString stringWithUTF8String:columnNames[i]]];
  24.     }
  25.     [queryValues addObject:[NSDictionary dictionaryWithDictionary:individualQueryValues]];
  26.     return 0;
  27. }
  28.  
  29. + (NSString *)pathToDB {
  30.     NSString *originalDBPath = [[NSBundle mainBundle] pathForResource:dbName ofType:@"sqlite"];
  31.     NSString *path = nil;
  32.     NSString *DOCUMENTS = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  33.     NSString *dbNameDir = [NSString stringWithFormat:@"%@/HomeManager", DOCUMENTS];
  34.     NSFileManager *fileManager = [NSFileManager defaultManager];
  35.     BOOL isDir = NO;
  36.     BOOL dirExists = [fileManager fileExistsAtPath:dbNameDir isDirectory:&isDir];
  37.     NSString *dbPath = [NSString stringWithFormat:@"%@/%@.db", dbNameDir, dbName];
  38.     if(dirExists && isDir) {
  39.         BOOL dbExists = [fileManager fileExistsAtPath:dbPath];
  40.         if(!dbExists) {
  41.             NSError *error = nil;
  42.             BOOL success = [fileManager copyItemAtPath:originalDBPath toPath:dbPath error:&error];
  43.             if(!success) {
  44.                 NSLog(@"error = %@", error);
  45.             } else {
  46.                 path = dbPath;
  47.             }
  48.         } else {
  49.             path = dbPath;
  50.         }
  51.     } else if(!dirExists) {
  52.         NSError *error = nil;
  53.         BOOL success = [fileManager createDirectoryAtPath:dbNameDir withIntermediateDirectories:YES attributes:nil error:&error];
  54.         if(!success) {
  55.             NSLog(@"failed to create dir");
  56.         }
  57.         success = [fileManager copyItemAtPath:originalDBPath toPath:dbPath error:&error];
  58.         if(!success) {
  59.             NSLog(@"error = %@", error);
  60.         } else {
  61.             path = dbPath;
  62.         }
  63.     }
  64.     return path;
  65. }
  66.  
  67. + (NSNumber *)executeSQL:(NSString *)sql withCallback:(void *)callbackFunction context:(id)contextObject {
  68.     NSString *path = [self pathToDB];
  69.     sqlite3 *db = NULL;
  70.     int rc = SQLITE_OK;
  71.     NSInteger lastRowId = 0;
  72.     rc = sqlite3_open([path UTF8String], &db);
  73.     if(SQLITE_OK != rc) {
  74.         NSLog(@"Can't open database: %s\n", sqlite3_errmsg(db));
  75.         sqlite3_close(db);
  76.         return nil;
  77.     } else {
  78.         char *zErrMsg = NULL;
  79.         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  80.         rc = sqlite3_exec(db, [sql UTF8String], callbackFunction, (void*)contextObject, &zErrMsg);
  81.         if(SQLITE_OK != rc) {
  82.             NSLog(@"Can't run query '%@' error message: %s\n", sql, sqlite3_errmsg(db));
  83.             sqlite3_free(zErrMsg);
  84.         }
  85.         lastRowId = sqlite3_last_insert_rowid(db);
  86.         sqlite3_close(db);
  87.         [pool release];
  88.     }
  89.     NSNumber *lastInsertRowId = nil;
  90.     if(0 != lastRowId) {
  91.         lastInsertRowId = [NSNumber numberWithInteger:lastRowId];
  92.     }
  93.     return lastInsertRowId;
  94. }
  95.  
  96. + (NSString *)selectOneValueSQL:(NSString *)sql {
  97.     NSMutableDictionary *queryValues = [NSMutableDictionary dictionary];
  98.     [self executeSQL:sql withCallback:singleRowCallback context:queryValues];
  99.     NSString *value = nil;
  100.     if([queryValues count] == 1) {
  101.         value = [[queryValues objectEnumerator] nextObject];
  102.     }
  103.     return value;
  104. }
  105.  
  106. + (NSArray *)selectManyValuesWithSQL:(NSString *)sql {
  107.     NSMutableArray *queryValues = [NSMutableArray array];
  108.     [self executeSQL:sql withCallback:multipleRowCallback context:queryValues];
  109.     NSMutableArray *values = [NSMutableArray array];
  110.     for(NSDictionary *dict in queryValues) {
  111.         [values addObject:[[dict objectEnumerator] nextObject]];
  112.     }
  113.     return values;
  114. }
  115.  
  116. + (NSDictionary *)selectOneRowWithSQL:(NSString *)sql {
  117.     NSMutableDictionary *queryValues = [NSMutableDictionary dictionary];
  118.     [self executeSQL:sql withCallback:singleRowCallback context:queryValues];
  119.     return [NSDictionary dictionaryWithDictionary:queryValues];    
  120. }
  121.  
  122. + (NSArray *)selectManyRowsWithSQL:(NSString *)sql {
  123.     NSMutableArray *queryValues = [NSMutableArray array];
  124.     [self executeSQL:sql withCallback:multipleRowCallback context:queryValues];
  125.     return [NSArray arrayWithArray:queryValues];
  126. }
  127.  
  128. + (NSNumber *)insertWithSQL:(NSString *)sql {
  129.     sql = [NSString stringWithFormat:@"BEGIN TRANSACTION; %@; COMMIT TRANSACTION;", sql];
  130.     return [self executeSQL:sql withCallback:NULL context:NULL];
  131. }
  132.  
  133. + (void)updateWithSQL:(NSString *)sql {
  134.     sql = [NSString stringWithFormat:@"BEGIN TRANSACTION; %@; COMMIT TRANSACTION;", sql];
  135.     [self executeSQL:sql withCallback:NULL context:nil];
  136. }
  137.  
  138. + (void)deleteWithSQL:(NSString *)sql {
  139.     sql = [NSString stringWithFormat:@"BEGIN TRANSACTION; %@; COMMIT TRANSACTION;", sql];
  140.     [self executeSQL:sql withCallback:NULL context:nil];
  141. }
  142.  
  143. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement