Search code examples
emacsisqlsqsh

Replace isql with sqsh in emacs (Ubuntu)


Using emacs on Ubuntu 11.10. I want to connect to a SQL Server database using sqsh instead of isql. I added the following to my initi.el

(set 'sql-sybase-program "sqsh")
(set 'sql-ms-program "sqsh")

It recompiles and loads successfully. However, when I use sql-ms and try to connect to the database, I am getting errors because emacs is using lower-case command parameters when it should be using upper-case command parameters. Furthermore, I can successfully connect to the database server using sqsh from the command line. When I try to run things within emacs, I get the following error:

sqsh: -d: Invalid integer expression

Process SQL exited abnormally with code 255

I did a pretty extensive Google search and I can't find much on how to do this (which makes me think it may not be possible). Obviously, I can run sqsh from within a shell, but then I lose the SQL mode integration. I'm not sure what I can / need to do to my init.el file to make this possible.

I think all I really need to do is figure out how to get emacs to send a -D not a -d to sqsh. Apparently isql doesn't care, but sqsh cares deeply about the difference.


Solution

  • As you say, the real answer is to get Emacs to use -D instead of -d. However, as a workaround, running sql-sybase instead of sql-ms seems to work fine for me.

    Update: Try this code, it removes the -n option from sql-ms-options and redefines sql-ms-options to use -D instead of -d as the option to select the database:

    (setq sql-ms-options (remove "-n" sql-ms-options))
    
    (defun sql-comint-ms (product options)
      "Create comint buffer and connect to Microsoft SQL Server."
      ;; Put all parameters to the program (if defined) in a list and call
      ;; make-comint.
      (let ((params options))
        (if (not (string= "" sql-server))
            (setq params (append (list "-S" sql-server) params)))
        (if (not (string= "" sql-database))
            (setq params (append (list "-D" sql-database) params)))
        (if (not (string= "" sql-user))
        (setq params (append (list "-U" sql-user) params)))
        (if (not (string= "" sql-password))
        (setq params (append (list "-P" sql-password) params))
          (if (string= "" sql-user)
          ;; if neither user nor password is provided, use system
          ;; credentials.
          (setq params (append (list "-E") params))
        ;; If -P is passed to ISQL as the last argument without a
        ;; password, it's considered null.
        (setq params (append params (list "-P")))))
        (sql-comint product params)))