Project

General

Profile

Send JavaScript » History » Version 2

Tomek Dziemidowicz, 2019-07-18 09:09 PM

1 1 Tomek Dziemidowicz
h1. Send JavaScript
2
3
<pre><code class="javascript">
4 2 Tomek Dziemidowicz
function sqlitesync_SyncSendData() {
5
    sqlitesync_AddLog('<b>Starting sending data</b>');
6 1 Tomek Dziemidowicz
7 2 Tomek Dziemidowicz
    sqlitesync_SyncGlobalTableIndex = 0;
8
    sqlitesync_SyncDataToSend = "<?xml version=\"1.0\" encoding=\"utf-8\"?><SyncData xmlns=\"urn:sync-schema\">";
9
    sqlitesync_SyncSendTable(sqlitesync_SyncGlobalTableIndex);
10
}
11 1 Tomek Dziemidowicz
12 2 Tomek Dziemidowicz
function sqlitesync_SyncSendTable(tableIndex) {
13
    var selectAllStatement = "select * from sqlite_master where type='table' and sql like '%RowId%'";
14
    sqlitesync_DB.transaction(function (tx) {
15
        tx.executeSql(selectAllStatement, [], function (tx, result) {
16
            var datasetTables = result.rows;
17
            var item = datasetTables.item(tableIndex);
18
            if (item['tbl_name'] != "MergeDelete") {
19
20
                sqlitesync_SyncDataToSend += "<tab n=\"" + item['tbl_name'] + "\">";
21
22
                var columnParts = datasetTables.item(tableIndex).sql.replace(/^[^\(]+\(([^\)]+)\)/g, '$1').split(',');
23
                var columnNames = [];
24
                for (var col in columnParts) {
25
                    if (typeof columnParts[col] === 'string')
26
                        columnNames.push(columnParts[col].split(" ")[0]);
27
                }
28
29
                /*** nowe rekordy***/
30
                var selectForTable = "select * from " + item['tbl_name'] + " where RowId is null";
31
                var tableName = item['tbl_name'];
32
                tx.executeSql(selectForTable, [], function (tx, result) {
33
                    var datasetTable = result.rows;
34
35
                    if (datasetTable.length > 0){
36
                        sqlitesync_AddLog('Sending a new records for the table: ' + item['tbl_name'] + '');
37
                    }
38
                    sqlitesync_SyncDataToSend += "<ins>";
39
                    for (var i = 0, row = null; i < datasetTable.length; i++) {
40
                        row = datasetTable.item(i);
41
                        sqlitesync_SyncDataToSend += "<r>";
42
                        for (var j = 0; j < columnNames.length; j++) {
43
                            if (columnNames[j].replace(/"/gi, '') != 'MergeUpdate') {
44
                                sqlitesync_SyncDataToSend += "<" + columnNames[j].replace(/"/gi, '') + ">";
45
                                sqlitesync_SyncDataToSend += "<![CDATA[" + row[columnNames[j].replace(/"/gi, '')] + "]]>";
46
                                sqlitesync_SyncDataToSend += "</" + columnNames[j].replace(/"/gi, '') + ">";
47
                            }
48
                        }
49
                        sqlitesync_SyncDataToSend += "</r>";
50
                    }
51
                    sqlitesync_SyncDataToSend += "</ins>";
52
                    /*** zakutalizowane rekordy***/
53
                    var selectForUpdateTable = "select * from " + item['tbl_name'] + " where MergeUpdate > 0 and RowId is not null";
54
                    tx.executeSql(selectForUpdateTable, [], function (tx, result) {
55
                        datasetTable = result.rows;
56
57
                        if (datasetTable.length > 0){
58
                            sqlitesync_AddLog('Sending updated records for the table: ' + item['tbl_name'] + '');
59
                        }
60
61
                        sqlitesync_SyncDataToSend += "<upd>";
62
                        for (var i = 0, row = null; i < datasetTable.length; i++) {
63
                            row = datasetTable.item(i);
64
                            sqlitesync_SyncDataToSend += "<r>";
65
                            for (var j = 0; j < columnNames.length; j++) {
66
                                if (columnNames[j].replace(/"/gi, '') != 'MergeUpdate') {
67
                                    sqlitesync_SyncDataToSend += "<" + columnNames[j].replace(/"/gi, '') + ">";
68
                                    sqlitesync_SyncDataToSend += "<![CDATA[" + row[columnNames[j].replace(/"/gi, '')] + "]]>";
69
                                    sqlitesync_SyncDataToSend += "</" + columnNames[j].replace(/"/gi, '') + ">";
70
                                }
71
                            }
72
                            sqlitesync_SyncDataToSend += "</r>";
73
                        }
74
                        sqlitesync_SyncDataToSend += "</upd>";
75
                        sqlitesync_SyncDataToSend += "</tab>";
76
                        sqlitesync_SyncGlobalTableIndex++;
77
                        if (sqlitesync_SyncGlobalTableIndex < datasetTables.length)
78
                            sqlitesync_SyncSendTable(sqlitesync_SyncGlobalTableIndex);
79
                        else
80
                            sqlitesync_SyncSendTableDelete(tx);
81
                    });
82
                    /***/
83
                });
84
                /***/
85
            }
86
            else {
87
                sqlitesync_SyncGlobalTableIndex++;
88
                if (sqlitesync_SyncGlobalTableIndex < datasetTables.length)
89
                    sqlitesync_SyncSendTable(sqlitesync_SyncGlobalTableIndex);
90
                else
91
                    sqlitesync_SyncSendTableDelete(tx);
92
            }
93
        });
94
    });
95
}
96
97
/*
98
 * After synchronization, we need to clear marker for updated records to make sure that we will not send it again
99
 */
100
function sqlitesync_SyncClearUpdateMarker() {
101
    var selectAllStatement = "select * from sqlite_master where type='table' and sql like '%RowId%'";
102
    sqlitesync_DB.transaction(function (tx) {
103
        tx.executeSql(selectAllStatement, [], function (tx, result) {
104
            var datasetTables = result.rows;
105
            for(var tableIndex=0; tableIndex<datasetTables.length; tableIndex++){
106
	            var item = datasetTables.item(tableIndex);
107
				if (item['tbl_name'].toString().toLowerCase() == "mergeidentity") {
108
					
109
					tx.executeSql("update MergeIdentity set MergeUpdate=0 where MergeUpdate > 0;", [],function (transaction, result) {},function (transaction, error) {console.log(error);});
110
				}
111
112
	            if (item['tbl_name'] != "MergeDelete" && item['tbl_name'] != "MergeIdentity") {
113
	                var selectForUpdateTable = "select *, ? as syncTableName from " + item['tbl_name'] + " where MergeUpdate > 0";
114
	                tx.executeSql(selectForUpdateTable, [item['tbl_name']], function (tx, result) {
115
	                	if(result.rows.length > 0){
116
	                		var syncTableName = result.rows.item(0)['syncTableName'];
117
	                		var selectTriggerStatement = "select * from sqlite_master where type='trigger' and name = 'trMergeUpdate_"+syncTableName+"'";
118
	    	                tx.executeSql(selectTriggerStatement, [], function (tx, result) {
119
	    	                	if(result.rows.length > 0){
120
	    	                		var syncTableName = result.rows.item(0)['tbl_name'];
121
	    	                		var trigger = result.rows.item(0)['sql'];
122
123
	    	                		tx.executeSql("drop trigger trMergeUpdate_"+syncTableName+";", [],function (transaction, result) {
124
125
                                    },
126
                                    function (transaction, error) {
127
                                    });
128
	    	                		tx.executeSql("update " + syncTableName + " set MergeUpdate=0 where MergeUpdate > 0;", [],function (transaction, result) {
129
130
                                    },
131
                                    function (transaction, error) {
132
                                    });
133
	    	                		tx.executeSql(trigger, [],function (transaction, result) {
134
135
                                    },
136
                                    function (transaction, error) {
137
                                    });
138
	    	                	}
139
	    	                });
140
	                	}
141
	                });
142
	                /***/
143
	            }
144
            }
145
        });
146
    });
147
}
148
149
/*
150
 * After synchronization, we need to clear marker for deleted records to make sure that we will not send it again
151
 */
152
function sqlitesync_SyncClearDeletedRecords() {
153
    var selectAllStatement = "delete from MergeDelete";
154
    sqlitesync_DB.transaction(function (tx) {
155
        tx.executeSql(selectAllStatement, [], function (tx, result) {
156
        });
157
    });
158
}
159
160
/*
161
 * Preparing and sending records deleted on device
162
 */
163
function sqlitesync_SyncSendTableDelete(tx) {
164
    var selectForDelete = "select * from MergeDelete";
165
    tx.executeSql(selectForDelete, [], function (tx, result) {
166
        datasetTable = result.rows;
167
        sqlitesync_SyncDataToSend += "<delete>";
168
        if (datasetTable.length > 0) {
169
            sqlitesync_AddLog('Sending records for deleting');
170
        }
171
        for (var i = 0, row = null; i < datasetTable.length; i++) {
172
            row = datasetTable.item(i);
173
            sqlitesync_SyncDataToSend += "<r>";
174
            sqlitesync_SyncDataToSend += "<tb>" + row['TableId'] + "</tb>";
175
            sqlitesync_SyncDataToSend += "<id>" + row['RowId'] + "</id>";
176
            sqlitesync_SyncDataToSend += "</r>";
177
        }
178
        sqlitesync_SyncDataToSend += "</delete>";
179
180
		sqlitesync_SyncSendToServer();
181
    });
182
}
183
184
/*
185
 * Send packge with changes to server
186
 */
187
function sqlitesync_SyncSendToServer() {
188
    sqlitesync_SyncDataToSend += "</SyncData>";
189
190
    $.ajax({
191
        url: sqlitesync_SyncServerURL + "Send",
192
        method: 'POST',
193
        headers: {
194
                'Content-Type':'application/json'
195
        },
196
        cache : false,
197
        scope:this,
198
        data: JSON.stringify({ "subscriber" : sqlitesync_syncPdaIdent, "content": sqlitesync_SyncDataToSend , "version" : "3" }),
199
        dataType: 'json',
200
        timeout: 5 * 60 * 1000,//10min
201
        success: function (response, status) { //Success Callback
202
203
        	sqlitesync_SyncClearUpdateMarker();
204
        	sqlitesync_SyncClearDeletedRecords();
205
206
            sqlitesync_AddLog('Sending finished');
207
            //after success we are getting changes from server
208
            sqlitesync_SyncTables();
209
        },
210
        failure: function (result, request) {
211
            var statusCode = result.status;
212
            var responseText = result.responseText;
213
            sqlitesync_AddLog('<p>Error while syncing with the server ' + responseText + '</p>');
214
        }
215
    });
216
}
217
218 1 Tomek Dziemidowicz
</code></pre>
Go to top