Stardog CLI tip

I'm writing this so that when I forget how to do it I know where to look to remind myself and I thought other people with a preference for all things CLI might appreciate.

I love the command line but what doesn't love the command line is SPARQL. How many times have you gone to run a quick query with stardog query myDatabase "select ?c where { ?c a :CocolateBar; ?a :hasManufacturer ?manufacturer . ?manufacturer :name "hersheys"

......damn forgot to start with a single quote or I'll have to escape all those double quotes. Whoops, forgot my prefixes, damn! How many curly brackets am in in for? Ugh, this is impossible without indenting.

Ok, you can create a file, edit that and then pass that to the command with stardog query myDatabase myQuery.rq but you're in a hurry and have things to do. So I have a couple of options for you.

  1. If you have vipe installed (it's in the moreutils package) you can run stardog query myDatabase "$(: | vipe | cat -)" and it will pop up your editor, either emacs or vi or whatever's registered with the env variable EDITOR, and when you quit it will write the contents to the query command.

  2. If you did create a query with myQuery.rq and you have it open in vi enter your command and then execute the query from within vi with :!stardog query % if you'd like to replace the current buffer with the contents of the output of the query then run %!stardog query %

I hope someone finds it useful. I know my future forgetful self will.

1 Like

Who am I kidding, I'm never going to remember $(: | vipe | cat -)

Try adding

alias stredit=": | vipe | cat -"

to get

stardog query myDb "$(stredit)"

for the win. Now that's something I can remember

1 Like

This is a very helpful hint! Using this approach, it is almost the same workflow as in the Postgres CLI, nice!