如何根据 mysql2 中的 columnType 整数值确定列类型名称?

How do I determine the column type name from the columnType integer value in mysql2?

当使用 mysql2 查询时,回调函数 fields 的第三个参数具有以下 documentation:

console.log(fields); // fields contains extra meta data about results, if available

好的,很好。但是当我查看字段数组中的值时,我看到以下内容:

[
      {
        "_buf":{},
        "_clientEncoding":"utf8",
        "_catalogLength":3,
        "_catalogStart":10,
        "_schemaLength":0,
        "_schemaStart":14,
        "_tableLength":11,
        "_tableStart":15,
        "_orgTableLength":0,
        "_orgTableStart":27,
        "_orgNameLength":2,
        "_orgNameStart":31,
        "characterSet":63,
        "encoding":"binary",
        "name":"id",
        "columnLength":11,
        "columnType":3, // <-- column type 3? This is an INTEGER field
        "flags":1,
        "decimals":0
      },
      {
        ...
        "columnType":253, // <-- column type 253? This is a VARCHAR field
        ...
      }
]

如何根据这些整数值确定实际列类型?

进一步挖掘后,我通过查看 source 找到了答案。 mysql2 公开一个 Types 字段,该字段将列类型名称作为键,将列 ID 作为值。

在发布时,该列表如下所示:

{
  "DECIMAL": 0,
  "TINY": 1,
  "SHORT": 2,
  "LONG": 3,
  "FLOAT": 4,
  "DOUBLE": 5,
  "NULL": 6,
  "TIMESTAMP": 7,
  "LONGLONG": 8,
  "INT24": 9,
  "DATE": 10,
  "TIME": 11,
  "DATETIME": 12,
  "YEAR": 13,
  "NEWDATE": 14,
  "VARCHAR": 15,
  "BIT": 16,
  "JSON": 245,
  "NEWDECIMAL": 246,
  "ENUM": 247,
  "SET": 248,
  "TINY_BLOB": 249,
  "MEDIUM_BLOB": 250,
  "LONG_BLOB": 251,
  "BLOB": 252,
  "VAR_STRING": 253,
  "STRING": 254,
  "GEOMETRY": 255
}