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

Refine DBSQLParameter class #190

Merged
merged 1 commit into from
Sep 30, 2023
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
15 changes: 12 additions & 3 deletions lib/DBSQLParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ interface DBSQLParameterOptions {
value: DBSQLParameterValue;
}

interface ToSparkParameterOptions {
name?: string;
}

export class DBSQLParameter {
public readonly type?: string;

Expand All @@ -35,19 +39,20 @@ export class DBSQLParameter {
this.value = value;
}

public toSparkParameter(): TSparkParameter {
public toSparkParameter({ name }: ToSparkParameterOptions = {}): TSparkParameter {
// If VOID type was set explicitly - ignore value
if (this.type === DBSQLParameterType.VOID) {
return new TSparkParameter(); // for NULL neither `type` nor `value` should be set
return new TSparkParameter({ name }); // for NULL neither `type` nor `value` should be set
}

// Infer NULL values
if (this.value === undefined || this.value === null) {
return new TSparkParameter(); // for NULL neither `type` nor `value` should be set
return new TSparkParameter({ name }); // for NULL neither `type` nor `value` should be set
}

if (typeof this.value === 'boolean') {
return new TSparkParameter({
name,
type: this.type ?? DBSQLParameterType.BOOLEAN,
value: new TSparkParameterValue({
stringValue: this.value ? 'TRUE' : 'FALSE',
Expand All @@ -57,6 +62,7 @@ export class DBSQLParameter {

if (typeof this.value === 'number') {
return new TSparkParameter({
name,
type: this.type ?? (Number.isInteger(this.value) ? DBSQLParameterType.INTEGER : DBSQLParameterType.DOUBLE),
value: new TSparkParameterValue({
stringValue: Number(this.value).toString(),
Expand All @@ -66,6 +72,7 @@ export class DBSQLParameter {

if (this.value instanceof Int64 || typeof this.value === 'bigint') {
return new TSparkParameter({
name,
type: this.type ?? DBSQLParameterType.BIGINT,
value: new TSparkParameterValue({
stringValue: this.value.toString(),
Expand All @@ -75,6 +82,7 @@ export class DBSQLParameter {

if (this.value instanceof Date) {
return new TSparkParameter({
name,
type: this.type ?? DBSQLParameterType.TIMESTAMP,
value: new TSparkParameterValue({
stringValue: this.value.toISOString(),
Expand All @@ -83,6 +91,7 @@ export class DBSQLParameter {
}

return new TSparkParameter({
name,
type: this.type ?? DBSQLParameterType.STRING,
value: new TSparkParameterValue({
stringValue: this.value,
Expand Down
7 changes: 2 additions & 5 deletions lib/DBSQLSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,14 @@ function getQueryParameters(
for (const name of Object.keys(namedParameters)) {
const value = namedParameters[name];
const param = value instanceof DBSQLParameter ? value : new DBSQLParameter({ value });
const sparkParam = param.toSparkParameter();
sparkParam.name = name;
result.push(sparkParam);
result.push(param.toSparkParameter({ name }));
}
}

if (ordinalParameters !== undefined) {
for (const value of ordinalParameters) {
const param = value instanceof DBSQLParameter ? value : new DBSQLParameter({ value });
const sparkParam = param.toSparkParameter();
result.push(sparkParam);
result.push(param.toSparkParameter());
}
}

Expand Down