(sql: string, editor: vscode.TextEditor, connectionOptions: IConnection, showInCurrentPanel: boolean = false)
| 125 | } |
| 126 | |
| 127 | public static async runQuery(sql: string, editor: vscode.TextEditor, connectionOptions: IConnection, showInCurrentPanel: boolean = false) { |
| 128 | // let uri = editor.document.uri.toString(); |
| 129 | // let title = path.basename(editor.document.fileName); |
| 130 | // let resultsUri = vscode.Uri.parse('postgres-results://' + uri); |
| 131 | let uri: string = ''; |
| 132 | let title: string = ''; |
| 133 | if (showInCurrentPanel) { |
| 134 | queryCounter++; |
| 135 | uri = `unnamed-query-${queryCounter}`; |
| 136 | title = `Unnamed Query ${queryCounter}`; |
| 137 | } else { |
| 138 | uri = editor.document.uri.toString(); |
| 139 | title = path.basename(editor.document.fileName); |
| 140 | } |
| 141 | let resultsUri = vscode.Uri.parse('postgres-results://' + uri); |
| 142 | |
| 143 | OutputChannel.displayMessage(resultsUri, 'Results: ' + title, 'Waiting for the query to complete...', showInCurrentPanel); |
| 144 | let connection: PgClient = null; |
| 145 | try { |
| 146 | let startTime = performance.now(); |
| 147 | connection = await Database.createConnection(connectionOptions); |
| 148 | const typeNamesQuery = `select oid, format_type(oid, typtypmod) as display_type, typname from pg_type`; |
| 149 | const types: TypeResults = await connection.query(typeNamesQuery); |
| 150 | const res: QueryResults | QueryResults[] = await connection.query({ text: sql, rowMode: 'array' }); |
| 151 | const results: QueryResults[] = Array.isArray(res) ? res : [res]; |
| 152 | let durationText = Database.getDurationText(performance.now() - startTime); |
| 153 | |
| 154 | OutputChannel.displayMessage(resultsUri, 'Results: ' + title, 'Query completed in ' + durationText + '. Building results view...', showInCurrentPanel); |
| 155 | vscode.window.showInformationMessage('Query completed in ' + durationText + '.'); |
| 156 | results.forEach((result) => { |
| 157 | result.fields.forEach((field) => { |
| 158 | let type = types.rows.find((t) => t.oid === field.dataTypeID); |
| 159 | if (type) { |
| 160 | field.format = type.typname; |
| 161 | field.display_type = type.display_type; |
| 162 | } |
| 163 | }); |
| 164 | }); |
| 165 | OutputChannel.displayResults(resultsUri, 'Results: ' + title, results, showInCurrentPanel); |
| 166 | if (!showInCurrentPanel) { |
| 167 | vscode.window.showTextDocument(editor.document, editor.viewColumn); |
| 168 | } |
| 169 | } catch (err) { |
| 170 | OutputChannel.displayMessage(resultsUri, 'Results: ' + title, 'ERROR: ' + err.message, showInCurrentPanel); |
| 171 | OutputChannel.appendLine(err); |
| 172 | vscode.window.showErrorMessage(err.message); |
| 173 | // vscode.window.showErrorMessage(err.message, "Show Console").then((button) => { |
| 174 | // if (button === 'Show Console') { |
| 175 | // OutputChannel.show(); |
| 176 | // } |
| 177 | // }); |
| 178 | } finally { |
| 179 | if (connection) |
| 180 | await connection.end(); |
| 181 | } |
| 182 | } |
| 183 | } |
no test coverage detected