多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
15 KiB

//define class
class xctrl {
6 years ago
constructor(app, mysql) {
this.app = app;
this.mysql = mysql;
}
async create(req, res) {
6 years ago
let query = "INSERT INTO ?? SET ?";
let params = [];
params.push(req.app.locals._tableName);
params.push(req.body);
var results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
async list(req, res) {
6 years ago
let queryParamsObj = {};
queryParamsObj.query = "";
queryParamsObj.params = [];
this.mysql.prepareListQuery(req, res, queryParamsObj, 0);
6 years ago
let results = await this.mysql.exec(
queryParamsObj.query,
queryParamsObj.params
);
res.status(200).json(results);
}
async nestedList(req, res) {
6 years ago
let queryParamsObj = {};
queryParamsObj.query = "";
queryParamsObj.params = [];
6 years ago
this.mysql.prepareListQuery(req, res, queryParamsObj, 1);
6 years ago
let results = await this.mysql.exec(
queryParamsObj.query,
queryParamsObj.params
);
res.status(200).json(results);
}
async findOne(req, res) {
6 years ago
let queryParamsObj = {};
queryParamsObj.query = "";
queryParamsObj.params = [];
this.mysql.prepareListQuery(req, res, queryParamsObj, 2);
6 years ago
let results = await this.mysql.exec(
queryParamsObj.query,
queryParamsObj.params
);
res.status(200).json(results);
}
async read(req, res) {
6 years ago
let query = "select * from ?? where ";
let params = [];
params.push(req.app.locals._tableName);
6 years ago
let clause = this.mysql.getPrimaryKeyWhereClause(
req.app.locals._tableName,
req.params.id.split("___")
);
if (!clause) {
return res.status(400).send({
6 years ago
error:
"Table is made of composite primary keys - all keys were not in input"
});
}
query += clause;
6 years ago
query += " LIMIT 1";
let results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
async exists(req, res) {
6 years ago
let query = "select * from ?? where ";
let params = [];
params.push(req.app.locals._tableName);
6 years ago
let clause = this.mysql.getPrimaryKeyWhereClause(
req.app.locals._tableName,
req.params.id.split("___")
);
if (!clause) {
return res.status(400).send({
6 years ago
error:
"Table is made of composite primary keys - all keys were not in input"
});
}
query += clause;
6 years ago
query += " LIMIT 1";
let results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
async update(req, res) {
6 years ago
let query = "REPLACE INTO ?? SET ?";
let params = [];
params.push(req.app.locals._tableName);
params.push(req.body);
var results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
async patch(req, res) {
6 years ago
let query = "UPDATE ?? SET ";
let keys = Object.keys(req.body);
// SET clause
6 years ago
let updateKeys = "";
for (let i = 0; i < keys.length; ++i) {
6 years ago
updateKeys += keys[i] + " = ? ";
if (i !== keys.length - 1) updateKeys += ", ";
}
// where clause
6 years ago
query += updateKeys + " where ";
let clause = this.mysql.getPrimaryKeyWhereClause(
req.app.locals._tableName,
req.params.id.split("___")
);
if (!clause) {
return res.status(400).send({
6 years ago
error:
"Table is made of composite primary keys - all keys were not in input"
});
}
query += clause;
// params
let params = [];
params.push(req.app.locals._tableName);
params = params.concat(Object.values(req.body));
let results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
async delete(req, res) {
6 years ago
let query = "DELETE FROM ?? WHERE ";
let params = [];
params.push(req.app.locals._tableName);
6 years ago
let clause = this.mysql.getPrimaryKeyWhereClause(
req.app.locals._tableName,
req.params.id.split("___")
);
if (!clause) {
return res.status(400).send({
6 years ago
error:
"Table is made of composite primary keys - all keys were not in input"
});
}
query += clause;
let results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
async bulkInsert(req, res) {
6 years ago
let queryParamsObj = {};
queryParamsObj.query = "";
queryParamsObj.params = [];
let results = [];
//console.log(req.app.locals._tableName, req.body);
6 years ago
this.mysql.prepareBulkInsert(
req.app.locals._tableName,
req.body,
queryParamsObj
);
6 years ago
results = await this.mysql.exec(
queryParamsObj.query,
queryParamsObj.params
);
res.status(200).json(results);
}
async bulkDelete(req, res) {
6 years ago
let query = "delete from ?? where ?? in ";
let params = [];
params.push(req.app.locals._tableName);
params.push(this.mysql.getPrimaryKeyName(req.app.locals._tableName));
6 years ago
query += "(";
if (req.query && req.query._ids) {
6 years ago
let ids = req.query._ids.split(",");
for (var i = 0; i < ids.length; ++i) {
if (i) {
6 years ago
query += ",";
}
6 years ago
query += "?";
params.push(ids[i]);
}
}
6 years ago
query += ")";
//console.log(query, params);
var results = await this.mysql.exec(query, params);
res.status(200).json(results);
}
async bulkRead(req, res) {
6 years ago
let queryParamsObj = {};
queryParamsObj.query = "";
queryParamsObj.params = [];
this.mysql.prepareListQuery(req, res, queryParamsObj, 3);
//console.log(queryParamsObj.query, queryParamsObj.params);
6 years ago
let results = await this.mysql.exec(
queryParamsObj.query,
queryParamsObj.params
);
res.status(200).json(results);
}
async count(req, res) {
6 years ago
let queryParams = {};
6 years ago
queryParams.query = "select count(1) as no_of_rows from ?? ";
queryParams.params = [];
queryParams.params.push(req.app.locals._tableName);
6 years ago
this.mysql.getWhereClause(
req.query._where,
req.app.locals._tableName,
queryParams,
" where "
);
let results = await this.mysql.exec(queryParams.query, queryParams.params);
res.status(200).json(results);
}
async distinct(req, res) {
6 years ago
let queryParamsObj = {};
queryParamsObj.query = "";
queryParamsObj.params = [];
this.mysql.prepareListQuery(req, res, queryParamsObj, 4);
6 years ago
let results = await this.mysql.exec(
queryParamsObj.query,
queryParamsObj.params
);
res.status(200).json(results);
}
async groupBy(req, res) {
if (req.query && req.query._fields) {
6 years ago
let queryParamsObj = {};
queryParamsObj.query = "select ";
queryParamsObj.params = [];
/**************** add columns and group by columns ****************/
6 years ago
this.mysql.getColumnsForSelectStmt(
req.app.locals._tableName,
req.query,
queryParamsObj
);
6 years ago
queryParamsObj.query += ",count(*) as _count from ?? group by ";
let tableName = req.app.locals._tableName;
queryParamsObj.params.push(tableName);
6 years ago
this.mysql.getColumnsForSelectStmt(
req.app.locals._tableName,
req.query,
queryParamsObj
);
if (!req.query._sort) {
6 years ago
req.query._sort = {};
req.query._sort = "-_count";
}
/**************** add having clause ****************/
6 years ago
this.mysql.getHavingClause(
req.query._having,
req.app.locals._tableName,
queryParamsObj,
" having "
);
/**************** add orderby clause ****************/
this.mysql.getOrderByClause(req.query, tableName, queryParamsObj);
//console.log(queryParamsObj.query, queryParamsObj.params);
6 years ago
var results = await this.mysql.exec(
queryParamsObj.query,
queryParamsObj.params
);
res.status(200).json(results);
} else {
6 years ago
res
.status(400)
.json({
message:
"Missing _fields query params eg: /api/tableName/groupby?_fields=column1"
});
}
}
async ugroupby(req, res) {
if (req.query && req.query._fields) {
6 years ago
let queryParamsObj = {};
queryParamsObj.query = "";
queryParamsObj.params = [];
6 years ago
let uGrpByResults = {};
/**************** add fields with count(*) *****************/
6 years ago
let fields = req.query._fields.split(",");
for (var i = 0; i < fields.length; ++i) {
6 years ago
uGrpByResults[fields[i]] = [];
if (i) {
6 years ago
queryParamsObj.query += " UNION ";
}
6 years ago
queryParamsObj.query +=
" SELECT IFNULL(CONCAT(?,?,??),?) as ugroupby, count(*) as _count from ?? GROUP BY ?? ";
queryParamsObj.params.push(fields[i]);
queryParamsObj.params.push("~");
queryParamsObj.params.push(fields[i]);
queryParamsObj.params.push(fields[i] + "~");
queryParamsObj.params.push(req.app.locals._tableName);
queryParamsObj.params.push(fields[i]);
}
//console.log(queryParamsObj.query, queryParamsObj.params);
6 years ago
var results = await this.mysql.exec(
queryParamsObj.query,
queryParamsObj.params
);
for (var i = 0; i < results.length; ++i) {
6 years ago
let grpByColName = results[i]["ugroupby"].split("~")[0];
let grpByColValue = results[i]["ugroupby"].split("~")[1];
6 years ago
let obj = {};
obj[grpByColValue] = results[i]["_count"];
6 years ago
uGrpByResults[grpByColName].push(obj);
}
res.status(200).json(uGrpByResults);
} else {
6 years ago
res
.status(400)
.json({
message:
"Missing _fields query params eg: /api/tableName/ugroupby?_fields=column1,column2"
});
}
}
async aggregate(req, res) {
if (req.query && req.query._fields) {
let tableName = req.app.locals._tableName;
6 years ago
let query = "select ";
let params = [];
let fields = req.query._fields.split(",");
for (var i = 0; i < fields.length; ++i) {
if (i) {
6 years ago
query = query + ",";
}
6 years ago
query =
query +
" min(??) as ?,max(??) as ?,avg(??) as ?,sum(??) as ?,stddev(??) as ?,variance(??) as ? ";
params.push(fields[i]);
6 years ago
params.push("min_of_" + fields[i]);
params.push(fields[i]);
6 years ago
params.push("max_of_" + fields[i]);
params.push(fields[i]);
6 years ago
params.push("avg_of_" + fields[i]);
params.push(fields[i]);
6 years ago
params.push("sum_of_" + fields[i]);
params.push(fields[i]);
6 years ago
params.push("stddev_of_" + fields[i]);
params.push(fields[i]);
6 years ago
params.push("variance_of_" + fields[i]);
}
6 years ago
query = query + " from ??";
params.push(tableName);
var results = await this.mysql.exec(query, params);
res.status(200).json(results);
} else {
6 years ago
res
.status(400)
.json({
message:
"Missing _fields in query params eg: /api/tableName/aggregate?_fields=numericColumn1"
});
}
}
async chart(req, res) {
6 years ago
let query = "";
let params = [];
let obj = {};
if (req.query) {
6 years ago
let isRange = false;
if (req.query.range) {
6 years ago
isRange = true;
}
if (req.query && req.query.min && req.query.max && req.query.step) {
//console.log(req.params.min, req.params.max, req.params.step);
6 years ago
obj = this.mysql.getChartQueryAndParamsFromMinMaxStep(
req.app.locals._tableName,
req.query._fields,
parseInt(req.query.min),
parseInt(req.query.max),
parseInt(req.query.step),
6 years ago
isRange
);
} else if (
req.query &&
req.query.steparray &&
req.query.steparray.length > 1
) {
obj = this.mysql.getChartQueryAndParamsFromStepArray(
req.app.locals._tableName,
req.query._fields,
6 years ago
req.query.steparray.split(",").map(Number),
isRange
);
} else if (
req.query &&
req.query.steppair &&
req.query.steppair.length > 1
) {
obj = this.mysql.getChartQueryAndParamsFromStepPair(
req.app.locals._tableName,
req.query._fields,
6 years ago
req.query.steppair.split(",").map(Number),
false
);
} else {
6 years ago
query =
"select min(??) as min,max(??) as max,stddev(??) as stddev,avg(??) as avg from ??";
params = [];
params.push(req.query._fields);
params.push(req.query._fields);
params.push(req.query._fields);
params.push(req.query._fields);
params.push(req.app.locals._tableName);
let _this = this;
6 years ago
let results = await _this.mysql.exec(query, params);
//console.log(results, results['max'], req.params);
6 years ago
obj = _this.mysql.getChartQueryAndParamsFromMinMaxStddev(
req.app.locals._tableName,
req.query._fields,
6 years ago
results[0]["min"],
results[0]["max"],
results[0]["stddev"],
isRange
6 years ago
);
}
6 years ago
this.mysql.getWhereClause(
req.query._where,
req.app.locals._tableName,
obj,
" where "
);
6 years ago
let results = await this.mysql.exec(obj.query, obj.params);
res.status(200).json(results);
} else {
6 years ago
res
.status(400)
.json({
message:
"Missing _fields in query params eg: /api/tableName/chart?_fields=numericColumn1"
});
}
}
async autoChart(req, res) {
6 years ago
let query = "describe ??";
let params = [req.app.locals._tableName];
let obj = {};
let results = [];
6 years ago
let isRange = false;
if (req.query.range) {
6 years ago
isRange = true;
}
6 years ago
let describeResults = await this.mysql.exec(query, params);
//console.log(describeResults);
for (var i = 0; i < describeResults.length; ++i) {
//console.log('is this numeric column', describeResults[i]['Type']);
6 years ago
if (
describeResults[i]["Key"] !== "PRI" &&
this.mysql.isTypeOfColumnNumber(describeResults[i]["Type"])
) {
query =
"select min(??) as min,max(??) as max,stddev(??) as stddev,avg(??) as avg from ??";
params = [];
6 years ago
params.push(describeResults[i]["Field"]);
params.push(describeResults[i]["Field"]);
params.push(describeResults[i]["Field"]);
params.push(describeResults[i]["Field"]);
params.push(req.app.locals._tableName);
let _this = this;
let minMaxResults = await _this.mysql.exec(query, params);
//console.log(minMaxResults, minMaxResults['max'], req.params);
6 years ago
query = "";
params = [];
6 years ago
obj = _this.mysql.getChartQueryAndParamsFromMinMaxStddev(
req.app.locals._tableName,
describeResults[i]["Field"],
minMaxResults[0]["min"],
minMaxResults[0]["max"],
minMaxResults[0]["stddev"],
isRange
6 years ago
);
let r = await this.mysql.exec(obj.query, obj.params);
6 years ago
let resultObj = {};
resultObj["column"] = describeResults[i]["Field"];
resultObj["chart"] = r;
results.push(resultObj);
}
}
res.status(200).json(results);
}
}
//expose class
6 years ago
module.exports = xctrl;