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"; export const DbContextsArray = ["Master", "Dsql User"]; /** * # Run DSQL users queries */ export default async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArray, tableName, debug, dbContext, forceLocal, }) { /** * Declare variables * * @description Declare "results" variable */ let result; let error; let tableSchema; if (dbSchema) { try { const table = tableName ? tableName : typeof query == "string" ? null : query ? query === null || query === void 0 ? void 0 : query.table : null; if (!table) throw new Error("No table name provided"); tableSchema = dbSchema.tables.filter((tb) => (tb === null || tb === void 0 ? void 0 : tb.tableName) === table)[0]; } catch (_err) { // console.log("ERROR getting tableSchema: ", _err.message); } } /** * Declare variables * * @description Declare "results" variable */ try { if (typeof query === "string") { const formattedQuery = trimSql(query); if (debug && global.DSQL_USE_LOCAL) { console.log("runQuery:formattedQuery", formattedQuery); } /** * Input Validation * * @description Input Validation */ if (readOnly && formattedQuery.match(/^alter|^delete|^create/i)) { throw new Error("Wrong Input!"); } if (readOnly) { result = await varReadOnlyDatabaseDbHandler({ queryString: formattedQuery, queryValuesArray: queryValuesArray === null || queryValuesArray === void 0 ? void 0 : queryValuesArray.map((vl) => String(vl)), tableSchema, forceLocal, }); } else { result = await fullAccessDbHandler({ queryString: formattedQuery, queryValuesArray: queryValuesArray === null || queryValuesArray === void 0 ? void 0 : queryValuesArray.map((vl) => String(vl)), tableSchema, forceLocal, }); } } 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({ dbContext, dbFullName, tableName: table, data: data, update, duplicateColumnName, duplicateColumnValue, tableSchema, debug, }); if (!(result === null || result === void 0 ? void 0 : result.insertId)) { error = "Couldn't insert data"; } break; case "update": result = await updateDbEntry({ dbContext, dbFullName, tableName: table, data: data, identifierColumnName, identifierValue, tableSchema, }); break; case "delete": result = await deleteDbEntry({ dbContext, dbFullName, tableName: table, identifierColumnName, identifierValue, tableSchema, }); break; default: result = null; break; } } } catch (err) { serverError({ component: "functions/backend/runQuery", message: err.message, }); if (debug && global.DSQL_USE_LOCAL) { console.log("runQuery:error", err.message); } result = null; error = err.message; } return { result, error }; }