kristijanhusak / vim-dadbod-ui

Simple UI for https://github.com/tpope/vim-dadbod
MIT License
1.45k stars 86 forks source link

Question: How to view columns when expanding table rather than table helpers? #162

Open jtw023 opened 1 year ago

jtw023 commented 1 year ago

I've looked through past issues and haven't found an answer. Apologies if I've missed it and this is a duplicate of another question.

I'm using MacOS Ventura, Kitty terminal emulator, and am connecting to an Amazon Redshift database using psql. My dadbod_ui config is at the bottom and I'm also attaching a screenshot right below this question for clarity. Do you know how I might show columns when expanding a table in the drawer rather than showing table helpers? Perhaps I can show the resulting columns from the query on the right of the screenshot?

Screenshot 2023-03-13 at 11 11 00 PM

This is my $HOME/.config/nvim/lua/plug-dadbod.lua:

-- Add to line 33 in:
-- $HOME/.config/nvim/autoload/plugged/vim-dadbod-ui/autoload/db_ui/schemas.vim
--
-- let s:postgres_tables_and_views = "
--       \ SELECT table_schema, table_name FROM information_schema.tables ;"
-- " let s:postgres_tables_and_views = "
-- "       \ SELECT table_schema, table_name FROM information_schema.tables UNION ALL
-- "       \ select schemaname, matviewname from pg_matviews;"

-- vvv     Potential solution to show columns instead of above query    vvv
-- let s:postgres_tables_and_views = "
--       \ SELECT t.table_schema, t.table_name, c.column_name
--       \ FROM information_schema.tables AS t
--       \ INNER JOIN information_schema.columns AS c
--       \ ON t.table_name = c.table_name ;"

-- Add to line 14 in:
-- $HOME/.config/nvim/autoload/plugged/vim-dadbod-ui/autoload/db_ui/table_helpers.vim
--
--  \ 'Columns': "SELECT column_name from information_schema.columns WHERE table_name='{table}' AND table_schema='{schema}'",
-- "\ 'Columns': "select * from information_schema.columns where table_name='{table}' and table_schema='{schema}'",

vim.g.db_ui_use_nerd_fonts = 1
vim.g.db_ui_execute_on_save = 0
vim.g.db_ui_save_location = 'full_path_to_save_location'
vim.g.completion_matching_strategy_list = {'exact', 'substring'}
vim.g.completion_matching_ignore_case = 1
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

kristijanhusak commented 1 year ago

There's no support for that at this moment. I think there was discussion somewhere around that but I'm not sure where it is.

jtw023 commented 9 months ago

It looks like #36 and #112 were the two previous discussions on this. Really wish I knew enough to do this myself. Maybe one day!

kristijanhusak commented 9 months ago

Would you expect something to happen when you select the column? If yes, what? I can see how this information can be useful, but I'm just how it could be used once we have it, beside seeing the list.

jtw023 commented 9 months ago

Would you expect something to happen when you select the column? If yes, what? I can see how this information can be useful, but I'm just how it could be used once we have it, beside seeing the list.

The current implementation shows me a set of query helpers after clicking enter on a table name.

My first idea was to open the dropdown of a table and immediately see the output of the query below just after the table name. Maybe keeping the table helpers as they are either above or below or allowing a flag to be set determining which version you'd like to see(above, below, or no table helpers).

My second idea was to keep the current implementation but then add in functionality to use some keybind to show the output of the below query in a temporary window, similar to the one being created to show the query execution time, essentially making this an autoexecuted table helper that returns the output into a new window.

Query:

SELECT
    column_name,
    CASE
        WHEN udt_name = 'numeric'
            THEN udt_name || numeric_precision
        ELSE COALESCE(udt_name || character_maximum_length, udt_name)
    END AS data_type
FROM information_schema.columns
WHERE 1 = 1
    AND table_name = '{table}'
    AND table_schema = '{schema}'
LIMIT 1600 -- this is Amazon Redshift's limit for columns

I would be happy to implement this myself and then present it as a proof of concept if you can give guidance on where to add this query so that I can make it show up where the table helpers show. Maybe I'll figure out how to offer some other options as well. The one caveat is that I use Redshift and would only be able to support that and postgres.

Also, the query to get my tables showing is slightly different because I use Redshift. Regardless of what's decided on this issue, I have a simple project in mind one of these days to fork the repo, create a redshift flag, put this query behind that flag, and then submit a pull request. Thought that would be a fun little challenge to help me learn a bit.

1) Screenshot 2023-11-30 at 6 08 02 PM

kristijanhusak commented 9 months ago

My first idea was to open the dropdown of a table and immediately see the output of the query below just after the table name. Maybe keeping the table helpers as they are either above or below or allowing a flag to be set determining which version you'd like to see(above, below, or no table helpers).

So you mean to have table name toggleable like it is now, but with column info?

My second idea was to keep the current implementation but then add in functionality to use some keybind to show the output of the below query in a temporary window, similar to the one being created to show the query execution time, essentially making this an autoexecuted table helper that returns the output into a new window.

This would be tricky because results are shown with vim-dadbod. You would need to do query behind the scenes and show the results. I'd rather not take this path.

I'm ok having a toggleable list that shows the column in the sidebar, just like we show the table helpers now. I'm just curious what should happen when you try to select any of the columns? Nothing?

jtw023 commented 9 months ago

So you mean to have table name toggleable like it is now, but with column info?

Yes, that's exactly it. It might even be a bit cleaner if there was sub-toggleable section that you have to open in order to see the columns. So, as an example, we have the current implementation(schemas -> schema name -> table name -> table helpers) and but with this implemented there could be an extra toggleable state called "columns" or "show columns" with the table helpers(schemas -> schema name -> table name -> table helpers and then "columns" above or below the table helpers). When you toggle open the "columns" section you then see the output of that query.

I'm just curious what should happen when you try to select any of the columns? Nothing?

I don't think I would use anything other than the column name and the data type, maybe how many rows are in the table? That doesn't mean nothing else is useful, I just haven't needed anything else so far. You could also have the column names toggleable and put the data type there so that when clicking on "columns" the user would only see the columns.