comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 940:c5e47b8a509c

Prefer getters of class Target.Validated over class Target This makes it easier to check that all settings are used.
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Wed, 08 Jan 2025 16:30:25 +0100 (3 months ago)
parents d416e9b6b3d0
children ff075ed5ce81
comparison
equal deleted inserted replaced
939:fd938d0a2b3a 940:c5e47b8a509c
37 import org.monetdb.mcl.io.BufferedMCLWriter; 37 import org.monetdb.mcl.io.BufferedMCLWriter;
38 import org.monetdb.mcl.io.LineType; 38 import org.monetdb.mcl.io.LineType;
39 import org.monetdb.mcl.net.ClientInfo; 39 import org.monetdb.mcl.net.ClientInfo;
40 import org.monetdb.mcl.net.MapiSocket; 40 import org.monetdb.mcl.net.MapiSocket;
41 import org.monetdb.mcl.net.Target; 41 import org.monetdb.mcl.net.Target;
42 import org.monetdb.mcl.net.ValidationError;
42 import org.monetdb.mcl.parser.HeaderLineParser; 43 import org.monetdb.mcl.parser.HeaderLineParser;
43 import org.monetdb.mcl.parser.MCLParseException; 44 import org.monetdb.mcl.parser.MCLParseException;
44 import org.monetdb.mcl.parser.StartOfHeaderParser; 45 import org.monetdb.mcl.parser.StartOfHeaderParser;
45 46
46 /** 47 /**
150 */ 151 */
151 MonetConnection(Target target) 152 MonetConnection(Target target)
152 throws SQLException, IllegalArgumentException 153 throws SQLException, IllegalArgumentException
153 { 154 {
154 this.target = target; 155 this.target = target;
156 Target.Validated validated;
157 try {
158 validated = target.validate();
159 } catch (ValidationError e) {
160 throw new SQLNonTransientConnectionException(e.getMessage());
161 }
155 server = new MapiSocket(); 162 server = new MapiSocket();
156 163
157 // we're debugging here... uhm, should be off in real life 164 // we're debugging here... uhm, should be off in real life
158 if (target.isDebug()) { 165 if (validated.isDebug()) {
159 try { 166 try {
160 String fname = target.getLogfile(); 167 String fname = validated.getLogfile();
161 if (fname == null) 168 if (fname == null)
162 fname = "monet_" + System.currentTimeMillis() + ".log"; 169 fname = "monet_" + System.currentTimeMillis() + ".log";
163 170
164 File f = new File(fname); 171 File f = new File(fname);
165 172
178 throw new SQLNonTransientConnectionException("Opening logfile failed: " + ex.getMessage(), "08M01"); 185 throw new SQLNonTransientConnectionException("Opening logfile failed: " + ex.getMessage(), "08M01");
179 } 186 }
180 } 187 }
181 188
182 SqlOptionsCallback callback = null; 189 SqlOptionsCallback callback = null;
183 switch (target.getLanguage()) { 190 switch (validated.getLanguage()) {
184 case "sql": 191 case "sql":
185 lang = LANG_SQL; 192 lang = LANG_SQL;
186 queryTempl[0] = "s"; // pre 193 queryTempl[0] = "s"; // pre
187 queryTempl[1] = "\n;"; // post 194 queryTempl[1] = "\n;"; // post
188 queryTempl[2] = "\n;\n"; // separator 195 queryTempl[2] = "\n;\n"; // separator
189 commandTempl[0] = "X"; // pre 196 commandTempl[0] = "X"; // pre
190 commandTempl[1] = ""; // post 197 commandTempl[1] = ""; // post
191 callback = new SqlOptionsCallback(); 198 callback = new SqlOptionsCallback(validated);
192 break; 199 break;
193 case "mal": 200 case "mal":
194 lang = LANG_MAL; 201 lang = LANG_MAL;
195 queryTempl[0] = ""; // pre 202 queryTempl[0] = ""; // pre
196 queryTempl[1] = ";\n"; // post 203 queryTempl[1] = ";\n"; // post
229 sqle.setNextException(new SQLNonTransientConnectionException(connex[1], "08001")); 236 sqle.setNextException(new SQLNonTransientConnectionException(connex[1], "08001"));
230 } 237 }
231 throw sqle; 238 throw sqle;
232 } 239 }
233 240
234 if (server.canClientInfo() && target.sendClientInfo()) { 241 if (server.canClientInfo() && validated.sendClientInfo()) {
235 ClientInfo info = new ClientInfo(); 242 ClientInfo info = new ClientInfo();
236 info.setDefaults(); 243 info.setDefaults();
237 String clientApplication = target.getClientApplication(); 244 String clientApplication = validated.getClientApplication();
238 String clientRemark = target.getClientRemark(); 245 String clientRemark = validated.getClientRemark();
239 if (!clientApplication.isEmpty()) 246 if (!clientApplication.isEmpty())
240 info.set("ApplicationName", clientApplication); 247 info.set("ApplicationName", clientApplication);
241 if (!clientRemark.isEmpty()) 248 if (!clientRemark.isEmpty())
242 info.set("ClientRemark", clientRemark); 249 info.set("ClientRemark", clientRemark);
243 sendClientInfo(info); 250 sendClientInfo(info);
244 } 251 }
245 252
246 // Now take care of any options not handled during the handshake 253 // Now take care of any options not handled during the handshake
247 curReplySize = defaultFetchSize; 254 curReplySize = defaultFetchSize;
248 if (lang == LANG_SQL) { 255 if (lang == LANG_SQL) {
249 if (autoCommit != target.isAutocommit()) { 256 if (autoCommit != validated.isAutocommit()) {
250 setAutoCommit(target.isAutocommit()); 257 setAutoCommit(validated.isAutocommit());
251 } 258 }
252 if (!callback.sizeHeaderEnabled) { 259 if (!callback.sizeHeaderEnabled) {
253 sendControlCommand("sizeheader 1"); 260 sendControlCommand("sizeheader 1");
254 } 261 }
255 if (!callback.timeZoneSet) { 262 if (!callback.timeZoneSet) {
256 setTimezone(60 * target.getTimezone()); 263 setTimezone(60 * validated.getTimezone());
257 } 264 }
258 } 265 }
259 266
260 // we're absolutely not closed, since we're brand new 267 // we're absolutely not closed, since we're brand new
261 closed = false; 268 closed = false;
262 269
263 if (!target.getSchema().isEmpty()) { 270 if (!validated.getSchema().isEmpty()) {
264 try (Statement stmt = this.createStatement()) { 271 try (Statement stmt = this.createStatement()) {
265 String escaped = target.getSchema().replaceAll("\"", "\"\""); 272 String escaped = validated.getSchema().replaceAll("\"", "\"\"");
266 stmt.execute("SET SCHEMA \"" + escaped + "\""); 273 stmt.execute("SET SCHEMA \"" + escaped + "\"");
267 } 274 }
268 } 275 }
269 } 276 }
270 277
3861 } 3868 }
3862 } 3869 }
3863 3870
3864 3871
3865 private class SqlOptionsCallback extends MapiSocket.OptionsCallback { 3872 private class SqlOptionsCallback extends MapiSocket.OptionsCallback {
3873 private final Target.Validated validated;
3866 private int level; 3874 private int level;
3867 boolean sizeHeaderEnabled = false; // used during handshake 3875 boolean sizeHeaderEnabled = false; // used during handshake
3868 boolean timeZoneSet = false; // used during handshake 3876 boolean timeZoneSet = false; // used during handshake
3877
3878 public SqlOptionsCallback(Target.Validated validated) {
3879 super();
3880 this.validated = validated;
3881 }
3869 3882
3870 3883
3871 @Override 3884 @Override
3872 public void addOptions(String lang, int level) { 3885 public void addOptions(String lang, int level) {
3873 if (!lang.equals("sql")) 3886 if (!lang.equals("sql"))
3874 return; 3887 return;
3875 this.level = level; 3888 this.level = level;
3876 3889
3877 // Try to add options and record that this happened if it succeeds. 3890 // Try to add options and record that this happened if it succeeds.
3878 if (contribute(SqlOption.Autocommit, target.isAutocommit() ? 1 : 0)) 3891 if (contribute(SqlOption.Autocommit, validated.isAutocommit() ? 1 : 0))
3879 autoCommit = target.isAutocommit(); 3892 autoCommit = target.isAutocommit();
3880 if (contribute(SqlOption.ReplySize, target.getReplySize())) 3893 if (contribute(SqlOption.ReplySize, validated.getReplySize()))
3881 defaultFetchSize = target.getReplySize(); 3894 defaultFetchSize = validated.getReplySize();
3882 if (contribute(SqlOption.SizeHeader, 1)) 3895 if (contribute(SqlOption.SizeHeader, 1))
3883 sizeHeaderEnabled = true; 3896 sizeHeaderEnabled = true;
3884 if (contribute(SqlOption.TimeZone, 60 * target.getTimezone())) 3897 if (contribute(SqlOption.TimeZone, 60 * target.getTimezone()))
3885 timeZoneSet = true; 3898 timeZoneSet = true;
3886 } 3899 }