comparison autoload/dbtables.vim @ 3:3aa33ab9a907 default tip

tags
author Luka Sitas <lsitas@avatarasoftware.com>
date Fri, 02 Oct 2026 10:39:52 -0400
parents 8530ebeda72c
children
comparison
equal deleted inserted replaced
2:c19add1b66a9 3:3aa33ab9a907
9 9
10 "============================================= 10 "=============================================
11 " Utilities 11 " Utilities
12 "============================================= 12 "=============================================
13 13
14 let s:query_history = [] 14 let g:query_history = []
15 let s:snippets = [] 15 let g:snippets = []
16 16
17 17
18 18
19 " - - - - - - - - - - - - - - - - - - - - - - 19 " - - - - - - - - - - - - - - - - - - - - - -
20 " DB/shell interaction 20 " DB/shell interaction
21 " - - - - - - - - - - - - - - - - - - - - - - 21 " - - - - - - - - - - - - - - - - - - - - - -
22 22
23 " Execute shell commands 23 " Execute shell commands
24 function! s:ExecuteShellCommand(command) 24 function! dbtables#ExecuteShellCommand(command)
25 let result = system(a:command . ' 2>&1') 25 let result = system(a:command . ' 2>&1')
26 return [v:shell_error == 0, result] 26 return [v:shell_error == 0, result]
27 endfunction 27 endfunction
28 28
29 " Execute a SQL query, directly to the db 29 " Execute a SQL query, directly to the db
30 function! s:ExecuteDBQuery(query) 30 function! dbtables#ExecuteDBQuery(query)
31 let command = printf('mariadb --user=%s --password=%s --database=%s --host=%s -e %s', 31 let command = printf('mariadb --user=%s --password=%s --database=%s --host=%s -e %s',
32 \ shellescape(g:db_user), shellescape(g:db_password), 32 \ shellescape(g:db_user), shellescape(g:db_password),
33 \ shellescape(g:db_name), shellescape(g:db_host), 33 \ shellescape(g:db_name), shellescape(g:db_host),
34 \ shellescape(a:query)) 34 \ shellescape(a:query))
35 35
36 let [success, result] = s:ExecuteShellCommand(command) 36 let [success, result] = dbtables#ExecuteShellCommand(command)
37 if success 37 if success
38 return split(result, "\n") 38 return split(result, "\n")
39 else 39 else
40 call s:HandleDBError(result, command) 40 call dbtables#HandleDBError(result, command)
41 return [] 41 return []
42 endif 42 endif
43 endfunction 43 endfunction
44 44
45 " Function to execute SQL from the current buffer with parameter prompt 45 " Function to execute SQL from the current buffer with parameter prompt
46 function! dbtables#ExecuteSQLQuery() 46 function! dbtables#ExecuteSQLQuery()
47 let query = join(getline(1, '$'), "\n") 47 let query = join(getline(1, '$'), "\n")
48 call s:PromptAndExecuteQuery(query) 48 call dbtables#PromptAndExecuteQuery(query)
49 endfunction 49 endfunction
50 50
51 " Function to execute SQL from a visual selection with parameter prompt 51 " Function to execute SQL from a visual selection with parameter prompt
52 function! dbtables#ExecuteVisualSQLQuery() range 52 function! dbtables#ExecuteVisualSQLQuery() range
53 " Get the content of the selected lines as a single SQL query 53 " Get the content of the selected lines as a single SQL query
54 let lines = getline(a:firstline, a:lastline) 54 let lines = getline(a:firstline, a:lastline)
55 let query = join(lines, "\n") 55 let query = join(lines, "\n")
56 56
57 " Execute query with prompt for parameters 57 " Execute query with prompt for parameters
58 call s:PromptAndExecuteQuery(query) 58 call dbtables#PromptAndExecuteQuery(query)
59 endfunction 59 endfunction
60 60
61 " Handle parameter prompts and execute query 61 " Handle parameter prompts and execute query
62 function! s:PromptAndExecuteQuery(query) 62 function! dbtables#PromptAndExecuteQuery(query)
63 " Identify parameters in the form of :parameter_name 63 " Identify parameters in the form of :parameter_name
64 let pattern = ':\(\k\+\)' 64 let pattern = ':\(\k\+\)'
65 let params = [] 65 let params = []
66 66
67 " Find all matches in the query 67 " Find all matches in the query
87 let updated_query = a:query 87 let updated_query = a:query
88 for [param, value] in items(param_values) 88 for [param, value] in items(param_values)
89 let updated_query = substitute(updated_query, param, value, 'g') 89 let updated_query = substitute(updated_query, param, value, 'g')
90 endfor 90 endfor
91 91
92 call add(s:query_history, updated_query) 92 call add(g:query_history, updated_query)
93 call s:OpenInNewTab('SQLQueryResult', s:ExecuteDBQuery(updated_query)) 93 call dbtables#OpenInNewTab('SQLQueryResult', dbtables#ExecuteDBQuery(updated_query))
94 endfunction 94 endfunction
95 95
96 " Handle database command errors 96 " Handle database command errors
97 function! s:HandleDBError(result, command) 97 function! dbtables#HandleDBError(result, command)
98 echoerr 'Shell command failed: ' . a:command 98 echoerr 'Shell command failed: ' . a:command
99 if a:result =~# 'Access denied' 99 if a:result =~# 'Access denied'
100 echoerr 'Authentication error: Check your username and password.' 100 echoerr 'Authentication error: Check your username and password.'
101 elseif a:result =~# 'Unknown database' 101 elseif a:result =~# 'Unknown database'
102 echoerr 'Database error: The specified database does not exist.' 102 echoerr 'Database error: The specified database does not exist.'
114 " - - - - - - - - - - - - - - - - - - - - - - 114 " - - - - - - - - - - - - - - - - - - - - - -
115 " Visuals 115 " Visuals
116 " - - - - - - - - - - - - - - - - - - - - - - 116 " - - - - - - - - - - - - - - - - - - - - - -
117 117
118 " Display data in a new read only buffer 118 " Display data in a new read only buffer
119 function! s:OpenInNewTab(title, data) 119 function! dbtables#OpenInNewTab(title, data)
120 tabnew 120 tabnew
121 enew 121 enew
122 execute 'file ' . a:title 122 execute 'file ' . a:title
123 if !empty(a:data) 123 if !empty(a:data)
124 call append(0, a:data) 124 call append(0, a:data)
140 140
141 function! dbtables#OpenDBTablesWindow() 141 function! dbtables#OpenDBTablesWindow()
142 execute '34 vsplit' 142 execute '34 vsplit'
143 enew 143 enew
144 file DBTables 144 file DBTables
145 let tables = s:ExecuteDBQuery("SHOW TABLES;") 145 let tables = dbtables#ExecuteDBQuery("SHOW TABLES;")
146 if empty(tables) 146 if empty(tables)
147 echo "No tables found or an error occurred." 147 echo "No tables found or an error occurred."
148 else 148 else
149 call append(0, ['Available Tables:'] + tables[1:]) 149 call append(0, ['Available Tabledbtables#'] + tables[1:])
150 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile 150 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
151 \ nomodifiable nonumber norelativenumber winfixwidth 151 \ nomodifiable nonumber norelativenumber winfixwidth
152 nnoremap <buffer> <CR> :call <SID>ShowTablePopup()<CR> 152 nnoremap <buffer> <CR> :call dbtables#ShowTablePopup()<CR>
153 endif 153 endif
154 endfunction 154 endfunction
155 155
156 " Open the database schema 156 " Open the database schema
157 function! s:OpenTableSchema(table) 157 function! dbtables#OpenTableSchema(table)
158 call s:OpenInNewTab(a:table . '_schema', s:ExecuteDBQuery('DESCRIBE ' . a:table . ';')) 158 call dbtables#OpenInNewTab(a:table . '_schema', dbtables#ExecuteDBQuery('DESCRIBE ' . a:table . ';'))
159 endfunction 159 endfunction
160 160
161 " Open the table data 161 " Open the table data
162 function! s:OpenTableData(table) 162 function! dbtables#OpenTableData(table)
163 call s:OpenInNewTab(a:table, s:ExecuteDBQuery('SELECT * FROM ' . a:table . ';')) 163 call dbtables#OpenInNewTab(a:table, dbtables#ExecuteDBQuery('SELECT * FROM ' . a:table . ';'))
164 endfunction 164 endfunction
165 165
166 let s:popup_table = "" 166 let s:popup_table = ""
167 167
168 " Popup selection for table actions 168 " Popup selection for table actions
169 function! s:ShowTablePopup() 169 function! dbtables#ShowTablePopup()
170 " Use Vim popup_menu if available 170 " Use Vim popup_menu if available
171 if exists('*popup_menu') 171 if exists('*popup_menu')
172 let s:popup_table = getline(line('.')) 172 let s:popup_table = getline(line('.'))
173 let items = ['View Data', 'View Schema', 'Cancel'] 173 let items = ['View Data', 'View Schema', 'Cancel']
174 call popup_menu(items, #{ 174 call popup_menu(items, #{
175 \ callback: '<SID>HandlePopupSelection', 175 \ callback: 'dbtables#HandlePopupSelection',
176 \ }) 176 \ })
177 return 177 return
178 endif 178 endif
179 " Popup_menu not available 179 " Popup_menu not available
180 echoerr 'Floating popup requires Vim8.2+.' 180 echoerr 'Floating popup requires Vim8.2+.'
181 call s:ShowTableMenuModal() 181 call dbtables#ShowTableMenuModal()
182 endfunction 182 endfunction
183 183
184 function! s:HandlePopupSelection(id, result) 184 function! dbtables#HandlePopupSelection(id, result)
185 let table = s:popup_table 185 let table = s:popup_table
186 call s:ClosePopup() 186 call dbtables#ClosePopup()
187 if a:result == 1 187 if a:result == 1
188 call s:OpenTableData(table) 188 call dbtables#OpenTableData(table)
189 elseif a:result == 2 189 elseif a:result == 2
190 call s:OpenTableSchema(table) 190 call dbtables#OpenTableSchema(table)
191 endif 191 endif
192 endfunction 192 endfunction
193 193
194 function! s:ClosePopup() 194 function! dbtables#ClosePopup()
195 unlet! s:popup_table 195 unlet! s:popup_table
196 endfunction 196 endfunction
197 197
198 198
199 199
206 " View query history 206 " View query history
207 function! dbtables#OpenQueryHistory() 207 function! dbtables#OpenQueryHistory()
208 " Open the query history in a new split window 208 " Open the query history in a new split window
209 execute 'vsplit' 209 execute 'vsplit'
210 enew 210 enew
211 call setline(1, s:query_history) 211 call setline(1, g:query_history)
212 nnoremap <buffer> <CR> :call s:OpenInNewTab('SQLQueryResult', 212 nnoremap <buffer> <CR> :call dbtables#OpenInNewTab('SQLQueryResult',
213 \ s:ExecuteDBQuery(getline(line('.'))))<CR> 213 \ dbtables#ExecuteDBQuery(getline(line('.'))))<CR>
214 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile wrap 214 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile wrap
215 endfunction 215 endfunction
216 216
217 217
218 218
228 " Open the query snippets in a new split window 228 " Open the query snippets in a new split window
229 execute '34 vsplit' 229 execute '34 vsplit'
230 enew 230 enew
231 231
232 " Populate the buffer with the list of snippet files 232 " Populate the buffer with the list of snippet files
233 let s:snippets = globpath(g:snippets_directory, '*', 0, 1) 233 let g:snippets = globpath(g:snippets_directory, '*', 0, 1)
234 let l:display = mapnew(s:snippets, 'fnamemodify(v:val, ":t:r")') 234 let l:display = mapnew(g:snippets, 'fnamemodify(v:val, ":t:r")')
235 call setline(1, l:display) 235 call setline(1, l:display)
236 236
237 nnoremap <buffer> <CR> :call s:OpenInNewTab(line('.'), 237 nnoremap <buffer> <CR> :execute "tabe " . fnameescape(g:snippets[line('.')-1])<CR>
238 \ fnameescape(s:snippets[a:lnum-1]))<CR>
239 238
240 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile wrap 239 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile wrap
241 endfunction 240 endfunction
242 241
243 242