Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /*
  2. //
  3. // SpySqlite.m
  4. // sqlx
  5. //
  6. // Created by Scott D. Yelich on 8/21/08.
  7. // Copyright 2008 n/a. All rights reserved.
  8. //
  9. */
  10.  
  11. #import "SpySqlite.h"
  12.  
  13.  
  14. @implementation SpySqlite
  15.  
  16. @synthesize delegate, headers, data;
  17.  
  18.  
  19. - (id) init {
  20. self = [super init];
  21. if (self != nil) {
  22. headers = [[NSMutableArray alloc] init];
  23. data = [[NSMutableArray alloc] init];
  24. filePath = nil;
  25. }
  26. return self;
  27. }
  28.  
  29. - (int)reOpenDatabase {
  30. [self close];
  31. return [self openDatabase:filePath];
  32. }
  33.  
  34. - (int)openDatabase:(NSString *)dbName {
  35. int rc = 0;
  36. if (nil == dbName) {
  37. dbName = [NSString stringWithFormat:@":memory:"];
  38. }
  39. if (rc = sqlite3_open([dbName UTF8String], &database)) {
  40. errorCode = rc;
  41. return rc;
  42. }
  43.  
  44. /*
  45. // I decided that using the busy_handler allowed for more control. So
  46. // this is left commented out.
  47. if (rc = sqlite3_busy_timeout(_db, 25)) { // 25ms?
  48. throw Xql3Exception(2, "set busy timeout");
  49. }
  50. */
  51. /*
  52. if (rc = sqlite3_busy_handler(_db, Xql3::busy, this)) {
  53. throw Xql3Exception(3, "set busy handler");
  54. }
  55. */
  56. filePath = [dbName copy];
  57. return rc;
  58. }
  59.  
  60. -(int)close {
  61. int rc = 0;
  62. if (database) {
  63. if (sqlStatement) {
  64. rc = sqlite3_finalize(sqlStatement); // check rc?
  65. }
  66. if (rc = sqlite3_close(database) ) {
  67. // error!
  68. }
  69. database = nil;
  70. }
  71. return rc;
  72. }
  73.  
  74. - (int)xSql:(NSString *)sql {
  75. int rc = SQLITE_OK;
  76.  
  77. if ([sql length] == 0 ) {
  78. return rc;
  79. }
  80.  
  81. int i;
  82. int ncols;
  83. sqlStatement = nil;
  84.  
  85. const char * zSql;
  86. const char * zLeftover = [sql UTF8String];
  87.  
  88. // clear out the old results ...
  89. [headers removeAllObjects];
  90. [data removeAllObjects]; // in case errors out
  91.  
  92. while (1) { // loop for all sql statements ...
  93.  
  94. sqlStatement = nil;
  95. zSql = zLeftover;
  96. if (!zSql[0]) {
  97. break;
  98. }
  99. // NSLog(@"preparing zsql[%s] db[%i] zleft[%s]", zSql, (int)database, zLeftover);
  100. if ( SQLITE_OK != (rc = sqlite3_prepare_v2(database, zSql, -1, &sqlStatement, &zLeftover))) {
  101. // NSLog(@"preparing error and now zsql[%s] db[%i] zleft[%s]", zSql, (int)database, zLeftover);
  102.  
  103. // NSLog(@"NOT OK in prepare rc[%i]!", rc);
  104. error = YES;
  105. return rc;
  106. }
  107. if (!sqlStatement) { // parsed blank or comment?
  108. // NSLog(@"sql is blank or comment, skipping (to next)");
  109. continue;
  110. }
  111.  
  112. ncols = sqlite3_column_count(sqlStatement);
  113. // NSLog(@"sql cols=%i", ncols);
  114.  
  115. while (1) { // loop for all results of an individual statement ...
  116.  
  117. rc = sqlite3_step(sqlStatement);
  118.  
  119. // NSMutableArray
  120.  
  121. if (SQLITE_ROW == rc) {
  122. if (0 == [headers count]) {
  123. for(i=0; i<ncols; i++) {
  124. [headers addObject:[NSString stringWithFormat:@"%s", sqlite3_column_name(sqlStatement, i)]];
  125. }
  126. }
  127.  
  128. [data removeAllObjects];
  129.  
  130. for(i=0; i<ncols; i++){
  131. if ( sqlite3_column_bytes(sqlStatement, i) > 0 ) {
  132. [data addObject:[NSString stringWithFormat:@"%s", sqlite3_column_text(sqlStatement, i)]];
  133. } else {
  134. [data addObject:[NSString stringWithFormat:@""]];
  135. }
  136. }
  137.  
  138. // NSLog(@"callback...");
  139. [delegate xSqlCallbackWithHeaders:headers andData:data];
  140.  
  141. } else {
  142. if ( SQLITE_DONE != rc ) {
  143. // NSLog(@"NOT OK in step, skipping ...");
  144. }
  145. rc = sqlite3_finalize(sqlStatement);
  146. sqlStatement = nil;
  147. zSql = zLeftover; // advance to next statement ... or error?
  148. while (isspace((unsigned char)zSql[0])) zSql++;
  149. break;
  150. }
  151.  
  152. }
  153.  
  154. }
  155. if (sqlStatement) sqlite3_finalize(sqlStatement);
  156. sqlStatement = nil;
  157.  
  158. return rc;
  159. }
  160.  
  161.  
  162. @end
Add Comment
Please, Sign In to add comment