-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from codefori/feature/run_from_uri
Ability to run a statement via URI
- Loading branch information
Showing
4 changed files
with
112 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { commands, env, Selection, Uri, UriHandler, window, workspace } from "vscode"; | ||
import querystring from "querystring"; | ||
import Document from "./language/sql/document"; | ||
import { ServerComponent } from "./connection/serverComponent"; | ||
import { remoteAssistIsEnabled } from "./language/providers/available"; | ||
|
||
export class Db2iUriHandler implements UriHandler { | ||
handleUri(uri: Uri) { | ||
const path = uri.path; | ||
|
||
switch (path) { | ||
case '/sql': | ||
const queryData = querystring.parse(uri.query); | ||
const content = String(queryData.content).trim(); | ||
|
||
if (content) { | ||
const asLower = content.toLowerCase(); | ||
const isValid = asLower.startsWith(`select `) || asLower.startsWith(`with `); | ||
|
||
if (isValid) { | ||
const run = queryData.run === `true`; | ||
|
||
if (run) { | ||
if (remoteAssistIsEnabled()) { | ||
commands.executeCommand(`vscode-db2i.runEditorStatement`, { | ||
content, | ||
qualifier: `statement`, | ||
open: true, | ||
}); | ||
} else { | ||
window.showErrorMessage(`You must be connected to a system to run a statement.`); | ||
} | ||
} else { | ||
workspace.openTextDocument({ language: `sql`, content }).then(textDoc => { | ||
window.showTextDocument(textDoc); | ||
}); | ||
} | ||
} else { | ||
window.showErrorMessage(`Only SELECT or WITH statements are supported.`); | ||
} | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
export const getStatementUri = commands.registerCommand(`vscode-db2i.getStatementUri`, async () => { | ||
const editor = window.activeTextEditor; | ||
|
||
if (editor) { | ||
const content = editor.document.getText(); | ||
|
||
const sqlDocument = new Document(content); | ||
const cursor = editor.document.offsetAt(editor.selection.active); | ||
|
||
const currentStmt = sqlDocument.getGroupByOffset(cursor); | ||
|
||
if (currentStmt) { | ||
const stmtContent = content.substring(currentStmt.range.start, currentStmt.range.end); | ||
editor.selection = new Selection(editor.document.positionAt(currentStmt.range.start), editor.document.positionAt(currentStmt.range.end)); | ||
const uri = `vscode://halcyontechltd.vscode-db2i/sql?content=${encodeURIComponent(stmtContent)}`; | ||
|
||
env.clipboard.writeText(uri); | ||
window.showInformationMessage(`Statement URI copied to clipboard.`); | ||
} | ||
} | ||
}); |