Skip to content

Commit

Permalink
ftplugin for autocmd. Could not show tables bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Orfo committed Oct 29, 2024
1 parent c0700bd commit 4a91816
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 52 deletions.
17 changes: 0 additions & 17 deletions ftplugin/javascript.lua

This file was deleted.

19 changes: 0 additions & 19 deletions ftplugin/sql.lua

This file was deleted.

4 changes: 0 additions & 4 deletions go/database/engine/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -231,15 +229,13 @@ 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))
}

if err := rows.Err(); err != nil {
logger.Errorf("Error iterating over rows:", err)
fmt.Printf("[ERROR] %v", err)
return
}

Expand Down
4 changes: 0 additions & 4 deletions go/database/engine/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ func (m *Mongo) GetTables() {
defer cancel()
db, closer, err := m.getDB(ctx)
if err != nil {
fmt.Printf(err.Error())
return
}
defer closer()

collections, err := db.ListCollections(ctx, bson.D{})
if err != nil {
logger.Errorf("Error listing collection:", err)
fmt.Printf("[ERROR] %v", err)
return
}

Expand All @@ -105,15 +103,13 @@ 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))
}

if err := collections.Err(); err != nil {
logger.Errorf("Error iterating over rows:", err)
fmt.Printf("[ERROR] %v", err)
return
}

Expand Down
43 changes: 43 additions & 0 deletions lua/dbeer.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}
local util = require 'dbeer.util'
local engines = require 'dbeer.engines'
local logger = util.logger

M.SETTINGS = {
commands = {
Expand Down Expand Up @@ -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, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ 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, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ noremap = true, silent = true })
end
end
end,
})
end

return M
26 changes: 18 additions & 8 deletions lua/dbeer/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
Expand Down

0 comments on commit 4a91816

Please sign in to comment.