codeschool / sqlite-parser

JavaScript implentation of SQLite 3 query parser
MIT License
331 stars 57 forks source link

Node Structure Change: Create Foreign Key #32

Open jdrew1303 opened 8 years ago

jdrew1303 commented 8 years ago

Input

{
  "type": "definition",
  "variant": "constraint",
  "definition": [
    {
      "type": "constraint",
      "variant": "foreign key",
      "references": {
        "type": "identifier",
        "variant": "table",
        "name": "hives"
      }
    }
  ],
  "columns": [
    {
      "type": "identifier",
      "variant": "column",
      "name": "hive_id"
    }
  ]
}

Output

FOREIGN KEY (hive_id) REFERENCES Hives

Issue

With the current structure of this node and the resulting text it's hard to build a generator that can handle this in a generic way. The generator recurses down the tree. By the time we reach the node to know that its a foreign key we have moved passed the other data necessary to build the sql. The 2 identifiers can be on a lower level but the foreign key part needs to be on a higher level.

To get an idea of the issue if you take a look at this line to see how it recourses down the tree and then this line to see how other constraint types are handled.

The main issue is the columns property. If we could pull that down into the foreign key node it would mean that I can handle this the same as the other nodes rather than writing code for edge cases if that's ok with you? Im open to other ideas on the generator if you've any (It's still a bit messy so be kind πŸ˜› )?

This is the first node that I've had problems with, there may be more where I run into this problem.

Note this is from the 1.0.0@beta AST

jdrew1303 commented 8 years ago

I've a work around for this at the moment so this can be put on the back burner πŸ˜ƒ. Checking for the columns field in the constraint allows me to switch.

image