2024-12-06 13:24:26 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2025-01-13 08:00:21 +00:00
|
|
|
import _ from "lodash";
|
|
|
|
import serverError from "../../backend/serverError";
|
|
|
|
import runQuery from "../../backend/db/runQuery";
|
|
|
|
import { DSQL_TableSchemaType, GetReturn } from "../../../types";
|
|
|
|
|
|
|
|
type Param = {
|
|
|
|
query: string;
|
|
|
|
queryValues?: (string | number)[];
|
|
|
|
dbFullName: string;
|
|
|
|
tableName?: string;
|
|
|
|
dbSchema?: import("../../../types").DSQL_DatabaseSchemaType;
|
|
|
|
useLocal?: boolean;
|
|
|
|
};
|
2024-12-06 13:24:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* # Get Function FOr API
|
|
|
|
*/
|
2025-01-13 08:00:21 +00:00
|
|
|
export default async function apiGet({
|
2024-12-06 13:24:26 +00:00
|
|
|
query,
|
|
|
|
dbFullName,
|
|
|
|
queryValues,
|
|
|
|
tableName,
|
|
|
|
dbSchema,
|
|
|
|
useLocal,
|
2025-01-13 08:00:21 +00:00
|
|
|
}: Param): Promise<import("../../../types").GetReturn> {
|
2024-12-06 13:24:26 +00:00
|
|
|
if (
|
|
|
|
typeof query == "string" &&
|
2024-12-08 08:57:48 +00:00
|
|
|
query.match(/^alter|^delete|information_schema|databases|^create/i)
|
2024-12-06 13:24:26 +00:00
|
|
|
) {
|
2024-12-08 08:57:48 +00:00
|
|
|
return { success: false, msg: "Wrong Input." };
|
2024-12-06 13:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let results;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let { result, error } = await runQuery({
|
|
|
|
dbFullName: dbFullName,
|
|
|
|
query: query,
|
|
|
|
queryValuesArray: queryValues,
|
|
|
|
readOnly: true,
|
|
|
|
dbSchema,
|
|
|
|
tableName,
|
|
|
|
local: useLocal,
|
|
|
|
});
|
|
|
|
|
2025-01-13 08:00:21 +00:00
|
|
|
let tableSchema: DSQL_TableSchemaType | undefined;
|
2024-12-06 13:24:26 +00:00
|
|
|
|
|
|
|
if (dbSchema) {
|
|
|
|
const targetTable = dbSchema.tables.find(
|
|
|
|
(table) => table.tableName === tableName
|
|
|
|
);
|
|
|
|
|
|
|
|
if (targetTable) {
|
|
|
|
const clonedTargetTable = _.cloneDeep(targetTable);
|
|
|
|
delete clonedTargetTable.childTable;
|
|
|
|
delete clonedTargetTable.childTableDbFullName;
|
|
|
|
delete clonedTargetTable.childTableName;
|
|
|
|
delete clonedTargetTable.childrenTables;
|
|
|
|
delete clonedTargetTable.updateData;
|
|
|
|
delete clonedTargetTable.tableNameOld;
|
|
|
|
delete clonedTargetTable.indexes;
|
|
|
|
tableSchema = clonedTargetTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) throw error;
|
|
|
|
if (result.error) throw new Error(result.error);
|
|
|
|
|
|
|
|
results = result;
|
|
|
|
|
2025-01-13 08:00:21 +00:00
|
|
|
const resObject: GetReturn = {
|
2024-12-06 13:24:26 +00:00
|
|
|
success: true,
|
|
|
|
payload: results,
|
|
|
|
schema: tableName && tableSchema ? tableSchema : undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
return resObject;
|
2025-01-13 08:00:21 +00:00
|
|
|
} catch (error: any) {
|
2024-12-06 13:24:26 +00:00
|
|
|
serverError({
|
|
|
|
component: "/api/query/get/lines-85-94",
|
|
|
|
message: error.message,
|
|
|
|
});
|
|
|
|
|
2025-01-13 08:00:21 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
payload: null,
|
|
|
|
error: error.message,
|
|
|
|
};
|
2024-12-06 13:24:26 +00:00
|
|
|
}
|
2025-01-13 08:00:21 +00:00
|
|
|
}
|