taozhi8833998 / node-sql-parser

Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL
https://taozhi8833998.github.io/node-sql-parser/
Apache License 2.0
798 stars 180 forks source link

[Typescript]: AST type inconsistencies with `Function` #1942

Closed tbantle22 closed 3 months ago

tbantle22 commented 3 months ago

node-sql-parser v5.0.0+

The query SELECT * FROM `test1` WHERE NOT (`id` = "4" AND `id2` = "2") parses to:

{
      "with": null,
      "type": "select",
      "options": null,
      "distinct": null,
      "columns": [
        {
          "expr": {
            "type": "column_ref",
            "table": null,
            "column": "*"
          },
          "as": null
        }
      ],
      "into": {
        "position": null
      },
      "from": [
        {
          "db": null,
          "table": "test1",
          "as": null
        }
      ],
      "where": {
        "type": "function",
        "name": {
          "name": [
            {
              "type": "default",
              "value": "NOT"
            }
          ]
        },
        "args": {
          "type": "expr_list",
          "value": [
            {
              "type": "binary_expr",
              "operator": "AND",
              "left": {
                "type": "binary_expr",
                "operator": "=",
                "left": {
                  "type": "column_ref",
                  "table": null,
                  "column": "id"
                },
                "right": {
                  "type": "double_quote_string",
                  "value": "4"
                }
              },
              "right": {
                "type": "binary_expr",
                "operator": "=",
                "left": {
                  "type": "column_ref",
                  "table": null,
                  "column": "id2"
                },
                "right": {
                  "type": "double_quote_string",
                  "value": "2"
                }
              }
            }
          ]
        },
        "over": null
      },
      "groupby": null,
      "having": null,
      "orderby": null,
      "limit": null,
      "locking_read": null,
      "window": null
    }

But some of these structures are not accurately reflected in types.d.ts

  1. The FunctionName -> name: ValueExpr<string>[] -> ValueExpr does not include a "default" option for the type field
  2. The Function -> args.value only allows ExpressionList[], but should also allow Expr as seen above