Skip to content

Cassandra Support #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ return {

## Features
* Autocomplete table names, with automatic quoting where needed. Works for all schemes that [vim-dadbod](https://github.com/tpope/vim-dadbod) supports.
* Autocomplete table columns, context aware. Also knows to read aliases (`select * from mytable tbl where tbl.id = 1`). Currently works for `PostgreSQL`, `MySQL`, `Oracle`, `SQLite` (requires version `3.37.0 (2021-11-27)`) and `SQLserver/MSSQL`.
* Autocomplete table columns, context aware. Also knows to read aliases (`select * from mytable tbl where tbl.id = 1`). Currently works for `PostgreSQL`, `MySQL`, `Cassandra`, `Oracle`, `SQLite` (requires version `3.37.0 (2021-11-27)`) and `SQLserver/MSSQL`.
* Out of the box integration with [vim-dadbod-ui](https://github.com/kristijanhusak/vim-dadbod-ui)

## How it works
Expand Down
48 changes: 31 additions & 17 deletions autoload/vim_dadbod_completion/schemas.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ let s:count_query = 'SELECT COUNT(*) AS total FROM INFORMATION_SCHEMA.COLUMNS'
let s:table_column_query = s:base_column_query.' WHERE TABLE_NAME={db_tbl_name}'
let s:reserved_words = vim_dadbod_completion#reserved_keywords#get_as_dict()
let s:quote_rules = {
\ 'camelcase': {val -> val =~# '[A-Z]' && val =~# '[a-z]'},
\ 'space': {val -> val =~# '\s'},
\ 'reserved_word': {val -> has_key(s:reserved_words, toupper(val))}
\ }
\ 'camelcase': {val -> val =~# '[A-Z]' && val =~# '[a-z]'},
\ 'space': {val -> val =~# '\s'},
\ 'reserved_word': {val -> has_key(s:reserved_words, toupper(val))}
\ }

if get(g:, 'vim_dadbod_completion_lowercase_keywords', 0) == 1
let s:quote_rules['reserved_word'] = {val -> has_key(s:reserved_words, tolower(val))}
Expand Down Expand Up @@ -43,19 +43,19 @@ function! s:count_parser(index, result) abort
endfunction

let s:postgres = {
\ 'args': ['-A', '-c'],
\ 'column_query': s:query,
\ 'count_column_query': s:count_query,
\ 'table_column_query': {table -> substitute(s:table_column_query, '{db_tbl_name}', "'".table."'", '')},
\ 'functions_query': "SELECT routine_name FROM information_schema.routines WHERE routine_type='FUNCTION'",
\ 'functions_parser': {list->list[1:-4]},
\ 'schemas_query': s:schema_query,
\ 'schemas_parser': function('s:map_and_filter', ['|']),
\ 'quote': ['"', '"'],
\ 'should_quote': function('s:should_quote', [['camelcase', 'reserved_word', 'space']]),
\ 'column_parser': function('s:map_and_filter', ['|']),
\ 'count_parser': function('s:count_parser', [1])
\ }
\ 'args': ['-A', '-c'],
\ 'column_query': s:query,
\ 'count_column_query': s:count_query,
\ 'table_column_query': {table -> substitute(s:table_column_query, '{db_tbl_name}', "'".table."'", '')},
\ 'functions_query': "SELECT routine_name FROM information_schema.routines WHERE routine_type='FUNCTION'",
\ 'functions_parser': {list->list[1:-4]},
\ 'schemas_query': s:schema_query,
\ 'schemas_parser': function('s:map_and_filter', ['|']),
\ 'quote': ['"', '"'],
\ 'should_quote': function('s:should_quote', [['camelcase', 'reserved_word', 'space']]),
\ 'column_parser': function('s:map_and_filter', ['|']),
\ 'count_parser': function('s:count_parser', [1])
\ }

let s:oracle_args = "echo \"SET linesize 4000;\nSET pagesize 4000;\n%s\" | "
let s:oracle_base_column_query = printf(s:oracle_args, "COLUMN column_name FORMAT a50;\nCOLUMN table_name FORMAT a50;\nSELECT C.table_name, C.column_name FROM all_tab_columns C JOIN all_users U ON C.owner = U.username WHERE U.common = 'NO' %s;")
Expand All @@ -72,6 +72,19 @@ let s:oracle = {
\ 'table_column_query': {table -> printf(s:oracle_base_column_query, "AND C.table_name='".table."'")},
\ }

let s:cassandra = {
\ 'column_parser': function('s:map_and_filter', ['|']),
\ 'column_query': "SELECT table_name, column_name FROM system_schema.columns;",
\ 'count_column_query': "SELECT COUNT(*) FROM system_schema.columns;",
\ 'count_parser': function('s:count_parser', [1]),
\ 'table_column_query': {table -> substitute("SELECT table_name, column_name FROM system_schema.columns WHERE table_name='{db_tbl_name}';", '{db_tbl_name}', "'".table."'", '')},
\ 'schemas_query': "SELECT keyspace_name, table_name FROM system_schema.columns GROUP BY keyspace_name,table_name;",
\ 'schemas_parser': function('s:map_and_filter', ['|']),
\ 'requires_stdin': v:true,
\ 'quote': ['"', '"'],
\ 'should_quote': function('s:should_quote', [['reserved_word', 'space']]),
\ }

let s:mysql = {
\ 'column_query': s:query,
\ 'count_column_query': s:count_query,
Expand All @@ -91,6 +104,7 @@ let s:schemas = {
\ 'mysql': s:mysql,
\ 'mariadb': s:mysql,
\ 'oracle': s:oracle,
\ 'cassandra': s:cassandra,
\ 'sqlite': {
\ 'args': ['-list'],
\ 'column_query': "SELECT m.name AS table_name, ii.name AS column_name FROM sqlite_schema AS m, pragma_table_list(m.name) AS il, pragma_table_info(il.name) AS ii WHERE m.type='table' ORDER BY column_name ASC;",
Expand Down
2 changes: 1 addition & 1 deletion doc/vim-dadbod-completion.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Configuration using lazy.nvim (https://github.com/folke/lazy.nvim) with vim-dadb
FEATURES *vim-dadbod-completion-features*

* Autocomplete table names, with automatic quoting where needed. Works for all schemes that vim-dadbod (https://github.com/tpope/vim-dadbod) supports.
* Autocomplete table columns, context aware. Also knows to read aliases (). Currently works for `PostgreSQL`, `MySQL`, `Oracle`, `SQLite` (requires version `3.37.0 (2021-11-27)`) and `SQLserver/MSSQL`.
* Autocomplete table columns, context aware. Also knows to read aliases (). Currently works for `PostgreSQL`, `MySQL`, `Cassandra`, `Oracle`, `SQLite` (requires version `3.37.0 (2021-11-27)`) and `SQLserver/MSSQL`.
* Out of the box integration with vim-dadbod-ui (https://github.com/kristijanhusak/vim-dadbod-ui)

--------------------------------------------------------------------------------
Expand Down