Skip to content

Commit da6bb62

Browse files
authored
[PECO-1052] Added enum type class to DBSQLParameter class (#175)
* Added enum Signed-off-by: nithinkdb <nithin.krishnamurthi@databricks.com> * Done Signed-off-by: nithinkdb <nithin.krishnamurthi@databricks.com> --------- Signed-off-by: nithinkdb <nithin.krishnamurthi@databricks.com>
1 parent f60bc74 commit da6bb62

5 files changed

Lines changed: 74 additions & 19 deletions

File tree

lib/DBSQLParameter.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ import { TSparkParameter, TSparkParameterValue } from '../thrift/TCLIService_typ
33

44
export type DBSQLParameterValue = boolean | number | bigint | Int64 | Date | string;
55

6+
export enum DBSQLParameterType {
7+
STRING = 'STRING',
8+
DATE = 'DATE',
9+
TIMESTAMP = 'TIMESTAMP',
10+
FLOAT = 'FLOAT',
11+
DECIMAL = 'DECIMAL',
12+
DOUBLE = 'DOUBLE',
13+
INTEGER = 'INTEGER',
14+
BIGINT = 'BIGINT',
15+
SMALLINT = 'SMALLINT',
16+
TINYINT = 'TINYINT',
17+
BOOLEAN = 'BOOLEAN',
18+
INTERVALMONTH = 'INTERVAL MONTH',
19+
INTERVALDAY = 'INTERVAL DAY',
20+
}
21+
622
interface DBSQLParameterOptions {
7-
type?: string;
23+
type?: DBSQLParameterType;
824
value: DBSQLParameterValue;
925
}
1026

11-
export default class DBSQLParameter {
27+
export class DBSQLParameter {
1228
public readonly type?: string;
1329

1430
public readonly value: DBSQLParameterValue;
@@ -21,7 +37,7 @@ export default class DBSQLParameter {
2137
public toSparkParameter(): TSparkParameter {
2238
if (typeof this.value === 'boolean') {
2339
return new TSparkParameter({
24-
type: this.type ?? 'BOOLEAN',
40+
type: this.type ?? DBSQLParameterType.BOOLEAN,
2541
value: new TSparkParameterValue({
2642
stringValue: this.value ? 'TRUE' : 'FALSE',
2743
}),
@@ -30,7 +46,7 @@ export default class DBSQLParameter {
3046

3147
if (typeof this.value === 'number') {
3248
return new TSparkParameter({
33-
type: this.type ?? (Number.isInteger(this.value) ? 'INTEGER' : 'DOUBLE'),
49+
type: this.type ?? (Number.isInteger(this.value) ? DBSQLParameterType.INTEGER : DBSQLParameterType.DOUBLE),
3450
value: new TSparkParameterValue({
3551
stringValue: Number(this.value).toString(),
3652
}),
@@ -39,7 +55,7 @@ export default class DBSQLParameter {
3955

4056
if (this.value instanceof Int64 || typeof this.value === 'bigint') {
4157
return new TSparkParameter({
42-
type: this.type ?? 'BIGINT',
58+
type: this.type ?? DBSQLParameterType.BIGINT,
4359
value: new TSparkParameterValue({
4460
stringValue: this.value.toString(),
4561
}),
@@ -48,15 +64,15 @@ export default class DBSQLParameter {
4864

4965
if (this.value instanceof Date) {
5066
return new TSparkParameter({
51-
type: this.type ?? 'TIMESTAMP',
67+
type: this.type ?? DBSQLParameterType.TIMESTAMP,
5268
value: new TSparkParameterValue({
5369
stringValue: this.value.toISOString(),
5470
}),
5571
});
5672
}
5773

5874
return new TSparkParameter({
59-
type: this.type ?? 'STRING',
75+
type: this.type ?? DBSQLParameterType.STRING,
6076
value: new TSparkParameterValue({
6177
stringValue: this.value,
6278
}),

lib/DBSQLSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import CloseableCollection from './utils/CloseableCollection';
3030
import IDBSQLLogger, { LogLevel } from './contracts/IDBSQLLogger';
3131
import HiveDriverError from './errors/HiveDriverError';
3232
import globalConfig from './globalConfig';
33-
import DBSQLParameter, { DBSQLParameterValue } from './DBSQLParameter';
33+
import { DBSQLParameter, DBSQLParameterValue } from './DBSQLParameter';
3434

3535
const defaultMaxRows = 100000;
3636

lib/contracts/IDBSQLSession.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import IOperation from './IOperation';
22
import Status from '../dto/Status';
33
import InfoValue from '../dto/InfoValue';
44
import { Int64 } from '../hive/Types';
5-
import DBSQLParameter, { DBSQLParameterValue } from '../DBSQLParameter';
5+
import { DBSQLParameter, DBSQLParameterValue } from '../DBSQLParameter';
66

77
export type ExecuteStatementOptions = {
88
queryTimeout?: Int64;

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import TCLIService from '../thrift/TCLIService';
33
import TCLIService_types from '../thrift/TCLIService_types';
44
import DBSQLClient from './DBSQLClient';
55
import DBSQLSession from './DBSQLSession';
6-
import DBSQLParameter from './DBSQLParameter';
6+
import { DBSQLParameter } from './DBSQLParameter';
77
import DBSQLLogger from './DBSQLLogger';
88
import PlainHttpAuthentication from './connection/auth/PlainHttpAuthentication';
99
import HttpConnection from './connection/connections/HttpConnection';

tests/unit/DBSQLParameter.test.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,67 @@ const { expect } = require('chai');
22

33
const Int64 = require('node-int64');
44
const { TSparkParameterValue, TSparkParameter } = require('../../thrift/TCLIService_types');
5-
const { default: DBSQLParameter } = require('../../dist/DBSQLParameter');
5+
const { DBSQLParameter, DBSQLParameterType } = require('../../dist/DBSQLParameter');
66

77
describe('DBSQLParameter', () => {
88
it('should infer types correctly', () => {
99
const cases = [
10-
[false, new TSparkParameter({ type: 'BOOLEAN', value: new TSparkParameterValue({ stringValue: 'FALSE' }) })],
11-
[true, new TSparkParameter({ type: 'BOOLEAN', value: new TSparkParameterValue({ stringValue: 'TRUE' }) })],
12-
[123, new TSparkParameter({ type: 'INTEGER', value: new TSparkParameterValue({ stringValue: '123' }) })],
13-
[3.14, new TSparkParameter({ type: 'DOUBLE', value: new TSparkParameterValue({ stringValue: '3.14' }) })],
14-
[BigInt(1234), new TSparkParameter({ type: 'BIGINT', value: new TSparkParameterValue({ stringValue: '1234' }) })],
10+
[
11+
false,
12+
new TSparkParameter({
13+
type: DBSQLParameterType.BOOLEAN,
14+
value: new TSparkParameterValue({ stringValue: 'FALSE' }),
15+
}),
16+
],
17+
[
18+
true,
19+
new TSparkParameter({
20+
type: DBSQLParameterType.BOOLEAN,
21+
value: new TSparkParameterValue({ stringValue: 'TRUE' }),
22+
}),
23+
],
24+
[
25+
123,
26+
new TSparkParameter({
27+
type: DBSQLParameterType.INTEGER,
28+
value: new TSparkParameterValue({ stringValue: '123' }),
29+
}),
30+
],
31+
[
32+
3.14,
33+
new TSparkParameter({
34+
type: DBSQLParameterType.DOUBLE,
35+
value: new TSparkParameterValue({ stringValue: '3.14' }),
36+
}),
37+
],
38+
[
39+
BigInt(1234),
40+
new TSparkParameter({
41+
type: DBSQLParameterType.BIGINT,
42+
value: new TSparkParameterValue({ stringValue: '1234' }),
43+
}),
44+
],
1545
[
1646
new Int64(1234),
17-
new TSparkParameter({ type: 'BIGINT', value: new TSparkParameterValue({ stringValue: '1234' }) }),
47+
new TSparkParameter({
48+
type: DBSQLParameterType.BIGINT,
49+
value: new TSparkParameterValue({ stringValue: '1234' }),
50+
}),
1851
],
1952
[
2053
new Date('2023-09-06T03:14:27.843Z'),
2154
new TSparkParameter({
22-
type: 'TIMESTAMP',
55+
type: DBSQLParameterType.TIMESTAMP,
2356
value: new TSparkParameterValue({ stringValue: '2023-09-06T03:14:27.843Z' }),
2457
}),
2558
],
26-
['Hello', new TSparkParameter({ type: 'STRING', value: new TSparkParameterValue({ stringValue: 'Hello' }) })],
59+
[
60+
'Hello',
61+
new TSparkParameter({
62+
type: DBSQLParameterType.STRING,
63+
value: new TSparkParameterValue({ stringValue: 'Hello' }),
64+
}),
65+
],
2766
];
2867

2968
for (const [value, expectedParam] of cases) {

0 commit comments

Comments
 (0)