Skip to content

Allow displaying multiple results #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Pool {
end: () => void;
}

export type QueryResult = ResultTable | string;
export type QueryResult = ResultTable[] | string;

export type Row = { [key: string]: string | number | null };

Expand All @@ -36,6 +36,7 @@ interface BaseConfig {

interface MySQLConfig extends BaseConfig {
driver: 'mysql';
multipleStatements: boolean;
}

export async function getPool(c: PoolConfig): Promise<Pool> {
Expand All @@ -57,8 +58,16 @@ async function createMySQLPool({
user,
password,
database,
multipleStatements,
}: MySQLConfig): Promise<Pool> {
return mysqlPool(mysql.createPool({ host, port, user, password, database }));
return mysqlPool(mysql.createPool({
host,
port,
user,
password,
database,
multipleStatements,
}));
}

function mysqlPool(pool: mysql.Pool): Pool {
Expand All @@ -77,12 +86,16 @@ function mysqlConn(conn: mysql.PoolConnection): Conn {
destroy() {
conn.destroy();
},
async query(q: string): Promise<ResultTable> {
async query(q: string): Promise<ResultTable[]> {
const [result] = (await conn.query(q)) as any;
if (!result.length) {
return [result] as ResultTable;
return [[result] as ResultTable];
}
return result as ResultTable;
if (result.length > 0 && !!result[0].length) {
// Nested row data for multiple statements.
return result as ResultTable[];
}
return [result as ResultTable];
},
release() {
conn.release();
Expand Down Expand Up @@ -125,9 +138,17 @@ function postgresPool(pool: pg.Pool): Pool {

function postgresConn(conn: pg.PoolClient): Conn {
return {
async query(q: string): Promise<ResultTable> {
async query(q: string): Promise<ResultTable[]> {
const response = await conn.query(q);
return response.rows;

// Typings for pg unfortunately miss that `query` may return an array of
// results when the query strings contains multiple sql statements.
const maybeResponses = response as any as pg.QueryResult<any>[];
if (!!maybeResponses.length) {
return maybeResponses.map(r => r.rows);
}

return [response.rows];
},
destroy() {
// TODO: verify
Expand Down Expand Up @@ -180,7 +201,7 @@ function mssqlConn(req: mssql.Request): Conn {
if (res.recordsets.length < 1) {
return `Rows affected: ${res.rowsAffected}`;
}
return res.recordsets[0];
return [res.recordsets[0]];
},
release() {
// TODO: verify correctness
Expand Down
16 changes: 9 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ class SQLNotebookController {
writeSuccess(execution, result);
return;
}
writeSuccess(execution, resultToMarkdownTable(result), 'text/markdown');
const tables = result.map(r => resultToMarkdownTable(r));
writeSuccess(execution, tables, 'text/markdown');
}
}

Expand Down Expand Up @@ -220,14 +221,15 @@ const writeErr = (execution: vscode.NotebookCellExecution, err: string) => {

const writeSuccess = (
execution: vscode.NotebookCellExecution,
text: string,
text: string | string[],
mimeType?: string
) => {
execution.replaceOutput([
new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text(text, mimeType),
]),
]);
const items = typeof text === 'string' ? [text] : text;
execution.replaceOutput(
items.map(item => new vscode.NotebookCellOutput([
vscode.NotebookCellOutputItem.text(item, mimeType),
])),
);
execution.end(true, Date.now());
};

Expand Down
8 changes: 7 additions & 1 deletion webview/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ function useDropdownValue() {
function showDriverConfig(driver: string) {
switch (driver) {
case 'mysql':
return <></>;
return (
<>
<vscode-checkbox name="multipleStatements" checked>
Multiple statements
</vscode-checkbox>
</>
);
case 'postgres':
return <></>;
case 'mssql':
Expand Down