"use strict";(function(){var t=0;var i=0;"use strict";var n=require("util");var e=require("events");var s=require("dsjslib").LinkedDeque;function l(t,i){e.EventEmitter.call(this);if(false===this instanceof l){return new l}this.interval=500;this.insertLimit=100;this.sqlLimit=typeof i!=="undefined"?i:500;this.db=t;this.sqlList=[];this.timeOut=null}n.inherits(l,e.EventEmitter);l.prototype.run=function(t,i,n,e){var s=this;if(i&&i instanceof Function){e=n;n=i;i=null}return s.insertSqlIntoList(t,e,n,i)};l.prototype.all=function(t,i,n){var e=this;if(i&&i instanceof Function){n=i;i=null}a(e);r(e,t,n,i)};l.prototype.insertSqlIntoList=function(t,i,n,e){var l=this;var o="";var c=false;if(i!=null&&i.length>0){o=i[0]}var v=u(l);if(v>=l.sqlLimit){return false}if(t.toLocaleString().search("select")!==-1){a(l);r(l,t,n,e);return true}for(var m=0;m<l.sqlList.length;m++){if(l.sqlList[m].tableName==o){var h={sql:t,params:e,callback:n};l.sqlList[m].sqlEntries.push(h);c=true;break}}if(c==false){var q=new s;var h={sql:t,params:e,callback:n};q.push(h);var b={tableName:o,sqlEntries:q};l.sqlList.push(b)}if(this.timeOut===null){this.timeOut=setTimeout(function(){f(l,l.interval)},l.interval)}return true};function r(t,i,n,e){if(e==null||e==undefined){t.db.all(i,n)}else{t.db.all(i,e,n)}}function a(i,n,e){var s="";var l="";i.db.run("begin",function(){s=new Date});var r=0;e=e===undefined?i.insertLimit:e;for(var a=0;a<i.sqlList.length;a++){while(i.sqlList[a].sqlEntries.size()>0){var u=i.sqlList[a].sqlEntries.shift();(function(t){if(t.params==null||t.params==undefined){i.db.run(t.sql,function(i){if(t.callback instanceof Function){t.callback(i)}})}else{i.db.run(t.sql,t.params,function(i){if(t.callback instanceof Function){t.callback(i)}})}})(u);r++;if(e!==0&&r>=e){break}}if(e!=0&&r>=e){break}}i.db.run("commit",function(){l=new Date;if(t){console.log("time consumption = "+(l-s)+"ms")}if(n!==undefined){n("finish")}})}l.prototype.flush=function(t){f(this,0,t,0)};function u(t){var i=0;for(var n=0;n<t.sqlList.length;n++){i+=t.sqlList[n].sqlEntries.size()}return i}function f(t,i,n,e){a(t,n,e);var s=u(t);if(s==0){if(t.timeOut!==null){clearTimeout(t.timeOut);t.timeOut=null}}else{t.timeOut=setTimeout(function(){f(t,i)},i)}}module.exports=l;if(i){}})();