Send Objective-C » History » Version 1
Tomek Dziemidowicz, 2019-07-18 09:06 PM
| 1 | 1 | Tomek Dziemidowicz | h1. Send Objective-C |
|---|---|---|---|
| 2 | |||
| 3 | <pre><code class="objc"> |
||
| 4 | -(void)sendLocalChanges:(NSString*)subscriberId error:(NSError **)error{ |
||
| 5 | sqlite3 *db; |
||
| 6 | sqlite3_stmt *stmt; |
||
| 7 | NSString *query; |
||
| 8 | |||
| 9 | NSMutableString *builder = [NSMutableString string]; |
||
| 10 | |||
| 11 | [builder appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><SyncData xmlns=\"urn:sync-schema\">"]; |
||
| 12 | |||
| 13 | if(sqlite3_open([_databasePath UTF8String], &db) == SQLITE_OK){ |
||
| 14 | NSMutableArray<NSString*> *tables = [NSMutableArray array]; |
||
| 15 | |||
| 16 | query = @"select tbl_Name from sqlite_master where type='table' and sql like '%RowId%';"; |
||
| 17 | if(sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL) == SQLITE_OK){ |
||
| 18 | while(sqlite3_step(stmt) == SQLITE_ROW) { |
||
| 19 | [tables addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 0)]]; |
||
| 20 | } |
||
| 21 | } |
||
| 22 | else{ |
||
| 23 | *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]]; |
||
| 24 | } |
||
| 25 | |||
| 26 | if(!*error){ |
||
| 27 | for(NSString *tableName in tables){ |
||
| 28 | if([tableName caseInsensitiveCompare:@"MergeDelete"] != NSOrderedSame){ |
||
| 29 | [builder appendFormat:@"<tab n=\"%@\">", tableName]; |
||
| 30 | |||
| 31 | [builder appendString:@"<ins>"]; |
||
| 32 | query = [NSString stringWithFormat:@"select * from %@ where RowId is null;", tableName]; |
||
| 33 | if(sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL) == SQLITE_OK){ |
||
| 34 | while(sqlite3_step(stmt) == SQLITE_ROW) { |
||
| 35 | [builder appendString:@"<r>"]; |
||
| 36 | for(int i = 0; i < sqlite3_column_count(stmt); i++){ |
||
| 37 | NSString *columnName = [NSString stringWithUTF8String:sqlite3_column_name(stmt, i)]; |
||
| 38 | if([columnName caseInsensitiveCompare:@"MergeUpdate"] != NSOrderedSame){ |
||
| 39 | [builder appendFormat:@"<%1$@><![CDATA[%2$s]]></%1$@>", columnName, sqlite3_column_text(stmt, i)]; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | [builder appendString:@"</r>"]; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | else{ |
||
| 46 | *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]]; |
||
| 47 | break; |
||
| 48 | } |
||
| 49 | [builder appendString:@"</ins>"]; |
||
| 50 | |||
| 51 | [builder appendString:@"<upd>"]; |
||
| 52 | query = [NSString stringWithFormat:@"select * from %@ where MergeUpdate > 0 and RowId is not null;", tableName]; |
||
| 53 | if(sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL) == SQLITE_OK){ |
||
| 54 | while(sqlite3_step(stmt) == SQLITE_ROW) { |
||
| 55 | [builder appendString:@"<r>"]; |
||
| 56 | for(int i = 0; i < sqlite3_column_count(stmt); i++){ |
||
| 57 | NSString *columnName = [NSString stringWithUTF8String:sqlite3_column_name(stmt, i)]; |
||
| 58 | if([columnName caseInsensitiveCompare:@"MergeUpdate"] != NSOrderedSame){ |
||
| 59 | [builder appendFormat:@"<%1$@><![CDATA[%2$s]]></%1$@>", columnName, sqlite3_column_text(stmt, i)]; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | [builder appendString:@"</r>"]; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | else{ |
||
| 66 | *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]]; |
||
| 67 | break; |
||
| 68 | } |
||
| 69 | [builder appendString:@"</upd>"]; |
||
| 70 | |||
| 71 | [builder appendString:@"</tab>"]; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | if(!*error){ |
||
| 77 | [builder appendString:@"<delete>"]; |
||
| 78 | query = @"select TableId,RowId from MergeDelete;"; |
||
| 79 | if(sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL) == SQLITE_OK){ |
||
| 80 | while(sqlite3_step(stmt) == SQLITE_ROW) { |
||
| 81 | [builder appendFormat:@"<r><tb>%1$s</tb><id>%2$s</id></r>", sqlite3_column_text(stmt, 0), sqlite3_column_text(stmt, 1)]; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | else{ |
||
| 85 | *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]]; |
||
| 86 | } |
||
| 87 | [builder appendString:@"</delete>"]; |
||
| 88 | } |
||
| 89 | |||
| 90 | if(stmt){ |
||
| 91 | sqlite3_finalize(stmt); |
||
| 92 | } |
||
| 93 | sqlite3_close(db); |
||
| 94 | } |
||
| 95 | else{ |
||
| 96 | *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:@"Failed to open database" forKey:NSLocalizedDescriptionKey]]; |
||
| 97 | } |
||
| 98 | |||
| 99 | [builder appendString:@"</SyncData>"]; |
||
| 100 | |||
| 101 | if(*error) return; |
||
| 102 | |||
| 103 | NSDictionary *inputObject = @{@"subscriber": subscriberId, @"content": builder, @"version": @"3"}; |
||
| 104 | NSData *inputData = [NSJSONSerialization dataWithJSONObject:inputObject options:NSJSONWritingPrettyPrinted error:error]; |
||
| 105 | |||
| 106 | if(*error) return; |
||
| 107 | |||
| 108 | NSString *requestUrlString = [NSString stringWithFormat:@"%@/Send", _serverURL]; |
||
| 109 | NSURL *requestURL = [NSURL URLWithString:requestUrlString]; |
||
| 110 | NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; |
||
| 111 | |||
| 112 | [request setURL:requestURL]; |
||
| 113 | [request setHTTPMethod:@"POST"]; |
||
| 114 | [request setHTTPBody:inputData]; |
||
| 115 | [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; |
||
| 116 | [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)inputData.length] forHTTPHeaderField:@"Content-Length"]; |
||
| 117 | |||
| 118 | NSHTTPURLResponse *response; |
||
| 119 | |||
| 120 | NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error]; |
||
| 121 | |||
| 122 | if(!*error){ |
||
| 123 | switch (response.statusCode) { |
||
| 124 | case 200: |
||
| 125 | break; |
||
| 126 | case 204: |
||
| 127 | break; |
||
| 128 | default: |
||
| 129 | *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] forKey:NSLocalizedDescriptionKey]]; |
||
| 130 | break; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | </code></pre> |