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