-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsql-indent-left.el
351 lines (324 loc) · 12 KB
/
sql-indent-left.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
;;; sql-indent-left.el --- configuration options to indent sql -*- lexical-binding: t -*-
;; Copyright (C) 2017 Free Software Foundation, Inc
;; Filename: sql-indent-left.el
;; Description:
;; Author: [email protected]
;; Maintainer: [email protected]
;; Created:
;; Version: [email protected]
;; Last-Updated:
;; By:
;; Update #: 0
;; URL:
;; Keywords: language sql indentation
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;; None
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Set configuration options to indent sql my way.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'sql-indent)
(defun indent-case-statement-items (syntax base-indentation)
"Look for a SYNTAX of ((block-start when) (in-block case \"\") ...)
or ((block-start else) (in-block case \"\") ...)."
(let ((outer (sqlind-outer-context syntax)))
(if (and (eq 'in-block (sqlind-syntax-symbol outer))
(eq 'case (sqlind-syntax-keyword outer))
(eq 'block-start (sqlind-syntax-symbol syntax))
(memq (sqlind-syntax-keyword syntax) '(when else)))
(+ base-indentation sqlind-basic-offset)
base-indentation)))
(defvar sqlind-indentation-right-offsets-alist
`((select-column-continuation sqlind-indent-select-column
sqlind-adjust-operator
sqlind-lone-semicolon)
(in-select-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-right-justify-logical-operator
sqlind-lone-semicolon)
(in-delete-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-right-justify-logical-operator
sqlind-lone-semicolon)
(in-insert-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-right-justify-logical-operator
sqlind-lone-semicolon)
(in-update-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-right-justify-logical-operator
sqlind-lone-semicolon)
;; mandatory
(select-table-continuation sqlind-indent-select-table +
sqlind-lone-semicolon)
;; rest picked up from the original indentation offsets
,@sqlind-default-indentation-offsets-alist)
"Align sql code like this :
clear columns
set linesize 2500
set trimout on trimspool on
select atc.column_name,
atc.data_type,
data_length,
data_precision,
nullable,
data_scale,
nvl(substr(comments, 1, 100), atc.column_name) comments
from all_tab_columns atc,
all_col_comments acc
where atc.owner = acc.owner
and atc.table_name = acc.table_name
and atc.column_name = acc.column_name
and atc.owner = user
and atc.table_name = 'MY_TABLE'
and atc.column_name = p_column_name
and not exists (select 1
from all_tab_columns atc1,
all_col_comments acc1
where atc1.owner = acc1.owner
and atc1.table_name = acc1.table_name
and atc1.column_name = acc1.column_name
and atc1.owner = atc.owner
and atc1.table_name = atc.table_name
and acc1.column_name = acc.column_name)
;
delete from my_table mt
where col_1 = v_col1
and ( col_2 = v_col2
or col_3 = v_col3)
and col_42 = '42'
;
update my_table
set col1_has_a_long_name = value1,
col2_is_short = value2
where cond1 is not null
and ( col_2 = v_col2
or col_3 = v_col3)
and col_42 = '42'
;
insert into xyzxx
( aaa, xxx, bbb, ccc,
ddd, eee, fff, ggg,
hhh )
select aaa,
xxx,
max (m.b1) as bbb,
min (m.b1) as ccc,
coalesce (max (n.c2), 0) as ddd,
coalesce (min (n.c2), 0) as eee,
max (m.b1) over ( partition by c2
order by aaa desc ) as fff,
min (m.b1) over ( partition by c2
order by aaa desc ) as ggg,
avg (n.c2) as hhh
from (select * from (select aaa,
jjj + kkk as b1,
row_number () over ( partition by qqq
order by rrr,
sss ) as rn
from mno)
where rn = 1) m
inner join (select aaa,
nnn + ooo as c2
from pqr) n
using (aaa),
group by aaa,
xxx
order by xxx desc,
aaa asc
;")
(defvar sqlind-indentation-left-offsets-alist
`((select-clause 0)
(insert-clause 0)
(delete-clause 0)
(update-clause 0)
(case-clause-item-cont 0)
(block-start indent-case-statement-items)
(begin-block 0)
(case-clause +)
(package +)
(package-body +)
(statement-continuation + sqlind-adjust-operator)
(nested-statement-open 1)
(nested-statement-continuation 1)
(nested-statement-close sqlind-use-anchor-indentation)
(string-continuation 0) ;; or should it be a beginning of line or aligned with the previous block ?
;; Anyway. It's really *BAD* to continue a string across lines.
(select-column sqlind-indent-select-column-alt
sqlind-adjust-operator
sqlind-lone-semicolon)
(select-column-continuation sqlind-indent-select-column-alt
sqlind-adjust-operator
sqlind-lone-semicolon)
(in-select-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-left-justify-logical-operator
sqlind-lone-semicolon)
(in-delete-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-left-justify-logical-operator
sqlind-lone-semicolon)
(in-insert-clause +
sqlind-adjust-operator
sqlind-left-justify-logical-operator
sqlind-lone-semicolon)
(in-update-clause sqlind-lineup-to-clause-end
sqlind-adjust-operator
sqlind-left-justify-logical-operator
sqlind-lone-semicolon)
(select-table-continuation + sqlind-lone-semicolon)
;; rest picked up from the original indentation offsets
,@sqlind-default-indentation-offsets-alist)
"Align sql code like this :
clear columns
set linesize 2500
set trimout on trimspool on
select DISTINCT
atc.column_name,
atc.data_type,
data_length,
data_precision,
nullable,
data_scale,
nvl(substr(comments, 1, 100), atc.column_name) comments
from all_tab_columns atc,
all_col_comments acc
where atc.owner = acc.owner
and atc.table_name = acc.table_name
and atc.column_name = acc.column_name
and atc.owner = user
and atc.table_name = 'MY_TABLE'
and atc.column_name = p_column_name
and not exists (select 1
from all_tab_columns atc1,
all_col_comments acc1
where atc1.owner = acc1.owner
and atc1.table_name = acc1.table_name
and atc1.column_name = acc1.column_name
and atc1.owner = atc.owner
and atc1.table_name = atc.table_name
and acc1.column_name = acc.column_name)
;
delete from my_table mt
where col_1 = v_col1
and ( col_2 = v_col2
or col_3 = v_col3)
and col_42 = '42'
;
update my_table
set col1_has_a_long_name = value1,
col2_is_short = value2
where cond1 is not null
and ( col_2 = v_col2
or col_3 = v_col3)
and col_42 = '42'
;
insert into xyzxx
( aaa, xxx, bbb, ccc,
ddd, eee, fff, ggg,
hhh )
select aaa,
xxx,
max (m.b1) as bbb,
min (m.b1) as ccc,
coalesce (max (n.c2), 0) as ddd,
coalesce (min (n.c2), 0) as eee,
max (m.b1) over ( partition by c2
order by aaa desc ) as fff,
min (m.b1) over ( partition by c2
order by aaa desc ) as ggg,
avg (n.c2) as hhh
from (select * from (select aaa,
jjj + kkk as b1,
row_number () over ( partition by qqq
order by rrr,
sss ) as rn
from mno)
where rn = 1) m
inner join (select aaa,
nnn + ooo as c2
from pqr) n
using (aaa),
group by aaa,
xxx
order by xxx desc,
aaa asc
;")
(defun sqlind-indent-select-column-alt (syntax base-indentation)
"Return the indentation for a column after a SELECT DISTINCT clause.
SYNTAX is the syntax of the current line, BASE-INDENTATION is the
current indentation, which we need to update.
Like `sqlind-indent-select-column' but we try to align to the KEYWORD,
but if we are the first column after the SELECT clause we simply
add `sqlind-basic-offset'."
(save-excursion
(goto-char (sqlind-anchor-point syntax))
(when (looking-at "select\\s *\\(top\\s +[0-9]+\\|distinct\\|unique\\)?")
(if (match-beginning 1)
(goto-char (match-beginning 1))
(goto-char (match-end 0))))
(skip-syntax-forward " ")
(if (or (looking-at sqlind-comment-start-skip)
(looking-at "$"))
(+ base-indentation sqlind-basic-offset)
(current-column))))
;;;###autoload
(defun sqlind-setup-style-left ()
"Define an sql-indentation style where keywords are left aligned."
(interactive)
(setq sqlind-indentation-offsets-alist sqlind-indentation-left-offsets-alist))
;;;###autoload
(defun sqlind-setup-style-right ()
"Define an sql-indentation style where keywords are right aligned."
(interactive)
(setq sqlind-indentation-offsets-alist sqlind-indentation-right-offsets-alist))
;;;###autoload
(defun sqlind-setup-style-default ()
"Define an sql-indentation style where keywords are right aligned."
(interactive)
(setq sqlind-indentation-offsets-alist sqlind-default-indentation-offsets-alist))
(provide 'sql-indent-left)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; sql-indent-left.el ends here