From aa2eae26965b786e9327f1a034629359721d9125 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Fri, 10 Apr 2020 13:49:41 +0200 Subject: [PATCH] messages from query --- packages/api/src/controllers/sessions.js | 32 +++++++++++++++--- packages/api/src/proc/sessionProcess.js | 17 +++++++--- packages/datalib/src/GridDisplay.ts | 40 ++++++++++++----------- packages/datalib/src/JslGridDisplay.ts | 22 ++++++++----- packages/engines/mssql/index.js | 14 +++++++- packages/web/src/sqleditor/JslDataGrid.js | 2 ++ 6 files changed, 89 insertions(+), 38 deletions(-) diff --git a/packages/api/src/controllers/sessions.js b/packages/api/src/controllers/sessions.js index bb74d20b..28274ffa 100644 --- a/packages/api/src/controllers/sessions.js +++ b/packages/api/src/controllers/sessions.js @@ -9,23 +9,44 @@ module.exports = { /** @type {import('@dbgate/types').OpenedSession[]} */ opened: [], - handle_error(sesid, props) { - const { error } = props; - console.log(`Error in database session ${sesid}: ${error}`); - }, + // handle_error(sesid, props) { + // const { error } = props; + // console.log(`Error in database session ${sesid}`, error); + // this.dispatchMessage(sesid, { + // severity: 'error', + // message: error && error.toString(), + // }); + // }, // handle_row(sesid, props) { // const { row } = props; // socket.emit('sessionRow', row); // }, + dispatchMessage(sesid, message) { + if (_.isString(message)) { + socket.emit(`session-info-${sesid}`, { + message, + time: new Date(), + severity: 'info', + }); + } + if (_.isPlainObject(message)) { + socket.emit(`session-info-${sesid}`, { + time: new Date(), + severity: 'info', + ...message, + }); + } + }, handle_info(sesid, props) { const { info } = props; - socket.emit(`session-info-${sesid}`, info); + this.dispatchMessage(sesid, info); }, handle_done(sesid) { socket.emit(`session-done-${sesid}`); + this.dispatchMessage(sesid, 'Query execution finished'); }, handle_recordset(sesid, props) { @@ -62,6 +83,7 @@ module.exports = { } console.log(`Processing query, sesid=${sesid}, sql=${sql}`); + this.dispatchMessage(sesid, 'Query execution started'); session.subprocess.send({ msgtype: 'executeQuery', sql }); return { state: 'ok' }; diff --git a/packages/api/src/proc/sessionProcess.js b/packages/api/src/proc/sessionProcess.js index 6d3b26ce..a7404e26 100644 --- a/packages/api/src/proc/sessionProcess.js +++ b/packages/api/src/proc/sessionProcess.js @@ -14,7 +14,7 @@ class StreamHandler { constructor() { this.recordset = this.recordset.bind(this); this.row = this.row.bind(this); - this.error = this.error.bind(this); + // this.error = this.error.bind(this); this.done = this.done.bind(this); this.info = this.info.bind(this); } @@ -38,9 +38,9 @@ class StreamHandler { // console.log('ACCEPT ROW', row); this.currentStream.write(JSON.stringify(row) + '\n'); } - error(error) { - process.send({ msgtype: 'error', error }); - } + // error(error) { + // process.send({ msgtype: 'error', error }); + // } done(result) { this.closeCurrentStream(); process.send({ msgtype: 'done', result }); @@ -91,7 +91,14 @@ function start() { try { await handleMessage(message); } catch (e) { - process.send({ msgtype: 'error', error: e.message }); + process.send({ + msgtype: 'info', + info: { + message: e.message, + severity: 'error', + }, + }); + //process.send({ msgtype: 'error', error: e.message }); } }); } diff --git a/packages/datalib/src/GridDisplay.ts b/packages/datalib/src/GridDisplay.ts index 00eed605..f8a1b6da 100644 --- a/packages/datalib/src/GridDisplay.ts +++ b/packages/datalib/src/GridDisplay.ts @@ -74,13 +74,14 @@ export abstract class GridDisplay { } reload() { - this.setCache(cache => ({ + this.setCache((cache) => ({ ...cache, refreshTime: new Date().getTime(), })); } includeInColumnSet(field: keyof GridConfigColumns, uniqueName: string, isIncluded: boolean) { + // console.log('includeInColumnSet', field, uniqueName, isIncluded); if (isIncluded) { this.setConfig({ ...this.config, @@ -89,7 +90,7 @@ export abstract class GridDisplay { } else { this.setConfig({ ...this.config, - [field]: (this.config[field] || []).filter(x => x != uniqueName), + [field]: (this.config[field] || []).filter((x) => x != uniqueName), }); } } @@ -104,12 +105,12 @@ export abstract class GridDisplay { hideAllColumns() { this.setConfig({ ...this.config, - hiddenColumns: this.columns.map(x => x.uniqueName), + hiddenColumns: this.columns.map((x) => x.uniqueName), }); } get hiddenColumnIndexes() { - return (this.config.hiddenColumns || []).map(x => _.findIndex(this.columns, y => y.uniqueName == x)); + return (this.config.hiddenColumns || []).map((x) => _.findIndex(this.columns, (y) => y.uniqueName == x)); } enrichExpandedColumns(list: DisplayColumn[]): DisplayColumn[] { @@ -134,8 +135,8 @@ export abstract class GridDisplay { requireFkTarget(column: DisplayColumn) { const { uniqueName, foreignKey } = column; - this.getTableInfo({ schemaName: foreignKey.refSchemaName, pureName: foreignKey.refTableName }).then(table => { - this.setCache(cache => ({ + this.getTableInfo({ schemaName: foreignKey.refSchemaName, pureName: foreignKey.refTableName }).then((table) => { + this.setCache((cache) => ({ ...cache, tables: { ...cache.tables, @@ -146,6 +147,7 @@ export abstract class GridDisplay { } isColumnChecked(column: DisplayColumn) { + // console.log('isColumnChecked', column, this.config.hiddenColumns); return column.uniquePath.length == 1 ? !this.config.hiddenColumns.includes(column.uniqueName) : this.config.addedColumns.includes(column.uniqueName); @@ -162,10 +164,10 @@ export abstract class GridDisplay { headerText: uniquePath.length == 1 ? col.columnName : `${table.pureName}.${col.columnName}`, uniqueName, uniquePath, - isPrimaryKey: table.primaryKey && !!table.primaryKey.columns.find(x => x.columnName == col.columnName), + isPrimaryKey: table.primaryKey && !!table.primaryKey.columns.find((x) => x.columnName == col.columnName), foreignKey: table.foreignKeys && - table.foreignKeys.find(fk => fk.columns.length == 1 && fk.columns[0].columnName == col.columnName), + table.foreignKeys.find((fk) => fk.columns.length == 1 && fk.columns[0].columnName == col.columnName), }; } @@ -231,7 +233,7 @@ export abstract class GridDisplay { addReferenceToSelect(select: Select, parentAlias: string, column: DisplayColumn) { const childAlias = `${column.uniqueName}_ref`; - if ((select.from.relations || []).find(x => x.alias == childAlias)) return; + if ((select.from.relations || []).find((x) => x.alias == childAlias)) return; const table = this.cache.tables[column.uniqueName]; select.from.relations = [ ...(select.from.relations || []), @@ -265,7 +267,7 @@ export abstract class GridDisplay { if (column.foreignKey) { const table = this.cache.tables[column.uniqueName]; if (table) { - const hintColumn = table.columns.find(x => x?.dataType?.toLowerCase()?.includes('char')); + const hintColumn = table.columns.find((x) => x?.dataType?.toLowerCase()?.includes('char')); if (hintColumn) { const parentUniqueName = column.uniquePath.slice(0, -1).join('.'); this.addReferenceToSelect(select, parentUniqueName ? `${parentUniqueName}_ref` : 'basetbl', column); @@ -315,9 +317,9 @@ export abstract class GridDisplay { applySortOnSelect(select: Select, displayedColumnInfo: DisplayedColumnInfo) { if (this.config.sort?.length > 0) { select.orderBy = this.config.sort - .map(col => ({ ...col, dispInfo: displayedColumnInfo[col.uniqueName] })) - .filter(col => col.dispInfo) - .map(col => ({ + .map((col) => ({ ...col, dispInfo: displayedColumnInfo[col.uniqueName] })) + .filter((col) => col.dispInfo) + .map((col) => ({ exprType: 'column', columnName: col.dispInfo.columnName, direction: col.order, @@ -329,8 +331,8 @@ export abstract class GridDisplay { getDisplayColumns(table: TableInfo, parentPath: string[]) { return ( table?.columns - ?.map(col => this.getDisplayColumn(table, col, parentPath)) - ?.map(col => ({ + ?.map((col) => this.getDisplayColumn(table, col, parentPath)) + ?.map((col) => ({ ...col, isChecked: this.isColumnChecked(col), hintColumnName: col.foreignKey ? `hint_${col.uniqueName}` : null, @@ -339,11 +341,11 @@ export abstract class GridDisplay { } getColumns(columnFilter) { - return this.enrichExpandedColumns(this.columns.filter(col => filterName(columnFilter, col.columnName))); + return this.enrichExpandedColumns(this.columns.filter((col) => filterName(columnFilter, col.columnName))); } getGridColumns() { - return this.getColumns(null).filter(x => this.isColumnChecked(x)); + return this.getColumns(null).filter((x) => this.isColumnChecked(x)); } isExpandedColumn(uniqueName: string) { @@ -378,7 +380,7 @@ export abstract class GridDisplay { } getSortOrder(uniqueName) { - return this.config.sort.find(x => x.uniqueName == uniqueName)?.order; + return this.config.sort.find((x) => x.uniqueName == uniqueName)?.order; } get filterCount() { @@ -399,7 +401,7 @@ export abstract class GridDisplay { } getChangeSetField(row, uniqueName, insertedRowIndex): ChangeSetFieldDefinition { - const col = this.columns.find(x => x.uniqueName == uniqueName); + const col = this.columns.find((x) => x.uniqueName == uniqueName); if (!col) return null; if (!this.baseTable) return null; if (this.baseTable.pureName != col.pureName || this.baseTable.schemaName != col.schemaName) return null; diff --git a/packages/datalib/src/JslGridDisplay.ts b/packages/datalib/src/JslGridDisplay.ts index 0d847e10..0119a87a 100644 --- a/packages/datalib/src/JslGridDisplay.ts +++ b/packages/datalib/src/JslGridDisplay.ts @@ -12,13 +12,19 @@ export class JslGridDisplay extends GridDisplay { setCache: ChangeCacheFunc ) { super(config, setConfig, cache, setCache, null, null); - this.columns = columns.map((col) => ({ - columnName: col.columnName, - headerText: col.columnName, - uniqueName: col.columnName, - uniquePath: [col.columnName], - notNull: col.notNull, - autoIncrement: col.autoIncrement, - })); + + this.columns = columns + .map((col) => ({ + columnName: col.columnName, + headerText: col.columnName, + uniqueName: col.columnName, + uniquePath: [col.columnName], + notNull: col.notNull, + autoIncrement: col.autoIncrement, + })) + ?.map((col) => ({ + ...col, + isChecked: this.isColumnChecked(col), + })); } } diff --git a/packages/engines/mssql/index.js b/packages/engines/mssql/index.js index 919cb740..f1d10130 100644 --- a/packages/engines/mssql/index.js +++ b/packages/engines/mssql/index.js @@ -63,6 +63,7 @@ const driver = { line: lineNumber, procedure: procName, time: new Date(), + severity: 'info', }); }; @@ -79,10 +80,21 @@ const driver = { options.recordset(extractColumns(columns)); }; + const handleError = (error) => { + const { message, lineNumber, procName } = error; + options.info({ + message, + line: lineNumber, + procedure: procName, + time: new Date(), + severity: 'error', + }); + }; + request.stream = true; request.on('recordset', handleRecordset); request.on('row', handleRow); - request.on('error', options.error); + request.on('error', handleError); request.on('done', handleDone); request.on('info', handleInfo); request.query(sql); diff --git a/packages/web/src/sqleditor/JslDataGrid.js b/packages/web/src/sqleditor/JslDataGrid.js index c7828abb..efab38ed 100644 --- a/packages/web/src/sqleditor/JslDataGrid.js +++ b/packages/web/src/sqleditor/JslDataGrid.js @@ -14,6 +14,8 @@ export default function JslDataGrid({ jslid }) { const display = React.useMemo(() => new JslGridDisplay(jslid, columns, config, setConfig, cache, setCache), [ jslid, columns, + config, + cache, ]); return ;