2025-01-13 08:00:21 +00:00
|
|
|
import fullAccessDbHandler from "../fullAccessDbHandler";
|
|
|
|
import varReadOnlyDatabaseDbHandler from "../varReadOnlyDatabaseDbHandler";
|
|
|
|
import serverError from "../serverError";
|
|
|
|
import addDbEntry from "./addDbEntry";
|
|
|
|
import updateDbEntry from "./updateDbEntry";
|
|
|
|
import deleteDbEntry from "./deleteDbEntry";
|
|
|
|
import trimSql from "../../../utils/trim-sql";
|
|
|
|
import { DSQL_TableSchemaType } from "../../../types";
|
|
|
|
|
2025-01-28 18:43:16 +00:00
|
|
|
export const DbContextsArray = ["Master", "Dsql User"] as const;
|
|
|
|
|
2025-01-13 08:00:21 +00:00
|
|
|
type Param = {
|
2025-01-28 18:43:16 +00:00
|
|
|
dbContext?: (typeof DbContextsArray)[number];
|
2025-01-13 08:00:21 +00:00
|
|
|
dbFullName: string;
|
|
|
|
query: string | any;
|
|
|
|
readOnly?: boolean;
|
2025-01-28 18:43:16 +00:00
|
|
|
debug?: boolean;
|
2025-01-13 08:00:21 +00:00
|
|
|
dbSchema?: import("../../../types").DSQL_DatabaseSchemaType;
|
|
|
|
queryValuesArray?: (string | number)[];
|
|
|
|
tableName?: string;
|
2025-02-12 16:56:44 +00:00
|
|
|
forceLocal?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Return = {
|
|
|
|
result: any;
|
|
|
|
error?: string;
|
2025-01-13 08:00:21 +00:00
|
|
|
};
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
/**
|
2025-01-13 08:00:21 +00:00
|
|
|
* # Run DSQL users queries
|
2024-11-05 11:12:42 +00:00
|
|
|
*/
|
2025-01-13 08:00:21 +00:00
|
|
|
export default async function runQuery({
|
2024-11-05 11:12:42 +00:00
|
|
|
dbFullName,
|
|
|
|
query,
|
|
|
|
readOnly,
|
|
|
|
dbSchema,
|
|
|
|
queryValuesArray,
|
|
|
|
tableName,
|
2025-01-28 18:43:16 +00:00
|
|
|
debug,
|
|
|
|
dbContext,
|
2025-02-12 16:56:44 +00:00
|
|
|
forceLocal,
|
|
|
|
}: Param): Promise<Return> {
|
2024-11-05 11:12:42 +00:00
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
|
|
|
|
2025-01-13 08:00:21 +00:00
|
|
|
let result: any;
|
2025-02-12 16:56:44 +00:00
|
|
|
let error: string | undefined;
|
2025-01-13 08:00:21 +00:00
|
|
|
let tableSchema: DSQL_TableSchemaType | undefined;
|
2024-11-05 11:12:42 +00:00
|
|
|
|
|
|
|
if (dbSchema) {
|
|
|
|
try {
|
|
|
|
const table = tableName
|
|
|
|
? tableName
|
|
|
|
: typeof query == "string"
|
|
|
|
? null
|
|
|
|
: query
|
|
|
|
? query?.table
|
|
|
|
: null;
|
|
|
|
if (!table) throw new Error("No table name provided");
|
|
|
|
tableSchema = dbSchema.tables.filter(
|
|
|
|
(tb) => tb?.tableName === table
|
|
|
|
)[0];
|
|
|
|
} catch (_err) {
|
|
|
|
// console.log("ERROR getting tableSchema: ", _err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
if (typeof query === "string") {
|
2024-11-26 09:31:39 +00:00
|
|
|
const formattedQuery = trimSql(query);
|
|
|
|
|
2025-01-28 18:43:16 +00:00
|
|
|
if (debug && global.DSQL_USE_LOCAL) {
|
|
|
|
console.log("runQuery:formattedQuery", formattedQuery);
|
|
|
|
}
|
|
|
|
|
2024-11-26 09:31:39 +00:00
|
|
|
/**
|
|
|
|
* Input Validation
|
|
|
|
*
|
|
|
|
* @description Input Validation
|
|
|
|
*/
|
2025-02-12 16:56:44 +00:00
|
|
|
if (readOnly && formattedQuery.match(/^alter|^delete|^create/i)) {
|
2024-11-26 09:31:39 +00:00
|
|
|
throw new Error("Wrong Input!");
|
|
|
|
}
|
|
|
|
|
2024-12-09 12:27:08 +00:00
|
|
|
if (readOnly) {
|
2024-11-05 11:12:42 +00:00
|
|
|
result = await varReadOnlyDatabaseDbHandler({
|
2024-11-26 09:31:39 +00:00
|
|
|
queryString: formattedQuery,
|
2024-12-06 13:24:26 +00:00
|
|
|
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
|
2024-11-05 11:12:42 +00:00
|
|
|
tableSchema,
|
2025-02-12 16:56:44 +00:00
|
|
|
forceLocal,
|
2024-11-05 11:12:42 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
result = await fullAccessDbHandler({
|
2024-11-26 09:31:39 +00:00
|
|
|
queryString: formattedQuery,
|
2024-12-06 13:24:26 +00:00
|
|
|
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
|
2024-11-05 11:12:42 +00:00
|
|
|
tableSchema,
|
2025-02-12 16:56:44 +00:00
|
|
|
forceLocal,
|
2024-11-05 11:12:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (typeof query === "object") {
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
|
|
|
const {
|
|
|
|
data,
|
|
|
|
action,
|
|
|
|
table,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
update,
|
|
|
|
duplicateColumnName,
|
|
|
|
duplicateColumnValue,
|
|
|
|
} = query;
|
|
|
|
|
|
|
|
switch (action.toLowerCase()) {
|
|
|
|
case "insert":
|
|
|
|
result = await addDbEntry({
|
2025-01-28 18:43:16 +00:00
|
|
|
dbContext,
|
2024-11-05 11:12:42 +00:00
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table,
|
|
|
|
data: data,
|
|
|
|
update,
|
|
|
|
duplicateColumnName,
|
|
|
|
duplicateColumnValue,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!result?.insertId) {
|
2025-02-12 16:56:44 +00:00
|
|
|
error = "Couldn't insert data";
|
2024-11-05 11:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "update":
|
|
|
|
result = await updateDbEntry({
|
2025-01-28 18:43:16 +00:00
|
|
|
dbContext,
|
2024-11-05 11:12:42 +00:00
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table,
|
|
|
|
data: data,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "delete":
|
|
|
|
result = await deleteDbEntry({
|
2025-01-28 18:43:16 +00:00
|
|
|
dbContext,
|
2024-11-05 11:12:42 +00:00
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
result = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2025-02-12 16:56:44 +00:00
|
|
|
} catch (err: any) {
|
2024-11-05 11:12:42 +00:00
|
|
|
serverError({
|
|
|
|
component: "functions/backend/runQuery",
|
2025-02-12 16:56:44 +00:00
|
|
|
message: err.message,
|
2024-11-05 11:12:42 +00:00
|
|
|
});
|
2025-01-28 18:43:16 +00:00
|
|
|
|
|
|
|
if (debug && global.DSQL_USE_LOCAL) {
|
2025-02-12 16:56:44 +00:00
|
|
|
console.log("runQuery:error", err.message);
|
2025-01-28 18:43:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-05 11:12:42 +00:00
|
|
|
result = null;
|
2025-02-12 16:56:44 +00:00
|
|
|
error = err.message;
|
2024-11-05 11:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { result, error };
|
|
|
|
}
|