Permalink
Browse files

Add XQexploreDb command

Add command to run an xquery to get a list of documents, their root
element, and any collections they are part of.  Inspired bye the
"Explore" button in MarkLogic's Query Console
  • Loading branch information...
1 parent 76bd0fc commit 819b8c28b0b2b3c740ca08d269b6de820d00a0c1 @coledarr committed Oct 22, 2012
Showing with 53 additions and 16 deletions.
  1. +9 −1 doc/xqmarklogic.txt
  2. +12 −0 ftplugin/exploreDb.xqy
  3. +32 −15 ftplugin/xquery.vim
View
@@ -115,11 +115,15 @@ stored in the viminfo file. Pull requests with fixes for this are welcome.
==============================================================================
3. Commands *xqmarklogic-commands*
-<leader>B Run current buffer as an XQuery against the MarkLogic
+<LEADER>B Run current buffer as an XQuery against the MarkLogic
App Server you setup earlier. This mapping runs XQmlquery, so
it is easy to remap this if needed.
<CTRL-CR> same as above. Control-Return that MarkLogic QueryConsole
uses
+<LEADER>E Run XQexploreDb which will list for each document, a database
+ uri, the root element, and any collections it is part of.
+ It is somewhat like the "Explore" button for a database in
+ MarkLogic's Query Console.
NOTE If you want your own mappings set |g:xqmarklogic_noMappings| in your
vimrc to skip setting the above.
@@ -162,6 +166,10 @@ vimrc to skip setting the above.
*XQmlquery* Run the current buffer against the MarkLogic server
and display the output in a new window.
+*XQexploreDb* Display in a new window each document in the current database, a
+ database uri, the root element, and any collections it
+ is part of. This is very similar to the "Explore"
+ button in MarkLogic's Query Console.
==============================================================================
4. Customization: Options and Settings *xqmarklogic-options*
View
@@ -0,0 +1,12 @@
+xquery version "1.0-ml";
+
+for $doc in fn:doc()
+ let $uri := fn:document-uri($doc)
+return
+ <document db_uri="{$uri}">
+ {if ($doc/name(/*) = "")
+ (: Assume a binary document if root element name is "" :)
+ then <binary-node>1</binary-node>
+ else <root-element>{$doc/name(/*)}</root-element>}
+ <collections>{xdmp:document-get-collections($uri)}</collections>
+ </document>
View
@@ -1,6 +1,6 @@
" xquery.vim - <Leader>B or <C-CR> run buffer against marklogic as an xquery
" Maintainer: Darren Cole <http://github.com/coledarr/vim-xqmarklogic>
-" Version: 1.0.4
+" Version: 1.0.5
" TODO: Add support for: GetLatestVimScripts: ### ### :AutoInstall: xqmarklogic
" TODO: see *glvs-plugins* might not work, but should at least try
"
@@ -101,11 +101,11 @@ function! s:initSettings()
endif
let b:xqmarklogic_script=g:xqmarklogic_defaultScript
if !exists('g:xqmarklogic_defaultDb')
- let g:xqmarklogic_defaultDb="Documents"
+ let g:xqmarklogic_defaultDb='Documents'
endif
let b:xqmarklogic_db=g:xqmarklogic_defaultDb
if !exists('g:xqmarklogic_defaultMaxLinesIndent')
- let g:xqmarklogic_defaultMaxLinesIndent="400"
+ let g:xqmarklogic_defaultMaxLinesIndent='400'
endif
let b:xqmarklogic_maxLinesIndent=g:xqmarklogic_defaultMaxLinesIndent
@@ -194,15 +194,37 @@ function! s:DisplaySettings()
echo 'g:xqmarklogic_noMappings = ' . g:xqmarklogic_noMappings
endfunction
-" Running the Query
+" List the documents in database
+let s:thisfile = expand('<sfile>')
+
+if (!g:xqmarklogic_noMappings)
+ map <Leader>E :XQexploreDb<cr>
+endif
+command -buffer XQexploreDb :execute s:ExploreDatabase()
+function! s:ExploreDatabase()
+ "let l:old = b:xqmarklogic_noOutCleanup
+ "let b:xqmarklogic_noOutCleanup = 1
+ call s:QueryGenericMarkLogic('@'.fnameescape(fnamemodify(s:thisfile, ':p:h'). '/exploreDb.xqy'))
+ "let b:xqmarklogic_noOutCleanup = l:old
+endfunction
+command -buffer XQlistDocsA :execute s:QueryGenericMarkLogic(<args>)
+
+" Running the Buffer as a Query
if (!g:xqmarklogic_noMappings)
map <Leader>B :XQmlquery<cr>
map <C-CR> :XQmlquery<cr>
endif
-command -buffer XQmlquery :execute s:QueryMarkLogic(expand("%"))
+command -buffer XQmlquery :execute s:QueryMarkLogic(expand('%'))
function! s:QueryMarkLogic(fname)
- let info = ''
+ if &modified
+ echoerr 'Buffer has been changed, using last saved version'
+ endif
+ call s:QueryGenericMarkLogic('@'.a:fname)
+endfunction
+" Runs the query using curl
+" a:data - what to put after the -d" in the curl command
+function! s:QueryGenericMarkLogic(data)
" setup local settings
let l:user = b:xqmarklogic_user
let l:password = b:xqmarklogic_password
@@ -216,11 +238,7 @@ function! s:QueryMarkLogic(fname)
let l:showCurlCmd = b:xqmarklogic_showCurlCmd
let l:showDuration = b:xqmarklogic_showDuration
let l:showDb = b:xqmarklogic_showDb
- let l:info=""
-
- if &modified
- echoerr "Buffer has been changed, using last saved version"
- endif
+ let l:info=''
" Could use preview window
"let s:out = tempname()
@@ -230,15 +248,14 @@ function! s:QueryMarkLogic(fname)
" Use a 'nofile' window
"botright new
belowright new
-
if (l:showDb)
let l:info .= ' db="' . l:db . '"'
endif
setlocal buftype=nofile
setlocal filetype=xml
- let curlCmd='curl --digest --user ' . l:user . ':' . l:password . ' -s -X PUT -d@"' . a:fname . '" ' . l:uri . l:host . ':' . l:port . l:script . '?db='.l:db
+ let curlCmd='curl --digest --user ' . l:user . ':' . l:password . ' -s -X PUT -d"' . a:data . '" ' . l:uri . l:host . ':' . l:port . l:script . '?db='.l:db
if (l:showCurlCmd)
call append(0, '<!-- ' . curlCmd . ' -->')
@@ -251,8 +268,8 @@ function! s:QueryMarkLogic(fname)
let end=reltimestr(reltime(start))
let l:info .= ' query_duration="' . end . '"'
endif
- if (l:info != "" )
- call append(0, '<!-- ' . l:info .'" -->')
+ if (l:info != '' )
+ call append(0, '<!-- ' . l:info .' -->')
endif
" cleanup output

0 comments on commit 819b8c2

Please sign in to comment.