Skip to content

Commit

Permalink
KXI-35930 for local q
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip-Carneiro-KX committed Jan 23, 2024
1 parent dc51644 commit c53dde8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/models/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Connection {
private options: nodeq.ConnectionParameters;
private connection?: nodeq.Connection;
public connected: boolean;
private isError: boolean = false;
private result?: string;

constructor(connectionString: string, creds?: string[], tls?: boolean) {
Expand Down Expand Up @@ -109,6 +110,7 @@ export class Connection {
!!stringify,
(err: Error, res: QueryResult) => {
if (err) {
this.isError = true;
this.result = handleQueryResults(
err.toString(),
QueryResultType.Error,
Expand All @@ -123,6 +125,10 @@ export class Connection {
const result = await this.waitForResult();

if (ext.resultsViewProvider.isVisible() && stringify) {
if (this.isError) {
this.isError = false;
return result;
}
return convertStringToArray(result);
}

Expand All @@ -142,6 +148,7 @@ export class Connection {

private handleQueryResult = (res: QueryResult): void => {
if (res.errored) {
this.isError = true;
this.result = handleQueryResults(
res.error + (res.backtrace ? "\n" + res.backtrace : ""),
QueryResultType.Error,
Expand Down
35 changes: 24 additions & 11 deletions src/services/resultsPanelProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export class KdbResultsViewProvider implements WebviewViewProvider {
});
}
} else {
if (typeof results[0] === "string") {
return results.map((key: string) => {
const sanitizedKey = this.sanitizeString(key);
return { field: sanitizedKey, headerName: sanitizedKey };
});
}
return Object.keys(results[0]).map((key: string) => {
const sanitizedKey = this.sanitizeString(key);
return { field: sanitizedKey, headerName: sanitizedKey };
Expand All @@ -140,17 +146,22 @@ export class KdbResultsViewProvider implements WebviewViewProvider {
const queryResult = isInsights ? results.rows : results;

const columnDefs = this.generateCoumnDefs(results, isInsights);
const rowData = queryResult.map((row: any) => {
for (const key in row) {
if (Object.prototype.hasOwnProperty.call(row, key)) {
row[key] =
row[key] !== undefined && row[key] !== null
? this.sanitizeString(row[key])
: "";
let rowData = [];
if (!isInsights && typeof results[0] === "string") {
rowData = [];
} else {
rowData = queryResult.map((row: any) => {
for (const key in row) {
if (Object.prototype.hasOwnProperty.call(row, key)) {
row[key] =
row[key] !== undefined && row[key] !== null
? this.sanitizeString(row[key])
: "";
}
}
}
return row;
});
return row;
});
}
if (rowData.length > 0) {
ext.resultPanelCSV = this.convertToCsv(rowData).join("\n");
}
Expand Down Expand Up @@ -224,7 +235,9 @@ export class KdbResultsViewProvider implements WebviewViewProvider {
if (typeof queryResult === "string" || typeof queryResult === "number") {
result =
queryResult !== ""
? `<p>${queryResult}</p>`
? `<p class="results-txt">${queryResult
.toString()
.replace(/\n/g, "<br/>")}</p>`
: "<p>No results to show</p>";
} else if (queryResult) {
isGrid = true;
Expand Down
13 changes: 9 additions & 4 deletions src/utils/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function runQFileTerminal(filename?: string): void {

export function handleQueryResults(
results: any,
type: QueryResultType
type: QueryResultType,
): string {
let handledResult: string;
switch (type) {
Expand Down Expand Up @@ -177,7 +177,7 @@ function processLine(
line: string,
index: number,
fieldLengths: number[],
fieldNames: string[]
fieldNames: string[],
): object {
let start = 0;
const obj: { [key: string]: any } = { Index: index + 1 };
Expand Down Expand Up @@ -207,10 +207,15 @@ export function convertStringToArray(str: string): any[] {
return lines.flatMap((line, index) =>
fieldLengths.length > 0
? processLine(line, index, fieldLengths, fieldNames)
: []
: [],
);
}

if (lines.length === 2 && lines[1].startsWith("---")) {
lines.splice(1, 1);
return lines[0].split(" ").filter((part) => part !== "");
}

return lines
.flatMap((line, index) => {
const parts = line.split("|").map((part) => part.trim());
Expand All @@ -219,6 +224,6 @@ export function convertStringToArray(str: string): any[] {
: processLineWithoutSeparator(line, index);
})
.filter(
(obj) => !("Value" in obj && (obj.Value as string).startsWith("-"))
(obj) => !("Value" in obj && (obj.Value as string).startsWith("-")),
);
}
4 changes: 4 additions & 0 deletions src/webview/styles/resultsPanel.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ body {
.results-header-datagrid {
background: #00000050;
}

.results-txt {
white-space: pre;
}

0 comments on commit c53dde8

Please sign in to comment.