|
16 | 16 | */ |
17 | 17 | package com.google.edwmigration.dumper.application.dumper.connector.oracle; |
18 | 18 |
|
| 19 | +import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_HOST; |
| 20 | +import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_ORACLE_SERVICE; |
| 21 | +import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_ORACLE_SID; |
| 22 | +import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_PORT; |
| 23 | +import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_URI; |
| 24 | + |
19 | 25 | import com.google.edwmigration.dumper.application.dumper.ConnectorArguments; |
20 | 26 | import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException; |
21 | 27 | import com.google.edwmigration.dumper.application.dumper.annotations.RespectsArgumentDriverRequired; |
@@ -142,20 +148,6 @@ String getFormatName() { |
142 | 148 | return connectorScope.formatName(); |
143 | 149 | } |
144 | 150 |
|
145 | | - private static boolean isOracleSid(ConnectorArguments arguments) |
146 | | - throws MetadataDumperUsageException { |
147 | | - String service = arguments.getOracleServicename(); |
148 | | - String sid = arguments.getOracleSID(); |
149 | | - if (sid != null && service == null) { |
150 | | - return true; |
151 | | - } else if (sid == null && service != null) { |
152 | | - return false; |
153 | | - } else { |
154 | | - throw new MetadataDumperUsageException( |
155 | | - "Provide either -oracle-service or -oracle-sid for oracle dumper"); |
156 | | - } |
157 | | - } |
158 | | - |
159 | 151 | @Nonnull |
160 | 152 | @Override |
161 | 153 | public Handle open(@Nonnull ConnectorArguments arguments) throws Exception { |
@@ -190,15 +182,50 @@ Properties buildProperties(@Nonnull ConnectorArguments arguments) { |
190 | 182 | // jdbc:oracle:thin:<TNSName> (from 10.2.0.1.0) |
191 | 183 | static String buildUrl(ConnectorArguments arguments) { |
192 | 184 | String url = arguments.getUri(); |
193 | | - if (url == null) { |
194 | | - String host = arguments.getHost(); |
195 | | - int port = arguments.getPort(OPT_PORT_DEFAULT); |
196 | | - if (isOracleSid(arguments)) { |
197 | | - url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + arguments.getOracleSID(); |
198 | | - } else { |
199 | | - url = "jdbc:oracle:thin:@//" + host + ":" + port + "/" + arguments.getOracleServicename(); |
200 | | - } |
| 185 | + String host = arguments.getHost(); |
| 186 | + int port = arguments.getPort(OPT_PORT_DEFAULT); |
| 187 | + if (url != null) { |
| 188 | + checkNonUriFlags(arguments); |
| 189 | + return url; |
| 190 | + } |
| 191 | + checkServiceAndSid(arguments); |
| 192 | + if (arguments.getOracleSID() != null) { |
| 193 | + return "jdbc:oracle:thin:@" + host + ":" + port + ":" + arguments.getOracleSID(); |
| 194 | + } else { |
| 195 | + return "jdbc:oracle:thin:@//" + host + ":" + port + "/" + arguments.getOracleServicename(); |
201 | 196 | } |
202 | | - return url; |
| 197 | + } |
| 198 | + |
| 199 | + private static void checkNonUriFlags(ConnectorArguments arguments) { |
| 200 | + if (arguments.getHost() != null) { |
| 201 | + throw extraFlagProvided(OPT_HOST); |
| 202 | + } else if (arguments.getPort() != null) { |
| 203 | + throw extraFlagProvided(OPT_PORT); |
| 204 | + } else if (arguments.getOracleServicename() != null) { |
| 205 | + throw extraFlagProvided(OPT_ORACLE_SERVICE); |
| 206 | + } else if (arguments.getOracleSID() != null) { |
| 207 | + throw extraFlagProvided(OPT_ORACLE_SID); |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private static void checkServiceAndSid(ConnectorArguments arguments) { |
| 212 | + boolean hasService = arguments.getOracleServicename() != null; |
| 213 | + boolean hasSid = arguments.getOracleSID() != null; |
| 214 | + if ((!hasService && !hasSid) || (hasService && hasSid)) { |
| 215 | + String message = |
| 216 | + String.format( |
| 217 | + "Provide either --%s or --%s for oracle dumper", OPT_ORACLE_SERVICE, OPT_ORACLE_SID); |
| 218 | + throw new MetadataDumperUsageException(message); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + private static MetadataDumperUsageException extraFlagProvided(String flagName) { |
| 223 | + String[] sentences = |
| 224 | + new String[] { |
| 225 | + String.format("Both the --%s and --%s flags were provided.", OPT_URI, flagName), |
| 226 | + String.format("If the --%s value is valid, please omit --%s.", OPT_URI, flagName), |
| 227 | + String.format("If all connection parameters are provided, please omit the --%s", OPT_URI) |
| 228 | + }; |
| 229 | + return new MetadataDumperUsageException(String.join(" ", sentences)); |
203 | 230 | } |
204 | 231 | } |
0 commit comments