" sqlplus.vim " author: Jamis Buck (jgb3@email.byu.edu) " version: 1.2.3 " " This file contains routines that may be used to execute SQL queries and describe " tables from within VIM. It depends on SQL*Plus. You must have $ORACLE_HOME " $ORACLE_SID set in your environment, although you can explicitly set the " database name to use with the :DB command. " " In command mode: " : execute the SELECT query under your cursor. The query must begin with " the "select" keyword and end with a ";" " : prompt for an SQL command/query to execute. " : treat the identifier under the cursor as a table name, and do a 'describe' " on it. " : prompt for a table to describe. " : set the current SQL*Plus username and password " sb: open an empty buffer in a new window to enter SQL commands in " ss: execute the (one-line) query on the current line " se: execute the query under the cursor (as ) " st: describe the table under the cursor (as ) " sc: open the user's common SQL buffer (g:sqlplus_common_buffer) in a " new window. " " :Select <...> -- execute the given Select query. " :Update <...> -- execute the given Update command. " :Delete <...> -- execute the given Delete command " :DB -- set the database name to " :SQL <...> -- open a blank SQL buffer in a new window, or if a filename is " specified, open the given file in a new window. " " In visual mode: " : execute the selected query " " If queries contain bind variables, you will be prompted to give a value for " each one. if the value is a string, you must explicitly put quotes around it. " If the query contains an INTO clause, it is removed before executing. " " You will be prompted for your user-name and password the first time you access " one of these functions during a session. After that, your user-id and password " will be remembered until the session ends. " " The results of the query/command are displayed in a separate window. " " You can specify the values of the following global variables in your .vimrc " file, to alter the behavior of this plugin: " " g:sqlplus_userid -- the user-id to log in to the database as. If this " is specified, g:sqlplus_passwd must be given as well, which is the " password to use. Default: "" " g:sqlplus_path -- the path the the SQL*Plus executable, including any " command line options. Default: $ORACLE_HOME . "/bin/sqlplus -s" " g:sqlplus_common_commands -- any SQL*Plus commands that should be " executed every time SQL*Plus is invoked. " Default: "set pagesize 10000\nset wrap off\nset linesize 9999\n" " g:sqlplus_common_buffer -- the name of a file that will contain " common SQL queries and expressions, that may be opened via the " sc command. " g:sqlplus_db -- the name of the database to connect to. This variable " may also be modified via the :DB command. " " ------------------------------------------------------------------------------ " Thanks to: " Matt Kunze (kunzem@optimiz.com) for getting this script to work under " Windows " ------------------------------------------------------------------------------ " Global variables (may be set in ~/.vimrc) {{{1 if !exists( "g:sqlplus_userid" ) let g:sqlplus_userid = "" let g:sqlplus_passwd = "" endif if !exists( "g:sqlplus_path" ) let g:sqlplus_path = $ORACLE_HOME . "/bin/sqlplus -s " endif if !exists( "g:sqlplus_common_commands" ) let g:sqlplus_common_commands = "set pagesize 10000\nset wrap off\nset linesize 9999\n" endif if !exists( "g:sqlplus_common_buffer" ) let g:sqlplus_common_buffer = "~/.vim_sql" endif if !exists( "g:sqlplus_db" ) let g:sqlplus_db = $ORACLE_SID endif "}}} function! AE_getSQLPlusUIDandPasswd( force ) "{{{1 if g:sqlplus_userid == "" || a:force != 0 if g:sqlplus_userid == "" if has("win32") let l:userid = '' else let l:userid = substitute( system( "whoami" ), "\n", "", "g" ) endif else let l:userid = g:sqlplus_userid endif let g:sqlplus_userid = input( "Please enter your SQL*Plus user-id: ", l:userid ) let g:sqlplus_passwd = inputsecret( "Please enter your SQL*Plus password: " ) let g:sqlplus_db = input( "Please enter your database name: ", g:sqlplus_db ) endif endfunction "}}} function! AE_configureOutputWindow() "{{{1 set ts=8 buftype=nofile nowrap sidescroll=5 listchars+=precedes:<,extends:> normal $G while getline(".") == "" normal dd endwhile normal 1G let l:newheight = line("$") if l:newheight < winheight(0) exe "resize " . l:newheight endif endfunction "}}} function! AE_configureSqlBuffer() "{{{1 set syn=sql endfunction "}}} function! AE_describeTable( tableName ) "{{{1 let l:cmd = "prompt DESCRIBING TABLE '" . a:tableName . "'\ndesc " . a:tableName call AE_execQuery( l:cmd ) endfunction "}}} function! AE_describeTableUnderCursor() "{{{1 normal viw"zy call AE_describeTable( @z ) endfunction "}}} function! AE_describeTablePrompt() "{{{1 let l:tablename = input( "Please enter the name of the table to describe: " ) call AE_describeTable( l:tablename ) endfunction "}}} function! AE_execQuery( sql_query ) "{{{1 call AE_getSQLPlusUIDandPasswd( 0 ) new let l:tmpfile = tempname() . ".sql" let l:oldo = @o let @o="i" . g:sqlplus_common_commands . a:sql_query let l:pos = match( @o, ";$" ) if l:pos < 0 let @o=@o . ";" endif let @o=@o . "\n" normal @o let @o=l:oldo exe "silent write " . l:tmpfile close new let l:cmd = g:sqlplus_path . g:sqlplus_userid . "/" . g:sqlplus_passwd . "@" . g:sqlplus_db let l:cmd = l:cmd . " @" . l:tmpfile exe "1,$!" . l:cmd call AE_configureOutputWindow() call delete( l:tmpfile ) endfunction "}}} function! AE_promptQuery() "{{{1 let l:sqlquery = input( "SQL Query: " ) call AE_execQuery( l:sqlquery ) endfunction "}}} function! AE_resetPassword() "{{{1 let g:sqlplus_userid = "" let g:sqlplus_passwd = "" endfunction "}}} function! AE_execLiteralQuery( sql_query ) "{{{1 let l:query = substitute( a:sql_query, '\c\.*\', 'FROM', 'g' ) let l:idx = stridx( l:query, "\n" ) while l:idx >= 0 let l:query = strpart( l:query, 0, l:idx ) . " " . strpart( l:query, l:idx+1 ) let l:idx = stridx( l:query, "\n" ) endwhile let l:var = matchstr( l:query, ':\h\w*' ) while l:var > "" let l:var_val = input( "Enter value for " . strpart( l:var, 1 ) . ": " ) let l:query = substitute( l:query, l:var . '\>', l:var_val, 'g' ) let l:var = matchstr( l:query, ':\h\w*' ) endwhile call AE_execQuery( l:query ) endfunction "}}} function! AE_execQueryUnderCursor() "{{{1 exe "silent norm! ?\\c[^.]*\\<\\(select\\|update\\|delete\\)\\>\nv/;\nh\"zy" noh call AE_execLiteralQuery( @z ) endfunction "}}} function! AE_execWholeScript() "{{{1 "exe "silent norm! :%y z" exe "%y z" call AE_getSQLPlusUIDandPasswd( 0 ) new let l:tmpfile = tempname() . ".sql" let @z="i" . @z . "\n" normal @z exe "silent write " . l:tmpfile close new let l:cmd = g:sqlplus_path . g:sqlplus_userid . "/" . g:sqlplus_passwd . "@" . g:sqlplus_db let l:cmd = l:cmd . " @" . l:tmpfile exe "1,$!" . l:cmd call AE_configureOutputWindow() call delete( l:tmpfile ) endfunction "}}} function! AE_openSqlBuffer( fname ) "{{{1 exe "new " . a:fname call AE_configureSqlBuffer() endfunction "}}} function! AE_openEmptySqlBuffer() "{{{1 call AE_openSqlBuffer( "" ) endfunction "}}} " command-mode mappings {{{1 map sb :call AE_openEmptySqlBuffer() map ss "zyy:call AE_execLiteralQuery( @z ) map se :call AE_execQueryUnderCursor() map st :call AE_describeTableUnderCursor() exe "map sc :call AE_openSqlBuffer( \"" . g:sqlplus_common_buffer . "\" )" map :call AE_execWholeScript() map :call AE_execQueryUnderCursor() map :call AE_promptQuery() map :call AE_describeTableUnderCursor() map :call AE_describeTablePrompt() map :call AE_getSQLPlusUIDandPasswd(1) "}}} " visual mode mappings {{{1 vmap "zy:call AE_execLiteralQuery( @z ) "}}} " commands {{{1 command! -nargs=+ Select :call AE_execQuery( "select " ) command! -nargs=+ Update :call AE_execQuery( "update " ) command! -nargs=+ Delete :call AE_execQuery( "delete " ) command! -nargs=1 DB :let g:sqlplus_db="" command! -nargs=? SQL :call AE_openSqlBuffer( "" ) :menu Oracle.Execute\ whole\ scriptF7 :call AE_execWholeScript() :menu Oracle.Execute\ query\ under\ cursorF8 :call AE_execQueryUnderCursor() :menu Oracle.Prompt\ for\ query\\F8 :call AE_promptQuery() :menu Oracle.Describe\ table\ under\ cursorF09 :call AE_describeTableUnderCursor() :menu Oracle.Prompt\ for\ table\ to\ describeF10 :call AE_describeTablePrompt() :menu Oracle.Change\ connect\ informationF11 :call AE_getSQLPlusUIDandPasswd(1) "}}}