Mercurial > hg > monetdb-java
changeset 70:17fbaf2635bb
Expand import java.*.* statements
Also removed trailing spaces and tabs
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 08 Dec 2016 16:23:46 +0100 (2016-12-08) |
parents | e092fa8d9ab7 |
children | db46b4ab2b9f |
files | src/main/java/nl/cwi/monetdb/client/JMonetDB.java src/main/java/nl/cwi/monetdb/client/JdbcClient.java src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLWriter.java src/main/java/nl/cwi/monetdb/merovingian/SabaothDB.java src/main/java/nl/cwi/monetdb/util/CmdLineOpts.java src/main/java/nl/cwi/monetdb/util/Exporter.java src/main/java/nl/cwi/monetdb/util/SQLRestore.java src/main/java/nl/cwi/monetdb/util/XMLExporter.java |
diffstat | 8 files changed, 157 insertions(+), 99 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/client/JMonetDB.java +++ b/src/main/java/nl/cwi/monetdb/client/JMonetDB.java @@ -8,10 +8,17 @@ package nl.cwi.monetdb.client; -import nl.cwi.monetdb.util.*; -import nl.cwi.monetdb.merovingian.*; -import java.io.*; -import java.util.*; +import nl.cwi.monetdb.merovingian.Control; +import nl.cwi.monetdb.merovingian.SabaothDB; +import nl.cwi.monetdb.util.CmdLineOpts; +import nl.cwi.monetdb.util.OptionsException; + +import java.io.BufferedWriter; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; /** * This program mimics the monetdb tool. It is meant as demonstration
--- a/src/main/java/nl/cwi/monetdb/client/JdbcClient.java +++ b/src/main/java/nl/cwi/monetdb/client/JdbcClient.java @@ -8,12 +8,36 @@ package nl.cwi.monetdb.client; -import nl.cwi.monetdb.util.*; -import java.sql.*; -import java.io.*; -import java.util.*; -import java.util.zip.*; -import java.net.*; +import nl.cwi.monetdb.util.CmdLineOpts; +import nl.cwi.monetdb.util.Exporter; +import nl.cwi.monetdb.util.OptionsException; +import nl.cwi.monetdb.util.SQLExporter; +import nl.cwi.monetdb.util.XMLExporter; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.zip.GZIPInputStream; /** * This program acts like an extended client program for MonetDB. Its @@ -250,7 +274,7 @@ public final class JdbcClient { dbmd = null; } stmt = con.createStatement(); - + // see if we will have to perform a database dump (only in SQL mode) if ("sql".equals(lang) && copts.getOption("dump").isPresent()) { ResultSet tbl; @@ -1171,7 +1195,6 @@ class Table { return fqname; } - static Table findTable(String fqname, List<Table> list) { for (Table l : list) { if (l.fqname.equals(fqname))
--- a/src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLWriter.java +++ b/src/main/java/nl/cwi/monetdb/mcl/io/BufferedMCLWriter.java @@ -8,7 +8,12 @@ package nl.cwi.monetdb.mcl.io; -import java.io.*; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; +import java.io.Writer; /** * Write text to a character-output stream, buffering characters so as
--- a/src/main/java/nl/cwi/monetdb/merovingian/SabaothDB.java +++ b/src/main/java/nl/cwi/monetdb/merovingian/SabaothDB.java @@ -8,11 +8,11 @@ package nl.cwi.monetdb.merovingian; -import java.util.*; +import java.util.Date; /** * Implementation of the Sabaoth C-struct as Java object. - * + * * This Class implements a parser for the string representation of a * sabaoth information struct as returned by monetdbd. * <br /> @@ -93,7 +93,6 @@ public class SabaothDB { } } - /** * Constructs a new SabaothDB object from a String. *
--- a/src/main/java/nl/cwi/monetdb/util/CmdLineOpts.java +++ b/src/main/java/nl/cwi/monetdb/util/CmdLineOpts.java @@ -8,26 +8,33 @@ package nl.cwi.monetdb.util; -import java.util.*; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; public class CmdLineOpts { /** the arguments we handle */ private Map<String, OptionContainer> opts = new HashMap<String, OptionContainer>(); /** the options themself */ private List<OptionContainer> options = new ArrayList<OptionContainer>(); - + /** no arguments */ - public static int CAR_ZERO = 0; + public static int CAR_ZERO = 0; /** always one argument */ - public static int CAR_ONE = 1; + public static int CAR_ONE = 1; /** zero or one argument */ public static int CAR_ZERO_ONE = 2; /** zero or many arguments */ public static int CAR_ZERO_MANY = 3; /** one or many arguments */ public static int CAR_ONE_MANY = 4; - + public CmdLineOpts() { } @@ -80,8 +87,8 @@ public class CmdLineOpts { for (Enumeration<?> e = prop.propertyNames(); e.hasMoreElements(); ) { String key = (String) e.nextElement(); OptionContainer option = opts.get(key); - if (option == null) throw - new OptionsException("Unknown option: " + key); + if (option == null) + throw new OptionsException("Unknown option: " + key); option.resetArguments(); option.addArgument(prop.getProperty(key)); } @@ -205,7 +212,7 @@ public class CmdLineOpts { // yes, we don't care about branch mispredictions here ;) if (maxlen < len) maxlen = len; } - + // get the individual strings StringBuilder ret = new StringBuilder(); for (OptionContainer oc : options) { @@ -320,7 +327,6 @@ public class CmdLineOpts { } name = (longa != null) ? longa : shorta; - options.add(this); }
--- a/src/main/java/nl/cwi/monetdb/util/Exporter.java +++ b/src/main/java/nl/cwi/monetdb/util/Exporter.java @@ -8,11 +8,12 @@ package nl.cwi.monetdb.util; -import java.io.*; -import java.sql.*; +import java.io.PrintWriter; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Arrays; - public abstract class Exporter { protected PrintWriter out; protected boolean useSchema; @@ -32,13 +33,13 @@ public abstract class Exporter { public abstract void setProperty(int type, int value) throws Exception; public abstract int getProperty(int type) throws Exception; - + //=== shared utilities - + public void useSchemas(boolean use) { useSchema = use; } - + /** * returns the given string between two double quotes for usage as * identifier such as column or table name in SQL queries.
--- a/src/main/java/nl/cwi/monetdb/util/SQLRestore.java +++ b/src/main/java/nl/cwi/monetdb/util/SQLRestore.java @@ -30,7 +30,7 @@ public class SQLRestore { private final String _user; private final String _password; private final String _dbName; - + public SQLRestore(String host, int port, String user, String password, String dbName) throws IOException { if (host == null || user == null || password == null || dbName == null) throw new NullPointerException(); @@ -40,16 +40,16 @@ public class SQLRestore { _password = password; _dbName = dbName; } - + private static class ServerResponseReader implements Runnable { private final BufferedMCLReader _is; private final AtomicBoolean _errorState = new AtomicBoolean(false); - private String _errorMessage = null; - - ServerResponseReader(BufferedMCLReader is) { + private String _errorMessage = null; + + ServerResponseReader(BufferedMCLReader is) { _is = is; } - + public void run() { try { while (true) { @@ -57,7 +57,7 @@ public class SQLRestore { if (line == null) break; int result = _is.getLineType(); - switch (result) { + switch (result) { case BufferedMCLReader.ERROR: _errorMessage = line; _errorState.set(true); @@ -66,7 +66,6 @@ public class SQLRestore { // do nothing... } } - } catch (IOException e) { _errorMessage = e.getMessage(); _errorState.set(true); @@ -78,7 +77,7 @@ public class SQLRestore { } } } - + /** * @return whether the server has responded with an error. Any * error is regarded as fatal. @@ -86,20 +85,19 @@ public class SQLRestore { public boolean inErrorState() { return _errorState.get(); } - + /** - * @return the error message if inErrorState() is true. Behaviour is - * not defined if called before inErrorState is true. + * @return the error message if inErrorState() is true. Behaviour is + * not defined if called before inErrorState is true. */ public String getErrorMessage() { return _errorMessage; } - } - + /** * Restores a given SQL dump to the database. - * + * * @param source * @throws IOException */ @@ -109,10 +107,10 @@ public class SQLRestore { ms.setLanguage("sql"); ms.setDatabase(_dbName); ms.connect(_host, _port, _user, _password); - + BufferedMCLWriter os = ms.getWriter(); BufferedMCLReader reader = ms.getReader(); - + ServerResponseReader srr = new ServerResponseReader(reader); Thread responseReaderThread = new Thread(srr); @@ -120,16 +118,16 @@ public class SQLRestore { try { // FIXME: we assume here that the dump is in system's default encoding BufferedReader sourceData = new BufferedReader(new FileReader(source)); - try { + try { os.write('s'); // signal that a new statement (or series of) is coming while(!srr.inErrorState()) { char[] buf = new char[4096]; int result = sourceData.read(buf); - if (result < 0) - break; + if (result < 0) + break; os.write(buf, 0, result); } - + os.flush(); // mark the end of the statement (or series of) os.close(); } finally { @@ -141,8 +139,8 @@ public class SQLRestore { } catch (InterruptedException e) { throw new IOException(e.getMessage()); } - - // if the server signalled an error, we should respect it... + + // if the server signalled an error, we should respect it... if (srr.inErrorState()) { throw new IOException(srr.getErrorMessage()); } @@ -155,19 +153,19 @@ public class SQLRestore { ms.close(); } } - + public void close() { // do nothing at the moment... } - - + + public static void main(String[] args) throws IOException { if (args.length != 6) { - System.err.println("USAGE: java " + SQLRestore.class.getName() + + System.err.println("USAGE: java " + SQLRestore.class.getName() + " <host> <port> <user> <password> <dbname> <dumpfile>"); System.exit(1); } - + // parse arguments String host = args[0]; int port = Integer.parseInt(args[1]); // FIXME: catch NumberFormatException @@ -175,13 +173,13 @@ public class SQLRestore { String password = args[3]; String dbName = args[4]; File dumpFile = new File(args[5]); - + // check arguments if (!dumpFile.isFile() || !dumpFile.canRead()) { System.err.println("Cannot read: " + dumpFile); System.exit(1); } - + SQLRestore md = new SQLRestore(host, port, user, password, dbName); try { System.out.println("Start restoring " + dumpFile);
--- a/src/main/java/nl/cwi/monetdb/util/XMLExporter.java +++ b/src/main/java/nl/cwi/monetdb/util/XMLExporter.java @@ -8,10 +8,16 @@ package nl.cwi.monetdb.util; -import java.io.*; -import java.sql.*; -import java.util.*; -import java.text.*; +import java.io.PrintWriter; +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.sql.Types; +import java.text.SimpleDateFormat; +import java.util.HashSet; +import java.util.Set; public class XMLExporter extends Exporter { private boolean useNil; @@ -42,7 +48,7 @@ public class XMLExporter extends Exporte out.print("<!-- unable to represent: CREATE " + type + " " + (!useSchema ? dq(schema) + "." : "") + dq(name)); out.print(" AS "); - out.print(tbl.getString("REMARKS").trim()); + out.print(tbl.getString("REMARKS").trim()); out.print(" -->"); return; } @@ -57,7 +63,7 @@ public class XMLExporter extends Exporte // http://books.xmlschemata.org/relaxng/relax-CHP-19.html while (cols.next()) { switch (cols.getInt("DATA_TYPE")) { - case java.sql.Types.CHAR: + case Types.CHAR: ident = "CHAR_" + cols.getString("COLUMN_SIZE"); if (types.contains(ident)) break; types.add(ident); @@ -72,8 +78,8 @@ public class XMLExporter extends Exporte out.println(" </xsd:restriction>"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.VARCHAR: - case java.sql.Types.LONGVARCHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: ident = "VARCHAR_" + cols.getString("COLUMN_SIZE"); if (types.contains(ident)) break; types.add(ident); @@ -88,7 +94,7 @@ public class XMLExporter extends Exporte out.println(" </xsd:restriction>"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.CLOB: + case Types.CLOB: ident = "CLOB"; if (types.contains(ident)) break; types.add(ident); @@ -99,8 +105,8 @@ public class XMLExporter extends Exporte out.println(" <xsd:restriction base=\"xsd:string\" />"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.DECIMAL: - case java.sql.Types.NUMERIC: + case Types.DECIMAL: + case Types.NUMERIC: ident = "DECIMAL_" + cols.getString("COLUMN_SIZE") + "_" + cols.getString("DECIMAL_DIGITS"); if (types.contains(ident)) break; @@ -119,7 +125,7 @@ public class XMLExporter extends Exporte out.println(" </xsd:restriction>"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.TINYINT: + case Types.TINYINT: ident = "TINYINT"; if (types.contains(ident)) break; types.add(ident); @@ -130,7 +136,7 @@ public class XMLExporter extends Exporte out.println(" <xsd:restriction base=\"xsd:byte\" />"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.SMALLINT: + case Types.SMALLINT: ident = "SMALLINT"; if (types.contains(ident)) break; types.add(ident); @@ -141,7 +147,7 @@ public class XMLExporter extends Exporte out.println(" <xsd:restriction base=\"xsd:short\" />"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.INTEGER: + case Types.INTEGER: ident = "INTEGER"; if (types.contains(ident)) break; types.add(ident); @@ -152,7 +158,7 @@ public class XMLExporter extends Exporte out.println(" <xsd:restriction base=\"xsd:integer\" />"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.BIGINT: + case Types.BIGINT: ident = "BIGINT"; if (types.contains(ident)) break; types.add(ident); @@ -163,8 +169,18 @@ public class XMLExporter extends Exporte out.println(" <xsd:restriction base=\"xsd:long\" />"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.BIT: - case java.sql.Types.BOOLEAN: + case Types.BIT: + ident = "BIT"; + if (types.contains(ident)) break; + types.add(ident); + + out.print(" <xsd:simpleType name="); + out.print(dq(ident)); + out.println(">"); + out.println(" <xsd:restriction base=\"xsd:bit\" />"); + out.println(" </xsd:simpleType>"); + break; + case Types.BOOLEAN: ident = "BOOLEAN"; if (types.contains(ident)) break; types.add(ident); @@ -175,7 +191,7 @@ public class XMLExporter extends Exporte out.println(" <xsd:restriction base=\"xsd:boolean\" />"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.DATE: + case Types.DATE: ident = "DATE"; if (types.contains(ident)) break; types.add(ident); @@ -186,7 +202,7 @@ public class XMLExporter extends Exporte out.println(" <xsd:restriction base=\"xsd:date\" />"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.TIME: + case Types.TIME: if ("timetz".equals(cols.getString("TYPE_NAME"))) { ident = "TIME_WTZ"; } else { @@ -201,7 +217,7 @@ public class XMLExporter extends Exporte out.println(" <xsd:restriction base=\"xsd:time\" />"); out.println(" </xsd:simpleType>"); break; - case java.sql.Types.TIMESTAMP: + case Types.TIMESTAMP: if ("timestamptz".equals(cols.getString("TYPE_NAME"))) { ident = "TIMESTAMP_WTZ"; } else { @@ -234,48 +250,50 @@ public class XMLExporter extends Exporte out.print(dq(cols.getString("COLUMN_NAME"))); out.print(" type="); switch (cols.getInt("DATA_TYPE")) { - case java.sql.Types.CHAR: + case Types.CHAR: ident = "CHAR_" + cols.getString("COLUMN_SIZE"); break; - case java.sql.Types.VARCHAR: - case java.sql.Types.LONGVARCHAR: + case Types.VARCHAR: + case Types.LONGVARCHAR: ident = "VARCHAR_" + cols.getString("COLUMN_SIZE"); break; - case java.sql.Types.CLOB: + case Types.CLOB: ident = "CLOB"; break; - case java.sql.Types.DECIMAL: - case java.sql.Types.NUMERIC: + case Types.DECIMAL: + case Types.NUMERIC: ident = "DECIMAL_" + cols.getString("COLUMN_SIZE") + "_" + cols.getString("DECIMAL_DIGITS"); break; - case java.sql.Types.TINYINT: + case Types.TINYINT: ident = "TINYINT"; break; - case java.sql.Types.SMALLINT: + case Types.SMALLINT: ident = "SMALLINT"; break; - case java.sql.Types.INTEGER: + case Types.INTEGER: ident = "INTEGER"; break; - case java.sql.Types.BIGINT: + case Types.BIGINT: ident = "BIGINT"; break; - case java.sql.Types.BIT: - case java.sql.Types.BOOLEAN: + case Types.BIT: + ident = "BIT"; + break; + case Types.BOOLEAN: ident = "BOOLEAN"; break; - case java.sql.Types.DATE: + case Types.DATE: ident = "DATE"; break; - case java.sql.Types.TIME: + case Types.TIME: if ("timetz".equals(cols.getString("TYPE_NAME"))) { ident = "TIME_WTZ"; } else { ident = "TIME"; } break; - case java.sql.Types.TIMESTAMP: + case Types.TIMESTAMP: if ("timestamptz".equals(cols.getString("TYPE_NAME"))) { ident = "TIMESTAMP_WTZ"; } else { @@ -322,14 +340,15 @@ public class XMLExporter extends Exporte public void dumpResultSet(ResultSet rs) throws SQLException { // write simple XML serialisation ResultSetMetaData rsmd = rs.getMetaData(); - if (!useSchema) out.println("<" + rsmd.getSchemaName(1) + ">"); + if (!useSchema) + out.println("<" + rsmd.getSchemaName(1) + ">"); out.println("<" + rsmd.getTableName(1) + ">"); String data; while (rs.next()) { out.println(" <row>"); for (int i = 1; i <= rsmd.getColumnCount(); i++) { switch (rsmd.getColumnType(i)) { - case java.sql.Types.TIMESTAMP: + case Types.TIMESTAMP: Timestamp ts = rs.getTimestamp(i); if ("timestamptz".equals(rsmd.getColumnTypeName(i))) { data = xsd_tstz.format(ts).toString();