github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/cmd/natsql/test-/001/natsql.js (about)

     1  //扩展方法
     2  // Date.prototype.Add = function (seconds) { var t = new Date(); t.setTime(this.getTime() + seconds * 1000); return t; };
     3  // Date.prototype.AddDate = Date.prototype.AddDays = function (days) { var t = new Date(); t.setTime(this.getTime() + days * 24 * 3600 * 1000); return t; };
     4  // Date.prototype.Date = function () { return this.toISOString().split("T")[0]; };
     5  // Date.prototype.Time = function () { return this.toISOString().split("T")[1].split(".")[0]; };
     6  // Date.prototype.DateTime = function () { return this.toISOString().replace("T", " ").split(".")[0]; };
     7  // String.prototype.Date = function () { return this.replace("T", " ").split(" ")[0]; };
     8  // String.prototype.Time = function () { return this.replace("T", " ").split(" ")[1].split(".")[0]; };
     9  String.prototype.DateTime = function () { return this.replace("T", " ").split(".")[0]; };
    10  function col(s) { if (!s) { return "NULL"; } return "'" + s.replace("'", "''") + "'"; };
    11  
    12  //订阅处理
    13  function sql(records) {
    14      if (!records || records.constructor.name != "Array") return;
    15      var items = records.filter(function (item) { return item.constructor.name == "Object" && item.hasOwnProperty("Code"); });
    16      if (items.length == 0) return;
    17      var sqlString = "insert into logtest(Type,Code,Message,Account,CreateTime,CreateUser) values"
    18          + items.map(function (item) {
    19              return "("
    20                  + item.Type + ","
    21                  + col(item.Code) + ","
    22                  + col(item.Message) + ","
    23                  + col(item.Account) + ","
    24                  + col(item.CreateTime.DateTime()) + ","
    25                  + col(item.CreateUser) + ")";
    26          }).join(",") + ";";
    27      // console.log(sqlString);
    28      return sqlString;
    29  }