Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/sql/recent-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
type TableReference,
} from "@query-doctor/core";
import { parse } from "@libpg-query/parser";
import { Sema } from "async-sema";
import z from "zod";
import type { LiveQueryOptimization } from "../remote/optimization.ts";

Expand All @@ -21,6 +22,7 @@ import type { LiveQueryOptimization } from "../remote/optimization.ts";
export class RecentQuery {
private static HARDCODED_LIMIT = 50;
private static rewriter = new PssRewriter();
private static prettierMutex = new Sema(1);

readonly formattedQuery: string;
readonly username: string;
Expand Down Expand Up @@ -134,15 +136,19 @@ export class RecentQuery {
}

private static async formatQuery(query: string): Promise<string> {
await RecentQuery.prettierMutex.acquire();
try {
return await prettier.format(query, {
parser: "sql",
plugins: [prettierPluginSql],
language: "postgresql",
keywordCase: "upper",
});
} catch {
} catch (error) {
console.error(`[prettier] Failed to format query: ${error}`);
return query;
} finally {
RecentQuery.prettierMutex.release();
}
}

Expand Down
Loading