branch: externals/sql-indent commit a6da8bd96b14b470b0a770106519b58b6c7fd47e Author: Alex Harsányi <alex-...@users.noreply.github.com> Commit: GitHub <nore...@github.com>
Recognize more create keywords (#75) --- sql-indent-test.el | 10 +++++ sql-indent.el | 82 ++++++++++++++++++++++++++++++++--------- test-data/pr75-oracle-syn.eld | 51 +++++++++++++++++++++++++ test-data/pr75-oracle.sql | 25 +++++++++++++ test-data/pr75-postgres-syn.eld | 67 +++++++++++++++++++++++++++++++++ test-data/pr75-postgres.sql | 30 +++++++++++++++ 6 files changed, 248 insertions(+), 17 deletions(-) diff --git a/sql-indent-test.el b/sql-indent-test.el index 8912daf..b0ca611 100644 --- a/sql-indent-test.el +++ b/sql-indent-test.el @@ -387,4 +387,14 @@ information read from DATA-FILE (as generated by (ert-deftest sqlind-ert-pr73 () (sqlind-ert-check-file-syntax "test-data/pr73.sql" "test-data/pr73-syn.eld")) +(ert-deftest sqlind-ert-prXX-postgres () + (sqlind-ert-check-file-syntax + "test-data/pr75-postgres.sql" + "test-data/pr75-postgres-syn.eld")) + +(ert-deftest sqlind-ert-prXX-oracle () + (sqlind-ert-check-file-syntax + "test-data/pr75-oracle.sql" + "test-data/pr75-oracle-syn.eld")) + ;;; sql-indent-test.el ends here diff --git a/sql-indent.el b/sql-indent.el index b2c03e3..aae7c2a 100644 --- a/sql-indent.el +++ b/sql-indent.el @@ -640,7 +640,7 @@ See also `sqlind-beginning-of-block'" 'declare-statement (list 'syntax-error "nested declare block" (point) (point)))))) -(defun sqlind-maybe-skip-mysql-create-options () +(defun sqlind-maybe-skip-create-options () "Move point past any MySQL option declarations. Statements like \"CREATE VIEW\" or \"CREATE TABLE\" can have @@ -648,30 +648,78 @@ various options betwen the CREATE keyword and the thing being created. If such options exist at (point) the cursor is moved past them. -Currently we move over the following options: +Currently we move over the following options, for different +products: + +MySQL: TEMPORARY ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE} DEFINER = { user | CURENT_USER } SQL SECURITY { DEFINER | INVOKER } +PostgresSQL + + TEMP + TEMPORARY + GLOBAL + LOCAL + UNLOGGED + MATERIALIZED + +Oracle + + PRIVATE + TEMP + TEMPORARY + MATERIALIZED + We don't consider if the options are valid or not for the thing being created. We just skip any and all of them that are present." - (when (eq sql-product 'mysql) - (catch 'finished - (while t - (cond - ((looking-at "temporary\\_>") - (goto-char (match-end 0)) - (sqlind-forward-syntactic-ws)) - ((looking-at "\\(definer\\|algorithm\\)\\(\\s-\\|[\n]\\)*=\\(\\s-\\|[\n]\\)*\\S-+") - (goto-char (match-end 0)) - (sqlind-forward-syntactic-ws)) - ((looking-at "sql\\(\\s-\\|[\n]\\)+security\\(\\s-\\|[\n]\\)+\\S-+") - (goto-char (match-end 0)) - (sqlind-forward-syntactic-ws)) - (t (throw 'finished nil))))))) + (cond + ((eq sql-product 'mysql) + (catch 'finished + (while t + (cond + ((looking-at "temporary\\_>") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + ((looking-at "\\(definer\\|algorithm\\)\\(\\s-\\|[\n]\\)*=\\(\\s-\\|[\n]\\)*\\S-+") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + ((looking-at "sql\\(\\s-\\|[\n]\\)+security\\(\\s-\\|[\n]\\)+\\S-+") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + (t (throw 'finished nil)))))) + ((eq sql-product 'postgres) + (catch 'finished + (while t + (cond + ((looking-at "temp\\(orary\\)?\\_>") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + ((looking-at "\\(global\\|local\\|unlogged\\)\\_>") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + ((looking-at "materialized\\_>") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + (t (throw 'finished nil)))))) + ((eq sql-product 'oracle) + (catch 'finished + (while t + (cond + ((looking-at "temp\\(orary\\)\\_>") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + ((looking-at "materialized\\_>") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + ((looking-at "private\\_>") + (goto-char (match-end 0)) + (sqlind-forward-syntactic-ws)) + (t (throw 'finished nil)))))))) (defun sqlind-maybe-create-statement () "If (point) is on a CREATE statement, report its syntax. @@ -683,7 +731,7 @@ See also `sqlind-beginning-of-block'" ;; let's see what are we creating (goto-char (match-end 0)) (sqlind-forward-syntactic-ws) - (sqlind-maybe-skip-mysql-create-options) + (sqlind-maybe-skip-create-options) (let ((what (intern (downcase (buffer-substring-no-properties (point) (progn (forward-word) (point)))))) diff --git a/test-data/pr75-oracle-syn.eld b/test-data/pr75-oracle-syn.eld new file mode 100644 index 0000000..1b1851e --- /dev/null +++ b/test-data/pr75-oracle-syn.eld @@ -0,0 +1,51 @@ +(((toplevel . 1)) + (((create-statement temp "table") + . 1)) + (((create-statement temp "table") + . 1)) + (((create-statement temp "table") + . 1)) + (((create-statement temp "table") + . 1)) + (((create-statement temp "table") + . 1)) + ((toplevel . 1)) + ((toplevel . 1)) + (((create-statement view "myview") + . 196)) + ((select-column . 233) + ((create-statement view "myview") + . 196)) + ((select-clause . 233) + ((create-statement view "myview") + . 196)) + ((select-table-continuation . 329) + ((create-statement view "myview") + . 196)) + ((toplevel . 1)) + ((toplevel . 1)) + (((create-statement table "foo") + . 403)) + (((create-statement table "foo") + . 403)) + (((create-statement table "foo") + . 403)) + ((select-column . 525) + ((create-statement table "foo") + . 403)) + ((select-clause . 525) + ((create-statement table "foo") + . 403)) + ((select-table-continuation . 621) + ((create-statement table "foo") + . 403)) + ((toplevel . 1)) + ((comment-start . 1) + (toplevel . 1)) + ((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/pr75-oracle.sql b/test-data/pr75-oracle.sql new file mode 100644 index 0000000..9210e74 --- /dev/null +++ b/test-data/pr75-oracle.sql @@ -0,0 +1,25 @@ +create or replace + private temp table mytable as + select distinct table1.fielda as firstfield, + table2.fieldb as secondfield + from table1 + join table2 on table1.table1id = table2.fktable1; + +create materialized view myview as + select distinct table1.fielda as firstfield, + table2.fieldb as secondfield + from table1 + join table2 on table1.table1id = table2.fktable1; + +create private -- apparently all temporary tables must be private in Oracle + temporary table if not exists foo + as + select distinct table1.fielda as firstfield, + table2.fieldb as secondfield + from table1 + join table2 on table1.table1id = table2.fktable1; + +-- local variables: +-- mode: sql +-- sql-product: oracle +-- end: diff --git a/test-data/pr75-postgres-syn.eld b/test-data/pr75-postgres-syn.eld new file mode 100644 index 0000000..d874f20 --- /dev/null +++ b/test-data/pr75-postgres-syn.eld @@ -0,0 +1,67 @@ +(((toplevel . 1)) + (((create-statement table "mytable") + . 1)) + (((create-statement table "mytable") + . 1)) + ((select-column . 52) + ((create-statement table "mytable") + . 1)) + ((select-clause . 52) + ((create-statement table "mytable") + . 1)) + ((select-table-continuation . 148) + ((create-statement table "mytable") + . 1)) + ((toplevel . 1)) + ((toplevel . 1)) + (((create-statement table "mytable") + . 222)) + ((select-column . 273) + ((create-statement table "mytable") + . 222)) + ((select-clause . 273) + ((create-statement table "mytable") + . 222)) + ((select-table-continuation . 369) + ((create-statement table "mytable") + . 222)) + ((toplevel . 1)) + ((toplevel . 1)) + (((create-statement view "myview") + . 443)) + ((select-column . 480) + ((create-statement view "myview") + . 443)) + ((select-clause . 480) + ((create-statement view "myview") + . 443)) + ((select-table-continuation . 576) + ((create-statement view "myview") + . 443)) + ((toplevel . 1)) + ((toplevel . 1)) + (((create-statement table "foo") + . 650)) + (((create-statement table "foo") + . 650)) + ((select-column . 698) + ((create-statement table "foo") + . 650)) + ((select-clause . 698) + ((create-statement table "foo") + . 650)) + ((select-table-continuation . 794) + ((create-statement table "foo") + . 650)) + ((toplevel . 1)) + ((comment-start . 1) + (toplevel . 1)) + ((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/pr75-postgres.sql b/test-data/pr75-postgres.sql new file mode 100644 index 0000000..8338b67 --- /dev/null +++ b/test-data/pr75-postgres.sql @@ -0,0 +1,30 @@ +create or replace + global temp table mytable as + select distinct table1.fielda as firstfield, + table2.fieldb as secondfield + from table1 + join table2 on table1.table1id = table2.fktable1; + +create local temporary unlogged table mytable as + select distinct table1.fielda as firstfield, + table2.fieldb as secondfield + from table1 + join table2 on table1.table1id = table2.fktable1; + +create materialized view myview as + select distinct table1.fielda as firstfield, + table2.fieldb as secondfield + from table1 + join table2 on table1.table1id = table2.fktable1; + +create temporary table if not exists foo + as + select distinct table1.fielda as firstfield, + table2.fieldb as secondfield + from table1 + join table2 on table1.table1id = table2.fktable1; + +-- local variables: +-- mode: sql +-- sql-product: postgres +-- end: