Project

General

Profile

Actions

Send JavaScript

function sqlitesync_SyncSendData() {
    sqlitesync_AddLog('<b>Starting sending data</b>');

    sqlitesync_SyncGlobalTableIndex = 0;
    sqlitesync_SyncDataToSend = "<?xml version=\"1.0\" encoding=\"utf-8\"?><SyncData xmlns=\"urn:sync-schema\">";
    sqlitesync_SyncSendTable(sqlitesync_SyncGlobalTableIndex);
}

function sqlitesync_SyncSendTable(tableIndex) {
    var selectAllStatement = "select * from sqlite_master where type='table' and sql like '%RowId%'";
    sqlitesync_DB.transaction(function (tx) {
        tx.executeSql(selectAllStatement, [], function (tx, result) {
            var datasetTables = result.rows;
            var item = datasetTables.item(tableIndex);
            if (item['tbl_name'] != "MergeDelete") {

                sqlitesync_SyncDataToSend += "<tab n=\"" + item['tbl_name'] + "\">";

                var columnParts = datasetTables.item(tableIndex).sql.replace(/^[^\(]+\(([^\)]+)\)/g, '$1').split(',');
                var columnNames = [];
                for (var col in columnParts) {
                    if (typeof columnParts[col] === 'string')
                        columnNames.push(columnParts[col].split(" ")[0]);
                }

                /*** nowe rekordy***/
                var selectForTable = "select * from " + item['tbl_name'] + " where RowId is null";
                var tableName = item['tbl_name'];
                tx.executeSql(selectForTable, [], function (tx, result) {
                    var datasetTable = result.rows;

                    if (datasetTable.length > 0){
                        sqlitesync_AddLog('Sending a new records for the table: ' + item['tbl_name'] + '');
                    }
                    sqlitesync_SyncDataToSend += "<ins>";
                    for (var i = 0, row = null; i < datasetTable.length; i++) {
                        row = datasetTable.item(i);
                        sqlitesync_SyncDataToSend += "<r>";
                        for (var j = 0; j < columnNames.length; j++) {
                            if (columnNames[j].replace(/"/gi, '') != 'MergeUpdate') {
                                sqlitesync_SyncDataToSend += "<" + columnNames[j].replace(/"/gi, '') + ">";
                                sqlitesync_SyncDataToSend += "<![CDATA[" + row[columnNames[j].replace(/"/gi, '')] + "]]>";
                                sqlitesync_SyncDataToSend += "</" + columnNames[j].replace(/"/gi, '') + ">";
                            }
                        }
                        sqlitesync_SyncDataToSend += "</r>";
                    }
                    sqlitesync_SyncDataToSend += "</ins>";
                    /*** zakutalizowane rekordy***/
                    var selectForUpdateTable = "select * from " + item['tbl_name'] + " where MergeUpdate > 0 and RowId is not null";
                    tx.executeSql(selectForUpdateTable, [], function (tx, result) {
                        datasetTable = result.rows;

                        if (datasetTable.length > 0){
                            sqlitesync_AddLog('Sending updated records for the table: ' + item['tbl_name'] + '');
                        }

                        sqlitesync_SyncDataToSend += "<upd>";
                        for (var i = 0, row = null; i < datasetTable.length; i++) {
                            row = datasetTable.item(i);
                            sqlitesync_SyncDataToSend += "<r>";
                            for (var j = 0; j < columnNames.length; j++) {
                                if (columnNames[j].replace(/"/gi, '') != 'MergeUpdate') {
                                    sqlitesync_SyncDataToSend += "<" + columnNames[j].replace(/"/gi, '') + ">";
                                    sqlitesync_SyncDataToSend += "<![CDATA[" + row[columnNames[j].replace(/"/gi, '')] + "]]>";
                                    sqlitesync_SyncDataToSend += "</" + columnNames[j].replace(/"/gi, '') + ">";
                                }
                            }
                            sqlitesync_SyncDataToSend += "</r>";
                        }
                        sqlitesync_SyncDataToSend += "</upd>";
                        sqlitesync_SyncDataToSend += "</tab>";
                        sqlitesync_SyncGlobalTableIndex++;
                        if (sqlitesync_SyncGlobalTableIndex < datasetTables.length)
                            sqlitesync_SyncSendTable(sqlitesync_SyncGlobalTableIndex);
                        else
                            sqlitesync_SyncSendTableDelete(tx);
                    });
                    /***/
                });
                /***/
            }
            else {
                sqlitesync_SyncGlobalTableIndex++;
                if (sqlitesync_SyncGlobalTableIndex < datasetTables.length)
                    sqlitesync_SyncSendTable(sqlitesync_SyncGlobalTableIndex);
                else
                    sqlitesync_SyncSendTableDelete(tx);
            }
        });
    });
}

/*
 * After synchronization, we need to clear marker for updated records to make sure that we will not send it again
 */
function sqlitesync_SyncClearUpdateMarker() {
    var selectAllStatement = "select * from sqlite_master where type='table' and sql like '%RowId%'";
    sqlitesync_DB.transaction(function (tx) {
        tx.executeSql(selectAllStatement, [], function (tx, result) {
            var datasetTables = result.rows;
            for(var tableIndex=0; tableIndex<datasetTables.length; tableIndex++){
                var item = datasetTables.item(tableIndex);
                if (item['tbl_name'].toString().toLowerCase() == "mergeidentity") {

                    tx.executeSql("update MergeIdentity set MergeUpdate=0 where MergeUpdate > 0;", [],function (transaction, result) {},function (transaction, error) {console.log(error);});
                }

                if (item['tbl_name'] != "MergeDelete" && item['tbl_name'] != "MergeIdentity") {
                    var selectForUpdateTable = "select *, ? as syncTableName from " + item['tbl_name'] + " where MergeUpdate > 0";
                    tx.executeSql(selectForUpdateTable, [item['tbl_name']], function (tx, result) {
                        if(result.rows.length > 0){
                            var syncTableName = result.rows.item(0)['syncTableName'];
                            var selectTriggerStatement = "select * from sqlite_master where type='trigger' and name = 'trMergeUpdate_"+syncTableName+"'";
                            tx.executeSql(selectTriggerStatement, [], function (tx, result) {
                                if(result.rows.length > 0){
                                    var syncTableName = result.rows.item(0)['tbl_name'];
                                    var trigger = result.rows.item(0)['sql'];

                                    tx.executeSql("drop trigger trMergeUpdate_"+syncTableName+";", [],function (transaction, result) {

                                    },
                                    function (transaction, error) {
                                    });
                                    tx.executeSql("update " + syncTableName + " set MergeUpdate=0 where MergeUpdate > 0;", [],function (transaction, result) {

                                    },
                                    function (transaction, error) {
                                    });
                                    tx.executeSql(trigger, [],function (transaction, result) {

                                    },
                                    function (transaction, error) {
                                    });
                                }
                            });
                        }
                    });
                    /***/
                }
            }
        });
    });
}

/*
 * After synchronization, we need to clear marker for deleted records to make sure that we will not send it again
 */
function sqlitesync_SyncClearDeletedRecords() {
    var selectAllStatement = "delete from MergeDelete";
    sqlitesync_DB.transaction(function (tx) {
        tx.executeSql(selectAllStatement, [], function (tx, result) {
        });
    });
}

/*
 * Preparing and sending records deleted on device
 */
function sqlitesync_SyncSendTableDelete(tx) {
    var selectForDelete = "select * from MergeDelete";
    tx.executeSql(selectForDelete, [], function (tx, result) {
        datasetTable = result.rows;
        sqlitesync_SyncDataToSend += "<delete>";
        if (datasetTable.length > 0) {
            sqlitesync_AddLog('Sending records for deleting');
        }
        for (var i = 0, row = null; i < datasetTable.length; i++) {
            row = datasetTable.item(i);
            sqlitesync_SyncDataToSend += "<r>";
            sqlitesync_SyncDataToSend += "<tb>" + row['TableId'] + "</tb>";
            sqlitesync_SyncDataToSend += "<id>" + row['RowId'] + "</id>";
            sqlitesync_SyncDataToSend += "</r>";
        }
        sqlitesync_SyncDataToSend += "</delete>";

        sqlitesync_SyncSendToServer();
    });
}

/*
 * Send packge with changes to server
 */
function sqlitesync_SyncSendToServer() {
    sqlitesync_SyncDataToSend += "</SyncData>";

    $.ajax({
        url: sqlitesync_SyncServerURL + "Send",
        method: 'POST',
        headers: {
                'Content-Type':'application/json'
        },
        cache : false,
        scope:this,
        data: JSON.stringify({ "subscriber" : sqlitesync_syncPdaIdent, "content": sqlitesync_SyncDataToSend , "version" : "3" }),
        dataType: 'json',
        timeout: 5 * 60 * 1000,//10min
        success: function (response, status) { //Success Callback

            sqlitesync_SyncClearUpdateMarker();
            sqlitesync_SyncClearDeletedRecords();

            sqlitesync_AddLog('Sending finished');
            //after success we are getting changes from server
            sqlitesync_SyncTables();
        },
        failure: function (result, request) {
            var statusCode = result.status;
            var responseText = result.responseText;
            sqlitesync_AddLog('<p>Error while syncing with the server ' + responseText + '</p>');
        }
    });
}

Updated by Tomek Dziemidowicz over 5 years ago · 2 revisions

Go to top