I am using C# to execute q code against a KDB database using the c.cs library provided by KX. I provide arguments in the form of objects (integers, strings, Dict instances etc.) to the c.k method in that library, and this all works fine. However, when I have a query that doesn't work using this method, invariably what I want to do is take the query I've just executed through the C# adapter and re-execute it in a Q console so that I can tinker with it and work out what I did wrong. The problem is, in the C# environment what I have as arguments to my Q function (in some cases somewhat complicated dictionary-based trees of arguments), It's a real pain to manually convert my arguments into the func[arg1;arg2;arg3...]; format expected by the Q console. What I'd really like to be able to do is pass my arguments to a function that would recompose the original query in a syntax that would enable me to easily re-execute the original query in the console. Something like this:
In C#
var client = new c();
[connect]
var funcName = "myFunc";
var arg1 = "test";
var arg2 = 12345;
var arg3 = new c.Dict(new [] { "a", "b" }, new [] { true, false });
try {
var result = c.k(funcName, arg1, arg2, arg3);
} catch {
var composedStatement = c.k("getComposedStatement", funcName, new[] { arg1, arg2, arg3 });
// composedStatement: "myFunc[`test;12345i;`a`b!10b];"
Logger.LogError("KDB Query failed: " + composedStatement);
}
In Q
getComposedStatement:{[funcName;arguments]
:(string funcName),"[",(qWizardryGoesHere arguments),"];";
}
Hopefully that's clear what I'm trying to achieve. The important thing is that the getComposedStatement function takes a function name, and an arbitrary number of arguments that could be of any type, including nested types (lists of lists, list of dictionary, dictionary of different types etc.) and returns a statement that can be directly copied and pasted into a Q console for execution.