@tejasanik/postgres-mcp-server 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +186 -10
- package/dist/db-manager/index.d.ts +7 -0
- package/dist/db-manager/index.d.ts.map +1 -0
- package/dist/db-manager/index.js +7 -0
- package/dist/db-manager/index.js.map +1 -0
- package/dist/db-manager/validation.d.ts +35 -0
- package/dist/db-manager/validation.d.ts.map +1 -0
- package/dist/db-manager/validation.js +54 -0
- package/dist/db-manager/validation.js.map +1 -0
- package/dist/db-manager.d.ts +175 -5
- package/dist/db-manager.d.ts.map +1 -1
- package/dist/db-manager.js +589 -26
- package/dist/db-manager.js.map +1 -1
- package/dist/index.js +141 -11
- package/dist/index.js.map +1 -1
- package/dist/tools/analysis-tools.d.ts.map +1 -1
- package/dist/tools/analysis-tools.js +53 -49
- package/dist/tools/analysis-tools.js.map +1 -1
- package/dist/tools/schema-tools.d.ts +40 -1
- package/dist/tools/schema-tools.d.ts.map +1 -1
- package/dist/tools/schema-tools.js +174 -92
- package/dist/tools/schema-tools.js.map +1 -1
- package/dist/tools/server-tools.d.ts +1 -0
- package/dist/tools/server-tools.d.ts.map +1 -1
- package/dist/tools/server-tools.js +10 -6
- package/dist/tools/server-tools.js.map +1 -1
- package/dist/tools/sql/utils/connection-utils.d.ts +79 -0
- package/dist/tools/sql/utils/connection-utils.d.ts.map +1 -0
- package/dist/tools/sql/utils/connection-utils.js +129 -0
- package/dist/tools/sql/utils/connection-utils.js.map +1 -0
- package/dist/tools/sql/utils/constants.d.ts +55 -0
- package/dist/tools/sql/utils/constants.d.ts.map +1 -0
- package/dist/tools/sql/utils/constants.js +55 -0
- package/dist/tools/sql/utils/constants.js.map +1 -0
- package/dist/tools/sql/utils/dry-run-utils.d.ts +31 -0
- package/dist/tools/sql/utils/dry-run-utils.d.ts.map +1 -0
- package/dist/tools/sql/utils/dry-run-utils.js +173 -0
- package/dist/tools/sql/utils/dry-run-utils.js.map +1 -0
- package/dist/tools/sql/utils/file-handler.d.ts +57 -0
- package/dist/tools/sql/utils/file-handler.d.ts.map +1 -0
- package/dist/tools/sql/utils/file-handler.js +150 -0
- package/dist/tools/sql/utils/file-handler.js.map +1 -0
- package/dist/tools/sql/utils/index.d.ts +12 -0
- package/dist/tools/sql/utils/index.d.ts.map +1 -0
- package/dist/tools/sql/utils/index.js +12 -0
- package/dist/tools/sql/utils/index.js.map +1 -0
- package/dist/tools/sql/utils/result-formatter.d.ts +94 -0
- package/dist/tools/sql/utils/result-formatter.d.ts.map +1 -0
- package/dist/tools/sql/utils/result-formatter.js +154 -0
- package/dist/tools/sql/utils/result-formatter.js.map +1 -0
- package/dist/tools/sql/utils/sql-parser.d.ts +125 -0
- package/dist/tools/sql/utils/sql-parser.d.ts.map +1 -0
- package/dist/tools/sql/utils/sql-parser.js +468 -0
- package/dist/tools/sql/utils/sql-parser.js.map +1 -0
- package/dist/tools/sql-tools.d.ts +21 -0
- package/dist/tools/sql-tools.d.ts.map +1 -1
- package/dist/tools/sql-tools.js +383 -532
- package/dist/tools/sql-tools.js.map +1 -1
- package/dist/types.d.ts +38 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/retry.d.ts +1 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/validation.d.ts +45 -9
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +335 -72
- package/dist/utils/validation.js.map +1 -1
- package/package.json +9 -2
- package/dist/__tests__/analysis-tools.test.d.ts +0 -2
- package/dist/__tests__/analysis-tools.test.d.ts.map +0 -1
- package/dist/__tests__/analysis-tools.test.js +0 -294
- package/dist/__tests__/analysis-tools.test.js.map +0 -1
- package/dist/__tests__/db-manager.test.d.ts +0 -2
- package/dist/__tests__/db-manager.test.d.ts.map +0 -1
- package/dist/__tests__/db-manager.test.js +0 -410
- package/dist/__tests__/db-manager.test.js.map +0 -1
- package/dist/__tests__/mcp-server.test.d.ts +0 -13
- package/dist/__tests__/mcp-server.test.d.ts.map +0 -1
- package/dist/__tests__/mcp-server.test.js +0 -146
- package/dist/__tests__/mcp-server.test.js.map +0 -1
- package/dist/__tests__/schema-tools.test.d.ts +0 -2
- package/dist/__tests__/schema-tools.test.d.ts.map +0 -1
- package/dist/__tests__/schema-tools.test.js +0 -171
- package/dist/__tests__/schema-tools.test.js.map +0 -1
- package/dist/__tests__/server-tools.test.d.ts +0 -2
- package/dist/__tests__/server-tools.test.d.ts.map +0 -1
- package/dist/__tests__/server-tools.test.js +0 -113
- package/dist/__tests__/server-tools.test.js.map +0 -1
- package/dist/__tests__/sql-tools.test.d.ts +0 -2
- package/dist/__tests__/sql-tools.test.d.ts.map +0 -1
- package/dist/__tests__/sql-tools.test.js +0 -1912
- package/dist/__tests__/sql-tools.test.js.map +0 -1
- package/dist/__tests__/validation.test.d.ts +0 -2
- package/dist/__tests__/validation.test.d.ts.map +0 -1
- package/dist/__tests__/validation.test.js +0 -203
- package/dist/__tests__/validation.test.js.map +0 -1
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File Handler Utilities
|
|
3
|
+
*
|
|
4
|
+
* Functions for validating and processing SQL files.
|
|
5
|
+
* Handles file validation, reading, and preprocessing.
|
|
6
|
+
*/
|
|
7
|
+
import * as fs from 'fs';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
import { MAX_SQL_FILE_SIZE } from './constants.js';
|
|
10
|
+
/**
|
|
11
|
+
* Validates a SQL file path and returns file information.
|
|
12
|
+
*
|
|
13
|
+
* @param filePath - Path to the SQL file
|
|
14
|
+
* @returns Validation result with resolved path and file size
|
|
15
|
+
*/
|
|
16
|
+
export function validateSqlFile(filePath) {
|
|
17
|
+
// Check file extension
|
|
18
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
19
|
+
if (ext !== '.sql') {
|
|
20
|
+
return {
|
|
21
|
+
isValid: false,
|
|
22
|
+
resolvedPath: '',
|
|
23
|
+
fileSize: 0,
|
|
24
|
+
error: 'Only .sql files are allowed. Received file extension: ' + ext,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
// Resolve path
|
|
28
|
+
const resolvedPath = path.resolve(filePath);
|
|
29
|
+
// Check file exists
|
|
30
|
+
if (!fs.existsSync(resolvedPath)) {
|
|
31
|
+
return {
|
|
32
|
+
isValid: false,
|
|
33
|
+
resolvedPath,
|
|
34
|
+
fileSize: 0,
|
|
35
|
+
error: `File not found: ${filePath}`,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
// Get file stats
|
|
39
|
+
const stats = fs.statSync(resolvedPath);
|
|
40
|
+
// Check it's a file
|
|
41
|
+
if (!stats.isFile()) {
|
|
42
|
+
return {
|
|
43
|
+
isValid: false,
|
|
44
|
+
resolvedPath,
|
|
45
|
+
fileSize: 0,
|
|
46
|
+
error: `Not a file: ${filePath}`,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
// Check file size
|
|
50
|
+
if (stats.size > MAX_SQL_FILE_SIZE) {
|
|
51
|
+
return {
|
|
52
|
+
isValid: false,
|
|
53
|
+
resolvedPath,
|
|
54
|
+
fileSize: stats.size,
|
|
55
|
+
error: `File too large: ${formatFileSize(stats.size)}. Maximum allowed: ${formatFileSize(MAX_SQL_FILE_SIZE)}`,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
// Check file not empty
|
|
59
|
+
if (stats.size === 0) {
|
|
60
|
+
return {
|
|
61
|
+
isValid: false,
|
|
62
|
+
resolvedPath,
|
|
63
|
+
fileSize: 0,
|
|
64
|
+
error: 'File is empty',
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
isValid: true,
|
|
69
|
+
resolvedPath,
|
|
70
|
+
fileSize: stats.size,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Reads a SQL file and optionally preprocesses it.
|
|
75
|
+
*
|
|
76
|
+
* @param resolvedPath - Resolved path to the SQL file
|
|
77
|
+
* @param stripPatterns - Optional patterns to remove from content
|
|
78
|
+
* @param stripAsRegex - If true, patterns are regex; if false, literal strings
|
|
79
|
+
* @returns Preprocessed SQL content
|
|
80
|
+
*/
|
|
81
|
+
export function readSqlFile(resolvedPath, stripPatterns, stripAsRegex = false) {
|
|
82
|
+
let content = fs.readFileSync(resolvedPath, 'utf-8');
|
|
83
|
+
if (stripPatterns && stripPatterns.length > 0) {
|
|
84
|
+
content = preprocessSqlContent(content, stripPatterns, stripAsRegex);
|
|
85
|
+
}
|
|
86
|
+
return content;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Preprocess SQL content by removing patterns.
|
|
90
|
+
* Supports both literal string matching and regex patterns.
|
|
91
|
+
*
|
|
92
|
+
* @param sql - The SQL content to preprocess
|
|
93
|
+
* @param patterns - Array of patterns to remove from SQL content
|
|
94
|
+
* @param isRegex - If true, patterns are treated as regex; if false, as literal strings
|
|
95
|
+
* @returns Preprocessed SQL content
|
|
96
|
+
*/
|
|
97
|
+
export function preprocessSqlContent(sql, patterns, isRegex = false) {
|
|
98
|
+
let result = sql;
|
|
99
|
+
for (const pattern of patterns) {
|
|
100
|
+
try {
|
|
101
|
+
if (isRegex) {
|
|
102
|
+
// Treat as regex pattern (multiline by default)
|
|
103
|
+
const regex = new RegExp(pattern, 'gm');
|
|
104
|
+
result = result.replace(regex, '');
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
// Treat as literal string - escape and match on its own line
|
|
108
|
+
const escapedPattern = pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
109
|
+
const regex = new RegExp(`^\\s*${escapedPattern}\\s*$`, 'gm');
|
|
110
|
+
result = result.replace(regex, '');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
// Invalid regex - skip this pattern silently in production
|
|
115
|
+
// Log only in non-production for debugging
|
|
116
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
117
|
+
console.error(`Warning: Invalid pattern "${pattern}": ${error instanceof Error ? error.message : String(error)}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Format file size in human-readable format.
|
|
125
|
+
*
|
|
126
|
+
* @param bytes - File size in bytes
|
|
127
|
+
* @returns Human-readable file size (e.g., "1.5 MB")
|
|
128
|
+
*/
|
|
129
|
+
export function formatFileSize(bytes) {
|
|
130
|
+
if (bytes === 0)
|
|
131
|
+
return '0 B';
|
|
132
|
+
const units = ['B', 'KB', 'MB', 'GB'];
|
|
133
|
+
const base = 1024;
|
|
134
|
+
const unitIndex = Math.min(Math.floor(Math.log(bytes) / Math.log(base)), units.length - 1);
|
|
135
|
+
const size = bytes / Math.pow(base, unitIndex);
|
|
136
|
+
return `${size.toFixed(unitIndex > 0 ? 1 : 0)} ${units[unitIndex]}`;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Ensure a file path is safe (no path traversal attacks).
|
|
140
|
+
*
|
|
141
|
+
* @param basePath - Base directory path
|
|
142
|
+
* @param filePath - File path to validate
|
|
143
|
+
* @returns True if the file path is within the base path
|
|
144
|
+
*/
|
|
145
|
+
export function isPathSafe(basePath, filePath) {
|
|
146
|
+
const resolvedBase = path.resolve(basePath);
|
|
147
|
+
const resolvedFile = path.resolve(filePath);
|
|
148
|
+
return resolvedFile.startsWith(resolvedBase);
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=file-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-handler.js","sourceRoot":"","sources":["../../../../src/tools/sql/utils/file-handler.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAA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CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC5C,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC5C,OAAO,YAAY,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL Tools Utilities
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all utility modules for convenient importing.
|
|
5
|
+
*/
|
|
6
|
+
export * from './constants.js';
|
|
7
|
+
export * from './connection-utils.js';
|
|
8
|
+
export * from './dry-run-utils.js';
|
|
9
|
+
export * from './file-handler.js';
|
|
10
|
+
export * from './result-formatter.js';
|
|
11
|
+
export * from './sql-parser.js';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/tools/sql/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL Tools Utilities
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all utility modules for convenient importing.
|
|
5
|
+
*/
|
|
6
|
+
export * from './constants.js';
|
|
7
|
+
export * from './connection-utils.js';
|
|
8
|
+
export * from './dry-run-utils.js';
|
|
9
|
+
export * from './file-handler.js';
|
|
10
|
+
export * from './result-formatter.js';
|
|
11
|
+
export * from './sql-parser.js';
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/tools/sql/utils/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,cAAc,gBAAgB,CAAC;AAC/B,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result Formatter Utilities
|
|
3
|
+
*
|
|
4
|
+
* Functions for formatting query results, handling large outputs,
|
|
5
|
+
* and timing measurements.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Result of large output handling.
|
|
9
|
+
*/
|
|
10
|
+
export interface LargeOutputResult {
|
|
11
|
+
truncated: boolean;
|
|
12
|
+
outputFile?: string;
|
|
13
|
+
rows: unknown[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Calculate execution time from hrtime bigints.
|
|
17
|
+
*
|
|
18
|
+
* @param startTime - Start time from process.hrtime.bigint()
|
|
19
|
+
* @param endTime - End time from process.hrtime.bigint()
|
|
20
|
+
* @returns Execution time in milliseconds (rounded to 2 decimal places)
|
|
21
|
+
*/
|
|
22
|
+
export declare function calculateExecutionTime(startTime: bigint, endTime: bigint): number;
|
|
23
|
+
/**
|
|
24
|
+
* Get current time for timing measurements.
|
|
25
|
+
*
|
|
26
|
+
* @returns Current high-resolution time as bigint
|
|
27
|
+
*/
|
|
28
|
+
export declare function getStartTime(): bigint;
|
|
29
|
+
/**
|
|
30
|
+
* Handle potentially large output by writing to file if necessary.
|
|
31
|
+
*
|
|
32
|
+
* @param rows - Result rows to check
|
|
33
|
+
* @param maxChars - Maximum characters before writing to file
|
|
34
|
+
* @returns Object with truncated flag, optional file path, and rows
|
|
35
|
+
*/
|
|
36
|
+
export declare function handleLargeOutput(rows: unknown[], maxChars?: number): LargeOutputResult;
|
|
37
|
+
/**
|
|
38
|
+
* Paginate result rows.
|
|
39
|
+
*
|
|
40
|
+
* @param rows - All result rows
|
|
41
|
+
* @param offset - Number of rows to skip
|
|
42
|
+
* @param maxRows - Maximum rows to return
|
|
43
|
+
* @returns Paginated rows and metadata
|
|
44
|
+
*/
|
|
45
|
+
export declare function paginateRows(rows: unknown[], offset: number, maxRows: number): {
|
|
46
|
+
rows: unknown[];
|
|
47
|
+
offset: number;
|
|
48
|
+
hasMore: boolean;
|
|
49
|
+
totalCount: number;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Truncate SQL for display in error messages or previews.
|
|
53
|
+
*
|
|
54
|
+
* @param sql - SQL string to truncate
|
|
55
|
+
* @param maxLength - Maximum length (default: 200)
|
|
56
|
+
* @returns Truncated SQL with ellipsis if needed
|
|
57
|
+
*/
|
|
58
|
+
export declare function truncateSql(sql: string, maxLength?: number): string;
|
|
59
|
+
/**
|
|
60
|
+
* Format field names from query result.
|
|
61
|
+
*
|
|
62
|
+
* @param fields - Array of field objects from pg result
|
|
63
|
+
* @returns Array of field names
|
|
64
|
+
*/
|
|
65
|
+
export declare function formatFieldNames(fields: Array<{
|
|
66
|
+
name: string;
|
|
67
|
+
}>): string[];
|
|
68
|
+
/**
|
|
69
|
+
* Create a summary message for statement execution.
|
|
70
|
+
*
|
|
71
|
+
* @param totalStatements - Total number of statements
|
|
72
|
+
* @param successCount - Number of successful statements
|
|
73
|
+
* @param failureCount - Number of failed statements
|
|
74
|
+
* @param skippedCount - Number of skipped statements
|
|
75
|
+
* @param rolledBack - Whether changes were rolled back
|
|
76
|
+
* @returns Summary message string
|
|
77
|
+
*/
|
|
78
|
+
export declare function createExecutionSummary(totalStatements: number, successCount: number, failureCount: number, skippedCount: number, rolledBack: boolean): string;
|
|
79
|
+
/**
|
|
80
|
+
* Count statements by type.
|
|
81
|
+
*
|
|
82
|
+
* @param types - Array of statement types
|
|
83
|
+
* @returns Object with counts by type
|
|
84
|
+
*/
|
|
85
|
+
export declare function countStatementsByType(types: string[]): Record<string, number>;
|
|
86
|
+
/**
|
|
87
|
+
* Create a human-readable file summary.
|
|
88
|
+
*
|
|
89
|
+
* @param statementsByType - Statement counts by type
|
|
90
|
+
* @param totalStatements - Total statement count
|
|
91
|
+
* @returns Summary string
|
|
92
|
+
*/
|
|
93
|
+
export declare function createFileSummary(statementsByType: Record<string, number>, totalStatements: number): string;
|
|
94
|
+
//# sourceMappingURL=result-formatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result-formatter.d.ts","sourceRoot":"","sources":["../../../../src/tools/sql/utils/result-formatter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,SAAS,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,OAAO,EAAE,CAAC;CACjB;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAEjF;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,MAAM,CAErC;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,OAAO,EAAE,EACf,QAAQ,GAAE,MAAyB,GAClC,iBAAiB,CAmBnB;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,OAAO,EAAE,EACf,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GACd;IACD,IAAI,EAAE,OAAO,EAAE,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;CACpB,CAWA;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,GAAE,MAAY,GAAG,MAAM,CAMxE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,GAAG,MAAM,EAAE,CAE1E;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CACpC,eAAe,EAAE,MAAM,EACvB,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,OAAO,GAClB,MAAM,CAwBR;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQ7E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,gBAAgB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACxC,eAAe,EAAE,MAAM,GACtB,MAAM,CAOR"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result Formatter Utilities
|
|
3
|
+
*
|
|
4
|
+
* Functions for formatting query results, handling large outputs,
|
|
5
|
+
* and timing measurements.
|
|
6
|
+
*/
|
|
7
|
+
import * as fs from 'fs';
|
|
8
|
+
import * as path from 'path';
|
|
9
|
+
import * as os from 'os';
|
|
10
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
11
|
+
import { MAX_OUTPUT_CHARS } from './constants.js';
|
|
12
|
+
/**
|
|
13
|
+
* Calculate execution time from hrtime bigints.
|
|
14
|
+
*
|
|
15
|
+
* @param startTime - Start time from process.hrtime.bigint()
|
|
16
|
+
* @param endTime - End time from process.hrtime.bigint()
|
|
17
|
+
* @returns Execution time in milliseconds (rounded to 2 decimal places)
|
|
18
|
+
*/
|
|
19
|
+
export function calculateExecutionTime(startTime, endTime) {
|
|
20
|
+
return Math.round((Number(endTime - startTime) / 1_000_000) * 100) / 100;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get current time for timing measurements.
|
|
24
|
+
*
|
|
25
|
+
* @returns Current high-resolution time as bigint
|
|
26
|
+
*/
|
|
27
|
+
export function getStartTime() {
|
|
28
|
+
return process.hrtime.bigint();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Handle potentially large output by writing to file if necessary.
|
|
32
|
+
*
|
|
33
|
+
* @param rows - Result rows to check
|
|
34
|
+
* @param maxChars - Maximum characters before writing to file
|
|
35
|
+
* @returns Object with truncated flag, optional file path, and rows
|
|
36
|
+
*/
|
|
37
|
+
export function handleLargeOutput(rows, maxChars = MAX_OUTPUT_CHARS) {
|
|
38
|
+
const output = JSON.stringify(rows);
|
|
39
|
+
if (output.length <= maxChars) {
|
|
40
|
+
return { truncated: false, rows };
|
|
41
|
+
}
|
|
42
|
+
// Write to temp file
|
|
43
|
+
const tempDir = os.tmpdir();
|
|
44
|
+
const fileName = `sql-result-${uuidv4()}.json`;
|
|
45
|
+
const filePath = path.join(tempDir, fileName);
|
|
46
|
+
fs.writeFileSync(filePath, output, { mode: 0o600 });
|
|
47
|
+
return {
|
|
48
|
+
truncated: true,
|
|
49
|
+
outputFile: filePath,
|
|
50
|
+
rows: [], // Return empty rows since output is in file
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Paginate result rows.
|
|
55
|
+
*
|
|
56
|
+
* @param rows - All result rows
|
|
57
|
+
* @param offset - Number of rows to skip
|
|
58
|
+
* @param maxRows - Maximum rows to return
|
|
59
|
+
* @returns Paginated rows and metadata
|
|
60
|
+
*/
|
|
61
|
+
export function paginateRows(rows, offset, maxRows) {
|
|
62
|
+
const totalCount = rows.length;
|
|
63
|
+
const paginatedRows = rows.slice(offset, offset + maxRows);
|
|
64
|
+
const hasMore = offset + paginatedRows.length < totalCount;
|
|
65
|
+
return {
|
|
66
|
+
rows: paginatedRows,
|
|
67
|
+
offset,
|
|
68
|
+
hasMore,
|
|
69
|
+
totalCount,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Truncate SQL for display in error messages or previews.
|
|
74
|
+
*
|
|
75
|
+
* @param sql - SQL string to truncate
|
|
76
|
+
* @param maxLength - Maximum length (default: 200)
|
|
77
|
+
* @returns Truncated SQL with ellipsis if needed
|
|
78
|
+
*/
|
|
79
|
+
export function truncateSql(sql, maxLength = 200) {
|
|
80
|
+
const trimmed = sql.trim();
|
|
81
|
+
if (trimmed.length <= maxLength) {
|
|
82
|
+
return trimmed;
|
|
83
|
+
}
|
|
84
|
+
return trimmed.substring(0, maxLength) + '...';
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Format field names from query result.
|
|
88
|
+
*
|
|
89
|
+
* @param fields - Array of field objects from pg result
|
|
90
|
+
* @returns Array of field names
|
|
91
|
+
*/
|
|
92
|
+
export function formatFieldNames(fields) {
|
|
93
|
+
return fields.map((f) => f.name);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Create a summary message for statement execution.
|
|
97
|
+
*
|
|
98
|
+
* @param totalStatements - Total number of statements
|
|
99
|
+
* @param successCount - Number of successful statements
|
|
100
|
+
* @param failureCount - Number of failed statements
|
|
101
|
+
* @param skippedCount - Number of skipped statements
|
|
102
|
+
* @param rolledBack - Whether changes were rolled back
|
|
103
|
+
* @returns Summary message string
|
|
104
|
+
*/
|
|
105
|
+
export function createExecutionSummary(totalStatements, successCount, failureCount, skippedCount, rolledBack) {
|
|
106
|
+
const parts = [];
|
|
107
|
+
if (rolledBack) {
|
|
108
|
+
parts.push(`Dry-run of ${totalStatements} statements:`);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
parts.push(`Executed ${totalStatements} statements:`);
|
|
112
|
+
}
|
|
113
|
+
if (successCount > 0) {
|
|
114
|
+
parts.push(`${successCount} succeeded`);
|
|
115
|
+
}
|
|
116
|
+
if (failureCount > 0) {
|
|
117
|
+
parts.push(`${failureCount} failed`);
|
|
118
|
+
}
|
|
119
|
+
if (skippedCount > 0) {
|
|
120
|
+
parts.push(`${skippedCount} skipped (non-rollbackable)`);
|
|
121
|
+
}
|
|
122
|
+
if (rolledBack) {
|
|
123
|
+
parts.push('All changes rolled back.');
|
|
124
|
+
}
|
|
125
|
+
return parts.join(', ').replace('statements:,', 'statements:');
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Count statements by type.
|
|
129
|
+
*
|
|
130
|
+
* @param types - Array of statement types
|
|
131
|
+
* @returns Object with counts by type
|
|
132
|
+
*/
|
|
133
|
+
export function countStatementsByType(types) {
|
|
134
|
+
const counts = {};
|
|
135
|
+
for (const type of types) {
|
|
136
|
+
counts[type] = (counts[type] || 0) + 1;
|
|
137
|
+
}
|
|
138
|
+
return counts;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Create a human-readable file summary.
|
|
142
|
+
*
|
|
143
|
+
* @param statementsByType - Statement counts by type
|
|
144
|
+
* @param totalStatements - Total statement count
|
|
145
|
+
* @returns Summary string
|
|
146
|
+
*/
|
|
147
|
+
export function createFileSummary(statementsByType, totalStatements) {
|
|
148
|
+
const parts = Object.entries(statementsByType)
|
|
149
|
+
.sort(([, a], [, b]) => b - a)
|
|
150
|
+
.map(([type, count]) => `${count} ${type}`)
|
|
151
|
+
.join(', ');
|
|
152
|
+
return `File contains ${totalStatements} statements: ${parts}`;
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=result-formatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result-formatter.js","sourceRoot":"","sources":["../../../../src/tools/sql/utils/result-formatter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAWlD;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAAC,SAAiB,EAAE,OAAe;IACvE,OAAO,IAAI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IAED,IAAI,UAAU,EAAE,CAAC;QACf,KAAK,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;IACzC,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC;AACjE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAe;IACnD,MAAM,MAAM,GAA2B,EAAE,CAAC;IAE1C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACzC,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC/B,gBAAwC,EACxC,eAAuB;IAEvB,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC;SAC3C,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;SAC7B,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,IAAI,IAAI,EAAE,CAAC;SAC1C,IAAI,CAAC,IAAI,CAAC,CAAC;IAEd,OAAO,iBAAiB,eAAe,gBAAgB,KAAK,EAAE,CAAC;AACjE,CAAC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL Parser Utilities
|
|
3
|
+
*
|
|
4
|
+
* Functions for parsing, analyzing, and splitting SQL statements.
|
|
5
|
+
* Handles PostgreSQL-specific syntax including:
|
|
6
|
+
* - Dollar-quoted strings ($$ or $tag$)
|
|
7
|
+
* - Nested block comments
|
|
8
|
+
* - Single and double-quoted strings with escape handling
|
|
9
|
+
*/
|
|
10
|
+
import { ParsedStatement } from '../../../types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Warning information from SQL parsing.
|
|
13
|
+
*/
|
|
14
|
+
export interface ParseWarning {
|
|
15
|
+
type: 'unclosed_dollar_quote' | 'unclosed_block_comment' | 'unclosed_string';
|
|
16
|
+
message: string;
|
|
17
|
+
lineNumber: number;
|
|
18
|
+
position: number;
|
|
19
|
+
tag?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Result of SQL parsing with optional warnings.
|
|
23
|
+
*/
|
|
24
|
+
export interface ParseResult {
|
|
25
|
+
statements: ParsedStatement[];
|
|
26
|
+
warnings: ParseWarning[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Strips leading line comments and block comments from SQL.
|
|
30
|
+
* Properly handles nested block comments (PostgreSQL supports nesting).
|
|
31
|
+
* Returns empty string if the entire content is just comments.
|
|
32
|
+
*
|
|
33
|
+
* IMPORTANT: This function only strips LEADING comments. Line numbers
|
|
34
|
+
* in the remaining SQL are NOT affected - they still correspond to
|
|
35
|
+
* the original file positions.
|
|
36
|
+
*
|
|
37
|
+
* @param sql - The SQL string to process
|
|
38
|
+
* @returns The SQL with leading comments stripped
|
|
39
|
+
*/
|
|
40
|
+
export declare function stripLeadingComments(sql: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Detect the type of SQL statement (SELECT, INSERT, UPDATE, DELETE, CREATE, etc.)
|
|
43
|
+
*
|
|
44
|
+
* @param sql - The SQL statement to analyze
|
|
45
|
+
* @returns The detected statement type
|
|
46
|
+
*/
|
|
47
|
+
export declare function detectStatementType(sql: string): string;
|
|
48
|
+
/**
|
|
49
|
+
* Split SQL content into individual statements with line number tracking.
|
|
50
|
+
* Handles PostgreSQL-specific syntax including:
|
|
51
|
+
* - Single and double-quoted strings with escape handling
|
|
52
|
+
* - Dollar-quoted strings ($$ or $tag$)
|
|
53
|
+
* - Line comments (--)
|
|
54
|
+
* - Nested block comments (PostgreSQL supports nested comments)
|
|
55
|
+
*
|
|
56
|
+
* Line numbers in the returned statements correspond to actual file positions.
|
|
57
|
+
* Comments are NOT excluded from line counting - all newlines are counted.
|
|
58
|
+
*
|
|
59
|
+
* @param sql - The SQL content to split
|
|
60
|
+
* @returns Array of parsed statements with accurate line numbers
|
|
61
|
+
*/
|
|
62
|
+
export declare function splitSqlStatementsWithLineNumbers(sql: string): ParsedStatement[];
|
|
63
|
+
/**
|
|
64
|
+
* Split SQL content into individual statements with line number tracking
|
|
65
|
+
* and collect any warnings about parsing issues.
|
|
66
|
+
*
|
|
67
|
+
* @param sql - The SQL content to split
|
|
68
|
+
* @returns Object with statements and any parsing warnings
|
|
69
|
+
*/
|
|
70
|
+
export declare function splitSqlStatementsWithWarnings(sql: string): ParseResult;
|
|
71
|
+
/**
|
|
72
|
+
* Table reference extracted from SQL.
|
|
73
|
+
*/
|
|
74
|
+
export interface TableReference {
|
|
75
|
+
schema: string;
|
|
76
|
+
table: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Extracts table names from a SQL query.
|
|
80
|
+
* Handles common patterns:
|
|
81
|
+
* - FROM, JOIN, INTO, UPDATE, DELETE FROM
|
|
82
|
+
* - MERGE INTO/USING
|
|
83
|
+
* - COPY (table) FROM/TO
|
|
84
|
+
* - LATERAL subquery references
|
|
85
|
+
* - USING clause in JOINs
|
|
86
|
+
*
|
|
87
|
+
* @param sql - The SQL query to analyze
|
|
88
|
+
* @returns Array of unique table references
|
|
89
|
+
*/
|
|
90
|
+
export declare function extractTablesFromSql(sql: string): TableReference[];
|
|
91
|
+
/**
|
|
92
|
+
* Filter parsed statements to only executable ones (non-empty, non-comment).
|
|
93
|
+
*
|
|
94
|
+
* @param statements - Array of parsed statements
|
|
95
|
+
* @returns Filtered array of executable statements
|
|
96
|
+
*/
|
|
97
|
+
export declare function filterExecutableStatements(statements: ParsedStatement[]): ParsedStatement[];
|
|
98
|
+
/**
|
|
99
|
+
* Normalize SQL by removing comments and extra whitespace.
|
|
100
|
+
* Properly handles nested block comments.
|
|
101
|
+
*
|
|
102
|
+
* @param sql - The SQL to normalize
|
|
103
|
+
* @returns Normalized SQL string
|
|
104
|
+
*/
|
|
105
|
+
export declare function normalizeSql(sql: string): string;
|
|
106
|
+
/**
|
|
107
|
+
* Checks if SQL content is empty after preprocessing (removing comments/whitespace).
|
|
108
|
+
*
|
|
109
|
+
* @param sql - The SQL to check
|
|
110
|
+
* @returns true if SQL is effectively empty, false otherwise
|
|
111
|
+
*/
|
|
112
|
+
export declare function isEmptyAfterPreprocessing(sql: string): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Detects potential schema change statements in SQL.
|
|
115
|
+
* Useful for warning about schema changes during multi-statement execution.
|
|
116
|
+
*
|
|
117
|
+
* @param sql - The SQL statement to check
|
|
118
|
+
* @returns Object with detected schema changes
|
|
119
|
+
*/
|
|
120
|
+
export declare function detectSchemaChanges(sql: string): {
|
|
121
|
+
hasSchemaChange: boolean;
|
|
122
|
+
changeType?: 'search_path' | 'schema_create' | 'schema_drop' | 'schema_alter';
|
|
123
|
+
newSchema?: string;
|
|
124
|
+
};
|
|
125
|
+
//# sourceMappingURL=sql-parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-parser.d.ts","sourceRoot":"","sources":["../../../../src/tools/sql/utils/sql-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAiEpD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,uBAAuB,GAAG,wBAAwB,GAAG,iBAAiB,CAAC;IAC7E,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,eAAe,EAAE,CA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