annotate plugins/dbtables/dbtables.vim @ 3:951569ccb9c7

Updated the dbtables plugin with a popup for selecting data vs schema.
author luka
date Thu, 24 Apr 2025 20:28:09 -0400
parents
children 4a7bc1c8551b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
1 let g:db_user = "" " Replace with your default database username
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
2 let g:db_password = "" " Replace with your default database password
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
3 let g:db_name = "" " Replace with your default database name
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
4 let g:db_host = "" " Replace with your default database name
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
5
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
6
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
7 " Helper function to execute a command and handle errors
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
8 function! s:ExecuteShellCommand(command)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
9 let result = system(a:command . ' 2>&1')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
10 if v:shell_error != 0
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
11 return [v:false, result]
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
12 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
13 return [v:true, result]
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
14 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
15
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
16 function! s:ExecuteDBQuery(query)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
17 let db_user = shellescape(g:db_user)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
18 let db_password = shellescape(g:db_password)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
19 let db_name = shellescape(g:db_name)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
20 let db_host = shellescape(g:db_host)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
21
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
22 let command = 'mariadb --user=' . db_user
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
23 \ . ' --password=' . db_password
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
24 \ . ' --database=' . db_name
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
25 \ . ' --host=' . db_host
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
26 \ . ' -e ' . shellescape(a:query)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
27
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
28 let [success, result] = s:ExecuteShellCommand(command)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
29
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
30 if !success
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
31 call s:HandleDBError(result, command)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
32 return []
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
33 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
34 return split(result, "\n")
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
35 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
36
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
37 function! s:HandleDBError(result, command)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
38 echoerr 'Shell command failed: ' . a:command
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
39 echoerr a:result
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
40
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
41 if a:result =~# 'Access denied'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
42 echoerr 'Authentication error: Check your username and password.'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
43 elseif a:result =~# 'Unknown database'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
44 echoerr 'Database error: The specified database does not exist.'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
45 elseif a:result =~# 'Could not connect'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
46 echoerr 'Connection error: Verify host and network connectivity.'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
47 elseif a:result =~# 'You have an error in your SQL syntax'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
48 echoerr 'Syntax error in SQL query.'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
49 else
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
50 echoerr 'Unexpected error: ' . a:result
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
51 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
52 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
53
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
54
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
55 " Show the structure/schema of the selected table
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
56 function! s:ShowTableSchema(table)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
57 " Open a new tab for schema
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
58 tabnew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
59 enew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
60 execute 'file ' . table . '_schema'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
61 " Describe the table structure
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
62 let data = s:ExecuteDBQuery('DESCRIBE ' . table . ';')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
63 if len(data) > 0
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
64 call append(0, data)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
65 else
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
66 call append(0, 'No schema found or an error occurred.')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
67 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
68 " Set buffer options and syntax
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
69 setlocal filetype=sql buftype=nofile bufhidden=wipe nobuflisted noswapfile nomodifiable
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
70 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
71
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
72
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
73 let s:popup_table = ""
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
74 " Popup selection for table actions
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
75 function! s:ShowTablePopup()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
76 " Use Vim popup_menu if available
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
77 if exists('*popup_menu')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
78 let s:popup_table = getline(line('.'))
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
79 let items = ['View Data', 'View Schema', 'Cancel']
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
80 call popup_menu(items, #{
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
81 \ callback: '<SID>HandlePopupSelection',
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
82 \ })
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
83 return
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
84 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
85 " Popup_menu not available
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
86 echoerr 'Floating popup requires Vim8.2+.'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
87 call s:ShowTableMenuModal()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
88 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
89
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
90 function! s:HandlePopupSelection(id, result)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
91 let table = s:popup_table
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
92 call s:ClosePopup()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
93 if a:result == 1
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
94 call s:OpenTableData(table)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
95 elseif a:result == 2
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
96 call s:OpenTableSchema(table)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
97 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
98 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
99
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
100 function! s:ClosePopup()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
101 unlet! s:popup_table
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
102 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
103
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
104
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
105 " Internal: remove any existing dropdown option lines from the DBTables buffer
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
106 function! s:ClearTableOptions()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
107 let to_del = []
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
108 for lnum in range(1, line('$'))
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
109 let ln = getline(lnum)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
110 if ln =~# '^\s\+\(Data\|Schema\)$'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
111 call add(to_del, lnum)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
112 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
113 endfor
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
114 if !empty(to_del)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
115 for lnum in reverse(to_del)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
116 call deletebufline('%', lnum)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
117 endfor
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
118 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
119 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
120
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
121
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
122 " Open table data for a given table name
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
123 function! s:OpenTableData(table)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
124 tabnew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
125 enew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
126 execute 'file ' . a:table
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
127 let data = s:ExecuteDBQuery('SELECT * FROM ' . a:table . ';')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
128 if !empty(data)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
129 call append(0, data)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
130 else
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
131 call append(0, 'No data found.')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
132 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
133 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nomodifiable
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
134 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
135
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
136 " Open table schema for a given table name
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
137 function! s:OpenTableSchema(table)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
138 tabnew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
139 enew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
140 execute 'file ' . a:table . '_schema'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
141 let data = s:ExecuteDBQuery('DESCRIBE ' . a:table . ';')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
142 if !empty(data)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
143 call append(0, data)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
144 else
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
145 call append(0, 'No schema found or an error occurred.')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
146 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
147 setlocal filetype=sql buftype=nofile bufhidden=wipe nobuflisted noswapfile nomodifiable
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
148 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
149
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
150 function! s:OpenDBTablesWindow()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
151 " Open a new vertical split window
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
152 execute '34 vsplit'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
153 " Create a new buffer
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
154 enew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
155 " Set buffer name to "DBTables"
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
156 file DBTables
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
157
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
158 let tables = s:ExecuteDBQuery("SHOW TABLES;")
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
159 if empty(tables)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
160 echo "No tables found or an error occurred."
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
161 return
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
162 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
163
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
164 " Display the tables in the buffer
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
165 call append(0, 'Available Tables:')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
166 call remove(tables, 0)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
167 for table in tables
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
168 call append('$', table)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
169 endfor
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
170
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
171 " Set buffer options
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
172 setlocal buftype=nofile
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
173 setlocal bufhidden=wipe
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
174 setlocal nobuflisted
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
175 setlocal noswapfile
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
176 setlocal nomodifiable
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
177 setlocal nonumber
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
178 setlocal norelativenumber
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
179 setlocal winfixwidth
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
180
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
181 " Map <Enter> to a modal menu (data or schema)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
182 nnoremap <buffer> <CR> :call <SID>ShowTablePopup()<CR>
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
183 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
184
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
185
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
186 function! s:ShowTableData()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
187 " Get the current line (table name)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
188 let lnum = line('.')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
189 let table_name = getline(lnum)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
190
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
191 " Open a new tab and create a new buffer
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
192 tabnew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
193 enew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
194 " Set buffer name to the table name
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
195 execute 'file ' . table_name
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
196
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
197 let data = s:ExecuteDBQuery("SELECT * FROM " . table_name . ";")
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
198
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
199 " Display the table data in the buffer
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
200 if len(data) > 0
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
201 call append(0, data)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
202 else
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
203 call append(0, 'No data found.')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
204 endif
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
205
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
206 " Set buffer options
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
207 setlocal buftype=nofile
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
208 setlocal bufhidden=wipe
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
209 setlocal nobuflisted
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
210 setlocal noswapfile
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
211 setlocal nomodifiable
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
212 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
213
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
214 let s:query_history = []
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
215
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
216 function! s:ExecuteSQLQuery()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
217 " Get the content of the current buffer (SQL query)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
218 let query = join(getline(1, '$'), " ")
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
219
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
220 " Append the query to history
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
221 call add(s:query_history, query)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
222
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
223 let data = s:ExecuteDBQuery(query)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
224
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
225 tabnew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
226 enew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
227 execute 'file SQLQueryResult'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
228 call append(0, data)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
229
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
230 " Set the buffer file type to SQL for syntax highlighting
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
231 setlocal filetype=sql
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
232
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
233 setlocal buftype=nofile
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
234 setlocal bufhidden=wipe
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
235 setlocal nobuflisted
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
236 setlocal noswapfile
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
237 setlocal nomodifiable
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
238 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
239
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
240 " Execute SQL from a visual selection
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
241 function! s:ExecuteVisualSQLQuery() range
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
242 " Get the content of the selected lines as a single SQL query
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
243 let lines = getline(a:firstline, a:lastline)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
244 let query = join(lines, " ")
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
245
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
246 " Append the query to history
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
247 call add(s:query_history, query)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
248
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
249 " Execute the query and capture results
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
250 let data = s:ExecuteDBQuery(query)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
251
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
252 " Open results in a new tab
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
253 tabnew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
254 enew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
255 execute 'file SQLQueryResult'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
256 call append(0, data)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
257
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
258 " Set buffer options and highlight
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
259 setlocal filetype=sql
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
260 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nomodifiable
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
261 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
262
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
263 function! s:OpenQueryHistory()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
264 " Open the query history in a new split window
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
265 execute 'vsplit'
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
266 enew
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
267 call setline(1, s:query_history)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
268
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
269 " Allow selecting a query to be put into a new buffer to execute
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
270 nnoremap <buffer> <CR> :call <SID>ExecuteHistoryQuery(line('.'))<CR>
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
271
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
272 " Set buffer options
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
273 setlocal buftype=nofile
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
274 setlocal bufhidden=wipe
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
275 setlocal nobuflisted
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
276 setlocal noswapfile
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
277 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
278
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
279 function! s:ExecuteHistoryQuery(lnum)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
280 " Execute the selected query from history
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
281 let query = getline(a:lnum)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
282 execute 'normal! i' . query
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
283 " Optionally call the execute function directly or process
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
284 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
285
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
286
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
287 function! DBConsole()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
288 " Save the current cursor position
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
289 let save_cursor = getpos(".")
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
290
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
291 "Format the files
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
292 let db_user = g:db_user
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
293 let db_password = g:db_password
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
294 let db_name = g:db_name
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
295 " let db_host = shellescape(g:db_host)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
296 let db_host = g:db_host
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
297
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
298
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
299
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
300 let command = 'mariadb --user=' . db_user . ' --password=' . db_password . ' --database=' . db_name . ' --host=' . db_host . ' '
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
301 let command = substitute(command, '\n', '', 'g')
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
302 execute ':term ' . command
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
303 call setpos(".", save_cursor)
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
304 endfunction
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
305
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
306 " Keybinding to open query history
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
307 nnoremap <Leader>qh :call <SID>OpenQueryHistory()<CR>
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
308 nnoremap <Leader>db :call DBConsole()<cr>
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
309
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
310 command! DBTables call s:OpenDBTablesWindow()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
311 command! ExecuteSQL call s:ExecuteSQLQuery()
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
312
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
313 " Function to refresh the list of tables
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
314 nnoremap <Leader>rt :DBTables<CR>
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
315
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
316 " Function to execute the contents of the current buffer as an SQL query
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
317 nnoremap <Leader>eq :ExecuteSQL<CR>
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
318
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
319 " Function to open the database tables window
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
320 nnoremap <Leader>dt :DBTables<CR>
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
321
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
322 " Optional: Shortcut to open MariaDB console
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
323 nnoremap <Leader>mc :call DBConsole()<CR>
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
324 " Map visual selection to execute selected SQL
951569ccb9c7 Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff changeset
325 xnoremap <silent> <Leader>ev :call <SID>ExecuteVisualSQLQuery()<CR>