Function: sqlind-syntax-in-select
sqlind-syntax-in-select is a natively compiled function defined in
sql-indent.el.
Signature
(sqlind-syntax-in-select POS START)
Documentation
Return the syntax ar POS which is inside a "select" statement at START.
Source Code
;; Defined in /nix/store/jxjf2h9nnkdqnn1w1mr0ag8bqng2rlrs-emacs-packages-deps/share/emacs/site-lisp/elpa/sql-indent-1.7/sql-indent.el
(defun sqlind-syntax-in-select (pos start)
"Return the syntax ar POS which is inside a \"select\" statement at START."
(save-excursion
(catch 'finished
(goto-char pos)
;; all select query components are indented relative to the
;; start of the select statement)
(when (looking-at sqlind-select-clauses-regexp)
(throw 'finished (cons 'select-clause start)))
;; when we are not looking at a select component, find the
;; nearest one from us.
(while (sqlind-search-backward (point) sqlind-select-clauses-regexp start)
(let* ((match-pos (match-beginning 0))
(clause (sqlind-match-string 0)))
(setq clause (replace-regexp-in-string "[ \t\r\n\f]" " " clause))
(when (sqlind-same-level-statement (point) start)
(cond
((or (looking-at "select\\(\\s *\\_<\\(top\\s +[0-9]+\\|distinct\\|unique\\)\\_>\\)?")
(and (eq sql-product 'postgres) (looking-at "perform\\_>")))
;; we are in the column selection section.
(goto-char pos)
(if (looking-at ",")
(throw 'finished (cons 'select-column match-pos))
(progn
(sqlind-backward-syntactic-ws)
(throw 'finished
(cons (if (or (eq (match-end 0) (1+ (point)))
(looking-at ","))
'select-column
'select-column-continuation)
match-pos)))))
((looking-at "from")
;; FROM is only keyword if the previous char is NOT a
;; comma ','
(forward-char -1)
(sqlind-backward-syntactic-ws)
(unless (looking-at ",")
;; We are in the from section. If this line starts with 'on'
;; or the previous line ends with 'on' we have a join
;; condition
(goto-char pos)
(when (or (looking-at sqlind-join-condition-regexp)
(progn (forward-word -1)
(and (sqlind-same-level-statement (point) pos)
(looking-at sqlind-select-join-regexp))))
;; look for the join start, that will be the anchor
(let ((jstart (sqlind-find-join-start (point) start)))
(when jstart
(throw 'finished (cons 'select-join-condition jstart))))))
;; if this line starts with a ',' or the previous line starts
;; with a ',', we have a new table
(goto-char pos)
;; NOTE: the combination of tests and movement operations in
;; the when clause is not ideal...
(when (or (looking-at ",")
(looking-at sqlind-select-join-regexp)
(looking-at "join\\b")
(progn
(sqlind-backward-syntactic-ws)
(or (looking-at ",")
(progn
(forward-word -1)
(or (looking-at sqlind-select-join-regexp)
(looking-at "join\\b")
(looking-at "from\\b"))))))
(throw 'finished (cons 'select-table match-pos)))
(goto-char pos)
(let ((limit match-pos))
(if (sqlind-search-backward (point) (regexp-opt (list "," "join") 'symbols) limit)
(progn
(goto-char (match-end 0))
(sqlind-forward-syntactic-ws)
(when (looking-at "lateral")
(forward-word 1)
(sqlind-forward-syntactic-ws))
;; otherwise, we continue the table definition from the
;; previous line.
(throw 'finished
;; If, after following all these joins, we got back
;; to our line, we are in a select-table after all,
;; otherwise it is a table continuation.
(if (eq (point) pos)
(cons 'select-table match-pos)
(cons 'select-table-continuation (point)))))
(progn ; this must be the first table in the FROM section
(when (looking-at "from\\b")
(forward-word)
(sqlind-forward-syntactic-ws))
(throw 'finished (cons 'select-table-continuation (point)))))))
(t
(throw 'finished
(cons (list 'in-select-clause clause) match-pos))))))))))