clone of my dotfiles.ssp.sh
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add SQL-Runner to query DBs

sspaeti d84cb7c7 9265ed6e

+278
+3
nvim/.config/nvim/lua/sspaeti/plugins_custom/init.lua
··· 2 2 target_file = "todo.md", 3 3 global_file = "~/Simon/SecondBrain/🌿 Projects/My Todos.md", 4 4 }) 5 + 6 + 7 + require("sspaeti.plugins_custom.sql_runner")
+275
nvim/.config/nvim/lua/sspaeti/plugins_custom/sql_runner.lua
··· 1 + -- Source: https://github.com/hendrikmi/dotfiles/blob/main/nvim/lua/tools/sql-runner.lua 2 + local M = {} 3 + 4 + -- Cache file for storing user-defined commands 5 + local cache_dir = vim.fn.stdpath 'data' .. '/sql-runner' 6 + local cache_file = cache_dir .. '/commands.json' 7 + 8 + -- Currently selected command 9 + M.selected_command = nil 10 + 11 + -- Load commands from cache 12 + local function load_commands() 13 + if vim.fn.filereadable(cache_file) == 0 then 14 + return {} 15 + end 16 + 17 + local file = io.open(cache_file, 'r') 18 + if not file then 19 + return {} 20 + end 21 + 22 + local content = file:read '*a' 23 + file:close() 24 + 25 + local ok, commands = pcall(vim.json.decode, content) 26 + if ok and commands then 27 + return commands 28 + end 29 + 30 + return {} 31 + end 32 + 33 + -- Save commands to cache 34 + local function save_commands(commands) 35 + -- Ensure cache directory exists 36 + vim.fn.mkdir(cache_dir, 'p') 37 + 38 + local file = io.open(cache_file, 'w') 39 + if not file then 40 + vim.notify('[sql-runner] Failed to save commands', vim.log.levels.ERROR) 41 + return 42 + end 43 + 44 + file:write(vim.json.encode(commands)) 45 + file:close() 46 + end 47 + 48 + -- Find an existing window showing the given absolute path 49 + local function find_win_by_path(abs_path) 50 + for _, win in ipairs(vim.api.nvim_list_wins()) do 51 + local buf = vim.api.nvim_win_get_buf(win) 52 + local name = vim.api.nvim_buf_get_name(buf) 53 + if name == abs_path then 54 + return win 55 + end 56 + end 57 + return nil 58 + end 59 + 60 + -- Common function to run queries with any backend 61 + local function run_query(name, cmd) 62 + local outfile = vim.fn.stdpath 'data' .. '/sql-runner/sql.out' 63 + local abs_out = vim.fn.fnamemodify(outfile, ':p') 64 + 65 + -- Status: running 66 + vim.api.nvim_echo({ { '[sql-runner] Running ' .. name .. ' query…', 'ModeMsg' } }, false, {}) 67 + local t0 = vim.loop.hrtime() 68 + 69 + -- Run the command 70 + local full_cmd = cmd .. ' > ' .. vim.fn.shellescape(outfile) 71 + local vim_cmd = string.format("'<,'>write !%s", full_cmd) 72 + 73 + -- Debug: show exact command being run 74 + -- vim.api.nvim_echo({ { '[sql-runner] Executing: ' .. vim_cmd, 'Comment' } }, false, {}) 75 + 76 + vim.cmd(vim_cmd) 77 + 78 + -- Open or focus existing results split, then reload contents 79 + local win = find_win_by_path(abs_out) 80 + if win then 81 + vim.api.nvim_set_current_win(win) 82 + vim.cmd 'noautocmd edit' -- reload file without flicker 83 + else 84 + vim.cmd('split ' .. vim.fn.fnameescape(outfile)) 85 + end 86 + 87 + -- Done message with timing 88 + local ms = math.floor((vim.loop.hrtime() - t0) / 1e6) 89 + vim.api.nvim_echo({ { string.format('[sql-runner] %s query done in %d ms', name, ms), 'ModeMsg' } }, false, {}) 90 + end 91 + 92 + -- Add a new SQL command 93 + function M.add_command() 94 + -- Get alias 95 + vim.ui.input({ 96 + prompt = 'Enter command alias (e.g. postgres, mysql): ', 97 + }, function(alias) 98 + if not alias or alias == '' then 99 + return 100 + end 101 + 102 + -- Get command 103 + vim.ui.input({ 104 + prompt = 'Enter command (selection will be piped to this): ', 105 + }, function(cmd) 106 + if not cmd or cmd == '' then 107 + return 108 + end 109 + 110 + -- Add to cached commands 111 + local commands = load_commands() 112 + commands[alias] = cmd 113 + save_commands(commands) 114 + 115 + vim.notify(string.format('[sql-runner] Added command "%s"', alias), vim.log.levels.INFO) 116 + end) 117 + end) 118 + end 119 + 120 + -- Remove commands 121 + function M.remove_command() 122 + local commands = load_commands() 123 + 124 + if vim.tbl_isempty(commands) then 125 + vim.notify('[sql-runner] No commands to remove.', vim.log.levels.WARN) 126 + return 127 + end 128 + 129 + local items = {} 130 + for alias, cmd in pairs(commands) do 131 + table.insert(items, alias) 132 + end 133 + 134 + table.sort(items) 135 + 136 + -- Use vim.ui.select with multiple selection 137 + local function remove_multiple() 138 + vim.ui.select(items, { 139 + prompt = 'Select commands to remove (ESC when done):', 140 + format_item = function(item) 141 + return item 142 + end, 143 + }, function(choice) 144 + if choice then 145 + commands[choice] = nil 146 + 147 + -- Clear selected command if it was removed 148 + if M.selected_command and M.selected_command.alias == choice then 149 + M.selected_command = nil 150 + end 151 + 152 + -- Remove from items list 153 + for i, item in ipairs(items) do 154 + if item == choice then 155 + table.remove(items, i) 156 + break 157 + end 158 + end 159 + 160 + vim.notify(string.format('[sql-runner] Removed command "%s"', choice), vim.log.levels.INFO) 161 + 162 + -- Continue removing if there are more items 163 + if #items > 0 then 164 + vim.schedule(function() 165 + remove_multiple() 166 + end) 167 + else 168 + save_commands(commands) 169 + vim.notify('[sql-runner] All commands removed.', vim.log.levels.INFO) 170 + end 171 + else 172 + -- User pressed ESC, save and finish 173 + save_commands(commands) 174 + if vim.tbl_count(commands) == 0 then 175 + vim.notify('[sql-runner] All commands removed.', vim.log.levels.INFO) 176 + end 177 + end 178 + end) 179 + end 180 + 181 + remove_multiple() 182 + end 183 + 184 + -- Select a command to use 185 + function M.select_command() 186 + local commands = load_commands() 187 + 188 + if vim.tbl_isempty(commands) then 189 + vim.notify('[sql-runner] No commands configured. Use :AddSqlCmd to add one.', vim.log.levels.WARN) 190 + return 191 + end 192 + 193 + local items = {} 194 + for alias, cmd in pairs(commands) do 195 + table.insert(items, { 196 + alias = alias, 197 + cmd = cmd, 198 + }) 199 + end 200 + 201 + -- Sort items by alias for consistent ordering 202 + table.sort(items, function(a, b) 203 + return a.alias < b.alias 204 + end) 205 + 206 + -- Add currently selected marker 207 + local current_alias = M.selected_command and M.selected_command.alias or nil 208 + 209 + vim.ui.select(items, { 210 + prompt = 'Select SQL command:', 211 + format_item = function(item) 212 + local marker = (item.alias == current_alias) and ' [current]' or '' 213 + return item.alias .. marker 214 + end, 215 + }, function(choice) 216 + if choice then 217 + M.selected_command = choice 218 + vim.notify(string.format('[sql-runner] Selected "%s"', choice.alias), vim.log.levels.INFO) 219 + end 220 + end) 221 + end 222 + 223 + -- Run SQL with the selected command 224 + function M.run_sql() 225 + if not M.selected_command then 226 + -- If no command selected, prompt to select one 227 + local commands = load_commands() 228 + 229 + if vim.tbl_isempty(commands) then 230 + vim.notify('[sql-runner] No commands configured. Use :AddSqlCmd to add one.', vim.log.levels.ERROR) 231 + return 232 + end 233 + 234 + local items = {} 235 + for alias, cmd in pairs(commands) do 236 + table.insert(items, { 237 + alias = alias, 238 + cmd = cmd, 239 + }) 240 + end 241 + 242 + -- Sort items by alias for consistent ordering 243 + table.sort(items, function(a, b) 244 + return a.alias < b.alias 245 + end) 246 + 247 + vim.ui.select(items, { 248 + prompt = 'Select SQL command to run:', 249 + format_item = function(item) 250 + return item.alias 251 + end, 252 + }, function(choice) 253 + if choice then 254 + M.selected_command = choice 255 + run_query(choice.alias, choice.cmd) 256 + end 257 + end) 258 + else 259 + run_query(M.selected_command.alias, M.selected_command.cmd) 260 + end 261 + end 262 + 263 + -- Register commands 264 + vim.api.nvim_create_user_command('AddSqlCmd', M.add_command, {}) 265 + vim.api.nvim_create_user_command('RemoveSqlCmd', M.remove_command, {}) 266 + vim.api.nvim_create_user_command('SelectSqlCmd', M.select_command, {}) 267 + vim.api.nvim_create_user_command('RunSQL', M.run_sql, { range = true }) 268 + 269 + -- Set up keymaps 270 + vim.keymap.set('v', '<leader>pr', ':RunSQL<CR>', { silent = true, desc = 'Run SQL with selected backend' }) 271 + vim.keymap.set('n', '<leader>ps', ':SelectSqlCmd<CR>', { silent = true, desc = 'Select SQL backend' }) 272 + vim.keymap.set('n', '<leader>pa', ':AddSqlCmd<CR>', { silent = true, desc = 'Add SQL command' }) 273 + vim.keymap.set('n', '<leader>px', ':RemoveSqlCmd<CR>', { silent = true, desc = 'Remove SQL command' }) 274 + 275 + return M