将多行捆绑为1个事务

时间:2013-11-21 20:14:09

标签: ajax sqlite

这里有很多代码,但我希望它是一个完整的例子。我已经努力使其成为通用的,以便其他人可以从中受益并从您的答案中受益。

我要做的是:

  1. 从iPad选择条形码ID
  2. FOR EACH Barcode,上传到服务器。
  3. FOR EACH Callback,更新iPad
  4. 我需要做的是以某种方式将所有行打包在一起作为单个事务,因为现在它太慢了。类似的东西:

    1. 从iPad选择条形码ID
    2. FOR EACH Barcode,连接JSON字符串
    3. 将JSON字符串上传到服务器。
    4. 开始交易
    5. 回调中的每个BarcodeID
    6. 提交交易
    7. var Variables = {};
      
      function SelectiPad(SQLTransaction) {
          var local = {};
      
          local.sqlite = "SELECT BarcodeID"
              + " FROM iPad"
              + " WHERE ServerSyncDateTime is null"
              + " ORDER BY BarcodeID";
          SQLTransaction.executeSql(
               local.sqlite
              ,[]
              ,SelectiPadSuccess
              ,SelectiPadFailure
          )
      };
      
      function SelectiPadSuccess(SQLTransaction,SQLResultSet) {
          Variables.iPadRecordcount = SQLResultSet.rows.length;
          for (var i=0; i < SQLResultSet.rows.length;i++) {
              UploadServer(SQLResultSet.rows.item(i));
          }
      };
      function UploadServer(arg) {
          var local = {};
      
          local.data = {};
          local.data.method = 'Save';
          local.type = 'post';
          local.data.BarcodeID = arg.BarcodeID;
      
          local.Promise = $.ajax('Server.cfc',local);
          local.Promise.done(UploadServerDone); // UPDATE Barcode set SyncDateTime
          local.Promise.fail(UploadServerFail);
      };
      function UploadServerDone(RESULT) {
          var local = {};
      
          local.ServerSyncDateTime = RESULT.QRY.DATA.SERVERSYNCDATETIME[0]; // UPPERCASE
          local.BarcodeID = RESULT.QRY.DATA.BARCODEID[0]; // UPPERCASE
          local.UpdateiPad = UpdateiPad.bind(this,local);
          dbo.transaction(local.UpdateiPad);
      }
      function UpdateiPad(arg,SQLTransaction) {
          var local = {};
      
          local.sqlite = "UPDATE iPad SET"
              + " ServerSyncDateTime=?1"
              + " WHERE BarcodeID=?2"
          SQLTransaction.executeSql(
              local.sqlite
              ,[
                   arg.ServerSyncDateTime // 1
                  ,arg.BarcodeID // 2
              ]
              ,UpdateiPadSuccess
              ,UpdateiPadFailure
          )
      }
      function UpdateiPadSuccess() {
      
          Variables.iPadRecordcount -=1;
          if (!Variables.iPadRecordcount) {
              Finished();
          }
      };
      function Finished() {
          alert('yay');
      }
      
      function SelectiPadFailure(SQLTransaction, SQLError) {
          $('.msg').text(SQLError.message);
          debugger;
      }
      
      function UploadServerFail(jqXHR, textStatus, errorThrown) {
          $('.msg').html(errorThrown);
          debugger;
      }
      
      function UpdateiPadFailure(SQLTransaction,SQLError) {
          $('.msg').html(SQLError.message);
          debugger;
      }
      

1 个答案:

答案 0 :(得分:0)

window.dbo = openDatabase('xxx','1.0','xxx', 10000);

