Added Result view
This commit is contained in:
		
							
								
								
									
										10
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								package.json
									
									
									
									
									
								
							@@ -17,10 +17,12 @@
 | 
				
			|||||||
    ],
 | 
					    ],
 | 
				
			||||||
    "main": "./out/extension",
 | 
					    "main": "./out/extension",
 | 
				
			||||||
    "contributes": {
 | 
					    "contributes": {
 | 
				
			||||||
        "commands": [{
 | 
					        "commands": [
 | 
				
			||||||
            "command": "extension.executeSnippet",
 | 
					            {
 | 
				
			||||||
            "title": "Execute SQL Snippet"
 | 
					                "command": "extension.executeSnippet",
 | 
				
			||||||
        }],
 | 
					                "title": "Execute SQL Snippet"
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
        "keybindings": [{
 | 
					        "keybindings": [{
 | 
				
			||||||
            "command": "extension.executeSnippet",
 | 
					            "command": "extension.executeSnippet",
 | 
				
			||||||
            "key": "ctrl+enter",
 | 
					            "key": "ctrl+enter",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,6 +3,7 @@
 | 
				
			|||||||
// Import the module and reference it with the alias vscode in your code below
 | 
					// Import the module and reference it with the alias vscode in your code below
 | 
				
			||||||
import * as vscode from 'vscode';
 | 
					import * as vscode from 'vscode';
 | 
				
			||||||
import { ConnectionProvider, ConnectionNode } from './connections'
 | 
					import { ConnectionProvider, ConnectionNode } from './connections'
 | 
				
			||||||
 | 
					import { TextDocumentContentProvider } from './resultsetview'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const { spawn } = require('child_process');
 | 
					const { spawn } = require('child_process');
 | 
				
			||||||
const table = require('text-table');
 | 
					const table = require('text-table');
 | 
				
			||||||
@@ -14,6 +15,8 @@ var dbSession = null;
 | 
				
			|||||||
// this method is called when your extension is activated
 | 
					// this method is called when your extension is activated
 | 
				
			||||||
// your extension is activated the very first time the command is executed
 | 
					// your extension is activated the very first time the command is executed
 | 
				
			||||||
export function activate(context: vscode.ExtensionContext) {
 | 
					export function activate(context: vscode.ExtensionContext) {
 | 
				
			||||||
 | 
					    let previewUri = vscode.Uri.parse('frog-view://authority/resultset');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const connectionProvider = new ConnectionProvider();
 | 
					    const connectionProvider = new ConnectionProvider();
 | 
				
			||||||
    let myOutputChannel = vscode.window.createOutputChannel('SQL Query Results');
 | 
					    let myOutputChannel = vscode.window.createOutputChannel('SQL Query Results');
 | 
				
			||||||
    context.subscriptions.push(myOutputChannel);
 | 
					    context.subscriptions.push(myOutputChannel);
 | 
				
			||||||
@@ -23,13 +26,13 @@ export function activate(context: vscode.ExtensionContext) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    vscode.window.registerTreeDataProvider('connections', connectionProvider);
 | 
					    vscode.window.registerTreeDataProvider('connections', connectionProvider);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Use the console to output diagnostic information (console.log) and errors (console.error)
 | 
					    let provider = new TextDocumentContentProvider();
 | 
				
			||||||
 | 
						let registration = vscode.workspace.registerTextDocumentContentProvider('frog-view', provider);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Use the console to output diagnostic information (console.log) and errors (console.error)
 | 
				
			||||||
    // This line of code will only be executed once when your extension is activated
 | 
					    // This line of code will only be executed once when your extension is activated
 | 
				
			||||||
    console.log('Congratulations, your extension "frog" is now active!');
 | 
					    console.log('Congratulations, your extension "frog" is now active!');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // The command has been defined in the package.json file
 | 
					 | 
				
			||||||
    // Now provide the implementation of the command with  registerCommand
 | 
					 | 
				
			||||||
    // The commandId parameter must match the command field in package.json
 | 
					 | 
				
			||||||
    let disposable = vscode.commands.registerCommand('extension.executeSnippet', () => {
 | 
					    let disposable = vscode.commands.registerCommand('extension.executeSnippet', () => {
 | 
				
			||||||
        // The code you place here will be executed every time your command is executed
 | 
					        // The code you place here will be executed every time your command is executed
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -79,14 +82,15 @@ export function activate(context: vscode.ExtensionContext) {
 | 
				
			|||||||
                            diagnosticCollection.set(document.uri, diagnostics);
 | 
					                            diagnosticCollection.set(document.uri, diagnostics);
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    } else {
 | 
					                    } else {
 | 
				
			||||||
                        const data = []
 | 
					                        provider.setData(json);
 | 
				
			||||||
                        data.push(json['columnDefinitions'].map((x) => x['name']));
 | 
					                        provider.update(previewUri);
 | 
				
			||||||
                        for (let row of json["rows"]) {
 | 
					
 | 
				
			||||||
                            data.push(row['columns'].map((x) => x['value']));
 | 
					                        vscode.commands.executeCommand('vscode.previewHtml', previewUri, vscode.ViewColumn.Two, 'Result Set View').then((success) => {
 | 
				
			||||||
                        }
 | 
					                        }, (reason) => {
 | 
				
			||||||
                        var t = table(data, { hsep: ' | '});
 | 
					                            vscode.window.showErrorMessage(reason);
 | 
				
			||||||
                        myOutputChannel.append(t);
 | 
					                        });
 | 
				
			||||||
                        myOutputChannel.append("\n");
 | 
					                
 | 
				
			||||||
 | 
					                        myOutputChannel.append("Query successful.\n");
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    myOutputChannel.append("-- " + new Date() + " -------------------------------\n");
 | 
					                    myOutputChannel.append("-- " + new Date() + " -------------------------------\n");
 | 
				
			||||||
@@ -117,7 +121,7 @@ export function activate(context: vscode.ExtensionContext) {
 | 
				
			|||||||
        dbSession.stdin.write(text + "\n--- END ---\n");
 | 
					        dbSession.stdin.write(text + "\n--- END ---\n");
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    context.subscriptions.push(disposable);
 | 
					    context.subscriptions.push(disposable, registration);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// this method is called when your extension is deactivated
 | 
					// this method is called when your extension is deactivated
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										58
									
								
								src/resultsetview.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/resultsetview.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
				
			|||||||
 | 
					import * as vscode from 'vscode';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
 | 
				
			||||||
 | 
					    private _onDidChange = new vscode.EventEmitter<vscode.Uri>();
 | 
				
			||||||
 | 
					    private _data = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public provideTextDocumentContent(uri: vscode.Uri): string {
 | 
				
			||||||
 | 
					        if (!this._data) return this.errorSnippet();
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        let styles = `<style>
 | 
				
			||||||
 | 
					                    table {
 | 
				
			||||||
 | 
					                        border: solid 1px white;
 | 
				
			||||||
 | 
					                        border-collapse: collapse;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    table td, table th {
 | 
				
			||||||
 | 
					                        border: solid 1px white;
 | 
				
			||||||
 | 
					                        padding: 3px;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                </style>`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let text = `<body>${styles}<table><tr>`
 | 
				
			||||||
 | 
					        text += this._data['columnDefinitions'].map((x) => `<th>${x['name']}</th>`).join('')
 | 
				
			||||||
 | 
					        text += '</tr>'
 | 
				
			||||||
 | 
					        text += this._data['rows'].map((r) => 
 | 
				
			||||||
 | 
					            '<tr>' + r['columns'].map((c) => '<td>' + c['value'] + '</td>').join('') + '</tr>'
 | 
				
			||||||
 | 
					        ).join('')
 | 
				
			||||||
 | 
					        text += '</table></body>'
 | 
				
			||||||
 | 
					        return text;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    get onDidChange(): vscode.Event<vscode.Uri> {
 | 
				
			||||||
 | 
					        return this._onDidChange.event;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public update(uri: vscode.Uri) {
 | 
				
			||||||
 | 
					        this._onDidChange.fire(uri);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public setData(data) {
 | 
				
			||||||
 | 
					        this._data = data;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private errorSnippet(): string {
 | 
				
			||||||
 | 
					        return `
 | 
				
			||||||
 | 
					            <body>
 | 
				
			||||||
 | 
					                <table>
 | 
				
			||||||
 | 
					                    <tr>
 | 
				
			||||||
 | 
					                        <th>ID</th>
 | 
				
			||||||
 | 
					                        <th>Name</th>
 | 
				
			||||||
 | 
					                    </tr>
 | 
				
			||||||
 | 
					                    <tr>
 | 
				
			||||||
 | 
					                        <td>1</td>
 | 
				
			||||||
 | 
					                        <td>Foobar</td>
 | 
				
			||||||
 | 
					                    </tr>
 | 
				
			||||||
 | 
					                </table>
 | 
				
			||||||
 | 
					            </body>`;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user