comparison src/main/java/org/monetdb/jdbc/MonetConnection.java @ 497:aed7f32e029a onclient

Refactor LineType to be an enum Makes it easier to catch them all when changing things.
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Fri, 13 Aug 2021 10:26:56 +0200 (2021-08-13)
parents c8f3950eec93
children 71d8ce4c3ac0
comparison
equal deleted inserted replaced
496:c8f3950eec93 497:aed7f32e029a
33 import java.util.WeakHashMap; 33 import java.util.WeakHashMap;
34 import java.util.concurrent.Executor; 34 import java.util.concurrent.Executor;
35 35
36 import org.monetdb.mcl.io.BufferedMCLReader; 36 import org.monetdb.mcl.io.BufferedMCLReader;
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.net.HandshakeOptions; 39 import org.monetdb.mcl.net.HandshakeOptions;
39 import org.monetdb.mcl.net.MapiSocket; 40 import org.monetdb.mcl.net.MapiSocket;
40 import org.monetdb.mcl.parser.HeaderLineParser; 41 import org.monetdb.mcl.parser.HeaderLineParser;
41 import org.monetdb.mcl.parser.MCLParseException; 42 import org.monetdb.mcl.parser.MCLParseException;
42 import org.monetdb.mcl.parser.StartOfHeaderParser; 43 import org.monetdb.mcl.parser.StartOfHeaderParser;
2098 * @param line the header line as String 2099 * @param line the header line as String
2099 * @param linetype the line type according to the MAPI protocol 2100 * @param linetype the line type according to the MAPI protocol
2100 * @return a non-null String if the line is invalid, 2101 * @return a non-null String if the line is invalid,
2101 * or additional lines are not allowed. 2102 * or additional lines are not allowed.
2102 */ 2103 */
2103 public abstract String addLine(String line, int linetype); 2104 public abstract String addLine(String line, LineType linetype);
2104 2105
2105 /** 2106 /**
2106 * Returns whether this Response expects more lines to be added 2107 * Returns whether this Response expects more lines to be added
2107 * to it. 2108 * to it.
2108 * 2109 *
2257 * @return a non-null String if the header cannot be parsed or 2258 * @return a non-null String if the header cannot be parsed or
2258 * is unknown 2259 * is unknown
2259 */ 2260 */
2260 // {{{ addLine 2261 // {{{ addLine
2261 @Override 2262 @Override
2262 public String addLine(final String tmpLine, final int linetype) { 2263 public String addLine(final String tmpLine, final LineType linetype) {
2263 if (isSet[LENS] && isSet[TYPES] && isSet[TABLES] && isSet[NAMES]) { 2264 if (isSet[LENS] && isSet[TYPES] && isSet[TABLES] && isSet[NAMES]) {
2264 return resultBlocks[0].addLine(tmpLine, linetype); 2265 return resultBlocks[0].addLine(tmpLine, linetype);
2265 } 2266 }
2266 2267
2267 if (linetype != BufferedMCLReader.HEADER) 2268 if (linetype != LineType.HEADER)
2268 return "header expected, got: " + tmpLine; 2269 return "header expected, got: " + tmpLine;
2269 2270
2270 // depending on the name of the header, we continue 2271 // depending on the name of the header, we continue
2271 try { 2272 try {
2272 switch (hlp.parse(tmpLine)) { 2273 switch (hlp.parse(tmpLine)) {
2632 * @param linetype the line type according to the MAPI protocol 2633 * @param linetype the line type according to the MAPI protocol
2633 * @return a non-null String if the line is invalid, 2634 * @return a non-null String if the line is invalid,
2634 * or additional lines are not allowed. 2635 * or additional lines are not allowed.
2635 */ 2636 */
2636 @Override 2637 @Override
2637 public String addLine(final String line, final int linetype) { 2638 public String addLine(final String line, final LineType linetype) {
2638 if (linetype != BufferedMCLReader.RESULT) 2639 if (linetype != LineType.RESULT)
2639 return "protocol violation: unexpected line in data block: " + line; 2640 return "protocol violation: unexpected line in data block: " + line;
2640 // add to the backing array 2641 // add to the backing array
2641 data[++pos] = line; 2642 data[++pos] = line;
2642 2643
2643 // all is well 2644 // all is well
2723 this.count = cnt; 2724 this.count = cnt;
2724 this.lastid = id; 2725 this.lastid = id;
2725 } 2726 }
2726 2727
2727 @Override 2728 @Override
2728 public String addLine(final String line, final int linetype) { 2729 public String addLine(final String line, final LineType linetype) {
2729 return "Header lines are not supported for an UpdateResponse"; 2730 return "Header lines are not supported for an UpdateResponse";
2730 } 2731 }
2731 2732
2732 @Override 2733 @Override
2733 public boolean wantsMore() { 2734 public boolean wantsMore() {
2760 // {{{ SchemaResponse class implementation 2761 // {{{ SchemaResponse class implementation
2761 class SchemaResponse implements Response { 2762 class SchemaResponse implements Response {
2762 public final int state = Statement.SUCCESS_NO_INFO; 2763 public final int state = Statement.SUCCESS_NO_INFO;
2763 2764
2764 @Override 2765 @Override
2765 public String addLine(final String line, final int linetype) { 2766 public String addLine(final String line, final LineType linetype) {
2766 return "Header lines are not supported for a SchemaResponse"; 2767 return "Header lines are not supported for a SchemaResponse";
2767 } 2768 }
2768 2769
2769 @Override 2770 @Override
2770 public boolean wantsMore() { 2771 public boolean wantsMore() {
2988 String queryLine = (templ[0] == null ? "" : templ[0]) + query + (templ[1] == null ? "" : templ[1]); 2989 String queryLine = (templ[0] == null ? "" : templ[0]) + query + (templ[1] == null ? "" : templ[1]);
2989 out.writeLine(queryLine); 2990 out.writeLine(queryLine);
2990 2991
2991 // go for new results 2992 // go for new results
2992 String tmpLine = in.readLine(); 2993 String tmpLine = in.readLine();
2993 int linetype = in.getLineType(); 2994 LineType linetype = in.getLineType();
2994 Response res = null; 2995 Response res = null;
2995 while (linetype != BufferedMCLReader.PROMPT) { 2996 while (linetype != LineType.PROMPT) {
2996 // each response should start with a start of header (or error) 2997 // each response should start with a start of header (or error)
2997 switch (linetype) { 2998 switch (linetype) {
2998 case BufferedMCLReader.SOHEADER: 2999 case SOHEADER:
2999 // make the response object, and fill it 3000 // make the response object, and fill it
3000 try { 3001 try {
3001 switch (sohp.parse(tmpLine)) { 3002 switch (sohp.parse(tmpLine)) {
3002 case StartOfHeaderParser.Q_PARSE: 3003 case StartOfHeaderParser.Q_PARSE:
3003 throw new MCLParseException("Q_PARSE header not allowed here", 1); 3004 throw new MCLParseException("Q_PARSE header not allowed here", 1);
3098 // read the next line (can be prompt, new result, error, etc.) 3099 // read the next line (can be prompt, new result, error, etc.)
3099 // before we start the loop over 3100 // before we start the loop over
3100 tmpLine = in.readLine(); 3101 tmpLine = in.readLine();
3101 linetype = in.getLineType(); 3102 linetype = in.getLineType();
3102 break; 3103 break;
3103 case BufferedMCLReader.INFO: 3104 case INFO:
3104 addWarning(tmpLine.substring(1), "01000"); 3105 addWarning(tmpLine.substring(1), "01000");
3105 // read the next line (can be prompt, new result, error, etc.) 3106 // read the next line (can be prompt, new result, error, etc.)
3106 // before we start the loop over 3107 // before we start the loop over
3107 tmpLine = in.readLine(); 3108 tmpLine = in.readLine();
3108 linetype = in.getLineType(); 3109 linetype = in.getLineType();
3109 break; 3110 break;
3110 default: // Yeah... in Java this is correct! 3111 default: // Yeah... in Java this is correct!
3111 // we have something we don't expect/understand, let's make it an error message 3112 // we have something we don't expect/understand, let's make it an error message
3112 tmpLine = "!M0M10!protocol violation, unexpected line: " + tmpLine; 3113 tmpLine = "!M0M10!protocol violation, unexpected line: " + tmpLine;
3113 // don't break; fall through... 3114 // don't break; fall through...
3114 case BufferedMCLReader.ERROR: 3115 case ERROR:
3115 // read everything till the prompt (should be 3116 // read everything till the prompt (should be
3116 // error) we don't know if we ignore some 3117 // error) we don't know if we ignore some
3117 // garbage here... but the log should reveal that 3118 // garbage here... but the log should reveal that
3118 error = in.waitForPrompt(); 3119 error = in.waitForPrompt();
3119 linetype = in.getLineType(); 3120 linetype = in.getLineType();