Skip to content

Commit e955f6c

Browse files
authored
add query timeout configuration option (#27)
Allow user to specify query timeout in the vscode settings menu. Default to 30 seconds.
1 parent c8ab0cf commit e955f6c

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
- When running on a compatible arch/os, notebooks will now
77
benefit from enhacned typed autocomplete and hover information
88
when connected to a valid database connection.
9+
- New configuration option for query timeout in milliseconds. Defaults to 30000.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ and view output interactively.
1515
- Open any `.sql` file as a Notebook.
1616
- Execute query blocks in the Notebook UI and view output.
1717
- Configure database connections in the SQL Notebook sidepanel.
18-
- Supports MySQL PostgreSQL, and MSSQL (Oracle support coming soon).
19-
- (coming soon) Built-in typed autocomplete.
18+
- Supports MySQL PostgreSQL, and MSSQL (OracleDB and SQLite support coming soon).
19+
- (unstable) Built-in typed autocomplete with an embeded language server.
2020

2121
## Usage
2222

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@
9797
"type": "boolean",
9898
"default": false,
9999
"description": "(Unstable) Use embeded language server for intelligent completion and hover information."
100+
},
101+
"SQLNotebook.queryTimeout": {
102+
"type": "number",
103+
"default": 30000,
104+
"description": "Query timeout in milliseconds for cell query execution."
100105
}
101106
}
102107
},

src/commands.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ export const connectToDatabase =
6969
}
7070

7171
try {
72-
globalConnPool.pool = await getPool({ ...match, password } as PoolConfig);
72+
globalConnPool.pool = await getPool({
73+
...match,
74+
password,
75+
queryTimeout: getQueryTimeoutConfiguration(),
76+
} as PoolConfig);
7377
const conn = await globalConnPool.pool.getConnection();
7478
await conn.query('SELECT 1'); // essentially a ping to see if the connection works
7579
connectionsSidepanel.setActive(match.name);
@@ -125,3 +129,11 @@ function shouldUseLanguageServer(): boolean {
125129
false
126130
);
127131
}
132+
133+
function getQueryTimeoutConfiguration() {
134+
const defaultTimeout = 30000; // make this the same as the package.json-level configuration default
135+
return (
136+
vscode.workspace.getConfiguration('SQLNotebook').get('queryTimeout') ??
137+
defaultTimeout
138+
);
139+
}

src/driver.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ interface BaseConfig {
4040
user: string;
4141
password?: string;
4242
database?: string;
43+
44+
queryTimeout: number;
4345
}
4446

4547
interface MySQLConfig extends BaseConfig {
@@ -67,6 +69,7 @@ async function createMySQLPool({
6769
password,
6870
database,
6971
multipleStatements,
72+
queryTimeout,
7073
}: MySQLConfig): Promise<Pool> {
7174
return mysqlPool(
7275
mysql.createPool({
@@ -76,28 +79,32 @@ async function createMySQLPool({
7679
password,
7780
database,
7881
multipleStatements,
79-
})
82+
}),
83+
queryTimeout
8084
);
8185
}
8286

83-
function mysqlPool(pool: mysql.Pool): Pool {
87+
function mysqlPool(pool: mysql.Pool, queryTimeout: number): Pool {
8488
return {
8589
async getConnection(): Promise<Conn> {
86-
return mysqlConn(await pool.getConnection());
90+
return mysqlConn(await pool.getConnection(), queryTimeout);
8791
},
8892
end() {
8993
pool.end();
9094
},
9195
};
9296
}
9397

94-
function mysqlConn(conn: mysql.PoolConnection): Conn {
98+
function mysqlConn(conn: mysql.PoolConnection, queryTimeout: number): Conn {
9599
return {
96100
destroy() {
97101
conn.destroy();
98102
},
99103
async query(q: string): Promise<ExecutionResult> {
100-
const [result, ok] = (await conn.query(q)) as any;
104+
const [result, ok] = (await conn.query({
105+
sql: q,
106+
timeout: queryTimeout,
107+
})) as any;
101108
console.debug('mysql query result', { result, ok });
102109

103110
if (!result.length) {
@@ -132,13 +139,15 @@ async function createPostgresPool({
132139
user,
133140
password,
134141
database,
142+
queryTimeout,
135143
}: PostgresConfig): Promise<Pool> {
136144
const pool = new pg.Pool({
137145
host,
138146
port,
139147
password,
140148
database,
141149
user,
150+
query_timeout: queryTimeout,
142151
});
143152
return postgresPool(pool);
144153
}
@@ -197,6 +206,7 @@ async function createMSSQLPool(config: MSSQLConfig): Promise<Pool> {
197206
user: config.user,
198207
password: config.password,
199208
database: config.database,
209+
requestTimeout: config.queryTimeout,
200210
options: {
201211
encrypt: config.encrypt,
202212
trustServerCertificate: config.trustServerCertificate,

0 commit comments

Comments
 (0)