/var/www/myprog.com.ua/server/node/db
Edit: /var/www/myprog.com.ua/server/node/db/db_context_hosts.js (4342B)
const Host = require('../objects/host');
const Device = require('../objects/device');
const log = require('../helpers/log');
module.exports = (DbContext) => {
DbContext.prototype.get_host_by_key = async function (key) {
let t1 = new Date().microtime(true);
let _self = this;
let host = null;
try {
let sql = `SELECT * FROM \`hosts\` WHERE \`key\`='${key}' LIMIT 1`
log.debug('DbContext', 'get_host_by_key()', `SQL: ${sql}`);
let result = await _self.query(sql);
//console.debug(result);
if(!Array.isArray(result))return null;
if (result[0].id > 0) {
host = new Host(result[0]);
}else{
return null;
}
} catch (e) {
log.error('DbContext', 'get_host_by_key()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_host_by_key()', `complete ${t2 - t1}`);
return host;
}
DbContext.prototype.get_host_by_id = async function (host_id) {
let t1 = new Date().microtime(true);
let _self = this;
let host = null;
try {
let sql = `SELECT * FROM \`hosts\` WHERE \`id\`='${host_id}' LIMIT 1`
log.debug('DbContext', 'get_host_by_id()', `SQL: ${sql}`);
let result = await _self.query(sql);
//console.debug(result);
if(!Array.isArray(result))return null;
if (result[0].id > 0) {
host = new Host(result[0]);
}else{
return null;
}
} catch (e) {
log.error('DbContext', 'get_host_by_id()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_host_by_id()', `complete ${t2 - t1}`);
return host;
}
DbContext.prototype.get_host_by_socket_id = async function (socket_id) {
let t1 = new Date().microtime(true);
let _self = this;
let host = null;
try {
let sql = `SELECT * FROM \`hosts\` WHERE \`socket_id\`='${socket_id}' LIMIT 1`
log.debug('DbContext', 'get_host_by_socket_id()', `SQL: ${sql}`);
let result = await _self.query(sql);
//console.debug(result);
if(!Array.isArray(result))return null;
if (result[0].id > 0) {
host = new Host(result[0]);
}else{
return null;
}
} catch (e) {
log.error('DbContext', 'get_host_by_socket_id()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_host_by_socket_id()', `complete ${t2 - t1}`);
return host;
}
DbContext.prototype.get_user_hosts = async function (user_id) {
let _self = this;
let t1 = new Date().microtime(true);
let sql = `SELECT h.* FROM users_hosts uh
INNER JOIN hosts h ON h.id=uh.host_id
WHERE uh.user_id='${user_id}' LIMIT 100`;
log.debug('DbContext', 'get_user_hosts()', `SQL: ${sql}`);
let hosts = [];
try {
let rows = await _self.query(sql);
for (row of rows) {
hosts.push(new Host(row));
}
} catch (e) {
log.error('DbContext', 'get_user_hosts()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_user_hosts()', `complete ${t2 - t1}`);
return hosts;
}
DbContext.prototype.update_host = async function (host) {
let t1 = new Date().microtime(true);
let _self = this;
//await this.query('START TRANSACTION');
try {
let sql = _self.sql_insert_or_update(host, 'hosts');
log.debug('DbContext', 'update_host()',`SQL: ${sql}`);
let result = await _self.query(sql);
if (result.insertId > 0) {
host.id = result.insertId;
}
//await _self.query('COMMIT');
let t2 = new Date().microtime(true);
log.debug('DbContext', 'update_host()',`complete ${t2 - t1}`);
return host;
} catch (e) {
log.error('DbContext', 'update_host()',e);
//await _self.query('ROLLBACK');
return null;
}
}
}