diff autoload/dbtables.vim @ 3:3aa33ab9a907 default tip

tags
author Luka Sitas <lsitas@avatarasoftware.com>
date Fri, 02 Oct 2026 10:39:52 -0400
parents 8530ebeda72c
children
line wrap: on
line diff
--- a/autoload/dbtables.vim	Wed Mar 11 12:03:31 2026 -0400
+++ b/autoload/dbtables.vim	Fri Oct 02 10:39:52 2026 -0400
@@ -11,8 +11,8 @@
 " 				Utilities	
 "=============================================
 
-let s:query_history = []
-let s:snippets = []
+let g:query_history = []
+let g:snippets = []
 
 
 
@@ -21,23 +21,23 @@
 " - - - - - - - - - - - - - - - - - - - - - -
 
 " Execute shell commands
-function! s:ExecuteShellCommand(command)
+function! dbtables#ExecuteShellCommand(command)
     let result = system(a:command . ' 2>&1')
     return [v:shell_error == 0, result]
 endfunction
 
 " Execute a SQL query, directly to the db
-function! s:ExecuteDBQuery(query)
+function! dbtables#ExecuteDBQuery(query)
     let command = printf('mariadb --user=%s --password=%s --database=%s --host=%s -e %s',
                 \ shellescape(g:db_user), shellescape(g:db_password),
                 \ shellescape(g:db_name), shellescape(g:db_host),
                 \ shellescape(a:query))
     
-    let [success, result] = s:ExecuteShellCommand(command)
+    let [success, result] = dbtables#ExecuteShellCommand(command)
     if success
         return split(result, "\n")
     else
-        call s:HandleDBError(result, command)
+        call dbtables#HandleDBError(result, command)
         return []
     endif
 endfunction
@@ -45,7 +45,7 @@
 " Function to execute SQL from the current buffer with parameter prompt
 function! dbtables#ExecuteSQLQuery()
     let query = join(getline(1, '$'), "\n")
-    call s:PromptAndExecuteQuery(query)
+    call dbtables#PromptAndExecuteQuery(query)
 endfunction
 
 " Function to execute SQL from a visual selection with parameter prompt
@@ -55,11 +55,11 @@
     let query = join(lines, "\n")
 
     " Execute query with prompt for parameters
-    call s:PromptAndExecuteQuery(query)
+    call dbtables#PromptAndExecuteQuery(query)
 endfunction
 
 " Handle parameter prompts and execute query
-function! s:PromptAndExecuteQuery(query)
+function! dbtables#PromptAndExecuteQuery(query)
     " Identify parameters in the form of :parameter_name
     let pattern = ':\(\k\+\)'
     let params = []
@@ -89,12 +89,12 @@
         let updated_query = substitute(updated_query, param, value, 'g')
     endfor
 
-    call add(s:query_history, updated_query)
-    call s:OpenInNewTab('SQLQueryResult', s:ExecuteDBQuery(updated_query))
+    call add(g:query_history, updated_query)
+    call dbtables#OpenInNewTab('SQLQueryResult', dbtables#ExecuteDBQuery(updated_query))
 endfunction
 
 " Handle database command errors
-function! s:HandleDBError(result, command)
+function! dbtables#HandleDBError(result, command)
     echoerr 'Shell command failed: ' . a:command
     if a:result =~# 'Access denied'
         echoerr 'Authentication error: Check your username and password.'
@@ -116,7 +116,7 @@
 " - - - - - - - - - - - - - - - - - - - - - -
 
 " Display data in a new read only buffer
-function! s:OpenInNewTab(title, data)
+function! dbtables#OpenInNewTab(title, data)
     tabnew
     enew
     execute 'file ' . a:title
@@ -142,56 +142,56 @@
     execute '34 vsplit'
     enew
     file DBTables
-    let tables = s:ExecuteDBQuery("SHOW TABLES;")
+    let tables = dbtables#ExecuteDBQuery("SHOW TABLES;")
     if empty(tables)
         echo "No tables found or an error occurred."
     else
