Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sql execute return promise api-877 #1046

Merged
merged 9 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ src/codec/*.ts
src/codec/custom/*.ts
src/.eslintrc.js
lib/**
docs/**
Copy link
Member Author

@srknzl srknzl Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed because of linting does eslint .. I forgot to add docs folder before

4 changes: 2 additions & 2 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2599,7 +2599,7 @@ await map.put('key1', 1);
await map.put('key2', 2);
await map.put('key3', 3);

const result = client.getSql().execute(`SELECT __key, this FROM my-distributed-map WHERE this > 1`);
const result = await client.getSql().execute(`SELECT __key, this FROM my-distributed-map WHERE this > 1`);

for await (const row of result) {
console.log(row); // {__key: 'key3', this: 3} and {__key: 'key2', this: 2}
Expand Down Expand Up @@ -2749,7 +2749,7 @@ comparable with `INTEGER`, the query needs a `CAST`. Note that, the cast can fai
an integer.

```javascript
const result = client.getSql().execute('SELECT * FROM myMap WHERE age > CAST(? AS INTEGER) AND age < CAST(? AS INTEGER)',
const result = await client.getSql().execute('SELECT * FROM myMap WHERE age > CAST(? AS INTEGER) AND age < CAST(? AS INTEGER)',
[13, 18]
);
```
Expand Down
5 changes: 2 additions & 3 deletions code_samples/bigdecimal_sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ function portableFactory(classId) {
'valueFormat' = 'decimal'
)
`;
// executions are async, await on update count to wait for execution.
await client.getSql().execute(createMappingQuery).getUpdateCount();
await client.getSql().execute(createMappingQuery);

// You can use BigDecimals for any operation
// Let's add some BigDecimals:
Expand All @@ -91,7 +90,7 @@ function portableFactory(classId) {

// You can run an SQL query with a BigDecimal:

const result = client.getSql().execute(
const result = await client.getSql().execute(
'SELECT * FROM decimalMap WHERE this > ?',
[BigDecimal.fromString('2.22222222222222222')]
);
Expand Down
5 changes: 2 additions & 3 deletions code_samples/datetime_classes_sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ function portableFactory(classId) {
'valueFormat' = 'timestamp with time zone'
)
`;
// executions are async, await on update count to wait for execution.
await client.getSql().execute(createMappingQuery).getUpdateCount();
await client.getSql().execute(createMappingQuery);

// You can use datetime classes for any operation
// Let's add some timestamp with timezones using `OffsetDatetime`:
Expand All @@ -105,7 +104,7 @@ function portableFactory(classId) {

// You can run an SQL query:

const result = client.getSql().execute('SELECT * FROM timestampWithTimezoneMap WHERE this > ?', [
const result = await client.getSql().execute('SELECT * FROM timestampWithTimezoneMap WHERE this > ?', [
OffsetDateTime.from(2020, 3, 1, 5, 6, 7, 123456789, 3600)
]);

Expand Down
64 changes: 22 additions & 42 deletions code_samples/sql-basic-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
'use strict';

const { Client, SqlColumnType, HazelcastSqlException } = require('hazelcast-client');
const { Client, SqlColumnType } = require('hazelcast-client');

(async () => {
try {
Expand All @@ -35,58 +35,38 @@ const { Client, SqlColumnType, HazelcastSqlException } = require('hazelcast-clie
'valueFormat' = 'double'
)
`;
// executions are async, await on update count to wait for execution.
await client.getSql().execute(createMappingQuery).getUpdateCount();
await client.getSql().execute(createMappingQuery);

await map.put('key1', 1);
await map.put('key2', 2);
await map.put('key3', 3);

let result;
try {
result = client.getSql().execute('SELECT __key, this FROM myMap WHERE this > ?', [1]);
const rowMetadata = await result.getRowMetadata();
const columns = await rowMetadata.getColumns();

console.log('Columns:');
for (const column of columns) {
console.log(`${column.name}: ${SqlColumnType[column.type]}`);
}
result = await client.getSql().execute('SELECT __key, this FROM myMap WHERE this > ?', [1]);
const rowMetadata = result.rowMetadata;
const columns = rowMetadata.getColumns();

console.log('Rows from query 1:');
for await (const row of result) {
// By default a row is a plain javascript object. Keys are column names and values are column values
console.log(`${row['__key']}: ${row['this']}`);
}
} catch (e) {
if (e instanceof HazelcastSqlException) {
// HazelcastSqlException is thrown if an error occurs during SQL execution.
console.log(`An SQL error occurred while running SQL: ${e}`);
} else {
// for all other errors
console.log(`An error occurred while running SQL: ${e}`);
}
console.log('Columns:');
for (const column of columns) {
console.log(`${column.name}: ${SqlColumnType[column.type]}`);
}

try {
// You can set returnRawResult to true to get rows as `SqlRow` objects
result = client.getSql().execute('SELECT __key, this FROM myMap WHERE this > ?', [1], {
returnRawResult: true
});
console.log('Rows from query 1:');
for await (const row of result) {
// By default a row is a plain javascript object. Keys are column names and values are column values
console.log(`${row['__key']}: ${row['this']}`);
}

// You can set returnRawResult to true to get rows as `SqlRow` objects
result = await client.getSql().execute('SELECT __key, this FROM myMap WHERE this > ?', [1], {
returnRawResult: true
});

console.log('Rows from query 2:');
for await (const row of result) {
console.log(`${row.getObject('__key')}: ${row.getObject('this')}`);
console.log(JSON.stringify(row.getMetadata().columns)); // SqlRow has a getter for row metadata
}
} catch (e) {
if (e instanceof HazelcastSqlException) {
// HazelcastSqlException is thrown if an error occurs during SQL execution.
console.log(`An SQL error occurred while running SQL: ${e}`);
} else {
// for all other errors
console.log(`An error occurred while running SQL: ${e}`);
}
console.log('Rows from query 2:');
for await (const row of result) {
console.log(`${row.getObject('__key')}: ${row.getObject('this')}`);
console.log(JSON.stringify(row.getMetadata().columns)); // SqlRow has a getter for row metadata
}

await client.shutdown();
Expand Down
111 changes: 39 additions & 72 deletions code_samples/sql-data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,15 @@ const varcharExample = async (client) => {
'valueFormat' = 'varchar'
)
`;
// executions are async, await on update count to wait for execution.
await client.getSql().execute(createMappingQuery).getUpdateCount();
await client.getSql().execute(createMappingQuery);

for (let key = 0; key < 10; key++) {
await someMap.set(key, key.toString());
}

try {
const result = client.getSql().execute('SELECT * FROM varcharMap WHERE this = ? OR this = ?', ['7', '2']);
const rowMetadata = await result.getRowMetadata();
const result = await client.getSql().execute('SELECT * FROM varcharMap WHERE this = ? OR this = ?', ['7', '2']);
const rowMetadata = result.rowMetadata;
const columnIndex = rowMetadata.findColumn('this');
const columnMetadata = rowMetadata.getColumn(columnIndex);
console.log(SqlColumnType[columnMetadata.type]); // VARCHAR
Expand Down Expand Up @@ -102,54 +101,33 @@ const integersExample = async (client) => {
'valueFormat' = 'bigint'
)
`;
// executions are async, await on update count to wait for execution.
await client.getSql().execute(createMappingQuery).getUpdateCount();
await client.getSql().execute(createMappingQuery);

for (let key = 0; key < 10; key++) {
await someMap.set(key, long.fromNumber(key * 2));
}

try {
const result = client.getSql().execute(
'SELECT * FROM bigintMap WHERE this > ? AND this < ?',
[long.fromNumber(10), long.fromNumber(18)]
);
const rowMetadata = await result.getRowMetadata();
const columnIndex = rowMetadata.findColumn('this');
const columnMetadata = rowMetadata.getColumn(columnIndex);
console.log(SqlColumnType[columnMetadata.type]); // BIGINT

for await (const row of result) {
console.log(row);
}
} catch (e) {
if (e instanceof HazelcastSqlException) {
// HazelcastSqlException is thrown if an error occurs during SQL execution.
console.log(`An SQL error occurred while running SQL: ${e}`);
} else {
// for all other errors
console.log(`An error occurred while running SQL: ${e}`);
}
const result = await client.getSql().execute(
'SELECT * FROM bigintMap WHERE this > ? AND this < ?',
[long.fromNumber(10), long.fromNumber(18)]
);
const rowMetadata = result.rowMetadata;
const columnIndex = rowMetadata.findColumn('this');
const columnMetadata = rowMetadata.getColumn(columnIndex);
console.log(SqlColumnType[columnMetadata.type]); // BIGINT

for await (const row of result) {
console.log(row);
}

try {
// Casting example. Casting to other integer types is also possible.
const result = client.getSql().execute(
'SELECT * FROM bigintMap WHERE this > CAST(? AS BIGINT) AND this < CAST(? AS BIGINT)',
[10, 18]
);
// Casting example. Casting to other integer types is also possible.
const result2 = await client.getSql().execute(
'SELECT * FROM bigintMap WHERE this > CAST(? AS BIGINT) AND this < CAST(? AS BIGINT)',
[10, 18]
);

for await (const row of result) {
console.log(row);
}
} catch (e) {
if (e instanceof HazelcastSqlException) {
// HazelcastSqlException is thrown if an error occurs during SQL execution.
console.log(`An SQL error occurred while running SQL: ${e}`);
} else {
// for all other errors
console.log(`An error occurred while running SQL: ${e}`);
}
for await (const row of result2) {
console.log(row);
}
};

Expand All @@ -173,39 +151,28 @@ const objectExample = async (client, classId, factoryId) => {
'valuePortableClassId' = '${classId}'
)
`;
// executions are async, await on update count to wait for execution.
await client.getSql().execute(createMappingQuery).getUpdateCount();
await client.getSql().execute(createMappingQuery);

for (let key = 0; key < 10; key++) {
await someMap.set(key, new Student(long.fromNumber(key), 1.1));
}

try {
// Note: If you do not specify `this` and use *, by default, `age` and `height` columns will be fetched
// instead of `this`.
// This is true only for complex custom objects like portable and identified serializable.
const result = client.getSql().execute(
'SELECT __key, this FROM studentMap WHERE age > CAST(? AS INTEGER) AND age < CAST(? AS INTEGER)',
[3, 8]
);

const rowMetadata = await result.getRowMetadata();
const columnIndex = rowMetadata.findColumn('this');
const columnMetadata = rowMetadata.getColumn(columnIndex);
console.log(SqlColumnType[columnMetadata.type]); // OBJECT

for await (const row of result) {
const student = row['this'];
console.log(student);
}
} catch (e) {
if (e instanceof HazelcastSqlException) {
// HazelcastSqlException is thrown if an error occurs during SQL execution.
console.log(`An SQL error occurred while running SQL: ${e}`);
} else {
// for all other errors
console.log(`An error occurred while running SQL: ${e}`);
}
// Note: If you do not specify `this` and use *, by default, `age` and `height` columns will be fetched
// instead of `this`.
// This is true only for complex custom objects like portable and identified serializable.
const result = await client.getSql().execute(
'SELECT __key, this FROM studentMap WHERE age > CAST(? AS INTEGER) AND age < CAST(? AS INTEGER)',
[3, 8]
);

const rowMetadata = result.rowMetadata;
const columnIndex = rowMetadata.findColumn('this');
const columnMetadata = rowMetadata.getColumn(columnIndex);
console.log(SqlColumnType[columnMetadata.type]); // OBJECT

for await (const row of result) {
const student = row['this'];
console.log(student);
}
};

Expand Down
43 changes: 11 additions & 32 deletions code_samples/sql-order-by-limit-offset-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
'use strict';

const { Client, HazelcastSqlException } = require('hazelcast-client');
const { Client } = require('hazelcast-client');

(async () => {
try {
Expand All @@ -34,8 +34,7 @@ const { Client, HazelcastSqlException } = require('hazelcast-client');
'valueFormat' = 'double'
)
`;
// executions are async, await on update count to wait for execution.
await client.getSql().execute(createMappingQuery).getUpdateCount();
await client.getSql().execute(createMappingQuery);

// populate map
await map.put('key1', 1);
Expand All @@ -44,21 +43,11 @@ const { Client, HazelcastSqlException } = require('hazelcast-client');
await map.put('key4', 4);
await map.put('key5', 5);

try {
const result = client.getSql().execute('SELECT * FROM myMap');
const result = await client.getSql().execute('SELECT * FROM myMap');

console.log('Rows from unsorted query:');
for await (const row of result) {
console.log(`${row['__key']}: ${row['this']}`);
}
} catch (e) {
if (e instanceof HazelcastSqlException) {
// HazelcastSqlException is thrown if an error occurs during SQL execution.
console.log(`An SQL error occurred while running SQL: ${e}`);
} else {
// for all other errors
console.log(`An error occurred while running SQL: ${e}`);
}
console.log('Rows from unsorted query:');
for await (const row of result) {
console.log(`${row['__key']}: ${row['this']}`);
}

// In order to add an index clear the map.
Expand All @@ -77,22 +66,12 @@ const { Client, HazelcastSqlException } = require('hazelcast-client');
await map.put('key4', 4);
await map.put('key5', 5);

try {
// Expected to see 2 3 4
const result = client.getSql().execute('SELECT * FROM myMap ORDER BY this ASC LIMIT 3 OFFSET 1');
// Expected to see 2 3 4
const result2 = await client.getSql().execute('SELECT * FROM myMap ORDER BY this ASC LIMIT 3 OFFSET 1');

console.log('Rows from sorted query with limit 3 and offset 1:');
for await (const row of result) {
console.log(`${row['__key']}: ${row['this']}`);
}
} catch (e) {
if (e instanceof HazelcastSqlException) {
// HazelcastSqlException is thrown if an error occurs during SQL execution.
console.log(`An SQL error occurred while running SQL: ${e}`);
} else {
// for all other errors
console.log(`An error occurred while running SQL: ${e}`);
}
console.log('Rows from sorted query with limit 3 and offset 1:');
for await (const row of result2) {
console.log(`${row['__key']}: ${row['this']}`);
}

await client.shutdown();
Expand Down
Loading