1 var DATA_TYPES = require('../../constants/DataTypes'),
  2   Helpers = require('../../utils/Helpers'),
  3   ErrorMessages = require('../../constants/ErrorMessages'),
  4   ColumnMetaData = require('../../resultset/ColumnMetaData'),
  5   CAS = require('../../constants/CASConstants');
  6 
  7 module.exports = GetSchemaPacket;
  8 
  9 /**
 10  * Constructor
 11  * @param options
 12  * @constructor
 13  */
 14 function GetSchemaPacket(options) {
 15   options = options || {};
 16 
 17   this.casInfo = options.casInfo;
 18   this.schemaType = options.schemaType;
 19 
 20   this.responseCode = 0;
 21   this.errorCode = 0;
 22   this.errorMsg = '';
 23 }
 24 
 25 /**
 26  * Write request schema data
 27  * @param writer
 28  */
 29 GetSchemaPacket.prototype.writeRequestSchema = function (writer) {
 30   var bufferLength = DATA_TYPES.DATA_LENGTH_SIZEOF + DATA_TYPES.CAS_INFO_SIZE +
 31     DATA_TYPES.BYTE_SIZEOF + 5 * DATA_TYPES.INT_SIZEOF + DATA_TYPES.BYTE_SIZEOF;
 32 
 33   writer._writeInt(bufferLength - DATA_TYPES.DATA_LENGTH_SIZEOF - DATA_TYPES.CAS_INFO_SIZE);
 34   writer._writeBytes(DATA_TYPES.CAS_INFO_SIZE, this.casInfo);
 35   writer._writeByte(CAS.CASFunctionCode.CAS_FC_SCHEMA_INFO);
 36   writer._writeInt(DATA_TYPES.INT_SIZEOF);
 37   writer._writeInt(this.schemaType);
 38   writer._writeInt(DATA_TYPES.BYTE_SIZEOF - 1);
 39   writer._writeInt(DATA_TYPES.BYTE_SIZEOF - 1);
 40   writer._writeInt(DATA_TYPES.BYTE_SIZEOF);
 41   writer._writeByte(1);
 42 
 43   return writer;
 44 };
 45 
 46 /**
 47  * Write fetch schema data
 48  * @param writer
 49  */
 50 GetSchemaPacket.prototype.writeFetchSchema = function (writer) {
 51   var bufferLength = DATA_TYPES.DATA_LENGTH_SIZEOF + DATA_TYPES.CAS_INFO_SIZE +
 52     DATA_TYPES.BYTE_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF +
 53     DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF +
 54     DATA_TYPES.INT_SIZEOF + DATA_TYPES.BYTE_SIZEOF + DATA_TYPES.INT_SIZEOF + DATA_TYPES.INT_SIZEOF;
 55 
 56   writer._writeInt(bufferLength - DATA_TYPES.DATA_LENGTH_SIZEOF - DATA_TYPES.CAS_INFO_SIZE);
 57   writer._writeBytes(DATA_TYPES.CAS_INFO_SIZE, this.casInfo);
 58 
 59   writer._writeByte(CAS.CASFunctionCode.CAS_FC_FETCH);
 60   writer._writeInt(DATA_TYPES.INT_SIZEOF); //int sizeof
 61   writer._writeInt(this.responseCode); //serverHandler
 62   writer._writeInt(DATA_TYPES.INT_SIZEOF); //int sizeof
 63   writer._writeInt(1); //Start position
 64   writer._writeInt(DATA_TYPES.INT_SIZEOF); //int sizeof
 65   writer._writeInt(100); //Fetch size; 0 = default; recommended = 100
 66   writer._writeInt(DATA_TYPES.BYTE_SIZEOF); //byte sizeof
 67   writer._writeByte(0); //Is case sensitive
 68   writer._writeInt(DATA_TYPES.INT_SIZEOF); //int sizeof
 69   writer._writeInt(0); //Is the ResultSet index...?
 70 
 71   return writer;
 72 };
 73 
 74 /**
 75  * Read request schema data
 76  * @param parser
 77  */
 78 GetSchemaPacket.prototype.parseRequestSchema = function (parser) {
 79   var reponseLength = parser._parseInt();
 80   this.casInfo = parser._parseBytes(DATA_TYPES.CAS_INFO_SIZE);
 81 
 82   this.responseCode = parser._parseInt();
 83   if (this.responseCode < 0) {
 84     this.errorCode = parser._parseInt();
 85     this.errorMsg = parser._parseNullTerminatedString(reponseLength - 2 * DATA_TYPES.INT_SIZEOF);
 86     if (this.errorMsg.length == 0) {
 87       this.errorMsg = Helpers._resolveErrorCode(this.errorCode);
 88     }
 89   } else {
 90     var resultTuple = parser._parseInt();
 91     var numColInfo = parser._parseInt();
 92     this.infoArray = new Array();
 93     for (var i = 0; i < numColInfo; i++) {
 94       var info = new ColumnMetaData();
 95       info.ColumnType = parser._parseByte();
 96       info.scale = parser._parseShort();
 97       info.precision = parser._parseInt();
 98       var len = parser._parseInt();
 99       info.Name = parser._parseNullTerminatedString(len);
100       this.infoArray[i] = info;
101     }
102   }
103 
104   return this;
105 };
106 
107 /**
108  * Read fetch schema data
109  * @param parser
110  */
111 GetSchemaPacket.prototype.parseFetchSchema = function (parser) {
112   var length = 0;
113   var responseLength = parser._parseInt();
114   this.casInfo = parser._parseBytes(DATA_TYPES.CAS_INFO_SIZE);
115 
116   this.responseCode = parser._parseInt();
117   if (this.responseCode !== 0) {
118     this.errorCode = parser._parseInt();
119     this.errorMsg = parser._parseNullTerminatedString(responseLength - 2 * DATA_TYPES.INT_SIZEOF);
120     if (this.errorMsg.length == 0) {
121       this.errorMsg = Helpers._resolveErrorCode(this.errorCode);
122     }
123   } else {
124     this.tupleCount = parser._parseInt();
125     var SchemaInfo = new Array(this.tupleCount);
126     for (var i = 0; i < this.tupleCount; i++) {
127       var index = parser._parseInt();
128       var Oid = parser._parseBytes(DATA_TYPES.OID_SIZEOF);
129       switch (this.schemaType) {
130         case CAS.CUBRIDSchemaType.CCI_SCH_CLASS:
131           length = parser._parseInt();
132           var name = parser._parseNullTerminatedString(length);
133           length = parser._parseInt();
134           var type = parser._parseShort();
135           SchemaInfo[i] = { Name : name, Type : type};
136           break;
137         case CAS.CUBRIDSchemaType.CCI_SCH_VCLASS:
138           length = parser._parseInt();
139           var name = parser._parseNullTerminatedString(length);
140           length = parser._parseInt();
141           var type = parser._parseShort();
142           SchemaInfo[i] = { Name : name, Type : type};
143           break;
144         case CAS.CUBRIDSchemaType.CCI_SCH_ATTRIBUTE:
145           length = parser._parseInt();
146           var name = parser._parseNullTerminatedString(length);
147           length = parser._parseInt();
148           var domain = parser._parseShort();
149           length = parser._parseInt();
150           var domain = parser._parseInt();
151           length = parser._parseInt();
152           var scale = parser._parseInt();
153           length = parser._parseInt();
154           var precision = parser._parseInt();
155           length = parser._parseInt();
156           var indexed = parser._parseInt();
157           length = parser._parseInt();
158           var non_null = parser._parseInt();
159           length = parser._parseInt();
160           var shared = parser._parseInt();
161           length = parser._parseInt();
162           var unique = parser._parseInt();
163           length = parser._parseInt();
164           var def = parser._parseInt();
165           length = parser._parseInt();
166           var attr_order = parser._parseInt();
167           length = parser._parseInt();
168           var class_name = parser._parseNullTerminatedString(length);
169           length = parser._parseInt();
170           var source_class = parser._parseNullTerminatedString(length);
171           length = parser._parseInt();
172           var is_key = parser._parseShort();
173           SchemaInfo[i] = { Name : name, Domain : domain, Scale : scale, Precision : precision,
174             Indexed              : index, NonNull : non_null, Shared : shared, Unique : unique, Default : def,
175             AttrOrder            : attr_order, ClassName : class_name, SourceClass : source_class, IsKey : is_key
176           };
177           break;
178       }
179     }
180     return SchemaInfo;
181   }
182 };
183