1
0
mirror of https://github.com/dbcli/pgcli synced 2024-06-16 01:42:23 +00:00
Commit Graph

75 Commits

Author SHA1 Message Date
Étienne BERSAC
e124c03575
Fix PEP8 empty line errors 2017-06-14 18:12:05 +02:00
Étienne BERSAC
219b4a52d9
Fix PEP8 for #693 2017-04-27 21:32:44 +02:00
Étienne BERSAC
293b6b3a41
Complete keywords depending on previous token
Keywords list is based on
https://www.postgresql.org/docs/9.6/static/sql-commands.html.
2017-04-27 21:32:43 +02:00
Owen Stephens
3d560baf6c Suggest columns for ORDER BY and DISTINCT (fixes #685)
Having typed an alias name in an `ORDER BY` or (`SELECT`) `DISTINCT`
clause, the alias was not taken account of, and the completion simply
listed all columns. This change fixes that, and makes the autocompletion
behave the same as in `SELECT` and `WHERE` clauses.
2017-04-26 01:10:57 +01:00
koljonen
1277752d62
Find statements inside function body
Consider this script
```
CREATE FUNCTION foo() returns text LANGUAGE SQL AS $func$
SELECT 1 FROM Bar;
SELECT <cursor> FROM Baz;
$func$;
```
The change here is that `SELECT <cursor> FROM Baz;` will be seen as the
current statement, instead of the whole function definition.
This means we'll no longer get column suggestions from `Bar`.
2017-03-09 01:41:11 +01:00
Darik Gamble
f314f8301b Suggest keywords after ALTER 2017-02-25 09:01:11 -05:00
koljonen
45326335e6
Support for table-qualifying column suggestions
... i.e. suggesting foo.fooid instead of just fooid
Controlled using a config-file setting:
**qualify_columns**: always/never/**if_more_than_one_table**.

To enable the proper sorting of qualified column suggestions, we
introduce the concept of synonyms for suggestions
(in `pgcompleter.find_matches`). They way synonyms work is that a
number of synonyms may be provided for a suggestion sent to
`find_matches`. If synonyms are provided, sorting is based on how
well the best synonym matches the input, instead of only comparing
the input to the suggestion text.
In this case, the unqualified name acts as a synonym.
I have a couple of other ideas of use cases where we can use synonyms
to get better completions with less typing for the user, which I intend
to explore later.
2016-12-05 03:15:53 +01:00
Joakim Koljonen
279d6e85a7
Fix crash after with 2016-11-20 21:18:40 +01:00
Darik Gamble
0881902207 If prev_keyword is an unrecognized keyword, go backward until we find a recognized one 2016-10-17 13:48:55 -04:00
Joakim Koljonen
514b594909
Fix crash bug with leading parenthesis 2016-09-12 23:48:22 +02:00
Joakim Koljonen
8d2265a65d
Temporary hack for sqlparse crashing after AS
And a regression test.
2016-08-23 20:14:00 +02:00
Darik Gamble
8b8f257100 Rename the suggestion field tables to table_refs so we can add more table-specific properties without getting confused 2016-07-27 15:33:58 -04:00
koljonen
f09bb42d67
Better scoping for tables in insert statements
This commit makes it so that given `INSERT INTO foo(<cursor1>) SELECT <cursor2> FROM bar;`, we suggest `bar` columns for `<cursor2>` and `foo` columns for `<cursor1>`. Previous behaviour is sugggesting columns from both tables in both cases.
2016-07-06 20:03:24 +02:00
koljonen
e15b3ec0a1
Suggest table aliases + add tests for casing
If config.main.generate_aliases is True, for `SELECT * FROM `, we suggest `FooBar FB` and `foo_bar fb` instead of `FooBar` and `foo_bar`, respectively.
To be able to add a test, I had to add support for testing with different settings, which meant I could also add tests for casing.

There are two non-obvious changes that I can think of:
1. The lexical sorting of matches is modified so as to sort spaces and underscores before letters and to sort case-insensitively. This is so that e.g `Foob F` comes before 'FooBar FB' when `foob` is input.
2. We now suggest `some_func()` instead of `some_func` (because suggesting `some_func sf` didn't make any sense).
2016-06-27 21:26:59 +02:00
koljonen
17f44545d8
In test_sqlcompletion, remove some code duplication 2016-06-27 21:15:29 +02:00
koljonen
b69404c215
Add a couple of tests suggested by @darikg 2016-06-14 22:44:36 +02:00
koljonen
6cb8c38628
Join conditions: alias tables already included in query
If we have the input `SELECT * FROM Foo JOIN `, we now suggest `Foo Foo2 ON Foo2.ParentID = Foo.ID` (given the appropriate casing file and FK).
There were also some problems with quoted tables and with the casing of table aliases, which are now fixed.
I also made a few cosmetic changes to get_join_matches (pgcompleter.py) just to make it a bit easier to work with.
2016-06-14 22:34:10 +02:00
koljonen
582852adb6
Various changes after review 2016-06-10 03:26:42 +02:00
koljonen
9b6a72e9e0
For 'JOIN <cursor>', suggest 'foo on foo.fooid = bar.fooid'
This is based on my previous work on suggesting join conditions, but here instead we suggest the whole join. What we do is simply check all the tables in the statement for FK relationships and then suggest joins based on those. I think this will not only save key presses, but also be rather useful when exploring an unfamiliar (part of a) database.

There's one non-obvious change in this commit (that I can think of): When calling **sqlcompletion.text_before_cursor**, the **text_before_cursor** argument now no longer includes **word_before_cursor**. This is because for 'SELECT * FROM foo JOIN bar<cursor>', we would otherwise consider the table **bar** already included in the statement and thus suggest e.g. 'baz on baz.barid = bar.barid'.
2016-06-09 00:55:07 +02:00
Darik Gamble
e5cc38d835 column keyword suggests columns 2016-06-07 14:44:52 -04:00
koljonen
7f8bd9bbd4
Make join-condition suggestions work with multi-line queries 2016-05-24 16:11:36 +02:00
koljonen
8431b32166
Support for join-condition suggestions after ON
The user types 'SELECT * FROM parenttable p JOIN childtable c on '. We then suggest ['c.parenttableid = p.parenttableid', 'c.createdby = p.createdby', ...] as completions.
The suggestions are FK matches first, then name matches for int columns, then name matches for other columns, sorted by proximity from the cursor to the other table.

Some changes:
    For "JOIN Foo USING(<cursor>)", now only columns present in Foo are suggested.
    For all suggestions after 'ON', now only the tables before the cursor are considered.
    meta[reltype]][relname] goes from being a list of columns to being an OrderedDict {column_name:ColumnMetadata}, where the ColumnMetaData contains the column's name, data type and a list of FKs involving the column. This entails modification of a number of functions dealing with columns.
2016-05-24 14:18:19 +02:00
koljonen
eaf3a11c94
Improve filtering of functions for FROM clause
Change from filtering out all non-set-returning functions to only filteirng out windowing functions and aggregate functions.
Non-set-returning functions are legal in the FROM clause and can be quite useful.
2016-05-07 13:48:11 +02:00
Darik Gamble
0f70ccad31 Add a bunch of tests for multiple joins 2015-11-25 16:17:08 -05:00
Darik Gamble
2c4f4e689d Add more tests for qualified table names with or without quotes 2015-11-23 16:48:27 -05:00
Darik Gamble
9295cb9686 Fix suggestions after a manually entered double quote escape 2015-11-21 16:39:30 -05:00
Darik Gamble
692afc8240 Replace suggestion dicts with namedtuples 2015-11-16 07:41:27 -05:00
darikg
4055b726cc Merge pull request #368 from dbcli/darikg/suggest-columns-from-functions
Suggest columns from functions
2015-10-04 09:31:11 -04:00
Darik Gamble
9a7cae6cf5 Expand more join tests to check compound statements 2015-10-03 14:10:51 -04:00
Darik Gamble
ee0995ceed Fix suggestions in compound join clauses
Previously, this worked correctly, suggesting columns etc. from `a`
`SELECT * FROM abc a JOIN def d ON a.`

But this suggested only keywords:
`SELECT * FROM abc a JOIN def d ON a.id = d.id AND a.`
2015-10-03 14:04:00 -04:00
Darik Gamble
62b2962b63 Suggest fields from functions used as tables 2015-09-29 09:07:20 -04:00
Darik Gamble
a0266de192 Extract functions in the FROM clause as tables 2015-09-29 08:54:49 -04:00
Darik Gamble
169eb3ff58 Suggest set-returning functions as tables 2015-09-23 14:09:38 -04:00
Amjith Ramanujam
d222e085b7 Update the tests for named query. 2015-08-25 19:41:04 -07:00
Iryna Cherniavska
31b867f632 Autocompletion in named queries. Connect #270. 2015-08-25 19:35:40 -07:00
Amjith Ramanujam
fb86930cd6 Add tests for where clause with keywords. 2015-08-24 19:31:25 -07:00
Amjith Ramanujam
65398f0051 Fix failing tests. 2015-08-23 01:43:54 -07:00
Amjith Ramanujam
ad9c99f790 Merge pull request #323 from dbcli/darikg/bugfix-283
Fix #283
2015-08-05 15:43:05 -07:00
Darik Gamble
3ef99aa043 Test suggest_type with escaped table aliases 2015-08-05 15:46:13 -04:00
Darik Gamble
e97cc6586d Add regression test for #317 2015-08-05 09:40:31 -04:00
Amjith Ramanujam
09ccb535ec Add a test for the crashing bug fix. 2015-08-01 08:29:33 -07:00
Darik Gamble
a169f01609 Suggest datatypes from a hardcoded whitelist 2015-05-23 08:33:39 -04:00
Darik Gamble
70732f0025 drop schema and create schema should suggest schemas 2015-05-20 17:59:32 -04:00
Darik Gamble
e060dd398e improve suggestions in non-trivial WHERE clauses
sqlparse groups tokens belonging to the where clause into a single Where token (a subclass of TokenList). In order to handle cases beyond the simplest `SELECT * FROM foo WHERE`, we need to look "inside" of this token list.
2015-05-02 17:33:25 -04:00
Daniel Rocco
7fe802a33f Add suggest tests for INSERT INTO, COPY, UPDATE, DESCRIBE, JOIN, and TRUNCATE 2015-04-18 17:34:59 -04:00
Amjith Ramanujam
049c51b971 Merge pull request #190 from darikg/joins
some join-related improvements
2015-04-16 22:30:38 -07:00
Daniel Rocco
e85116d846 Handle a ',' entered before any completions gracefully
Fixes #197
2015-04-11 09:55:34 -04:00
Darik Gamble
90974ecff8 support 'JOIN ... USING (' by suggesting columns present in more than one table 2015-04-07 15:43:29 -04:00
Darik Gamble
070b138cf3 bugfix: suggestions were broken after specifying a join type
because sqlparse parses things like 'inner join' as a single token, simply checking if token value
matches 'join' fails on further specified join types
2015-04-06 05:52:38 -04:00
Darik Gamble
05dac16040 suggest view names 2015-04-05 16:38:04 -04:00