/var/www/myprog.com.ua/server/node/db
Edit: /var/www/myprog.com.ua/server/node/db/db_context_scenario.js (3214B)
const Scenario = require('../objects/scenario');
const log = require('../helpers/log');
module.exports = (DbContext) => {
DbContext.prototype.get_scenario = async function () {
let _self = this;
let t1 = new Date().microtime(true);
let sql = `SELECT * FROM scenario
WHERE active='1' AND deleted='0' LIMIT 100000`;
log.debug('DbContext', 'get_scenario()', `SQL: ${sql}`);
let scenario = [];
try {
let rows = await _self.query(sql);
for (row of rows) {
scenario.push(new Scenario(row));
}
} catch (e) {
log.error('DbContext', 'get_user_scenario()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_scenario()', `complete ${t2 - t1}`);
return scenario;
}
DbContext.prototype.get_scenario_by_id = async function (id) {
let t1 = new Date().microtime(true);
let _self = this;
let scenario = null;
try {
let sql = `SELECT * FROM scenario WHERE id='${id}' LIMIT 1`;
log.debug('DbContext', 'get_scenario_by_id()', `SQL: ${sql}`);
let result = await _self.query(sql);
if (result[0].id > 0) {
scenario = new Scenario(result[0]);
}
} catch (e) {
log.error('DbContext', 'get_scenario_by_id()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_scenario_by_id()', `complete ${t2 - t1}`);
return scenario;
}
DbContext.prototype.get_user_scenario = async function (user_id) {
let _self = this;
let t1 = new Date().microtime(true);
let sql = `SELECT * FROM scenario
WHERE user_id='${user_id}' AND deleted='0' LIMIT 1000`;
log.debug('DbContext', 'get_user_scenario()', `SQL: ${sql}`);
let scenario = [];
try {
let rows = await _self.query(sql);
for (row of rows) {
scenario.push(new Scenario(row));
}
} catch (e) {
log.error('DbContext', 'get_user_scenario()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_user_scenario()', `complete ${t2 - t1}`);
return scenario;
}
DbContext.prototype.update_scenario = async function (scenario) {
let t1 = new Date().microtime(true);
let _self = this;
//await this.query('START TRANSACTION');
try {
let sql = _self.sql_insert_or_update(scenario, 'scenario');
//log.debug('DbContext', 'update_scenario()',`SQL: ${sql}`);
let result = await _self.query(sql);
if (result.insertId > 0) {
scenario.id = result.insertId;
}
//await _self.query('COMMIT');
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'update_scenario()',`complete ${t2 - t1}`);
return scenario;
} catch (e) {
log.error('DbContext', 'update_scenario()',e);
//await _self.query('ROLLBACK');
return null;
}
}
}