Support gsql output in JSON format

I've talked to at least a few teams that are directly querying
the database over gsql and scraping its output.  To mirror our
gerrit stream-events output gsql can now produce its records as
JSON objects, making them more suitable for machine consumption.

Change-Id: Ib2812b60a5d77824a48d511c50f6d8c2b23c4190
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2010-02-26 18:42:49 -08:00
parent 9f54bc41b7
commit 177ddaca30
4 changed files with 239 additions and 33 deletions

View File

@@ -24,6 +24,8 @@ import com.google.gerrit.sshd.commands.QueryShell;
import com.google.gerrit.sshd.commands.QueryShell.Factory;
import com.google.inject.Injector;
import org.kohsuke.args4j.Option;
import java.io.IOException;
/** Run Gerrit's SQL query tool */
@@ -31,6 +33,12 @@ public class Gsql extends SiteProgram {
private final LifecycleManager manager = new LifecycleManager();
private Injector dbInjector;
@Option(name = "--format", usage = "Set output format")
private QueryShell.OutputFormat format = QueryShell.OutputFormat.PRETTY;
@Option(name = "-c", metaVar = "SQL QUERY", usage = "Query to execute")
private String query;
@Override
public int run() throws Exception {
mustHaveValidSite();
@@ -47,7 +55,13 @@ public class Gsql extends SiteProgram {
manager.stop();
}
});
shellFactory().create(System.in, System.out).run();
final QueryShell shell = shellFactory().create(System.in, System.out);
shell.setOutputFormat(format);
if (query != null) {
shell.execute(query);
} else {
shell.run();
}
return 0;
}