forked from utPLSQL/utPLSQL-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionInfo.java
More file actions
138 lines (105 loc) · 3.62 KB
/
ConnectionInfo.java
File metadata and controls
138 lines (105 loc) · 3.62 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.utplsql.cli;
import com.beust.jcommander.ParameterException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Created by Vinicius on 21/04/2017.
*/
public class ConnectionInfo {
private static final String DEFAULT_HOST = "127.0.0.1";
private static final int DEFAULT_PORT = 1521;
private String user;
private String password;
private String host;
private int port;
private String database;
public ConnectionInfo() {}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(getConnectionUrl(), getUser(), getPassword());
}
public ConnectionInfo parseConnectionString(String connectionString)
throws ParameterException, IllegalArgumentException {
if (connectionString == null || connectionString.isEmpty())
throw invalidConnectionString();
int i = connectionString.lastIndexOf("@");
if (i == -1)
throw invalidConnectionString();
String credentials = connectionString.substring(0, i);
String host = connectionString.substring(i+1);
parseCredentials(credentials);
parseHost(host);
return this;
}
private void parseCredentials(String str) throws ParameterException, IllegalArgumentException {
int barIdx = str.indexOf("/");
if (barIdx == -1 || barIdx == 0 || barIdx == str.length() - 1)
throw invalidConnectionString();
this.setUser(str.substring(0, barIdx));
this.setPassword(str.substring(barIdx+1));
}
private void parseHost(String str) throws ParameterException, IllegalArgumentException {
if (str == null || str.isEmpty())
throw invalidConnectionString();
int colonIdx = str.indexOf(":");
int barIdx = str.indexOf("/");
if ((colonIdx != -1 && barIdx == -1) || barIdx == 0) // @host:port or // @/db
throw invalidConnectionString();
if (colonIdx != -1) { // @host:port/db
setHost(str.substring(0, colonIdx));
setPort(Integer.parseInt(str.substring(colonIdx + 1, barIdx)));
setDatabase(str.substring(barIdx + 1));
}
else
if (barIdx != -1) { // @host/db
setHost(str.substring(0, barIdx));
setPort(DEFAULT_PORT);
setDatabase(str.substring(barIdx + 1));
}
else { // @db
setHost(DEFAULT_HOST);
setPort(DEFAULT_PORT);
setDatabase(str);
}
}
private ParameterException invalidConnectionString() {
return new ParameterException("Invalid connection string.");
}
public String getUser() {
return user;
}
private void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
private void setPassword(String password) {
this.password = password;
}
public String getHost() {
return host;
}
private void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
private void setPort(int port) {
this.port = port;
}
public String getDatabase() {
return database;
}
private void setDatabase(String database) {
this.database = database;
}
public String getConnectionUrl() {
return String.format("jdbc:oracle:thin:@//%s:%d/%s", getHost(), getPort(), getDatabase());
}
@Override
public String toString() {
return String.format("%s@%s:%d/%s", getUser(), getHost(), getPort(), getDatabase());
}
}