Untitled
user_4058768
plain_text
18 days ago
16 kB
2
Indexable
Never
// Table name: SDH_CONFIG_HOC_PHAN { id, maMonHoc, maHocPhan, phanTram, namHoc, hocKy, maLoaiDiem } const keys = ['ID']; const obj2Db = { 'id': 'ID', 'maMonHoc': 'MA_MON_HOC', 'maHocPhan': 'MA_HOC_PHAN', 'phanTram': 'PHAN_TRAM', 'namHoc': 'NAM_HOC', 'hocKy': 'HOC_KY', 'maLoaiDiem': 'MA_LOAI_DIEM' }; module.exports = app => { const db = 'main'; const tableName = 'SDH_CONFIG_HOC_PHAN'; const type = 'table'; const schema = { ID: { type: 'NUMBER', length: '22,0', autoIncrement: true, primaryKey: true }, MA_MON_HOC: { type: 'NVARCHAR2', length: '50' }, MA_HOC_PHAN: { type: 'NVARCHAR2', length: '50' }, PHAN_TRAM: { type: 'NUMBER', length: '5,0' }, NAM_HOC: { type: 'NVARCHAR2', length: '11' }, HOC_KY: { type: 'NUMBER', length: '1,0' }, MA_LOAI_DIEM: { type: 'NVARCHAR2', length: '2' } }; const methods = { 'searchPage': 'SDH_CAU_HINH_HOC_PHAN_SEARCH_PAGE', 'getStudent': 'SDH_HOC_PHAN_GET_STUDENT', 'getDataHocPhan': 'SDH_HOC_PHAN_GET_DATA' }; app.model.sdhConfigHocPhan = { create: (data, done) => new Promise((resolve, reject) => { let statement = '', values = '', parameter = {}; Object.keys(data).forEach(column => { if (obj2Db[column]) { statement += ', ' + obj2Db[column]; values += ', :' + column; parameter[column] = data[column]; } }); if (statement.length == 0) { done && done('Data is empty!'); reject('Data is empty!'); } else { const sql = 'INSERT INTO SDH_CONFIG_HOC_PHAN (' + statement.substring(2) + ') VALUES (' + values.substring(2) + ')'; app.database.oracle.connection.main.executeExtra(sql, parameter, (error, resultSet) => { if (error == null && resultSet && resultSet.lastRowid) { app.model.sdhConfigHocPhan.get({ rowId: resultSet.lastRowid }).then(item => { done && done(null, item); resolve(item); }).catch(error => { done && done(error); reject(error); }); } else { done && done(error ? error : 'Execute SQL command fail! Sql = ' + sql); reject(error ? error : 'Execute SQL command fail! Sql = ' + sql); } }); } }), get: (condition, selectedColumns, orderBy, done) => new Promise((resolve, reject) => { if (condition == undefined) { done = null; condition = {}; selectedColumns = '*'; } else if (typeof condition == 'function') { done = condition; condition = {}; selectedColumns = '*'; } else if (selectedColumns && typeof selectedColumns == 'function') { done = selectedColumns; selectedColumns = '*'; } else { selectedColumns = selectedColumns ? selectedColumns : '*'; } if (orderBy) Object.keys(obj2Db).sort((a, b) => b.length - a.length).forEach(key => orderBy = orderBy.replaceAll(key, obj2Db[key])); condition = app.database.oracle.buildCondition(obj2Db, condition, ' AND '); const parameter = condition.parameter ? condition.parameter : {}; const sql = 'SELECT ' + app.database.oracle.parseSelectedColumns(obj2Db, selectedColumns) + ' FROM (SELECT * FROM SDH_CONFIG_HOC_PHAN' + (condition.statement ? ' WHERE ' + condition.statement : '') + (orderBy ? ' ORDER BY ' + orderBy : '') + ') WHERE ROWNUM=1'; app.database.oracle.connection.main.executeExtra(sql, parameter, (error, resultSet) => { if (error) { done && done(error); reject(error); } else { const item = resultSet && resultSet.rows && resultSet.rows.length ? resultSet.rows[0] : null; done && done(null, item); resolve(item); } }); }), getAll: (condition, selectedColumns, orderBy, done) => new Promise((resolve, reject) => { if (condition == undefined) { done = null; condition = {}; selectedColumns = '*'; } else if (typeof condition == 'function') { done = condition; condition = {}; selectedColumns = '*'; } else if (selectedColumns && typeof selectedColumns == 'function') { done = selectedColumns; selectedColumns = '*'; } else { selectedColumns = selectedColumns ? selectedColumns : '*'; } if (orderBy) Object.keys(obj2Db).sort((a, b) => b.length - a.length).forEach(key => orderBy = orderBy.replaceAll(key, obj2Db[key])); condition = app.database.oracle.buildCondition(obj2Db, condition, ' AND '); const parameter = condition.parameter ? condition.parameter : {}; const sql = 'SELECT ' + app.database.oracle.parseSelectedColumns(obj2Db, selectedColumns) + ' FROM SDH_CONFIG_HOC_PHAN' + (condition.statement ? ' WHERE ' + condition.statement : '') + (orderBy ? ' ORDER BY ' + orderBy : ''); app.database.oracle.connection.main.executeExtra(sql, parameter, (error, resultSet) => { if (error) { done && done(error); reject(error); } else { const items = resultSet && resultSet.rows ? resultSet.rows : []; done && done(null, items); resolve(items); } }); }), getPage: (pageNumber, pageSize, condition, selectedColumns, orderBy, done) => new Promise((resolve, reject) => { if (condition == undefined) { done = null; condition = {}; selectedColumns = '*'; } else if (typeof condition == 'function') { done = condition; condition = {}; selectedColumns = '*'; } else if (selectedColumns && typeof selectedColumns == 'function') { done = selectedColumns; selectedColumns = '*'; } else { selectedColumns = selectedColumns ? selectedColumns : '*'; } if (orderBy) Object.keys(obj2Db).sort((a, b) => b.length - a.length).forEach(key => orderBy = orderBy.replaceAll(key, obj2Db[key])); condition = app.database.oracle.buildCondition(obj2Db, condition, ' AND '); let leftIndex = (pageNumber <= 1 ? 0 : pageNumber - 1) * pageSize, parameter = condition.parameter ? condition.parameter : {}; const sqlCount = 'SELECT COUNT(*) FROM SDH_CONFIG_HOC_PHAN' + (condition.statement ? ' WHERE ' + condition.statement : ''); app.database.oracle.connection.main.executeExtra(sqlCount, parameter, (error, res) => { if (error) { done && done(error); reject(error); } else { let result = {}; let totalItem = res && res.rows && res.rows[0] ? res.rows[0]['COUNT(*)'] : 0; result = { totalItem, pageSize, pageTotal: Math.ceil(totalItem / pageSize) }; result.pageNumber = Math.max(1, Math.min(pageNumber, result.pageTotal)); leftIndex = Math.max(0, result.pageNumber - 1) * pageSize; const sql = 'SELECT ' + app.database.oracle.parseSelectedColumns(obj2Db, selectedColumns) + ' FROM (SELECT SDH_CONFIG_HOC_PHAN.*, ROW_NUMBER() OVER (ORDER BY ' + (orderBy ? orderBy : keys) + ') R FROM SDH_CONFIG_HOC_PHAN' + (condition.statement ? ' WHERE ' + condition.statement : '') + ') WHERE R BETWEEN ' + (leftIndex + 1) + ' and ' + (leftIndex + pageSize); app.database.oracle.connection.main.executeExtra(sql, parameter, (error, resultSet) => { if (error) { done && done(error); reject(error); } else { result.list = resultSet && resultSet.rows ? resultSet.rows : []; done && done(null, result); resolve(result); } }); } }); }), update: (condition, changes, done) => new Promise((resolve, reject) => { condition = app.database.oracle.buildCondition(obj2Db, condition, ' AND '); changes = app.database.oracle.buildCondition(obj2Db, changes, ', ', 'NEW_'); if (Object.keys(condition).length == 0) { done && done('No condition!'); reject('No condition!'); } else if (changes.statement) { const parameter = app.clone(condition.parameter ? condition.parameter : {}, changes.parameter ? changes.parameter : {}); const sql = 'UPDATE SDH_CONFIG_HOC_PHAN SET ' + changes.statement + (condition.statement ? ' WHERE ' + condition.statement : ''); app.database.oracle.connection.main.executeExtra(sql, parameter, (error, resultSet) => { if (error == null && resultSet && resultSet.lastRowid) { app.model.sdhConfigHocPhan.get({ rowId: resultSet.lastRowid }).then(item => { done && done(null, item); resolve(item); }).catch(error => { done && done(error); reject(error); }); } else { done && done(error); reject(error); } }); } else { done && done('No changes!'); reject('No changes!'); } }), delete: (condition, done) => new Promise((resolve, reject) => { if (condition == undefined) { done = null; condition = {}; } else if (typeof condition == 'function') { done = condition; condition = {}; } condition = app.database.oracle.buildCondition(obj2Db, condition, ' AND '); if (Object.keys(condition).length == 0) { done && done('No condition!'); reject('No condition!'); } else { const parameter = condition.parameter ? condition.parameter : {}; const sql = 'DELETE FROM SDH_CONFIG_HOC_PHAN' + (condition.statement ? ' WHERE ' + condition.statement : ''); app.database.oracle.connection.main.executeExtra(sql, parameter, error => { if (error) { done && done(error); reject(error); } else { done && done(); resolve(); } }); } }), count: (condition, done) => new Promise((resolve, reject) => { if (condition == undefined) { done = null; condition = {}; } else if (typeof condition == 'function') { done = condition; condition = {}; } condition = app.database.oracle.buildCondition(obj2Db, condition, ' AND '); const parameter = condition.parameter ? condition.parameter : {}; const sql = 'SELECT COUNT(*) FROM SDH_CONFIG_HOC_PHAN' + (condition.statement ? ' WHERE ' + condition.statement : ''); app.database.oracle.connection.main.executeExtra(sql, parameter, (error, result) => { if (error) { done && done(error); reject(error); } else { done && done(null, result); resolve(result); } }); }), searchPage: (pagenumber, pagesize, filter, mahocphan, searchterm, done) => new Promise((resolve, reject) => { app.database.oracle.connection.main.executeExtra('BEGIN :ret:=sdh_cau_hinh_hoc_phan_search_page(:pagenumber, :pagesize, :filter, :mahocphan, :searchterm, :totalitem, :pagetotal); END;', { ret: { dir: app.database.oracle.BIND_OUT, type: app.database.oracle.CURSOR }, pagenumber: { val: pagenumber, dir: app.database.oracle.BIND_INOUT, type: app.database.oracle.NUMBER }, pagesize: { val: pagesize, dir: app.database.oracle.BIND_INOUT, type: app.database.oracle.NUMBER }, filter, mahocphan, searchterm, totalitem: { dir: app.database.oracle.BIND_OUT, type: app.database.oracle.NUMBER }, pagetotal: { dir: app.database.oracle.BIND_OUT, type: app.database.oracle.NUMBER } }, (error, result) => app.database.oracle.fetchRowsFromCursor(error, result, (error, result) => { if (error) { done && done(error); reject(error); } else { done && done(null, result); resolve(result); } })); }), getStudent: (mahocphan, filter, done) => new Promise((resolve, reject) => { app.database.oracle.connection.main.executeExtra('BEGIN :ret:=sdh_hoc_phan_get_student(:mahocphan, :filter); END;', { ret: { dir: app.database.oracle.BIND_OUT, type: app.database.oracle.CURSOR }, mahocphan, filter }, (error, result) => app.database.oracle.fetchRowsFromCursor(error, result, (error, result) => { if (error) { done && done(error); reject(error); } else { done && done(null, result); resolve(result); } })); }), getDataHocPhan: (filter, done) => new Promise((resolve, reject) => { app.database.oracle.connection.main.executeExtra('BEGIN :ret:=sdh_hoc_phan_get_data(:filter, :dataconfigmonhoc); END;', { ret: { dir: app.database.oracle.BIND_OUT, type: app.database.oracle.CURSOR }, filter, dataconfigmonhoc: { dir: app.database.oracle.BIND_OUT, type: app.database.oracle.CURSOR } }, (error, result) => app.database.oracle.fetchRowsFromCursor(error, result, (error, result) => { if (error) { done && done(error); reject(error); } else { done && done(null, result); resolve(result); } })); }), }; return { db, tableName, type, schema, methods, keys }; };
Leave a Comment