mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-20 09:35:38 -05:00
Notebooks: Grid Support (#3832)
* First grid support in notebooks * still trying to get nteract ipynb to display grid correctly * works opening with existing 'application/vnd.dataresource+json' table * fixing merge issue due to core folder structure changing a bit * PR feedback, fix for XSS
This commit is contained in:
@@ -17,6 +17,7 @@ import { Disposable } from 'vs/base/common/lifecycle';
|
||||
import { IErrorMessageService } from 'sql/platform/errorMessage/common/errorMessageService';
|
||||
import { ConnectionProfile } from 'sql/platform/connection/common/connectionProfile';
|
||||
import { IConnectionProfile } from 'sql/platform/connection/common/interfaces';
|
||||
import { escape } from 'sql/base/common/strings';
|
||||
|
||||
export const sqlKernel: string = localize('sqlKernel', 'SQL');
|
||||
export const sqlKernelError: string = localize("sqlKernelError", "SQL kernel error");
|
||||
@@ -299,28 +300,7 @@ export class SQLFuture extends Disposable implements FutureInternal {
|
||||
setIOPubHandler(handler: nb.MessageHandler<nb.IIOPubMessage>): void {
|
||||
this._register(this._queryRunner.onBatchEnd(batch => {
|
||||
this._queryRunner.getQueryRows(0, batch.resultSetSummaries[0].rowCount, 0, 0).then(d => {
|
||||
let data: SQLData = {
|
||||
columns: batch.resultSetSummaries[0].columnInfo.map(c => c.columnName),
|
||||
rows: d.resultSubset.rows.map(r => r.map(c => c.displayValue))
|
||||
};
|
||||
let table: HTMLTableElement = document.createElement('table');
|
||||
table.createTHead();
|
||||
table.createTBody();
|
||||
let hrow = <HTMLTableRowElement>table.insertRow();
|
||||
// headers
|
||||
for (let column of data.columns) {
|
||||
let cell = hrow.insertCell();
|
||||
cell.innerHTML = column;
|
||||
}
|
||||
|
||||
for (let row in data.rows) {
|
||||
let hrow = <HTMLTableRowElement>table.insertRow();
|
||||
for (let column in data.columns) {
|
||||
let cell = hrow.insertCell();
|
||||
cell.innerHTML = data.rows[row][column];
|
||||
}
|
||||
}
|
||||
let tableHtml = '<table>' + table.innerHTML + '</table>';
|
||||
let columns = batch.resultSetSummaries[0].columnInfo;
|
||||
|
||||
let msg: nb.IIOPubMessage = {
|
||||
channel: 'iopub',
|
||||
@@ -333,7 +313,7 @@ export class SQLFuture extends Disposable implements FutureInternal {
|
||||
output_type: 'execute_result',
|
||||
metadata: {},
|
||||
execution_count: 0,
|
||||
data: { 'text/html': tableHtml },
|
||||
data: { 'application/vnd.dataresource+json': this.convertToDataResource(columns, d), 'text/html': this.convertToHtmlTable(columns, d) }
|
||||
},
|
||||
metadata: undefined,
|
||||
parent_header: undefined
|
||||
@@ -348,4 +328,63 @@ export class SQLFuture extends Disposable implements FutureInternal {
|
||||
removeMessageHook(hook: (msg: nb.IIOPubMessage) => boolean | Thenable<boolean>): void {
|
||||
// no-op
|
||||
}
|
||||
|
||||
private convertToDataResource(columns: IDbColumn[], d: QueryExecuteSubsetResult): IDataResource {
|
||||
let columnsResources: IDataResourceSchema[] = [];
|
||||
columns.forEach(column => {
|
||||
columnsResources.push({name: escape(column.columnName)});
|
||||
});
|
||||
let columnsFields: IDataResourceFields = { fields: undefined };
|
||||
columnsFields.fields = columnsResources;
|
||||
return {
|
||||
schema: columnsFields,
|
||||
data: d.resultSubset.rows.map(row => {
|
||||
let rowObject: { [key: string]: any; } = {};
|
||||
row.forEach((val, index) => {
|
||||
rowObject[index] = val.displayValue;
|
||||
});
|
||||
return rowObject;
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
private convertToHtmlTable(columns: IDbColumn[], d: QueryExecuteSubsetResult): string {
|
||||
let data: SQLData = {
|
||||
columns: columns.map(c => escape(c.columnName)),
|
||||
rows: d.resultSubset.rows.map(r => r.map(c => c.displayValue))
|
||||
};
|
||||
let table: HTMLTableElement = document.createElement('table');
|
||||
table.createTHead();
|
||||
table.createTBody();
|
||||
let hrow = <HTMLTableRowElement>table.insertRow();
|
||||
// headers
|
||||
for (let column of data.columns) {
|
||||
let cell = hrow.insertCell();
|
||||
cell.innerHTML = column;
|
||||
}
|
||||
|
||||
for (let row in data.rows) {
|
||||
let hrow = <HTMLTableRowElement>table.insertRow();
|
||||
for (let column in data.columns) {
|
||||
let cell = hrow.insertCell();
|
||||
cell.innerHTML = escape(data.rows[row][column]);
|
||||
}
|
||||
}
|
||||
let tableHtml = '<table>' + table.innerHTML + '</table>';
|
||||
return tableHtml;
|
||||
}
|
||||
}
|
||||
|
||||
export interface IDataResource {
|
||||
schema: IDataResourceFields;
|
||||
data: any[];
|
||||
}
|
||||
|
||||
export interface IDataResourceFields {
|
||||
fields: IDataResourceSchema[];
|
||||
}
|
||||
|
||||
export interface IDataResourceSchema {
|
||||
name: string;
|
||||
type?: string;
|
||||
}
|
||||
Reference in New Issue
Block a user