LoginSignup
0
0

More than 3 years have passed since last update.

Emacs sql-modeでH2を使う

Posted at

SQLもJavaもよくわかってないんですが、弊社のJavaおじさんが「H2使うよー」と言ったので使うことになりました。
よくわかんないけどEmacsのsql-modeあたりからexecuteできるじゃろと思いきや、H2は対応していなかったので自力でなんとかしてみました。
sql-modeに書いてあったエンジン追加方法ではうまくいかなかったけど、最終的になんとかなりました。

elispもよくわかってないので適当ですが、自分だけで使うより世間の目に晒しておいたほうがよいっぽいので晒しておきます。

  • あんまり動作確認してないです。
    • このタイミングで記事にしとかないとお蔵入りしそうなんで
  • 同じデータベースがすでにサーバモードじゃないプログラムから使われているとエラーになるかもです
  • ほかのエンジンを使うときと使用感が同じなのか違うのかよくわかってないです(ほかのデータベース自力で使えないので…)
  • 接続情報で Database: って聞かれたら -url のパラメータを入れてください。前後に""はいらないです。

こんな記事読んでる人はきっと私なんかはるかにしのぐemacs ninjaだと思うので説明も適当です…
これを書いてもorg-babelでコードブロック評価できないことをあとから知ってしょんぼりです

sql-h2.el
(require 'sql)

;; Driver(よくわかってない)も取得したい
;; urlにくっつけるオプションを別途取得するようにしたい


;; 各自書き換えてください
(defcustom sql-h2-jar-path "/usr/local/Cellar/h2/1.4.200/libexec/bin/h2-1.4.200.jar"
  "The location of the h2 executable JAR."
  :type 'string
  :group 'sql-h2
  )

;; sql-modeに書いてあった追加方法
;; 1) Add the product to the list of known products.
;; ここで書いてあるとおりにやaddするとエラーになります

;; 2) Define font lock settings.  All ANSI keywords will be
;;    highlighted automatically, so only product specific keywords
;;    need to be defined here.
;; と言ってるのでカスタマイズしていない

;; (defvar sql-mode-h2-font-lock-keywords
;;   '(("\\b\\(red\\|orange\\|yellow\\)\\b"
;;      . font-lock-keyword-face))
;;   "H2DB SQL keywords used by font-lock.")

;; (sql-set-product-feature 'h2
;;                          :font-lock
;;                          'sql-mode-h2-font-lock-keywords)

;; 3) Define any special syntax characters including comments and
;;    identifier characters.
;; よくわかんないから使ってない
;; (sql-set-product-feature 'h2
;;                          :syntax-alist ((?# . "_")))

;; 4) Define the interactive command interpreter for the database
;;    product.

(defcustom sql-h2-program "java"
  "Command to start ih2 by H2DB."
  :type 'file
  :group 'SQL)


;; 5) Define login parameters and command line formatting.

(defcustom sql-h2-login-params '(user password database)
  "Login parameters to needed to connect to H2DB."
  :type 'sql-login-params
  :group 'SQL)


;; 将来的に使うかもしれん
;; (defcustom sql-h2-options '("-X" "-Y" "-Z")
;;   "List of additional options for `sql-h2-program'."
;;   :type '(repeat string)
;;   :group 'SQL)

;; (sql-set-product-feature 'h2
;;                          :sqli-options 'sql-h2-options))



(defun sql-comint-h2 (product options &optional buf-name)
  "Connect to H2DB in a comint buffer."

  ;; Do something with `sql-user', `sql-password',
  ;; `sql-database', and `sql-server'.
  (let ((params
         (append
          (list "-cp" sql-h2-jar-path "org.h2.tools.Shell")
          (if (not (string= "" sql-database))
              (list "-url" sql-database))
          (if (not (string= "" sql-user))
              (list "-user" sql-user))
          (if (not (string= "" sql-password))
              (list "-passsword" sql-password))
          options)))
    (sql-comint product params buf-name)))

;; まとめてsql-add-productしないとだめだった
(sql-add-product 'h2 "H2DB"
                 :free-software t
                 :sqli-program 'sql-h2-program
                 :prompt-regexp "^sql> "
                 :prompt-length 7
                 :sqli-login 'sql-h2-login-params
                 :sqli-comint-func 'sql-comint-h2
                 )

;; 6) Define a convenience function to invoke the SQL interpreter.

(defun sql-h2 (&optional buffer)
  "Run ih2 by H2DB as an inferior process."
  (interactive "P")
  (sql-product-interactive 'h2 buffer))

(provide 'sql-h2)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0