2015-12-02 14:23:39 +00:00
|
|
|
#+TITLE: SQL layer
|
2015-06-10 16:44:30 +00:00
|
|
|
|
2019-05-07 08:21:07 +00:00
|
|
|
#+TAGS: dsl|layer|programming
|
2019-05-05 17:26:40 +00:00
|
|
|
|
2015-06-10 16:44:30 +00:00
|
|
|
[[file:img/sql.png]]
|
|
|
|
|
2019-05-07 20:05:06 +00:00
|
|
|
* Table of Contents :TOC_5_gh:noexport:
|
2017-05-22 14:16:12 +00:00
|
|
|
- [[#description][Description]]
|
2018-02-14 22:18:09 +00:00
|
|
|
- [[#features][Features:]]
|
2017-05-22 14:16:12 +00:00
|
|
|
- [[#install][Install]]
|
2020-02-20 20:32:23 +00:00
|
|
|
- [[#external-dependencies][External Dependencies]]
|
2016-11-20 18:11:56 +00:00
|
|
|
- [[#sql-keywords-capitalization][SQL Keywords Capitalization]]
|
|
|
|
- [[#sql-interactive-mode][SQL Interactive Mode]]
|
|
|
|
- [[#blacklisting-keywords][Blacklisting keywords]]
|
2018-07-31 08:17:48 +00:00
|
|
|
- [[#auto-indent][Auto-Indent]]
|
2017-05-22 14:16:12 +00:00
|
|
|
- [[#key-bindings][Key bindings]]
|
|
|
|
- [[#highlighting][Highlighting]]
|
|
|
|
- [[#inferior-process-interactions-sqli][Inferior Process Interactions (SQLi)]]
|
2020-02-20 20:32:23 +00:00
|
|
|
- [[#send-sql-queries-to-sqli][Send SQL queries to SQLi]]
|
2017-05-22 14:16:12 +00:00
|
|
|
- [[#sqli-buffer][SQLi buffer]]
|
2018-05-24 02:12:30 +00:00
|
|
|
- [[#code-formatting][Code Formatting]]
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
|
|
* Description
|
2018-02-14 22:18:09 +00:00
|
|
|
This layer adds support for a wide range of SQL dialects to Spacemacs.
|
|
|
|
|
|
|
|
** Features:
|
|
|
|
- Syntax highlighting for the following SQL dialects
|
|
|
|
- ANSI
|
|
|
|
- DB2
|
|
|
|
- Informix
|
|
|
|
- Ingres
|
|
|
|
- Interbase
|
|
|
|
- Linter
|
|
|
|
- Microsoft
|
|
|
|
- MySQL
|
|
|
|
- Oracle
|
|
|
|
- Postgres
|
|
|
|
- Solid
|
|
|
|
- SQLite
|
|
|
|
- Sybase
|
|
|
|
- Vertica
|
|
|
|
- Syntax-checking via [[https://github.com/purcell/sqlint][sqlint]] for ANSI SQL.
|
2020-02-18 20:55:35 +00:00
|
|
|
- Format code with [[https://github.com/mjibson/sqlfmt][=sqlfmt=]]
|
2018-02-14 22:18:09 +00:00
|
|
|
- Snippet insertion for the more general SQL constructs.
|
|
|
|
- REPL support via =SQLi= buffer.
|
|
|
|
- Automatic capitalization of keywords.
|
2020-06-25 12:42:37 +00:00
|
|
|
- LSP support via [[https://github.com/lighttiger2505/sqls][sqls]].
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
|
|
* Install
|
2016-01-06 05:21:55 +00:00
|
|
|
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
|
|
|
|
add =sql= to the existing =dotspacemacs-configuration-layers= list in this
|
|
|
|
file.
|
2015-06-10 16:44:30 +00:00
|
|
|
|
2020-02-20 20:32:23 +00:00
|
|
|
** External Dependencies
|
|
|
|
Some functionality, like linting, rely on external binaries:
|
|
|
|
- *Syntax Checking*: Install [[https://www.ruby-lang.org/en/about/][ruby]] and the =sqlint= gem.
|
2018-02-14 22:18:09 +00:00
|
|
|
|
|
|
|
#+BEGIN_SRC ruby
|
2018-09-19 03:54:47 +00:00
|
|
|
gem install sqlint
|
2018-02-14 22:18:09 +00:00
|
|
|
#+END_SRC
|
2020-06-25 12:42:37 +00:00
|
|
|
- *Formatting*: Install [[https://github.com/mjibson/sqlfmt][sqlfmt]] and move it into your =$PATH=
|
|
|
|
|
2020-02-20 20:32:23 +00:00
|
|
|
#+BEGIN_SRC sh
|
|
|
|
# Download and extract the binary 0.4.0 for linux
|
|
|
|
wget -q -O - https://github.com/mjibson/sqlfmt/releases/latest/download/sqlfmt_0.4.0_linux_amd64.tar.gz | tar -xpvzf - --directory "${installdir}/bin"
|
|
|
|
#+END_SRC
|
2020-06-25 12:42:37 +00:00
|
|
|
- *LSP supporting (sqls)*:
|
2020-07-01 19:13:36 +00:00
|
|
|
- Install [[https://golang.org/dl/][Go]] lang and [[https://github.com/lighttiger2505/sqls][sqls]], then setting the variable =sql-backend= to ='lsp=.
|
2020-06-25 12:42:37 +00:00
|
|
|
- One of way to connect DB is setting variable =sql-lsp-sqls-workspace-config-path= to ='workspace= or ='root= and put json file at these directory.
|
|
|
|
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(
|
|
|
|
(sql :variables
|
|
|
|
sql-backend 'lsp
|
|
|
|
sql-lsp-sqls-workspace-config-path 'workspace)))
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
#+BEGIN_SRC json
|
2020-07-01 19:13:36 +00:00
|
|
|
{
|
|
|
|
"sqls": {
|
|
|
|
"connections": [
|
|
|
|
{
|
|
|
|
"driver": "mysql",
|
|
|
|
"dataSourceName": "user1:password1@tcp(localhost:3306)/sample_db"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2020-06-25 12:42:37 +00:00
|
|
|
}
|
|
|
|
#+END_SRC
|
|
|
|
|
2016-11-20 18:11:56 +00:00
|
|
|
** SQL Keywords Capitalization
|
|
|
|
SQL, by convention, uses upper-case keywords, although lower-case works just as
|
|
|
|
well. As humans, the separation between upper-case and lower-case helps scan and
|
|
|
|
parse the code much more quickly.
|
|
|
|
|
|
|
|
To install [[https://github.com/Trevoke/sqlup-mode.el][sqlup-mode]] which enables auto capitalization in =sql mode= set the
|
|
|
|
variable =sql-capitalize-keywords= to =t=.
|
|
|
|
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(
|
|
|
|
(sql :variables sql-capitalize-keywords t)))
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
*** SQL Interactive Mode
|
|
|
|
If you want capitalization only in =sql-mode= and not in =sql-interactive-mode=
|
|
|
|
you can set the variable =sql-capitalize-keywords-disable-interactive= to =t=.
|
|
|
|
|
|
|
|
*** Blacklisting keywords
|
|
|
|
[[https://github.com/Trevoke/sqlup-mode.el][sqlup-mode]] can be configured to ignore certain keywords. For example if you use
|
|
|
|
=name= as column name it would be annoying to have it upcased. You can prevent
|
|
|
|
this behaviour by setting the variable =sql-capitalize-keywords-blacklist= to
|
2017-05-24 10:13:13 +00:00
|
|
|
a list with keywords to ignore, e.g.
|
2016-11-20 18:11:56 +00:00
|
|
|
|
2017-05-24 10:13:13 +00:00
|
|
|
#+BEGIN_SRC emacs-lisp
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(
|
|
|
|
(sql :variables
|
|
|
|
sql-capitalize-keywords t
|
|
|
|
sql-capitalize-keywords-blacklist '("name" "varchar"))))
|
|
|
|
#+END_SRC
|
|
|
|
|
|
|
|
This layer is blacklisting =name= by default as it is a very common name for
|
2016-11-20 18:11:56 +00:00
|
|
|
column and NAME is non-reserved SQL keyword.
|
|
|
|
|
2018-07-31 08:17:48 +00:00
|
|
|
** Auto-Indent
|
2019-05-26 20:58:52 +00:00
|
|
|
This mode use [[https://github.com/alex-hhh/emacs-sql-indent][sql-indent]] to indent the code. You can check the package's README
|
|
|
|
to adjust the rules. If that's not what you want, you can also disable
|
2018-07-31 08:17:48 +00:00
|
|
|
auto-indent by setting the variable =sql-auto-indent= to =nil=.
|
|
|
|
|
|
|
|
#+BEGIN_SRC emacs-lisp
|
|
|
|
(setq-default dotspacemacs-configuration-layers '(
|
|
|
|
(sql :variables sql-auto-indent nil)))
|
|
|
|
#+END_SRC
|
|
|
|
|
2015-06-10 16:44:30 +00:00
|
|
|
* Key bindings
|
|
|
|
** Highlighting
|
|
|
|
|
2018-12-05 03:03:03 +00:00
|
|
|
| Key binding | Description |
|
2015-06-10 16:44:30 +00:00
|
|
|
|-------------+-----------------------------------|
|
|
|
|
| ~SPC m h k~ | select a SQL dialect to highlight |
|
|
|
|
|
|
|
|
** Inferior Process Interactions (SQLi)
|
|
|
|
|
2019-07-11 12:59:22 +00:00
|
|
|
| Key binding | Description |
|
|
|
|
|-------------+------------------------------------------------------|
|
|
|
|
| ~SPC m b b~ | show the SQLi buffer name |
|
|
|
|
| ~SPC m b c~ | connect to a SQLi buffer from your saved buffer list |
|
|
|
|
| ~SPC m b s~ | set the SQLi buffer |
|
|
|
|
| ~SPC m l a~ | List all objects |
|
|
|
|
| ~SPC m l t~ | list all objects in a table |
|
2015-06-10 16:44:30 +00:00
|
|
|
|
2020-02-20 20:32:23 +00:00
|
|
|
*** Send SQL queries to SQLi
|
2015-06-10 16:44:30 +00:00
|
|
|
|
2018-10-26 15:39:08 +00:00
|
|
|
| Key binding | Description |
|
|
|
|
|-------------+-------------------------------------------------------------------------------------------------------|
|
|
|
|
| ~SPC m s b~ | Send the whole buffer to the SQLi buffer |
|
|
|
|
| ~SPC m s B~ | Send the whole buffer to the SQLi buffer and switch to it in =insert state= |
|
|
|
|
| ~SPC m s i~ | Start the SQLi process |
|
|
|
|
| ~SPC m s f~ | Send the paragraph around point to the SQLi buffer |
|
|
|
|
| ~SPC m s F~ | Send the paragraph around point to the SQLi buffer and switch to it in =insert state= |
|
|
|
|
| ~SPC m s l~ | Send the current line to the SQLi buffer and move to the next line =insert state= |
|
|
|
|
| ~SPC m s L~ | Send the current line to the SQLi buffer and move to the next line and switch to it in =insert state= |
|
|
|
|
| ~SPC m s q~ | Prompt for a string to send to the SQLi buffer |
|
|
|
|
| ~SPC m s Q~ | Prompt for a string to send to the SQLi buffer and switch to it in =insert state= |
|
|
|
|
| ~SPC m s r~ | Send the selected region to the SQLi buffer |
|
|
|
|
| ~SPC m s R~ | Send the selected region to the SQLi buffer and switch to it in =insert state= |
|
2015-06-10 16:44:30 +00:00
|
|
|
|
|
|
|
** SQLi buffer
|
|
|
|
|
2018-12-05 03:03:03 +00:00
|
|
|
| Key binding | Description |
|
2015-06-10 16:44:30 +00:00
|
|
|
|-------------+--------------------------------------------------------------|
|
|
|
|
| ~SPC m b r~ | rename buffer (follow up in the SQL buffer with ~SPC m b s~) |
|
|
|
|
| ~SPC m b S~ | save the current connection |
|
2016-11-20 18:11:56 +00:00
|
|
|
|
2018-05-24 02:12:30 +00:00
|
|
|
** Code Formatting
|
2016-11-20 18:11:56 +00:00
|
|
|
|
2020-02-20 20:32:23 +00:00
|
|
|
| Key binding | Description |
|
|
|
|
|-------------+--------------------------------------------------------------|
|
|
|
|
| ~SPC m = c~ | capitalize SQL keywords in region (if capitalize is enabled) |
|
|
|
|
| ~SPC m = =~ | ~sqlfmt~ whole buffer |
|
|
|
|
| ~SPC m = r~ | ~sqlfmt~ active region |
|