Mercurial > vim
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 4:1a705d7a7521 | 5:4a7bc1c8551b |
|---|---|
| 211 setlocal nomodifiable | 211 setlocal nomodifiable |
| 212 endfunction | 212 endfunction |
| 213 | 213 |
| 214 let s:query_history = [] | 214 let s:query_history = [] |
| 215 | 215 |
| 216 " Function to execute SQL from the current buffer with parameter prompt | |
| 216 function! s:ExecuteSQLQuery() | 217 function! s:ExecuteSQLQuery() |
| 217 " Get the content of the current buffer (SQL query) | 218 " Get the content of the current buffer (SQL query) |
| 218 let query = join(getline(1, '$'), " ") | 219 let query = join(getline(1, '$'), "\n") |
| 219 | 220 |
| 220 " Append the query to history | 221 " Execute query with prompt for parameters |
| 221 call add(s:query_history, query) | 222 call s:PromptAndExecuteQuery(query) |
| 222 | 223 endfunction |
| 223 let data = s:ExecuteDBQuery(query) | 224 |
| 224 | 225 " Function to execute SQL from a visual selection with parameter prompt |
| 225 tabnew | |
| 226 enew | |
| 227 execute 'file SQLQueryResult' | |
| 228 call append(0, data) | |
| 229 | |
| 230 " Set the buffer file type to SQL for syntax highlighting | |
| 231 setlocal filetype=sql | |
| 232 | |
| 233 setlocal buftype=nofile | |
| 234 setlocal bufhidden=wipe | |
| 235 setlocal nobuflisted | |
| 236 setlocal noswapfile | |
| 237 setlocal nomodifiable | |
| 238 endfunction | |
| 239 | |
| 240 " Execute SQL from a visual selection | |
| 241 function! s:ExecuteVisualSQLQuery() range | 226 function! s:ExecuteVisualSQLQuery() range |
| 242 " Get the content of the selected lines as a single SQL query | 227 " Get the content of the selected lines as a single SQL query |
| 243 let lines = getline(a:firstline, a:lastline) | 228 let lines = getline(a:firstline, a:lastline) |
| 244 let query = join(lines, " ") | 229 let query = join(lines, "\n") |
| 230 | |
| 231 " Execute query with prompt for parameters | |
| 232 call s:PromptAndExecuteQuery(query) | |
| 233 endfunction | |
| 234 | |
| 235 | |
| 236 " Function to prompt user for parameter values and execute query | |
| 237 function! s:PromptAndExecuteQuery(query) | |
| 238 " Identify parameters in the form of :parameter_name | |
| 239 let pattern = ':\(\k\+\)' | |
| 240 let params = [] | |
| 241 | |
| 242 " Find all matches in the query | |
| 243 let start = 0 | |
| 244 while match(a:query, pattern, start) != -1 | |
| 245 let match = matchstr(a:query, pattern, start) | |
| 246 if index(params, match) == -1 | |
| 247 call add(params, match) | |
| 248 endif | |
| 249 let start = matchend(a:query, pattern, start) | |
| 250 endwhile | |
| 251 | |
| 252 " Initialize a dictionary for parameters and their values | |
| 253 let param_values = {} | |
| 254 | |
| 255 " Prompt user for each parameter | |
| 256 for param in params | |
| 257 let value = input('Enter value for ' . param . ': ') | |
| 258 let param_values[param] = value | |
| 259 endfor | |
| 260 | |
| 261 " Replace parameters in the query with user-provided values | |
| 262 let updated_query = a:query | |
| 263 for [param, value] in items(param_values) | |
| 264 let updated_query = substitute(updated_query, param, shellescape(value), 'g') | |
| 265 endfor | |
| 245 | 266 |
| 246 " Append the query to history | 267 " Append the query to history |
| 247 call add(s:query_history, query) | 268 call add(s:query_history, updated_query) |
| 248 | 269 |
| 249 " Execute the query and capture results | 270 " Execute the updated query |
| 250 let data = s:ExecuteDBQuery(query) | 271 let data = s:ExecuteDBQuery(updated_query) |
| 251 | 272 |
| 252 " Open results in a new tab | 273 " Open results in a new tab |
| 253 tabnew | 274 tabnew |
| 254 enew | 275 enew |
| 255 execute 'file SQLQueryResult' | 276 execute 'file SQLQueryResult' |
| 257 | 278 |
| 258 " Set buffer options and highlight | 279 " Set buffer options and highlight |
| 259 setlocal filetype=sql | 280 setlocal filetype=sql |
| 260 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nomodifiable | 281 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nomodifiable |
| 261 endfunction | 282 endfunction |
| 283 | |
| 262 | 284 |
| 263 function! s:OpenQueryHistory() | 285 function! s:OpenQueryHistory() |
| 264 " Open the query history in a new split window | 286 " Open the query history in a new split window |
| 265 execute 'vsplit' | 287 execute 'vsplit' |
| 266 enew | 288 enew |
