Project

General

Profile

Objective-C » History » Version 1

Tomek Dziemidowicz, 2019-07-04 08:49 PM

1 1 Tomek Dziemidowicz
h1. Objective-C
2
3
<pre><code class="objc">
4
-(void)getRemoteChangesForTable:(NSString*)subscriberId tableName:(NSString*)tableName error:(NSError **)error{
5
    NSString *requestUrlString = [NSString stringWithFormat:@"%@/Sync/%@/%@", _serverURL, subscriberId, tableName];
6
    NSURL *requestURL = [NSURL URLWithString:requestUrlString];
7
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
8
9
    [request setURL:requestURL];
10
    [request setHTTPMethod:@"GET"];
11
12
    NSHTTPURLResponse *response;
13
14
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];
15
16
    if(!*error){
17
        switch (response.statusCode) {
18
            case 200:
19
                break;
20
            default:
21
                *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] forKey:NSLocalizedDescriptionKey]];
22
                break;
23
        }
24
    }
25
26
    if(*error) return;
27
28
    NSArray<SQLiteSyncData*> *syncDatas = [SQLiteSyncData arrayOfModelsFromData:data error:error];
29
30
    if(*error) return;
31
32
    for(SQLiteSyncData *syncData in syncDatas){        
33
        if([syncData.SyncId intValue] > 0){
34
            sqlite3 *db;
35
            sqlite3_stmt *stmt = nil;
36
37
            if(sqlite3_open([_databasePath UTF8String], &db) == SQLITE_OK){
38
                sqlite3_exec(db, "BEGIN TRANSACTION", 0, 0, 0);
39
40
                if(!*error && [syncData.TriggerInsertDrop length] > 0){
41
                    if(sqlite3_prepare_v2(db, [syncData.TriggerInsertDrop UTF8String], -1, &stmt, NULL) != SQLITE_OK
42
                       || sqlite3_step(stmt) != SQLITE_DONE){
43
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
44
                    }
45
                }
46
                if(!*error && [syncData.TriggerUpdateDrop length] > 0){
47
                    if(sqlite3_prepare_v2(db, [syncData.TriggerUpdateDrop UTF8String], -1, &stmt, NULL) != SQLITE_OK
48
                       || sqlite3_step(stmt) != SQLITE_DONE){
49
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
50
                    }
51
                }
52
                if(!*error && [syncData.TriggerDeleteDrop length] > 0){
53
                    if(sqlite3_prepare_v2(db, [syncData.TriggerDeleteDrop UTF8String], -1, &stmt, NULL) != SQLITE_OK
54
                       || sqlite3_step(stmt) != SQLITE_DONE){
55
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
56
                    }
57
                    sqlite3_reset(stmt);
58
                }
59
60
                NSArray<SQLiteSyncDataRecord*> *records;
61
62
                if(!*error){
63
                    records = [syncData SQLiteSyncDataRecordsWithError:error];
64
                }
65
66
                if(!*error){
67
                    for(SQLiteSyncDataRecord *record in records){
68
                        if([record.action intValue] == 1 || [record.action intValue] == 2 || [record.action intValue] == 3){
69
                            NSString *query;
70
71
                            switch ([record.action intValue]) {
72
                                case 1:
73
                                    query = syncData.QueryInsert;
74
                                    break;
75
                                case 2:
76
                                    query = syncData.QueryUpdate;
77
                                    break;
78
                                case 3:
79
                                    query = [syncData.QueryDelete stringByAppendingString:@"?"];
80
                                    break;
81
                            }
82
83
                            if(sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL) == SQLITE_OK){
84
                                for(int i = 0; i < [record.columns count]; i++){
85
                                    sqlite3_bind_text(stmt, i + 1, [[record.columns objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
86
                                }
87
88
                                if(sqlite3_step(stmt) != SQLITE_DONE){
89
                                    *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
90
                                }
91
92
                                sqlite3_reset(stmt);
93
                            }
94
                            else{
95
                                *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
96
                            }
97
98
                            if(*error) break;
99
                        }
100
                    }
101
                }
102
103
                if(!*error && [syncData.TriggerInsert length] > 0){
104
                    if(sqlite3_prepare_v2(db, [syncData.TriggerInsert UTF8String], -1, &stmt, NULL) != SQLITE_OK
105
                       || sqlite3_step(stmt) != SQLITE_DONE){
106
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
107
                    }
108
                    sqlite3_reset(stmt);
109
                }
110
                if(!*error && [syncData.TriggerUpdate length] > 0){
111
                    if(sqlite3_prepare_v2(db, [syncData.TriggerUpdate UTF8String], -1, &stmt, NULL) != SQLITE_OK
112
                       || sqlite3_step(stmt) != SQLITE_DONE){
113
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
114
                    }
115
                    sqlite3_reset(stmt);
116
                }
117
                if(!*error && [syncData.TriggerDelete length] > 0){
118
                    if(sqlite3_prepare_v2(db, [syncData.TriggerDelete UTF8String], -1, &stmt, NULL) != SQLITE_OK
119
                       || sqlite3_step(stmt) != SQLITE_DONE){
120
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
121
                    }
122
                    sqlite3_reset(stmt);
123
                }
124
125
                if(*error){
126
                    sqlite3_exec(db, "ROLLBACK TRANSACTION", 0, 0, 0);
127
                }
128
                else{
129
                    sqlite3_exec(db, "COMMIT", 0, 0, 0);
130
                }
131
132
                if(stmt){
133
                    sqlite3_finalize(stmt);
134
                }
135
136
                sqlite3_close(db);
137
            }
138
            else{
139
                *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:@"Failed to open database" forKey:NSLocalizedDescriptionKey]];
140
            }
141
142
            if(*error) return;
143
144
            [self commitSynchronization:syncData.SyncId error:error];
145
146
            if(*error) return;
147
        }
148
    }
149
}
150
</code></pre>
Go to top