mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-07-28 17:53:21 +08:00
chore: update util functions
This commit is contained in:
@ -2,6 +2,7 @@ import { toast } from "react-hot-toast";
|
||||
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
|
||||
import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
|
||||
import { useConnectionStore, useQueryStore } from "@/store";
|
||||
import { checkStatementIsSelect } from "@/utils";
|
||||
import Icon from "./Icon";
|
||||
|
||||
interface Props {
|
||||
@ -9,10 +10,6 @@ interface Props {
|
||||
value: string;
|
||||
}
|
||||
|
||||
const checkStatementIsSelect = (statement: string) => {
|
||||
return statement.toUpperCase().trim().startsWith("SELECT");
|
||||
};
|
||||
|
||||
export const CodeBlock = (props: Props) => {
|
||||
const { language, value } = props;
|
||||
const connectionStore = useConnectionStore();
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { openAIApiKey } from "@/utils/openai";
|
||||
import { openAIApiKey } from "@/utils";
|
||||
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { newConnector } from "@/lib/connectors";
|
||||
import { Connection } from "@/types";
|
||||
import { checkStatementIsSelect } from "@/utils";
|
||||
|
||||
// POST /api/connection/execute
|
||||
// req body: { connection: Connection, db: string, statement: string }
|
||||
@ -12,6 +13,11 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const connection = req.body.connection as Connection;
|
||||
const db = req.body.db as string;
|
||||
const statement = req.body.statement as string;
|
||||
// We only support SELECT statements for now.
|
||||
if (!checkStatementIsSelect(statement)) {
|
||||
return res.status(400).json([]);
|
||||
}
|
||||
|
||||
try {
|
||||
const connector = newConnector(connection);
|
||||
const result = await connector.execute(db, statement);
|
||||
|
5
utils/id.ts
Normal file
5
utils/id.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export const generateUUID = () => {
|
||||
return uuidv4();
|
||||
};
|
@ -1,5 +1,3 @@
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export const generateUUID = () => {
|
||||
return uuidv4();
|
||||
};
|
||||
export * from "./id";
|
||||
export * from "./openai";
|
||||
export * from "./sql";
|
||||
|
3
utils/sql.ts
Normal file
3
utils/sql.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export const checkStatementIsSelect = (statement: string) => {
|
||||
return statement.toUpperCase().trim().startsWith("SELECT");
|
||||
};
|
Reference in New Issue
Block a user