Support autocompletion with table aliases #7
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "patch-1"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Table names are only searched for between FROM and WHERE (if present) and only on the same level as the alias usage, so false positives should not appear.
It might not be implemented in the most performant way since I'm not familiar with token traversal in CM 6, but I hope it's not too bad. I only tested small queries, which are fine.
Could you add some tests so I can see what kind of situations this is targeting?
Oh, unfortunately I don't know how to write tests for this. What it should do is provide completions for an aliased schema:
Some basic tests:
Same goes for a schema like
{ "db.table": [ "column" ] }and when parts or all identifiers are quoted in SQL.What's currently not included in this PR is autocompleting aliases themselves, since it would be much more involved and would have to be executed on any completion, even emtpy ones. This would be cool, but need much more testing and maybe a larger rewrite of existing functions for better (and more performant) integration.
See
test/test-complete.tsfor examples of autocompletion tests.I did already, but still don't fully understand the syntax.
Is
|the cursor position and what's the difference between the last arguments ("email, id"/'"email", "id"') ingithub.com/codemirror/lang-sql@905c81a436/test/test-complete.ts (L71)and
github.com/codemirror/lang-sql@905c81a436/test/test-complete.ts (L76)?
Yes.
One has quotes in the completion options, one doesn't.
Thanks. My bad, I just misread something when looking at the test file and only noticed right after posting my comment.
Added some tests but don't know how to write tests for false positives, but maybe they are not that important?
Also I did not include tests for completing quoted columns since the alias handling only affects schema and table names.
Names with periods in them will be tokenized as separate tokens for the identifiers and the periods, so this doesn't look like it'll work.
We're targeting browsers that don't have
endsWith—could you change this to/Identifier$/.test?Are names with periods in them valid?
If so can you recommend a nice way to split or tokenize this? I would like to avoid an ugly regex with lookaheads because those are hard to read and maintain.
Sure
I noticed that a
CompositeIdentifieris already tokenized (as children of the node), so there's no need to split. Pushed new code where I addressed this issue and also refactored the code a bit.I ended up going with a different approach (see
ca17998) that is a bit less ad-hoc (yours had a few potential null-dereferences and didn't work with some query formats) and allows completing the names of aliases. Does that patch work for you?Works great for me, thank you.
Now that it autocompletes aliases you could make it less strict to also include names of aliases for unknown entities (like a subselect).
Maybe this idea sounds also interesting to you, otherwise sorry for going off-topic here:
github.com/codemirror/lang-sql@ca17998ec7/src/complete.ts (L64)Additionally search prevSiblings for a
USEstatement to set/override the default schema. It's nothing I need but looks like a low hanging fruit and an intuitive feature.Tagged as 6.3.0. I don't currently have time to add more features to this.
Pull request closed