I am using sql-interactive-mode to connect to 2 databases: MySQL and SQLite. I created yasnippets for mysql in yasnippets/sql-interactive-mode
folder. For example to add a column in MySQL I use the following snippet:
# -*- mode: snippet -*-
# name: Add column
# key: addcol
# --
ALTER TABLE $1 ADD COLUMN \`$2\` $3;
But SQLite uses different syntax. How can I create different yasnippets for different databases?
As explained here you can add arbitrary Emacs Lisp code (enclosed in backquotes) to yasnippet
snippets that will be evaluated when they expand. In sql-mode
and sql-interactive-mode
there is a variable called sql-product
that you can check to determine which type of database (mysql
, sqlite
, postgres
, etc.) you are currently working with.
That's basically all you need to know. Here is an example of how to modify your addcol
snippet:
# ...
ALTER TABLE $1 `(if (eq sql-product 'mysql) "ADD" "FROB")` COLUMN \`$2\` $3;
This will expand to
ALTER TABLE $1 ADD COLUMN \`$2\` $3;
for mysql
and
ALTER TABLE $1 FROB COLUMN \`$2\` $3;
for other types of databases.