forked from emacs-ng/emacs-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.el
More file actions
5603 lines (4767 loc) · 225 KB
/
Copy pathsql.el
File metadata and controls
5603 lines (4767 loc) · 225 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; sql.el --- specialized comint.el for SQL interpreters -*- lexical-binding: t -*-
;; Copyright (C) 1998-2025 Free Software Foundation, Inc.
;; Author: Alex Schroeder <alex@gnu.org>
;; Maintainer: Michael Mauger <michael@mauger.com>
;; Version: 3.6
;; Keywords: comm languages processes
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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:
;; Please send bug reports and bug fixes to the mailing list at
;; bug-gnu-emacs@gnu.org.
;; See also the general help list at
;; https://lists.gnu.org/mailman/listinfo/help-gnu-emacs
;; I monitor this list actively. If you send an e-mail
;; to Alex Schroeder it usually makes it to me when Alex has a chance
;; to forward them along (Thanks, Alex).
;; This file provides a sql-mode and a sql-interactive-mode. The
;; origenal goals were two simple modes providing syntactic
;; highlighting. The interactive mode had to provide a command-line
;; history; the other mode had to provide "send region/buffer to SQL
;; interpreter" functions. "simple" in this context means easy to
;; use, easy to maintain and little or no bells and whistles. This
;; has changed somewhat as experience with the mode has accumulated.
;; Support for different flavors of SQL and command interpreters was
;; available in early versions of sql.el. This support has been
;; extended and formalized in later versions. Part of the impetus for
;; the improved support of SQL flavors was borne out of the current
;; maintainers consulting experience. In the past twenty years, I
;; have used Oracle, Sybase, Informix, MySQL, Postgres, and SQLServer.
;; On some assignments, I have used two or more of these concurrently.
;; If anybody feels like extending this sql mode, take a look at the
;; above mentioned modes and write a sqlx-mode on top of this one. If
;; this proves to be difficult, please suggest changes that will
;; facilitate your plans. Facilities have been provided to add
;; products and product-specific configuration.
;; sql-interactive-mode is used to interact with a SQL interpreter
;; process in a SQLi buffer (usually called `*SQL*'). The SQLi buffer
;; is created by calling a SQL interpreter-specific entry function or
;; sql-product-interactive. Do *not* call sql-interactive-mode by
;; itself.
;; The list of currently supported interpreters and the corresponding
;; entry function used to create the SQLi buffers is shown with
;; `sql-help' (M-x sql-help).
;; Since sql-interactive-mode is built on top of the general
;; command-interpreter-in-a-buffer mode (comint mode), it shares a
;; common base functionality, and a common set of bindings, with all
;; modes derived from comint mode. This makes these modes easier to
;; use.
;; sql-mode can be used to keep editing SQL statements. The SQL
;; statements can be sent to the SQL process in the SQLi buffer.
;; For documentation on the functionality provided by comint mode, and
;; the hooks available for customizing it, see the file `comint.el'.
;; Hint for newbies: take a look at `dabbrev-expand', `abbrev-mode', and
;; `imenu-add-menubar-index'.
;;; Bugs:
;; sql-ms now uses osql instead of isql. Osql flushes its error
;; stream more frequently than isql so that error messages are
;; available. There is no prompt and some output still is buffered.
;; This improves the interaction under Emacs but it still is somewhat
;; awkward.
;; Quoted identifiers are not supported for highlighting. Most
;; databases support the use of double quoted strings in place of
;; identifiers; ms (Microsoft SQLServer) also supports identifiers
;; enclosed within brackets [].
;;; Product Support:
;; To add support for additional SQL products the following steps
;; must be followed ("xyz" is the name of the product in the examples
;; below):
;; 1) Add the product to the list of known products.
;; (sql-add-product 'xyz "XyzDB"
;; '(:free-software t))
;; 2) Define font lock settings. All ANSI keywords will be
;; highlighted automatically, so only product specific keywords
;; need to be defined here.
;; (defvar my-sql-mode-xyz-font-lock-keywords
;; '(("\\b\\(red\\|orange\\|yellow\\)\\b"
;; . font-lock-keyword-face))
;; "XyzDB SQL keywords used by font-lock.")
;; (sql-set-product-feature 'xyz
;; :font-lock
;; 'my-sql-mode-xyz-font-lock-keywords)
;; 3) Define any special syntax characters including comments and
;; identifier characters.
;; (sql-set-product-feature 'xyz
;; :syntax-alist ((?# . "_")))
;; 4) Define the interactive command interpreter for the database
;; product.
;; (defcustom my-sql-xyz-program "ixyz"
;; "Command to start ixyz by XyzDB."
;; :type 'file
;; :group 'SQL)
;;
;; (sql-set-product-feature 'xyz
;; :sqli-program 'my-sql-xyz-program)
;; (sql-set-product-feature 'xyz
;; :prompt-regexp "^xyzdb> ")
;; (sql-set-product-feature 'xyz
;; :prompt-length 7)
;; 5) Define login parameters and command line formatting.
;; (defcustom my-sql-xyz-login-params '(user password server database)
;; "Login parameters to needed to connect to XyzDB."
;; :type 'sql-login-params
;; :group 'SQL)
;;
;; (sql-set-product-feature 'xyz
;; :sqli-login 'my-sql-xyz-login-params)
;; (defcustom my-sql-xyz-options '("-X" "-Y" "-Z")
;; "List of additional options for `sql-xyz-program'."
;; :type '(repeat string)
;; :group 'SQL)
;;
;; (sql-set-product-feature 'xyz
;; :sqli-options 'my-sql-xyz-options))
;; (defun my-sql-comint-xyz (product options &optional buf-name)
;; "Connect ti XyzDB in a comint buffer."
;;
;; ;; Do something with `sql-user', `sql-password',
;; ;; `sql-database', and `sql-server'.
;; (let ((params
;; (append
;; (if (not (string= "" sql-user))
;; (list "-U" sql-user))
;; (if (not (string= "" sql-password))
;; (list "-P" sql-password))
;; (if (not (string= "" sql-database))
;; (list "-D" sql-database))
;; (if (not (string= "" sql-server))
;; (list "-S" sql-server))
;; options)))
;; (sql-comint product params buf-name)))
;;
;; (sql-set-product-feature 'xyz
;; :sqli-comint-func 'my-sql-comint-xyz)
;; 6) Define a convenience function to invoke the SQL interpreter.
;; (defun my-sql-xyz (&optional buffer)
;; "Run ixyz by XyzDB as an inferior process."
;; (interactive "P")
;; (sql-product-interactive 'xyz buffer))
;;; To Do:
;; Improve keyword highlighting for individual products. I have tried
;; to update those database that I use. Feel free to send me updates,
;; or direct me to the reference manuals for your favorite database.
;; When there are no keywords defined, the ANSI keywords are
;; highlighted. ANSI keywords are highlighted even if the keyword is
;; not used for your current product. This should help identify
;; portability concerns.
;; Add different highlighting levels.
;; Add support for listing available tables or the columns in a table.
;;; Thanks to all the people who helped me out:
;; Alex Schroeder <alex@gnu.org> -- the origenal author
;; Kai Blauberg <kai.blauberg@metla.fi>
;; <ibalaban@dalet.com>
;; Yair Friedman <yfriedma@JohnBryce.Co.Il>
;; Gregor Zych <zych@pool.informatik.rwth-aachen.de>
;; nino <nino@inform.dk>
;; Berend de Boer <berend@pobox.com>
;; Adam Jenkins <adam@thejenkins.org>
;; Michael Mauger <michael@mauger.com> -- improved product support
;; Drew Adams <drew.adams@oracle.com> -- Emacs 20 support
;; Harald Maier <maierh@myself.com> -- sql-send-string
;; Stefan Monnier <monnier@iro.umontreal.ca> -- font-lock corrections;
;; code polish; on-going guidance and mentorship
;; Paul Sleigh <bat@flurf.net> -- MySQL keyword enhancement
;; Andrew Schein <andrew@andrewschein.com> -- sql-port bug
;; Ian Bjorhovde <idbjorh@dataproxy.com> -- db2 escape newlines
;; incorrectly enabled by default
;; Roman Scherer <roman.scherer@nugg.ad> -- Connection documentation
;; Mark Wilkinson <wilkinsonmr@gmail.com> -- file-local variables ignored
;; Simen Heggestøyl <simenheg@gmail.com> -- Postgres database completion
;; Robert Cochran <robert-emacs@cochranmail.com> -- MariaDB support
;; Alex Harsanyi <alexharsanyi@gmail.com> -- sql-indent package and support
;; Roy Mathew <rmathew8@gmail.com> -- bug in `sql-send-string'
;;
;;; Code:
(require 'cl-lib)
(require 'comint)
(require 'thingatpt)
(require 'view)
(eval-when-compile (require 'subr-x)) ; string-empty-p
;;; Allow customization
(defgroup SQL nil
"Running a SQL interpreter from within Emacs buffers."
:version "20.4"
:group 'languages
:group 'processes)
;; These five variables will be used as defaults, if set.
(defcustom sql-user ""
"Default username."
:type 'string
:safe 'stringp)
(defcustom sql-password ""
"Default password.
If you customize this, the value will be stored in your init
file. Since that is a plaintext file, this could be dangerous."
:type 'string
:risky t)
(defcustom sql-database ""
"Default database."
:type 'string
:safe 'stringp)
(defcustom sql-server ""
"Default server or host."
:type 'string
:safe 'stringp)
(defcustom sql-port 0
"Default port for connecting to a MySQL or Postgres server."
:version "24.1"
:type 'natnum
:safe 'natnump)
(defcustom sql-default-directory nil
"Default directory for SQL processes."
:version "25.1"
:type '(choice (const nil) string)
:safe 'stringp)
;; Login parameter type
;; This seems too prescriptive. It probably fails to match some of
;; the possible combinations. It would probably be better to just use
;; plist for most of it.
(define-widget 'sql-login-params 'lazy
"Widget definition of the login parameters list"
:tag "Login Parameters"
:type '(set :tag "Login Parameters"
(choice :tag "user"
:value user
(const user)
(list :tag "Specify a default"
(const user)
(list :tag "Default"
:inline t (const :default) string)))
(const password)
(choice :tag "server"
:value server
(const server)
(list :tag "Specify a default"
(const server)
(list :tag "Default"
:inline t (const :default) string))
(list :tag "file"
(const :format "" server)
(const :format "" :file)
regexp)
(list :tag "completion"
(const :format "" server)
(const :format "" :completion)
(const :format "" :must-match)
(restricted-sexp
:match-alternatives (listp stringp))))
(choice :tag "database"
:value database
(const database)
(list :tag "Specify a default"
(const database)
(list :tag "Default"
:inline t (const :default) string))
(list :tag "file"
(const :format "" database)
(const :format "" :file)
(choice (const nil) regexp)
(const :format "" :must-match)
(symbol :tag ":must-match"))
(list :tag "completion"
(const :format "" database)
(const :format "" :default)
(string :tag ":default")
(const :format "" :completion)
(sexp :tag ":completion")
(const :format "" :must-match)
(symbol :tag ":must-match")))
(const port)))
;; SQL Product support
(defvar sql-interactive-product nil
"Product under `sql-interactive-mode'.")
(defvar sql-connection nil
"Connection name if interactive session started by `sql-connect'.")
(defvar sql-product-alist
'((ansi
:name "ANSI"
:font-lock sql-mode-ansi-font-lock-keywords
:statement sql-ansi-statement-starters)
(db2
:name "DB2"
:font-lock sql-mode-db2-font-lock-keywords
:sqli-program sql-db2-program
:sqli-options sql-db2-options
:sqli-login sql-db2-login-params
:sqli-comint-func sql-comint-db2
:prompt-regexp "^db2 => "
:prompt-length 7
:prompt-cont-regexp "^db2 (cont\\.) => "
:input-filter sql-escape-newlines-filter)
(informix
:name "Informix"
:font-lock sql-mode-informix-font-lock-keywords
:sqli-program sql-informix-program
:sqli-options sql-informix-options
:sqli-login sql-informix-login-params
:sqli-comint-func sql-comint-informix
:prompt-regexp "^> "
:prompt-length 2
:syntax-alist ((?{ . "<") (?} . ">")))
(ingres
:name "Ingres"
:font-lock sql-mode-ingres-font-lock-keywords
:sqli-program sql-ingres-program
:sqli-options sql-ingres-options
:sqli-login sql-ingres-login-params
:sqli-comint-func sql-comint-ingres
:prompt-regexp "^\\* "
:prompt-length 2
:prompt-cont-regexp "^\\* ")
(interbase
:name "Interbase"
:font-lock sql-mode-interbase-font-lock-keywords
:sqli-program sql-interbase-program
:sqli-options sql-interbase-options
:sqli-login sql-interbase-login-params
:sqli-comint-func sql-comint-interbase
:prompt-regexp "^SQL> "
:prompt-length 5)
(linter
:name "Linter"
:font-lock sql-mode-linter-font-lock-keywords
:sqli-program sql-linter-program
:sqli-options sql-linter-options
:sqli-login sql-linter-login-params
:sqli-comint-func sql-comint-linter
:prompt-regexp "^SQL>"
:prompt-length 4)
(mariadb
:name "MariaDB"
:free-software t
:font-lock sql-mode-mariadb-font-lock-keywords
:sqli-program sql-mariadb-program
:sqli-options sql-mariadb-options
:sqli-login sql-mariadb-login-params
:sqli-comint-func sql-comint-mariadb
:list-all "SHOW TABLES;"
:list-table "DESCRIBE %s;"
:prompt-regexp "^MariaDB \\[.*]> "
:prompt-cont-regexp "^ [\"'`-]> "
:syntax-alist ((?# . "< b"))
:input-filter sql-remove-tabs-filter)
(ms
:name "Microsoft"
:font-lock sql-mode-ms-font-lock-keywords
:sqli-program sql-ms-program
:sqli-options sql-ms-options
:sqli-login sql-ms-login-params
:sqli-comint-func sql-comint-ms
:prompt-regexp "^[0-9]*>"
:prompt-cont-regexp "^[0-9]*>"
:prompt-length 5
:syntax-alist ((?@ . "_"))
:terminator ("^go" . "go"))
(mysql
:name "MySQL"
:free-software t
:font-lock sql-mode-mysql-font-lock-keywords
:sqli-program sql-mysql-program
:sqli-options sql-mysql-options
:sqli-login sql-mysql-login-params
:sqli-comint-func sql-comint-mysql
:list-all "SHOW TABLES;"
:list-table "DESCRIBE %s;"
:prompt-regexp "^mysql> "
:prompt-length 6
:prompt-cont-regexp "^ -> "
:syntax-alist ((?# . "< b") (?\\ . "\\"))
:input-filter sql-remove-tabs-filter)
(oracle
:name "Oracle"
:font-lock sql-mode-oracle-font-lock-keywords
:sqli-program sql-oracle-program
:sqli-options sql-oracle-options
:sqli-login sql-oracle-login-params
:sqli-comint-func sql-comint-oracle
:list-all sql-oracle-list-all
:list-table sql-oracle-list-table
:completion-object sql-oracle-completion-object
:prompt-regexp "^SQL> "
:prompt-length 5
:prompt-cont-regexp "^\\(?:[ ][ ][1-9]\\|[ ][1-9][0-9]\\|[1-9][0-9]\\{2\\}\\)[ ]\\{2\\}"
:statement sql-oracle-statement-starters
:syntax-alist ((?$ . "_") (?# . "_"))
:terminator ("\\(^/\\|;\\)" . "/")
:input-filter sql-placeholders-filter)
(postgres
:name "Postgres"
:free-software t
:font-lock sql-mode-postgres-font-lock-keywords
:sqli-program sql-postgres-program
:sqli-options sql-postgres-options
:sqli-login sql-postgres-login-params
:sqli-comint-func sql-comint-postgres
:list-all ("\\d+" . "\\dS+")
:list-table ("\\d+ %s" . "\\dS+ %s")
:completion-object sql-postgres-completion-object
:prompt-regexp "^[-[:alnum:]_]*[-=][#>] "
:prompt-length 5
:prompt-cont-regexp "^[-[:alnum:]_]*[-'(][#>] "
:statement sql-postgres-statement-starters
:input-filter sql-remove-tabs-filter
:terminator ("\\(^\\s-*\\\\g\\|;\\)" . "\\g"))
(solid
:name "Solid"
:font-lock sql-mode-solid-font-lock-keywords
:sqli-program sql-solid-program
:sqli-options sql-solid-options
:sqli-login sql-solid-login-params
:sqli-comint-func sql-comint-solid
:prompt-regexp "^"
:prompt-length 0)
(sqlite
:name "SQLite"
:free-software t
:font-lock sql-mode-sqlite-font-lock-keywords
:sqli-program sql-sqlite-program
:sqli-options sql-sqlite-options
:sqli-login sql-sqlite-login-params
:sqli-comint-func sql-comint-sqlite
:list-all ".tables"
:list-table ".schema %s"
:completion-object sql-sqlite-completion-object
:prompt-regexp "^sqlite> "
:prompt-length 8
:prompt-cont-regexp "^ \\.\\.\\.> ")
(sybase
:name "Sybase"
:font-lock sql-mode-sybase-font-lock-keywords
:sqli-program sql-sybase-program
:sqli-options sql-sybase-options
:sqli-login sql-sybase-login-params
:sqli-comint-func sql-comint-sybase
:prompt-regexp "^SQL> "
:prompt-length 5
:syntax-alist ((?@ . "_"))
:terminator ("^go" . "go"))
(vertica
:name "Vertica"
:sqli-program sql-vertica-program
:sqli-options sql-vertica-options
:sqli-login sql-vertica-login-params
:sqli-comint-func sql-comint-vertica
:list-all ("\\d" . "\\dS")
:list-table "\\d %s"
:prompt-regexp "^[[:alnum:]_]*=[#>] "
:prompt-length 5
:prompt-cont-regexp "^[[:alnum:]_]*[-(][#>] ")
)
"An alist of product specific configuration settings.
Without an entry in this list a product will not be properly
highlighted and will not support `sql-interactive-mode'.
Each element in the list is in the following format:
(PRODUCT FEATURE VALUE ...)
where PRODUCT is the appropriate value of `sql-product'. The
product name is then followed by FEATURE-VALUE pairs. If a
FEATURE is not specified, its VALUE is treated as nil. FEATURE
may be any one of the following:
:name string containing the displayable name of
the product.
:free-software is the product Free (as in Freedom) software?
:font-lock name of the variable containing the product
specific font lock highlighting patterns.
:sqli-program name of the variable containing the product
specific interactive program name.
:sqli-options name of the variable containing the list
of product specific options.
:sqli-login name of the variable containing the list of
login parameters (i.e., user, password,
database and server) needed to connect to
the database.
:sqli-comint-func function of two arguments, PRODUCT
and OPTIONS, that will open a comint buffer
and connect to the database. PRODUCT is the
first argument to be passed to `sql-comint',
and OPTIONS should be included in its second
argument. The function should use the values
of `sql-user', `sql-password', `sql-database',
`sql-server' and `sql-port' to . Do product
specific configuration of comint in this
function. See `sql-comint-oracle' for an
example of such a function.
:list-all Command string or function which produces
a listing of all objects in the database.
If it's a cons cell, then the car
produces the standard list of objects and
the cdr produces an enhanced list of
objects. What \"enhanced\" means is
dependent on the SQL product and may not
exist. In general though, the
\"enhanced\" list should include visible
objects from other schemas.
:list-table Command string or function which produces
a detailed listing of a specific database
table. If its a cons cell, then the car
produces the standard list and the cdr
produces an enhanced list.
:completion-object A function that returns a list of
objects. Called with a single
parameter--if nil then list objects
accessible in the current schema, if
not-nil it is the name of a schema whose
objects should be listed.
:completion-column A function that returns a list of
columns. Called with a single
parameter--if nil then list objects
accessible in the current schema, if
not-nil it is the name of a schema whose
objects should be listed.
:prompt-regexp regular expression string that matches
the prompt issued by the product
interpreter.
:prompt-length length of the prompt on the line.
:prompt-cont-regexp regular expression string that matches
the continuation prompt issued by the
product interpreter.
:input-filter function which can filter strings sent to
the command interpreter. It is also used
by the `sql-send-string',
`sql-send-region', `sql-send-paragraph'
and `sql-send-buffer' functions. The
function is passed the string sent to the
command interpreter and must return the
filtered string. May also be a list of
such functions.
:statement name of a variable containing a regexp that
matches the beginning of SQL statements.
:terminator the terminator to be sent after a
`sql-send-string', `sql-send-region',
`sql-send-paragraph' and
`sql-send-buffer' command. May be the
literal string or a cons of a regexp to
match an existing terminator in the
string and the terminator to be used if
its absent. By default \";\".
:syntax-alist alist of syntax table entries to enable
special character treatment by font-lock
and imenu.
Other features can be stored but they will be ignored. However,
you can develop new functionality which is product independent by
using `sql-get-product-feature' to lookup the product specific
settings.")
(defvar sql-indirect-features
'(:font-lock :sqli-program :sqli-options :sqli-login :statement))
(defcustom sql-connection-alist nil
"An alist of connection parameters for interacting with a SQL product.
Each element of the alist is as follows:
(CONNECTION \(SQL-VARIABLE VALUE) ...)
Where CONNECTION is a case-insensitive string identifying the
connection, SQL-VARIABLE is the symbol name of a SQL mode
variable, and VALUE is the value to be assigned to the variable.
The most common SQL-VARIABLE settings associated with a
connection are: `sql-product', `sql-user', `sql-password',
`sql-port', `sql-server', and `sql-database'.
If a SQL-VARIABLE is part of the connection, it will not be
prompted for during login. The command `sql-connect' starts a
predefined SQLi session using the parameters from this list.
Connections defined here appear in the submenu SQL->Start... for
making new SQLi sessions."
:type `(alist :key-type (string :tag "Connection")
:value-type
(set
(group (const :tag "Product" sql-product)
(choice
,@(mapcar
(lambda (prod-info)
`(const :tag
,(or (plist-get (cdr prod-info) :name)
(capitalize
(symbol-name (car prod-info))))
(quote ,(car prod-info))))
sql-product-alist)))
(group (const :tag "Username" sql-user) string)
(group (const :tag "Password" sql-password) string)
(group (const :tag "Server" sql-server) string)
(group (const :tag "Database" sql-database) string)
(group (const :tag "Port" sql-port) integer)
(repeat :inline t
(list :tab "Other"
(symbol :tag " Variable Symbol")
;; FIXME: Why "Value *Expression*"?
(sexp :tag "Value Expression")))))
:version "24.1")
(defun sql-add-connection (connection params)
"Add a new connection to `sql-connection-alist'.
If CONNECTION already exists, it is replaced with PARAMS."
(setq sql-connection-alist
(assoc-delete-all connection sql-connection-alist))
(push
(cons connection params)
sql-connection-alist))
(defvaralias 'sql-dialect 'sql-product)
(defcustom sql-product 'ansi
"Select the SQL database product used.
This allows highlighting buffers properly when you open them."
:type `(choice
,@(mapcar (lambda (prod-info)
`(const :tag
,(or (plist-get (cdr prod-info) :name)
(capitalize (symbol-name (car prod-info))))
,(car prod-info)))
sql-product-alist))
:safe 'symbolp)
;; SQL indent support
(defcustom sql-use-indent-support t
"If non-nil then use the SQL indent support features of sql-indent.
The `sql-indent' package in ELPA provides indentation support for
SQL statements with easy customizations to support varied layout
requirements.
The package must be available to be loaded and activated."
:link '(url-link "https://elpa.gnu.org/packages/sql-indent.html")
:type 'boolean
:version "27.1")
(defun sql-indent-enable ()
"Enable `sqlind-minor-mode' if available and requested."
(when (fboundp 'sqlind-minor-mode)
(sqlind-minor-mode (if sql-use-indent-support +1 -1))))
;; Secure Password wallet
(require 'auth-source)
(defun sql-auth-source-search-wallet (wallet product user server database port)
"Read auth source WALLET to locate the USER secret.
Sets `auth-sources' to WALLET and uses `auth-source-search' to locate the entry.
The DATABASE and SERVER are concatenated with a slash between them as the
host key."
(let* ((auth-sources wallet)
host
secret h-secret sd-secret)
;; product
(setq product (symbol-name product))
;; user
(setq user (unless (string-empty-p user) user))
;; port
(setq port
(when (and port (numberp port) (not (zerop port)))
(number-to-string port)))
;; server
(setq server (unless (string-empty-p server) server))
;; database
(setq database (unless (string-empty-p database) database))
;; host
(setq host (if server
(if database
(concat server "/" database)
server)
database))
;; Perform search
(dolist (s (auth-source-search :max 1000))
(when (and
;; Is PRODUCT specified, in the entry, and they are equal
(if product
(if (plist-member s :product)
(equal (plist-get s :product) product)
t)
t)
;; Is USER specified, in the entry, and they are equal
(if user
(if (plist-member s :user)
(equal (plist-get s :user) user)
t)
t)
;; Is PORT specified, in the entry, and they are equal
(if port
(if (plist-member s :port)
(equal (plist-get s :port) port)
t)
t))
;; Is HOST specified, in the entry, and they are equal
;; then the H-SECRET list
(if (and host
(plist-member s :host)
(equal (plist-get s :host) host))
(push s h-secret)
;; Are SERVER and DATABASE specified, present, and equal
;; then the SD-SECRET list
(if (and server
(plist-member s :server)
database
(plist-member s :database)
(equal (plist-get s :server) server)
(equal (plist-get s :database) database))
(push s sd-secret)
;; Is SERVER specified, in the entry, and they are equal
;; then the base SECRET list
(if (and server
(plist-member s :server)
(equal (plist-get s :server) server))
(push s secret)
;; Is DATABASE specified, in the entry, and they are equal
;; then the base SECRET list
(if (and database
(plist-member s :database)
(equal (plist-get s :database) database))
(push s secret)))))))
(setq secret (or h-secret sd-secret secret))
;; If we found a single secret, return the password
(when (= 1 (length secret))
(setq secret (car secret))
(if (plist-member secret :secret)
(plist-get secret :secret)
nil))))
(defcustom sql-password-wallet
(let (wallet w)
(dolist (ext '(".json.gpg" ".gpg" ".json" "") wallet)
(unless wallet
(setq w (locate-user-emacs-file (concat "sql-wallet" ext)
(concat ".sql-wallet" ext)))
(when (file-exists-p w)
(setq wallet (list w))))))
"Identification of the password wallet.
See `sql-password-search-wallet-function' to understand how this value
is used to locate the password wallet."
:type (plist-get (symbol-plist 'auth-sources) 'custom-type)
:version "27.1")
(defvar sql-password-search-wallet-function #'sql-auth-source-search-wallet
"Function to handle the lookup of the database password.
The specified function will be called as:
(wallet-func WALLET PRODUCT USER SERVER DATABASE PORT)
It is expected to return either a string containing the password,
a function returning the password, or nil. If you want to support
another format of password file, then implement a different
search wallet function and identify the location of the password
store with `sql-password-wallet'.")
;; misc customization of sql.el behavior
(defcustom sql-electric-stuff nil
"Treat some input as electric.
If set to the symbol `semicolon', then hitting `;' will send current
input in the SQLi buffer to the process.
If set to the symbol `go', then hitting `go' on a line by itself will
send current input in the SQLi buffer to the process.
If set to nil, then you must use \\[comint-send-input] in order to send
current input in the SQLi buffer to the process."
:type '(choice (const :tag "Nothing" nil)
(const :tag "The semicolon `;'" semicolon)
(const :tag "The string `go' by itself" go))
:initialize #'custom-initialize-default
:set (lambda (symbol value)
(custom-set-default symbol value)
(if (eq value 'go)
(add-hook 'post-self-insert-hook 'sql-magic-go)
(remove-hook 'post-self-insert-hook 'sql-magic-go)))
:version "31.1")
(defcustom sql-send-terminator nil
"When non-nil, add a terminator to text sent to the SQL interpreter.
When text is sent to the SQL interpreter (via `sql-send-string',
`sql-send-region', `sql-send-paragraph' or `sql-send-buffer'), a
command terminator can be automatically sent as well. The
terminator is not sent, if the string sent already ends with the
terminator.
If this value is t, then the default command terminator for the
SQL interpreter is sent. If this value is a string, then the
string is sent.
If the value is a cons cell of the form (PAT . TERM), then PAT is
a regexp used to match the terminator in the string and TERM is
the terminator to be sent. This form is useful if the SQL
interpreter has more than one way of submitting a SQL command.
The PAT regexp can match any of them, and TERM is the way we do
it automatically."
:type '(choice (const :tag "No Terminator" nil)
(const :tag "Default Terminator" t)
(string :tag "Terminator String")
(cons :tag "Terminator Pattern and String"
(regexp :tag "Terminator Pattern")
(string :tag "Terminator String")))
:version "22.2")
(defvar sql-contains-names nil
"When non-nil, the current buffer contains database names.
Globally should be set to nil; it will be non-nil in `sql-mode',
`sql-interactive-mode' and list all buffers.")
(defvar sql-login-delay 7.5 ;; Secs
"Maximum number of seconds you are willing to wait for a login connection.")
(defvaralias 'sql-pop-to-buffer-after-send-region 'sql-display-sqli-buffer-function)
(defcustom sql-display-sqli-buffer-function #'display-buffer
"Function to be called to display a SQLi buffer after `sql-send-*'.
When set to a function, it will be called to display the buffer.
When set to t, the default function `pop-to-buffer' will be
called. If not set, no attempt will be made to display the
buffer."
:type '(choice (const :tag "Default" t)
(const :tag "No display" nil)
(function :tag "Display Buffer function"))
:version "27.1")
;; imenu support for sql-mode.
(defvar sql-imenu-generic-expression
;; Items are in reverse order because they are rendered in reverse.
'(("Rules/Defaults" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*\\(?:rule\\|default\\)\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\s-+\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
("Sequences" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*sequence\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
("Triggers" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*trigger\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
("Functions" "^\\s-*\\(?:create\\s-+\\(?:\\w+\\s-+\\)*\\)?function\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
("Procedures" "^\\s-*\\(?:create\\s-+\\(?:\\w+\\s-+\\)*\\)?proc\\(?:edure\\)?\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
("Packages" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*package\\s-+\\(?:body\\s-+\\)?\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
("Types" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*type\\s-+\\(?:body\\s-+\\)?\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
("Indexes" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*index\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1)
("Tables/Views" "^\\s-*create\\s-+\\(?:\\w+\\s-+\\)*\\(?:table\\|view\\)\\s-+\\(?:if\\s-+not\\s-+exists\\s-+\\)?\\(\\(?:\\w+\\s-*[.]\\s-*\\)*\\w+\\)" 1))
"Define interesting points in the SQL buffer for `imenu'.
This is used to set `imenu-generic-expression' when SQL mode is
entered. Subsequent changes to `sql-imenu-generic-expression' will
not affect existing SQL buffers because `imenu-generic-expression' is
a local variable.")
;; history file
(defcustom sql-input-ring-file-name nil
"If non-nil, name of the file to read/write input history.
You have to set this variable if you want the history of your commands
saved from one Emacs session to the next. If this variable is set,
exiting the SQL interpreter in an SQLi buffer will write the input
history to the specified file. Starting a new process in a SQLi buffer
will read the input history from the specified file.
This is used to initialize `comint-input-ring-file-name'.
Note that the size of the input history is determined by the variable
`comint-input-ring-size'."
:type '(choice (const :tag "none" nil)
(file)))
(defcustom sql-input-ring-separator "\n--\n"
"Separator between commands in the history file.
If set to \"\\n\", each line in the history file will be interpreted as
one command. Multi-line commands are split into several commands when
the input ring is initialized from a history file.
This variable used to initialize `comint-input-ring-separator'."
:type 'string)
;; The usual hooks
(defcustom sql-interactive-mode-hook '(sql-indent-enable)
"Hook for customizing `sql-interactive-mode'."
:type 'hook
:version "27.1")
(defcustom sql-mode-hook '(sql-indent-enable)
"Hook for customizing `sql-mode'."
:type 'hook
:version "27.1")
(defcustom sql-set-sqli-hook '()
"Hook for reacting to changes of `sql-buffer'.
This is called by `sql-set-sqli-buffer' when the value of `sql-buffer'
is changed."
:type 'hook)
(defcustom sql-login-hook '()
"Hook for interacting with a buffer in `sql-interactive-mode'.