"use strict";(function(){"use strict";var e=require("util");var o=require("./jsUtil");function s(){if(false===this instanceof s){return new s}}s.prototype.setUpDb=function(e){var s=this;var n=require("fs");var t=o.getDataPath();var i=t+"/db";var r=t+"/db/history.db";if(!o.fsExistsSync(i)){n.mkdirSync(i)}this.exists=o.fsExistsSync(r);if(!this.exists){n.openSync(r,"w")}var a=require("sqlite3").verbose();this.db=new a.Database(r);this.db.serialize();this.db.run("CREATE TABLE IF NOT EXISTS loginHis(protocal VARCHAR(10), com VARCHAR(10), baudrate INTEGER, username VARCHAR(32), password VARCHAR(32), host VARCHAR(32), enablePassword VARCHAR(32), port INTEGER, encoding VARCHAR(10))");s.db.all("PRAGMA table_info(loginHis)",function(e,o){var n=false;var t=false;for(var i=0;i<o.length;i++){var r=o[i];if(r.name=="encoding"){n=true}if(r.name=="port"){t=true}}if(n==false){s.db.run("ALTER TABLE loginHis ADD COLUMN encoding VARCHAR(10);")}if(t==false){s.db.run("ALTER TABLE loginHis ADD COLUMN port INTEGER;")}})};s.prototype.insertIntoDb=function(e,o){var s=this;var n={protocal:e.protocal,comNum:typeof e.comNum!=="undefined"?e.comNum:"COMx",baudrate:typeof e.baudrate!=="undefined"?e.baudrate:0,host:typeof e.host!=="undefined"?e.host:"",username:typeof e.username!=="undefined"?e.username:"",password:typeof e.password!=="undefined"?e.password:"",enablePassword:typeof e.enablePassword!=="undefined"?e.enablePassword:"",port:typeof e.port!=="undefined"?e.port:"",encoding:typeof e.encoding!=="undefined"?e.encoding:"utf8"};this.db.serialize(function(){s.deleteFromDb(n);s.db.run("REPLACE INTO loginHis(protocal, com, baudrate, username, password, host, enablePassword, port, encoding) "+"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",[n.protocal,n.comNum,n.baudrate,n.username,n.password,n.host,n.enablePassword,n.port,n.encoding],function(e){if(e){o(e)}else{o("success")}})})};s.prototype.deleteFromDb=function(e,o){if(e==="NULL"){this.db.run("DELETE FROM loginHis;",function(e){if(o!=undefined){if(e){o(e)}else{console.log("error");o("success")}}})}else{if(e.protocal==="telnet"){this.db.run("DELETE FROM loginHis WHERE host = ? and protocal = ?;",[e.host,"telnet"],function(e){if(o!=undefined){if(e){o(e)}else{o("success")}}})}else if(e.protocal==="serial"){this.db.run("DELETE FROM loginHis WHERE com = ?;",[e.comNum],function(e){if(o!=undefined){if(e){o(e)}else{o("success")}}})}else if(e.protocal==="SSH"){this.db.run("DELETE FROM loginHis WHERE host = ? and protocal = ? ;",[e.host,"SSH"],function(e){if(o!=undefined){if(e){o(e)}else{o("success")}}})}else if(e.protocal==="http"){this.db.run("DELETE FROM loginHis WHERE host = ? and protocal = ?;",[e.host,"http"],function(e){if(o!=undefined){if(e){o(e)}else{o("success")}}})}else if(e.protocal==="https"){this.db.run("DELETE FROM loginHis WHERE host = ? and protocal = ?;",[e.host,"https"],function(e){if(o!=undefined){if(e){o(e)}else{o("success")}}})}}};s.prototype.queryFromDb=function(e){this.db.all("SELECT * FROM loginHis;",function(o,s){e(o,s)})};s.prototype.closeDb=function(){this.db.close()};module.exports=s})();