UNPKG

jh-common-dataaccess

Version:
251 lines (249 loc) 6.55 kB
const DsParams = require('./DsParams'); const DataAccess = require('./DataAccess'); const TransParams = require('./TransParams'); const OperationEnum = require('./OperationEnum'); module.exports = class Public { constructor(dbconf, tbconf) { this.dbconf = dbconf; this.tbconf = {}; Object.keys(tbconf).forEach(key => { this.tbconf[key] = tbconf[key]; }); this.ds = new DataAccess(this.dbconf); } /** * 生成参数化SQL语句 * @param {*} obj 实体类/实体类数组 * @param {*} operationType 操作类型 */ operationSQLParams(obj, operationType) { let tableName = ''; let pkNames = ''; if (obj instanceof Array) { if (obj.length > 0) { tableName = this.tbconf[obj[0].constructor.name].name; pkNames = this.tbconf[obj[0].constructor.name].pk; } else { return null; } } else { tableName = this.tbconf[obj.constructor.name].name; pkNames = this.tbconf[obj.constructor.name].pk; obj = [obj]; } switch (operationType) { case OperationEnum.Create: return this.insertSQLParams(obj, tableName); break; case OperationEnum.Delete: return this.deleteSQLParams(obj, tableName, pkNames); break; case OperationEnum.Update: return this.updateSQLParams(obj, tableName, pkNames); break; case OperationEnum.UpdateNoCheck: return this.updateNoCheckSQLParams(obj, tableName, pkNames); break; default: return null; break; } } /** * 插入数据 * @param {*} objlist 实体类 * @param {*} tableName 表名 */ insertSQLParams(objlist, tableName) { const tparams = new TransParams(); const params = []; const row = []; const column = []; tableName = tableName || ''; const cdField = this.tbconf[objlist[0].constructor.name].cd; const udField = this.tbconf[objlist[0].constructor.name].ud; objlist.forEach((obj, index) => { if (typeof obj === 'object') { if (cdField) { obj[cdField] = new Date(); } if (udField) { obj[udField] = new Date(); } column.push(' ( '); Object.keys(obj).forEach(pf => { if (![null, undefined].includes(obj[pf])) { if (index === 0) { row.push(this.ds.packCaseLow(pf), ' , '); } column.push('@' + index + '_' + pf, ' , '); params.push( new DsParams({ paramsname: index + '_' + pf, paramsvalue: obj[pf], }) ); } }); if (index === 0) { row.pop(); } column.pop(); column.push(' ) ', ' , '); } }); row.unshift('insert into ' + this.ds.packCaseLow(tableName) + ' ( '); row.push(' ) ', ' values '); column.pop(); tparams.set({ sql: row.join('') + column.join(''), l_dp: params, }); return tparams; } /** * 删除数据(物理删除) * @param {*} objlist * @param {*} tableName * @param {*} pkNames */ deleteSQLParams(objlist, tableName, pkNames) { const tparams = new TransParams(); const params = []; const row = []; tableName = tableName || ''; objlist.forEach((obj, index) => { if (typeof obj === 'object') { const properties = []; pkNames.split(',').forEach(pk => { Object.keys(obj).forEach(key => { if (pk === key) { properties.push(key); } }); }); if (properties.length > 0) { const filter = []; const _row = []; _row.push('delete from ' + this.ds.packCaseLow(tableName) + ' where '); properties.forEach(pf => { if (![null, undefined].includes(obj[pf])) { if (filter.length !== 0) { filter.push(' and '); } filter.push(this.ds.packCaseLow(pf) + ' = @' + index + '_' + pf); params.push( new DsParams({ paramsname: index + '_' + pf, paramsvalue: obj[pf], }) ); } }); if (filter.length > 0) { _row.push(filter.join('')); row.push(_row.join(''), ' ; '); } } } }); if (row.length === 0) { return null; } tparams.set({ sql: row.join(''), l_dp: params, }); return tparams; } /** * 更新数据(内部检查修改时间有效性,loading) * @param {*} objlist * @param {*} tableName * @param {*} pkNames */ updateSQLParams(objlist, tableName, pkNames) { return this.updateNoCheckSQLParams(objlist, tableName, pkNames); } /** * 更新数据,不检查修改时间有效性 * @param {*} objlist * @param {*} tableName * @param {*} pkNames */ updateNoCheckSQLParams(objlist, tableName, pkNames) { const tparams = new TransParams(); const params = []; const row = []; tableName = tableName || ''; const cdField = this.tbconf[objlist[0].constructor.name].cd; const udField = this.tbconf[objlist[0].constructor.name].ud; objlist.forEach((obj, index) => { if (typeof obj === 'object') { const properties = Object.keys(obj); if (udField) { obj[udField] = new Date(); } if (properties.length > 0) { const filter = []; const _row = []; _row.push('update ' + this.ds.packCaseLow(tableName) + ' set '); properties.forEach(pf => { if (![null, undefined].includes(obj[pf])) { if (pkNames.includes(pf)) { if (filter.length == 0) { filter.push(' where ' + this.ds.packCaseLow(pf) + ' = @' + index + '_' + pf); } else { filter.push(' and ' + this.ds.packCaseLow(pf) + ' = @' + index + '_' + pf); } params.push( new DsParams({ paramsname: index + '_' + pf, paramsvalue: obj[pf], }) ); } else if (![cdField].includes(pf)) { if ([udField].includes(pf)) { obj[pf] = new Date(); } _row.push(this.ds.packCaseLow(pf) + ' = @' + index + '_' + pf, ' , '); params.push( new DsParams({ paramsname: index + '_' + pf, paramsvalue: obj[pf], }) ); } } }); if (filter.length > 0) { _row.pop(); row.push(_row.join('') + filter.join(''), ' ; '); } } } }); if (row.length === 0) { return null; } tparams.set({ sql: row.join(''), l_dp: params, }); return tparams; } /** * 生成主键 */ static buildCode() { var d = new Date().getTime(); var uuid = 'xxxxxxxxxxxxxxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16); }); return uuid.replace(/((-)|([a-z]))/gi, ($1, $2) => { return Number.parseInt(Math.random() * 10, 10); }); } };