Format MSSQL query logs as executable multi-line SQL with DECLARE parameter headers (including TVP support)#170
Conversation
|
@copilot formatSqlQueryForLog in lib/sql.js doesn't handle mssql.Table (TVP) parameters — it dumps them as JSON. Add detection via instanceof mssql.Table, then for TVPs generate executable T-SQL: DECLARE @name [schema].[type] using table.path, followed by INSERT INTO @name (col1, col2) VALUES (val1, val2) using table.columns for names and the existing stringifySqlValue for values — batch up to 100 rows per INSERT with multi-row VALUES. Mix TVP DECLARE+INSERT before scalar DECLARE lines. Add a test in tests/sql-log-format.test.js that creates an mssql.Table, passes it as a parameter, and asserts the output contains the type path and readable row values (not JSON). |
Implemented in commit
I also added tests in |
There was a problem hiding this comment.
Pull request overview
This PR improves MSSQL query logging by formatting logged queries as readable, executable multi-line SQL, including generated DECLARE statements for scalar parameters and expanded TVP (mssql.Table) parameters as DECLARE + batched INSERT statements. It updates the slow-query, threshold logger, and error logger paths to use the new formatter and adds targeted tests to validate the emitted log shape.
Changes:
- Added
formatSqlQueryForLogto generate executable SQL log output (scalar params + TVP support with 100-row batching). - Updated MSSQL slow-query, threshold logging (
createQueryLogger), and error logging (runQuerycatch) to emit formatted SQL. - Added
tests/sql-log-format.test.jsto verify formatting behavior and usage across logging call sites.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| lib/sql.js | Adds SQL log formatting helpers and applies them to query duration, slow-query, and error logging paths. |
| tests/sql-log-format.test.js | Introduces coverage to validate the formatted SQL output (including TVPs and batching) and that all intended log sites use it. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
DECLARE statements for NVARCHAR/NCHAR/NTEXT parameters and TVP columns now use the correct N'...' prefix instead of VARCHAR literals, preventing silent corruption of non-ASCII characters in logged executable SQL.
| import { test } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import Sql, { createQueryLogger, formatSqlQueryForLog, mssql } from '../lib/sql.js'; | ||
|
|
| assert.match(query, /DECLARE @Big BIGINT = 9007199254740993/); | ||
| assert.match(query, /DECLARE @Circular NVARCHAR\(MAX\) = '\[Unserializable:/); |
SQL query logs were difficult to read because query text and parameters were emitted as JSON fields with escaped control characters. This updates MSSQL logging to emit readable multi-line SQL and prepend parameter
DECLAREstatements with inferred SQL types and correctly formatted values.Added SQL log formatter for
{ query, parameters }formatSqlQueryForLoginlib/sql.jsDECLARElines:INT,VARCHAR(n),DATETIME2,BIT, etc.)'escaped strings',NULL,1/0for bit, datetime literals, hex for buffers)Added TVP (
mssql.Table) formatting supportinstanceof mssql.TableDECLARE @Param [schema].[Type](fromtable.path)INSERT INTO @Param ([col1], [col2], ...) VALUES (...)table.columnsVALUESApplied formatter at all MSSQL query log sites
logSlowQuery)createQueryLogger)runQuerycatch path)Added focused coverage for log output shape
tests/sql-log-format.test.jsDECLAREgeneration + type/value formatting