diff 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
line wrap: on
line diff
--- 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'