Skip to content

Commit

Permalink
dbeaver#3143: Add number conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
phantom committed Dec 20, 2024
1 parent 48e0cda commit ae7050a
Showing 1 changed file with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ export class ResultSetFormatAction
return false;
}

isNumeric(key: IResultSetPartialKey): boolean {
if (!key?.column) {
return false;
}

const column = this.view.getColumn(key.column);

if (column?.dataKind?.toLocaleLowerCase() === 'numeric') {
return true;
}
return false;
}

isText(key: IResultSetPartialKey): boolean {
if (!key?.column) {
return false;
Expand Down Expand Up @@ -160,6 +173,7 @@ export class ResultSetFormatAction

getText(key: IResultSetElementKey): string {
const value = this.get(key);
const numConversionNeeded = this.isNumeric(key);

if (value === null) {
return '';
Expand Down Expand Up @@ -199,15 +213,22 @@ export class ResultSetFormatAction
return JSON.stringify(value);
}

if (typeof value === 'number' || typeof value === 'boolean') {
return String(value);
if (numConversionNeeded && (typeof value === "string")) {
const numberValue = parseFloat(value);
if (!isNaN(numberValue)) {
return this.formatNumber(value);
}
}

if (typeof value === 'number' || typeof value === 'boolean') {
return this.formatNumber(String(value));
}
return value;
}

getDisplayString(key: IResultSetElementKey): string {
const value = this.get(key);
const numConversionNeeded = this.isNumeric(key);

if (value === null) {
return '[null]';
Expand Down Expand Up @@ -248,8 +269,14 @@ export class ResultSetFormatAction

return '[null]';
}

return this.truncateText(String(value), DISPLAY_STRING_LENGTH);
let to_trunc = String(value);
if (numConversionNeeded && (typeof value === "string")) {
const numberValue = parseFloat(value);
if (!isNaN(numberValue)) {
to_trunc = this.formatNumber(value);
}
}
return this.truncateText(to_trunc, DISPLAY_STRING_LENGTH);
}

truncateText(text: string, length: number): string {
Expand All @@ -259,4 +286,13 @@ export class ResultSetFormatAction
.map(v => (v.charCodeAt(0) < 32 ? ' ' : v))
.join('');
}

formatNumber(value: string): string {
const [integerPart, decimalPart] = value.split(".");
if (typeof integerPart !== "undefined") {
const intWithCommas = parseFloat(integerPart).toLocaleString("en-US");
return decimalPart ? `${intWithCommas}.${decimalPart}` : intWithCommas
}
return value
}
}

0 comments on commit ae7050a

Please sign in to comment.