# HG changeset patch # User Luka Sitas # Date 1761594725 14400 # Node ID 4a7bc1c8551b2f225cd7565b174e27ef7b3eb3f2 # Parent 1a705d7a7521cee4128bdf41099e3027f871907b support for params in queries diff -r 1a705d7a7521 -r 4a7bc1c8551b .vimrc --- a/.vimrc Thu Oct 23 12:33:51 2025 -0400 +++ b/.vimrc Mon Oct 27 15:52:05 2025 -0400 @@ -8,7 +8,7 @@ " Support for tags closing and classes source $HOME/.vim/plugins/taghelp -source /home/luka/.vim/plugins/dbtables/dbtables.vim +source $HOME/.vim/plugins/dbtables/dbtables.vim let g:db_user = "budgeter_db_user" " Replace with your default database username let g:db_password = "qSTedfw39(0" " Replace with your default database password diff -r 1a705d7a7521 -r 4a7bc1c8551b plugins/dbtables/dbtables.vim --- a/plugins/dbtables/dbtables.vim Thu Oct 23 12:33:51 2025 -0400 +++ b/plugins/dbtables/dbtables.vim Mon Oct 27 15:52:05 2025 -0400 @@ -213,41 +213,62 @@ let s:query_history = [] +" Function to execute SQL from the current buffer with parameter prompt function! s:ExecuteSQLQuery() " Get the content of the current buffer (SQL query) - let query = join(getline(1, '$'), " ") - - " Append the query to history - call add(s:query_history, query) - - let data = s:ExecuteDBQuery(query) + let query = join(getline(1, '$'), "\n") - tabnew - enew - execute 'file SQLQueryResult' - call append(0, data) - - " Set the buffer file type to SQL for syntax highlighting - setlocal filetype=sql - - setlocal buftype=nofile - setlocal bufhidden=wipe - setlocal nobuflisted - setlocal noswapfile - setlocal nomodifiable + " Execute query with prompt for parameters + call s:PromptAndExecuteQuery(query) endfunction -" Execute SQL from a visual selection +" Function to execute SQL from a visual selection with parameter prompt function! s:ExecuteVisualSQLQuery() range " Get the content of the selected lines as a single SQL query let lines = getline(a:firstline, a:lastline) - let query = join(lines, " ") + let query = join(lines, "\n") + + " Execute query with prompt for parameters + call s:PromptAndExecuteQuery(query) +endfunction + + +" Function to prompt user for parameter values and execute query +function! s:PromptAndExecuteQuery(query) + " Identify parameters in the form of :parameter_name + let pattern = ':\(\k\+\)' + let params = [] + + " Find all matches in the query + let start = 0 + while match(a:query, pattern, start) != -1 + let match = matchstr(a:query, pattern, start) + if index(params, match) == -1 + call add(params, match) + endif + let start = matchend(a:query, pattern, start) + endwhile + + " Initialize a dictionary for parameters and their values + let param_values = {} + + " Prompt user for each parameter + for param in params + let value = input('Enter value for ' . param . ': ') + let param_values[param] = value + endfor + + " Replace parameters in the query with user-provided values + let updated_query = a:query + for [param, value] in items(param_values) + let updated_query = substitute(updated_query, param, shellescape(value), 'g') + endfor " Append the query to history - call add(s:query_history, query) + call add(s:query_history, updated_query) - " Execute the query and capture results - let data = s:ExecuteDBQuery(query) + " Execute the updated query + let data = s:ExecuteDBQuery(updated_query) " Open results in a new tab tabnew @@ -260,6 +281,7 @@ setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nomodifiable endfunction + function! s:OpenQueryHistory() " Open the query history in a new split window execute 'vsplit'