Skip to content

Commit beabcb0

Browse files
committed
corrected queries, search tables and columns
1 parent f25f0ce commit beabcb0

File tree

5 files changed

+153
-97
lines changed

5 files changed

+153
-97
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"onCommand:sqltools.*"
3838
],
3939
"main": "./out/extension.js",
40+
"ls": "./out/ls/plugin.js",
4041
"dependencies": {
4142
"@sqltools/base-driver": "^0.1.8",
4243
"@sqltools/types": "^0.1.5",

src/ls/driver.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import { IConnectionDriver, MConnectionExplorer, NSDatabase, ContextValue, Arg0
44
import { v4 as generateId } from 'uuid';
55
import IRISdb, { IRISDirect, IQueries } from './irisdb';
66
import keywordsCompletion from './keywords';
7-
// import { workspace } from "vscode";
7+
8+
const toBool = (v: any) => v && (v.toString() === '1' || v.toString().toLowerCase() === 'true' || v.toString().toLowerCase() === 'yes');
89

910
type DriverOptions = any;
11+
1012
export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> implements IConnectionDriver {
1113

1214
queries: IQueries = queries;
15+
private showSystem = false;
1316

1417
public async open() {
1518
if (this.connection) {
@@ -18,19 +21,10 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
1821

1922
const { namespace } = this.credentials;
2023
let config: IRISDirect;
24+
this.showSystem = this.credentials.showSystem || false;
25+
2126
if (this.credentials.serverName) {
22-
// const serverName = this.credentials.serverName;
23-
// const server = workspace.getConfiguration(`intersystems.servers.${serverName}.webServer`);
24-
// let { scheme, host, port, pathPrefix, username, password } = server;
25-
// config = {
26-
// https: scheme === "https",
27-
// host,
28-
// port,
29-
// pathPrefix,
30-
// namespace,
31-
// username,
32-
// password
33-
// };
27+
throw new Error("not supported");
3428
} else {
3529
let { https, server: host, port, pathPrefix, username, password } = this.credentials;
3630
config = {
@@ -60,16 +54,13 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
6054
}
6155

6256
private splitQueries(queries: string): string[] {
63-
if (!queries.includes(';')) {
64-
return [queries]
65-
}
66-
67-
return queries.split(/;\s*\n/gm).filter(query => query.trim().length);
57+
return queries.split(/;\s*(\n|$)/gm).filter(query => query.trim().length);
6858
}
6959

7060
public query: (typeof AbstractDriver)['prototype']['query'] = async (queries, opt = {}) => {
7161
const irisdb = await this.open();
72-
const queriesResults = await Promise.all(this.splitQueries(queries.toString()).map(query => irisdb.query(query, [])));
62+
const listQueries = this.splitQueries(queries.toString());
63+
const queriesResults = await Promise.all(listQueries.map(query => irisdb.query(query, [])));
7364
const resultsAgg: NSDatabase.IResult[] = [];
7465
queriesResults.forEach(queryResult => {
7566
resultsAgg.push({
@@ -114,44 +105,58 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
114105
case ContextValue.TABLE:
115106
case ContextValue.VIEW:
116107
return this.getColumns(item as NSDatabase.ITable);
108+
case ContextValue.FUNCTION:
109+
return [];
117110
}
118111
return [];
119112
}
120113

121114
private async getSchemas({ item }: Arg0<IConnectionDriver['getChildrenForItem']>) {
115+
item['showSystem'] = this.showSystem;
116+
122117
switch (item.childType) {
123118
case ContextValue.TABLE:
124-
return this.queryResults(this.queries.fetchTableSchemas());
119+
return this.queryResults(this.queries.fetchTableSchemas(item as NSDatabase.IDatabase));
125120
case ContextValue.VIEW:
126-
return this.queryResults(this.queries.fetchViewSchemas());
121+
return this.queryResults(this.queries.fetchViewSchemas(item as NSDatabase.IDatabase));
127122
case ContextValue.FUNCTION:
128-
return this.queryResults(this.queries.fetchFunctionSchemas());
123+
return this.queryResults(this.queries.fetchFunctionSchemas(item as NSDatabase.IDatabase));
129124
}
130125
return [];
131126
}
132127

133128
private async getChildrenForSchema({ item }: Arg0<IConnectionDriver['getChildrenForItem']>) {
129+
item['showSystem'] = this.showSystem;
130+
134131
switch (item.childType) {
135132
case ContextValue.TABLE:
136133
return this.queryResults(this.queries.fetchTables(item as NSDatabase.ISchema));
137134
case ContextValue.VIEW:
138135
return this.queryResults(this.queries.fetchViews(item as NSDatabase.ISchema));
139136
case ContextValue.FUNCTION:
140-
return this.queryResults(this.queries.fetchFunctions(item as NSDatabase.ISchema));
137+
return this.queryResults(this.queries.fetchFunctions(item as NSDatabase.ISchema)).then(r => r.map(t => {
138+
t.childType = ContextValue.NO_CHILD;
139+
t["snippet"] = "Testing";
140+
return t;
141+
}));
141142
}
142143
return [];
143144
}
144145

145146
/**
146147
* This method is a helper for intellisense and quick picks.
147148
*/
148-
public async searchItems(itemType: ContextValue, _search: string, _extraParams: any = {}): Promise<NSDatabase.SearchableItem[]> {
149+
public async searchItems(itemType: ContextValue, search: string, extraParams: any = {}): Promise<NSDatabase.SearchableItem[]> {
149150
switch (itemType) {
150151
case ContextValue.TABLE:
152+
case ContextValue.FUNCTION:
151153
case ContextValue.VIEW:
152-
return []
154+
return this.queryResults(this.queries.searchEverything({ search, showSystem: this.showSystem })).then(r => r.map(t => {
155+
t.isView = toBool(t.isView);
156+
return t;
157+
}));
153158
case ContextValue.COLUMN:
154-
return [];
159+
return this.queryResults(this.queries.searchColumns({ search, ...extraParams }));
155160
}
156161
return [];
157162
}

src/ls/irisdb.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export interface IQueries extends IBaseQueries {
1717
fetchTableSchemas?: QueryBuilder<NSDatabase.IDatabase, NSDatabase.ISchema>;
1818
fetchViewSchemas?: QueryBuilder<NSDatabase.IDatabase, NSDatabase.ISchema>;
1919
fetchFunctionSchemas?: QueryBuilder<NSDatabase.IDatabase, NSDatabase.ISchema>;
20-
20+
2121
fetchViews: QueryBuilder<NSDatabase.ISchema, NSDatabase.ITable>;
22+
23+
searchEverything: QueryBuilder<{ search: string, limit?: number }, NSDatabase.ITable>;
2224
}
2325

2426
export default class IRISdb {
@@ -124,12 +126,10 @@ export default class IRISdb {
124126
return response;
125127
})
126128
.then(response => {
127-
// console.log(`APIResponse: ${method} ${proto}://${host}:${port}${path}`)
128129
if (method === "HEAD") {
129130
return this.cookies;
130131
}
131132
const data = response.body;
132-
console.log('data', data);
133133
/// deconde encoded content
134134
if (data.result && data.result.enc && data.result.content) {
135135
data.result.enc = false;
@@ -180,7 +180,6 @@ export default class IRISdb {
180180

181181
public async query(query: string, parameters: string[]): Promise<any> {
182182
console.log('SQL: ' + query);
183-
console.log('SQLPARAMS: ' + JSON.stringify(parameters));
184183
return this.request(1, "POST", `${this.config.namespace}/action/query`, {
185184
parameters,
186185
query,

src/ls/keywords.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ const keywordsCompletion: { [w: string]: NSDatabase.IStaticCompletion } = keywor
286286
label: word,
287287
detail: word,
288288
filterText: word,
289-
sortText: (['SELECT', 'CREATE', 'UPDATE', 'DELETE'].includes(word) ? '2:' : '') + word,
289+
sortText: (['SELECT', 'CREATE', 'UPDATE', 'DELETE', 'FROM', 'INSERT', 'INTO'].includes(word) ? '2:' : '99:') + word,
290290
documentation: {
291291
value: `\`\`\`yaml\nWORD: ${word}\n\`\`\``,
292292
kind: 'markdown'

0 commit comments

Comments
 (0)