Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
import picocli.CommandLine;


@CommandLine.Command(name = "database", description = "Database (catalog) operations",
@CommandLine.Command(name = DatabaseCommand.COMMAND_NAME, description = "Database (catalog) operations",
subcommands = {
DatabaseListCommand.class,
DatabaseDDLCommand.class
})
public class DatabaseCommand extends AbstractMetaObjectCommand {
public static final String COMMAND_NAME = "database";

@Override
public boolean isRelevantObject(@NotNull DBSObject object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
import org.jkiss.dbeaver.model.struct.rdb.DBSSchema;
import picocli.CommandLine;

@CommandLine.Command(name = "schema", description = "Schema operations",
@CommandLine.Command(name = SchemaCommand.COMMAND_NAME, description = "Schema operations",
subcommands = {
SchemaListCommand.class,
SchemaDDLCommand.class
})
public class SchemaCommand extends AbstractMetaObjectCommand {
public static final String COMMAND_NAME = "schema";

@Override
public boolean isRelevantObject(@NotNull DBSObject object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
import org.jkiss.dbeaver.model.struct.DBSObjectContainer;
import picocli.CommandLine;

@CommandLine.Command(name = "table", description = "Table meta operations",
@CommandLine.Command(name = TableCommand.COMMAND_NAME, description = "Table meta operations",
subcommands = {
TableListCommand.class,
TableDDLCommand.class
})
public class TableCommand extends AbstractMetaObjectCommand {
public static final String COMMAND_NAME = "table";

@Override
public boolean isRelevantObject(@NotNull DBSObject object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

public abstract class AbstractDDLCommand extends AbstractMetaCommand {

public static final String COMMAND_NAME = "ddl";

@CommandLine.Option(names = {"--full"}, description = "Show full DDL")
protected boolean fullDDL;

Expand All @@ -57,20 +59,20 @@

@Override
public void run() throws CLIException {
getParentCommand().executeWithMonitor("Get " + getObjectTypeName() + " DDL", this::execute);
}

protected void execute(@NotNull DBRProgressMonitor monitor) throws DBException {
AbstractMetaObjectCommand parent = getParentCommand();

Check warning on line 66 in bundles/org.dbvr.cli.sql/src/org/dbvr/cli/sql/meta/ddl/AbstractDDLCommand.java

View workflow job for this annotation

GitHub Actions / Check style / Lint

[checkstyle] reported by reviewdog 🐶 Distance between variable 'parent' declaration and its first usage is 4, but allowed 3. Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value). Raw Output: /github/workspace/./bundles/org.dbvr.cli.sql/src/org/dbvr/cli/sql/meta/ddl/AbstractDDLCommand.java:66:9: warning: Distance between variable 'parent' declaration and its first usage is 4, but allowed 3. Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value). (com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck)
DBPDataSource dataSource = connectDataSource();
checkDDLSupported(monitor, dataSource);
String objectName = getTargetObjectName();
if (CommonUtils.isEmpty(objectName)) {
throw new CLIException(
"Object name is not specified",
getObjectTypeName() + " name is not specified",
CLIConstants.EXIT_CODE_ILLEGAL_ARGUMENTS
);
}
parent.executeWithMonitor("Get " + getObjectTypeName() + " DDL", monitor -> execute(monitor, objectName));
}

protected void execute(@NotNull DBRProgressMonitor monitor, @NotNull String objectName) throws DBException {
AbstractMetaObjectCommand parent = getParentCommand();
DBPDataSource dataSource = connectDataSource();
DBSObjectContainer container = getBaseContainer(monitor, dataSource);
if (container == null) {
return;
Expand All @@ -90,10 +92,19 @@
context().addResult(ddl.trim());
context().setPostAction(CLIProcessResult.PostAction.SHUTDOWN);
} else {
throw new CLIException(
getObjectTypeName() + " '" + objectName + "' does not support DDL",
CLIConstants.EXIT_CODE_ERROR
);
throw new CLIException(getUnsupportedDDLMessage(objectName), CLIConstants.EXIT_CODE_ERROR);
}
}

protected void checkDDLSupported(
@NotNull DBRProgressMonitor monitor,
@NotNull DBPDataSource dataSource
) throws DBException {
// supported by default
}

@NotNull
protected String getUnsupportedDDLMessage(@NotNull String objectName) {
return getObjectTypeName() + " '" + objectName + "' does not support DDL";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,31 @@
*/
package org.dbvr.cli.sql.meta.ddl;

import org.dbvr.cli.sql.meta.AbstractMetaObjectCommand;
import org.dbvr.cli.sql.meta.DatabaseCommand;
import org.dbvr.cli.sql.meta.MetaDatabaseOptions;
import org.dbvr.cli.sql.meta.*;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.cli.CLIConstants;
import org.jkiss.dbeaver.model.cli.CLIException;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.struct.DBSObjectContainer;
import org.jkiss.dbeaver.model.struct.rdb.DBSCatalog;
import picocli.CommandLine;

@CommandLine.Command(name = "ddl", description = "Get database DDL")
import java.util.Collection;

@CommandLine.Command(name = AbstractDDLCommand.COMMAND_NAME, description = "Get database DDL")
public class DatabaseDDLCommand extends AbstractDDLCommand {

private static final String UNSUPPORTED_MESSAGE = "Database DDL is not supported for this database type. " +
"Try a lower-level DDL command, such as '"
+ MetaCommand.COMMAND_NAME + " " + SchemaCommand.COMMAND_NAME + " " + AbstractDDLCommand.COMMAND_NAME
+ "' or '"
+ MetaCommand.COMMAND_NAME + " " + TableCommand.COMMAND_NAME + " " + AbstractDDLCommand.COMMAND_NAME
+ "'.";

@CommandLine.ParentCommand
private DatabaseCommand parent;

Expand Down Expand Up @@ -60,6 +71,41 @@ protected DBSObjectContainer getBaseContainer(
@NotNull DBRProgressMonitor monitor,
@NotNull DBPDataSource dataSource
) throws DBException {
return parent.getBaseContainer(monitor, dataSource, containerOptions.getDatabaseName(), null);
return parent.getBaseContainer(monitor, dataSource, null, null);
}

@Override
protected void checkDDLSupported(
@NotNull DBRProgressMonitor monitor,
@NotNull DBPDataSource dataSource
) throws DBException {
if (!hasDatabases(monitor, dataSource)) {
throw new CLIException(UNSUPPORTED_MESSAGE, CLIConstants.EXIT_CODE_ERROR);
}
}

@NotNull
@Override
protected String getUnsupportedDDLMessage(@NotNull String objectName) {
return UNSUPPORTED_MESSAGE;
}

private boolean hasDatabases(
@NotNull DBRProgressMonitor monitor,
@NotNull DBPDataSource dataSource
) throws DBException {
if (!(dataSource instanceof DBSObjectContainer container)) {
return false;
}
Collection<? extends DBSObject> children = container.getChildren(monitor);
if (children == null) {
return false;
}
for (DBSObject child : children) {
if (child instanceof DBSCatalog) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.jkiss.dbeaver.model.struct.DBSObjectContainer;
import picocli.CommandLine;

@CommandLine.Command(name = "ddl", description = "Get schema DDL")
@CommandLine.Command(name = AbstractDDLCommand.COMMAND_NAME, description = "Get schema DDL")
public class SchemaDDLCommand extends AbstractDDLCommand {

@CommandLine.ParentCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.jkiss.dbeaver.model.struct.DBSObjectContainer;
import picocli.CommandLine;

@CommandLine.Command(name = "ddl", description = "Get table DDL")
@CommandLine.Command(name = AbstractDDLCommand.COMMAND_NAME, description = "Get table DDL")
public class TableDDLCommand extends AbstractDDLCommand {

@CommandLine.ParentCommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,31 @@ public void testDatabaseDDL() throws Exception {
CLIProcessResult ddlResult = cmd.executeCommandLineCommands(null, false, false, ddlArgs);
Assert.assertNotNull(ddlResult);
String ddlOutput = ddlResult.getOutput() != null ? String.join("\n", ddlResult.getOutput()) : "";
Assert.assertEquals(CLIConstants.EXIT_CODE_OK, ddlResult.getExitCode());
Assert.assertEquals(CLIConstants.EXIT_CODE_ERROR, ddlResult.getExitCode());
Assert.assertTrue(
ddlOutput.contains("Database doesn't support databases/catalogs")
ddlOutput.contains("Database DDL is not supported for this database type")
);
} finally {
h2Ds.getRegistry().removeDataSource(h2Ds);
}
}

@Test
public void testDatabaseDDLNoDatabaseName() throws Exception {
DBPDataSourceContainer h2Ds = createFakeDataSource("h2-test-db-ddl-noname");
try {
var cmd = DBVRTestSuite.getApplication().createCommandLine();
var ddlArgs = new String[] {
"meta", "database", "ddl",
"--datasource=" + h2Ds.getName()
};
CLIProcessResult ddlResult = cmd.executeCommandLineCommands(null, false, false, ddlArgs);
Assert.assertNotNull(ddlResult);
String ddlOutput = ddlResult.getOutput() != null ? String.join("\n", ddlResult.getOutput()) : "";
Assert.assertEquals(CLIConstants.EXIT_CODE_ERROR, ddlResult.getExitCode());
Assert.assertTrue(
"Output should explain that database DDL is unsupported, but was: " + ddlOutput,
ddlOutput.contains("Database DDL is not supported for this database type")
);
} finally {
h2Ds.getRegistry().removeDataSource(h2Ds);
Expand Down
Loading