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"; type Param = { dbFullName: string; query: string | any; readOnly?: boolean; local?: boolean; dbSchema?: import("../../../types").DSQL_DatabaseSchemaType; queryValuesArray?: (string | number)[]; tableName?: string; }; /** * # Run DSQL users queries */ export default async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArray, tableName, local, }: Param): Promise { /** * Declare variables * * @description Declare "results" variable */ let result: any; let error: any; let tableSchema: DSQL_TableSchemaType | undefined; 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") { const formattedQuery = trimSql(query); /** * Input Validation * * @description Input Validation */ if ( readOnly && formattedQuery.match( /^alter|^delete|information_schema|^create/i ) ) { throw new Error("Wrong Input!"); } if (readOnly) { result = await varReadOnlyDatabaseDbHandler({ queryString: formattedQuery, queryValuesArray: queryValuesArray?.map((vl) => String(vl)), database: dbFullName, tableSchema, useLocal: local, }); } else { result = await fullAccessDbHandler({ queryString: formattedQuery, queryValuesArray: queryValuesArray?.map((vl) => String(vl)), database: dbFullName, tableSchema, local, }); } } 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: local ? "Master" : "Dsql User", paradigm: "Full Access", dbFullName: dbFullName, tableName: table, data: data, update, duplicateColumnName, duplicateColumnValue, tableSchema, useLocal: local, }); if (!result?.insertId) { error = new Error("Couldn't insert data"); } break; case "update": result = await updateDbEntry({ dbContext: local ? "Master" : "Dsql User", paradigm: "Full Access", dbFullName: dbFullName, tableName: table, data: data, identifierColumnName, identifierValue, tableSchema, useLocal: local, }); break; case "delete": result = await deleteDbEntry({ dbContext: local ? "Master" : "Dsql User", paradigm: "Full Access", dbFullName: dbFullName, tableName: table, identifierColumnName, identifierValue, tableSchema, useLocal: local, }); break; default: result = null; break; } } } catch (/** @type {any} */ error: any) { serverError({ component: "functions/backend/runQuery", message: error.message, }); result = null; error = error.message; } return { result, error }; }