]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/sql-complete.el
13765bcd981e67272f86e63006d0c2fba489feeb
[.emacs.d.git] / emacs / sql-complete.el
1 ;;; sql-complete.el --- provide completion for tables and columns
2
3 ;; Copyright (C) 2001  Free Software Foundation, Inc.
4
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Alex Schroeder <alex@gnu.org>
7 ;; Version: 0.0.1
8 ;; Keywords: comm languages processes
9
10 ;; This file is NOT part of GNU Emacs.
11
12 ;; This is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; This is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Trying to provide a framework for completion that will eventually
30 ;; make it into sql.el.
31
32  
33
34 ;;; Code:
35
36 (require 'sql)
37
38 (defcustom sql-oracle-data-dictionary
39   "select '(\"'||table_name||'\" \"'||column_name||'\")'
40    from user_tab_columns 
41    order by table_name;"
42   "SQL Statement to determine all tables and columns."
43   :group 'SQL
44   :type 'string)
45
46 ;; backends
47
48 (defun sql-data-dictionary (statement)
49   "Return table and columns from the Oracle Data Dictionary using SQL.
50 STATEMENT must be a SQL statement that returns the data dictionary
51 one column per line.  Each line must look like this:
52
53 \(\"table-name\" \"column-name\")
54
55 Any lines not looking like this will be skipped to allow for column
56 headers and other fancy markup.
57
58 This currently depends very much on a good `comint-prompt-regexp'."
59   (when (null sql-buffer)
60     (error "No SQLi buffer available"))
61   (save-excursion
62     (set-buffer sql-buffer)
63     (let (result end)
64       (comint-simple-send sql-buffer statement)
65       (comint-previous-prompt 1)
66       (while (= 0 (forward-line 1))
67         (message "%S" (point))
68         (when (looking-at "^(.*)$")
69           (let* ((entry (car (read-from-string (match-string 0))))
70                  (table (car entry))
71                  (column (cadr entry))
72                  (item (cdr (assoc table result))))
73             (if item
74                 (nconc item (list column))
75               (setq result (append (list entry) result))))))
76       result)))
77
78 ;; framework
79
80 (defvar sql-data-dictionary nil
81   "The data dictionary to use for completion.
82 Each element of the list has the form
83 \(TABLE COLUMN1 COLUMN2 ...)")
84
85 (defun sql-oracle-data-dictionary ()
86   (interactive)
87   ;; FIXME No cleanup
88   (setq sql-data-dictionary
89         (sql-data-dictionary sql-oracle-data-dictionary)))
90
91 (defun sql-complete ()
92   (interactive)
93   (let ((completions (apply 'append sql-data-dictionary)))
94     (comint-dynamic-simple-complete 
95      (comint-word "A-Za-z_")
96      completions)))
97
98 (defun sql-complete-table ()
99   (interactive)
100   (let ((completions (mapcar 'car sql-data-dictionary)))
101     (comint-dynamic-simple-complete 
102      (comint-word "A-Za-z_")
103      completions)))
104
105 ;;; sql-complete.el ends here