home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / modes / sql.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  22.5 KB  |  660 lines

  1. ;From ark1!uakari.primate.wisc.edu!samsung!cs.utexas.edu!tut.cis.ohio-state.edu!indetech.com!lrs Mon Nov  6 13:10:43 1989
  2. ;Article 646 of gnu.emacs
  3. ;Path: ark1!uakari.primate.wisc.edu!samsung!cs.utexas.edu!tut.cis.ohio-state.edu!indetech.com!lrs
  4. ;>From lrs@indetech.com (Lynn Slater)
  5. ;Newsgroups: gnu.emacs
  6. ;Subject: SQL mode
  7. ;Message-ID: <m0gNBfg-0000FAC@fire.indetech.com>
  8. ;Date: 3 Nov 89 21:42:00 GMT
  9. ;Sender: daemon@tut.cis.ohio-state.edu
  10. ;Distribution: gnu
  11. ;Organization: GNUs Not Usenet
  12. ;Lines: 644
  13. ;
  14. ;Enclosed is SQL mode. This mode provides both for editing of sql files
  15. ;and for the transmittal to another process of sql statements.  This later
  16. ;ability provides a significant functionality improvement over either 'isql'
  17. ;(from Sybase) or 'sqlplus' (from Oracle).
  18. ;
  19. ;In either case, you start the vendor supplied sql interpretor in its own
  20. ;buffer (a shell buffer will do), position the cursor on or near the sql you
  21. ;wish transmitted, and use one of the transmittal bindings listed below.
  22. ;Unlike the vendor supplied sql interpretors, you will have a complete set
  23. ;of editing capabilities and can work on more than the most recient
  24. ;expression.  All capabilities of the vendor supplied interpretors are
  25. ;retained as they are doing the actual work in the subshell.
  26. ;
  27. ;This mode is very useful for incrimental development of sql scripts. Once
  28. ;your expressions are developed with this mode, the file can be saved and
  29. ;run in a batch manner.
  30. ;
  31. ;===============================================================
  32. ;Lynn Slater -- lrs@indetech.com or {sun, ames, pacbell}!indetech!lrs
  33. ;42075 Lawrence Place, Fremont Ca 94538
  34. ;Office (415) 438-2048; Home (415) 796-4149; Fax (415) 438-2034
  35. ;===============================================================
  36. ;
  37. ;-*- File: ~/local/sql.el
  38. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  39. ;; sql.el --- sql mode and support to send statements to sql interpretors.
  40. ;; Author          : Lynn R Slater
  41. ;; Created On      : Thu Dec 10 15:20:26 1987
  42. ;; Last Modified By: Lynn Slater
  43. ;; Last Modified On: Fri Nov  3 13:40:22 1989
  44. ;; Update Count    : 97
  45. ;; Status          : Beta areleased
  46. ;;
  47. ;; History         
  48. ;; 3-Nov-1989        Lynn Slater    
  49. ;;    Last Modified: Fri Nov  3 13:32:34 1989 #93 (Lynn Slater)
  50. ;;    Cleaned up for distribution.
  51. ;; 15-Jun-1989        Lynn Slater    
  52. ;;    Last Modified: Thu Jun 15 10:18:23 1989 #69 (Lynn Slater)
  53. ;;    added oracle support
  54. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  55.  
  56. ;; Copyright 1989 Lynn Randolph Slater, Jr. (lrs@indetech.com)
  57.  
  58. ;; This file may become part of GNU Emacs.
  59. ;; GNU Emacs is distributed in the hope that it will be useful,
  60. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  61. ;; accepts responsibility to anyone for the consequences of using it
  62. ;; or for whether it serves any particular purpose or works at all,
  63. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  64. ;; License for full details.
  65.  
  66. ;; Everyone is granted permission to copy, modify and redistribute
  67. ;; GNU Emacs, but only under the conditions described in the
  68. ;; GNU Emacs General Public License.   A copy of this license is
  69. ;; supposed to have been given to you along with GNU Emacs so you
  70. ;; can know your rights and responsibilities.  It should be in a
  71. ;; file named COPYING.  Among other things, the copyright notice
  72. ;; and this notice must be preserved on all copies.
  73.  
  74. (provide 'sql)
  75. (require 'shell)
  76.  
  77. ;; See the sql mode help for instructions.
  78.  
  79. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  80. ;; Customization 
  81. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  82. (defvar sql-buffer-name "*shell*"
  83.   "Name of the buffer to which the sql commands send data.
  84.    Is typically '*shell*' or '*<host>-telnet'")
  85.  
  86. (defvar sql-seperator-regexp
  87.   "^[ \t]*go[ \t\n]"              ;; For Sybase "isql" program
  88.   ;; ";[ \t]*$"                   ;; For Oracle "sqlplus" program
  89.   "Regexp used to seperate sql forms. It's value should differ from vendor
  90.    to vendor.")
  91.  
  92. (defun sql-set-buffer ()
  93.   "Sets the buffer to which sql statements will be sent."
  94.   (interactive)
  95.   (if (get-buffer-process (current-buffer))
  96.       (progn
  97.     (setq sql-buffer-name (buffer-name (current-buffer)))
  98.     (message "SQL statements will now sent to buffer %s" sql-buffer-name))
  99.     (error "This buffer is not talking to anybody!")
  100.     ))
  101.  
  102. ;;;;;;;
  103.  
  104. (defun echo-in-buffer (buffer-name string &optional force-display)
  105.   "Displays string in the named buffer, creating the buffer if needed.
  106.    If force-display is true, the buffer will appear if not already shown."
  107.   ;; lrs April 23, 1987
  108.   (let ((buffer (get-buffer-create buffer-name)))
  109.     (if force-display (display-buffer buffer))
  110.     ;; there is probably a better way to do this
  111.     (set-buffer buffer)
  112.     (end-of-buffer)
  113.     (insert string)
  114.     (if force-display
  115.     (set-window-point (get-buffer-window buffer-name) (point-max)))))
  116.  
  117. (defmacro sit-pause ()
  118.   (if (fboundp 'sit-for-millisecs)
  119.       (sit-for-millisecs 400)
  120.     (sit-for 1)))
  121.  
  122. (defun flash-region (min max)
  123.   "Temporarely moves the curser to the endpoints of a region."
  124.   ; should probably be added to emacs, is usefull all over.
  125.   ; April 23, 1987. lrs of Silvar-Lisco @sun!silvlis!lrs
  126.   (interactive "r")
  127.   (save-excursion
  128.     (if (not (eq (point) min))
  129.     (progn
  130.       (goto-char min)
  131.       (sit-pause)))
  132.     (if (not (eq (point) max))
  133.     (progn
  134.       (goto-char max)
  135.       (sit-pause)))
  136.     ))
  137.  
  138. ;;;;;;;
  139. (defun sql-verify-buffer ()
  140.   "Generates reasonable error messages abouut the sql connection"
  141.   (if (not (get-buffer sql-buffer-name))
  142.       (error "The buffer %s does not exist! Try M-x sql-set-buffer" sql-buffer-name))
  143.   (if (not (get-buffer-process sql-buffer-name))
  144.       (error "The buffer %s is not talking to anybody!" sql-buffer-name)))
  145.   
  146. (defun sql-send-strings (strings)
  147.   ;; To help avoid losing track of what sql is doing, echo.
  148.   ;; Cause the buffer to be always displayed and a hint given.
  149.   (sql-verify-buffer)
  150.   (echo-in-buffer sql-buffer-name (apply 'concat strings))
  151.   ;;(lisp-goto-output-point)
  152.   (let ((string (apply 'concat strings))
  153.     (process  (get-buffer-process sql-buffer-name)))
  154.     (send-string process (concat string "\n"))
  155.     (if (eq (current-buffer) (process-buffer process))
  156.     (set-marker (process-mark process) (point)))
  157.     ))
  158.  
  159. (defun sql-send-region (start end)
  160.   "Send the region to the SQL process."
  161.   (interactive "r")
  162.   (save-excursion
  163.     (sql-send-strings (list (buffer-substring start end))))
  164.   (flash-region start end)        ; April 23, 1987. @sun!silvlis!lrs
  165.   )
  166.  
  167. (defun sql-left ()
  168.   "Send the expression on this line and to the left of `point' to the SQL
  169.    process." 
  170.   (interactive)
  171.     (sql-send-region
  172.       (progn (beginning-of-line) (point)) (progn (end-of-line)
  173.                          (point))))
  174.  
  175. (defun sql-mark-current ()
  176.   "Marks as the region the current sql form found by matching pairs of the items
  177.    listed in 'sql-seperater-regexp. Moves to end of region."
  178.   (interactive)
  179.   (beginning-of-line)
  180.   (or (and (re-search-backward sql-seperator-regexp nil t)
  181.        (goto-char (match-end 0)))
  182.       (and (re-search-backward "^[ \t]*$" nil t) (goto-char (match-end 0)))
  183.       (goto-char (point-min)))
  184.   (skip-chars-forward " \t\n")
  185.   (set-mark (point))
  186.   (re-search-forward sql-seperator-regexp)
  187.   (goto-char (match-end 0))
  188.   )
  189.  
  190. (defun sql-send-current (arg)
  191.   "Send the current sql form to the sql process named by sql-buffer-name.
  192.    Marks what was sent.
  193.    If given a numeric argument, sends that many expressions."
  194.   (interactive "p")
  195.   (sql-mark-current)
  196.   (call-interactively 'sql-send-region)
  197.   (forward-line 1)
  198.   (if (> arg 1) (ss (1- (prefix-numeric-value arg)))))
  199.  
  200. (defun sql-goto-error (n)
  201.   "moves to the n'th line in the region"
  202.   (interactive "NLine number of error: ")
  203.   (goto-char (region-beginning))
  204.   (forward-line (1- n)))
  205.  
  206. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  207. ;; Format Conversions
  208. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  209. (defun sql-convert-sybase-to-oracle ()
  210.   "Converts sql seperated by 'go' statements sql seperated by semi-colons.
  211.    Also converts some other sybase vendor specific stuff into oracle vendor
  212.    specific stuff. I.E.
  213.       datetime -> date
  214.       money -> number
  215.       tinyint -> number
  216.       etc."
  217.   (interactive)
  218.   (save-excursion
  219.     (goto-char (point-min))
  220.     (if (not (eq major-mode 'sql-mode)) (sql-mode))
  221.     (replace-regexp "^[ \t]*go[ \t]*$" ";")
  222.  
  223.     (goto-char (point-min))
  224.     (replace-string "datetime" "date")
  225.  
  226.     (goto-char (point-min))
  227.     (replace-string "money" "number /* money */")
  228.    
  229.     (goto-char (point-min))
  230.     (replace-string "tinyint" "number /* tinyintes */")
  231.    
  232.     (goto-char (point-min))
  233.     (replace-string "clustered" "/* clustered */")
  234.  
  235.     (goto-char (point-min))
  236.     (replace-string "nonclustered" " /* nonclustered */")
  237.  
  238.     (goto-char (point-min))
  239.     (while (and (re-search-forward "^[ \t]*$" nil t)
  240.         (not (eobp)))
  241.       (forward-line -1)
  242.       (back-to-indentation)
  243.       (let ((column (current-column)))
  244.     (forward-line 1)
  245.     (if (not (= column 0))
  246.         (progn
  247.           (kill-region (point) (progn (end-of-line) (point)))
  248.           (insert (make-string column ? ) "/* Blank Line */")
  249.           )
  250.       (forward-line 1)
  251.       )))
  252.     )
  253.   (message "Oracle conversion complete"))
  254.          
  255.  
  256. (defun sql-insert-gos ()
  257.   "Inserts 'go' statements between each apparent block of sql code.
  258.    Is good for making a isql script program out of plain sql."
  259.   (interactive)
  260.   (while (not (eobp))
  261.     (forward-line 1)
  262.     (if (and (looking-at "[a-z]") (not (looking-at "go")))
  263.     (progn (insert "go\n")
  264.            ;;(forward-line 1)
  265.            ))
  266.     ))
  267.  
  268. (defun sql-insert-semi-colons ()
  269.   "Inserts ';'s between each apparent block of sql code.
  270.    Is good for making a isql script program out of plain sql."
  271.   (interactive)
  272.   (while (not (eobp))
  273.     (forward-line 1)
  274.     (if (and (looking-at "[a-z]") (not (looking-at ";")))
  275.     (progn (insert ";\n")
  276.            ;;(forward-line 1)
  277.            ))
  278.     ))
  279.  
  280. (defun sql-make-deinstall-script
  281.   "Turns an install script into a deinstall script by converting create
  282.    commands to drop commands.  Is particularly usefukl with stored
  283.    procedures in Sybase."
  284.   (interactive)
  285.   (keep-lines "^[ \\t]*create")
  286.   (query-replace "create" "drop" nil)
  287.   (delete-matching-lines "clustered")
  288.   (query-replace " as" "" nil)
  289.   (make-header)
  290.   (write-file "/flan/ada/scripts/de-installflan")
  291.   )
  292.  
  293. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  294. ;; Mass Actions not possiblew in SQL
  295. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  296. (defun sql-drop-tables (query)
  297.   "Drops all the tables named by QUERY.  Is handy as sql does not let the
  298.    table name be passed as an argument."
  299.   (interactive (list (read-string "query: "
  300.                   (format "select TABLE_NAME from all_tables where OWNER = '%s';" (user-login-name)))))
  301.   (sql-verify-buffer)
  302.   (switch-to-buffer sql-buffer-name)
  303.   (goto-char (point-max))
  304.   (let ((here (point))
  305.     (max)
  306.     table
  307.     )
  308.     ;; Send the query
  309.     (insert query)
  310.     (shell-send-input)
  311.     (message "am awaiting sql to list tables")
  312.     (sit-for 5)
  313.     (goto-char (point-max))
  314.     (setq max (point))
  315.     (re-search-backward
  316.      "^\\(\\-+\\|no records selected\\) $") ;; Regexp may be oracle specific
  317.     (forward-line 1)
  318.  
  319.     (while (re-search-forward "^\\([a-zA-Z][a-zA-Z0-9_]+\\) $" nil t)
  320.       (save-excursion
  321.     (goto-char (point-max))
  322.     (setq table (buffer-substring (match-beginning 1) (match-end 1)))
  323.     (insert "drop table " table)
  324.     (shell-send-input)
  325.     (insert ";")
  326.     (shell-send-input)
  327.     (sit-for 4)
  328.     (message "am giving sql time to drop table '%s'" table)))    
  329.     )
  330.   (message "done"))
  331.  
  332. (defun sybase-drop-non-system-procs ()
  333.   "Sybase specific drop of all stored procedures not owned by system.
  334.    Is useful to clear out a database.
  335.    (Oracle has no stored procedures and this function is N/A.)"
  336.   (interactive)
  337.   (sql-verify-buffer)
  338.   (switch-to-buffer sql-buffer-name)
  339.   (goto-char (point-max))
  340.   (let ((here (point))
  341.     (max)
  342.     )
  343.     (insert "select sysobjects.name, sysusers.name, sysobjects.uid from sysobjects, sysusers where not sysobjects.uid = 1 and type = \"P \" and sysobjects.uid = sysusers.uid")
  344.     (shell-send-input)
  345.     (insert "go")
  346.     (shell-send-input)
  347.     (sit-for 5)
  348.     (goto-char (point-max))
  349.     (setq max (point))
  350.  
  351.     (goto-char here)
  352.     (while (re-search-forward "^ \\([^ \t]+\\)[ \t]+\\([^ \t]+\\)" nil t)
  353.       (save-excursion
  354.     (goto-char (point-max))
  355.     (insert "drop proc "
  356.         (buffer-substring (match-beginning 2) (match-end 2))
  357.         "."
  358.         (buffer-substring (match-beginning 1) (match-end 1))
  359.         )
  360.     (shell-send-input)
  361.     (insert "go")
  362.     (shell-send-input)
  363.     (sit-for 1)))    
  364.     ))
  365.  
  366. (defun sybase-drop-non-system-types ()
  367.   "Sybase specific drop of all types not owned by system.
  368.    Is useful to clear out a database."
  369.   (interactive)
  370.   (sql-verify-buffer)
  371.   (switch-to-buffer sql-buffer-name)
  372.   (goto-char (point-max))
  373.   (let ((here (point))
  374.     (max)
  375.     )
  376.     (insert "select uid, name from systypes where uid != 1")
  377.     (shell-send-input)
  378.     (insert "go")
  379.     (shell-send-input)
  380.     (sit-for 5)
  381.     (goto-char (point-max))
  382.     (setq max (point))
  383.  
  384.     (goto-char here)
  385.     (while (re-search-forward "^ +\\([^ \t]+\\)[ \t]+\\([^ \t]+\\)" nil t)
  386.       (save-excursion
  387.     (goto-char (point-max))
  388.     (insert "sp_droptype "
  389.         (buffer-substring (match-beginning 2) (match-end 2))
  390.         )
  391.     (shell-send-input)
  392.     (insert "go")
  393.     (shell-send-input)
  394.     (sit-for 1)))    
  395.     ))
  396.  
  397. (defun sybase-drop-non-system-tables ()
  398.   "Sybase specific drop of all tables not owned by system.
  399.    Is useful to clear out a database."
  400.   (interactive)
  401.   (sql-verify-buffer)
  402.   (switch-to-buffer sql-buffer-name)
  403.   (goto-char (point-max))
  404.   (let ((here (point))
  405.     (max)
  406.     )
  407.     (insert "select sysobjects.name, sysusers.name, sysobjects.uid from sysobjects, sysusers where not sysobjects.uid = 1 and type = \"U \" and sysobjects.uid = sysusers.uid")
  408.     (shell-send-input)
  409.     (insert "go")
  410.     (shell-send-input)
  411.     (sit-for 5)
  412.     (goto-char (point-max))
  413.     (setq max (point))
  414.  
  415.     (goto-char here)
  416.     (while (re-search-forward "^ \\([^ \t]+\\)[ \t]+\\([^ \t]+\\)" nil t)
  417.       (save-excursion
  418.     (goto-char (point-max))
  419.     (insert "drop table "
  420.         (buffer-substring (match-beginning 2) (match-end 2))
  421.         "."
  422.         (buffer-substring (match-beginning 1) (match-end 1))
  423.         )
  424.     (shell-send-input)
  425.     (insert "go")
  426.     (shell-send-input)
  427.     (sit-for 1)))    
  428.     ))
  429.  
  430. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  431. ;; SQL mode
  432. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  433. (defvar sql-mode-abbrev-table nil
  434.   "Abbrev table in use in Sql-mode buffers.")
  435.  
  436. (defvar sql-mode-map nil
  437.   "Keymap used in Sql mode.")
  438.  
  439. (defun sql-mode ()
  440.   "This mode provides both for editing of sql files and for the transmittal to
  441. another process of sql statements.  This later ability provides a
  442. significant functionality improvement over either 'isql' (from Sybase) or
  443. 'sqlplus' (from Oracle).
  444.  
  445. In either case, you start the vendor supplied sql interpretor in its own
  446. buffer (a shell buffer will do), position the cursor on or near the sql you
  447. wish transmitted, and use one of the transmittal bindings listed below.
  448. Unlike the vendor supplied sql interpretors, you will have a complete set
  449. of editing capabilities and can work on more than the most recient
  450. expression.  All capabilities of the vendor supplied interpretors are
  451. retained as they are doing the actual work in the subshell.
  452.  
  453. This mode is very useful for incrimental development of sql scripts. Once
  454. your expressions are developed with this mode, the file can be saved and
  455. run in a batch manner.
  456.  
  457. Control-C ? will give you useage help in much the same manner as
  458. help-for-help. 
  459.  
  460. Interesting functions probably not bound to anything:
  461.   sql-set-buffer          -- defines which buffer will be sent to.
  462.   sql-send-region
  463.  
  464.   sql-make-deinstall-script       
  465.   sql-insert-gos
  466.   sql-convert-sybase-to-oracle
  467.   sql-insert-semi-colons
  468.   sql-drop-tables
  469.   sybase-drop-non-system-procs
  470.   sybase-drop-non-system-types
  471.   sybase-drop-non-system-tables
  472.  
  473. Command apropos on sql- or sybase- will show all the interesting functions.
  474.  
  475. Mode Specific Bindings:
  476.      \\{sql-mode-map} "
  477.   (interactive)
  478.   (c-mode)
  479.   (use-local-map sql-mode-map)
  480.   (setq major-mode 'sql-mode)
  481.   (setq mode-name "sql")
  482.   (setq local-abbrev-table sql-mode-abbrev-table)
  483.   (run-hooks 'sql-mode-hook)
  484.   )
  485.  
  486. (defun show-sql-shell (&optional fcn)
  487.   "Makes the isql shell visible in the other window"
  488.   (interactive)
  489.   (sql-verify-buffer)
  490.   (let ((window  (selected-window)))
  491.     (if (not (eq (window-buffer window) (get-buffer sql-buffer-name)))
  492.     (switch-to-buffer-other-window sql-buffer-name))
  493.     (if fcn (apply fcn nil))
  494.     (select-window window)))
  495.  
  496. (defun sql-kill-output-from-shell ()
  497.   "In sql shell, Kill shell buffer above current prompt."
  498.   (interactive)
  499.   (show-sql-shell 'kill-output-from-shell))
  500.  
  501. (defun sql-kill-all-output-from-shell ()
  502.   "In sql shell, Kill shell buffer above current prompt."
  503.   (interactive)
  504.   (show-sql-shell 'kill-all-output-from-shell))
  505.  
  506. (defun sql-shell-next-command ()
  507.   "Search for the next command in the sql shell."
  508.   (interactive)
  509.   (show-sql-shell 'shell-next-command))
  510.  
  511. (defun sql-shell-prev-command ()
  512.   "Search for the previous command in a shell."
  513.   (interactive)
  514.   (show-sql-shell 'shell-prev-command))
  515.  
  516. (defun sql-shell-bottom-command ()
  517.   "Search for the previous command in a shell."
  518.   (interactive)
  519.   (show-sql-shell 'end-of-buffer))
  520.  
  521. (defun sql-shell-scroll-up ()
  522.   "Scroll-up in the sql shell"
  523.   (interactive)
  524.   (show-sql-shell)
  525.   (scroll-other-window))
  526.  
  527. (defun sql-shell-scroll-down ()
  528.   "Scroll-down in the sql shell"
  529.   (interactive)
  530.   (sql-verify-buffer)
  531.   (let ((window  (selected-window))
  532.     h)
  533.     (switch-to-buffer-other-window sql-buffer-name)
  534.     (setq h (-  (window-height) 3))
  535.     (select-window window)
  536.     (scroll-other-window  (* -1 h))))  
  537.  
  538. (defun sql-shell-kill-input ()
  539.   "Do a kill-shell-input in the sql shell"
  540.   (interactive)
  541.   (show-sql-shell 'kill-shell-input))
  542.  
  543. (defun sql-show-output-from-shell ()
  544.   "In sql shell, Display start of this batch of shell output at top of window.
  545. Also put cursor there."
  546.   (interactive)
  547.   (show-sql-shell 'show-output-from-shell))
  548.  
  549. (defun sql-help ()
  550.   "You have typed \\[sql-help], the help character.  Type a Sql option:
  551. s  Send current sql expression
  552. e  Same as S
  553. g  Goto error line
  554.  
  555. B  Move the sql window to the bottom of all input/output
  556. V  Make the sql window become visible in the other window
  557. T  Move the sql window to the top of the last input
  558. +  Move the sql window to the top of the next input
  559. -  Move the sql window to the top of the previous input
  560. N  Do a scroll-up (next-page) in the sql window
  561. P  Do a scroll-down (previous-page) in the sql window
  562. I  Kill all pending unprocessed input not yet sent to sql
  563. U  Kill the results of the last input
  564. K  Kill all results so far
  565.  
  566. C  Convert to oracle format
  567. "
  568.   (interactive)
  569.   (let ((line-prompt "s e g B V T + - N P  I U K C. Type ? for more help: "))
  570.     (message (substitute-command-keys line-prompt))
  571.     (let ((char (read-char)))
  572.       (if (or (= char ??) (= char help-ch))
  573.       (save-window-excursion
  574.         (switch-to-buffer "*Help*")
  575.         (erase-buffer)
  576.         (insert (documentation 'sql-help))
  577.         (goto-char (point-min))
  578.         (while (memq char (cons help-ch '(?? ?\C-v ?\ ?\177 ?\M-v)))
  579.           (if (memq char '(?\C-v ?\ ))
  580.           (scroll-up))
  581.           (if (memq char '(?\177 ?\M-v))
  582.           (scroll-down))
  583.           (message "%s%s: "
  584.                line-prompt
  585.                (if (pos-visible-in-window-p (point-max))
  586.                "" " or Space to scroll"))
  587.           (let ((cursor-in-echo-area t))
  588.         (setq char (read-char))))))
  589.       (let ((defn (cdr (assq (downcase char)
  590.                  (cdr (cdr (assq 3 sql-mode-map)))))))
  591.     (if defn (call-interactively defn) (ding))))))
  592.  
  593.  
  594. (if (not sql-mode-map)
  595.     (progn
  596.       (setq sql-mode-map (copy-keymap c-mode-map))
  597.       ;; take out some C things
  598.       (define-key sql-mode-map ";"     'self-insert-command)
  599.       (define-key sql-mode-map ":"     'self-insert-command)
  600.       ;; Add in SQL things
  601.       (define-key sql-mode-map "\C-ck" 'sql-kill-all-output-from-shell)
  602.       (define-key sql-mode-map "\C-cu" 'sql-kill-output-from-shell)
  603.       (define-key sql-mode-map "\C-ci" 'sql-shell-kill-input)
  604.       (define-key sql-mode-map "\C-c-" 'sql-shell-prev-command)
  605.       (define-key sql-mode-map "\C-c+" 'sql-shell-next-command)
  606.       (define-key sql-mode-map "\C-cp" 'sql-shell-scroll-down)
  607.       (define-key sql-mode-map "\C-cn" 'sql-shell-scroll-up)
  608.       (define-key sql-mode-map "\C-ct" 'sql-show-output-from-shell)
  609.       (define-key sql-mode-map "\C-cv" 'show-sql-shell)
  610.       (define-key sql-mode-map "\C-cb" 'sql-shell-bottom-command)
  611.       (define-key sql-mode-map "\C-cc"  'sql-convert-sybase-to-oracle)
  612.       (define-key sql-mode-map "\C-c\C-@" 'sql-mark-current)
  613.       (define-key sql-mode-map "\C-c\?" 'sql-help)
  614.       (define-key sql-mode-map "\C-cg"  'sql-goto-error)
  615.       (define-key sql-mode-map "\C-x\C-e" 'sql-send-current)
  616.       (define-key sql-mode-map "\C-cs" 'sql-send-current)
  617.       ))
  618.  
  619. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  620. ;; SQL abbreviations. To enable 
  621. ;;    (defun on-abbrev () (abbrev-mode 1))
  622. ;;    (setq sql-mode-hook 'on-abbrev)
  623. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  624. (progn
  625.   (define-abbrev-table 'sql-mode-abbrev-table ())
  626.   (define-abbrev sql-mode-abbrev-table "arc" "archivelog" nil)
  627.  
  628.   (define-abbrev sql-mode-abbrev-table "s" "select" nil)
  629.   (define-abbrev sql-mode-abbrev-table "f" "from" nil)
  630.   (define-abbrev sql-mode-abbrev-table "fr" "from" nil)
  631.   (define-abbrev sql-mode-abbrev-table "w" "where" nil)
  632.   (define-abbrev sql-mode-abbrev-table "o" "order by" nil)
  633.   (define-abbrev sql-mode-abbrev-table "nu" "number" nil)
  634.   (define-abbrev sql-mode-abbrev-table "da" "date" nil)
  635.   (define-abbrev sql-mode-abbrev-table "co" "connect" nil)
  636.   (define-abbrev sql-mode-abbrev-table "be" "between" nil)
  637.   (define-abbrev sql-mode-abbrev-table "sy" "synonym" nil)
  638.   (define-abbrev sql-mode-abbrev-table "tr" "trigger" nil)
  639.   (define-abbrev sql-mode-abbrev-table "up" "update" nil)
  640.   (define-abbrev sql-mode-abbrev-table "ins" "insert" nil)
  641.   (define-abbrev sql-mode-abbrev-table "gr" "grant" nil)
  642.   (define-abbrev sql-mode-abbrev-table "gra" "grant all to" nil)
  643.   (define-abbrev sql-mode-abbrev-table "pu" "public" nil)
  644.   (define-abbrev sql-mode-abbrev-table "un" "unique" nil)
  645.   (define-abbrev sql-mode-abbrev-table "cl" "cluster" nil)
  646.   (define-abbrev sql-mode-abbrev-table "we" "whenever" nil)
  647.   (define-abbrev sql-mode-abbrev-table "ta" "table" nil)
  648.   (define-abbrev sql-mode-abbrev-table "pr" "priviledges" nil)
  649.   (define-abbrev sql-mode-abbrev-table "dr" "drop" nil)
  650.   (define-abbrev sql-mode-abbrev-table "ro" "rollback" nil)
  651.   (define-abbrev sql-mode-abbrev-table "rb" "rollback" nil)
  652.   (define-abbrev sql-mode-abbrev-table "tr" "transaction" nil)
  653.   (define-abbrev sql-mode-abbrev-table "us" "using" nil)
  654.   (define-abbrev sql-mode-abbrev-table "u" "using" nil)
  655.   )
  656.  
  657. ;-*- End File: ~/local/sql.el
  658.  
  659.  
  660.