branch: externals/sql-indent
commit 74c7c1cfff8fad70de6550ab3db3541b62e7b4bd
Author: Alex Harsanyi <[email protected]>
Commit: Alex Harsanyi <[email protected]>

    Recognize MsSQL directives and declarations (#40)
---
 sql-indent-test.el     |  3 +++
 sql-indent.el          | 24 ++++++++++++++----------
 test-data/pr29-syn.eld |  9 ++++++++-
 test-data/pr29.sql     | 11 +++++++----
 test-data/pr40-syn.eld | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 test-data/pr40.sql     | 24 ++++++++++++++++++++++++
 6 files changed, 105 insertions(+), 15 deletions(-)

diff --git a/sql-indent-test.el b/sql-indent-test.el
index 10f7698..6ddc42e 100644
--- a/sql-indent-test.el
+++ b/sql-indent-test.el
@@ -336,4 +336,7 @@ information read from DATA-FILE (as generated by
 (ert-deftest sqlind-ert-pr39 ()
   (sqlind-ert-check-file-syntax "test-data/pr39.sql" "test-data/pr39-syn.eld"))
 
+(ert-deftest sqlind-ert-pr40 ()
+  (sqlind-ert-check-file-syntax "test-data/pr40.sql" "test-data/pr40-syn.eld"))
+
 ;;; sql-indent-test.el ends here
diff --git a/sql-indent.el b/sql-indent.el
index ba45073..bcd97b6 100644
--- a/sql-indent.el
+++ b/sql-indent.el
@@ -268,11 +268,12 @@ A directive always stands on a line by itself -- we use 
that to
 determine the statement start in SQLite scripts.")
 
 (defconst sqlind-ms-directive
-  (concat "^"
-          (regexp-opt '("declare") t)
-          "\\>")
+  (concat
+   "\\(^set\\s-+\\(\\w\\|\\s_\\)+\\s-+\\(on\\|off\\)\\)\\|\\(^"
+   (regexp-opt '("use" "go" "declare") t)
+   "\\)\\b")
   "Match an MS SQL Sever directive at the beginning of a line.")
-
+  
 (defun sqlind-beginning-of-directive ()
   "Return the position of an SQL directive, or nil.
 We will never move past one of these in our scan.  We also assume
@@ -624,7 +625,8 @@ See also `sqlind-beginning-of-block'"
 (defun sqlind-maybe-create-statement ()
   "If (point) is on a CREATE statement, report its syntax.
 See also `sqlind-beginning-of-block'"
-  (when (looking-at "create\\_>\\(?:[ \n\r\f]+\\)\\(or\\(?:[ 
\n\r\f]+\\)replace\\_>\\)?")
+  (when (or (looking-at "create\\_>\\(?:[ \n\r\f]+\\)\\(or\\(?:[ 
\n\r\f]+\\)replace\\_>\\)?")
+            (looking-at "alter\\_>"))
     (prog1 t                            ; make sure we return t
       (save-excursion
        ;; let's see what are we creating
@@ -635,13 +637,13 @@ See also `sqlind-beginning-of-block'"
                                       (progn (forward-word) (point))))))
              (name (downcase (buffer-substring-no-properties
                               (progn (sqlind-forward-syntactic-ws) (point))
-                              (progn (skip-syntax-forward "w_") (point))))))
+                              (progn (skip-syntax-forward "w_()") (point))))))
          (when (and (eq what 'package) (equal name "body"))
            (setq what 'package-body)
            (setq name (downcase
                        (buffer-substring-no-properties
                         (progn (sqlind-forward-syntactic-ws) (point))
-                        (progn (skip-syntax-forward "w_") (point))))))
+                        (progn (skip-syntax-forward "w_()") (point))))))
 
          (if (memq what '(procedure function package package-body))
              ;; check is name is in the form user.name, if so then suppress 
user part.
@@ -700,7 +702,8 @@ See also `sqlind-beginning-of-block'"
              (forward-word -1)
              (when (looking-at "replace")
                (forward-word -2))
-             (when (and (looking-at "create\\([ \t\r\n\f]+or[ 
\t\r\n\f]+replace\\)?")
+             (when (and (or (looking-at "create\\([ \t\r\n\f]+or[ 
\t\r\n\f]+replace\\)?")
+                             (looking-at "alter"))
                         (not (sqlind-in-comment-or-string (point))))
                (setq real-start (point))))
            (goto-char real-start))
@@ -732,7 +735,7 @@ See also `sqlind-beginning-of-block'"
 (defconst sqlind-start-block-regexp
   (concat "\\(\\b"
          (regexp-opt '("if" "then" "else" "elsif" "loop"
-                       "begin" "declare" "create" "exception"
+                       "begin" "declare" "create" "alter" "exception"
                        "procedure" "function" "end" "case") t)
          "\\b\\)\\|)")
   "Regexp to match the start of a block.")
@@ -756,7 +759,8 @@ reverse order (a stack) and is used to skip over nested 
blocks."
             (sqlind-maybe-else-statement)
             (sqlind-maybe-loop-statement)
             (sqlind-maybe-begin-statement)
-            (sqlind-maybe-declare-statement)
+            (when (eq sql-product 'oracle) ; declare statements only start 
blocks in PL/SQL
+              (sqlind-maybe-declare-statement))
             (sqlind-maybe-create-statement)
             (sqlind-maybe-defun-statement))))
     'toplevel))
diff --git a/test-data/pr29-syn.eld b/test-data/pr29-syn.eld
index b84660a..afd254f 100644
--- a/test-data/pr29-syn.eld
+++ b/test-data/pr29-syn.eld
@@ -71,4 +71,11 @@
   ((in-begin-block toplevel-block "")
    . 165))
  ((toplevel . 1))
- ((toplevel . 1)))
\ No newline at end of file
+ ((comment-start . 1)
+  (toplevel . 1))
+ ((comment-start . 1)
+  (toplevel . 1))
+ ((comment-start . 1)
+  (toplevel . 1))
+ ((toplevel . 1)))
+ 
\ No newline at end of file
diff --git a/test-data/pr29.sql b/test-data/pr29.sql
index 6f5a453..fa2c923 100644
--- a/test-data/pr29.sql
+++ b/test-data/pr29.sql
@@ -1,10 +1,10 @@
 select y ,
        case
-         when foo>5 then "great"
-         when foo=5 then "normal"
-         else "poor"
+       when foo>5 then "great"
+       when foo=5 then "normal"
+       else "poor"
        end as level
-from   bar;
+  from   bar;
 
 declare
   dummy number;
@@ -23,3 +23,6 @@ begin
   end case;
 end;
 /
+-- Local Variables:
+-- sql-product: oracle
+-- End:
diff --git a/test-data/pr40-syn.eld b/test-data/pr40-syn.eld
new file mode 100644
index 0000000..4c1646e
--- /dev/null
+++ b/test-data/pr40-syn.eld
@@ -0,0 +1,49 @@
+(((toplevel . 1))
+ ((toplevel . 14))
+ ((toplevel . 17))
+ ((toplevel . 35))
+ ((toplevel . 38))
+ ((toplevel . 63))
+ ((toplevel . 66))
+ (((defun-start "[test]")
+   . 66))
+ (((defun-start "[test]")
+   . 66))
+ (((defun-start "[test]")
+   . 66))
+ (((defun-start "[test]")
+   . 66))
+ ((nested-statement-open . 130)
+  (statement-continuation . 130))
+ ((nested-statement-continuation . 130)
+  (statement-continuation . 130))
+ ((nested-statement-continuation . 130)
+  (statement-continuation . 130))
+ (((block-start is-or-as)
+   . 66)
+  ((defun-start "[test]")
+   . 66))
+ (((block-start begin)
+   . 66)
+  ((defun-start "[test]")
+   . 66))
+ (((in-begin-block defun "[test]")
+   . 211))
+ (((in-begin-block defun "[test]")
+   . 211))
+ (((in-begin-block defun "[test]")
+   . 211))
+ (((in-begin-block defun "[test]")
+   . 211))
+ (((block-end defun "[test]")
+   . 211)
+  ((in-begin-block defun "[test]")
+   . 211))
+ ((comment-start . 66)
+  (toplevel . 66))
+ ((comment-start . 66)
+  (toplevel . 66))
+ ((comment-start . 66)
+  (toplevel . 66))
+ ((toplevel . 66)))
+ 
\ No newline at end of file
diff --git a/test-data/pr40.sql b/test-data/pr40.sql
new file mode 100644
index 0000000..3786c80
--- /dev/null
+++ b/test-data/pr40.sql
@@ -0,0 +1,24 @@
+USE [testdb]
+GO
+SET ANSI_NULLS ON
+GO
+SET QUOTED_IDENTIFIER ON
+GO
+ALTER FUNCTION [dbo].[Test]
+()
+RETURNS
+@TestBatch TABLE
+(
+  [param] [varchar](20) NOT NULL,
+  [param2] [datetime] NOT NULL,
+)
+AS
+BEGIN
+  DECLARE @var int;
+  DECLARE @var2 int;
+  set @var = 100;
+  RETURN;
+END
+-- Local Variables:
+-- sql-product: ms
+-- End:

Reply via email to