Skip to content

Commit

Permalink
[#6420] improve(CLI): Add column command context CLI (#6436)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Add column command context CLI

### Why are the changes needed?

Fix: #6420 

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

local test.
  • Loading branch information
Abyss-lord authored Feb 11, 2025
1 parent 82087ef commit 918fc49
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class ColumnCommandHandler extends CommandHandler {
private final GravitinoCommandLine gravitinoCommandLine;
private final CommandLine line;
private final String command;
private final boolean ignore;
private final String url;
private final CommandContext context;
private final FullName name;
private final String metalake;
private final String catalog;
Expand All @@ -44,16 +43,19 @@ public class ColumnCommandHandler extends CommandHandler {
* @param gravitinoCommandLine The Gravitino command line instance.
* @param line The command line arguments.
* @param command The command to execute.
* @param ignore Ignore server version mismatch.
* @param context The command context.
*/
public ColumnCommandHandler(
GravitinoCommandLine gravitinoCommandLine, CommandLine line, String command, boolean ignore) {
GravitinoCommandLine gravitinoCommandLine,
CommandLine line,
String command,
CommandContext context) {
this.gravitinoCommandLine = gravitinoCommandLine;
this.line = line;
this.command = command;
this.ignore = ignore;
this.context = context;

this.url = getUrl(line);
this.context.setUrl(getUrl(line));
this.name = new FullName(line);
this.metalake = name.getMetalakeName();
this.catalog = name.getCatalogName();
Expand Down Expand Up @@ -120,7 +122,7 @@ private boolean executeCommand() {
private void handleDetailsCommand() {
if (line.hasOption(GravitinoOptions.AUDIT)) {
gravitinoCommandLine
.newColumnAudit(url, ignore, metalake, catalog, schema, table, column)
.newColumnAudit(context, metalake, catalog, schema, table, column)
.validate()
.handle();
} else {
Expand All @@ -144,8 +146,7 @@ private void handleCreateCommand() {

gravitinoCommandLine
.newAddColumn(
url,
ignore,
context,
metalake,
catalog,
schema,
Expand All @@ -164,7 +165,7 @@ private void handleCreateCommand() {
/** Handles the "DELETE" command. */
private void handleDeleteCommand() {
gravitinoCommandLine
.newDeleteColumn(url, ignore, metalake, catalog, schema, table, column)
.newDeleteColumn(context, metalake, catalog, schema, table, column)
.validate()
.handle();
}
Expand All @@ -174,44 +175,43 @@ private void handleUpdateCommand() {
if (line.hasOption(GravitinoOptions.COMMENT)) {
String comment = line.getOptionValue(GravitinoOptions.COMMENT);
gravitinoCommandLine
.newUpdateColumnComment(url, ignore, metalake, catalog, schema, table, column, comment)
.newUpdateColumnComment(context, metalake, catalog, schema, table, column, comment)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.RENAME)) {
String newName = line.getOptionValue(GravitinoOptions.RENAME);
gravitinoCommandLine
.newUpdateColumnName(url, ignore, metalake, catalog, schema, table, column, newName)
.newUpdateColumnName(context, metalake, catalog, schema, table, column, newName)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.DATATYPE) && !line.hasOption(GravitinoOptions.DEFAULT)) {
String datatype = line.getOptionValue(GravitinoOptions.DATATYPE);
gravitinoCommandLine
.newUpdateColumnDatatype(url, ignore, metalake, catalog, schema, table, column, datatype)
.newUpdateColumnDatatype(context, metalake, catalog, schema, table, column, datatype)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.POSITION)) {
String position = line.getOptionValue(GravitinoOptions.POSITION);
gravitinoCommandLine
.newUpdateColumnPosition(url, ignore, metalake, catalog, schema, table, column, position)
.newUpdateColumnPosition(context, metalake, catalog, schema, table, column, position)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.NULL)) {
boolean nullable = line.getOptionValue(GravitinoOptions.NULL).equals("true");
gravitinoCommandLine
.newUpdateColumnNullability(
url, ignore, metalake, catalog, schema, table, column, nullable)
.newUpdateColumnNullability(context, metalake, catalog, schema, table, column, nullable)
.validate()
.handle();
}
if (line.hasOption(GravitinoOptions.AUTO)) {
boolean autoIncrement = line.getOptionValue(GravitinoOptions.AUTO).equals("true");
gravitinoCommandLine
.newUpdateColumnAutoIncrement(
url, ignore, metalake, catalog, schema, table, column, autoIncrement)
context, metalake, catalog, schema, table, column, autoIncrement)
.validate()
.handle();
}
Expand All @@ -220,7 +220,7 @@ private void handleUpdateCommand() {
String dataType = line.getOptionValue(GravitinoOptions.DATATYPE);
gravitinoCommandLine
.newUpdateColumnDefault(
url, ignore, metalake, catalog, schema, table, column, defaultValue, dataType)
context, metalake, catalog, schema, table, column, defaultValue, dataType)
.validate()
.handle();
}
Expand All @@ -229,7 +229,7 @@ private void handleUpdateCommand() {
/** Handles the "LIST" command. */
private void handleListCommand() {
gravitinoCommandLine
.newListColumns(url, ignore, metalake, catalog, schema, table)
.newListColumns(context, metalake, catalog, schema, table)
.validate()
.handle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void executeCommand() {
} else if (line.hasOption(GravitinoOptions.OWNER)) {
new OwnerCommandHandler(this, line, command, ignore, entity).handle();
} else if (entity.equals(CommandEntities.COLUMN)) {
new ColumnCommandHandler(this, line, command, ignore).handle();
new ColumnCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.TABLE)) {
new TableCommandHandler(this, line, command, context).handle();
} else if (entity.equals(CommandEntities.SCHEMA)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,19 +538,18 @@ protected UntagEntity newUntagEntity(
}

protected ColumnAudit newColumnAudit(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String column) {
return new ColumnAudit(url, ignore, metalake, catalog, schema, table, column);
return new ColumnAudit(context, metalake, catalog, schema, table, column);
}

protected ListColumns newListColumns(
String url, boolean ignore, String metalake, String catalog, String schema, String table) {
return new ListColumns(url, ignore, metalake, catalog, schema, table);
CommandContext context, String metalake, String catalog, String schema, String table) {
return new ListColumns(context, metalake, catalog, schema, table);
}

protected SetOwner newSetOwner(
Expand Down Expand Up @@ -703,8 +702,7 @@ protected RemoveFilesetProperty newRemoveFilesetProperty(
}

protected AddColumn newAddColumn(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
Expand All @@ -717,8 +715,7 @@ protected AddColumn newAddColumn(
boolean autoIncrement,
String defaultValue) {
return new AddColumn(
url,
ignore,
context,
metalake,
catalog,
schema,
Expand All @@ -733,95 +730,84 @@ protected AddColumn newAddColumn(
}

protected DeleteColumn newDeleteColumn(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String column) {
return new DeleteColumn(url, ignore, metalake, catalog, schema, table, column);
return new DeleteColumn(context, metalake, catalog, schema, table, column);
}

protected UpdateColumnComment newUpdateColumnComment(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String column,
String comment) {
return new UpdateColumnComment(url, ignore, metalake, catalog, schema, table, column, comment);
return new UpdateColumnComment(context, metalake, catalog, schema, table, column, comment);
}

protected UpdateColumnName newUpdateColumnName(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String column,
String rename) {
return new UpdateColumnName(url, ignore, metalake, catalog, schema, table, column, rename);
return new UpdateColumnName(context, metalake, catalog, schema, table, column, rename);
}

protected UpdateColumnDatatype newUpdateColumnDatatype(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String column,
String datatype) {
return new UpdateColumnDatatype(
url, ignore, metalake, catalog, schema, table, column, datatype);
return new UpdateColumnDatatype(context, metalake, catalog, schema, table, column, datatype);
}

protected UpdateColumnPosition newUpdateColumnPosition(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String column,
String position) {
return new UpdateColumnPosition(
url, ignore, metalake, catalog, schema, table, column, position);
return new UpdateColumnPosition(context, metalake, catalog, schema, table, column, position);
}

protected UpdateColumnNullability newUpdateColumnNullability(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String column,
boolean nullable) {
return new UpdateColumnNullability(
url, ignore, metalake, catalog, schema, table, column, nullable);
return new UpdateColumnNullability(context, metalake, catalog, schema, table, column, nullable);
}

protected UpdateColumnAutoIncrement newUpdateColumnAutoIncrement(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
String table,
String column,
boolean autoIncrement) {
return new UpdateColumnAutoIncrement(
url, ignore, metalake, catalog, schema, table, column, autoIncrement);
context, metalake, catalog, schema, table, column, autoIncrement);
}

protected UpdateColumnDefault newUpdateColumnDefault(
String url,
boolean ignore,
CommandContext context,
String metalake,
String catalog,
String schema,
Expand All @@ -830,7 +816,7 @@ protected UpdateColumnDefault newUpdateColumnDefault(
String defaultValue,
String dataType) {
return new UpdateColumnDefault(
url, ignore, metalake, catalog, schema, table, column, defaultValue, dataType);
context, metalake, catalog, schema, table, column, defaultValue, dataType);
}

protected CreateTable newCreateTable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.gravitino.cli.commands;

import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.DefaultConverter;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.cli.ParseType;
Expand Down Expand Up @@ -49,8 +50,7 @@ public class AddColumn extends Command {
/**
* Adds an optional column to a table.
*
* @param url The URL of the Gravitino server.
* @param ignoreVersions If true don't check the client/server versions match.
* @param context The command context.
* @param metalake The name of the metalake.
* @param catalog The name of the catalog.
* @param schema The name of the schema.
Expand All @@ -64,8 +64,7 @@ public class AddColumn extends Command {
* @param defaultValue Default value of the column (optional).
*/
public AddColumn(
String url,
boolean ignoreVersions,
CommandContext context,
String metalake,
String catalog,
String schema,
Expand All @@ -77,7 +76,7 @@ public AddColumn(
boolean nullable,
boolean autoIncrement,
String defaultValue) {
super(url, ignoreVersions);
super(context);
this.metalake = metalake;
this.catalog = catalog;
this.schema = schema;
Expand Down Expand Up @@ -123,6 +122,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

System.out.println(column + " added to table " + table + ".");
printInformation(column + " added to table " + table + ".");
}
}
Loading

0 comments on commit 918fc49

Please sign in to comment.