/* uglified */
const path=require("path"),fs=require("fs"),_=require("../utils/Underscore"),db=function(e){e=e||2097152;const o=openDatabase("11wlw_pos","1.0.0","",e);return o.dbName="11wlw_pos",o.exec=((e,n)=>{if(console.time("DB.exec"),!e)throw new Error("准备批量执行SQL，但参数SQL为空");o.transaction(function(o){e="string"==typeof e?e.split(";"):e;for(let n=0;n<e.length;n++)e[n].match(/(\w+)/)&&o.executeSql(e[n])},function(o,r,t){throw console.group("DB.exec"),console.log("sql=",e),console.table(o,r,t),console.groupEnd("DB.exec"),console.timeEnd("DB.exec"),_.isFunction(n)&&n(o),console.info(e),console.error(o),new Error(o.message)},function(o,r,t){console.group("DB.exec"),console.log("sql=",e),console.table(o,r,t),console.groupEnd("DB.exec"),console.timeEnd("DB.exec"),_.isFunction(n)&&n()})}),o.run=((e,n,r)=>{if(console.time("DB.run"),n||(n=[]),!e)throw new Error("DB.run 方法，参数SQL 必须填写");if(_.isArray(e)){if(!_.isFunction(n))throw new Error("批量执行多条SQL时，第二个参数必须是回调函数");return o.exec(e,r),!1}!0===_.isFunction(n)&&(r=n,n=[]),o.transaction(o=>{o.executeSql(e,n,(o,t)=>{console.group("DB.run"),console.log("sql=",e),console.log("params=",n),console.table(t),console.groupEnd("DB.run"),console.timeEnd("DB.run"),t.rows&&(t.rows=JSON.parse(JSON.stringify(t.rows)));let s={rows:[]};for(let e=0;e<t.rows.length;e++)s.rows.push(t.rows[e]);try{void 0!==t.rowsAffected&&(s.rowsAffected=t.rowsAffected),void 0!==t.insertId&&(s.insertId=t.insertId)}catch(e){}_.isFunction(r)&&r(s)},(o,t)=>{console.group("DB.run"),console.log("sql=",e),console.log("params=",n),console.error("sql error: "+t.message),console.error(o,t),console.groupEnd("DB.run"),console.timeEnd("DB.run"),_.isFunction(r)&&r(t)})})}),o.select=((e,n,r)=>{if(!e)throw new Error("DB.select 方法，参数SQL 必须填写");n||(n=[]),!0===_.isFunction(n)&&(r=n,n=[]),o.run(e,n,function(e){_.isFunction(r)&&r(e.rows)})}),o.insert=((e,n,r)=>{if(!e&&!n)throw new Error("DB.insert 方法，参数table, data缺一不可");if("string"!=typeof e)throw new Error("DB.insert 方法的table参数必须是 string");if(!_.isObject(n))throw new Error("DB.insert 方法的data参数必须是Object");let t=[],s=[],i=[];for(let e in n)t.push(e),/.+Time/g.test(e)?s.push("strftime('%Y-%m-%d %H:%M:%S', ?)"):s.push("?"),i.push(n[e]);let l="INSERT INTO "+e+"("+t.join(",")+")VALUES("+s.join(",")+")";o.run(l,i,function(e){_.isFunction(r)&&r(e.insertId)})}),o.update=((e,n,r,t,s)=>{if(!e&&!n&&!r)throw new Error("DB.update 方法，参数table, data, where缺一不可");t||(t=[]),_.isFunction(t)&&(s=t,t=[]);let i=o.__mkParam(n);for(let e=i.param.length-1;e>=0;e--)t.unshift(i.param[e]);let l="UPDATE "+e+" SET "+i.sql;r&&(l+=" WHERE "+r),o.run(l,t,function(e){_.isFunction(s)&&s(e.rowsAffected)})}),o.__dbInit__=(e=>{let n=path.resolve(__dirname,"../initialize.dll");fs.readFile(n,"utf-8",(n,r)=>{o.exec(r,e)})}),o.__dbUpdate__=(e=>{let n=path.resolve(__dirname,"../update.dll");fs.readFile(n,"utf-8",(r,t)=>{console.info("开始升级数据库"),console.info(t),t=t?t.split(";"):[];for(let e=0;e<t.length;e++)t[e].match(/(\w+)/)&&o.exec(t[e],function(o){o&&(console.info(t[e]),console.error(o))});try{fs.unlinkSync(n)}catch(e){console.error(e)}_.isFunction(e)&&e()})}),o.__mkParam=(e=>{let o=[],n=[];if("object"==typeof e)for(let r in e)/.+Time/g.test(r)?o.push(r+"=strftime('%Y-%m-%d %H:%M:%S', ?)"):o.push(r+"=?"),n.push(e[r]);return{sql:o.join(" , "),param:n}}),o};module.exports=new db;