Skip to content

Commit 9715c77

Browse files
committed
Add support for advanced decoding CQL BigInt
Allows to decode CQL BigInt into either BigInt type or Long type, according to provided configuration option. This still requires the configuration option to be correctly passed from the option provided by the client into the rust part of the code.
1 parent aa0d16c commit 9715c77

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

lib/types/result-set.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class ResultSet {
8181
}
8282
return;
8383
}
84+
85+
let decodingOptions = result.getDecodingOptions();
8486
/**
8587
* Gets an array rows returned by the query.
8688
* When the result set represents a response from a write query, this property will be <code>undefined</code>.
@@ -89,7 +91,10 @@ class ResultSet {
8991
* following pages of results.
9092
* @type {Array<Row>|undefined}
9193
*/
92-
this.rows = resultsWrapper.getRowsFromResultsWrapper(result);
94+
this.rows = resultsWrapper.getRowsFromResultsWrapper(
95+
result,
96+
decodingOptions,
97+
);
9398

9499
/**
95100
* Gets the row length of the result, regardless if the result has been buffered or not

lib/types/results-wrapper.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ const Row = require("./row");
1313
/**
1414
* Checks the type of value wrapper object and gets it from the underlying value
1515
* @param {rust.CqlValueWrapper} field
16+
* @param {rust.DecodingOptions | null} decodingOptions
1617
* @returns {any}
1718
*/
18-
function getCqlObject(field) {
19+
function getCqlObject(field, decodingOptions) {
1920
if (field == null) return null;
2021
let type = field.getType();
2122
let res, fields;
2223
switch (type) {
2324
case rust.CqlType.Ascii:
2425
return field.getAscii();
2526
case rust.CqlType.BigInt:
27+
if (decodingOptions && decodingOptions.useBigIntAsLong) {
28+
return field.getBigint();
29+
}
2630
return bigintToLong(field.getBigint());
2731
case rust.CqlType.Blob:
2832
return field.getBlob();
@@ -87,9 +91,10 @@ function getCqlObject(field) {
8791
* Simple way of getting results from rust driver.
8892
* Call the driver O(columns * rows) times
8993
* @param {rust.QueryResultWrapper} result
94+
* @param {rust.DecodingOptions} decodingOptions
9095
* @returns {Array<Row> | undefined} Returns array of rows if ResultWrapper has any, and undefined otherwise
9196
*/
92-
function getRowsFromResultsWrapper(result) {
97+
function getRowsFromResultsWrapper(result, decodingOptions) {
9398
let rustRows = result.getRows();
9499
if (rustRows == null) {
95100
// Empty results are treated as undefined
@@ -105,7 +110,7 @@ function getRowsFromResultsWrapper(result) {
105110
for (let j = 0; j < cols.length; j++) {
106111
// By default driver returns row as a map:
107112
// column name -> column value
108-
collectedRow[colNames[j]] = getCqlObject(cols[j]);
113+
collectedRow[colNames[j]] = getCqlObject(cols[j], decodingOptions);
109114
}
110115
rows.push(collectedRow);
111116
}

src/result.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@ enum QueryResultVariant {
2929
RowsResult(QueryRowsResult),
3030
}
3131

32+
#[napi]
33+
#[derive(Clone)]
34+
pub struct EncodingOptions {
35+
pub use_big_int_as_long: bool,
36+
pub use_big_int_as_varint: bool,
37+
}
38+
3239
#[napi]
3340
pub struct QueryResultWrapper {
3441
internal: QueryResultVariant,
42+
decoding_options: EncodingOptions,
3543
}
3644

3745
#[napi]
@@ -62,7 +70,14 @@ impl QueryResultWrapper {
6270
return Err(err_to_napi(e));
6371
}
6472
};
65-
Ok(QueryResultWrapper { internal: value })
73+
let empty_decoding_options = EncodingOptions {
74+
use_big_int_as_long: false,
75+
use_big_int_as_varint: false,
76+
};
77+
Ok(QueryResultWrapper {
78+
internal: value,
79+
decoding_options: empty_decoding_options,
80+
})
6681
}
6782

6883
#[napi]
@@ -134,6 +149,11 @@ impl QueryResultWrapper {
134149
QueryResultVariant::EmptyResult(v) => v.tracing_id().map(UuidWrapper::from_cql_uuid),
135150
}
136151
}
152+
153+
#[napi]
154+
pub fn get_decoding_options(&self) -> EncodingOptions {
155+
self.decoding_options.clone()
156+
}
137157
}
138158

139159
#[napi]

test/unit/cql-value-wrapper-tests.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ describe("Cql value wrapper", function () {
3434
assert.strictEqual(Long.fromString("69").equals(value), true);
3535
});
3636

37+
it("should get bigInt type correctly from napi, as bigint", function () {
38+
let element = rust.testsGetCqlWrapperBigint();
39+
let type = element.getType();
40+
assert.strictEqual(type, rust.CqlType.BigInt);
41+
let value = getCqlObject(element, { useBigIntAsLong: true });
42+
/* Corresponding value:
43+
let element = CqlValue::BigInt(69); */
44+
assert.strictEqual(BigInt(69), value);
45+
});
46+
3747
it("should get boolean type correctly from napi", function () {
3848
let element = rust.testsGetCqlWrapperBoolean();
3949
let type = element.getType();

0 commit comments

Comments
 (0)