"use strict";
exports.id = 4733;
exports.ids = [4733];
exports.modules = {

/***/ 8499:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

/** # MODULE TRACE 
======================================================================
 * Detected 3 files that call this module. The files are listed below:
======================================================================
 * `import` Statement Found in [get.js] => file:///d:\GitHub\datasquirel\pages\api\query\get.js
 * `import` Statement Found in [post.js] => file:///d:\GitHub\datasquirel\pages\api\query\post.js
 * `import` Statement Found in [add-user.js] => file:///d:\GitHub\datasquirel\pages\api\user\add-user.js
==== MODULE TRACE END ==== */ // @ts-check

const fs = __webpack_require__(7147);
const fullAccessDbHandler = __webpack_require__(8539);
const varReadOnlyDatabaseDbHandler = __webpack_require__(3118);
const serverError = __webpack_require__(3017);
const addDbEntry = __webpack_require__(5338);
const updateDbEntry = __webpack_require__(5886);
const deleteDbEntry = __webpack_require__(6147);
const DB_HANDLER = __webpack_require__(2224);
const parseDbResults = __webpack_require__(8326);
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
 * Run DSQL users queries
 * ==============================================================================
 * @param {object} params - An object containing the function parameters.
 * @param {string} params.dbFullName - Database full name. Eg. "datasquire_user_2_test"
 * @param {string | any} params.query - Query string or object
 * @param {boolean} [params.readOnly] - Is this operation read only?
 * @param {boolean} [params.local] - Is this operation read only?
 * @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database schema
 * @param {string[]} [params.queryValuesArray] - An optional array of query values if "?" is used in the query string
 * @param {string} [params.tableName] - Table Name
 *
 * @return {Promise<any>}
 */ async function runQuery({ dbFullName , query , readOnly , dbSchema , queryValuesArray , tableName , local ,  }) {
    /**
     * Declare variables
     *
     * @description Declare "results" variable
     */ /** @type {any} */ let result;
    /** @type {any} */ let error;
    /** @type {import("../../../types").DSQL_TableSchemaType | undefined} */ let tableSchema;
    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") {
            if (local) {
                const rawResults = await DB_HANDLER(query, queryValuesArray);
                result = tableSchema ? parseDbResults({
                    unparsedResults: rawResults,
                    tableSchema
                }) : rawResults;
            } else if (readOnly) {
                result = await varReadOnlyDatabaseDbHandler({
                    queryString: query,
                    queryValuesArray,
                    database: dbFullName,
                    tableSchema
                });
            } else {
                result = await fullAccessDbHandler({
                    queryString: query,
                    queryValuesArray,
                    database: dbFullName,
                    tableSchema
                });
            }
        } else if (typeof query === "object") {
            /**
             * Declare variables
             *
             * @description Declare "results" variable
             */ const { data , action , table: table1 , 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: table1,
                        data: data,
                        update,
                        duplicateColumnName,
                        duplicateColumnValue,
                        tableSchema
                    });
                    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: table1,
                        data: data,
                        identifierColumnName,
                        identifierValue,
                        tableSchema
                    });
                    break;
                case "delete":
                    result = await deleteDbEntry({
                        dbContext: local ? "Master" : "Dsql User",
                        paradigm: "Full Access",
                        dbFullName: dbFullName,
                        tableName: table1,
                        identifierColumnName,
                        identifierValue,
                        tableSchema
                    });
                    break;
                default:
                    result = null;
                    break;
            }
        }
    ////////////////////////////////////////
    ////////////////////////////////////////
    ////////////////////////////////////////
    } catch (/** @type {any} */ error1) {
        serverError({
            component: "functions/backend/runQuery",
            message: error1.message
        });
        result = null;
        error1 = error1.message;
    }
    ////////////////////////////////////////
    ////////////////////////////////////////
    ////////////////////////////////////////
    return {
        result,
        error
    };
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
}
module.exports = runQuery;


/***/ }),

/***/ 5425:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

// @ts-check

