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

Handle duplicate column names in query results when using 2023.1+ #61

Merged
merged 1 commit into from
Feb 26, 2025
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
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@
"devDependencies": {
"@babel/preset-env": "^7.14.2",
"@intersystems-community/intersystems-servermanager": "^3.8.0",
"@types/lodash": "^4.17.15",
"@types/node": "^14.17.0",
"@types/vscode": "^1.93.0",
"@vscode/vsce": "^2.19.0",
"lodash": "^4.17.21",
"rimraf": "^3.0.2",
"ts-loader": "^9.2.1",
"typescript": "^4.9.5",
Expand Down
46 changes: 37 additions & 9 deletions src/ls/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IConnectionDriver, MConnectionExplorer, NSDatabase, ContextValue, Arg0
import { v4 as generateId } from 'uuid';
import IRISdb, { IRISDirect, IQueries } from './irisdb';
import keywordsCompletion from './keywords';
import zipObject from 'lodash/zipObject';

const toBool = (v: any) => v && (v.toString() === '1' || v.toString().toLowerCase() === 'true' || v.toString().toLowerCase() === 'yes');

Expand Down Expand Up @@ -55,21 +56,48 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
return queries.split(/;\s*(\n|$)/gm).filter(query => query.trim().length);
}

// Handle duplicate column names by appending counter
private getColumnNames(columns: { name: string, type: string }[]): string[] {
return columns.reduce((names, { name }) => {
const count = names.filter((n) => n === name).length;
return names.concat(count > 0 ? `${name} (${count})` : name);
}, []);
}

// Modify to take account of deduplicated column names
private mapRows(rows: any[], columns: string[]): any[] {
return rows.map((r) => zipObject(columns, r));
}

public query: (typeof AbstractDriver)['prototype']['query'] = async (queries, opt = {}) => {
const irisdb = await this.open();
const listQueries = this.splitQueries(queries.toString());
const queriesResults = await Promise.all(listQueries.map(query => irisdb.query(query, [])));
const resultsAgg: NSDatabase.IResult[] = [];
queriesResults.forEach(queryResult => {
resultsAgg.push({
cols: queryResult.length ? Object.keys(queryResult[0]) : [],
connId: this.getId(),
messages: [{ date: new Date(), message: `Query ok with ${queryResult.length} results` }],
results: queryResult,
query: queries.toString(),
requestId: opt.requestId,
resultId: generateId(),
});
if (irisdb.apiVersion < 6) {
resultsAgg.push({
cols: queryResult.content.length ? Object.keys(queryResult.content[0]) : [],
connId: this.getId(),
messages: [{ date: new Date(), message: `Query ok with ${queryResult.content.length} results` }],
results: queryResult.content,
query: queries.toString(),
requestId: opt.requestId,
resultId: generateId(),
});
}
else {
const cols = this.getColumnNames(queryResult[0].columns || []);
resultsAgg.push({
cols,
connId: this.getId(),
messages: [{ date: new Date(), message: `Query ok with ${queryResult[0]?.content.length ?? 'no'} results` }],
results: this.mapRows(queryResult[0]?.content, cols),
query: queries.toString(),
requestId: opt.requestId,
resultId: generateId(),
});
}
});

return resultsAgg;
Expand Down
16 changes: 10 additions & 6 deletions src/ls/irisdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export default class IRISdb {

private config: IRISDirect;
private cookies: string[] = [];
private apiVersion = 1;
private _apiVersion = 1;

public get apiVersion() {
return this._apiVersion;
}

public constructor(config: IRISDirect) {
this.config = config;
Expand Down Expand Up @@ -57,11 +61,11 @@ export default class IRISdb {
headers?: any
): Promise<any> {
const { https, host, port, pathPrefix, username, password } = this.config;
if (minVersion > this.apiVersion) {
return Promise.reject(`${path} not supported by API version ${this.apiVersion}`);
if (minVersion > this._apiVersion) {
return Promise.reject(`${path} not supported by API version ${this._apiVersion}`);
}
if (minVersion && minVersion > 0) {
path = `v${this.apiVersion}/${path}`;
path = `v${this._apiVersion}/${path}`;
}

headers = {
Expand Down Expand Up @@ -169,7 +173,7 @@ export default class IRISdb {
You must select one of the following: ${data.namespaces.join(", ")}.`,
};
}
this.apiVersion = data.api;
this._apiVersion = data.api;
return info;
}
});
Expand All @@ -183,7 +187,7 @@ export default class IRISdb {
return this.request(1, "POST", `${this.config.namespace}/action/query`, {
parameters,
query,
}).then(data => data.result.content)
}, this._apiVersion >= 6 ? { positional: true } : {}).then(data => data.result)
}


Expand Down
Loading