diff --git a/ftplugin/javascript.lua b/ftplugin/javascript.lua deleted file mode 100644 index b02708c..0000000 --- a/ftplugin/javascript.lua +++ /dev/null @@ -1,17 +0,0 @@ -local logger = require 'dbeer.util'.logger -local setup = require 'dbeer'.SETTINGS -local db = setup.db - -if db.connections then - local connection = db.connections[require 'dbeer'.default_db] - if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine == "mongo" then - logger:info(string.format("Database set to [%s]", connection.name)) - - vim.api.nvim_set_keymap('v', setup.commands.execute, 'lua require("dbeer.core").run()', - { noremap = true, silent = true }) - vim.api.nvim_set_keymap('n', setup.commands.execute, 'lua require("dbeer.core").run()', - { noremap = true, silent = true }) - vim.api.nvim_set_keymap('n', setup.commands.close, 'lua require("dbeer.core").close()', - { noremap = true, silent = true }) - end -end diff --git a/ftplugin/sql.lua b/ftplugin/sql.lua deleted file mode 100644 index 0c0d758..0000000 --- a/ftplugin/sql.lua +++ /dev/null @@ -1,19 +0,0 @@ -local logger = require 'dbeer.util'.logger -local setup = require 'dbeer'.SETTINGS -local db = setup.db - -if db.connections then - local connection = db.connections[require 'dbeer'.default_db] - if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] then - logger:info(string.format("Database set to [%s]", connection.name)) - - vim.api.nvim_set_keymap('v', setup.commands.execute, 'lua require("dbeer.core").run()', - { noremap = true, silent = true }) - vim.api.nvim_set_keymap('n', setup.commands.execute, 'lua require("dbeer.core").run()', - { noremap = true, silent = true }) - vim.api.nvim_set_keymap('n', setup.commands.close, 'lua require("dbeer.core").close()', - { noremap = true, silent = true }) - end -else - logger:info("No database configured.") -end diff --git a/go/database/engine/model/model.go b/go/database/engine/model/model.go index aff27bc..89096b0 100644 --- a/go/database/engine/model/model.go +++ b/go/database/engine/model/model.go @@ -211,7 +211,6 @@ func (p *ProtoSQL) ExecuteSelect(db *sql.DB) { func (p *ProtoSQL) GetTables() { db, closer, err := p.GetDB() if err != nil { - fmt.Printf(err.Error()) return } defer closer() @@ -221,7 +220,6 @@ func (p *ProtoSQL) GetTables() { rows, err := db.Query(p.Queries) if err != nil { logger.Errorf("Error executing query:", err) - fmt.Printf("[ERROR] %v", err) return } defer rows.Close() @@ -231,7 +229,6 @@ func (p *ProtoSQL) GetTables() { var table string if err := rows.Scan(&table); err != nil { logger.Errorf("Error scanning row:", err) - fmt.Printf("[ERROR] %v", err) return } values = append(values, strings.ToUpper(table)) @@ -239,7 +236,6 @@ func (p *ProtoSQL) GetTables() { if err := rows.Err(); err != nil { logger.Errorf("Error iterating over rows:", err) - fmt.Printf("[ERROR] %v", err) return } diff --git a/go/database/engine/mongo/mongo.go b/go/database/engine/mongo/mongo.go index e80836a..c61c072 100644 --- a/go/database/engine/mongo/mongo.go +++ b/go/database/engine/mongo/mongo.go @@ -87,7 +87,6 @@ func (m *Mongo) GetTables() { defer cancel() db, closer, err := m.getDB(ctx) if err != nil { - fmt.Printf(err.Error()) return } defer closer() @@ -95,7 +94,6 @@ func (m *Mongo) GetTables() { collections, err := db.ListCollections(ctx, bson.D{}) if err != nil { logger.Errorf("Error listing collection:", err) - fmt.Printf("[ERROR] %v", err) return } @@ -105,7 +103,6 @@ func (m *Mongo) GetTables() { err := collections.Decode(&collection) if err != nil { logger.Errorf("Error decoding collection:", err) - fmt.Printf("[ERROR] %v", err) return } values = append(values, collection["name"].(string)) @@ -113,7 +110,6 @@ func (m *Mongo) GetTables() { if err := collections.Err(); err != nil { logger.Errorf("Error iterating over rows:", err) - fmt.Printf("[ERROR] %v", err) return } diff --git a/lua/dbeer.lua b/lua/dbeer.lua index 817ac44..0347aed 100644 --- a/lua/dbeer.lua +++ b/lua/dbeer.lua @@ -1,6 +1,7 @@ local M = {} local util = require 'dbeer.util' local engines = require 'dbeer.engines' +local logger = util.logger M.SETTINGS = { commands = { @@ -122,6 +123,48 @@ function M.setup(opts) end util.logger:debug("Configuration: " .. vim.inspect(M.SETTINGS)) + + vim.api.nvim_create_autocmd("BufEnter", { + pattern = "*.js", + callback = function() + local db = M.SETTINGS.db + + if db.connections then + local connection = db.connections[require 'dbeer'.default_db] + if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine == "mongo" then + logger:info(string.format("Database set to [%s]", connection.name)) + + vim.api.nvim_set_keymap('v', M.SETTINGS.commands.execute, 'lua require("dbeer.core").run()', + { noremap = true, silent = true }) + vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, 'lua require("dbeer.core").run()', + { noremap = true, silent = true }) + vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, 'lua require("dbeer.core").close()', + { noremap = true, silent = true }) + end + end + end, + }) + + vim.api.nvim_create_autocmd("BufEnter", { + pattern = "*.sql", + callback = function() + local db = M.SETTINGS.db + + if db.connections then + local connection = db.connections[require 'dbeer'.default_db] + if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine == "mongo" then + logger:info(string.format("Database set to [%s]", connection.name)) + + vim.api.nvim_set_keymap('v', M.SETTINGS.commands.execute, 'lua require("dbeer.core").run()', + { noremap = true, silent = true }) + vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, 'lua require("dbeer.core").run()', + { noremap = true, silent = true }) + vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, 'lua require("dbeer.core").close()', + { noremap = true, silent = true }) + end + end + end, + }) end return M diff --git a/lua/dbeer/table.lua b/lua/dbeer/table.lua index d1c4fdd..56249c9 100644 --- a/lua/dbeer/table.lua +++ b/lua/dbeer/table.lua @@ -76,7 +76,7 @@ local function show_table_info(table_selected) do_after = function() vim.cmd [[ setlocal nowrap ]] vim.cmd [[ setl noma ]] - vim.cmd(line_1:gsub("?", "󰠵")) + vim.cmd(line_1) end } @@ -100,22 +100,32 @@ local function get_tables() engines.db[conn.engine].executor, conn.engine, core.get_connection_string(), util.dbeer_log_file, conn.dbname, setup.internal.log_debug)) - util.logger:debug(result) + if result ~= "" then + util.logger:debug(result) - local str = result:gsub("%[", ""):gsub("%]", ""):gsub("^%s*(.-)%s*$", "%1"):gsub(",", "") + local str = result:gsub("%[", ""):gsub("%]", ""):gsub("^%s*(.-)%s*$", "%1"):gsub(",", "") - local table_names = {} - for word in str:gmatch("%S+") do - table.insert(table_names, word) + local table_names = {} + for word in str:gmatch("%S+") do + table.insert(table_names, word) + end + return true, table_names + else + util.logger:error("Could not get system tables.") + return false, nil end - return table_names end function M.show() + local ok, tables = get_tables() + if not ok then + return + end + pickers.new({ prompt_title = " DBeer - Table Picker", finder = finders.new_table({ - results = get_tables(), + results = tables, entry_maker = function(entry) return { value = entry,