Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(sql): handle SQL Server select into

t8y2487ffb578644
diff --git a/apps/desktop/src/lib/queryResultPagination.ts b/apps/desktop/src/lib/queryResultPagination.ts
index fe1a4c3d6c..914e806dd3 100644
--- a/apps/desktop/src/lib/queryResultPagination.ts
+++ b/apps/desktop/src/lib/queryResultPagination.ts
@@ -132,6 +132,121 @@ function singleSelectableStatement(
if (!/^\s*(SELECT|WITH)\b/i.test(statement)) {
return { ok: false, reason: "not_select" };
}
+ if (hasTopLevelSelectInto(statement)) {
+ return { ok: false, reason: "not_select" };
+ }
return { ok: true, sql: statement };
}
+
+function hasTopLevelSelectInto(sql: string): boolean {
+ let sawSelect = false;
+ for (const token of topLevelSqlTokens(sql)) {
+ if (!sawSelect) {
+ sawSelect = token === "SELECT";
+ continue;
+ }
+ if (token === "INTO") return true;
+ }
+ return false;
+}
+
+function topLevelSqlTokens(sql: string): string[] {
+ const tokens: string[] = [];
+ let i = 0;
+ let depth = 0;
+
+ while (i < sql.length) {
+ const ch = sql[i];
+ const next = sql[i + 1];
+
+ if (ch === "-" && next === "-") {
+ i += 2;
+ while (i < sql.length && sql[i] !== "\n") i++;
+ continue;
+ }
+
+ if (ch === "/" && next === "*") {
+ i += 2;
+ while (i < sql.length - 1 && !(sql[i] === "*" && sql[i + 1] === "/")) i++;
+ i = Math.min(i + 2, sql.length);
+ continue;
+ }
+
+ if (ch === "'" || ch === '"' || ch === "`") {
+ i = skipSqlQuoted(sql, i, ch);
+ continue;
+ }
+
+ if (ch === "[") {
+ i = skipSqlBracketIdentifier(sql, i);
+ continue;
+ }
+
+ if (ch === "(") {
+ depth++;
+ i++;
+ continue;
+ }
+
+ if (ch === ")") {
+ depth = Math.max(0, depth - 1);
+ i++;
+ continue;
+ }
+
+ if (depth === 0 && isSqlTokenStart(ch)) {
+ const start = i;
+ i++;
+ while (i < sql.length && isSqlTokenPart(sql[i])) i++;
+ tokens.push(sql.slice(start, i).toUpperCase());
+ continue;
+ }
+
+ i++;
+ }
+
+ return tokens;
+}
+
+function skipSqlQuoted(sql: string, pos: number, quote: string): number {
+ let i = pos + 1;
+ while (i < sql.length) {
+ if (sql[i] === quote) {
+ if (sql[i + 1] === quote) {
+ i += 2;
+ continue;
+ }
+ return i + 1;
+ }
+ if (quote === "'" && sql[i] === "\\") {
+ i += 2;
+ continue;
+ }
+ i++;
+ }
+ return sql.length;
+}
+
+function skipSqlBracketIdentifier(sql: string, pos: number): number {
+ let i = pos + 1;
+ while (i < sql.length) {
+ if (sql[i] === "]") {
+ if (sql[i + 1] === "]") {
+ i += 2;
+ continue;
+ }
+ return i + 1;
+ }
+ i++;
+ }
+ return sql.length;
+}
+
+function isSqlTokenStart(ch: string): boolean {
+ return /[A-Za-z_]/.test(ch);
+}
+
+function isSqlTokenPart(ch: string): boolean {
+ return /[A-Za-z0-9_$#]/.test(ch);
+}
diff --git a/packages/app-tests/queryResultPagination.test.ts b/packages/app-tests/queryResultPagination.test.ts
index d90e6b0226..86c91ec967 100644
--- a/packages/app-tests/queryResultPagination.test.ts
+++ b/packages/app-tests/queryResultPagination.test.ts
@@ -42,6 +42,17 @@ test("does not generate SQL Server OFFSET pagination for later query-result page
});
});
+test("does not paginate SQL Server SELECT INTO statements", () => {
+ const result = buildPaginatedQuerySql(
+ "SELECT * INTO DingTalk_SyncTask FROM YonSuite_SyncTask WHERE 1=2",
+ "sqlserver",
+ 100,
+ 0,
+ );
+
+ assert.deepEqual(result, { ok: false, reason: "not_select" });
+});
+
test("uses fetch first pagination for Oracle first page", () => {
const result = buildPaginatedQuerySql("SELECT id FROM users", "oracle", 100, 0);
@@ -90,6 +101,12 @@ test("uses MySQL style quoting for count query alias", () => {
});
});
+test("does not build a count query for SELECT INTO statements", () => {
+ const result = buildCountQuerySql("SELECT * INTO copy_users FROM users WHERE active = 1", "sqlserver");
+
+ assert.deepEqual(result, { ok: false, reason: "not_select" });
+});
+
test("rejects count query for unsupported database types", () => {
const result = buildCountQuerySql("SELECT * FROM nodes", "neo4j");
@@ -151,3 +168,20 @@ test("uses SQL pagination instead of jdbc cursor for random agent page jumps", (
assert.equal(plan.pageOffset, 1500);
assert.equal(plan.useAgentResultSession, false);
});
+
+test("executes SELECT INTO without pagination or count wrapping", () => {
+ const sql = "SELECT * INTO DingTalk_SyncTask FROM YonSuite_SyncTask WHERE 1=2";
+ const plan = buildQueryPaginationExecutionPlan({
+ sql,
+ queryBaseSql: sql,
+ databaseType: "sqlserver",
+ pagination: { limit: 500, offset: 0 },
+ useAgentCursor: false,
+ });
+
+ assert.equal(plan.sqlToExecute, sql);
+ assert.equal(plan.pageSql, undefined);
+ assert.equal(plan.countSql, undefined);
+ assert.equal(plan.pageLimit, undefined);
+ assert.equal(plan.pageOffset, undefined);
+});