Changeset: 18b66c736467 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=18b66c736467 Modified Files: java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedConnection.java java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedStatement.java java/embedded/src/main/java/org/monetdb/embedded/MonetDBEmbedded.java java/embedded/src/test/java/org/monetdb/embedded/test/EmbeddedTest.java java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java java/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java Branch: embedded-java Log Message:
Better accomodation JDBC connection diffs (204 lines): diff --git a/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedConnection.java b/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedConnection.java --- a/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedConnection.java +++ b/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedConnection.java @@ -25,27 +25,23 @@ public class MonetDBEmbeddedConnection e private final MonetDBEmbedded database; public MonetDBEmbeddedConnection(Properties props) throws SQLException, IllegalArgumentException { - super(props); + super(props.getProperty("database")); this.databaseLocationString = props.getProperty("database"); if (databaseLocationString == null || databaseLocationString.isEmpty()) { throw new IllegalArgumentException("Database location is not set."); } File databaseLocation = new File(databaseLocationString); - if (databaseLocation.mkdir()) { - database = new MonetDBEmbedded(databaseLocation); - try { - database.start(); - } catch (IOException e) { - throw new SQLException(e); - } - } else { - throw new IllegalArgumentException("Database location is not valid: " + databaseLocationString); + database = new MonetDBEmbedded(databaseLocation); + try { + database.start(); + } catch (IOException e) { + throw new SQLException(e); } } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { - Statement ret = new MonetDBEmbeddedStatement(database); + Statement ret = new MonetDBEmbeddedStatement(this); statements.put(ret, null); return ret; } @@ -54,4 +50,17 @@ public class MonetDBEmbeddedConnection e public String getJDBCURL() { return "jdbc:monetdb://" + databaseLocationString; } + + public MonetDBEmbedded getDatabase() { + return database; + } + + @Override + public void close() { + try { + database.close(); + } catch (IOException e) { + // Do nothing. We can't throw it up + } + } } diff --git a/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedStatement.java b/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedStatement.java --- a/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedStatement.java +++ b/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedStatement.java @@ -23,10 +23,10 @@ public class MonetDBEmbeddedStatement ex private final MonetDBEmbedded database; private EmbeddedQueryResult resultSet; - public MonetDBEmbeddedStatement(MonetDBEmbedded database) + public MonetDBEmbeddedStatement(MonetDBEmbeddedConnection connection) throws SQLException, IllegalArgumentException { - super(null, ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT); - this.database = database; + super(connection, ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT); + this.database = connection.getDatabase(); } /** diff --git a/java/embedded/src/main/java/org/monetdb/embedded/MonetDBEmbedded.java b/java/embedded/src/main/java/org/monetdb/embedded/MonetDBEmbedded.java --- a/java/embedded/src/main/java/org/monetdb/embedded/MonetDBEmbedded.java +++ b/java/embedded/src/main/java/org/monetdb/embedded/MonetDBEmbedded.java @@ -8,21 +8,20 @@ package org.monetdb.embedded; +import java.io.Closeable; import java.io.File; import java.io.IOException; import java.sql.SQLException; import org.monetdb.embedded.result.EmbeddedQueryResult; -import nl.cwi.monetdb.jdbc.MonetDBEmbeddedStatement; - /** * Embedded version of MonetDB. * Communication between Java and native C is done via JNI. * <br/> * <strong>Note</strong>: You can have only one embedded MonetDB database running per JVM process. */ -public class MonetDBEmbedded { +public class MonetDBEmbedded implements Closeable { static { // Load the embedded library System.loadLibrary("embedded_java"); @@ -109,9 +108,9 @@ public class MonetDBEmbedded { * @return The statement object * @throws SQLException */ - public MonetDBEmbeddedStatement createStatement() throws SQLException { - return new MonetDBEmbeddedStatement(this); - } +// public MonetDBEmbeddedStatement createStatement() throws SQLException { +// return new MonetDBEmbeddedStatement(this); +// } /** * Start the embedded database. @@ -131,4 +130,10 @@ public class MonetDBEmbedded { * @throws SQLException */ private native EmbeddedQueryResult queryWrapper(String query) throws SQLException; + + @Override + public void close() throws IOException { + // Do nothing for now + // TODO: stop the databse + } } diff --git a/java/embedded/src/test/java/org/monetdb/embedded/test/EmbeddedTest.java b/java/embedded/src/test/java/org/monetdb/embedded/test/EmbeddedTest.java --- a/java/embedded/src/test/java/org/monetdb/embedded/test/EmbeddedTest.java +++ b/java/embedded/src/test/java/org/monetdb/embedded/test/EmbeddedTest.java @@ -290,23 +290,9 @@ public class EmbeddedTest { } @Test - public void simpleCreateStatementAndResultSetJDBCTest() throws SQLException { - try (MonetDBEmbeddedStatement statement = db.createStatement()) { - statement.execute("SELECT * FROM test;"); - try (ResultSet result = statement.getResultSet()) { - assertEquals(10, result.getInt(1)); - } - } - } - - @Test public void simpleConnectionAndCreateStatementAndResultSetJDBCTest() throws SQLException { Properties props = new Properties(); - props.put("host", "localhost"); props.put("database", datbaseDirectory.toString()); - props.put("port", "50000"); - props.put("user", "monetdb"); - props.put("password", "monetdb"); try (Connection connection = new MonetDBEmbeddedConnection(props)) { try (Statement statement = connection.createStatement()) { diff --git a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java --- a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java +++ b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @@ -301,6 +301,34 @@ public class MonetConnection extends Mon // we're absolutely not closed, since we're brand new closed = false; } + + /** + * Only use for creating embedded connection objects. + * + * @param database + * @throws SQLException + * @throws IllegalArgumentException + */ + protected MonetConnection(String database) throws SQLException, IllegalArgumentException { + this.hostname = "localhost"; + this.port = 0; + this.database = database; + this.username = "monetdb"; + this.password = "monetdb"; + this.blobIsBinary = false; + this.lang = LANG_SQL; + + // initialise query templates (filled later, but needed below) + this.queryTempl = new String[3]; // pre, post, sep + this.commandTempl = new String[3]; // pre, post, sep + + this.server = null; + this.in = null; + this.out = null; + + // we're absolutely not closed, since we're brand new + this.closed = false; + } //== methods of interface Connection diff --git a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java --- a/java/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java +++ b/java/src/main/java/nl/cwi/monetdb/jdbc/MonetStatement.java @@ -90,9 +90,8 @@ public class MonetStatement extends Mone int resultSetHoldability) throws SQLException, IllegalArgumentException { - if (connection == null) { - addWarning("No Connection given!", "01000"); - } + if (connection == null) throw + new IllegalArgumentException("No Connection given!"); this.connection = connection; this.resultSetType = resultSetType; _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list