-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql-complete.el
105 lines (81 loc) · 3.04 KB
/
sql-complete.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
;;; sql-complete.el --- provide completion for tables and columns
;; Copyright (C) 2001 Free Software Foundation, Inc.
;; Author: Alex Schroeder <alex@gnu.org>
;; Maintainer: Alex Schroeder <alex@gnu.org>
;; Version: 0.0.1
;; Keywords: comm languages processes
;; This file is NOT part of GNU Emacs.
;; This 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 2, or (at your option)
;; any later version.
;; This 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; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; Trying to provide a framework for completion that will eventually
;; make it into sql.el.
;;; Code:
(require 'sql)
(defcustom sql-oracle-data-dictionary
"select '(\"'||table_name||'\" \"'||column_name||'\")'
from user_tab_columns
order by table_name;"
"SQL Statement to determine all tables and columns."
:group 'SQL
:type 'string)
;; backends
(defun sql-data-dictionary (statement)
"Return table and columns from the Oracle Data Dictionary using SQL.
STATEMENT must be a SQL statement that returns the data dictionary
one column per line. Each line must look like this:
\(\"table-name\" \"column-name\")
Any lines not looking like this will be skipped to allow for column
headers and other fancy markup.
This currently depends very much on a good `comint-prompt-regexp'."
(when (null sql-buffer)
(error "No SQLi buffer available"))
(save-excursion
(set-buffer sql-buffer)
(let (result end)
(comint-simple-send sql-buffer statement)
(comint-previous-prompt 1)
(while (= 0 (forward-line 1))
(message "%S" (point))
(when (looking-at "^(.*)$")
(let* ((entry (car (read-from-string (match-string 0))))
(table (car entry))
(column (cadr entry))
(item (cdr (assoc table result))))
(if item
(nconc item (list column))
(setq result (append (list entry) result))))))
result)))
;; framework
(defvar sql-data-dictionary nil
"The data dictionary to use for completion.
Each element of the list has the form
\(TABLE COLUMN1 COLUMN2 ...)")
(defun sql-oracle-data-dictionary ()
(interactive)
;; FIXME No cleanup
(setq sql-data-dictionary
(sql-data-dictionary sql-oracle-data-dictionary)))
(defun sql-complete ()
(interactive)
(let ((completions (apply 'append sql-data-dictionary)))
(comint-dynamic-simple-complete
(comint-word "A-Za-z_")
completions)))
(defun sql-complete-table ()
(interactive)
(let ((completions (mapcar 'car sql-data-dictionary)))
(comint-dynamic-simple-complete
(comint-word "A-Za-z_")
completions)))
;;; sql-complete.el ends here