/
DbSqlCommand.java
123 lines (108 loc) · 4.44 KB
/
DbSqlCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.dbcp.BasicDataSource;
import org.h2.tools.Shell;
public class DbSqlCommand extends AbstractCommandLauncher {
private static final String OPTION_SQL = "sql";
private static final String OPTION_SQLFILE = "sqlfile";
private Options localOptions;
public DbSqlCommand() {
super("dbsql", "", "DbSql.Option.");
}
public static void main(String[] args) {
new DbSqlCommand().execute(args);
}
@Override
protected void printHelp(CommandLine cmd, Options options) {
System.out.println(app + " version " + Version.version());
System.out.println("Provides a sql shell for database interaction from the command line.\n");
super.printHelp(cmd, options);
}
@Override
protected void buildOptions(Options options) {
super.buildOptions(options);
addOption(options, null, OPTION_SQL, true);
addOption(options, null, OPTION_SQLFILE, true);
// Need reference to it for later, if errors
localOptions = options;
}
@Override
protected boolean printHelpIfNoOptionsAreProvided() {
return false;
}
@Override
protected boolean requiresPropertiesFile(CommandLine line) {
return true;
}
@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
BasicDataSource basicDataSource = getDatabasePlatform(false).getDataSource();
String url = basicDataSource.getUrl();
String user = basicDataSource.getUsername();
String password = basicDataSource.getPassword();
String driver = basicDataSource.getDriverClassName();
Shell shell = new Shell();
if (line.hasOption(OPTION_SQL)) {
String sql = line.getOptionValue(OPTION_SQL);
shell.runTool("-url", url, "-user", user, "-password", password, "-driver", driver, "-sql", sql);
} else if(line.hasOption(OPTION_SQLFILE)) {
File file = new File(line.getOptionValue(OPTION_SQLFILE));
if(file.exists()) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String sql = null;
while((sql = br.readLine()) != null) {
sql = sql.trim();
if(sql.endsWith(";")) {
sql = sql.substring(0,sql.length()-1);
}
if(sql.length() > 0) {
// Output the sql so the user knows the result of each sql statement
// The H2 shell tool outputs the result of the statement execution
System.out.println(sql);
shell.runTool("-url", url, "-user", user, "-password", password, "-driver", driver, "-sql", sql);
}
}
} finally {
if(br != null) {
br.close();
}
}
} else {
// Notify user about missing file name
System.err.println("-------------------------------------------------------------------------------");
System.err.println("File does not exist: " + file.getPath());
System.err.println("-------------------------------------------------------------------------------");
printHelp(line, localOptions);
}
} else {
shell.runTool("-url", url, "-user", user, "-password", password, "-driver", driver);
}
return true;
}
}