const { scryptSync , createDecipheriv  } = __webpack_require__(6113);
const { Buffer  } = __webpack_require__(4300);
/**
 * @param {string} encryptedString
 * @returns {string | null}
 */ const decrypt = (encryptedString)=>{
    const algorithm = "aes-192-cbc";
    const password = process.env.DSQL_ENCRYPTION_PASSWORD || "";
    const salt = process.env.DSQL_ENCRYPTION_SALT || "";
    let key = scryptSync(password, salt, 24);
    let iv = Buffer.alloc(16, 0);
    // @ts-ignore
    const decipher = createDecipheriv(algorithm, key, iv);
    try {
        let decrypted = decipher.update(encryptedString, "hex", "utf8");
        decrypted += decipher.final("utf8");
        return decrypted;
    } catch (error) {
        return null;
    }
};
module.exports = decrypt;


/***/ }),

/***/ 8539:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

// @ts-check

const DSQL_USER_DB_HANDLER = __webpack_require__(3403);
const parseDbResults = __webpack_require__(8326);
const serverError = __webpack_require__(3017);
/**
 *
 * @param {object} param0
 * @param {string} param0.queryString
 * @param {string} param0.database
 * @param {boolean} [param0.local]
 * @param {import("../../types").DSQL_TableSchemaType | null} [param0.tableSchema]
 * @param {string[]} [param0.queryValuesArray]
 * @returns
 */ module.exports = async function fullAccessDbHandler({ queryString , database , tableSchema , queryValuesArray , local ,  }) {
    /**
     * Declare variables
     *
     * @description Declare "results" variable
     */ let results;
    /**
     * Fetch from db
     *
     * @description Fetch data from db if no cache
     */ try {
        /** ********************* Run Query */ results = await DSQL_USER_DB_HANDLER({
            paradigm: "Full Access",
            database,
            queryString,
            queryValues: queryValuesArray
        });
    ////////////////////////////////////////
    } catch (/** @type {any} */ error) {
        ////////////////////////////////////////
        serverError({
            component: "fullAccessDbHandler",
            message: error.message
        });
        /**
         * Return error
         */ return error.message;
    }
    /**
     * Return results
     *
     * @description Return results add to cache if "req" param is passed
     */ if (results && tableSchema) {
        const unparsedResults = results;
        const parsedResults = await parseDbResults({
            unparsedResults: unparsedResults,
            tableSchema: tableSchema
        });
        return parsedResults;
    } else if (results) {
        return results;
    } else {
        return null;
    }
};


/***/ }),

/***/ 3118:
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

// @ts-check

const fs = __webpack_require__(7147);
const serverError = __webpack_require__(3017);
const parseDbResults = __webpack_require__(8326);
const DSQL_USER_DB_HANDLER = __webpack_require__(3403);
/**
 *
 * @param {object} param0
 * @param {string} param0.queryString
 * @param {string} param0.database
 * @param {string[]} [param0.queryValuesArray]
 * @param {import("../../types").DSQL_TableSchemaType} [param0.tableSchema]
 * @returns
 */ module.exports = async function varReadOnlyDatabaseDbHandler({ queryString , database , queryValuesArray , tableSchema ,  }) {
    /**
     * Declare variables
     *
     * @description Declare "results" variable
     */ let results;
    /**
     * Fetch from db
     *
     * @description Fetch data from db if no cache
     */ try {
        results = await DSQL_USER_DB_HANDLER({
            paradigm: "Read Only",
            database,
            queryString,
            queryValues: queryValuesArray
        });
    ////////////////////////////////////////
    } catch (/** @type {any} */ error) {
        ////////////////////////////////////////
        serverError({
            component: "varReadOnlyDatabaseDbHandler",
            message: error.message,
            noMail: true
        });
        /**
         * Return error
         */ return error.message;
    }
    /**
     * Return results
     *
     * @description Return results add to cache if "req" param is passed
     */ if (results) {
        const unparsedResults = results;
        const parsedResults = await parseDbResults({
            unparsedResults: unparsedResults,
            tableSchema: tableSchema
        });
        return parsedResults;
    } else {
        return null;
    }
};


/***/ })

};
;