#5-intelligent-groups #23
2 changed files with 71 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import DynamicQueryBuilder from "../../helpers/dynamicQueryBuilder";
|
import DynamicQueryBuilder from "../../helpers/dynamicQueryBuilder";
|
||||||
|
import { dataSource } from "../../data-source";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description get all table metas
|
* @description get all table metas
|
||||||
|
@ -37,22 +38,71 @@ export async function executeQuery(req: Request, res: Response): Promise<any> {
|
||||||
let count = parseInt((req.query.count as string) ?? "25");
|
let count = parseInt((req.query.count as string) ?? "25");
|
||||||
const query = req.body.query;
|
const query = req.body.query;
|
||||||
|
|
||||||
try {
|
if (typeof query == "string") {
|
||||||
let [rows, total] = await DynamicQueryBuilder.buildQuery(query, offset, count).getManyAndCount();
|
const upperQuery = query.trim().toUpperCase();
|
||||||
|
if (!upperQuery.startsWith("SELECT") || /INSERT|UPDATE|DELETE|ALTER|DROP|CREATE|TRUNCATE/.test(upperQuery)) {
|
||||||
|
return res.json({
|
||||||
|
stats: "error",
|
||||||
|
sql: query,
|
||||||
|
code: "UNALLOWED",
|
||||||
|
msg: "Not allowed to change rows",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
try {
|
||||||
stats: "success",
|
let data: Array<any> = [];
|
||||||
rows: rows,
|
|
||||||
total: total,
|
const result = await dataSource
|
||||||
offset: offset,
|
.transaction(async (manager) => {
|
||||||
count: count,
|
data = await manager.query(query);
|
||||||
});
|
|
||||||
} catch (error) {
|
throw new Error("AllwaysRollbackQuery");
|
||||||
res.json({
|
})
|
||||||
stats: "error",
|
.catch((error) => {
|
||||||
sql: error.sql,
|
if (error.message === "AllwaysRollbackQuery") {
|
||||||
code: error.code,
|
return {
|
||||||
msg: error.sqlMessage,
|
stats: "success",
|
||||||
});
|
rows: data,
|
||||||
|
total: data.length,
|
||||||
|
offset: offset,
|
||||||
|
count: count,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
stats: "error",
|
||||||
|
sql: error.sql,
|
||||||
|
code: error.code,
|
||||||
|
msg: error.sqlMessage,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
res.send(result);
|
||||||
|
} catch (error) {
|
||||||
|
res.json({
|
||||||
|
stats: "error",
|
||||||
|
sql: error.sql,
|
||||||
|
code: error.code,
|
||||||
|
msg: error.sqlMessage,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
let [rows, total] = await DynamicQueryBuilder.buildQuery(query, offset, count).getManyAndCount();
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
stats: "success",
|
||||||
|
rows: rows,
|
||||||
|
total: total,
|
||||||
|
offset: offset,
|
||||||
|
count: count,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.json({
|
||||||
|
stats: "error",
|
||||||
|
sql: error.sql,
|
||||||
|
code: error.code,
|
||||||
|
msg: error.sqlMessage,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ export default abstract class DynamicQueryBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const select of selects) {
|
for (const select of selects) {
|
||||||
if (firstSelect) {
|
if (firstSelect && depth == 0) {
|
||||||
query.select(`${alias}.${select}`);
|
query.select(`${alias}.${select}`);
|
||||||
firstSelect = false;
|
firstSelect = false;
|
||||||
} else {
|
} else {
|
||||||
|
@ -214,6 +214,10 @@ export default abstract class DynamicQueryBuilder {
|
||||||
query += ` LIKE :${parameterKey}`;
|
query += ` LIKE :${parameterKey}`;
|
||||||
parameters[parameterKey] = `%${condition.value}`;
|
parameters[parameterKey] = `%${condition.value}`;
|
||||||
break;
|
break;
|
||||||
|
case "timespanEq":
|
||||||
|
query += ` BETWEEN :${parameterKey}_start AND :${parameterKey}_end`;
|
||||||
|
parameters[`${parameterKey}_start`] = new Date(new Date().getFullYear() - (condition.value as number), 0, 1);
|
||||||
|
parameters[`${parameterKey}_end`] = new Date(new Date().getFullYear() - (condition.value as number), 11, 31);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { query, parameters };
|
return { query, parameters };
|
||||||
|
|
Loading…
Reference in a new issue