/** * Encode Int 64 * @param value * @returns {Buffer} */ function put64 (value) { return Buffer.from(Long.fromString(value.toString()).toBytesLE()) }
it('decodes Integer64', function() { expect(types.decode('Integer64', new Buffer('8000000180000002', 'hex'))) .toEqual(Long.fromString('8000000180000002', false, 16)); });
function winTickToMs(num) { const l = Long.fromString(`${num}`); if (l.compare(0) === 0) return 0; return ( l .divide(10000) .subtract(windowsTickEpoch) .toString() * 1 ); }
function createLongStr(str) { const buf = Buffer.alloc(8); const l = Long.fromString(`${str}`); buf.writeInt32LE(l.getLowBits(), 0); buf.writeInt32LE(l.getHighBits(), 4); return buf; }
it('int escape', function (done) { const buf = 19925; assert.equal(shareConn.escape(buf), '19925'); const maxValue = Long.fromString('18446744073709551615', true); assert.equal(shareConn.escape(maxValue), '18446744073709551615'); shareConn .query(' SELECT ' + shareConn.escape(buf) + ' t') .then((rows) => { assert.deepEqual(rows, [{ t: buf }]); done(); }) .catch(done); });
const checkJSBILong = function (data, signed) { const { MAX_INT64, MIN_INT64, MAX_UINT64, MIN_UINT64 } = _getJSBIConstants(); data = JSBI.BigInt(data.toString()); if (signed) { if (JSBI.greaterThan(data, MAX_INT64)) { throw new Error('data was out of range (greater than max int64)'); } else if (JSBI.lessThan(data, MIN_INT64)) { throw new Error('data was out of range (less than min int64)'); } } else { if (JSBI.greaterThan(data, MAX_UINT64)) { throw new Error('data was out of range (greater than max uint64)'); } else if (JSBI.lessThan(data, MIN_UINT64)) { throw new Error('data was out of range (less than min uint64)'); } } return Long.fromString(data.toString(), true); }
readLongLengthEncoded(supportBigNumbers, bigNumberStrings, unsigned) { const len = this.readUnsignedLength(); if (len === null) return null; let result = 0; let negate = false; let begin = this.pos; //minus sign if (len > 0 && this.buf[begin] === 45) { negate = true; begin++; } for (; begin < this.pos + len; begin++) { result = result * 10 + (this.buf[begin] - 48); } let val = negate ? -1 * result : result; this.pos += len; if (!Number.isSafeInteger(val)) { const str = this.buf.toString('ascii', this.pos - len, this.pos); if (bigNumberStrings) return str; if (supportBigNumbers) { return Long.fromString(str, unsigned, 10); } } return val; }
it('using very big number', function (done) { const maxValue = Long.fromString('18446744073709551615', true); base.createConnection({ supportBigNumbers: true }).then((conn) => { conn.query('CREATE TEMPORARY TABLE BIG_NUMBER (val BIGINT unsigned)'); conn .query('INSERT INTO BIG_NUMBER values (?), (?)', [10, maxValue]) .then(() => { return conn.query('SELECT * FROM BIG_NUMBER LIMIT ?', [maxValue]); }) .then((res) => { assert.deepEqual(res, [{ val: 10 }, { val: maxValue }]); conn.end(); done(); }) .catch(done); }); });
const checkBigIntLong = function (data, signed) { const { MAX_INT64, MIN_INT64, MAX_UINT64, MIN_UINT64 } = _getBigIntConstants(); if (typeof data !== 'bigint') { data = BigInt(data.toString()); } if (signed) { if (data > MAX_INT64) { throw new Error('data was out of range (greater than max int64)'); } else if (data < MIN_INT64) { throw new Error('data was out of range (less than min int64)'); } } else { if (data > MAX_UINT64) { throw new Error('data was out of range (greater than max uint64)'); } else if (data < MIN_UINT64) { throw new Error('data was out of range (less than min uint64)'); } } return Long.fromString(data.toString(), true); }
it('encodes Integer64', function() { expect(types.encode('Integer64', Long.fromString('8000000180000002', false, 16)).toString('hex')) .toBe('8000000180000002'); });
it('encodes Unsigned64', function() { expect(types.encode('Unsigned64', Long.fromString('8000000180000002', true, 16)).toString('hex')) .toBe('8000000180000002'); });
it('decodes Unsigned64', function() { expect(types.decode('Unsigned64', new Buffer('8000000180000002', 'hex'))) .toEqual(Long.fromString('8000000180000002', true, 16)); });