Skip to content

Commit

Permalink
Add some docs. Override config with command line parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jdlopez committed Dec 13, 2020
1 parent 2f1a6a7 commit 2a1af3d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ hs_err_pid*

.idea
*.iml
target/
target/
.classpath
.project
.settings/
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# sqlcmd
SQL command line executor using JDBC drivers. Usefull for cron jobs and some automation. Porting of my previous sf.net code
SQL command line executor using JDBC drivers. Useful for cron jobs and some automation. Porting of my previous sf.net code

Usage:

java -jar sqlcmd.jar -c:sqlcmd_config.properties <<EOF
SELECT *
FROM MY_TABLE
EOF

Config values:

* jdbcDriverPath=Path to jdbc driver jar (loaded dynamically)
* jdbcDriverClass=Jdbc Driver class full name
* jdbcUrl=Jdbc connection URL
* jdbcUser=
* jdbcPass=
* inputSQL=Path to input sql file (not required/optional if not present it uses stdin)
* outputResult=Path to output file (if not present uses stdout)
* printHeader=true/false If true adds header with query's column name. Default true
* printFieldSeparator=Field separator for printing. Default tab (\t)

Config values can be set also with parameters:

-p:paran_name=param_value

Ex:

-p:printHeader=false

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<name>GitHub jdlopez Apache Maven Packages</name>
<url>https://maven.pkg.github.com/jdlopez/sqlcmd</url>
</repository>
</distributionManagement>
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/es/jdlopez/sqlcmd/MainRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,26 @@ private static String readAll(InputStream in) throws IOException {

private static RunnerConfig readArgs(String[] args) throws ConfigException {
RunnerConfig conf = null;
if (args != null)
for (String a: args) {
if (args != null) {
Properties p = new Properties();
for (String a : args) {
if (a.startsWith("-c:")) { // config
Properties p = new Properties();
try {
p.load(new FileReader(a.substring("-c:".length())));
} catch (IOException e) {
throw new ConfigException("configFile", "Reading config file", e);
}
conf = BeanReader.fromProperties(p, RunnerConfig.class);
} // else if ... for any other args TODO
} else if (a != null && a.startsWith("-p:")) {
String s = a.substring("-p:".length());
int idxEq = s != null? s.indexOf("="):-1;
if (idxEq < 0)
throw new ConfigException(s, "Param argument must be: -p:name=value");
else
p.setProperty(s.substring(0, idxEq), s.substring(idxEq + 1));
}
}
else
conf = BeanReader.fromProperties(p, RunnerConfig.class);
} else
throw new ConfigException(null, "No arguments");
// default values (cant use constructor, explicit or implicit)
if (conf.getPrintFieldSeparator() == null)
Expand Down

0 comments on commit 2a1af3d

Please sign in to comment.