Mercurial > hg > monetdb-java
changeset 180:fdf4c888d5b7
Small code and layout improvements
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 05 Oct 2017 17:01:17 +0200 (2017-10-05) |
parents | f0a704e33fe4 |
children | 237816fef586 |
files | src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLReader.java src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLWriter.java src/main/java/nl/cwi/monetdb/mcl/net/MapiSocket.java src/main/java/nl/cwi/monetdb/mcl/parser/HeaderLineParser.java src/main/java/nl/cwi/monetdb/mcl/parser/StartOfHeaderParser.java src/main/java/nl/cwi/monetdb/mcl/parser/TupleLineParser.java |
diffstat | 8 files changed, 107 insertions(+), 108 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetDriver.java.in @@ -144,7 +144,7 @@ final public class MonetDriver implement int uri_port = uri.getPort(); if (uri_port > 0) - info.put("port", "" + uri_port); + info.put("port", Integer.toString(uri_port)); // check the database String uri_path = uri.getPath(); @@ -404,7 +404,7 @@ final public class MonetDriver implement * @return the version string */ public static String getDriverVersion() { - return "" + DRIVERMAJOR + "." + DRIVERMINOR + " (" + DRIVERVERSIONSUFFIX + ")"; + return DRIVERMAJOR + "." + DRIVERMINOR + " (" + DRIVERVERSIONSUFFIX + ")"; } public static int getDriverMajorVersion() {
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java @@ -192,8 +192,8 @@ public class MonetResultSet /** * Internal utility method to fill the JdbcSQLTypes array with derivable values. - * By doing it once (in the constructor) we can avoid doing this in many getXyz() methods again and again - * thereby improving getXyz() method performance. + * By doing it once (in the constructor) we can avoid doing this in many getXyz() + * methods again and again thereby improving getXyz() method performance. */ private void populateJdbcSQLtypesArray() { MonetConnection connection = null; @@ -203,15 +203,15 @@ public class MonetResultSet for (int i = 0; i < types.length; i++) { int javaSQLtype = MonetDriver.getJavaType(types[i]); - JdbcSQLTypes[i] = javaSQLtype; if (javaSQLtype == Types.CLOB) { if (connection != null && connection.mapClobAsVarChar()) - JdbcSQLTypes[i] = Types.VARCHAR; + javaSQLtype = Types.VARCHAR; } else if (javaSQLtype == Types.BLOB) { if (connection != null && connection.mapBlobAsVarBinary()) - JdbcSQLTypes[i] = Types.VARBINARY; + javaSQLtype = Types.VARBINARY; } + JdbcSQLTypes[i] = javaSQLtype; } }
--- a/src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLReader.java +++ b/src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLReader.java @@ -20,7 +20,7 @@ import java.io.UnsupportedEncodingExcept * to provide a means for efficient reading of characters, arrays and * lines. This class is based on the BufferedReader class, and provides * extra functionality useful for MCL. - * + * * The BufferedMCLReader is typically used as layer inbetween an * InputStream and a specific interpreter of the data. * <pre> @@ -42,29 +42,28 @@ import java.io.UnsupportedEncodingExcept * @see nl.cwi.monetdb.mcl.io.BufferedMCLWriter */ public class BufferedMCLReader extends BufferedReader { + /** "there is currently no line", or the the type is unknown is represented by UNKNOWN */ + public final static int UNKNOWN = 0; + /** a line starting with ! indicates ERROR */ + public final static int ERROR = '!'; + /** a line starting with % indicates HEADER */ + public final static int HEADER = '%'; + /** a line starting with [ indicates RESULT */ + public final static int RESULT = '['; + /** a line which matches the pattern of prompt1 is a PROMPT */ + public final static int PROMPT = '.'; + /** a line which matches the pattern of prompt2 is a MORE */ + public final static int MORE = ','; + /** a line starting with & indicates the start of a header block */ + public final static int SOHEADER = '&'; + /** a line starting with ^ indicates REDIRECT */ + public final static int REDIRECT = '^'; + /** a line starting with # indicates INFO */ + public final static int INFO = '#'; + /** The type of the last line read */ private int lineType; - /** "there is currently no line", or the the type is unknown is - represented by UNKNOWN */ - public final static int UNKNOWN = 0; - /** a line starting with ! indicates ERROR */ - public final static int ERROR = '!'; - /** a line starting with % indicates HEADER */ - public final static int HEADER = '%'; - /** a line starting with [ indicates RESULT */ - public final static int RESULT = '['; - /** a line which matches the pattern of prompt1 is a PROMPT */ - public final static int PROMPT = '.'; - /** a line which matches the pattern of prompt2 is a MORE */ - public final static int MORE = ','; - /** a line starting with & indicates the start of a header block */ - public final static int SOHEADER = '&'; - /** a line starting with ^ indicates REDIRECT */ - public final static int REDIRECT = '^'; - /** a line starting with # indicates INFO */ - public final static int INFO = '#'; - /** * Create a buffering character-input stream that uses a * default-sized input buffer. @@ -94,7 +93,7 @@ public class BufferedMCLReader extends B * carriage return followed immediately by a linefeed. Before this * method returns, it sets the linetype to any of the in MCL * recognised line types. - * + * * Warning: until the server properly prefixes all of its error * messages with SQLSTATE codes, this method prefixes all errors it * sees without sqlstate with the generic data exception code @@ -120,38 +119,38 @@ public class BufferedMCLReader extends B * * @param line the string to examine */ - void setLineType(String line) { + public void setLineType(String line) { lineType = UNKNOWN; if (line == null || line.length() == 0) return; switch (line.charAt(0)) { case '!': lineType = ERROR; - break; + break; case '&': lineType = SOHEADER; - break; + break; case '%': lineType = HEADER; - break; + break; case '[': lineType = RESULT; - break; + break; case '=': lineType = RESULT; - break; + break; case '^': lineType = REDIRECT; - break; + break; case '#': lineType = INFO; - break; + break; case '.': lineType = PROMPT; - break; + break; case ',': lineType = MORE; - break; + break; } } @@ -171,25 +170,26 @@ public class BufferedMCLReader extends B * for a new command. All read data is discarded. If the last line * read by readLine() was a prompt, this method will immediately * return. - * + * * If there are errors present in the lines that are read, then they * are put in one string and returned <b>after</b> the prompt has * been found. If no errors are present, null will be returned. * * @return a string containing error messages, or null if there aren't any * @throws IOException if an IO exception occurs while talking to the server - * - * TODO(Wouter): should probably not have to be synchronized. + StringBuilder... + * + * TODO(Wouter): should probably not have to be synchronized. */ final public synchronized String waitForPrompt() throws IOException { - String ret = "", tmp; + String tmp; + StringBuilder ret = new StringBuilder(128); while (lineType != PROMPT) { - if ((tmp = readLine()) == null) + tmp = readLine(); + if (tmp == null) throw new IOException("Connection to server lost!"); if (lineType == ERROR) - ret += "\n" + tmp.substring(1); + ret.append('\n').append(tmp.substring(1)); } - return ret == "" ? null : ret.trim(); + return ret.length() == 0 ? null : ret.toString().trim(); } - }
--- a/src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLWriter.java +++ b/src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLWriter.java @@ -34,7 +34,7 @@ import java.io.Writer; * * @author Fabian Groffen <Fabian.Groffen> * @see nl.cwi.monetdb.mcl.net.MapiSocket - * @see nl.cwi.monetdb.mcl.io.BufferedMCLWriter + * @see nl.cwi.monetdb.mcl.io.BufferedMCLReader */ public class BufferedMCLWriter extends BufferedWriter { private BufferedMCLReader reader; @@ -63,6 +63,18 @@ public class BufferedMCLWriter extends B } /** + * Write a line separator. The line separator string is in this + * class always the single newline character '\n'. + * + * @throws IOException If an I/O error occurs + */ + @Override + public void newLine() throws IOException { + write('\n'); + } + + + /** * Registers the given reader in this writer. A registered reader * receives a linetype reset when a line is written from this * writer. @@ -74,17 +86,6 @@ public class BufferedMCLWriter extends B } /** - * Write a line separator. The line separator string is in this - * class always the single newline character '\n'. - * - * @throws IOException If an I/O error occurs - */ - @Override - public void newLine() throws IOException { - write('\n'); - } - - /** * Write a single line, terminated with a line separator, and flush * the stream. This is a shorthand method for a call to write() * and flush(). @@ -96,6 +97,7 @@ public class BufferedMCLWriter extends B write(line); flush(); // reset reader state, last line isn't valid any more now - if (reader != null) reader.setLineType(null); + if (reader != null) + reader.setLineType(null); } }
--- a/src/main/java/nl/cwi/monetdb/mcl/net/MapiSocket.java +++ b/src/main/java/nl/cwi/monetdb/mcl/net/MapiSocket.java @@ -252,15 +252,16 @@ public final class MapiSocket { * @throws MCLException if an MCL related error occurs */ public List<String> connect(String host, int port, String user, String pass) - throws IOException, MCLParseException, MCLException { + throws IOException, MCLParseException, MCLException + { // Wrap around the internal connect that needs to know if it // should really make a TCP connection or not. return connect(host, port, user, pass, true); } - private List<String> connect(String host, int port, String user, String pass, - boolean makeConnection) - throws IOException, MCLParseException, MCLException { + private List<String> connect(String host, int port, String user, String pass, boolean makeConnection) + throws IOException, MCLParseException, MCLException + { if (ttl-- <= 0) throw new MCLException("Maximum number of redirects reached, aborting connection attempt. Sorry."); @@ -385,7 +386,7 @@ public final class MapiSocket { close(); } tmp = u.getPath(); - if (tmp != null && tmp.length() != 0) { + if (tmp != null && tmp.length() > 0) { tmp = tmp.substring(1).trim(); if (!tmp.isEmpty() && !tmp.equals(database)) { warns.add("redirect points to different " + "database: " + tmp); @@ -438,8 +439,8 @@ public final class MapiSocket { // parse the challenge string, split it on ':' String[] chaltok = chalstr.split(":"); - if (chaltok.length <= 4) throw - new MCLParseException("Server challenge string unusable! Challenge contains too few tokens: " + chalstr); + if (chaltok.length <= 4) + throw new MCLParseException("Server challenge string unusable! Challenge contains too few tokens: " + chalstr); // challenge string to use as salt/key String challenge = chaltok[0]; @@ -549,9 +550,9 @@ public final class MapiSocket { } // generate response - response = "BIG:"; // JVM byte-order is big-endian - response += username + ":" + pwhash + ":" + language; - response += ":" + (database == null ? "" : database) + ":"; + response = "BIG:" // JVM byte-order is big-endian + + username + ":" + pwhash + ":" + language + + ":" + (database == null ? "" : database) + ":"; return response; }
--- a/src/main/java/nl/cwi/monetdb/mcl/parser/HeaderLineParser.java +++ b/src/main/java/nl/cwi/monetdb/mcl/parser/HeaderLineParser.java @@ -19,10 +19,10 @@ package nl.cwi.monetdb.mcl.parser; public class HeaderLineParser extends MCLParser { private int type; - public final static int NAME = 1; - public final static int LENGTH = 2; - public final static int TABLE = 3; - public final static int TYPE = 4; + public final static int NAME = 1; + public final static int LENGTH = 2; + public final static int TABLE = 3; + public final static int TYPE = 4; /** * Constructs a HeaderLineParser which expects columncount columns. @@ -53,7 +53,7 @@ public class HeaderLineParser extends MC int pos = 0; boolean foundChar = false; boolean nameFound = false; - // find header name + // find header name searching from the end of the line for (int i = len - 1; i >= 0; i--) { switch (chrLine[i]) { case ' ': @@ -107,8 +107,7 @@ public class HeaderLineParser extends MC } break; default: - throw new MCLParseException("unknown header: " + - (new String(chrLine, pos, len - pos))); + throw new MCLParseException("unknown header: " + (new String(chrLine, pos, len - pos))); } // adjust colno
--- a/src/main/java/nl/cwi/monetdb/mcl/parser/StartOfHeaderParser.java +++ b/src/main/java/nl/cwi/monetdb/mcl/parser/StartOfHeaderParser.java @@ -54,29 +54,29 @@ public class StartOfHeaderParser { soh.get(); // skip the & int type = soh.get(); switch (type) { - default: - throw new MCLParseException("invalid or unknown header", 1); case Q_PARSE: case Q_SCHEMA: len = 0; - break; + break; case Q_TABLE: case Q_PREPARE: len = 4; soh.get(); - break; + break; case Q_UPDATE: len = 2; soh.get(); - break; + break; case Q_TRANS: len = 1; soh.get(); - break; + break; case Q_BLOCK: len = 3; soh.get(); - break; + break; + default: + throw new MCLParseException("invalid or unknown header", 1); } pos = 0; return type; @@ -98,8 +98,8 @@ public class StartOfHeaderParser { public final int getNextAsInt() throws MCLParseException { boolean positive = true; pos++; - if (!soh.hasRemaining()) throw - new MCLParseException("unexpected end of string", soh.position() - 1); + if (!soh.hasRemaining()) + throw new MCLParseException("unexpected end of string", soh.position() - 1); int tmp = 0; char chr = soh.get(); // note: don't use Character.isDigit() here, because @@ -117,8 +117,8 @@ public class StartOfHeaderParser { } while (soh.hasRemaining() && (chr = soh.get()) != ' ') { - tmp *= 10; if (chr >= '0' && chr <= '9') { + tmp *= 10; tmp += (int)chr - (int)'0'; } else { throw new MCLParseException("expected a digit", soh.position() - 1); @@ -130,8 +130,8 @@ public class StartOfHeaderParser { public final String getNextAsString() throws MCLParseException { pos++; - if (!soh.hasRemaining()) throw - new MCLParseException("unexpected end of string", soh.position() - 1); + if (!soh.hasRemaining()) + throw new MCLParseException("unexpected end of string", soh.position() - 1); int cnt = 0; soh.mark(); while (soh.hasRemaining() && soh.get() != ' ') {
--- a/src/main/java/nl/cwi/monetdb/mcl/parser/TupleLineParser.java +++ b/src/main/java/nl/cwi/monetdb/mcl/parser/TupleLineParser.java @@ -66,10 +66,10 @@ public class TupleLineParser extends MCL switch(chrLine[i]) { default: escaped = false; - break; + break; case '\\': escaped = !escaped; - break; + break; case '"': /** * If all strings are wrapped between two quotes, a \" can @@ -92,7 +92,7 @@ public class TupleLineParser extends MCL // reset escaped flag escaped = false; - break; + break; case '\t': if (!inString && (i > 0 && chrLine[i - 1] == ',') || @@ -114,29 +114,29 @@ public class TupleLineParser extends MCL switch (chrLine[pos]) { case '\\': uesc.append('\\'); - break; + break; case 'f': uesc.append('\f'); - break; + break; case 'n': uesc.append('\n'); - break; + break; case 'r': uesc.append('\r'); - break; + break; case 't': uesc.append('\t'); - break; + break; case '"': uesc.append('"'); - break; + break; case '0': case '1': case '2': case '3': // this could be an octal number, let's check it out if (pos + 2 < i - 2 && chrLine[pos + 1] >= '0' && chrLine[pos + 1] <= '7' && chrLine[pos + 2] >= '0' && chrLine[pos + 2] <= '7' ) { - // we got the number! + // we got an octal number try { uesc.append((char)(Integer.parseInt("" + chrLine[pos] + chrLine[pos + 1] + chrLine[pos + 2], 8))); pos += 2; @@ -148,11 +148,11 @@ public class TupleLineParser extends MCL // do default action if number seems not to be correct uesc.append(chrLine[pos]); } - break; + break; default: - // this is wrong, just ignore the escape, and print the char + // this is wrong usage of escape, just ignore the \-escape and print the char uesc.append(chrLine[pos]); - break; + break; } } else { uesc.append(chrLine[pos]); @@ -161,20 +161,17 @@ public class TupleLineParser extends MCL // put the unescaped string in the right place values[column++] = uesc.toString(); - } else if ((i - 1) - cursor == 4 && - source.indexOf("NULL", cursor) == cursor) - { + } else if ((i - 1) - cursor == 4 && source.indexOf("NULL", cursor) == cursor) { values[column++] = null; } else { - values[column++] = - source.substring(cursor, i - 1); + values[column++] = source.substring(cursor, i - 1); } cursor = i + 1; } // reset escaped flag escaped = false; - break; + break; } } // check if this result is of the size we expected it to be