Skip to content

Commit

Permalink
allow launcher to describe arg syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Apr 16, 2012
1 parent 2334cc3 commit c58ff5e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 24 deletions.
Expand Up @@ -72,7 +72,9 @@ public abstract class AbstractCommandLauncher {

protected static final String COMMON_MESSAGE_KEY_PREFIX = "Common.Option.";

protected String commandName;
protected String app;

protected String argSyntax;

protected String messageKeyPrefix;

Expand All @@ -82,8 +84,9 @@ public abstract class AbstractCommandLauncher {

protected IDatabasePlatform platform;

public AbstractCommandLauncher(String commandName, String messageKeyPrefix) {
this.commandName = commandName;
public AbstractCommandLauncher(String app, String argSyntax, String messageKeyPrefix) {
this.app = app;
this.argSyntax = argSyntax;
this.messageKeyPrefix = messageKeyPrefix;
}

Expand Down Expand Up @@ -126,13 +129,13 @@ public void execute(String args[]) {
}

protected void printHelp(Options options) {
new HelpFormatter().printHelp(commandName, options);
new HelpFormatter().printHelp(app + " " + argSyntax, options);
}

protected void printUsage(Options options) {
PrintWriter writer = new PrintWriter(System.out);
new HelpFormatter().printUsage(writer, 80, commandName, options);
writer.write("For more options, use " + commandName + " --" + OPTION_HELP + "\n");
new HelpFormatter().printUsage(writer, 80, app, options);
writer.write("For more options, use " + app + " --" + OPTION_HELP + "\n");
writer.flush();
}

Expand Down
Expand Up @@ -66,6 +66,8 @@ public enum Compatible { DB2, DERBY, FIREBIRD, H2, HSQLDB, HSQLDB2, INFORMIX, IN

private boolean noData;

private boolean ignoreMissingTables;

private boolean comments;

private String catalog;
Expand Down Expand Up @@ -115,7 +117,7 @@ public void exportTables(OutputStream output, String[] tableNames) throws IOExce
Table table = platform.readTableFromDatabase(catalog, schema, tableName);
if (table != null) {
tableList.add(table);
} else {
} else if (! ignoreMissingTables){
throw new RuntimeException("Cannot find table " + tableName + " in catalog " + catalog +
" and schema " + schema);
}
Expand All @@ -132,8 +134,8 @@ public void exportTables(OutputStream output, Table[] tables) throws IOException
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
csvWriter.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
writeComment(writer, "SymmetricDS " + Version.version() + " " + DbExport.class.getSimpleName());
writeComment(writer, "Catalog: " + catalog);
writeComment(writer, "Schema: " + schema);
writeComment(writer, "Catalog: " + (catalog != null ? catalog : ""));
writeComment(writer, "Schema: " + (schema != null ? schema : ""));
writeComment(writer, "Started on " + df.format(new Date()));

for (Table table : tables) {
Expand Down Expand Up @@ -256,4 +258,12 @@ public String getSchema() {
public void setSchema(String schema) {
this.schema = schema;
}

public boolean isIgnoreMissingTables() {
return ignoreMissingTables;
}

public void setIgnoreMissingTables(boolean ignoreMissingTables) {
this.ignoreMissingTables = ignoreMissingTables;
}
}
Expand Up @@ -44,15 +44,15 @@ public class DbExportCommand extends AbstractCommandLauncher {
private static final String OPTION_COMMENTS = "comments";

public DbExportCommand() {
super("dbexport", "DbExport.Option.");
super("dbexport", "[tablename...]", "DbExport.Option.");
}

public static void main(String[] args) {
new DbExportCommand().execute(args);
}

protected void printHelp(Options options) {
System.out.println(commandName + " version " + Version.version());
System.out.println(app + " version " + Version.version());
System.out.println("Export the structure and data from database tables to file.\n");
super.printHelp(options);
}
Expand Down
Expand Up @@ -54,7 +54,7 @@ public enum Format { SQL, CSV, XML };
private String catalog;

private String schema;

private IDatabasePlatform platform;

public DbImport(IDatabasePlatform platform) {
Expand All @@ -72,18 +72,22 @@ public void importTables(String importData) throws IOException {
}

public void importTables(InputStream in) throws IOException {
importTables(in, null);
}

public void importTables(InputStream in, String tableName) throws IOException {
if (format == Format.SQL) {
importTablesFromSql(in);
} else if (format == Format.CSV) {
importTablesFromCsv(in);
importTablesFromCsv(in, tableName);
} else if (format == Format.XML) {
importTablesFromXml(in);
}
}
protected void importTablesFromCsv(InputStream in) throws IOException {

protected void importTablesFromCsv(InputStream in, String tableName) throws IOException {
ISqlTemplate sqlTemplate = platform.getSqlTemplate();
Table table = platform.readTableFromDatabase(catalog, schema, "item");
Table table = platform.readTableFromDatabase(catalog, schema, tableName);
if (table == null) {
throw new RuntimeException("Unable to find table");
}
Expand Down Expand Up @@ -154,4 +158,5 @@ public IDatabasePlatform getPlatform() {
public void setPlatform(IDatabasePlatform platform) {
this.platform = platform;
}

}
Expand Up @@ -34,17 +34,23 @@
public class DbImportCommand extends AbstractCommandLauncher {

private static final String OPTION_FORMAT = "format";

private static final String OPTION_CATALOG = "catalog";

private static final String OPTION_SCHEMA = "schema";

private static final String OPTION_TABLE = "table";

public DbImportCommand() {
super("dbimport", "DbImport.Option.");
super("dbimport", "[file...]", "DbImport.Option.");
}

public static void main(String[] args) {
new DbImportCommand().execute(args);
}

protected void printHelp(Options options) {
System.out.println(commandName + " version " + Version.version());
System.out.println(app + " version " + Version.version());
System.out.println("Import data from file to database tables.\n");
super.printHelp(options);
}
Expand All @@ -53,6 +59,9 @@ protected void printHelp(Options options) {
protected void buildOptions(Options options) {
super.buildOptions(options);
addOption(options, null, OPTION_FORMAT, true);
addOption(options, null, OPTION_CATALOG, true);
addOption(options, null, OPTION_SCHEMA, true);
addOption(options, null, OPTION_TABLE, true);
}

@Override
Expand All @@ -62,10 +71,16 @@ protected boolean executeOptions(CommandLine line) throws Exception {
if (line.hasOption(OPTION_FORMAT)) {
dbImport.setFormat(Format.valueOf(line.getOptionValue(OPTION_FORMAT).toUpperCase()));
}
if (line.hasOption(OPTION_CATALOG)) {
dbImport.setCatalog(line.getOptionValue(OPTION_CATALOG));
}
if (line.hasOption(OPTION_SCHEMA)) {
dbImport.setSchema(line.getOptionValue(OPTION_SCHEMA));
}

String[] args = line.getArgs();
if (args.length == 0) {
dbImport.importTables(System.in);
dbImport.importTables(System.in, line.getOptionValue(OPTION_TABLE));
} else {
for (String fileName : args) {
if (! new File(fileName).exists()) {
Expand All @@ -74,7 +89,7 @@ protected boolean executeOptions(CommandLine line) throws Exception {
}
for (String fileName : args) {
FileInputStream in = new FileInputStream(fileName);
dbImport.importTables(in);
dbImport.importTables(in, line.getOptionValue(OPTION_TABLE));
in.close();
}
}
Expand Down
Expand Up @@ -77,16 +77,16 @@ public class SymmetricAdmin extends AbstractCommandLauncher {

private static final String OPTION_CREATE_WAR = "create-war";

public SymmetricAdmin(String commandName, String messageKeyPrefix) {
super(commandName, messageKeyPrefix);
public SymmetricAdmin(String app, String argSyntax, String messageKeyPrefix) {
super(app, argSyntax, messageKeyPrefix);
}

public static void main(String[] args) throws Exception {
new SymmetricAdmin("symadmin", "SymAdmin.Option.").execute(args);
new SymmetricAdmin("symadmin", "", "SymAdmin.Option.").execute(args);
}

protected void printHelp(Options options) {
System.out.println(commandName + " version " + Version.version());
System.out.println(app + " version " + Version.version());
System.out.println("Perform administration tasks with SymmetricDS.\n");
super.printHelp(options);
}
Expand Down
Expand Up @@ -45,3 +45,10 @@ DbExport.Option.no-create-info=Do not write statements to create tables.
DbExport.Option.no-data=Do not write statements to insert into tables.
DbExport.Option.comments=Write informational comments.
DbExport.Option.format=Output format: SQL, CSV, or XML.
DbExport.Option.catalog=Look for tables in catalog.
DbExport.Option.schema=Look for tables in schema.

DbImport.Option.format=Input format: SQL, CSV, or XML.
DbImport.Option.catalog=Look for tables in catalog.
DbImport.Option.schema=Look for tables in schema.
DbImport.Option.table=Specify table to import.

0 comments on commit c58ff5e

Please sign in to comment.