/var/www/myprog.com.ua/server/node/db
Edit: /var/www/myprog.com.ua/server/node/db/db_context_commands.js (3609B)
const Command = require('../objects/command');
const CommandHistory = require('../objects/command_history');
const log = require('../helpers/log');
module.exports = (DbContext) => {
DbContext.prototype.get_command = async function (host_id, command, command_v) {
//let t1 = new Date().microtime(true);
let _self = this;
let cmd = null;
try {
let sql = `SELECT * FROM \`commands\` WHERE host_id='${host_id}' AND c='${command}' AND v='${command_v}' LIMIT 1`;
//log.debug('DbContext', 'get_value()', `SQL: ${sql}`);
let result = await _self.query(sql);
if(result[0]!=undefined){
if (result[0].id > 0) {
cmd = new Command(result[0]);
}
}
} catch (e) {
log.error('DbContext', 'get_command()', e);
}
//let t2 = new Date().microtime(true);
//log.debug('DbContext', 'get_command()', `complete ${t2 - t1}`);
return cmd;
}
DbContext.prototype.update_command = async function (command) {
//let t1 = new Date().microtime(true);
let _self = this;
//await this.query('START TRANSACTION');
try {
let sql = _self.sql_insert_or_update(command, 'commands');
//log.debug('DbContext', 'update_value()',`SQL: ${sql}`);
let result = await _self.query(sql);
if (result.insertId > 0) {
command.id = result.insertId;
}
//await _self.query('COMMIT');
//let t2 = new Date().microtime(true);
//log.debug('DbContext', 'update_command()',`complete ${t2 - t1}`);
return command;
} catch (e) {
log.error('DbContext', 'update_command()',e);
//await _self.query('ROLLBACK');
return null;
}
}
DbContext.prototype.update_command_history = async function (command) {
// let t1 = new Date().microtime(true);
let _self = this;
//await this.query('START TRANSACTION');
try {
let sql = _self.sql_insert_or_update(command, 'commands_history');
//log.debug('DbContext', 'update_command_history()',`SQL: ${sql}`);
let result = await _self.query(sql);
if (result.insertId > 0) {
command.id = result.insertId;
}
//await _self.query('COMMIT');
// let t2 = new Date().microtime(true);
// log.debug('DbContext', 'update_command_history()',`complete ${t2 - t1}`);
return command;
} catch (e) {
log.error('DbContext', 'update_command_history()',e);
//await _self.query('ROLLBACK');
return null;
}
}
DbContext.prototype.get_user_commands = async function (user_id) {
let _self = this;
let t1 = new Date().microtime(true);
let sql = `SELECT c.* FROM \`commands\` c
INNER JOIN users_hosts uh ON uh.host_id=c.host_id
WHERE uh.user_id='${user_id}' LIMIT 100`;
//log.debug('DbContext', 'get_user_commands()', `SQL: ${sql}`);
let commands = [];
try {
let rows = await _self.query(sql);
for (row of rows) {
commands.push(new Command(row));
}
} catch (e) {
log.error('DbContext', 'get_user_commands()', e);
}
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'get_user_commands()', `complete ${t2 - t1}`);
return commands;
}
}