Mercurial > vim
annotate plugins/dbtables/dbtables.vim @ 6:3017fd33ad8b
Better support for db tables.
Adding some simple diffing support for hg.
| author | Luka Sitas <lsitas@avatarasoftware.com> |
|---|---|
| date | Tue, 28 Oct 2025 15:26:12 -0400 |
| parents | 4a7bc1c8551b |
| children |
| rev | line source |
|---|---|
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
1 let g:db_user = "" |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
2 let g:db_password = "" |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
3 let g:db_name = "" |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
4 let g:db_host = "" |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
5 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
6 " Execute shell commands |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
7 function! s:ExecuteShellCommand(command) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
8 let result = system(a:command . ' 2>&1') |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
9 return [v:shell_error == 0, result] |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
10 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
11 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
12 " Execute a SQL query |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
13 function! s:ExecuteDBQuery(query) |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
14 let command = printf('mariadb --user=%s --password=%s --database=%s --host=%s -e %s', |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
15 \ shellescape(g:db_user), shellescape(g:db_password), |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
16 \ shellescape(g:db_name), shellescape(g:db_host), |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
17 \ shellescape(a:query)) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
18 |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
19 let [success, result] = s:ExecuteShellCommand(command) |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
20 if success |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
21 return split(result, "\n") |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
22 else |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
23 call s:HandleDBError(result, command) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
24 return [] |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
25 endif |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
26 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
27 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
28 " Handle database command errors |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
29 function! s:HandleDBError(result, command) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
30 echoerr 'Shell command failed: ' . a:command |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
31 if a:result =~# 'Access denied' |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
32 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
|
33 elseif a:result =~# 'Unknown database' |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
34 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
|
35 elseif a:result =~# 'Could not connect' |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
36 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
|
37 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
|
38 echoerr 'Syntax error in SQL query.' |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
39 else |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
40 echoerr 'Unexpected error: ' . a:result |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
41 endif |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
42 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
43 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
44 " Display schema or data in a new buffer |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
45 function! s:OpenInNewTab(title, data) |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
46 tabnew |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
47 enew |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
48 execute 'file ' . a:title |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
49 if !empty(a:data) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
50 call append(0, a:data) |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
51 else |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
52 call append(0, 'No data found or an error occurred.') |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
53 endif |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
54 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
|
55 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
56 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
57 " Open the database schema |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
58 function! s:OpenTableSchema(table) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
59 call s:OpenInNewTab(a:table . '_schema', s:ExecuteDBQuery('DESCRIBE ' . a:table . ';')) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
60 endfunction |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
61 |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
62 " Open the table data |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
63 function! s:OpenTableData(table) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
64 call s:OpenInNewTab(a:table, s:ExecuteDBQuery('SELECT * FROM ' . a:table . ';')) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
65 endfunction |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
66 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
67 let s:popup_table = "" |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
68 " Popup selection for table actions |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
69 function! s:ShowTablePopup() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
70 " Use Vim popup_menu if available |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
71 if exists('*popup_menu') |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
72 let s:popup_table = getline(line('.')) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
73 let items = ['View Data', 'View Schema', 'Cancel'] |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
74 call popup_menu(items, #{ |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
75 \ callback: '<SID>HandlePopupSelection', |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
76 \ }) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
77 return |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
78 endif |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
79 " Popup_menu not available |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
80 echoerr 'Floating popup requires Vim8.2+.' |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
81 call s:ShowTableMenuModal() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
82 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
83 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
84 function! s:HandlePopupSelection(id, result) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
85 let table = s:popup_table |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
86 call s:ClosePopup() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
87 if a:result == 1 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
88 call s:OpenTableData(table) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
89 elseif a:result == 2 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
90 call s:OpenTableSchema(table) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
91 endif |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
92 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
93 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
94 function! s:ClosePopup() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
95 unlet! s:popup_table |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
96 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
97 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
98 " Open the database tables list |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
99 function! s:OpenDBTablesWindow() |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
100 execute '34 vsplit' |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
101 enew |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
102 file DBTables |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
103 let tables = s:ExecuteDBQuery("SHOW TABLES;") |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
104 if empty(tables) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
105 echo "No tables found or an error occurred." |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
106 else |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
107 call append(0, ['Available Tables:'] + tables[1:]) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
108 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nomodifiable nonumber norelativenumber winfixwidth |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
109 nnoremap <buffer> <CR> :call <SID>ShowTablePopup()<CR> |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
110 endif |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
111 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
112 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
113 let s:query_history = [] |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
114 |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
115 " Function to execute SQL from the current buffer with parameter prompt |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
116 function! s:ExecuteSQLQuery() |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
117 let query = join(getline(1, '$'), "\n") |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
118 call s:PromptAndExecuteQuery(query) |
|
3
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 |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
121 " Function to execute SQL from a visual selection with parameter prompt |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
122 function! s:ExecuteVisualSQLQuery() range |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
123 " 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
|
124 let lines = getline(a:firstline, a:lastline) |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
125 let query = join(lines, "\n") |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
126 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
127 " Execute query with prompt for parameters |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
128 call s:PromptAndExecuteQuery(query) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
129 endfunction |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
130 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
131 " Handle parameter prompts and execute query |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
132 function! s:PromptAndExecuteQuery(query) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
133 " Identify parameters in the form of :parameter_name |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
134 let pattern = ':\(\k\+\)' |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
135 let params = [] |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
136 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
137 " Find all matches in the query |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
138 let start = 0 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
139 while match(a:query, pattern, start) != -1 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
140 let match = matchstr(a:query, pattern, start) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
141 if index(params, match) == -1 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
142 call add(params, match) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
143 endif |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
144 let start = matchend(a:query, pattern, start) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
145 endwhile |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
146 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
147 " Initialize a dictionary for parameters and their values |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
148 let param_values = {} |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
149 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
150 " Prompt user for each parameter |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
151 for param in params |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
152 let value = input('Enter value for ' . param . ': ') |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
153 let param_values[param] = value |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
154 endfor |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
155 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
156 " Replace parameters in the query with user-provided values |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
157 let updated_query = a:query |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
158 for [param, value] in items(param_values) |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
159 let updated_query = substitute(updated_query, param, value, 'g') |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
160 endfor |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
161 |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
162 call add(s:query_history, updated_query) |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
163 call s:OpenInNewTab('SQLQueryResult', s:ExecuteDBQuery(updated_query)) |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
164 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
165 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
166 " View query history |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
167 function! s:OpenQueryHistory() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
168 " 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
|
169 execute 'vsplit' |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
170 enew |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
171 call setline(1, s:query_history) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
172 nnoremap <buffer> <CR> :call <SID>ExecuteHistoryQuery(line('.'))<CR> |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
173 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
174 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
175 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
176 function! s:ExecuteHistoryQuery(lnum) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
177 let query = getline(a:lnum) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
178 execute 'normal! i' . query |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
179 " Optional execution or further processing |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
180 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
181 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
182 " Open MariaDB console |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
183 function! DBConsole() |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
184 " Save the current cursor position |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
185 let save_cursor = getpos(".") |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
186 let command = printf('mariadb --user=%s --password=%s --database=%s --host=%s', |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
187 \ g:db_user, g:db_password, |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
188 \ g:db_name, g:db_host) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
189 |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
190 let command = substitute(command, '\n', '', 'g') |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
191 execute ':term ' . command |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
192 call setpos(".", save_cursor) |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
193 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
194 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
195 " Keybinding to open query history |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
196 nnoremap <Leader>qh :call <SID>OpenQueryHistory()<CR> |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
5
diff
changeset
|
197 nnoremap <Leader>db :call DBConsole()<CR> |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
198 command! DBTables call s:OpenDBTablesWindow() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
199 command! ExecuteSQL call s:ExecuteSQLQuery() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
200 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
201 " Function to refresh the list of tables |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
202 nnoremap <Leader>rt :DBTables<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
203 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
204 " 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
|
205 nnoremap <Leader>eq :ExecuteSQL<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
206 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
207 " Function to open the database tables window |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
208 nnoremap <Leader>dt :DBTables<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
209 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
210 " Optional: Shortcut to open MariaDB console |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
211 nnoremap <Leader>mc :call DBConsole()<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
212 " Map visual selection to execute selected SQL |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
213 xnoremap <silent> <Leader>ev :call <SID>ExecuteVisualSQLQuery()<CR> |
