Skip to content

Commit

Permalink
Merge pull request #339 from dliocode/feat/add-type-int128-fb4
Browse files Browse the repository at this point in the history
feat: Add type Int128 - FB4
  • Loading branch information
mariuz authored Jul 4, 2024
2 parents 882a200 + 9237c48 commit 1c46627
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/wire/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ function describe(buff, statement) {
case Const.SQL_LONG: param = new Xsql.SQLVarInt(); break;
case Const.SQL_SHORT: param = new Xsql.SQLVarShort(); break;
case Const.SQL_INT64: param = new Xsql.SQLVarInt64(); break;
case Const.SQL_INT128: param = new Xsql.SQLVarInt128(); break;
case Const.SQL_BOOLEAN: param = new Xsql.SQLVarBoolean(); break;
default:
throw new Error('Unexpected');
Expand Down Expand Up @@ -1355,6 +1356,9 @@ Connection.prototype.executeStatement = function(transaction, statement, params,

default:
switch (typeof value) {
case 'bigint':
ret[i] = new Xsql.SQLParamInt128(value);
break;
case 'number':
if (value % 1 === 0) {
if (value >= Const.MIN_INT && value <= Const.MAX_INT)
Expand Down
2 changes: 2 additions & 0 deletions lib/wire/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ const sqlType = {
SQL_TYPE_TIME : 560,
SQL_TYPE_DATE : 570,
SQL_INT64 : 580,
SQL_INT128: 32752, // >= 4.0
SQL_BOOLEAN : 32764, // >: 3.0
SQL_NULL : 32766, // >= 2.5
};
Expand Down Expand Up @@ -263,6 +264,7 @@ const blr = {
blr_sql_date : 12,
blr_sql_time : 13,
blr_int64 : 16,
blr_int128 : 26, // >: 4.0
blr_blob2 : 17, // >: 2.0
blr_domain_name : 18, // >: 2.1
blr_domain_name2 : 19, // >: 2.1
Expand Down
24 changes: 24 additions & 0 deletions lib/wire/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ XdrWriter.prototype.addInt64 = function (value) {
this.pos += 4;
};

XdrWriter.prototype.addInt128 = function (value) {
this.ensure(16);

const bigValue = BigInt(value);

const high = bigValue >> BigInt(64);
const low = bigValue & BigInt("0xFFFFFFFFFFFFFFFF");

this.buffer.writeBigUInt64BE(high, this.pos);
this.pos += 8;
this.buffer.writeBigUInt64BE(low, this.pos);
this.pos += 8;
};

XdrWriter.prototype.addUInt = function (value) {
this.ensure(4);
this.buffer.writeUInt32BE(value, this.pos);
Expand Down Expand Up @@ -365,6 +379,16 @@ XdrReader.prototype.readInt64 = function () {
return new Long(low, high).toNumber();
};

XdrReader.prototype.readInt128 = function () {
var high = this.buffer.readBigUInt64BE(this.pos)
this.pos += 8

var low = this.buffer.readBigUInt64BE(this.pos)
this.pos += 8

return (BigInt(high) << BigInt(64)) + BigInt(low)
};

XdrReader.prototype.readShort = function () {
var r = this.buffer.readInt16BE(this.pos);
this.pos += 2;
Expand Down
57 changes: 56 additions & 1 deletion lib/wire/xsqlvar.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,40 @@ SQLVarInt64.prototype.calcBlr = function(blr) {

//------------------------------------------------------

function SQLVarFloat() {}
function SQLVarInt128() {}

SQLVarInt128.prototype.decode = function (data, lowerV13) {
var retBigInt = BigInt(data.readInt128())

if (retBigInt > BigInt(Number.MAX_SAFE_INTEGER)) {
var ret = retBigInt.toString();

var integerPart = ret.slice(0, Math.abs(this.scale) * -1)
var decimalPart = ret.slice(Math.abs(this.scale) * -1)

if (integerPart === '') integerPart = '0'

ret = `${integerPart}.${decimalPart}`
} else {
var ret = Number(retBigInt);
ret = ret / ScaleDivisor[Math.abs(this.scale)];
}

if (!lowerV13 || !data.readInt()) {
return ret;
}

return null;
};

SQLVarInt128.prototype.calcBlr = function (blr) {
blr.addByte(Const.blr_int128);
blr.addShort(this.scale);
};

//------------------------------------------------------

function SQLVarFloat() { }

SQLVarFloat.prototype.decode = function(data, lowerV13) {
var ret = data.readFloat();
Expand Down Expand Up @@ -326,6 +359,26 @@ SQLParamInt64.prototype.encode = function(data) {

//------------------------------------------------------

function SQLParamInt128(value) {
this.value = value;
}

SQLParamInt128.prototype.calcBlr = function (blr) {
blr.addByte(Const.blr_int128);
blr.addShort(0);
};

SQLParamInt128.prototype.encode = function (data) {
if (this.value != null) {
data.addInt128(this.value);
} else {
data.addInt128(0);
data.addInt(1);
}
};

//------------------------------------------------------

function SQLParamDouble(value) {
this.value = value;
}
Expand Down Expand Up @@ -446,6 +499,7 @@ module.exports = {
SQLVarDouble,
SQLVarInt,
SQLVarInt64,
SQLVarInt128,
SQLVarFloat,
SQLVarNull,
SQLVarShort,
Expand All @@ -458,6 +512,7 @@ module.exports = {
SQLParamDouble,
SQLParamInt,
SQLParamInt64,
SQLParamInt128,
SQLParamQuad,
SQLParamString,
};

0 comments on commit 1c46627

Please sign in to comment.