Skip to content
Adrian Ortiz edited this page Nov 27, 2024 · 4 revisions

1. Using Command Modifiers

Command modifiers in (Neo)Vim allow you to customize how the result window appears. Here are some practical examples:

Open Results in a Horizontal Split

vim.api.nvim_set_keymap('v', ';eq', ':<C-U>split \'<,\'>SqlsExecuteQuery<CR>', { noremap = true, silent = true })
  • Opens the results in a horizontal split below the current window
  • Useful for comparing results while keeping your query visible.

Open Results in a Vertical Split

vim.api.nvim_set_keymap('v', ';eq', ':<C-U>vsplit \'<,\'>SqlsExecuteQuery<CR>', { noremap = true, silent = true })
  • Opens the results in a vertical split next to the current window
  • Ideal for wide results sets.

Opens Results in a Bottom Window

vim.api.nvim_set_keymap('v', ';eq', ':<C-U>botright \'<,\'>SqlsExecuteQuery<CR>', { noremap = true, silent = true })
  • Positions the results at the bottom of the screen, ensuring they don´t overlap with your main query.

Combine Modifiers for Custom Layouts

You can combine modifiers to achieve more specific layouts. For example:

vim.api.nvim_set_keymap('v', ';eq', ':<C-U>vertical botright \'<,\'>SqlsExecuteQuery<CR>', { noremap = true, silent = true })
  • Opens the results in a vertical split positioned at the far right.

2. Show current DataBase on LuaLine

Displaying the current database in your (Neo)Vim status line using lualine can enhance your workflow, especially when working with multiple databases. This guide explains how to achieve this by retrieving the database name via SQL queries and displaying it in the status line.

Step-by-Step Guide:

  1. Setup SQLs Configuration

Add the following configuration to your neovim setup to intregrate sqls with lspconfig and retrieve the current database name dinamically:

local lspconfig = require('lspconfig')

-- Configuración personalizada para sqls
lspconfig.sqls.setup({
    cmd = { "sqls" },
    on_attach = function(client, bufnr)
        require('sqls').on_attach(client, bufnr)
        if client.config.settings and client.config.settings.sqls and client.config.settings.sqls.connections then
            local connections = client.config.settings.sqls.connections
            if connections[1] and connections[1].dataSourceName then
                local dataSourceName = connections[1].dataSourceName
                local db_name = dataSourceName:match("database=([^&]+)")
                if db_name then
                    vim.g.current_database = db_name
                else
                    vim.g.current_database = 'N/A'
                end
            end
        end

        vim.api.nvim_create_autocmd('User', {
            pattern = 'SqlsConnectionChoice',
            callback = function(args)
                local choice = args.data.choice
                vim.g.current_database = choice:match("database=([^&]+)")
            end
        })
    end,
    settings = {
        sqls = {
            connections = {
                {
                    driver = 'mssql',
                    dataSourceName = 'sqlserver://<username>:<password>@<host>:<port>?database=<database_name>&encrypt=true&trustServerCertificate=true',
                },
                {
                    driver = 'mssql',
                    dataSourceName = 'sqlserver://<username>:<password>@<host>:<port>?database=<database_name>&encrypt=true&trustServerCertificate=true',
                },
            },
        },
    },
})
  1. Define a Lua Function for LuaLine

Add a helper function to fetch the current database name:

local function get_current_database()
    if vim.g.current_database then
        return 'DB: ' .. vim.g.current_database
    else
        return 'DB: N/A'
    end
end
  1. Integrate with lualine
require('lualine').setup({
    sections = {
        lualine_c = {
            { get_current_database }, -- Displays the current database in the center section
        },
    },
})

The result be some like this

LuaLineSQLs