1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java Mon Dec 16 12:10:45 2013 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java Mon Dec 16 15:15:32 2013 +0100
1.3 @@ -1,8 +1,10 @@
1.4 package info.globalcode.sql.dk;
1.5
1.6 import static info.globalcode.sql.dk.Functions.isNotEmpty;
1.7 +import static info.globalcode.sql.dk.Functions.equalz;
1.8 import java.util.ArrayList;
1.9 import java.util.Collection;
1.10 +import java.util.EnumSet;
1.11 import java.util.List;
1.12
1.13 /**
1.14 @@ -15,13 +17,24 @@
1.15 private String sql;
1.16 private String databaseName;
1.17 private String namePrefix = DEFAULT_NAME_PREFIX;
1.18 + private String formatterName;
1.19 private boolean batch;
1.20
1.21 public enum MODE {
1.22
1.23 QUERY_NOW,
1.24 PREPARE_BATCH,
1.25 - EXECUTE_BATCH
1.26 + EXECUTE_BATCH,
1.27 + JUST_SHOW_INFO
1.28 + }
1.29 +
1.30 + public enum INFO_TYPE {
1.31 +
1.32 + HELP,
1.33 + VERSION,
1.34 + LICENSE,
1.35 + FORMATTERS,
1.36 + TYPES
1.37 }
1.38
1.39 public enum COMMAND_TYPE {
1.40 @@ -34,12 +47,36 @@
1.41 private COMMAND_TYPE commandType;
1.42 private final Collection<NamedParameter> namedParameters = new ArrayList<>();
1.43 private final List<Parameter> numberedParameters = new ArrayList<>();
1.44 + private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class);
1.45
1.46 public void validate() throws InvalidOptionsException {
1.47 InvalidOptionsException e = new InvalidOptionsException();
1.48
1.49 - if (getMode() == null) {
1.50 + MODE mode = getMode();
1.51 + if (mode == null) {
1.52 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
1.53 + } else if (mode == MODE.JUST_SHOW_INFO) {
1.54 + if (!namedParameters.isEmpty()) {
1.55 + e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
1.56 + }
1.57 + if (!numberedParameters.isEmpty()) {
1.58 + e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
1.59 + }
1.60 + if (isNotEmpty(sql, false)) {
1.61 + e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
1.62 + }
1.63 + if (isNotEmpty(databaseName, false)) {
1.64 + e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
1.65 + }
1.66 + if (batch) {
1.67 + e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
1.68 + }
1.69 + if (isNotEmpty(formatterName, false)) {
1.70 + e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
1.71 + }
1.72 + if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
1.73 + e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
1.74 + }
1.75 }
1.76
1.77 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
1.78 @@ -74,7 +111,7 @@
1.79 } else if (hasDb() && batch && !hasSql()) {
1.80 return MODE.EXECUTE_BATCH;
1.81 } else {
1.82 - return null;
1.83 + return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
1.84 }
1.85 }
1.86
1.87 @@ -129,4 +166,20 @@
1.88 public void setNamePrefix(String namePrefix) {
1.89 this.namePrefix = namePrefix;
1.90 }
1.91 +
1.92 + public String getFormatterName() {
1.93 + return formatterName;
1.94 + }
1.95 +
1.96 + public void setFormatterName(String formatterName) {
1.97 + this.formatterName = formatterName;
1.98 + }
1.99 +
1.100 + public void addShowInfo(INFO_TYPE info) {
1.101 + showInfo.add(info);
1.102 + }
1.103 +
1.104 + public EnumSet<INFO_TYPE> getShowInfo() {
1.105 + return showInfo;
1.106 + }
1.107 }
2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java Mon Dec 16 12:10:45 2013 +0100
2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java Mon Dec 16 15:15:32 2013 +0100
2.3 @@ -96,6 +96,24 @@
2.4 }
2.5 }
2.6 break;
2.7 + case Tokens.FORMATTER:
2.8 + options.setFormatterName(fetchNext(args, ++i));
2.9 + break;
2.10 + case Tokens.INFO_HELP:
2.11 + options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
2.12 + break;
2.13 + case Tokens.INFO_FORMATTERS:
2.14 + options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
2.15 + break;
2.16 + case Tokens.INFO_LICENSE:
2.17 + options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
2.18 + break;
2.19 + case Tokens.INFO_TYPES:
2.20 + options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
2.21 + break;
2.22 + case Tokens.INFO_VERSION:
2.23 + options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
2.24 + break;
2.25 default:
2.26 throw new CLIParserException("Unknown option: " + arg);
2.27 }
2.28 @@ -121,6 +139,12 @@
2.29 public static final String DATA = "--data";
2.30 public static final String NAME_PREFIX = "--name-prefix";
2.31 public static final String TYPES = "--types";
2.32 + public static final String FORMATTER = "--formatter";
2.33 + public static final String INFO_HELP = "--help";
2.34 + public static final String INFO_VERSION = "--version";
2.35 + public static final String INFO_LICENSE = "--license";
2.36 + public static final String INFO_FORMATTERS = "--list-formatters";
2.37 + public static final String INFO_TYPES = "--list-types";
2.38
2.39 private Tokens() {
2.40 }
3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Mon Dec 16 12:10:45 2013 +0100
3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java Mon Dec 16 15:15:32 2013 +0100
3.3 @@ -13,10 +13,23 @@
3.4
3.5 public static void main(String[] args) {
3.6 try {
3.7 + /** Parse options */
3.8 CLIParser parser = new CLIParser();
3.9 CLIOptions options = parser.parseOptions(args);
3.10 +
3.11 +
3.12 + options.validate();
3.13 +
3.14 + /** Show info */
3.15 + if (!options.getShowInfo().isEmpty()) {
3.16 + InfoLister infoLister = new InfoLister();
3.17 + infoLister.showInfo(options.getShowInfo(), System.err);
3.18 + }
3.19 +
3.20 } catch (CLIParserException e) {
3.21 - log.log(Level.SEVERE, null, e);
3.22 + log.log(Level.SEVERE, "Unable to parse CLI options", e);
3.23 + } catch (InvalidOptionsException e) {
3.24 + log.log(Level.SEVERE, "Invalid CLI options", e);
3.25 }
3.26 }
3.27 }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java Mon Dec 16 15:15:32 2013 +0100
4.3 @@ -0,0 +1,39 @@
4.4 +package info.globalcode.sql.dk;
4.5 +
4.6 +import java.io.PrintStream;
4.7 +import java.util.EnumSet;
4.8 +
4.9 +/**
4.10 + * Displays info like help, version etc.
4.11 + *
4.12 + * @author Ing. František Kučera (frantovo.cz)
4.13 + */
4.14 +public class InfoLister {
4.15 +
4.16 + public void showInfo(EnumSet<CLIOptions.INFO_TYPE> infoTypes, PrintStream out) {
4.17 + for (CLIOptions.INFO_TYPE infoType : infoTypes) {
4.18 + switch (infoType) {
4.19 + /**
4.20 + * TODO: implement show info
4.21 + */
4.22 + case FORMATTERS:
4.23 + out.println("TODO: list available formatters");
4.24 + break;
4.25 + case HELP:
4.26 + out.println("TODO: show some help");
4.27 + break;
4.28 + case LICENSE:
4.29 + out.println("TODO: show license");
4.30 + break;
4.31 + case TYPES:
4.32 + out.println("TODO: list supported types");
4.33 + break;
4.34 + case VERSION:
4.35 + out.println("TODO: show version");
4.36 + break;
4.37 + default:
4.38 + throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
4.39 + }
4.40 + }
4.41 + }
4.42 +}
5.1 --- a/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java Mon Dec 16 12:10:45 2013 +0100
5.2 +++ b/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java Mon Dec 16 15:15:32 2013 +0100
5.3 @@ -159,4 +159,15 @@
5.4 assertEquals(options.getDatabaseName(), DATABASE_NAME_1);
5.5 assertEquals(options.getMode(), CLIOptions.MODE.EXECUTE_BATCH);
5.6 }
5.7 +
5.8 + @Test
5.9 + public void testParseOptions_ShowInfo_Help() throws InvalidOptionsException, CLIParserException {
5.10 + String[] args = new String[]{Tokens.INFO_HELP};
5.11 + CLIOptions options = parser.parseOptions(args);
5.12 + options.validate();
5.13 +
5.14 + assertEquals(options.getMode(), CLIOptions.MODE.JUST_SHOW_INFO);
5.15 + assertEquals(options.getShowInfo().size(), 1);
5.16 + assertTrue(options.getShowInfo().contains(CLIOptions.INFO_TYPE.HELP));
5.17 + }
5.18 }
5.19 \ No newline at end of file