|
0
|
1 let g:db_user = "budgeter_db_user" " Replace with your default database username
|
|
|
2 let g:db_password = "qSTedfw39(0" " Replace with your default database password
|
|
|
3 let g:db_name = "budgeter" " Replace with your default database name
|
|
|
4 let g:db_host = "127.0.0.1" " Replace with your default database name
|
|
|
5
|
|
|
6
|
|
|
7 function! s:ExecuteDBQuery(query)
|
|
|
8 " Fetch the list of tables from the database
|
|
|
9 let db_user = shellescape(g:db_user)
|
|
|
10 let db_password = shellescape(g:db_password)
|
|
|
11 let db_name = shellescape(g:db_name)
|
|
|
12 let db_host = shellescape(g:db_host)
|
|
|
13
|
|
|
14 " Command to fetch tables
|
|
|
15 let command = 'mariadb --user=' . db_user . ' --password=' . db_password . ' -e "' . a:query . '" --database=' . db_name . ' --host=' . db_host
|
|
|
16 " echo "Running command: " . command
|
|
|
17 " Capture both stdout and stderr
|
|
|
18 let result = systemlist(command . ' 2>&1')
|
|
|
19
|
|
|
20 " Check for errors
|
|
|
21 if v:shell_error != 0
|
|
|
22 echoerr "Shell command failed: " . command
|
|
|
23 echoerr join(result, "\n")
|
|
|
24 return
|
|
|
25 endif
|
|
|
26 return result
|
|
|
27 endfunction
|
|
|
28
|
|
|
29
|
|
|
30 function! s:OpenDBTablesWindow()
|
|
|
31 " Open a new vertical split window
|
|
|
32 execute '34 vsplit'
|
|
|
33 " Create a new buffer
|
|
|
34 enew
|
|
|
35 " Set buffer name to "DBTables"
|
|
|
36 file DBTables
|
|
|
37
|
|
|
38 let tables = s:ExecuteDBQuery("SHOW TABLES;")
|
|
|
39
|
|
|
40 " Display the tables in the buffer
|
|
|
41 call append(0, 'Available Tables:')
|
|
|
42 call remove(tables, 0)
|
|
|
43 for table in tables
|
|
|
44 call append('$', table)
|
|
|
45 endfor
|
|
|
46
|
|
|
47 " Set buffer options
|
|
|
48 setlocal buftype=nofile
|
|
|
49 setlocal bufhidden=wipe
|
|
|
50 setlocal nobuflisted
|
|
|
51 setlocal noswapfile
|
|
|
52 setlocal nomodifiable
|
|
|
53 setlocal nonumber
|
|
|
54 setlocal norelativenumber
|
|
|
55 setlocal winfixwidth
|
|
|
56
|
|
|
57 " Map <Enter> to a function that displays table data
|
|
|
58 nnoremap <buffer> <CR> :call <SID>ShowTableData()<CR>
|
|
|
59 endfunction
|
|
|
60
|
|
|
61
|
|
|
62 function! s:ShowTableData()
|
|
|
63 " Get the current line (table name)
|
|
|
64 let lnum = line('.')
|
|
|
65 let table_name = getline(lnum)
|
|
|
66
|
|
|
67 " Open a new tab and create a new buffer
|
|
|
68 tabnew
|
|
|
69 enew
|
|
|
70 " Set buffer name to the table name
|
|
|
71 execute 'file ' . table_name
|
|
|
72
|
|
|
73 let data = s:ExecuteDBQuery("SELECT * FROM " . table_name . ";")
|
|
|
74
|
|
|
75 " Display the table data in the buffer
|
|
|
76 if len(data) > 0
|
|
|
77 call append(0, data)
|
|
|
78 else
|
|
|
79 call append(0, 'No data found.')
|
|
|
80 endif
|
|
|
81
|
|
|
82 " Set buffer options
|
|
|
83 setlocal buftype=nofile
|
|
|
84 setlocal bufhidden=wipe
|
|
|
85 setlocal nobuflisted
|
|
|
86 setlocal noswapfile
|
|
|
87 setlocal nomodifiable
|
|
|
88 endfunction
|
|
|
89
|
|
|
90 function! s:ExecuteSQLQuery()
|
|
|
91 " Get the content of the current buffer (SQL query)
|
|
|
92 let query = join(getline(1, '$'), " ")
|
|
|
93 let db_user = shellescape(g:db_user)
|
|
|
94 let db_password = shellescape(g:db_password)
|
|
|
95 let db_name = shellescape(g:db_name)
|
|
|
96
|
|
|
97 let data = s:ExecuteDBQuery(query)
|
|
|
98
|
|
|
99 tabnew
|
|
|
100 enew
|
|
|
101 execute 'file SQLQueryResult'
|
|
|
102 call append(0, data)
|
|
|
103 setlocal buftype=nofile
|
|
|
104 setlocal bufhidden=wipe
|
|
|
105 setlocal nobuflisted
|
|
|
106 setlocal noswapfile
|
|
|
107 setlocal nomodifiable
|
|
|
108 endfunction
|
|
|
109
|
|
|
110 function! DBConsole()
|
|
|
111 " Save the current cursor position
|
|
|
112 let save_cursor = getpos(".")
|
|
|
113
|
|
|
114 "Format the files
|
|
|
115 let db_user = g:db_user
|
|
|
116 let db_password = g:db_password
|
|
|
117 let db_name = g:db_name
|
|
|
118 " let db_host = shellescape(g:db_host)
|
|
|
119 let db_host = g:db_host
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123 let command = 'mariadb --user=' . db_user . ' --password=' . db_password . ' --database=' . db_name . ' --host=' . db_host . ' '
|
|
|
124 let command = substitute(command, '\n', '', 'g')
|
|
|
125 execute ':term ' . command
|
|
|
126 call setpos(".", save_cursor)
|
|
|
127 endfunction
|
|
|
128
|
|
|
129 function! OpenMariaDBConsole()
|
|
|
130 " The command to connect to MariaDB. Customize it with your actual connection details.
|
|
|
131 let cmd = 'mariadb -u your_username -p your_database_name'
|
|
|
132
|
|
|
133 " Open a new terminal window at the bottom with 10 lines height.
|
|
|
134 " Split the window horizontally; you can adjust the height by changing `10`.
|
|
|
135 botright 10split
|
|
|
136 call termopen(cmd)
|
|
|
137 endfunction
|
|
|
138
|
|
|
139 nnoremap <Leader>db :call DBConsole()<cr>
|
|
|
140
|
|
|
141 command! DBTables call s:OpenDBTablesWindow()
|
|
|
142 command! ExecuteSQL call s:ExecuteSQLQuery()
|