home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / s / sql-mode.zip / SQL-MODE.DOC < prev    next >
Text File  |  1992-11-26  |  18KB  |  362 lines

  1. Although SQL*Plus is a powerful tool for producing ad hoc queries and
  2. interacting with an Oracle database, the user interface is less than
  3. state-of-the-art.
  4.  
  5. I am now pleased to announce a new utility available to users of Emacs and
  6. SQL*Plus that allows SQL*Plus to run in an Emacs buffer.  Even if you don't
  7. know Emacs, this utility makes SQL*Plus so much friendlier that you may want
  8. to learn the basics of Emacs just to take advantage of the improvements.  For
  9. those of you who have been using the current version, this version includes
  10. many significant enhancements (see below).
  11.  
  12. If anyone receives this message that does not yet have access to this utility
  13. but would like to use it, send me a mail message and I can send you the
  14. appropriate files with instructions.
  15.  
  16. If you are not familiar with Emacs, some of the terms used below may be
  17. foreign to you (buffers, modes, key sequences, etc.).  However, Emacs comes
  18. with an excellent tutorial that will teach you the basics (just press
  19. Control-H, then the letter "T", 'C-h t', after starting Emacs) as well as full
  20. on-line documentation (C-h i).
  21.  
  22.  
  23. Description
  24. ===========
  25.  
  26. Here is a brief description of the enhancements that this utility adds to 
  27. SQL*Plus (features added since version 1.1 are indicated with NEW!):
  28.  
  29.         * Fully compatible with SQL*Plus.  If none of the new features
  30.           are accessed, SQL*Plus looks and behaves exactly like it
  31.           does normally.  You can enter commands one line at a time and
  32.           use all the awkward line editing commands you've become
  33.           accustomed to. 
  34.  
  35.         * Full history buffer.  All text entered and received in the 
  36.           current session is stored so you can scroll forward and back
  37.           to examine prior results.
  38.  
  39. NEW!    * Save history between sessions.  You have the option of saving
  40.           your sessions automatically so that the next time SQL*Plus
  41.           is invoked, you are returned to where you left off--with all
  42.           prior input and output intact!
  43.  
  44.         * Full command line editing.  All of Emacs' editing commands
  45.           may be used to correct errors in the current input line before
  46.           pressing RETURN.  When entering multi-line statements you may
  47.           easily move up to prior lines to change them before executing
  48.           the entire statement.
  49.  
  50.         * Recall, edit, and re-execute prior commands.  Any prior command
  51.           entered in the buffer may be modified and re-executed.  Also, 
  52.           full cut and paste of commands or parts or commands is supported
  53.           using standard Emacs features.
  54.  
  55.         * Entry shortcuts.  Single letter abbreviations are provided for
  56.           commonly used keywords.  For example, typing S<space> inserts 
  57.           "SELECT ".  Also, any other keyword, table, or column name
  58.           used earlier may be abbreviated with the first few letters
  59.           (see dynamic expansion below).
  60.  
  61. NEW!    * Powerful "word grab" feature.  Position the cursor on a column
  62.           or table name and with the press of a key, the text is appended
  63.           to the end of the current SQL statement.  Commas are even inserted
  64.           automatically when appropriate!
  65.  
  66. NEW!    * Run multiple SQL*Plus sessions simultaneously.  Using Emacs'
  67.           multiple buffer/window features, two or more sessions may be
  68.           active concurrently--each attached to a different database if
  69.           desired.
  70.  
  71.         * Edit SQL*Plus batch files and execute them with a keystroke.
  72.           You no longer need to use the EDIT or HOST commands to move
  73.           in and out of a full screen editor while refining SQL scripts.  
  74.           Just edit in one Emacs window and with the press of a key, the 
  75.           commands will be executed with the output appearing in a second 
  76. NEW!      window. You can even execute SQL statements imbedded in a 
  77.           SQL*Report, SQL*Forms (.inp), or Pro*C (Fortran, COBOL, etc.) file!
  78.  
  79.         * On-line help.  Help is available in any mode by pressing C-h m.
  80.           (This is true of all Emacs modes, not just the new SQL modes.) 
  81.           You can also get help on any key sequence with C-h k <key sequence>.
  82.  
  83.         * Multi-tasking!  Since you are in Emacs, you can perform
  84.           other tasks while you wait for a complex SQL statement to finish
  85.           executing.  Switch to another document, open a new window, 
  86.           compose a letter, run an operating system shell, and if Emacs
  87.           is run from the Unix c-shell, it can be "suspended" and you 
  88.           can return to it later.
  89.  
  90. This utility is implemented as an Emacs "major mode" for editing text. There
  91. are actually two new major modes.  The first is sqlplus-mode which runs a
  92. SQL*Plus session in a buffer called *sqlplus* and allows you to edit and
  93. re-execute prior commands, as well as providing a history of all interaction.
  94. This mode is similar to shell-mode (and is, in fact, based upon it), but with
  95. additional features specific to SQL*Plus.
  96.  
  97. Another mode called sql-mode can be used to edit SQL*Plus batch programs which
  98. can then be executed with a keypress with the output appearing in another
  99. window.
  100.  
  101. Like most other special Emacs modes, the commands unique to these new modes
  102. are accessed using a keystroke sequence of Control-C followed by another
  103. control character.
  104.  
  105.  
  106. SQL*Plus Mode
  107. =============
  108.  
  109. To experiment with the interactive SQL*Plus mode, invoke Emacs with these
  110. options: 
  111.  
  112.         emacs -l sql-mode -f sqlplus
  113.  
  114. You will be prompted to enter a username/password combination to access an
  115. Oracle database.  Interaction in the *sqlplus* buffer is exactly like normal
  116. SQL*Plus, including its limited editing capabilities, if all commands are
  117. entered at the end of the buffer.  Prior commands may be recalled, edited, and
  118. re-executed in the following ways.
  119.  
  120.         1) Manually cursor back up to the command to be edited, make changes,
  121.            then press [Return] when the cursor is on any line of a multi-line
  122.            statement.  This is convenient after a command that produces a
  123.            small amount of output or after a LIST command.
  124.  
  125.         2) Use C-c C-b and C-c C-f (back/forward command) to move the cursor to
  126.            the previous or next command in the buffer.  Then edit and
  127.            re-execute by pressing [Return].  C-c C-b is also useful after a
  128.            command has produced several pages of output and you wish to begin
  129.            reviewing it from the beginning.  I have these commands bound to
  130.            ESC up-arrow and ESC down-arrow in my .emacs file.
  131.  
  132.         3) Use C-c C-p and C-c C-n (previous/next command) to recall the prior
  133.            commands directly after the last prompt at the end of the buffer.
  134.            When the desired command has been recalled, press [Return] to
  135.            execute, or edit and then re-execute with [Return].
  136.  
  137.  
  138. Typing Shortcuts
  139. ----------------
  140.  
  141. A powerful feature which is built into Emacs is the use of abbreviations.  In
  142. sqlplus-mode, standard abbreviations are automatically available for common
  143. keywords such as DESCRIBE, SELECT, FROM, WHERE, ORDER BY, etc. by entering the
  144. first letter followed by space or tab.  You may add to or modify this table
  145. and save it for later use (consult an Emacs manual or the on-line
  146. documentation for more information on using abreviations).
  147.  
  148. Dynamic abbreviations are also very useful.  Entering the first few characters
  149. of a table or column name (or any other word) included anywhere in the buffer
  150. and typing M-/ (note: M = ESC) will complete the name; successive M-/'s will
  151. cycle through all matches.  I use this after "DESCRIBE table" so that all
  152. column names are conveniently available.
  153.  
  154. Another way to quickly and easily build queries after using DESCRIBE is to
  155. position the cursor over (or after) the first column name you wish to SELECT
  156. and press C-c C-w (copy word).  If the command line at the end of the buffer
  157. is blank, "SELECT" will be inserted, followed by the column name.  Repeat this
  158. for each column to be selected and the names will be appended to the line with
  159. commas separating each entry.  This technique may also be used to copy words
  160. from an earlier line of the same statement while entering expressions,
  161. comparisons, or ORDER BY clauses.  Building SELECT statements has never been
  162. so easy!
  163.  
  164.  
  165. Saving Your Session
  166. -------------------
  167.  
  168. Since all output is saved in the buffer, you can easily scroll back to review
  169. prior queries, without having to re-execute them.  You may also save your
  170. session with C-c C-s (or M-x sqlplus-save-session) to any file and re-load it
  171. later with C-x i (insert-file).  A better option is to have Emacs do this
  172. for you when you exit.  By setting the the Emacs variable sqlplus-keep-history
  173. to a non-nil value in your .emacs file, your session will saved in the file
  174. .sqlhist in your home directory and automatically loaded the next time sqlplus
  175. is run (an example of how to set this variable is included later).
  176.  
  177. Since your .sqlhist file is likely to grow very large after only a few
  178. sessions, I have provided some ways of preventing the history buffer from
  179. growing too large.  When you exit SQL*Plus by typing EXIT or QUIT, the number
  180. of lines in the buffer is compared with the variable sqlplus-lines-to-keep,
  181. which defaults to 1000.  If it is larger, you will be asked if you wish to
  182. tuncate the buffer to the indicated size before saving (the oldest lines will
  183. be deleted).  You can also perform this truncation at any time during your
  184. session with the command C-c C-d (delete old lines).  If you wish to exit
  185. without saving the session, use C-x k (kill-buffer).
  186.  
  187. Another way to drastically shrink the buffer is with C-c C-x which deletes all
  188. output lines in the buffer, keeping only the commands that have been entered.
  189. This way you still have access to prior commands for re-execution.  
  190.  
  191. After entering a SQL command that generates an error, or produces a report
  192. that still is not quite correct, rather than recall the command to edit and
  193. re-execute it, use C-c C-k (kill) which deletes either the last output
  194. generated by SQL*Plus or the current command being entered, depending on where
  195. the cursor is when it is used.  If executed while the cursor is within a SQL
  196. statement, the statement and any text after it are deleted.  If the cursor is
  197. within or at the end of output generated by SQL*Plus, the output is deleted
  198. and the cursor is positioned at the end of the SQL statement that generated
  199. the output.  Thus, it can be used like an undo command to alternately delete
  200. commands and output from the end of the buffer.  This helps prevent the buffer
  201. from growing as a result of several "false starts" and makes finding prior
  202. valid commands easier (because you don't have to wade through several bad
  203. commands and their associated output).
  204.  
  205.  
  206.  
  207. which will also kill the process (but will not save your session).
  208.  
  209.  
  210. Summary of Commands
  211. -------------------
  212.  
  213.         C-c C-p   Recall previous command.
  214.         C-c C-n   Recall next command.
  215.         C-c C-b   Back one command.
  216.         C-c C-f   Forward one command.
  217.         C-c C-e   Move end of buffer (like M->, but does not set the mark.)
  218.         C-c C-r   Return to beginning of last output produced by SQL*Plus.
  219.         C-c C-c   Send ^C to interrupt current command execution.
  220. NEW!    C-c C-k   Kill current SQL statement or last output.
  221. NEW!    C-c C-x   Delete all output lines.
  222. NEW!    C-c C-d   Drop old lines (truncate to value of sqlplus-lines-to-keep).
  223. NEW!    C-c C-w   Copy word to end.
  224. NEW!    C-c C-s   Save session to file.
  225.  
  226. You can generate a list like this while in sqlplus-mode by typing C-h m (Help
  227. with current Mode) or C-h b (Help keyBindings).  You can also get help on any
  228. specific command with C-h k (Help Key) followed by the key sequence.
  229.  
  230.  
  231. SQL Edit Mode
  232. =============
  233.  
  234. In a text buffer, the command 'M-x sql-mode' will enable SQL text editing
  235. mode.  The keyword abbreviations described above are also available in this
  236. mode.  Two special commands are provided to execute the SQL*Plus statements
  237. contained in a sql-mode buffer.  C-c C-x will send the entire buffer to
  238. SQL*Plus.  The output is displayed in the *sqlplus* buffer in a second window
  239. (if SQL*Plus is not currently running, a new buffer is created and the process
  240. started).  C-c C-r will send the commands in the currently defined region
  241. (between point and mark).  This feature may be useful to Pro*C programmers who
  242. need to test SQL statements imbedded in their code.
  243.  
  244. If the SQL statements to be executed contain variables prefixed with colons
  245. or INTO clauses, the colons are converted into ampersands and the INTO clauses
  246. are removed before being sent to SQL*Plus.  This provides compatibility with
  247. Pro*C, SQL*Report, and SQL*Forms (.inp files).  For example,
  248.  
  249.      SELECT SYSDATE + :days_added INTO :variable FROM SYSTEM.DUAL
  250.  
  251. is converted to
  252.  
  253.      SELECT SYSDATE + &days_added FROM SYSTEM.DUAL
  254.  
  255. and the user is prompted to enter the value of days_added.  This substitution
  256. process can be prevented by using a prefix argument with the
  257. sqlplus-send-region command.  In practice, this is done by typing 'C-u C-c
  258. C-r'.  Also, variables of the form :block_name.field_name are converted to
  259. &block_name_field_name for convenience when editing .inp files.
  260.  
  261. In the future, this mode may automatically indent your SQL*Plus code based on
  262. Oracle standards (if they have been defined).  Your suggestions are welcome.
  263.  
  264.  
  265. Accessing the New Modes
  266. =======================
  267.  
  268. If Emacs is started normally, the new modes will not be available until the
  269. new library is loaded with the command:
  270.  
  271.          M-x load-library RET sql-mode RET      (RET = the RETURN key)
  272.  
  273. Or Emacs can be invoked with the option '-l sql-mode'.
  274.  
  275. Once the library is loaded, the commands 'M-x sqlplus' and 'M-x sql-mode' can
  276. be used.  This can be automated by including the following commands in your
  277. .emacs file:
  278.  
  279.         (autoload 'sql-mode "sql-mode" "SQL editing mode" t)
  280.         (autoload 'sqlplus "sql-mode" "Run SQL*Plus interactively" t)
  281.  
  282. This will load the sql-mode library the first time either of these commands is
  283. used.
  284.  
  285. Sql-mode can be invoked automatically whenever a file ending in .sql is edited
  286. by including this command in .emacs:
  287.  
  288.    (setq auto-mode-alist (cons '("\\.sql$" . sql-mode) auto-mode-alist))
  289.  
  290. Note: The above statements may eventually be added to the global
  291. initialization files used on each machine where Emacs is loaded
  292. (i.e. site-init.el).
  293.  
  294.  
  295. Customizing Your Environment
  296. ============================
  297.  
  298. When sqlplus is initially executed, you will be prompted to enter a username
  299. and password to access an Oracle database.  If you always use the same
  300. username/password combination, this can be suppressed by setting a special
  301. variable in your .emacs file as follows:
  302.  
  303.         (setq sqlplus-username-password "myname/mypassword")
  304.  
  305. You may also wish to assign some of the special functions to more convenient
  306. keys on your keyboard.  Each of these modes calls a "hook" routine (like all
  307. other Emacs modes).  By assigning custom routines to these hooks, you can add
  308. additional functionality.  Below are some statements that will assign common
  309. commands used in sqlplus and sql-mode to VT320 function keys.
  310.  
  311. (global-unset-key "\e\e"              ; allow ESC as a prefix to function keys
  312. (setq sql-mode-hook 'set-sql-keys)
  313. (setq sqlplus-mode-hook 'set-sqlplus-keys)
  314.  
  315. (defun set-sql-keys ()
  316.   "Customize sql-mode keys."
  317.   (local-set-key "\eOM" 'sql-send-buffer)                 ; Enter
  318.   (local-set-key "\e\eOM" 'sql-send-region))              ; ESC Enter
  319.  
  320. (defun set-sqlplus-keys ()
  321.   "Customize sqlplus-mode keys."
  322.   (local-set-key "\e\eOA" 'sqlplus-back-command)          ; ESC up-arrow
  323.   (local-set-key "\e\eOB" 'sqlplus-forward-command)       ; ESC down-arrow
  324.   (local-set-key "\e\eOD" 'sqlplus-previous-command)      ; ESC left-arrow
  325.   (local-set-key "\e\eOC" 'sqlplus-next-command)          ; ESC right-arrow
  326.   (local-set-key "\e\r" 'sqlplus-end-of-buffer))          ; ESC Return
  327.  
  328. These key bindings will only be active in the buffers running the new modes
  329. and thus will not affect other functions assigned to the same keys in other
  330. buffers.
  331.  
  332. SQL*Plus reads and executes the contents of the file login.sql in your home
  333. directory when it starts up.  If you have modified the default parameters you
  334. may need to make changes to this file for compatibility with sqlplus-mode.
  335.  
  336. The current version requires that the SQL*Plus prompt be "SQL> " and that
  337. continuation prompts are numbered lines (the default), so do not change the
  338. settings of the SQL*Plus variables 'sqlprompt' or 'sqlnumber'.  Page pause
  339. should be left OFF (set pause off) since you can page forward and back using
  340. Emacs commands (although setting pause ON will still work).
  341.  
  342. Since Emacs automatically wraps long lines you can set the line length used by
  343. SQL*Plus to the maximum value (set linesize 500).  Then if your terminal
  344. supports wide displays or variable size windows, long lines will always wrap
  345. to fit (however, this will also affect the centering of titles).
  346.  
  347.  
  348. Known Bugs 
  349. ==========
  350.  
  351. When entering a new command, if you move up to a prior line (of the same
  352. multi-line statement), make a change, then move back to the last line and
  353. finish entering it, SQL*Plus will not see the change--it will only process the
  354. last line entered.  However, if you move up to any line except the last one
  355. before pressing [Return], all lines will be re-evaluated when the command is
  356. executed.
  357.  
  358. If you discover additional bugs or think of some good enhancements, send them
  359. via email to jlange@us.oracle.com, or call me at (415) 506-4669.
  360.  
  361.     -Jim Lange
  362.