kostafey / ejc-sql

Emacs SQL client uses Clojure JDBC.
279 stars 29 forks source link

Less aggressive separator detection #113

Closed alf-mindshift closed 4 years ago

alf-mindshift commented 4 years ago

I suggest the following change:

(defn get-separator-re [separator]
  "We only look for the separator as the first character of the line.
E.g. you can use default separator char `/` in this query:
      SELECT count / 1000 FROM urls WHERE path like '%http://localhost%'
      /
      SELECT 1"
  (re-pattern
   (format
    "(?m)^\\s*%s"
    ;; Escape chars for `$$` separators.
    (.replace separator "$" "\\$"))))
kostafey commented 4 years ago

Ok, looks like I should explain ejc-sql separators usage a little bit. ejc-sql attempts to be 2 things simultaneously: easy ad-hoc client for databases like h2 or SQLite and full-fledged client for databases like Oracle. There are 2 meanings of separators in ejc-sql.

E.g. you can easily fill you h2 database:

INSERT INTO product (name, price) VALUES ('socks', 1.25);
INSERT INTO product (name, price) VALUES ('sweater', 14.56);
INSERT INTO product (name, price) VALUES ('jeans', 25.30);
/
SELECT * FROM other_table

Here, the first part is a single snippet passed to ejc-sql backend but evaluated by 3 independent SQL statements. Furthermore, you can change the delimiter inside this snippet, e.g. for MySQL:

DELIMITER $$
CREATE PROCEDURE GetAllProducts()
BEGIN
  SELECT * FROM products;
END $$
/
CALL GetAllProducts();

Anyway, we can't use ; for Oracle to separate SQL statements since it's a part of its procedures syntax:

CREATE OR REPLACE PROCEDURE welcome_msg (p_name IN VARCHAR2) 
IS
BEGIN
dbms_output.put_line ('Welcome '|| p_name);
END;
/
EXEC welcome_msg ('John Doe')

That's why :separator "/" is used for Oracle db connections configuration examples, to seperate SQL statements only by this / symbol (https://github.com/kostafey/ejc-sql#oracle-connection).

Probably, this behavior should be reconsidered. @alf-mindshift, what do you think? Anyway, the last commit should fix the problem with division in SQL for connections, configured with :separator parameter:

SELECT 10/2 math FROM dual
alf commented 4 years ago

Being able to do math solves my issue perfectly. Thank you for the quick turn around time.

While I do find the separator-stuff a bit confusing, my feeling is that this issue was more a bug than a user error.