/var/www/myprog.com.ua/server/node/db
Edit: /var/www/myprog.com.ua/server/node/db/db_context_devices.js (4058B)
const Device = require('../objects/device');
const log = require('../helpers/log');
module.exports = (DbContext) => {
DbContext.prototype.get_devices = async function () {
let t1 = new Date().microtime(true);
let _self = this;
let devices = [];
try {
let sql = "SELECT * FROM devices ORDER BY `type`,`n` LIMIT 100000";
//log.debug('DbContext', 'get_devices()', `SQL: ${sql}`);
let result = await _self.query(sql);
result.forEach(row => {
devices.push(new Device(row));
});
} catch (e) {
log.error('DbContext', 'get_devices()', e);
}
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'get_devices()', `complete ${t2 - t1}`);
return devices;
}
DbContext.prototype.get_device_by_id = async function (id) {
let t1 = new Date().microtime(true);
let _self = this;
let device = null;
try {
let sql = `SELECT * FROM devices WHERE id='${id}' LIMIT 1`;
log.debug('DbContext', 'get_device_by_id()', `SQL: ${sql}`);
let result = await _self.query(sql);
if (result[0].id > 0) {
device = new Device(result[0]);
}
} catch (e) {
log.error('DbContext', 'get_device_by_id()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_device_by_id()', `complete ${t2 - t1}`);
return device;
}
DbContext.prototype.get_device = async function (host_id, device_type, device_n) {
let t1 = new Date().microtime(true);
let _self = this;
let device = null;
try {
let sql = `SELECT * FROM devices WHERE host_id='${host_id}' AND type='${device_type}' AND n='${device_n}' LIMIT 1`;
//log.debug('DbContext', 'get_device()', `SQL: ${sql}`);
let result = await _self.query(sql);
if(result[0]!=undefined){
if (result[0].id > 0) {
device = new Device(result[0]);
}
}
} catch (e) {
log.error('DbContext', 'get_device()', e);
}
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'get_device()', `complete ${t2 - t1}`);
return device;
}
DbContext.prototype.get_user_devices = async function (user_id) {
let _self = this;
let t1 = new Date().microtime(true);
let sql = `SELECT d.* FROM devices d
INNER JOIN users_hosts uh ON uh.host_id=d.host_id
WHERE uh.user_id='${user_id}' ORDER BY d.type,d.n LIMIT 1000`;
log.debug('DbContext', 'get_user_devices()', `SQL: ${sql}`);
let devices = [];
try {
let rows = await _self.query(sql);
for (row of rows) {
devices.push(new Device(row));
}
} catch (e) {
log.error('DbContext', 'get_user_devices()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_user_devices()', `complete ${t2 - t1}`);
return devices;
}
DbContext.prototype.update_device = async function (device) {
let t1 = new Date().microtime(true);
let _self = this;
//await this.query('START TRANSACTION');
try {
let sql = _self.sql_insert_or_update(device, 'devices');
//log.debug('DbContext', 'update_device()',`SQL: ${sql}`);
let result = await _self.query(sql);
if (result.insertId > 0) {
device.id = result.insertId;
}
//await _self.query('COMMIT');
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'update_device()',`complete ${t2 - t1}`);
return device;
} catch (e) {
log.error('DbContext', 'update_device()',e);
//await _self.query('ROLLBACK');
return null;
}
}
}