1 var DATA_TYPES = require('../../constants/DataTypes'),
  2   Helpers = require('../../utils/Helpers'),
  3   CAS = require('../../constants/CASConstants');
  4 
  5 module.exports = BatchExecuteNoQueryPacket;
  6 
  7 /**
  8  * Constructor
  9  * @constructor
 10  */
 11 function BatchExecuteNoQueryPacket(options) {
 12   options = options || {};
 13 
 14   this.SQLs = options.SQLs;
 15   this.casInfo = options.casInfo;
 16   this.autoCommit = options.autoCommitMode;
 17 
 18   this.responseCode = 0;
 19   this.errorCode = 0;
 20   this.errorMsg = '';
 21 
 22   this.arrResultsCode = [];
 23   this.arrResultsMsg = [];
 24 }
 25 
 26 /**
 27  * Write data
 28  * @param writer
 29  */
 30 BatchExecuteNoQueryPacket.prototype.write = function (writer) {
 31   var statementsLength = 0;
 32   for (var i = 0; i < this.SQLs.length; i++) {
 33     statementsLength += DATA_TYPES.INT_SIZEOF;
 34     statementsLength += (Buffer.byteLength(this.SQLs[i]) + 1);
 35   }
 36   var bufferLength = DATA_TYPES.DATA_LENGTH_SIZEOF + DATA_TYPES.CAS_INFO_SIZE +
 37     DATA_TYPES.BYTE_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.BYTE_SIZEOF + statementsLength;
 38 
 39   writer._writeInt(bufferLength - DATA_TYPES.DATA_LENGTH_SIZEOF - DATA_TYPES.CAS_INFO_SIZE);
 40   writer._writeBytes(DATA_TYPES.CAS_INFO_SIZE, this.casInfo);
 41   writer._writeByte(CAS.CASFunctionCode.CAS_FC_EXECUTE_BATCH);
 42   writer._writeInt(DATA_TYPES.BYTE_SIZEOF);
 43   writer._writeByte(this.autoCommit ? 1 : 0); //Autocommit
 44   for (var j = 0; j < this.SQLs.length; j++) {
 45     writer._writeNullTerminatedString(this.SQLs[j]);
 46   }
 47 
 48   return writer;
 49 };
 50 
 51 /**
 52  * Read data
 53  * @param parser
 54  */
 55 BatchExecuteNoQueryPacket.prototype.parse = function (parser) {
 56   var reponseLength = parser._parseInt();
 57   this.casInfo = parser._parseBytes(DATA_TYPES.CAS_INFO_SIZE);
 58 
 59   this.responseCode = parser._parseInt();
 60   if (this.responseCode < 0) {
 61     this.errorCode = parser._parseInt();
 62     this.errorMsg = parser._parseNullTerminatedString(reponseLength - 2 * DATA_TYPES.INT_SIZEOF);
 63     if (this.errorMsg.length == 0) {
 64       this.errorMsg = Helpers._resolveErrorCode(this.errorCode);
 65     }
 66   } else {
 67     this.executedCount = parser._parseInt();
 68     for (var i = 0; i < this.executedCount; i++) {
 69       parser._parseByte(); //not used
 70       var result = parser._parseInt();
 71       if (result < 0) {
 72         var errMsgLength = parser._parseInt();
 73         var errMsg = parser._parseString(errMsgLength);
 74         this.arrResultsCode.push(result);
 75         this.arrResultsMsg.push(errMsg);
 76       } else {
 77         this.arrResultsCode.push(result);
 78         this.arrResultsMsg.push('');
 79         parser._parseInt(); //not used
 80         parser._parseShort(); //not used
 81         parser._parseShort(); //not used
 82       }
 83     }
 84   }
 85 
 86   return this;
 87 };
 88 
 89