Skip to content

Commit

Permalink
Tets non session
Browse files Browse the repository at this point in the history
  • Loading branch information
flintforge committed Nov 15, 2024
1 parent dcd34d0 commit 709fe11
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
56 changes: 29 additions & 27 deletions ob-sql-session-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ Assume the source block is at POSITION if non-nil."
(funcall body)))


(defun babel-block-test (setup header code expect &optional expected-result)
"Execute SQL in a `sql-session' Babel block comparing the result against WANT."
(defun babel-block-test (setup header code expect)
"Given SETUP function, execute SQL CODE block with HEADER.
Compare the result against EXPECT."
(setup
(lambda ()
(let ((buffer-contents (format "
#+begin_src %s
%s
#+end_src" header code)))
(message buffer-contents)
(with-buffer-contents
buffer-contents
(org-mode)
Expand All @@ -68,10 +70,11 @@ Assume the source block is at POSITION if non-nil."
(should (string= expect (results-block-contents)))
)))))

(defun sqlite-test (code expect &optional expected-result)
(babel-block-test #'setup
"sql :engine sqlite :session Tests :results raw"
code expect))
(defun sqlite-test (code expect)
"SQL test CODE block, EXPECT 'ing this result."
(babel-block-test
#'setup "sql :engine sqlite :session sqlite::tests :results raw"
code expect))

(ert-deftest sqllite-000:test-header ()
"Create table."
Expand Down Expand Up @@ -158,36 +161,35 @@ sqlite|3.4
(let ((kill-buffer-query-functions nil))
(kill-this-buffer))))

(defun pg-test (code expect &optional expected-result)
(defun pg-test (code expect)
(babel-block-test
#'setup
"sql :session PG::tests :engine postgres :dbhost localhost :database pg :dbuser pg :dbpassword pg :results raw"
"sql :engine postgres :dbhost localhost :database pg :dbuser pg :dbpassword pg :results raw"
code expect))

(ert-deftest pg-000:test-session-var ()
"Select in a table."
(pg-test "\\set id10 10 \n \\set id13 13" nil))
(defun pg-test-session (code expect)
(babel-block-test
#'setup
"sql :engine postgres :session pg::tests :dbhost localhost :database pg :dbuser pg :dbpassword pg :results raw" code expect))

(ert-deftest pg-001:test-session-var-read ()
(ert-deftest pg-001:test-session-var-set ()
"Select in a table."
(pg-test "select :id10 as A,:id13 as B;" "a|b\n10|13\n"))

(ert-deftest pg-002:test-session-drop ()
"insert in a table."
(pg-test "DROP TABLE IF EXISTS publications;" "DROP TABLE\n"))
(pg-test-session "\\set id10 10 \n \\set id13 13" nil))

(ert-deftest pg-003:test-session-test-create-table ()
"insert in a table."
(pg-test "CREATE TABLE publications (id int2, database text);" "CREATE TABLE\n"))

(ert-deftest pg-004:test-session-insert ()
"insert in a table."
(pg-test "insert into publications values (:id10, 'HGNC'), (:id13, 'FlyBase');" "INSERT 0 2\n"))
(ert-deftest pg-002:test-session-var-read ()
"Select in a table."
(pg-test-session "select :id10 as A,:id13 as B;" "a|b\n10|13\n"))

(ert-deftest pg-005:test-session-query ()
(ert-deftest pg-001:test-create-insert-select ()
"Select in a table."
(pg-test "SELECT database from publications where id=:id10 or id=:id13;"
"database\nHGNC\nFlyBase\n"))
(pg-test "DROP TABLE if exists publications;
CREATE TABLE publications (id int2, database text);
INSERT INTO publications VALUES (10, 'HGNC'), (13, 'FlyBase');
SELECT database FROM publications where id=10 or id=13;"
"DROP TABLE
CREATE TABLE
INSERT 0 2
database\nHGNC\nFlyBase\n"))


;; (eval-buffer)
Expand Down
41 changes: 21 additions & 20 deletions ob-sql.el
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
;;
;; TODO:
;; - support for more engines
;; - what's a reasonable way to drop table data into SQL?
;;
;; - provide babel to SQL
;; - expand body for sessions

;;; Code:

Expand Down Expand Up @@ -120,7 +120,7 @@
:safe t)

(defcustom org-babel-sql-timeout '5.0
"Abort on timeout"
"Abort on timeout."
:type '(number)
:group 'org-babel-sql
:safe t)
Expand Down Expand Up @@ -264,7 +264,7 @@ database connections."
(cdr (assoc-string dbconnection sql-connection-alist t))))))))

