chore: update util functions

This commit is contained in:
steven
2023-03-28 17:10:43 +08:00
parent 3e7dd33013
commit 55a2f99e2d
6 changed files with 19 additions and 10 deletions

View File

@ -2,6 +2,7 @@ import { toast } from "react-hot-toast";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism"; import { oneDark } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { useConnectionStore, useQueryStore } from "@/store"; import { useConnectionStore, useQueryStore } from "@/store";
import { checkStatementIsSelect } from "@/utils";
import Icon from "./Icon"; import Icon from "./Icon";
interface Props { interface Props {
@ -9,10 +10,6 @@ interface Props {
value: string; value: string;
} }
const checkStatementIsSelect = (statement: string) => {
return statement.toUpperCase().trim().startsWith("SELECT");
};
export const CodeBlock = (props: Props) => { export const CodeBlock = (props: Props) => {
const { language, value } = props; const { language, value } = props;
const connectionStore = useConnectionStore(); const connectionStore = useConnectionStore();

View File

@ -1,4 +1,4 @@
import { openAIApiKey } from "@/utils/openai"; import { openAIApiKey } from "@/utils";
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser"; import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
import { NextRequest } from "next/server"; import { NextRequest } from "next/server";

View File

@ -1,6 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next"; import { NextApiRequest, NextApiResponse } from "next";
import { newConnector } from "@/lib/connectors"; import { newConnector } from "@/lib/connectors";
import { Connection } from "@/types"; import { Connection } from "@/types";
import { checkStatementIsSelect } from "@/utils";
// POST /api/connection/execute // POST /api/connection/execute
// req body: { connection: Connection, db: string, statement: string } // 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 connection = req.body.connection as Connection;
const db = req.body.db as string; const db = req.body.db as string;
const statement = req.body.statement 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 { try {
const connector = newConnector(connection); const connector = newConnector(connection);
const result = await connector.execute(db, statement); const result = await connector.execute(db, statement);

5
utils/id.ts Normal file
View File

@ -0,0 +1,5 @@
import { v4 as uuidv4 } from "uuid";
export const generateUUID = () => {
return uuidv4();
};

View File

@ -1,5 +1,3 @@
import { v4 as uuidv4 } from "uuid"; export * from "./id";
export * from "./openai";
export const generateUUID = () => { export * from "./sql";
return uuidv4();
};

3
utils/sql.ts Normal file
View File

@ -0,0 +1,3 @@
export const checkStatementIsSelect = (statement: string) => {
return statement.toUpperCase().trim().startsWith("SELECT");
};