Skip to content

Commit

Permalink
corrected queries, search tables and columns
Browse files Browse the repository at this point in the history
  • Loading branch information
daimor committed Jul 12, 2020
1 parent f25f0ce commit beabcb0
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 97 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"onCommand:sqltools.*"
],
"main": "./out/extension.js",
"ls": "./out/ls/plugin.js",
"dependencies": {
"@sqltools/base-driver": "^0.1.8",
"@sqltools/types": "^0.1.5",
Expand Down
57 changes: 31 additions & 26 deletions src/ls/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { IConnectionDriver, MConnectionExplorer, NSDatabase, ContextValue, Arg0
import { v4 as generateId } from 'uuid';
import IRISdb, { IRISDirect, IQueries } from './irisdb';
import keywordsCompletion from './keywords';
// import { workspace } from "vscode";

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

type DriverOptions = any;

export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> implements IConnectionDriver {

queries: IQueries = queries;
private showSystem = false;

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

const { namespace } = this.credentials;
let config: IRISDirect;
this.showSystem = this.credentials.showSystem || false;

if (this.credentials.serverName) {
// const serverName = this.credentials.serverName;
// const server = workspace.getConfiguration(`intersystems.servers.${serverName}.webServer`);
// let { scheme, host, port, pathPrefix, username, password } = server;
// config = {
// https: scheme === "https",
// host,
// port,
// pathPrefix,
// namespace,
// username,
// password
// };
throw new Error("not supported");
} else {
let { https, server: host, port, pathPrefix, username, password } = this.credentials;
config = {
Expand Down Expand Up @@ -60,16 +54,13 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
}

private splitQueries(queries: string): string[] {
if (!queries.includes(';')) {
return [queries]
}

return queries.split(/;\s*\n/gm).filter(query => query.trim().length);
return queries.split(/;\s*(\n|$)/gm).filter(query => query.trim().length);
}

public query: (typeof AbstractDriver)['prototype']['query'] = async (queries, opt = {}) => {
const irisdb = await this.open();
const queriesResults = await Promise.all(this.splitQueries(queries.toString()).map(query => irisdb.query(query, [])));
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({
Expand Down Expand Up @@ -114,44 +105,58 @@ export default class IRISDriver extends AbstractDriver<IRISdb, DriverOptions> im
case ContextValue.TABLE:
case ContextValue.VIEW:
return this.getColumns(item as NSDatabase.ITable);
case ContextValue.FUNCTION:
return [];
}
return [];
}

private async getSchemas({ item }: Arg0<IConnectionDriver['getChildrenForItem']>) {
item['showSystem'] = this.showSystem;

switch (item.childType) {
case ContextValue.TABLE:
return this.queryResults(this.queries.fetchTableSchemas());
return this.queryResults(this.queries.fetchTableSchemas(item as NSDatabase.IDatabase));
case ContextValue.VIEW:
return this.queryResults(this.queries.fetchViewSchemas());
return this.queryResults(this.queries.fetchViewSchemas(item as NSDatabase.IDatabase));
case ContextValue.FUNCTION:
return this.queryResults(this.queries.fetchFunctionSchemas());
return this.queryResults(this.queries.fetchFunctionSchemas(item as NSDatabase.IDatabase));
}
return [];
}

private async getChildrenForSchema({ item }: Arg0<IConnectionDriver['getChildrenForItem']>) {
item['showSystem'] = this.showSystem;

switch (item.childType) {
case ContextValue.TABLE:
return this.queryResults(this.queries.fetchTables(item as NSDatabase.ISchema));
case ContextValue.VIEW:
return this.queryResults(this.queries.fetchViews(item as NSDatabase.ISchema));
case ContextValue.FUNCTION:
return this.queryResults(this.queries.fetchFunctions(item as NSDatabase.ISchema));
return this.queryResults(this.queries.fetchFunctions(item as NSDatabase.ISchema)).then(r => r.map(t => {
t.childType = ContextValue.NO_CHILD;
t["snippet"] = "Testing";
return t;
}));
}
return [];
}

/**
* This method is a helper for intellisense and quick picks.
*/
public async searchItems(itemType: ContextValue, _search: string, _extraParams: any = {}): Promise<NSDatabase.SearchableItem[]> {
public async searchItems(itemType: ContextValue, search: string, extraParams: any = {}): Promise<NSDatabase.SearchableItem[]> {
switch (itemType) {
case ContextValue.TABLE:
case ContextValue.FUNCTION:
case ContextValue.VIEW:
return []
return this.queryResults(this.queries.searchEverything({ search, showSystem: this.showSystem })).then(r => r.map(t => {
t.isView = toBool(t.isView);
return t;
}));
case ContextValue.COLUMN:
return [];
return this.queryResults(this.queries.searchColumns({ search, ...extraParams }));
}
return [];
}
Expand Down
7 changes: 3 additions & 4 deletions src/ls/irisdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export interface IQueries extends IBaseQueries {
fetchTableSchemas?: QueryBuilder<NSDatabase.IDatabase, NSDatabase.ISchema>;
fetchViewSchemas?: QueryBuilder<NSDatabase.IDatabase, NSDatabase.ISchema>;
fetchFunctionSchemas?: QueryBuilder<NSDatabase.IDatabase, NSDatabase.ISchema>;

fetchViews: QueryBuilder<NSDatabase.ISchema, NSDatabase.ITable>;

searchEverything: QueryBuilder<{ search: string, limit?: number }, NSDatabase.ITable>;
}

export default class IRISdb {
Expand Down Expand Up @@ -124,12 +126,10 @@ export default class IRISdb {
return response;
})
.then(response => {
// console.log(`APIResponse: ${method} ${proto}://${host}:${port}${path}`)
if (method === "HEAD") {
return this.cookies;
}
const data = response.body;
console.log('data', data);
/// deconde encoded content
if (data.result && data.result.enc && data.result.content) {
data.result.enc = false;
Expand Down Expand Up @@ -180,7 +180,6 @@ export default class IRISdb {

public async query(query: string, parameters: string[]): Promise<any> {
console.log('SQL: ' + query);
console.log('SQLPARAMS: ' + JSON.stringify(parameters));
return this.request(1, "POST", `${this.config.namespace}/action/query`, {
parameters,
query,
Expand Down
2 changes: 1 addition & 1 deletion src/ls/keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ const keywordsCompletion: { [w: string]: NSDatabase.IStaticCompletion } = keywor
label: word,
detail: word,
filterText: word,
sortText: (['SELECT', 'CREATE', 'UPDATE', 'DELETE'].includes(word) ? '2:' : '') + word,
sortText: (['SELECT', 'CREATE', 'UPDATE', 'DELETE', 'FROM', 'INSERT', 'INTO'].includes(word) ? '2:' : '99:') + word,
documentation: {
value: `\`\`\`yaml\nWORD: ${word}\n\`\`\``,
kind: 'markdown'
Expand Down
Loading

0 comments on commit beabcb0

Please sign in to comment.