obcon-scada / app /include /modelForSql.js
chanmin0723's picture
Initial obcon SCADA deploy
e4bf523
Raw
History Blame Contribute Delete
11.3 kB
'use strict'
/**
* Copyright (c) 2017~2022, OBCon Inc.
* All rights reserved.
*/
/**
* @file
* @copyright 2017~2022, OBCon Inc.
* @author gye hyun james kim [pnuskgh@gmail.com]
*/
//--- SQL문을 사용하여 Sequelize 라이브러리를 대체하는 class
//--- 사용하지 않음
class ModelForSql {
constructor() {
this._use = true; //--- true. 사용하는 모듈
this._title = '정류기 데이터'; //--- 테이블 한글명
this._tableName = 'devicedata1s'; //--- 테이블 영문명
this._type = 'mariadb'; //--- Database type
this._conn = null; //--- Database connection
}
get tableName() {
return this._tableName;
}
get conn() {
return this._conn;
}
set conn(newValue) {
this._conn = newValue;
}
get table() {
return {
findOne: this.findOne.bind(this),
findAll: this.findAll.bind(this),
create: this.create.bind(this),
update: this.update.bind(this),
destroy: this.destroy.bind(this),
};
}
get fieldDesc() {
return {
// name: {
// type: 'string', len: 72,
// allowNull: false,
// defaultValue: '',
// validate: {},
// title: '이름',
// strType: 'string'
// },
// description: {
// type: 'text',
// allowNull: true,
// defaultValue: '',
// validate: {},
// title: '상세 설명',
// strType: 'text'
// },
// assignedUserId: {
// type: 'integer', len: 11,
// allowNull: false,
// defaultValue: 1,
// validate: {
// min: 1
// },
// title: '담당자 ID',
// strType: 'integer'
// },
// assignedUserName: {
// type: 'string', len: 72,
// allowNull: true,
// defaultValue: '',
// validate: {},
// title: '담당자',
// strType: 'string'
// },
// createdBy: {
// type: 'integer', len: 11,
// allowNull: false,
// defaultValue: 1,
// validate: {
// min: 1
// },
// title: '생성자',
// strType: 'integer'
// },
// updatedBy: {
// type: 'integer', len: 11,
// allowNull: false,
// defaultValue: 1,
// validate: {
// min: 1
// },
// title: '수정자',
// strType: 'integer'
// },
// deleted: {
// type: 'boolean',
// allowNull: false,
// defaultValue: false,
// validate: {},
// title: '삭제',
// strType: 'boolean'
// }
};
}
get fieldNames() {
return Object.getOwnPropertyNames(this.fieldDesc);
}
async findOne(sql, strQuery = null) {
//--- To-Do : 아래 코드를 수정하여 동작하도록 작성할 것
// let results = await this.findAll(sql, strQuery);
// if (0 < results.count) {
// if ((strQuery != null) && (-1 < strQuery.indexOf('NEXTVAL'))) {
// return results[0]['NEXTVAL'];
// } else {
// return results[0];
// }
// } else {
// return null;
// }
}
async findAll(sql, strQuery = null) {
//--- To-Do : 아래 코드를 수정하여 동작하도록 작성할 것
// let query = [];
// if (strQuery == null) {
// query.push('SELECT *');
// query.push(` FROM ${this._tableName}`);
// //--- WHERE 문 처리
// if (typeof(sql.where) != 'undefined') {
// query.push(' WHERE');
// query.push(this._set(sql.where, 'AND'));
// }
// //--- ORDER BY 문 처리
// if (typeof(sql.order) != 'undefined') {
// query.push(' ORDER BY');
// let data = [];
// for (let idx = 0; idx < sql.order.length; idx++) {
// let item = sql.order[idx];
// switch(item[0]) {
// case 'site': break;
// default:
// data.push(`${item[0]} ${item[1]}`);
// break;
// }
// }
// query.push(data.join(', '));
// }
// //--- LIMIT 문 처리
// if (typeof(sql.limit) != 'undefined') {
// query.push(` LIMIT ${sql.limit}`);
// }
// query = query.join(' ');
// } else {
// query = strQuery;
// }
// logger.info(query);
// let results = await db.conn.query(query);
// if (config.databases.obcon.type == 'Tibero') {
// for (let idx = 0; idx < results.length; idx++) {
// for (let pos = 0; pos < this._fieldNames.length; pos++) {
// let fieldName = this._fieldNames[pos];
// if (typeof(results[idx][fieldName.toUpperCase()]) != 'undefined') {
// results[idx][fieldName] = results[idx][fieldName.toUpperCase()];
// }
// }
// }
// }
// return results;
}
_set(item, delimiter, isIn=false) {
//--- To-Do : 아래 코드를 수정하여 동작하도록 작성할 것
// let data = [];
// for (const [key, value] of Object.entries(item)) {
// if (isIn) {
// // let key1 = (config.databases.obcon.type == 'Tibero') ? key.toUpperCase():key;
// if (this._fieldNames.includes(key) == false) {
// continue;
// }
// }
// switch(key) {
// case 'site': break;
// default:
// switch(typeof(value)) {
// case 'string':
// data.push(`${key} = '${value}'`);
// break;
// case 'number':
// case 'boolean':
// default:
// data.push(`${key} = ${value}`);
// break;
// }
// break;
// }
// }
// return data.join(` ${delimiter} `);
}
async create(item, strQuery = null) {
//--- To-Do : 아래 코드를 수정하여 동작하도록 작성할 것
// let query = `SELECT seq_${this._tableName}.NEXTVAL FROM DUAL`.replace('OBICON.', '');
// let id = await this.findOne(null, query);
// item['id'] = id;
// query = [];
// if (strQuery == null) {
// query.push(`INSERT INTO ${this._tableName} (`);
// let data = [];
// for (const [key, value] of Object.entries(item)) {
// // let key1 = (config.databases.obcon.type == 'Tibero') ? key.toUpperCase():key;
// if (this._fieldNames.includes(key)) {
// data.push(key);
// }
// }
// query.push(data.join(', '));
// query.push(') VALUES (');
// data = [];
// for (const [key, value] of Object.entries(item)) {
// // let key1 = (config.databases.obcon.type == 'Tibero') ? key.toUpperCase():key;
// if (this._fieldNames.includes(key)) {
// switch(key) {
// case 'site': break;
// default:
// switch(typeof(value)) {
// case 'string':
// data.push(`'${value}'`);
// break;
// case 'number':
// case 'boolean':
// default:
// data.push(`${value}`);
// break;
// }
// break;
// }
// }
// }
// query.push(data.join(', '));
// query.push(')');
// query = query.join(' ');
// } else {
// query = strQuery;
// }
// logger.info(`SQL Create : ${query}`);
// let results = await db.conn.query(query);
// results[0] = results.count;
// return results;
}
async update(item, sql = null, strQuery = null) {
//--- To-Do : 아래 코드를 수정하여 동작하도록 작성할 것
// let query = [];
// if (strQuery == null) {
// query.push(`UPDATE ${this._tableName}`);
// query.push(' SET');
// query.push(this._set(item, ',', true));
// //--- WHERE 문 처리
// if (typeof(sql.where) != 'undefined') {
// query.push(' WHERE');
// query.push(this._set(sql.where, 'AND'));
// }
// query = query.join(' ');
// } else {
// query = strQuery;
// }
// logger.info(`SQL Update : ${query}`);
// let results = await db.conn.query(query);
// results[0] = results.count;
// return results;
}
async destroy(sql, strQuery = null) {
//--- To-Do : 아래 코드를 수정하여 동작하도록 작성할 것
// let query = [];
// if (strQuery == null) {
// query.push(`DELETE FROM ${this._tableName}`);
// //--- WHERE 문 처리
// if (typeof(sql.where) != 'undefined') {
// query.push(' WHERE');
// query.push(this._set(sql.where, 'AND'));
// }
// query = query.join(' ');
// } else {
// query = strQuery;
// }
// //--- To-Do : ppp, 테스트 필요
// logger.info(`SQL delete : ${query}`);
// if (-1 < query.indexOf('id = ')) {
// let results = await db.conn.query(query);
// results[0] = results.count;
// return results;
// }
// return 0;
}
//--- 데이터 초기화
init() {
}
}
module.exports = ModelForSql;