Mercurial > vim
annotate plugins/dbtables/dbtables.vim @ 5:4a7bc1c8551b
support for params in queries
| author | Luka Sitas <lsitas@avatarasoftware.com> |
|---|---|
| date | Mon, 27 Oct 2025 15:52:05 -0400 |
| parents | 951569ccb9c7 |
| children | 3017fd33ad8b |
| 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 |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
216 " 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
|
217 function! s:ExecuteSQLQuery() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
218 " Get the content of the current buffer (SQL query) |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
219 let query = join(getline(1, '$'), "\n") |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
220 |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
221 " Execute query with prompt for parameters |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
222 call s:PromptAndExecuteQuery(query) |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
223 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
224 |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
225 " 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
|
226 function! s:ExecuteVisualSQLQuery() range |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
227 " 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
|
228 let lines = getline(a:firstline, a:lastline) |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
229 let query = join(lines, "\n") |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
230 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
231 " Execute query with prompt for parameters |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
232 call s:PromptAndExecuteQuery(query) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
233 endfunction |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
234 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
235 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
236 " Function to prompt user for parameter values and execute query |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
237 function! s:PromptAndExecuteQuery(query) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
238 " Identify parameters in the form of :parameter_name |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
239 let pattern = ':\(\k\+\)' |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
240 let params = [] |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
241 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
242 " Find all matches in the query |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
243 let start = 0 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
244 while match(a:query, pattern, start) != -1 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
245 let match = matchstr(a:query, pattern, start) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
246 if index(params, match) == -1 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
247 call add(params, match) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
248 endif |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
249 let start = matchend(a:query, pattern, start) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
250 endwhile |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
251 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
252 " Initialize a dictionary for parameters and their values |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
253 let param_values = {} |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
254 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
255 " Prompt user for each parameter |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
256 for param in params |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
257 let value = input('Enter value for ' . param . ': ') |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
258 let param_values[param] = value |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
259 endfor |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
260 |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
261 " Replace parameters in the query with user-provided values |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
262 let updated_query = a:query |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
263 for [param, value] in items(param_values) |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
264 let updated_query = substitute(updated_query, param, shellescape(value), 'g') |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
265 endfor |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
266 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
267 " Append the query to history |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
268 call add(s:query_history, updated_query) |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
269 |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
270 " Execute the updated query |
|
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
271 let data = s:ExecuteDBQuery(updated_query) |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
272 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
273 " Open results in a new tab |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
274 tabnew |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
275 enew |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
276 execute 'file SQLQueryResult' |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
277 call append(0, data) |
|
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 " Set buffer options and highlight |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
280 setlocal filetype=sql |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
281 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
|
282 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
283 |
|
5
4a7bc1c8551b
support for params in queries
Luka Sitas <lsitas@avatarasoftware.com>
parents:
3
diff
changeset
|
284 |
|
3
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
285 function! s:OpenQueryHistory() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
286 " 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
|
287 execute 'vsplit' |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
288 enew |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
289 call setline(1, s:query_history) |
|
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 " 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
|
292 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
|
293 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
294 " Set buffer options |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
295 setlocal buftype=nofile |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
296 setlocal bufhidden=wipe |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
297 setlocal nobuflisted |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
298 setlocal noswapfile |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
299 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
300 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
301 function! s:ExecuteHistoryQuery(lnum) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
302 " Execute the selected query from history |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
303 let query = getline(a:lnum) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
304 execute 'normal! i' . query |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
305 " 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
|
306 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
307 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
308 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
309 function! DBConsole() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
310 " Save the current cursor position |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
311 let save_cursor = getpos(".") |
|
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 "Format the files |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
314 let db_user = g:db_user |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
315 let db_password = g:db_password |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
316 let db_name = g:db_name |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
317 " let db_host = shellescape(g:db_host) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
318 let db_host = g:db_host |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
319 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
320 |
|
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 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
|
323 let command = substitute(command, '\n', '', 'g') |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
324 execute ':term ' . command |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
325 call setpos(".", save_cursor) |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
326 endfunction |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
327 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
328 " Keybinding to open query history |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
329 nnoremap <Leader>qh :call <SID>OpenQueryHistory()<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
330 nnoremap <Leader>db :call DBConsole()<cr> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
331 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
332 command! DBTables call s:OpenDBTablesWindow() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
333 command! ExecuteSQL call s:ExecuteSQLQuery() |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
334 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
335 " Function to refresh the list of tables |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
336 nnoremap <Leader>rt :DBTables<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
337 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
338 " 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
|
339 nnoremap <Leader>eq :ExecuteSQL<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
340 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
341 " Function to open the database tables window |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
342 nnoremap <Leader>dt :DBTables<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
343 |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
344 " Optional: Shortcut to open MariaDB console |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
345 nnoremap <Leader>mc :call DBConsole()<CR> |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
346 " Map visual selection to execute selected SQL |
|
951569ccb9c7
Updated the dbtables plugin with a popup for selecting data vs schema.
luka
parents:
diff
changeset
|
347 xnoremap <silent> <Leader>ev :call <SID>ExecuteVisualSQLQuery()<CR> |
