On this page
Extending BroadSQL with custom commands
BroadSQL can load extra commands from JAR files without recompiling the application. This page covers where extensions live, the objects a command actually has to work with (writing to the console, running SQL directly, running another BroadSQL command), a worked example that uses all three, and the rules BroadSQL enforces on your keywords and JAR at load time.
The extensions available for BroadSQL, such as BroadSQL Desk, are listed on the Extensions page.
Prefer a working project over copying snippets from this page? Get the BroadSQL Extension Kit, a ready-to-build starter with five example commands and tests. Details at the bottom of this page.
Third-party extensions
BroadSQL does not review, validate, or endorse extensions written by anyone other than UpAndCoding.com, whether shared publicly or written for internal use. Install and run a third-party extension at your own risk, the same way you would any other JAR of unknown origin: an extension has the same access to your session as a core command (console output, the current connection, running another BroadSQL command), so review its source before trusting it with a real connection.
Where extensions live
Drop a JAR containing your command classes into the extensions/ folder of the installation (configurable via CustomExtensionsFolder in conf/broadsql.ini, extensions on Windows and on Linux). BroadSQL scans it for classes at startup, on Windows and Linux alike.
This means a restart, not a hot reload. The launcher scripts (BroadSQL.bat/broadsql.sh) start the JVM with lib/*, drivers/* and extensions/* on the classpath: every JAR present in extensions/ at that moment becomes part of the classpath for that run. Dropping a new JAR in while BroadSQL is already running does nothing until you exit and start it again.
Writing a command
Each command is a class extending Command:
public class CommandMyThing extends Command {
public CommandMyThing() {
super("MYTHING", "MT"); // first keyword is primary, the rest are synonyms
}
@Override
public void execute(String query) throws BroadSQLException {
String[] args = parseArgs(query);
console.writeln("Hello from MYTHING");
}
@Override
public String getDescription() {
return "One-line description shown in HELP";
}
@Override
public String getArguments() {
return "Describe expected arguments here, or an empty string if none";
}
@Override
public String getExamples() {
return "MYTHING;\n\tMYTHING somearg;";
}
}
getKeywords() (fed by the constructor's super(...) call above), getDescription(), getArguments(), and getExamples() feed both the in-app HELP command and the generated command reference: keep them accurate. parseArgs(query) splits the typed command line into arguments after the recognized keyword, the same way every core command does.
getDescription(), getArguments(), and getExamples() default to an empty string, so a command that has nothing to say for one of them can leave it out.
What a command has to work with
Once BroadSQL loads your command, it sets a handful of fields on it before calling execute(query): these are protected, so they're available directly by name inside your class:
| Field | Type | What it's for |
|---|---|---|
console | ShellConsole | Writing output, see below. |
sqlDatabase | DatabaseConnection | The current connection, running SQL directly, see below. |
shellConsolePrinter | ConsolePrinter | Lower-level output used internally by result-set printing; most commands only ever need console. |
consoleLogger | ConsoleLogger | The per-connection activity log (see Command activity log): most commands never touch this directly. |
consoleSettings | ConsoleSettings | Read-only view of the current broadsql.ini settings. |
Three more things are reachable through public getter methods rather than a field directly (useful from a command in any package, including your own extension's):
getConsoleCommandInterpreter(): lets you run another BroadSQL command from inside yours. See "Running another BroadSQL command" below.getSession(): the current login session.getDatabaseConnectionsVault(): the CDF's connections vault, e.g. to checkgetDatabaseConnectionsVault().contains("SOMEID")before referencing a connection by name.
Writing to the console
console offers several output methods: the distinction that actually matters is whether the current prompt (e.g. MYDB> ) gets prepended:
| Method | Prepends the prompt? | Typical use |
|---|---|---|
console.writeln(String) / console.write(String) | No | Normal command output, a result, a summary line. This is what you want most of the time. |
console.println(String) / console.print(String) | Yes | A line meant to look like a fresh prompted line, rather than output continuing the current one. |
console.error(String) | Yes, plus an ERROR: prefix | Reporting a failure. |
console.warn(String) / console.info(String) | Yes, plus a WARNING: /INFO: prefix | A non-fatal note. |
Running SQL directly
sqlDatabase runs SQL against whichever connection is currently open, exactly like typing it at the prompt would:
sqlDatabase.executeSelectQuery(String sql): runs aSELECTand prints the results the same way BroadSQL prints any query's results (respectingSET LIST, the current export mode, and so on).sqlDatabase.executeUpdateQuery(String sql): runs anINSERT/UPDATE/DELETE/DDL statement and prints a summary line ("N row(s) updated."etc.).sqlDatabase.getInt(String sql): runs a query and returns the first column of its first row as anInteger, for a quick scalar (a count, a max, an id) without handling aResultSetyourself.
For anything more involved (iterating a ResultSet yourself, binding parameters, batching), sqlDatabase.getJdbcTemplate() returns the underlying Spring JdbcTemplate, and sqlDatabase.getDirectConnection() returns the raw java.sql.Connection.
Running another BroadSQL command
getConsoleCommandInterpreter() returns the CommandInterpreter currently driving the session: set its query and ask it to execute, exactly the way BroadSQL's own CONNECT command runs SHOW DBINFO and SHOW AUTOCOMMIT automatically after opening a connection:
getConsoleCommandInterpreter().setQuery("SHOW DBINFO");
getConsoleCommandInterpreter().executeCommand();
The query string is exactly what you'd type at the prompt, without the trailing ;: the interpreter strips it before this point when you type at the prompt, so calling it programmatically skips straight to the part after that.
Worked example: a command using all three
TABLEINFO <tableName> below prints a row count (direct SQL, via sqlDatabase.getInt) followed by the table's structure (a real BroadSQL command, DESCR, run through the interpreter): a small, realistic combination of everything above:
package com.example.broadsql.extensions;
import com.upandcoding.broadsql.controller.errors.BroadSQLException;
import com.upandcoding.broadsql.controller.shell.commands.Command;
import com.upandcoding.broadsql.controller.shell.commands.CommandUtils;
public class CommandTableInfo extends Command {
public CommandTableInfo() {
super("TABLEINFO", "TI");
}
@Override
public void execute(String query) throws BroadSQLException {
String[] args = parseArgs(query);
if (!CommandUtils.isValidArgs(args)) {
console.error("You must specify a table name");
return;
}
String tableName = args[0].trim();
// 1. Write plain output; no prompt prefix, since this continues the command's own output.
console.writeln("Summary for " + tableName + ":");
// 2. Run SQL directly against the current connection.
int rowCount = sqlDatabase.getInt("SELECT COUNT(*) FROM " + tableName);
console.writeln(rowCount + " row(s).");
console.writeln("");
// 3. Run a real BroadSQL command, the same way CONNECT runs SHOW DBINFO internally.
getConsoleCommandInterpreter().setQuery("DESCR " + tableName);
getConsoleCommandInterpreter().executeCommand();
}
@Override
public String getDescription() {
return "Displays a table's row count and structure in one call";
}
@Override
public String getArguments() {
return "<tableName> (mandatory)";
}
@Override
public String getExamples() {
return "TABLEINFO CURRENCY;";
}
}
Typed at the prompt, the new command prints:
WORLD> TABLEINFO CURRENCY;
Summary for CURRENCY:
166 row(s).
Column Type Nullable
------------------ ----------------------- --------
CODE VARCHAR_IGNORECASE(3) NO
NAME VARCHAR_IGNORECASE(70) NO
CODE2 VARCHAR_IGNORECASE(3) NO
ACTIVITY_STATUS_ID VARCHAR_IGNORECASE(3) YES
(DESCR's actual output format follows its own command reference; shown here only to illustrate that TABLEINFO really did trigger it.)
Keyword rules
Keywords are validated when BroadSQL starts:
- At least one keyword, and none of them blank.
- Only letters, digits, and spaces (
[a-zA-Z1-9 ]*). - Must not collide with a keyword already registered by a core command or another extension: the conflicting class is skipped (logged, not fatal) and the rest of BroadSQL still starts normally.
Packaging
Build your command classes into a plain JAR (with any of their own dependencies, since custom extension JARs are loaded independently) and copy it into extensions/. A JAR that fails to load, or a single incompatible class inside it, is logged and skipped: it will not prevent BroadSQL from starting or block other extensions from loading.
To see what was skipped, run SHOW EXTENSION ERRORS;: it lists each failure (the JAR or class and the reason) recorded when BroadSQL started, or reports that there were none. The full detail stays in the log file.
Two messages point at an installation problem rather than at your code:
Unable to load commands from '...': it is damaged or not a JAR file (...): the file is not a readable JAR. Build and copy it again.'...' is not on BroadSQL's classpath: extensions must be in the extensions folder of the installation: the JAR was found, but the JVM was not started with it. The launchers putextensions/*on the classpath; a JAR in another folder (for example one named by a differentCustomExtensionsFolder) cannot load.
Declaring your extension in its manifest (optional)
These attributes of your JAR's META-INF/MANIFEST.MF tell BroadSQL more about it. All are optional: a JAR without them loads exactly as described above.
| Attribute | Example | Effect |
|---|---|---|
BroadSQL-Extension-Packages | com.example.broadsql.extensions | BroadSQL loads only the commands of these packages (comma-separated; each package alone, not its subpackages). Without it, BroadSQL loads and initializes every class of the JAR at startup. The other classes of your JAR are still used normally by your commands. |
BroadSQL-Extension-Id | com.example.myext | An identity for your extension. With it, BroadSQL loads your JAR only with the BroadSQL releases listed in BroadSQL-Supported-Versions, when your JAR's Implementation-Version is a release number: with any other release it is refused at startup, with a message listing the supported releases, instead of failing later on a missing method. |
BroadSQL-Supported-Versions | 5.6.0, 5.6.1 | The BroadSQL releases you tested your extension with, comma-separated, each one in full (no range). Only read with BroadSQL-Extension-Id. |
BroadSQL-Built-Against | 5.6.0 | The BroadSQL release your extension was compiled against. Information only. |
BroadSQL-Host-Api | 1 | Used by BroadSQL's own optional extensions, which rely on BroadSQL's internal interface: a JAR built for another level is refused at startup. Do not set it in your own extensions. |
With Maven, the maven-jar-plugin writes them:
<archive>
<manifestEntries>
<BroadSQL-Extension-Packages>com.example.broadsql.extensions</BroadSQL-Extension-Packages>
</manifestEntries>
</archive>
A refused JAR is listed by SHOW EXTENSION ERRORS;, for example 'my-ext.jar' (com.example.myext 1.2.0) does not support BroadSQL 5.6.1, so it is not loaded. com.example.myext 1.2.0 supports BroadSQL 5.6.0. Keep using a supported BroadSQL release, or install a version of the extension that supports this one; BroadSQL itself works as usual. A development build (an Implementation-Version that is not a release number, of your extension or of BroadSQL) is not checked.
Upgrading extensions from earlier releases
BroadSQL's Java packages moved from com.projectsontracks to com.upandcoding.broadsql in release 5.3. This is a deliberate clean break with no compatibility layer: an extension JAR compiled against an earlier release refers to classes that no longer exist under their old names, so it cannot be loaded (a class that fails to load is logged and skipped, as described above). To upgrade an extension, change its imports to com.upandcoding.broadsql... (for example com.upandcoding.broadsql.controller.shell.commands.Command) and rebuild it against the 5.3 JAR. The BroadSQL Extension Kit below is already set up for the new package.
BroadSQL currently ships no extension commands. The Extension Commands category is reserved for commands provided through this mechanism: HELP lists every command of an extension JAR there (HELP EXT; lists them alone). Bundled commands remain classified by their function, regardless of their internal Java package.
BroadSQL Extension Kit
Everything above shows the mechanism and one worked example. The BroadSQL Extension Kit is a ready-to-build starting point on GitHub: a small Maven project with five working example commands, unit tests, and a build already set up to compile correctly against a real BroadSQL installation, so you can copy a working class instead of assembling one from scratch.
| Command | Demonstrates |
|---|---|
HELLO | The minimal command shape: no database, one optional argument. |
ROWCOUNT | Querying the current connection, argument validation. |
CMPROWS | A two-argument command, reaching into a second connection. |
XTABLE | Redirecting query output to a file, safely restoring session state afterward. |
RUNTOOL | Shelling out to an external program without a command-injection hole. |
Prerequisites: JDK 21, Maven, and a working BroadSQL 5.3 installation. The kit compiles against your own installed lib/broadsql.jar (installed into your local Maven repository with one documented command, since BroadSQL is closed source and not published to a public artifact repository): nothing else to configure. Each example class's own comments explain the specific thing it demonstrates: start with CommandHello, then copy whichever example is closest to what you are building. Full setup steps are in the repository's own README.
The kit's example code and build setup are MIT licensed, so you're free to copy and adapt them; BroadSQL itself remains separate, closed source, commercially licensed software, and a valid installation is still required to build or run anything from the kit.
Get the BroadSQL Extension Kit on GitHub
Found something wrong or unclear on this page? Tell us, with the page address.