1 var DATA_TYPES = require('../../constants/DataTypes'),
  2   Helpers = require('../../utils/Helpers'),
  3   ErrorMessages = require('../../constants/ErrorMessages'),
  4   CAS = require('../../constants/CASConstants');
  5 
  6 module.exports = FetchPacket;
  7 
  8 /**
  9  * Constructor
 10  * @param options
 11  * @constructor
 12  */
 13 function FetchPacket(options) {
 14   this.casInfo = options.casInfo;
 15 
 16   this.responseCode = 0;
 17   this.errorCode = 0;
 18   this.errorMsg = '';
 19 }
 20 
 21 /**
 22  * Write data
 23  * @param writer
 24  * @param queryPacket
 25  */
 26 FetchPacket.prototype.write = function (writer, queryPacket) {
 27   var bufferLength = DATA_TYPES.DATA_LENGTH_SIZEOF + DATA_TYPES.CAS_INFO_SIZE +
 28     DATA_TYPES.BYTE_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF +
 29     DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF +
 30     DATA_TYPES.INT_SIZEOF + DATA_TYPES.BYTE_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF;
 31 
 32   writer._writeInt(bufferLength - DATA_TYPES.DATA_LENGTH_SIZEOF - DATA_TYPES.CAS_INFO_SIZE);
 33   writer._writeBytes(DATA_TYPES.CAS_INFO_SIZE, this.casInfo);
 34 
 35   writer._writeByte(CAS.CASFunctionCode.CAS_FC_FETCH);
 36   writer._writeInt(DATA_TYPES.INT_SIZEOF); //int sizeof
 37   writer._writeInt(queryPacket.handle); //serverHandler
 38   writer._writeInt(DATA_TYPES.INT_SIZEOF); //int sizeof
 39   writer._writeInt(queryPacket.currentTupleCount + 1); //Start position (= current cursor position + 1)
 40   writer._writeInt(DATA_TYPES.INT_SIZEOF); //int sizeof
 41   writer._writeInt(100); //Fetch size; 0 = default; recommended = 100
 42   writer._writeInt(DATA_TYPES.BYTE_SIZEOF); //byte sizeof
 43   writer._writeByte(0); //Is case sensitive
 44   writer._writeInt(DATA_TYPES.INT_SIZEOF); //int sizeof
 45   writer._writeInt(0); //Is the ResultSet index...?
 46 
 47   return writer;
 48 };
 49 
 50 /**
 51  * Read data
 52  * @param parser
 53  * @param queryPacket
 54  */
 55 FetchPacket.prototype.parse = function (parser, queryPacket) {
 56   var responseLength = 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(responseLength - 2 * DATA_TYPES.INT_SIZEOF);
 63     if (this.errorMsg.length == 0) {
 64       this.errorMsg = Helpers._resolveErrorCode(this.errorCode);
 65     }
 66   } else {
 67     this.tupleCount = parser._parseInt();
 68     return JSON.stringify({ColumnValues : queryPacket._getData(parser, this.tupleCount)});
 69   }
 70 };
 71 
 72 
 73