(defun org-babel-execute:sql (body params)
"Execute a block of Sql code with Babel.
"Execute a block of SQL code in BODY with PARAMS.
This function is called by `org-babel-execute-src-block'."

(let* ((result-params (cdr (assq :result-params params)))
Expand All @@ -284,9 +284,8 @@ This function is called by `org-babel-execute-src-block'."
(session-p (not (string= session "none")))
(header-delim ""))

(org-babel-expand-body:sql body params)
(setq org-babel-sql-out-file out-file)
(sql-set-product in-engine)

(if (or session-p org-babel-sql-run-comint-p)
;; run through comint
(let ((sql--buffer
Expand All @@ -298,7 +297,10 @@ This function is called by `org-babel-execute-src-block'."

(with-current-buffer (get-buffer sql--buffer)
(process-send-string (current-buffer)
(ob-sql-session-format-query body))
(ob-sql-session-format-query
body
;;(org-babel-expand-body:sql body params)
))
;; todo: check org-babel-comint-async-register
(while (not ob-sql-session-command-terminated)
;; could there be a race condition here as described in (elisp) Accepting Output?
Expand Down Expand Up @@ -422,10 +424,10 @@ SET COLSEP '|'
(`vertica "\\a\n")
(_ ""))
;; "sqsh" requires "go" inserted at EOF.
(if (string= engine "sqsh") "\ngo" "")))
(if (string= engine "sqsh") "\ngo" "")
(org-babel-expand-body:sql body params))) ;; insert body
(org-babel-eval command ""))))


;; collect results
(org-babel-result-cond result-params
(with-temp-buffer
Expand Down Expand Up @@ -491,11 +493,10 @@ the :engine header argument provided in INFO."
(let ((prologue (cdr (assq :prologue params)))
(epilogue (cdr (assq :epilogue params))))
(mapconcat 'identity
(list
prologue
(org-babel-sql-expand-vars
body (org-babel--get-vars params))
epilogue)
(delq nil (list prologue
(org-babel-sql-expand-vars
body (org-babel--get-vars params))
epilogue))
"\n")))

(defun org-babel-sql-expand-vars (body vars &optional sqlite)
Expand Down Expand Up @@ -549,8 +550,8 @@ specified, its `sql-product' or `sql-connection' must match."
(let ((proc (get-buffer-process buffer)))
(and proc (memq (process-status proc) '(open run)))))))

(defun org-babel-sql-session-connect (engine params session)
"Start the SQL client of ENGINE if it has not.
(defun org-babel-sql-session-connect (in-engine params session)
"Start the SQL client of IN-ENGINE if it has not.
PARAMS provides the sql connection parameters for a new or
existing SESSION. Clear the intermediate buffer from previous
output, and set the process filter. Return the comint process
Expand All @@ -562,7 +563,7 @@ that clearly identifies the connexion from Emacs,
to *SQL [session]* in order to retrieve a session with its
name alone, the other parameters in the header args beeing
no longer needed while the session stays open."

(sql-set-product in-engine)
(let* ( (sql-server (cdr (assoc :dbhost params)))
;; (sql-port (cdr (assoc :port params)))
(sql-database (cdr (assoc :database params)))
Expand Down Expand Up @@ -594,10 +595,10 @@ no longer needed while the session stays open."
;; otherwise initiate a connection
(save-window-excursion
(setq ob-sql-buffer ; start the client
(ob-sql-connect engine buffer-name)))
(ob-sql-connect in-engine buffer-name)))
(let ((sql-term-proc (get-buffer-process ob-sql-buffer)))
(unless sql-term-proc
(user-error (format "SQL %s didn't start" engine)))
(user-error (format "SQL %s didn't start" in-engine)))

;; clear the welcoming message out of the output from the
;; first command, in the case where we forgot quiet mode.
Expand Down Expand Up @@ -719,7 +720,7 @@ Carefully separate client commands from SQL commands
Concatenate SQL commands as one line is one way to stop on error.
Otherwise the entire batch will be emitted no matter what.
Finnally add the termination command."

(message str)
;;(setq sql-product engine)
(concat
(let ((commands (split-string str "\n"))
Expand Down

0 comments on commit 709fe11

Please sign in to comment.