Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
connections.json
*.sh
21 changes: 11 additions & 10 deletions lua/sqlua/connection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ function Connection:query(query, data)
if ui.buffers.results ~= nil then
setSidebarModifiable(ui.buffers.results, true)
vim.api.nvim_buf_set_lines(ui.buffers.results, 0, -1, false, data)
vim.api.nvim_win_set_cursor(ui.windows.results, {1, 0})
setSidebarModifiable(ui.buffers.results, false)
else
print("QUERY RESULTS PANE")
ui.createResultsPane(data)
ui:createResultsPane(data)
vim.api.nvim_win_set_cursor(ui.windows.results, {1, 0})
end
vim.api.nvim_set_current_win(win)
vim.api.nvim_win_set_buf(win, buf)
Expand Down Expand Up @@ -281,7 +282,7 @@ function Connection:executeUv(query_type, query_data)
vim.api.nvim_buf_set_lines(
ui.buffers.results, 0, -1, false, {})
else
ui.createResultsPane({})
ui:createResultsPane({})
end
if not ui.windows.query_float then
local w = vim.api.nvim_win_get_width(ui.windows.results)
Expand Down Expand Up @@ -405,18 +406,18 @@ function Connection:execute(--[[optional mode string]] mode)
end
end

local final_query = {}
if query then
for i, j in ipairs(query) do
query[i] = query[i]:gsub("[\r\n]", " ")
query[i] = query[i]:gsub("%s+", " ")
local cleaned = j:gsub("%s+", "")
if cleaned:sub(1, 1) == "-" and cleaned:sub(2, 2) == '-' then
table.remove(query, i)
query[i] = j:gsub("[\r\n]", " ")
query[i] = " "..query[i]:match("^%s*(.-)%s*$").." "
local cleaned = j:match("^%s*(.-)%s*$")
if cleaned:match("^%-%-") or cleaned:match("^%#") then
else
query[i] = " "..query[i]
table.insert(final_query, query[i])
end
end
self:executeUv("query", query)
self:executeUv("query", final_query)
end
end

Expand Down
45 changes: 44 additions & 1 deletion lua/sqlua/queries/mysql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,50 @@ M.ddl = {
"Indexes",
"References",
"Foreign Keys",
"DDL",
}

---@param tbl string
---@param schema string
---@param limit integer
---@return string[]
M.getQueries = function(tbl, schema, limit)
return {
Data = [[
SELECT *
FROM ]]..schema.."."..tbl.."\n"..[[
LIMIT ]]..limit,
Columns = "DESCRIBE "..schema.."."..tbl,
PrimaryKeys = [[
SHOW KEYS FROM ]]..schema.."."..tbl..[[

WHERE key_name = 'PRIMARY'
]],
Indexes = "SHOW INDEX FROM "..schema.."."..tbl,
References = [[
SELECT
kc.constraint_name,
kc.table_schema,
kc.table_name,
kc.column_name,
c.column_type,
c.column_key,
kc.referenced_table_schema foreign_schema,
kc.referenced_table_name foreign_table
FROM information_schema.key_column_usage kc
JOIN information_schema.columns c
ON c.table_schema = kc.table_schema
AND c.table_name = kc.table_name
AND c.column_name = kc.column_name
WHERE kc.constraint_name <> 'PRIMARY'
AND kc.referenced_table_schema = ']]..schema..[['
AND kc.referenced_table_name = ']]..tbl..[['
]],
ForeignKeys = [[
SHOW KEYS FROM ]]..schema.."."..tbl..[[

WHERE key_name <> 'PRIMARY'
]]
}
end

return M
70 changes: 28 additions & 42 deletions lua/sqlua/queries/postgres.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ M.ddl = {
"Indexes",
"References",
"Foreign Keys",
"DDL",
}

---@param tbl string
Expand All @@ -53,20 +52,11 @@ M.ddl = {
---@return string[]
M.getQueries = function(tbl, schema, limit)
return {
Data = [[
Data = [[
SELECT *
FROM ]] .. schema .. "." .. tbl .. [[
LIMIT ]] .. limit,

Columns = [[
SELECT
column_name, column_default, is_nullable, data_type
FROM information_schema.columns
WHERE table_name = ']] .. tbl .. [['
AND table_schema = ']] .. schema .. [['
ORDER BY column_name
]],

FROM ]]..schema.."."..tbl.."\n"..[[
LIMIT ]]..limit,
Columns = "\\d+ "..schema.."."..tbl,
PrimaryKeys = [[
SELECT
tc.constraint_name,
Expand All @@ -84,15 +74,22 @@ FROM information_schema.table_constraints AS tc
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'PRIMARY KEY'
AND tc.table_name = ']] .. tbl .. [['
AND tc.table_schema = ']] .. schema .. [['
AND tc.table_name = ']] .. tbl .. [['
AND tc.table_schema = ']] .. schema .. [['
]],

Indexes = [[
SELECT *
FROM pg_indexes
SELECT
tc.constraint_name,
tc.table_name,
kcu.column_name,
pgi.indexdef AS index_definition
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN pg_indexes pgi
ON tc.constraint_name = pgi.indexname
WHERE tablename = ']] .. tbl .. [['
AND schemaname = ']] .. schema .. "'",
AND schemaname = ']] .. schema .. "'",
References = [[
SELECT
tc.constraint_name,
Expand All @@ -104,18 +101,17 @@ SELECT
rc.delete_rule
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.referential_constraints as rc
ON tc.constraint_name = rc.constraint_name
ON tc.constraint_name = rc.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
ON tc.constraint_name = ccu.constraint_name
WHERE constraint_type = 'FOREIGN KEY'
AND ccu.table_name = ']] .. tbl .. [['
AND tc.table_schema = ']] .. schema .. [['
AND ccu.table_name = ']] .. tbl .. [['
AND ccu.table_schema = ']] .. schema .. [['
]],

ForeignKeys = [[
SELECT
SELECT DISTINCT
tc.constraint_name,
tc.table_name,
kcu.column_name,
Expand All @@ -125,24 +121,14 @@ SELECT
rc.delete_rule
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.referential_constraints as rc
ON tc.constraint_name = rc.constraint_name
ON tc.constraint_name = rc.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
ON tc.constraint_name = ccu.constraint_name
WHERE constraint_type = 'FOREIGN KEY'
AND ccu.table_name = ']] .. tbl .. [['
AND tc.table_schema = ']] .. schema .. [['
]],

DDL = [[
SELECT
table_name,
pg_size_pretty(pg_relation_size(quote_ident(table_name))),
pg_relation_size(quote_ident(table_name))
FROM Information_schema.tables
WHERE table_schema = 'public'
ORDER BY 3 DESC;
AND tc.table_name = ']] .. tbl .. [['
AND tc.table_schema = ']] .. schema .. [['
]],
}
end
Expand Down
Loading