Created
April 30, 2025 02:19
-
-
Save eduardoarandah/fbf0c0f82160d0a3006bb0bfc3e16a66 to your computer and use it in GitHub Desktop.
I made a quick and dirty plugin to describe my database so that I can send it to the LLM it has 3 commands: SQLDefine SQLTables SQLExecute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- expect a .env file in project root with the following variables: | |
-- DB_CONNECTION=mysql | |
-- DB_HOST=db | |
-- DB_PORT=3306 | |
-- DB_DATABASE=example | |
-- DB_USERNAME=asdf | |
-- DB_PASSWORD=asdf | |
local M = {} | |
-- Helper function to execute SQL and insert results | |
local function execute_sql(sql) | |
-- Check if .env file exists in project root or current directory | |
local env_file = vim.fn.findfile(".env", ".;") | |
if env_file == "" then | |
print("Error: .env file not found in project hierarchy") | |
return nil | |
end | |
-- Get current line number | |
local line_num = vim.fn.line(".") | |
-- Insert the SQL command | |
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, false, { sql }) | |
-- Execute the command and get the output | |
local lines = vim.fn.systemlist('source .env && mysql -u $DB_USERNAME -p$DB_PASSWORD -h $DB_HOST -P $DB_PORT $DB_DATABASE -e "' .. sql .. '"') | |
-- Filter lines with [Warning] | |
lines = vim.tbl_filter(function(line) | |
return not line:match("%[Warning%]") | |
end, lines) | |
-- add a blank line at the end | |
table.insert(lines, "") | |
-- Insert the filtered lines into the buffer | |
vim.api.nvim_buf_set_lines(0, line_num, line_num, false, lines) | |
end | |
-- Function to describe a table | |
local function sql_define() | |
-- Get current line content | |
local line_content = vim.fn.trim(vim.fn.getline(".")) | |
-- Check if it's exactly one word | |
local words = vim.fn.split(line_content, "\\s\\+") | |
if #words ~= 1 then | |
print("Error: Current line must contain exactly one word (table name)") | |
return | |
end | |
local table_name = words[1] | |
-- Create and execute the SQL command | |
execute_sql("DESCRIBE " .. table_name .. ";") | |
end | |
-- Function to show all tables in the database | |
local function sql_tables() | |
execute_sql("SHOW TABLES;") | |
end | |
-- Function to execute current line or visual selection as SQL | |
local function sql_execute() | |
-- Get current line | |
local sql = vim.fn.getline(".") | |
-- Trim whitespace | |
sql = vim.fn.trim(sql) | |
-- Check if empty | |
if sql == "" then | |
print("Error: No SQL query to execute") | |
return | |
end | |
-- Add semicolon if not present | |
if not sql:match(";%s*$") then | |
sql = sql .. ";" | |
end | |
-- Execute the SQL | |
execute_sql(sql) | |
end | |
function M.setup() | |
-- Register commands | |
vim.api.nvim_create_user_command("SQLDefine", function() | |
sql_define() | |
end, { desc = "Describe table from current line and insert results in buffer" }) | |
vim.api.nvim_create_user_command("SQLTables", function() | |
sql_tables() | |
end, { desc = "Show all tables in the database" }) | |
vim.api.nvim_create_user_command("SQLExecute", function() | |
sql_execute() | |
end, { desc = "Execute current line or visual selection as SQL", range = true }) | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment