@vim-geek/coc-db 0.0.0-stage → 0.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +123 -2
- package/after/plugin/vim_dadbod_completion.lua +9 -0
- package/after/plugin/vim_dadbod_completion.vim +3 -0
- package/autoload/vim_dadbod_completion/alias_parser.vim +34 -0
- package/autoload/vim_dadbod_completion/compe.vim +41 -0
- package/autoload/vim_dadbod_completion/job.vim +86 -0
- package/autoload/vim_dadbod_completion/reserved_keywords.vim +164 -0
- package/autoload/vim_dadbod_completion/schemas.vim +160 -0
- package/autoload/vim_dadbod_completion/utils.vim +9 -0
- package/autoload/vim_dadbod_completion.vim +449 -0
- package/denops/@ddc-sources/dadbod-completion.ts +34 -0
- package/doc/vim-dadbod-completion.txt +167 -0
- package/lua/vim_dadbod_completion/blink.lua +88 -0
- package/lua/vim_dadbod_completion.lua +71 -0
- package/package.json +69 -4
- package/plugin/vim_dadbod_completion.vim +12 -0
- package/rplugin/python3/deoplete/source/vim_dadbod_completion.py +23 -0
- package/src/index.js +44 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
let s:base_column_query = 'SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS'
|
|
2
|
+
let s:query = s:base_column_query.' ORDER BY COLUMN_NAME ASC'
|
|
3
|
+
let s:schema_query = 'SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS GROUP BY TABLE_SCHEMA,TABLE_NAME'
|
|
4
|
+
let s:count_query = 'SELECT COUNT(*) AS total FROM INFORMATION_SCHEMA.COLUMNS'
|
|
5
|
+
let s:table_column_query = s:base_column_query.' WHERE TABLE_NAME={db_tbl_name}'
|
|
6
|
+
let s:reserved_words = vim_dadbod_completion#reserved_keywords#get_as_dict()
|
|
7
|
+
let s:quote_rules = {
|
|
8
|
+
\ 'camelcase': {val -> val =~# '[A-Z]' && val =~# '[a-z]'},
|
|
9
|
+
\ 'space': {val -> val =~# '\s'},
|
|
10
|
+
\ 'reserved_word': {val -> has_key(s:reserved_words, toupper(val))}
|
|
11
|
+
\ }
|
|
12
|
+
|
|
13
|
+
if get(g:, 'vim_dadbod_completion_lowercase_keywords', 0) == 1
|
|
14
|
+
let s:quote_rules['reserved_word'] = {val -> has_key(s:reserved_words, tolower(val))}
|
|
15
|
+
endif
|
|
16
|
+
|
|
17
|
+
function! s:map_and_filter(delimiter, list) abort
|
|
18
|
+
return filter(
|
|
19
|
+
\ map(a:list, { _, table -> map(split(table, a:delimiter), 'trim(v:val)') }),
|
|
20
|
+
\ 'len(v:val) ==? 2'
|
|
21
|
+
\ )
|
|
22
|
+
endfunction
|
|
23
|
+
|
|
24
|
+
function! s:should_quote(rules, val) abort
|
|
25
|
+
if empty(trim(a:val))
|
|
26
|
+
return 0
|
|
27
|
+
endif
|
|
28
|
+
|
|
29
|
+
let do_quote = 0
|
|
30
|
+
|
|
31
|
+
for rule in a:rules
|
|
32
|
+
let do_quote = s:quote_rules[rule](a:val)
|
|
33
|
+
if do_quote
|
|
34
|
+
break
|
|
35
|
+
endif
|
|
36
|
+
endfor
|
|
37
|
+
|
|
38
|
+
return do_quote
|
|
39
|
+
endfunction
|
|
40
|
+
|
|
41
|
+
function! s:count_parser(index, result) abort
|
|
42
|
+
return str2nr(get(a:result, a:index, 0))
|
|
43
|
+
endfunction
|
|
44
|
+
|
|
45
|
+
let s:postgres = {
|
|
46
|
+
\ 'args': ['-A', '-c'],
|
|
47
|
+
\ 'column_query': s:query,
|
|
48
|
+
\ 'count_column_query': s:count_query,
|
|
49
|
+
\ 'table_column_query': {table -> substitute(s:table_column_query, '{db_tbl_name}', "'".table."'", '')},
|
|
50
|
+
\ 'functions_query': "SELECT DISTINCT(routine_name) FROM information_schema.routines WHERE routine_type='FUNCTION'",
|
|
51
|
+
\ 'functions_parser': {list->list[1:-4]},
|
|
52
|
+
\ 'schemas_query': s:schema_query,
|
|
53
|
+
\ 'schemas_parser': function('s:map_and_filter', ['|']),
|
|
54
|
+
\ 'quote': ['"', '"'],
|
|
55
|
+
\ 'should_quote': function('s:should_quote', [['camelcase', 'reserved_word', 'space']]),
|
|
56
|
+
\ 'column_parser': function('s:map_and_filter', ['|']),
|
|
57
|
+
\ 'count_parser': function('s:count_parser', [1])
|
|
58
|
+
\ }
|
|
59
|
+
|
|
60
|
+
let s:oracle_args = "echo \"SET linesize 4000;\nSET pagesize 4000;\n%s\" | "
|
|
61
|
+
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;")
|
|
62
|
+
let s:oracle = {
|
|
63
|
+
\ 'column_parser': function('s:map_and_filter', ['\s\s\+']),
|
|
64
|
+
\ 'column_query': printf(s:oracle_base_column_query, 'ORDER BY C.column_name ASC'),
|
|
65
|
+
\ 'count_column_query': printf(s:oracle_args, "COLUMN total FORMAT 9999999;\nSELECT COUNT(*) AS total FROM all_tab_columns C JOIN all_users U ON C.owner = U.username WHERE U.common = 'NO';"),
|
|
66
|
+
\ 'count_parser': function('s:count_parser', [1]),
|
|
67
|
+
\ 'quote': ['"', '"'],
|
|
68
|
+
\ 'requires_stdin': v:true,
|
|
69
|
+
\ 'schemas_query': printf(s:oracle_args, "COLUMN owner FORMAT a20;\nCOLUMN table_name FORMAT a25;\nSELECT T.owner, T.table_name FROM all_tables T JOIN all_users U ON T.owner = U.username WHERE U.common = 'NO' ORDER BY T.table_name;"),
|
|
70
|
+
\ 'schemas_parser': function('s:map_and_filter', ['\s\s\+']),
|
|
71
|
+
\ 'should_quote': function('s:should_quote', [['camelcase', 'reserved_word', 'space']]),
|
|
72
|
+
\ 'table_column_query': {table -> printf(s:oracle_base_column_query, "AND C.table_name='".table."'")},
|
|
73
|
+
\ }
|
|
74
|
+
|
|
75
|
+
let s:mysql = {
|
|
76
|
+
\ 'column_query': s:query,
|
|
77
|
+
\ 'count_column_query': s:count_query,
|
|
78
|
+
\ 'table_column_query': {table -> substitute(s:table_column_query, '{db_tbl_name}', "'".table."'", '')},
|
|
79
|
+
\ 'schemas_query': s:schema_query,
|
|
80
|
+
\ 'schemas_parser': function('s:map_and_filter', ['\t']),
|
|
81
|
+
\ 'requires_stdin': v:true,
|
|
82
|
+
\ 'quote': ['`', '`'],
|
|
83
|
+
\ 'should_quote': function('s:should_quote', [['reserved_word', 'space']]),
|
|
84
|
+
\ 'column_parser': function('s:map_and_filter', ['\t']),
|
|
85
|
+
\ 'count_parser': function('s:count_parser', [1])
|
|
86
|
+
\ }
|
|
87
|
+
|
|
88
|
+
let s:clickhouse_base_column_query = "SELECT table AS TABLE_NAME, name AS COLUMN_NAME FROM system.columns"
|
|
89
|
+
let s:clickhouse_query = s:clickhouse_base_column_query . " ORDER BY COLUMN_NAME ASC"
|
|
90
|
+
let s:clickhouse_count_query = "SELECT count() AS total FROM system.columns"
|
|
91
|
+
let s:clickhouse_schema_query = "SELECT DISTINCT database AS TABLE_SCHEMA, table AS TABLE_NAME FROM system.columns ORDER BY TABLE_SCHEMA, TABLE_NAME"
|
|
92
|
+
let s:clickhouse_table_column_query = s:clickhouse_base_column_query . " WHERE table = {db_tbl_name}"
|
|
93
|
+
|
|
94
|
+
let s:clickhouse = {
|
|
95
|
+
\ 'args': ['--query'],
|
|
96
|
+
\ 'column_query': s:clickhouse_query,
|
|
97
|
+
\ 'count_column_query': s:clickhouse_count_query,
|
|
98
|
+
\ 'table_column_query': {table -> substitute(s:clickhouse_table_column_query, '{db_tbl_name}', "'".table."'", '')},
|
|
99
|
+
\ 'schemas_query': s:clickhouse_schema_query,
|
|
100
|
+
\ 'schemas_parser': function('s:map_and_filter', ['\t']),
|
|
101
|
+
\ 'quote': ['`', '`'],
|
|
102
|
+
\ 'should_quote': function('s:should_quote', [['reserved_word', 'space']]),
|
|
103
|
+
\ 'column_parser': function('s:map_and_filter', ['\t']),
|
|
104
|
+
\ 'count_parser': function('s:count_parser', [0]),
|
|
105
|
+
\ }
|
|
106
|
+
|
|
107
|
+
let s:schemas = {
|
|
108
|
+
\ 'postgres': s:postgres,
|
|
109
|
+
\ 'postgresql': s:postgres,
|
|
110
|
+
\ 'mysql': s:mysql,
|
|
111
|
+
\ 'mariadb': s:mysql,
|
|
112
|
+
\ 'oracle': s:oracle,
|
|
113
|
+
\ 'sqlite': {
|
|
114
|
+
\ 'args': ['-list'],
|
|
115
|
+
\ '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;",
|
|
116
|
+
\ 'count_column_query': "SELECT count(*) AS total FROM sqlite_schema AS m, pragma_table_list(m.name) AS il, pragma_table_info(il.name) AS ii WHERE m.type='table';",
|
|
117
|
+
\ 'table_column_query': {table -> substitute("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' AND table_name={db_tbl_name};", '{db_tbl_name}', "'".table."'", '')},
|
|
118
|
+
\ 'quote': ['"', '"'],
|
|
119
|
+
\ 'should_quote': function('s:should_quote', [['reserved_word', 'space']]),
|
|
120
|
+
\ 'column_parser': function('s:map_and_filter', ['|']),
|
|
121
|
+
\ 'count_parser': function('s:count_parser', [1]),
|
|
122
|
+
\ },
|
|
123
|
+
\ 'sqlserver': {
|
|
124
|
+
\ 'args': ['-h-1', '-W', '-s', '|', '-Q'],
|
|
125
|
+
\ 'column_query': s:query,
|
|
126
|
+
\ 'count_column_query': s:count_query,
|
|
127
|
+
\ 'table_column_query': {table -> substitute(s:table_column_query, '{db_tbl_name}', "'".table."'", '')},
|
|
128
|
+
\ 'schemas_query': s:schema_query,
|
|
129
|
+
\ 'schemas_parser': function('s:map_and_filter', ['|']),
|
|
130
|
+
\ 'quote': ['[', ']'],
|
|
131
|
+
\ 'should_quote': function('s:should_quote', [['reserved_word', 'space']]),
|
|
132
|
+
\ 'column_parser': function('s:map_and_filter', ['|']),
|
|
133
|
+
\ 'count_parser': function('s:count_parser', [0])
|
|
134
|
+
\ },
|
|
135
|
+
\ 'clickhouse': s:clickhouse,
|
|
136
|
+
\ }
|
|
137
|
+
|
|
138
|
+
function! vim_dadbod_completion#schemas#get(scheme)
|
|
139
|
+
return get(s:schemas, a:scheme, {})
|
|
140
|
+
endfunction
|
|
141
|
+
|
|
142
|
+
function! vim_dadbod_completion#schemas#get_quotes_rgx() abort
|
|
143
|
+
let open = []
|
|
144
|
+
let close = []
|
|
145
|
+
for db in values(s:schemas)
|
|
146
|
+
if index(open, db.quote[0]) <= -1
|
|
147
|
+
call add(open, db.quote[0])
|
|
148
|
+
endif
|
|
149
|
+
|
|
150
|
+
if index(close, db.quote[1]) <= -1
|
|
151
|
+
call add(close, db.quote[1])
|
|
152
|
+
endif
|
|
153
|
+
endfor
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
\ 'open': escape(join(open, '\|'), '[]'),
|
|
157
|
+
\ 'close': escape(join(close, '\|'), '[]')
|
|
158
|
+
\ }
|
|
159
|
+
endfunction
|
|
160
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
let s:disable_notifications = get(g:, 'vim_dadbod_completion_disable_notifications', get(g:, 'db_ui_disable_info_notifications', 0))
|
|
2
|
+
|
|
3
|
+
function! vim_dadbod_completion#utils#msg(msg) abort
|
|
4
|
+
if s:disable_notifications
|
|
5
|
+
return
|
|
6
|
+
endif
|
|
7
|
+
redraw!
|
|
8
|
+
echom printf('[dadbod completion] %s', a:msg)
|
|
9
|
+
endfunction
|
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
let s:cache = {}
|
|
2
|
+
let s:buffers = {}
|
|
3
|
+
let s:filetypes = ['sql', 'mysql', 'plsql']
|
|
4
|
+
|
|
5
|
+
let s:quotes = vim_dadbod_completion#schemas#get_quotes_rgx()
|
|
6
|
+
let s:trigger_rgx = printf('\(%s\|\.\)$', s:quotes.open)
|
|
7
|
+
let s:findstart_rgx = printf('\(^\|\s\+\|\.\|(\|%s\)\@<=\w\+\(%s\)\?$', s:quotes.open, s:quotes.close)
|
|
8
|
+
let s:table_scope_rgx = printf('\(%s\)\?\(\w\+\)\(%s\)\?\.\(%s\)\?\w*\(%s\)\?$', s:quotes.open, s:quotes.close, s:quotes.open, s:quotes.close)
|
|
9
|
+
let s:filter_rgx = printf('^\(%s\)\?', s:quotes.open)
|
|
10
|
+
let s:mark = get(g:, 'vim_dadbod_completion_mark', '[DB]')
|
|
11
|
+
let s:default_limit = 200
|
|
12
|
+
let s:limits = get(g:, 'vim_dadbod_completion_source_limits', {})
|
|
13
|
+
let s:mapped_reserved_words = map(copy(vim_dadbod_completion#reserved_keywords#get()), {i,word ->
|
|
14
|
+
\ {'word': word, 'abbr': word, 'menu': s:mark, 'info': 'SQL reserved word', 'kind': 'R' }
|
|
15
|
+
\ })
|
|
16
|
+
|
|
17
|
+
function! vim_dadbod_completion#omni(findstart, base)
|
|
18
|
+
let line = getline('.')[0:col('.') - 2]
|
|
19
|
+
let current_char = getline('.')[col('.') - 2]
|
|
20
|
+
if a:findstart
|
|
21
|
+
let trigger_char = match(line, s:trigger_rgx)
|
|
22
|
+
if trigger_char > -1
|
|
23
|
+
return trigger_char + 1
|
|
24
|
+
endif
|
|
25
|
+
return match(line, s:findstart_rgx)
|
|
26
|
+
endif
|
|
27
|
+
|
|
28
|
+
let is_trigger_char = current_char =~? s:trigger_rgx
|
|
29
|
+
let bufnr = bufnr('%')
|
|
30
|
+
|
|
31
|
+
if empty(a:base) && !is_trigger_char
|
|
32
|
+
return []
|
|
33
|
+
endif
|
|
34
|
+
|
|
35
|
+
if !has_key(s:buffers, bufnr)
|
|
36
|
+
call vim_dadbod_completion#fetch(bufnr(''))
|
|
37
|
+
endif
|
|
38
|
+
|
|
39
|
+
" Nothing found, returning empty
|
|
40
|
+
if !has_key(s:buffers, bufnr)
|
|
41
|
+
return []
|
|
42
|
+
endif
|
|
43
|
+
|
|
44
|
+
let buf = s:buffers[bufnr]
|
|
45
|
+
let s:buffers[bufnr].aliases = vim_dadbod_completion#alias_parser#parse(bufnr, s:cache[buf.db].tables_list)
|
|
46
|
+
|
|
47
|
+
let table_scope_match = get(matchlist(line, s:table_scope_rgx), 2, '')
|
|
48
|
+
let table_scope = s:get_scope(buf, table_scope_match, 'table')
|
|
49
|
+
let buffer_table_scope = s:get_scope(buf, buf.table, 'table')
|
|
50
|
+
let schema_scope = s:get_scope(buf, table_scope_match, 'schema')
|
|
51
|
+
|
|
52
|
+
let cache_db = s:cache[buf.db]
|
|
53
|
+
|
|
54
|
+
let tables = []
|
|
55
|
+
let schemas = []
|
|
56
|
+
let aliases = []
|
|
57
|
+
let columns = []
|
|
58
|
+
let reserved_words = []
|
|
59
|
+
let bind_params = []
|
|
60
|
+
let functions = []
|
|
61
|
+
let should_filter = !(empty(a:base) && is_trigger_char)
|
|
62
|
+
let bind_params_match = match(line, '[[:blank:]]*:\w*$') > -1
|
|
63
|
+
|
|
64
|
+
if bind_params_match && exists('b:dbui_bind_params')
|
|
65
|
+
for [param_name, param_val] in items(b:dbui_bind_params)
|
|
66
|
+
call add(bind_params, {
|
|
67
|
+
\ 'word': param_name[1:],
|
|
68
|
+
\ 'abbr': param_name,
|
|
69
|
+
\ 'menu': s:mark,
|
|
70
|
+
\ 'info': param_val
|
|
71
|
+
\ })
|
|
72
|
+
endfor
|
|
73
|
+
endif
|
|
74
|
+
|
|
75
|
+
if empty(table_scope) && empty(schema_scope)
|
|
76
|
+
let tables = copy(cache_db.tables)
|
|
77
|
+
if should_filter
|
|
78
|
+
call filter(tables, 'v:val.abbr =~? s:filter_rgx.a:base')
|
|
79
|
+
endif
|
|
80
|
+
let tables = tables[0:s:limit('tables')]
|
|
81
|
+
|
|
82
|
+
let schemas = keys(cache_db.schemas)
|
|
83
|
+
if should_filter
|
|
84
|
+
call filter(schemas, 'v:val =~? s:filter_rgx.a:base')
|
|
85
|
+
endif
|
|
86
|
+
let schemas = schemas[0:s:limit('schemas')]
|
|
87
|
+
call map(schemas, function('s:map_item', ['string', 'schema', 'S']))
|
|
88
|
+
|
|
89
|
+
for [tbl, alias] in items(s:buffers[bufnr].aliases)
|
|
90
|
+
for a in alias
|
|
91
|
+
call add(aliases, [tbl, a])
|
|
92
|
+
endfor
|
|
93
|
+
endfor
|
|
94
|
+
|
|
95
|
+
if should_filter
|
|
96
|
+
call filter(aliases, 'v:val[1] =~? s:filter_rgx.a:base')
|
|
97
|
+
endif
|
|
98
|
+
call map(aliases, function('s:map_item', ['list', 'alias for table %s', 'A']))
|
|
99
|
+
|
|
100
|
+
if !is_trigger_char
|
|
101
|
+
let reserved_words = copy(s:mapped_reserved_words)
|
|
102
|
+
if !empty(a:base)
|
|
103
|
+
call filter(reserved_words, 'v:val.abbr =~? ''^''.a:base')
|
|
104
|
+
endif
|
|
105
|
+
let reserved_words = reserved_words[0:s:limit('reserved_words')]
|
|
106
|
+
endif
|
|
107
|
+
|
|
108
|
+
let functions = copy(cache_db.functions)
|
|
109
|
+
if !empty(a:base) && !is_trigger_char
|
|
110
|
+
call filter(functions, 'v:val.abbr =~? ''^''.a:base')
|
|
111
|
+
endif
|
|
112
|
+
|
|
113
|
+
let functions = functions[0:s:limit('functions')]
|
|
114
|
+
endif
|
|
115
|
+
|
|
116
|
+
if !empty(schema_scope)
|
|
117
|
+
let tables = copy(cache_db.schemas[schema_scope])
|
|
118
|
+
if should_filter
|
|
119
|
+
call filter(tables, 'v:val.abbr =~? s:filter_rgx.a:base')
|
|
120
|
+
endif
|
|
121
|
+
let tables = tables[0:s:limit('tables')]
|
|
122
|
+
endif
|
|
123
|
+
|
|
124
|
+
if !empty(table_scope)
|
|
125
|
+
let columns = s:get_table_scope_columns(buf.db, table_scope)
|
|
126
|
+
elseif !empty(buffer_table_scope)
|
|
127
|
+
let columns = s:get_table_scope_columns(buf.db, buffer_table_scope)
|
|
128
|
+
elseif !cache_db.fetch_columns_by_table && empty(schema_scope)
|
|
129
|
+
let columns = copy(cache_db.columns)
|
|
130
|
+
endif
|
|
131
|
+
|
|
132
|
+
if should_filter
|
|
133
|
+
call filter(columns, 'v:val.abbr =~? s:filter_rgx.a:base')
|
|
134
|
+
endif
|
|
135
|
+
|
|
136
|
+
let columns = columns[0:s:limit('columns')]
|
|
137
|
+
|
|
138
|
+
return s:quote_results(bind_params + reserved_words + schemas + tables + aliases + columns + functions)
|
|
139
|
+
endfunction
|
|
140
|
+
|
|
141
|
+
function! s:map_item(type, info_val, kind, index, item) abort
|
|
142
|
+
let word = a:type ==? 'string' ? a:item : a:item[1]
|
|
143
|
+
let info = a:type ==? 'string' ? a:info_val : printf(a:info_val, a:item[0])
|
|
144
|
+
return {
|
|
145
|
+
\ 'word': word,
|
|
146
|
+
\ 'abbr': word,
|
|
147
|
+
\ 'menu': s:mark,
|
|
148
|
+
\ 'kind': a:kind,
|
|
149
|
+
\ 'info': info,
|
|
150
|
+
\ }
|
|
151
|
+
endfunction
|
|
152
|
+
|
|
153
|
+
function vim_dadbod_completion#clear_cache() abort
|
|
154
|
+
let s:cache = {}
|
|
155
|
+
call vim_dadbod_completion#utils#msg('Cache cleared')
|
|
156
|
+
if index(s:filetypes, &filetype) > -1
|
|
157
|
+
call vim_dadbod_completion#utils#msg('Reloading completion for current buffer...')
|
|
158
|
+
call vim_dadbod_completion#fetch(bufnr(''))
|
|
159
|
+
call vim_dadbod_completion#utils#msg('Reloading completion for current buffer...Done.')
|
|
160
|
+
endif
|
|
161
|
+
endfunction
|
|
162
|
+
|
|
163
|
+
function! vim_dadbod_completion#fetch(bufnr) abort
|
|
164
|
+
if !has('nvim') && !exists('*job_start')
|
|
165
|
+
let s:buffers[a:bufnr] = {}
|
|
166
|
+
return
|
|
167
|
+
endif
|
|
168
|
+
if !exists('g:db_adapter_postgres')
|
|
169
|
+
let g:db_adapter_postgres = 'db#adapter#postgresql#'
|
|
170
|
+
endif
|
|
171
|
+
|
|
172
|
+
if !exists('g:db_adapter_sqlite3')
|
|
173
|
+
let g:db_adapter_sqlite3 = 'db#adapter#sqlite#'
|
|
174
|
+
endif
|
|
175
|
+
|
|
176
|
+
if index(s:filetypes, getbufvar(a:bufnr, '&filetype')) < 0 && empty(getbufvar(a:bufnr, 'dbui_db_key_name'))
|
|
177
|
+
return
|
|
178
|
+
endif
|
|
179
|
+
let db_info = s:get_buffer_db_info(a:bufnr)
|
|
180
|
+
|
|
181
|
+
return s:save_to_cache(a:bufnr, db_info.url, db_info.table, db_info.dbui)
|
|
182
|
+
endfunction
|
|
183
|
+
|
|
184
|
+
function! s:save_to_cache(bufnr, db, table, dbui) abort
|
|
185
|
+
if empty(a:db)
|
|
186
|
+
return
|
|
187
|
+
endif
|
|
188
|
+
|
|
189
|
+
let tables = []
|
|
190
|
+
if !has_key(s:buffers, a:bufnr)
|
|
191
|
+
let s:buffers[a:bufnr] = {}
|
|
192
|
+
endif
|
|
193
|
+
|
|
194
|
+
if !has_key(s:buffers[a:bufnr], 'aliases')
|
|
195
|
+
let s:buffers[a:bufnr].aliases = {}
|
|
196
|
+
endif
|
|
197
|
+
|
|
198
|
+
if !empty(a:dbui)
|
|
199
|
+
let s:buffers[a:bufnr].scheme = a:dbui.scheme
|
|
200
|
+
if a:dbui.connected
|
|
201
|
+
let tables = copy(a:dbui.tables)
|
|
202
|
+
endif
|
|
203
|
+
else
|
|
204
|
+
let parsed = db#url#parse(a:db)
|
|
205
|
+
let s:buffers[a:bufnr].scheme = parsed.scheme
|
|
206
|
+
endif
|
|
207
|
+
|
|
208
|
+
let s:buffers[a:bufnr].table = a:table
|
|
209
|
+
let s:buffers[a:bufnr].db = a:db
|
|
210
|
+
let s:buffers[a:bufnr].dbui = a:dbui
|
|
211
|
+
|
|
212
|
+
if has_key(s:cache, a:db)
|
|
213
|
+
return
|
|
214
|
+
endif
|
|
215
|
+
|
|
216
|
+
let s:cache[a:db] = {
|
|
217
|
+
\ 'tables': tables,
|
|
218
|
+
\ 'tables_list': copy(tables),
|
|
219
|
+
\ 'schemas': {},
|
|
220
|
+
\ 'columns': [],
|
|
221
|
+
\ 'functions': [],
|
|
222
|
+
\ 'columns_by_table': {},
|
|
223
|
+
\ 'fetch_columns_by_table': 1,
|
|
224
|
+
\ 'scheme': {}
|
|
225
|
+
\ }
|
|
226
|
+
|
|
227
|
+
if empty(s:cache[a:db].tables)
|
|
228
|
+
let tables = db#adapter#call(a:db, 'tables', [a:db], [])
|
|
229
|
+
let s:cache[a:db].tables = uniq(tables)
|
|
230
|
+
let s:cache[a:db].tables_list = copy(s:cache[a:db].tables)
|
|
231
|
+
call map(s:cache[a:db].tables, function('s:map_item', ['string', 'table', 'T']))
|
|
232
|
+
elseif type(s:cache[a:db].tables[0]) ==? type('')
|
|
233
|
+
call map(s:cache[a:db].tables, function('s:map_item', ['string', 'table', 'T']))
|
|
234
|
+
endif
|
|
235
|
+
|
|
236
|
+
let scheme = vim_dadbod_completion#schemas#get(s:buffers[a:bufnr].scheme)
|
|
237
|
+
let s:cache[a:db].scheme = scheme
|
|
238
|
+
if !empty(scheme)
|
|
239
|
+
let ccq = s:generate_query(a:db, 'count_column_query')
|
|
240
|
+
call vim_dadbod_completion#job#run(ccq[0], function('s:count_columns_and_cache', [a:db]), ccq[1])
|
|
241
|
+
if has_key(scheme, 'functions_query')
|
|
242
|
+
let fnq = s:generate_query(a:db, 'functions_query')
|
|
243
|
+
call vim_dadbod_completion#job#run(fnq[0], function('s:parse_functions', [a:db]), fnq[1])
|
|
244
|
+
endif
|
|
245
|
+
if has_key(scheme, 'schemas_query')
|
|
246
|
+
let scmq = s:generate_query(a:db, 'schemas_query')
|
|
247
|
+
call vim_dadbod_completion#job#run(scmq[0], function('s:parse_schemas', [a:db]), scmq[1])
|
|
248
|
+
endif
|
|
249
|
+
endif
|
|
250
|
+
endfunction
|
|
251
|
+
|
|
252
|
+
function! s:parse_functions(db, functions) abort
|
|
253
|
+
let s:cache[a:db].functions = s:cache[a:db].scheme.functions_parser(a:functions)
|
|
254
|
+
call map(s:cache[a:db].functions, {i,fn -> {'word': fn, 'abbr': fn, 'menu': s:mark, 'info': 'Function', 'kind': 'F' }})
|
|
255
|
+
endfunction
|
|
256
|
+
|
|
257
|
+
function! s:parse_schemas(db, schemas) abort
|
|
258
|
+
let data = s:cache[a:db].scheme.schemas_parser(a:schemas)
|
|
259
|
+
for schema in data
|
|
260
|
+
if !has_key(s:cache[a:db].schemas, schema[0])
|
|
261
|
+
let s:cache[a:db].schemas[schema[0]] = []
|
|
262
|
+
endif
|
|
263
|
+
call add(s:cache[a:db].schemas[schema[0]], schema[1])
|
|
264
|
+
endfor
|
|
265
|
+
for k in keys(s:cache[a:db].schemas)
|
|
266
|
+
call map(s:cache[a:db].schemas[k], function('s:map_item', ['string', 'table', 'T']))
|
|
267
|
+
endfor
|
|
268
|
+
endfunction
|
|
269
|
+
|
|
270
|
+
function! s:generate_query(db, query_key, ...) abort
|
|
271
|
+
let scheme = s:cache[a:db].scheme
|
|
272
|
+
let base_query = db#adapter#dispatch(a:db, 'interactive') + get(scheme, 'args', [])
|
|
273
|
+
let Query = scheme[a:query_key]
|
|
274
|
+
if a:0 > 0
|
|
275
|
+
let Query = Query(a:1)
|
|
276
|
+
endif
|
|
277
|
+
if get(scheme, 'requires_stdin')
|
|
278
|
+
return [base_query, Query]
|
|
279
|
+
endif
|
|
280
|
+
return [base_query + [Query], '']
|
|
281
|
+
endfunction
|
|
282
|
+
|
|
283
|
+
function! s:count_columns_and_cache(db, count) abort
|
|
284
|
+
let column_count = s:cache[a:db].scheme.count_parser(a:count)
|
|
285
|
+
if column_count <= 10000
|
|
286
|
+
let [query, stdin] = s:generate_query(a:db, 'column_query')
|
|
287
|
+
call vim_dadbod_completion#job#run(query, function('s:cache_all_columns', [a:db]), stdin)
|
|
288
|
+
endif
|
|
289
|
+
endfunction
|
|
290
|
+
|
|
291
|
+
function! s:cache_all_columns(db, result) abort
|
|
292
|
+
let columns = call(s:cache[a:db].scheme.column_parser, [a:result])
|
|
293
|
+
let s:cache[a:db].columns = map(copy(columns), function('s:map_item', ['list', '%s table column', 'C']))
|
|
294
|
+
call map(copy(columns), function('s:map_columns_by_table', [a:db]))
|
|
295
|
+
let s:cache[a:db].fetch_columns_by_table = 0
|
|
296
|
+
endfunction
|
|
297
|
+
|
|
298
|
+
function! s:map_columns_by_table(db, index, column) abort
|
|
299
|
+
if !has_key(s:cache[a:db].columns_by_table, a:column[0])
|
|
300
|
+
let s:cache[a:db].columns_by_table[a:column[0]] = []
|
|
301
|
+
endif
|
|
302
|
+
call add(s:cache[a:db].columns_by_table[a:column[0]], s:map_item('list', '%s table column', 'C', 0, a:column))
|
|
303
|
+
return a:column
|
|
304
|
+
endfunction
|
|
305
|
+
|
|
306
|
+
function! s:get_table_scope_columns(db, table_scope) abort
|
|
307
|
+
if has_key(s:cache[a:db].columns_by_table, a:table_scope)
|
|
308
|
+
return copy(s:cache[a:db].columns_by_table[a:table_scope])
|
|
309
|
+
endif
|
|
310
|
+
|
|
311
|
+
if empty(s:cache[a:db].scheme)
|
|
312
|
+
return []
|
|
313
|
+
endif
|
|
314
|
+
|
|
315
|
+
let g:vim_dadbod_completion_refresh_deoplete = 1
|
|
316
|
+
|
|
317
|
+
call vim_dadbod_completion#utils#msg(printf('Fetching columns for table %s...', a:table_scope))
|
|
318
|
+
let [query, stdin] = s:generate_query(a:db, 'table_column_query', a:table_scope)
|
|
319
|
+
call vim_dadbod_completion#job#run(query, function('s:cache_table_columns', [a:db, a:table_scope]), stdin)
|
|
320
|
+
return []
|
|
321
|
+
endfunction
|
|
322
|
+
|
|
323
|
+
function! s:cache_table_columns(db, table_scope, result)
|
|
324
|
+
let s:cache[a:db].columns_by_table[a:table_scope] = []
|
|
325
|
+
let columns = call(s:cache[a:db].scheme.column_parser, [a:result])
|
|
326
|
+
call map(columns, function('s:map_columns_by_table', [a:db]))
|
|
327
|
+
if exists('*coc#refresh')
|
|
328
|
+
call coc#start()
|
|
329
|
+
elseif exists('g:loaded_deoplete')
|
|
330
|
+
let g:vim_dadbod_completion_refresh_deoplete = 0
|
|
331
|
+
elseif exists('g:loaded_compe')
|
|
332
|
+
call compe#complete()
|
|
333
|
+
elseif exists('g:loaded_completion') && exists('*completion#completion_wrapper')
|
|
334
|
+
call completion#completion_wrapper()
|
|
335
|
+
elseif &omnifunc ==? 'vim_dadbod_completion#omni'
|
|
336
|
+
call feedkeys("\<C-x>\<C-o>")
|
|
337
|
+
endif
|
|
338
|
+
call vim_dadbod_completion#utils#msg(printf('Fetching columns for table %s...Done.', a:table_scope))
|
|
339
|
+
endfunction
|
|
340
|
+
|
|
341
|
+
function! s:get_scope(buffer, table_scope, type) abort
|
|
342
|
+
let cache_db = s:cache[a:buffer.db]
|
|
343
|
+
if empty(a:table_scope)
|
|
344
|
+
return ''
|
|
345
|
+
endif
|
|
346
|
+
|
|
347
|
+
if a:type ==? 'schema'
|
|
348
|
+
if has_key(cache_db.schemas, a:table_scope)
|
|
349
|
+
return a:table_scope
|
|
350
|
+
endif
|
|
351
|
+
return ''
|
|
352
|
+
endif
|
|
353
|
+
|
|
354
|
+
if has_key(cache_db.columns_by_table, a:table_scope)
|
|
355
|
+
return a:table_scope
|
|
356
|
+
endif
|
|
357
|
+
|
|
358
|
+
let is_valid = index(cache_db.tables_list, a:table_scope) > -1
|
|
359
|
+
|
|
360
|
+
if is_valid
|
|
361
|
+
return a:table_scope
|
|
362
|
+
endif
|
|
363
|
+
|
|
364
|
+
let alias = filter(copy(a:buffer.aliases), 'index(v:val, a:table_scope) > -1')
|
|
365
|
+
|
|
366
|
+
if empty(alias)
|
|
367
|
+
return ''
|
|
368
|
+
endif
|
|
369
|
+
|
|
370
|
+
let alias_table = keys(alias)[0]
|
|
371
|
+
|
|
372
|
+
if index(cache_db.tables_list, alias_table) > -1
|
|
373
|
+
return alias_table
|
|
374
|
+
endif
|
|
375
|
+
|
|
376
|
+
return ''
|
|
377
|
+
endfunction
|
|
378
|
+
|
|
379
|
+
function! s:get_buffer_db_info(bufnr) abort
|
|
380
|
+
let dbui_db_key_name = getbufvar(a:bufnr, 'dbui_db_key_name')
|
|
381
|
+
let dbui_table_name = getbufvar(a:bufnr, 'dbui_table_name')
|
|
382
|
+
|
|
383
|
+
if !empty(dbui_db_key_name)
|
|
384
|
+
let dbui = db_ui#get_conn_info(dbui_db_key_name)
|
|
385
|
+
let conn = dbui.conn
|
|
386
|
+
if empty(conn)
|
|
387
|
+
let conn = db#connect(dbui.url)
|
|
388
|
+
endif
|
|
389
|
+
return {
|
|
390
|
+
\ 'url': conn,
|
|
391
|
+
\ 'table': dbui_table_name,
|
|
392
|
+
\ 'dbui': dbui,
|
|
393
|
+
\ }
|
|
394
|
+
endif
|
|
395
|
+
|
|
396
|
+
let db = $DATABASE_URL
|
|
397
|
+
for dict in [w:, t:, b:, g:]
|
|
398
|
+
if has_key(dict, 'db') && !empty(dict.db)
|
|
399
|
+
let db = dict.db
|
|
400
|
+
break
|
|
401
|
+
endif
|
|
402
|
+
endfor
|
|
403
|
+
if !empty(db)
|
|
404
|
+
call vim_dadbod_completion#utils#msg('Connecting to db...')
|
|
405
|
+
let db = db#connect(db#resolve(db))
|
|
406
|
+
call vim_dadbod_completion#utils#msg('Connecting to db...Done.')
|
|
407
|
+
endif
|
|
408
|
+
let db_table = getbufvar(a:bufnr, 'db_table')
|
|
409
|
+
return {
|
|
410
|
+
\ 'url': db,
|
|
411
|
+
\ 'table': db_table,
|
|
412
|
+
\ 'dbui': {},
|
|
413
|
+
\ }
|
|
414
|
+
endfunction
|
|
415
|
+
|
|
416
|
+
function! s:quote(scheme, left_wrap, right_wrap, index, item) abort
|
|
417
|
+
if a:item.kind !=? 'F' && a:item.kind !=? 'R' && a:scheme.should_quote(a:item.abbr)
|
|
418
|
+
let a:item.word = a:left_wrap.a:item.abbr.a:right_wrap
|
|
419
|
+
endif
|
|
420
|
+
return a:item
|
|
421
|
+
endfunction
|
|
422
|
+
|
|
423
|
+
function! s:quote_results(results) abort
|
|
424
|
+
if !has_key(s:buffers, bufnr('%'))
|
|
425
|
+
return a:results
|
|
426
|
+
endif
|
|
427
|
+
let scheme = vim_dadbod_completion#schemas#get(s:buffers[bufnr('%')].scheme)
|
|
428
|
+
|
|
429
|
+
if empty(scheme)
|
|
430
|
+
return a:results
|
|
431
|
+
endif
|
|
432
|
+
|
|
433
|
+
let line = getline('.')
|
|
434
|
+
let [l_quote, r_quote] = scheme.quote
|
|
435
|
+
let l_quote_esc = escape(l_quote, '[')
|
|
436
|
+
let r_quote_esc = escape(r_quote, ']')
|
|
437
|
+
let left_wrap = match(line, l_quote_esc.'\w*\%'.col('.').'c') > -1 ? '' : l_quote
|
|
438
|
+
let right_wrap = matchstr(line, '\%>'.(col('.') - 1).'c['.r_quote_esc.' \.]') !=? r_quote ? r_quote : ''
|
|
439
|
+
|
|
440
|
+
return map(a:results, function('s:quote', [scheme, left_wrap, right_wrap]))
|
|
441
|
+
endfunction
|
|
442
|
+
|
|
443
|
+
function! s:limit(type) abort
|
|
444
|
+
return get(s:limits, a:type, s:default_limit)
|
|
445
|
+
endfunction
|
|
446
|
+
|
|
447
|
+
function! vim_dadbod_completion#refresh_deoplete() abort
|
|
448
|
+
return get(g:, 'vim_dadbod_completion_refresh_deoplete', 0)
|
|
449
|
+
endfunction
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { BaseSource, Item } from "https://deno.land/x/ddc_vim@v5.0.1/types.ts";
|
|
2
|
+
import {
|
|
3
|
+
GatherArguments,
|
|
4
|
+
GetCompletePositionArguments,
|
|
5
|
+
} from "https://deno.land/x/ddc_vim@v5.0.1/base/source.ts";
|
|
6
|
+
|
|
7
|
+
type Params = Record<never, never>;
|
|
8
|
+
|
|
9
|
+
export class Source extends BaseSource<Params> {
|
|
10
|
+
override async gather(
|
|
11
|
+
args: GatherArguments<Params>,
|
|
12
|
+
): Promise<Item[]> {
|
|
13
|
+
const items = await args.denops.call(
|
|
14
|
+
"vim_dadbod_completion#omni",
|
|
15
|
+
0,
|
|
16
|
+
args.completeStr,
|
|
17
|
+
) as Item[];
|
|
18
|
+
return items;
|
|
19
|
+
}
|
|
20
|
+
override async getCompletePosition(
|
|
21
|
+
args: GetCompletePositionArguments<Params>,
|
|
22
|
+
): Promise<number> {
|
|
23
|
+
const pos = await args.denops.call(
|
|
24
|
+
"vim_dadbod_completion#omni",
|
|
25
|
+
1,
|
|
26
|
+
"",
|
|
27
|
+
) as number;
|
|
28
|
+
return pos;
|
|
29
|
+
}
|
|
30
|
+
override params(): Params {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|