(function() {
    var Variables = {};
    Variables.Recordcount = 500;

    dbo.transaction(DropTable);
    function DropTable(SQLTransaction) {
        var local = {};
        local.sqlite = "DROP TABLE IF EXISTS myTable";
        SQLTransaction.executeSql(
             local.sqlite
            ,[]
            ,DropTableSuccess
            ,DropTableFailure
        );
    }
    function DropTableSuccess() {
        dbo.transaction(CreateTable);
    }
    function DropTableFailure() {
        debugger;
    }

    function CreateTable(SQLTransaction) {
        var local = {};
        local.sqlite = "CREATE TABLE IF NOT EXISTS myTable"
            + "(myTableID Integer NOT NULL PRIMARY KEY AUTOINCREMENT"
            + ",SyncDateTime Varchar(128)"
            + ");"
        SQLTransaction.executeSql(
             local.sqlite
            ,[]
            ,CreateTableSuccess
            ,CreateTableFailure
        );
    }

    function CreateTableSuccess() {
        dbo.transaction(myTableTransaction);
    }
    function CreateTableFailure() {
        debugger;
    }

    function myTableTransaction(SQLTransaction) {
        var local = {};

        for (local.i=0; local.i < Variables.Recordcount; local.i++) {
            local.InsertSuccess = InsertSuccess.bind(this,local.i);
            local.sqlite = "INSERT INTO myTable(SyncDateTime) VALUES('')"
            SQLTransaction.executeSql(
             local.sqlite
            ,[]
            ,local.InsertSuccess
            ,InsertFailure
            );
        }
    }
    function InsertSuccess(argCurrentRow,SQLTransaction,SQLResultSet) {
        if (argCurrentRow+1 === Variables.Recordcount) {
            alert('Done!');
        }
    }
    function InsertFailure(A,B,C) {
        debugger;
    }
})();


    window.dbo = openDatabase('xxx','1.0','xxx', 10000);

    (function() {
        var Variables = {};

        dbo.transaction(SelectmyTable);
        function SelectmyTable(SQLTransaction) {
            var local = {};
            local.sqlite = "SELECT * FROM myTable ORDER BY myTableID";
            SQLTransaction.executeSql(
                 local.sqlite
                ,[]
                ,SelectSuccess
                ,SelectFailure
            );
        }
        function SelectSuccess(SQLTransaction,SQLResultSet) {
            var local = {};

            local.data = {};
            local.data.myTableIDs = '';
            Variables.Recordcount = SQLResultSet.rows.length;
            for (local.i = 0; local.i < SQLResultSet.rows.length; local.i++) {
                if (local.i) {
                    local.data.myTableIDs += ',';
                }
                local.data.myTableIDs += SQLResultSet.rows.item(local.i).myTableID;
            }

            local.data.method = 'Save';
            local.dataType = 'text'; // no return value.
            local.done = UploadDone.bind(this,local.data.myTableIDs);
            local.Promise = $.ajax('myTable.cfc',local);
            local.Promise.done(local.done);
            local.Promise.fail(UploadFail);
        };
        function UploadDone(argTableIDs,result,textStatus,jqXHR) {
            var local = {};

            if (result === '') {
                local.UpdateMyTable = UpdateMyTable.bind(this,argTableIDs);
                dbo.transaction(local.UpdateMyTable);
            } else {
                debugger;
            }
        }
        function UpdateMyTable(argTableIDs,SQLTransaction) {
            var local = {};

            local.arr = argTableIDs.split(',');
            local.myDate = yyyymmddhhnnssl();
            for (local.i=0; local.i < local.arr.length; local.i++) {
                local.UpdateSuccess = UpdateSuccess.bind(this,local.i);
                local.sqlite = "UPDATE myTable SET SyncDateTime=? WHERE myTableID=?";
                SQLTransaction.executeSql(
                     local.sqlite
                    ,[
                     local.myDate
                    ,local.arr[local.i]
                    ]
                    ,local.UpdateSuccess
                    ,UpdateFailure
                );
            }
        }
        function UpdateSuccess(argCounter,A,B,C) {
            if (argCounter+1 === Variables.Recordcount) {
                alert('done!');
            }
        }
        function SelectFailure(A,B,C) {
            debugger;
        };
        function UploadFail(A,B,C) {
            debugger;
        }
        function UpdateFailure(A,B,C) {
            debugger;
        }
    })();