gnh1201 / welsonjs

WelsonJS - Build a Windows app on the Windows built-in JavaScript engine

Home Page:https://catswords.social/@catswords_oss

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[lib/system] Porting Squel.js to WelsonJS (WMI SQL on WSH)

gnh1201 opened this issue · comments

WelsonJS supports WMI Query (working on Microsoft WSH), which has standard SQL syntax. for example:

// lib/system.js:L53
return WMI.execQuery("SELECT * FROM Win32_OperatingSystem").fetch().get("Caption").rtrim();
// lib/system.js:L65
return WMI.execQuery("SELECT * FROM Win32_OperatingSystem").fetch().get("OSArchitecture");
// lib/system.js:L69
return WMI.execQuery("SELECT * FROM Win32_ComputerSystemProduct").fetch().get("UUID").toLowerCase();
// lib/system.js:L112
return WMI.execQuery("SELECT * FROM Win32_NetworkAdapterConfiguration").fetchAll();
// lib/system.js:L171
return WMI.execQuery("Select * From Win32_PingStatus where address='" + address + "'").fetch().get("ResponseTime");

We want to simplify this using the SQL query builder as follows. like this:

log(
    squel.select()
        .field("ResponseTime")
        .from("Win32_PingStatus")
        .where("address = '127.0.0.1'")
        .toString()
);
/*  SELECT ResponseTime FROM Win32_PingStatus WHERE (address = '127.0.0.1')  */

but, Squel.js does not work on the Microsoft WSH JavaScript engine. Squel.js can be found at the website below.

This problem is presumed to be caused by Object.defineProperty.

By default, the WSH JavaScript engine has IE 9 level compatibility. WelsonJS uses shim and babel to raise the compatibility level to ES6. However, Object.defineProperty is not fully supported.

This issue has been resolved. 4e3c52e

To solve this problem, we needed to include the polyfill of the label and then deal with some reserved terms. For example, the original squel.js contains the following content.

  try {
    for (var _iterator2 = handlers[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
      var typeHandler = _step2.value;

      if (typeHandler.type === type) {
        typeHandler.handler = handler;

        return;
      }
    }
  } catch (err) {
    _didIteratorError2 = true;
    _iteratorError2 = err;
  } finally {
    try {
      if (!_iteratorNormalCompletion2 && _iterator2.return) {
        _iterator2.return();
      }
    } finally {
      if (_didIteratorError2) {
        throw _iteratorError2;
      }
    }
  }

Since return is a reserved term, a polyfill is offering it as RETURN as a replacement.

  var _squel = {
    VERSION: '5.13.1',
    flavour: flavour,
    expr: function expr(options) {
      return new cls.Expression(options);
    },
    case: function _case(name, options) {
      return new cls.Case(name, options);
    },
    select: function select(options, blocks) {
      return new cls.Select(options, blocks);
    },
    update: function update(options, blocks) {
      return new cls.Update(options, blocks);
    },
    insert: function insert(options, blocks) {
      return new cls.Insert(options, blocks);
    },
    delete: function _delete(options, blocks) {
      return new cls.Delete(options, blocks);
    },
	str: function str() {
      var inst = new cls.FunctionBlock();
	  inst.function.apply(inst, arguments);
      return inst;
    },
    rstr: function rstr() {
      var inst = new cls.FunctionBlock({
        rawNesting: true
      });
	  inst.function.apply(inst, arguments);
      return inst;
    },
    registerValueHandler: cls.registerValueHandler
  };

Since case, delete, function is a reserved term, we could replace it with an underscore.

The results are as follows.

code:

exports.main = function() {
	console.log(squel.select({ separator: "\n" })
        .from("students")
        .field("name")
        .field("MIN(test_score)")
        .field("MAX(test_score)")
        .field("GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ')")
        .group("name")
        .toString());
};

result:

$ cscript app.js sqlquerystring
  * SELECT
name, MIN(test_score), MAX(test_score), GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ')
FROM students
GROUP BY name