Support for relational databases?

Apologies if this is already answered somewhere (I’ve looked in this forum and on sccode)…

Maybe a naive question, but is there a native way of interfacing supercollider/sclang with a relational database? I’m thinking about creating a „living“ installation with SC that would be fed by a DB, but I don’t assume there are any drivers?

Maybe there is a proxy or something one could use?

I know you can read JSON/YAML/CSV files but I’d rather not write a cron job that renders out the records…

Or would the solution be resorting to SonicPi or something and completely bypass sclang?

pretty sure there is support for http so, at the very least you could expose the database via rest

1 Like

Support for http was the next thing I‘d research, thanks!

Yeah if you have to stay in sclang I’d recommend e.g. https://postgrest.org via HTTP. Otherwise probably the SC Javascript/Python/Haskell/Clojure/etc clients.

Do we have? WebView perhaps, but if you just want access to the page’s contents without displaying them, I don’t see any http-related methods.

Also, make sure to test whether it’s synchronous (blocking) or asynchronous. You don’t want database requests to freeze the interpreter for noticeable durations.

If I were doing this, I’d research command-line tools that print SQL results to stdout in plain text. Then you could run the query using unixCmd (this is asynchronous, so, no freezes), where the command string pipes to a file, and in the unixCmd action, read the file (File primitives are pretty fast).

I’m not sure that receiving results as HTML would have any advantages over CSV or TSV. Probably processing HTML would be slower in sclang so tbh HTTP/HTML would probably not be my first choice.

Built-in direct support for db drivers would be a massive engineering and testing effort for a volunteer team to take on without financial support. That’s extremely unlikely to happen.

hjh

hmm yeah - i made a rash assumption thinking about WebView…that said, it looks like something like this could potentially work if you create a wrapper around it - but maybe not ideal…

(
var webview = WebView();
webview.onLoadFinished = {
	webview.toPlainText({|val|
		val.postln;
		webview.close;
	});
};
webview.url = "https://jsonplaceholder.typicode.com/todos/1"
)

I’m not sure that receiving results as HTML would have any advantages over CSV or TSV. Probably processing HTML would be slower in sclang so tbh HTTP/HTML would probably not be my first choice.

I think the suggestion is to use HTTP, but not HTML - e.g. you could request JSON values. If there’s no built-in HTTP request support, though, you probably would want to use unixCmd to call out to an external tool (even just curl). If you want to do more complex stuff, a non-sclang client with proper SQL support would imo be the way to go.

This does work – and running a quick routine alongside it shows that it’s asynchronous (nonblocking). And indeed it isn’t necessary to display the view. So that’s all good.

I just don’t see a significant advantage over curl. (Sometimes the way the question is framed implicitly biases one or another answer. Starting the question with “I’m looking for native DB support in sclang” creates a bias towards query methods built into SC – but the question is really “What is the best way to get DB query results into SC?” and the best answer may in fact be to offload the query to another process. My gut feeling is to prefer the other process, to reduce the risk of musical-sequencing thread timing being affected.)

hjh

Thank you all!

Sorry if I framed the question wrongly, I’m still pretty new to SC. But I’m very comfortable offloading stuff to curl or any other unix cmd if that is the way to go. I will report back :+1:t2:

No, don’t apologize! One of the really useful things in a programming forum is to explore different ways of framing problems. (And, sometimes problems become a lot simpler just by reframing, even before writing code.)

Here’s a quick and dirty curl way:

(
var tempfile = PathName.tmp +/+ UniqueID.next ++ ".txt";
var str, file;

"curl https://jsonplaceholder.typicode.com/todos/1 >%".format(tempfile)
.unixCmd({ |exit|
	protect {
		if(exit == 0) {
			d = tempfile.parseJSONFile.postln;
		} {
			"Error processing query (exit code = %)".format(exit).warn;
		};
	} { File.delete(tempfile) };
});
)

hjh

If you want to give it a try, that is one of the use cases that moved me to make this port: https://github.com/smrg-lm/sc3 Of course is not an all-in like sclang but may solve other complex problems.

1 Like