mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(database): search and pagination on result query (#1432)
Adds a search bar to query result grids (match case / words / regex, previous/next occurrence, Filter rows) and a pager for console results (first/previous/next/last, page size). Later pages re-run the read-only query with a skip, which every driver reads past without decoding, so only one page crosses IPC; Last counts rows with the statement wrapped so a trailing line comment can't swallow the closing parenthesis. Closes #1431 Co-authored-by: kidp2h <40386174+kidp2h@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isReadOnlyQuery } from "./readOnly";
|
||||
|
||||
describe("isReadOnlyQuery", () => {
|
||||
it("accepts queries that only read", () => {
|
||||
for (const text of [
|
||||
"SELECT * FROM t",
|
||||
"with x as (select 1) select * from x",
|
||||
"VALUES (1), (2)",
|
||||
"show tables",
|
||||
"(select 1) union (select 2)",
|
||||
])
|
||||
expect(isReadOnlyQuery(text, "postgres"), text).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects statements that change data", () => {
|
||||
for (const text of [
|
||||
"DELETE FROM t RETURNING *",
|
||||
"UPDATE t SET a = 1 RETURNING a",
|
||||
"INSERT INTO t VALUES (1) RETURNING id",
|
||||
"with d as (delete from t returning *) select * from d",
|
||||
"select * into t2 from t",
|
||||
"select * from t for update",
|
||||
"select nextval('s')",
|
||||
])
|
||||
expect(isReadOnlyQuery(text, "postgres"), text).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { DbDriver } from "@/platform/ipc";
|
||||
import { lex } from "./lexer";
|
||||
|
||||
/** Statements that only read. */
|
||||
const READ_VERBS = new Set(["SELECT", "WITH", "VALUES", "TABLE", "SHOW", "DESCRIBE", "DESC", "EXPLAIN", "PRAGMA"]);
|
||||
/** Words that make a reading statement write (data-modifying CTEs, SELECT INTO, row locks, sequences). */
|
||||
const WRITES = new Set(["INSERT", "UPDATE", "DELETE", "MERGE", "INTO", "NEXTVAL", "SETVAL", "CALL", "EXEC", "EXECUTE"]);
|
||||
|
||||
/** Whether running `text` again only reads (paging a result re-runs its query). */
|
||||
export function isReadOnlyQuery(text: string, driver: DbDriver): boolean {
|
||||
const tokens = lex(text, driver).tokens;
|
||||
const first = tokens.find((t) => t.text !== "(");
|
||||
if (!first || first.kind !== "word" || !READ_VERBS.has(first.upper)) return false;
|
||||
return !tokens.some((t) => t.kind === "word" && WRITES.has(t.upper));
|
||||
}
|
||||
Reference in New Issue
Block a user