avataralpha2phi

Summary

This article explores plugins that can turn Neovim into a database browser or explorer, focusing on vim-dadbod, sqls.nvim, and vim-database, and also discussing the built-in SQL completion feature in Vim/Neovim.

Abstract

The article "Neovim for Beginners — Database Explorer" discusses various plugins and features that enable database management within Neovim. The primary focus is on three plugins: vim-dadbod, sqls.nvim, and vim-database. The article provides installation and configuration instructions for each plugin, along with demonstrations of their usage. Additionally, the article covers the built-in SQL completion feature in Vim/Neovim, explaining how to use it for syntax, keywords, functions, types, statements, tables, procedures, views, and columns. The article is part of the "Neovim for Beginners" series and includes links to the author's Neovim configuration files and related articles.

Bullet points

  • The article explores plugins that can turn Neovim into a database explorer.
  • The main plugins discussed are vim-dadbod, sqls.nvim, and vim-database.
  • vim-dadbod supports a wide range of backends, including NoSQL databases.
  • sqls.nvim requires the sqls language server and SQLite driver for demonstration purposes.
  • The article also covers the built-in SQL completion feature in Vim/Neovim.
  • The author's Neovim configuration files are available in a GitHub repository.
  • The article is part of the "Neovim for Beginners" series.
  • Related articles are linked for more details on specific topics.

Neovim for Beginners — Database Explorer

Set up a database explorer with LSP completion for Neovim.

Neovim for Beginners — Database Explorer

In this article, let’s explore plugins that can turn Neovim into a database browser or explorer.

This article is part of the Neovim for Beginners series.

The Neovim configuration files are available in this repository.

Getting Started

As Neovim users, we want to do all our development from within Neovim, including managing the databases.

In this article, let’s explore plugins that provide an interface to interact with databases. We will also explore the built-in SQL completion, a feature provided by Vim/Neovim.

vim-dadbod

Using the vim-dadbod plugin and the following plugins provide us with a very user-friendly database explorer.

In the lua/plugins.lua file, we install these plugins.

Create the lua/config/dadbod.lua file to configure the plugins.

We configure the key mappings in the lua/config/whichkey.lua file.

Now we can use Neovim as a database explorer!

vim-dadbod

vim-dadbod supports a modern array of backends, including NoSQL databases:

  • ClickHouse
  • Impala
  • jq
  • MongoDB
  • MySQL
  • Oracle
  • osquery
  • PostgreSQL
  • Presto
  • Redis
  • SQL Server
  • SQLite
  • Your own easily implemented adapter

Check out these articles for more details!

Notes:

sqls.nvim

sqls.nvim is another Neovim plugin we can use to interact with databases.

Configuration

In the lua/plugins.lua file, we install the plugin.

This plugin needs the sqls language server. In the lsp/config/init.lua file, we configure mason.nvim to install the language server (line 3).

In the same file, we also add the code to attach the language server to the buffer (line 12 — line 14).

Check out this article related to mason.nvim.

We are going to use an SQLite database for demonstration purposes. Let’s install the SQLite driver for sqls.

go get github.com/mattn/go-sqlite3

We define the connection string in $XDG_CONFIG_HOME/sqls/config.yml.

Testing

We are ready to test now. Create a file with the .sql extension and run the :SqlsShowConnections command.

Show Connections

We can use the command :SqlsExecuteQuery to run a query.

Execute Query

vim-database

vim-database is another plugin that enables database management from within Neovim.

In the lua/plugins.lua file, we install this plugin.

We also need to install the Python libraries.

pip install pynvim pyyaml 

Now we can use this plugin to manage the databases.

vim-database

Built-in SQL Completion

Vim/Neovim also comes with a built-in filetype SQL plugin (:h ft_sql.txt).

Type :e $VIMRUNTIME/ftplugin/sql.vim and we can see the code of the plugin.

sql.vim

There is also a tutorial (:h sql-completion-tutorial) that shows how the omni-completion works for tables, columns, procedures, and views.

Let’s create a file with an .sql extension to test, e.g., test.sql.

Set SQL Dialect

We use the :SQLSetType command to set the SQL dialect. In the above screenshot, we configure the current dialect to MySQL.

This plugin defines the following omni-completion key mappings.

The g:ftplugin_sql_omni_key is <C-C>.

if !exists('g:ftplugin_sql_omni_key')
    let g:ftplugin_sql_omni_key = '<C-C>'
endif

Omni-completion should work now using MySQL dialect. E.g.,

  • <C-C>a for syntax
  • <C-C>k for keywords
  • <C-C>f for functions
  • <C-C>T for types
  • <C-C>s for statements
  • <C-C>t for tables
  • <C-C>p for procedures
  • <C-C>v for views
  • <C-C>c for columns
SQL Completion

Check out the article Learn Neovim The Practical Way for all the Vim/Neovim articles!

If you are not a Medium member yet and want to become one, click here. You will gain unlimited access to all Medium articles and support my work directly.

Vim
Programming
Software Development
Coding
Software Engineering
Recommended from ReadMedium
avatarAI Rabbit
Goodbye Obsidian

7 min read