-        call append(0, ['Available Tables:'] + tables[1:])
+        call append(0, ['Available Tabledbtables#'] + tables[1:])
         setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
 	   		\ nomodifiable nonumber norelativenumber winfixwidth
-        nnoremap <buffer> <CR> :call <SID>ShowTablePopup()<CR>
+        nnoremap <buffer> <CR> :call dbtables#ShowTablePopup()<CR>
     endif
 endfunction
 
 " Open the database schema
-function! s:OpenTableSchema(table)
-    call s:OpenInNewTab(a:table . '_schema', s:ExecuteDBQuery('DESCRIBE ' . a:table . ';'))
+function! dbtables#OpenTableSchema(table)
+    call dbtables#OpenInNewTab(a:table . '_schema', dbtables#ExecuteDBQuery('DESCRIBE ' . a:table . ';'))
 endfunction
 
 " Open the table data
-function! s:OpenTableData(table)
-    call s:OpenInNewTab(a:table, s:ExecuteDBQuery('SELECT * FROM ' . a:table . ';'))
+function! dbtables#OpenTableData(table)
+    call dbtables#OpenInNewTab(a:table, dbtables#ExecuteDBQuery('SELECT * FROM ' . a:table . ';'))
 endfunction
 
 let s:popup_table = ""
 
 " Popup selection for table actions
-function! s:ShowTablePopup()
+function! dbtables#ShowTablePopup()
     " Use Vim popup_menu if available
     if exists('*popup_menu')
         let s:popup_table = getline(line('.'))
         let items = ['View Data', 'View Schema', 'Cancel']
         call popup_menu(items, #{ 
-		\ callback: '<SID>HandlePopupSelection',
+		\ callback: 'dbtables#HandlePopupSelection',
 		\ })
         return
     endif
     " Popup_menu not available
     echoerr 'Floating popup requires Vim8.2+.'
-    call s:ShowTableMenuModal()
+    call dbtables#ShowTableMenuModal()
 endfunction
 
-function! s:HandlePopupSelection(id, result)
+function! dbtables#HandlePopupSelection(id, result)
     let table = s:popup_table
-    call s:ClosePopup()
+    call dbtables#ClosePopup()
     if a:result == 1
-        call s:OpenTableData(table)
+        call dbtables#OpenTableData(table)
     elseif a:result == 2
-        call s:OpenTableSchema(table)
+        call dbtables#OpenTableSchema(table)
     endif
 endfunction
 
-function! s:ClosePopup()
+function! dbtables#ClosePopup()
     unlet! s:popup_table
 endfunction
 
@@ -208,9 +208,9 @@
     " Open the query history in a new split window
     execute 'vsplit'
     enew
-    call setline(1, s:query_history)
-    nnoremap <buffer> <CR> :call s:OpenInNewTab('SQLQueryResult', 
-				\ s:ExecuteDBQuery(getline(line('.'))))<CR>
+    call setline(1, g:query_history)
+    nnoremap <buffer> <CR> :call dbtables#OpenInNewTab('SQLQueryResult', 
+				\ dbtables#ExecuteDBQuery(getline(line('.'))))<CR>
     setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile wrap
 endfunction
 
@@ -230,12 +230,11 @@
     enew
 
 	" Populate the buffer with the list of snippet files
-	let s:snippets = globpath(g:snippets_directory, '*', 0, 1)
-	let l:display = mapnew(s:snippets, 'fnamemodify(v:val, ":t:r")')
+	let g:snippets = globpath(g:snippets_directory, '*', 0, 1)
+	let l:display = mapnew(g:snippets, 'fnamemodify(v:val, ":t:r")')
 	call setline(1, l:display)
 	
-	nnoremap <buffer> <CR> :call s:OpenInNewTab(line('.'),
-				\ fnameescape(s:snippets[a:lnum-1]))<CR>
+	nnoremap <buffer> <CR> :execute "tabe " . fnameescape(g:snippets[line('.')-1])<CR>
 
     setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile wrap
 endfunction