SQL from STDIN: --sql-in v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 17 Jan 2014 23:24:07 +0100
branchv_0
changeset 1665488c2dcf680
parent 165 871185e406b3
child 167 84aaa91642bf
SQL from STDIN: --sql-in
java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/Functions.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Thu Jan 16 12:28:08 2014 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Fri Jan 17 23:24:07 2014 +0100
     1.3 @@ -17,8 +17,11 @@
     1.4   */
     1.5  package info.globalcode.sql.dk;
     1.6  
     1.7 +import static info.globalcode.sql.dk.Functions.readString;
     1.8  import info.globalcode.sql.dk.InfoLister.InfoType;
     1.9  import info.globalcode.sql.dk.configuration.Property;
    1.10 +import java.io.IOException;
    1.11 +import java.io.InputStream;
    1.12  import java.util.ArrayList;
    1.13  import java.util.HashMap;
    1.14  import java.util.List;
    1.15 @@ -35,7 +38,7 @@
    1.16  
    1.17  	public static final String TYPE_NAME_SEPARATOR = ":";
    1.18  
    1.19 -	public CLIOptions parseOptions(String[] args) throws CLIParserException {
    1.20 +	public CLIOptions parseOptions(String[] args, InputStream in) throws CLIParserException {
    1.21  		CLIOptions options = new CLIOptions();
    1.22  
    1.23  		List<SQLType> numberedTypes = new ArrayList<>();
    1.24 @@ -70,6 +73,13 @@
    1.25  				case Tokens.SQL:
    1.26  					options.setSql(fetchNext(args, ++i));
    1.27  					break;
    1.28 +				case Tokens.SQL_IN:
    1.29 +					try {
    1.30 +						options.setSql(readString(in));
    1.31 +					} catch (IOException e) {
    1.32 +						throw new CLIParserException("Unable to read SQL from the input stream", e);
    1.33 +					}
    1.34 +					break;
    1.35  				case Tokens.BATCH:
    1.36  					options.setBatch(true);
    1.37  					break;
    1.38 @@ -160,6 +170,7 @@
    1.39  		public static final String DB = "--db"; // bash-completion:option // help: database name
    1.40  		public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
    1.41  		public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
    1.42 +		public static final String SQL_IN = "--sql-in"; // bash-completion:option // help: SQL query/command
    1.43  		public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
    1.44  		public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
    1.45  		public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Thu Jan 16 12:28:08 2014 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Fri Jan 17 23:24:07 2014 +0100
     2.3 @@ -75,7 +75,7 @@
     2.4  
     2.5  		try {
     2.6  			CLIParser parser = new CLIParser();
     2.7 -			CLIOptions options = parser.parseOptions(args);
     2.8 +			CLIOptions options = parser.parseOptions(args, System.in);
     2.9  			options.validate();
    2.10  			CLIStarter starter = new CLIStarter(options);
    2.11  			starter.installDefaultConfiguration();
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Thu Jan 16 12:28:08 2014 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Fri Jan 17 23:24:07 2014 +0100
     3.3 @@ -21,12 +21,14 @@
     3.4  import java.io.BufferedReader;
     3.5  import java.io.File;
     3.6  import java.io.IOException;
     3.7 +import java.io.InputStream;
     3.8  import java.io.InputStreamReader;
     3.9  import java.io.PrintWriter;
    3.10  import java.util.Arrays;
    3.11  import java.util.Collection;
    3.12  import java.util.Collections;
    3.13  import java.util.Map;
    3.14 +import java.util.Scanner;
    3.15  
    3.16  /**
    3.17   *
    3.18 @@ -152,4 +154,15 @@
    3.19  		}
    3.20  		return new String(hexChars);
    3.21  	}
    3.22 +
    3.23 +	public static String readString(InputStream in) throws IOException {
    3.24 +		try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
    3.25 +			StringBuilder result = new StringBuilder();
    3.26 +			for (String line = br.readLine(); line != null; line = br.readLine()) {
    3.27 +				result.append(line);
    3.28 +				result.append('\n');
    3.29 +			}
    3.30 +			return result.toString();
    3.31 +		}
    3.32 +	}
    3.33  }