2023-09-21 14:00:04 +00:00
|
|
|
/** # MODULE TRACE
|
|
|
|
======================================================================
|
2024-11-06 06:26:23 +00:00
|
|
|
* Detected 3 files that call this module. The files are listed below:
|
2023-09-21 14:00:04 +00:00
|
|
|
======================================================================
|
2024-11-06 06:26:23 +00:00
|
|
|
* `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
|
2023-09-21 14:00:04 +00:00
|
|
|
==== MODULE TRACE END ==== */
|
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
2024-11-17 04:36:56 +00:00
|
|
|
const LOCAL_DB_HANDLER = require("../../../utils/backend/global-db/LOCAL_DB_HANDLER");
|
2024-11-06 06:26:23 +00:00
|
|
|
const fullAccessDbHandler = require("../fullAccessDbHandler");
|
|
|
|
const varReadOnlyDatabaseDbHandler = require("../varReadOnlyDatabaseDbHandler");
|
|
|
|
const serverError = require("../serverError");
|
|
|
|
|
2023-09-21 14:00:04 +00:00
|
|
|
const addDbEntry = require("./addDbEntry");
|
|
|
|
const updateDbEntry = require("./updateDbEntry");
|
|
|
|
const deleteDbEntry = require("./deleteDbEntry");
|
2024-11-06 06:26:23 +00:00
|
|
|
const parseDbResults = require("../parseDbResults");
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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"
|
2024-11-06 06:26:23 +00:00
|
|
|
* @param {string | any} params.query - Query string or object
|
2023-09-21 14:00:04 +00:00
|
|
|
* @param {boolean} [params.readOnly] - Is this operation read only?
|
2024-11-06 06:26:23 +00:00
|
|
|
* @param {boolean} [params.local] - Is this operation read only?
|
|
|
|
* @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database schema
|
2023-09-21 14:00:04 +00:00
|
|
|
* @param {string[]} [params.queryValuesArray] - An optional array of query values if "?" is used in the query string
|
|
|
|
* @param {string} [params.tableName] - Table Name
|
|
|
|
*
|
2024-11-06 06:26:23 +00:00
|
|
|
* @return {Promise<any>}
|
2023-09-21 14:00:04 +00:00
|
|
|
*/
|
2024-10-14 06:49:01 +00:00
|
|
|
async function runQuery({
|
|
|
|
dbFullName,
|
|
|
|
query,
|
|
|
|
readOnly,
|
|
|
|
dbSchema,
|
|
|
|
queryValuesArray,
|
|
|
|
tableName,
|
2024-11-06 06:26:23 +00:00
|
|
|
local,
|
2024-10-14 06:49:01 +00:00
|
|
|
}) {
|
2023-09-21 14:00:04 +00:00
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
|
|
|
|
2024-11-06 06:26:23 +00:00
|
|
|
/** @type {any} */
|
|
|
|
let result;
|
|
|
|
/** @type {any} */
|
|
|
|
let error;
|
|
|
|
/** @type {import("../../../types").DSQL_TableSchemaType | undefined} */
|
|
|
|
let tableSchema;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
if (dbSchema) {
|
|
|
|
try {
|
2024-10-14 06:49:01 +00:00
|
|
|
const table = tableName
|
|
|
|
? tableName
|
|
|
|
: typeof query == "string"
|
|
|
|
? null
|
|
|
|
: query
|
|
|
|
? query?.table
|
|
|
|
: null;
|
2023-09-21 14:00:04 +00:00
|
|
|
if (!table) throw new Error("No table name provided");
|
2024-10-14 06:49:01 +00:00
|
|
|
tableSchema = dbSchema.tables.filter(
|
|
|
|
(tb) => tb?.tableName === table
|
|
|
|
)[0];
|
2024-11-06 06:26:23 +00:00
|
|
|
} catch (_err) {
|
|
|
|
// console.log("ERROR getting tableSchema: ", _err.message);
|
|
|
|
}
|
2023-09-21 14:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
if (typeof query === "string") {
|
2024-11-18 09:47:14 +00:00
|
|
|
const formattedQuery = query
|
|
|
|
.replace(/\n|\r|\n\r|\r\n/gm, " ")
|
|
|
|
.replace(/ {2,}/g, " ")
|
|
|
|
.trim();
|
|
|
|
|
2024-11-18 09:59:12 +00:00
|
|
|
/**
|
|
|
|
* Input Validation
|
|
|
|
*
|
|
|
|
* @description Input Validation
|
|
|
|
*/
|
|
|
|
if (
|
|
|
|
readOnly &&
|
|
|
|
(formattedQuery.match(
|
|
|
|
/^alter|^delete|information_schema|databases|^create/i
|
|
|
|
) ||
|
|
|
|
!formattedQuery.match(/^select|^\( ?select/i))
|
|
|
|
) {
|
|
|
|
throw new Error("Wrong Input!");
|
|
|
|
}
|
|
|
|
|
2024-11-06 06:26:23 +00:00
|
|
|
if (local) {
|
2024-11-17 04:36:56 +00:00
|
|
|
const rawResults = await LOCAL_DB_HANDLER(
|
2024-11-18 09:47:14 +00:00
|
|
|
formattedQuery,
|
2024-11-17 04:36:56 +00:00
|
|
|
queryValuesArray
|
|
|
|
);
|
2024-11-06 06:26:23 +00:00
|
|
|
result = tableSchema
|
|
|
|
? parseDbResults({
|
|
|
|
unparsedResults: rawResults,
|
|
|
|
tableSchema,
|
|
|
|
})
|
|
|
|
: rawResults;
|
|
|
|
} else if (readOnly) {
|
|
|
|
result = await varReadOnlyDatabaseDbHandler({
|
2024-11-18 09:47:14 +00:00
|
|
|
queryString: formattedQuery,
|
2024-11-06 06:26:23 +00:00
|
|
|
queryValuesArray,
|
|
|
|
database: dbFullName,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
result = await fullAccessDbHandler({
|
2024-11-18 09:47:14 +00:00
|
|
|
queryString: formattedQuery,
|
2024-11-06 06:26:23 +00:00
|
|
|
queryValuesArray,
|
|
|
|
database: dbFullName,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
}
|
2023-09-21 14:00:04 +00:00
|
|
|
} else if (typeof query === "object") {
|
|
|
|
/**
|
|
|
|
* Declare variables
|
|
|
|
*
|
|
|
|
* @description Declare "results" variable
|
|
|
|
*/
|
2024-10-14 06:49:01 +00:00
|
|
|
const {
|
|
|
|
data,
|
|
|
|
action,
|
|
|
|
table,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
update,
|
|
|
|
duplicateColumnName,
|
|
|
|
duplicateColumnValue,
|
|
|
|
} = query;
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
switch (action.toLowerCase()) {
|
|
|
|
case "insert":
|
|
|
|
result = await addDbEntry({
|
2024-11-06 06:26:23 +00:00
|
|
|
dbContext: local ? "Master" : "Dsql User",
|
|
|
|
paradigm: "Full Access",
|
2023-09-21 14:00:04 +00:00
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table,
|
|
|
|
data: data,
|
|
|
|
update,
|
|
|
|
duplicateColumnName,
|
|
|
|
duplicateColumnValue,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!result?.insertId) {
|
|
|
|
error = new Error("Couldn't insert data");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "update":
|
|
|
|
result = await updateDbEntry({
|
2024-11-06 06:26:23 +00:00
|
|
|
dbContext: local ? "Master" : "Dsql User",
|
2023-09-21 14:00:04 +00:00
|
|
|
paradigm: "Full Access",
|
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table,
|
|
|
|
data: data,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "delete":
|
|
|
|
result = await deleteDbEntry({
|
2024-11-06 06:26:23 +00:00
|
|
|
dbContext: local ? "Master" : "Dsql User",
|
2023-09-21 14:00:04 +00:00
|
|
|
paradigm: "Full Access",
|
|
|
|
dbFullName: dbFullName,
|
|
|
|
tableName: table,
|
|
|
|
identifierColumnName,
|
|
|
|
identifierValue,
|
|
|
|
tableSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2024-11-06 06:26:23 +00:00
|
|
|
result = null;
|
2023-09-21 14:00:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
2024-11-06 06:26:23 +00:00
|
|
|
} catch (/** @type {any} */ error) {
|
|
|
|
serverError({
|
|
|
|
component: "functions/backend/runQuery",
|
|
|
|
message: error.message,
|
|
|
|
});
|
|
|
|
result = null;
|
2023-09-21 14:00:04 +00:00
|
|
|
error = error.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
return { result, error };
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = runQuery;
|