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 4 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
7 changes: 3 additions & 4 deletions code_samples/sql-basic-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ 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]);
result = await client.getSql().execute('SELECT __key, this FROM myMap WHERE this > ?', [1]);
const rowMetadata = await result.getRowMetadata();
const columns = await rowMetadata.getColumns();

Expand All @@ -70,7 +69,7 @@ const { Client, SqlColumnType, HazelcastSqlException } = require('hazelcast-clie

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], {
result = await client.getSql().execute('SELECT __key, this FROM myMap WHERE this > ?', [1], {
returnRawResult: true
});

Expand Down
17 changes: 7 additions & 10 deletions code_samples/sql-data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ 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 result = await client.getSql().execute('SELECT * FROM varcharMap WHERE this = ? OR this = ?', ['7', '2']);
const rowMetadata = await result.getRowMetadata();
srknzl marked this conversation as resolved.
Show resolved Hide resolved
const columnIndex = rowMetadata.findColumn('this');
const columnMetadata = rowMetadata.getColumn(columnIndex);
Expand Down Expand Up @@ -102,15 +101,14 @@ 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(
const result = await client.getSql().execute(
'SELECT * FROM bigintMap WHERE this > ? AND this < ?',
[long.fromNumber(10), long.fromNumber(18)]
);
Expand All @@ -134,7 +132,7 @@ const integersExample = async (client) => {

try {
// Casting example. Casting to other integer types is also possible.
const result = client.getSql().execute(
const result = await client.getSql().execute(
'SELECT * FROM bigintMap WHERE this > CAST(? AS BIGINT) AND this < CAST(? AS BIGINT)',
[10, 18]
);
Expand Down Expand Up @@ -173,8 +171,7 @@ 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));
Expand All @@ -184,7 +181,7 @@ const objectExample = async (client, classId, factoryId) => {
// 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(
const result = await client.getSql().execute(
'SELECT __key, this FROM studentMap WHERE age > CAST(? AS INTEGER) AND age < CAST(? AS INTEGER)',
[3, 8]
);
Expand Down
7 changes: 3 additions & 4 deletions code_samples/sql-order-by-limit-offset-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -45,7 +44,7 @@ const { Client, HazelcastSqlException } = require('hazelcast-client');
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) {
Expand Down Expand Up @@ -79,7 +78,7 @@ const { Client, HazelcastSqlException } = require('hazelcast-client');

try {
// Expected to see 2 3 4
const result = client.getSql().execute('SELECT * FROM myMap ORDER BY this ASC LIMIT 3 OFFSET 1');
const result = 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) {
Expand Down
25 changes: 11 additions & 14 deletions src/sql/SqlService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,24 @@ import {SqlPage} from './SqlPage';
*/
export interface SqlService {
/**
* Executes SQL and returns an SqlResult.
* Executes SQL and returns an {@link SqlResult}.
srknzl marked this conversation as resolved.
Show resolved Hide resolved
* Converts passed SQL string and parameter values into an {@link SqlStatement} object and invokes {@link executeStatement}.
*
* @param sql SQL string. SQL string placeholder character is question mark(`?`)
* @param params Parameter list. The parameter count must be equal to number of placeholders in the SQL string
* @param options Options that are affecting how the query is executed
* @throws {@link IllegalArgumentError} if arguments are not valid
* @throws {@link HazelcastSqlException} in case of an execution error
* @returns {@link SqlResult}
*/
execute(sql: string, params?: any[], options?: SqlStatementOptions): SqlResult;
execute(sql: string, params?: any[], options?: SqlStatementOptions): Promise<SqlResult>;

/**
* Executes SQL and returns an SqlResult.
* Executes SQL and returns an {@link SqlResult}.
* @param sql SQL statement object
* @throws {@link IllegalArgumentError} if arguments are not valid
* @throws {@link HazelcastSqlException} in case of an execution error
* @returns {@link SqlResult}
*/
executeStatement(sql: SqlStatement): SqlResult;
executeStatement(sql: SqlStatement): Promise<SqlResult>;
}

/** @internal */
Expand Down Expand Up @@ -284,7 +282,7 @@ export class SqlServiceImpl implements SqlService {
);
}

executeStatement(sqlStatement: SqlStatement): SqlResult {
executeStatement(sqlStatement: SqlStatement): Promise<SqlResult> {
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
try {
SqlServiceImpl.validateSqlStatement(sqlStatement);
} catch (error) {
Expand Down Expand Up @@ -359,21 +357,20 @@ export class SqlServiceImpl implements SqlService {
this.connectionManager.getClientUuid()
);

this.invocationService.invokeOnConnection(connection, requestMessage).then(clientMessage => {
return this.invocationService.invokeOnConnection(connection, requestMessage).then(clientMessage => {
SqlServiceImpl.handleExecuteResponse(clientMessage, res);
return res;
}).catch(err => {
res.onExecuteError(
this.rethrow(err, connection)
);
const error = this.rethrow(err, connection);
srknzl marked this conversation as resolved.
Show resolved Hide resolved
res.onExecuteError(error);
throw error;
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
});

return res;
} catch (error) {
throw this.rethrow(error, connection);
}
}

execute(sql: string, params?: any[], options?: SqlStatementOptions): SqlResult {
execute(sql: string, params?: any[], options?: SqlStatementOptions): Promise<SqlResult> {
const sqlStatement: SqlStatement = {
sql: sql
};
Expand Down
2 changes: 1 addition & 1 deletion test/TestUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ exports.createMapping = async (serverVersionNewerThanFive, client, keyFormat, va
)
`;

const result = exports.getSql(client).execute(createMappingQuery);
const result = await exports.getSql(client).execute(createMappingQuery);
// Wait for execution to end.
await result.getUpdateCount();
};
Loading