Changeset: f4ccb9794a78 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=f4ccb9794a78 Added Files: java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedConnection.java Modified Files: java/embedded/src/test/java/org/monetdb/embedded/test/EmbeddedTest.java java/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java Branch: embedded-java Log Message:
Initial JDBC connection sutff diffs (133 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 new file mode 100644 --- /dev/null +++ b/java/embedded/src/main/java/nl/cwi/monetdb/jdbc/MonetDBEmbeddedConnection.java @@ -0,0 +1,57 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2008-2015 MonetDB B.V. + */ + +package nl.cwi.monetdb.jdbc; + +import java.io.File; +import java.io.IOException; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + +import org.monetdb.embedded.MonetDBEmbedded; + +/** + * A JDBC connection for the embedded MonetDB. + * + */ +public class MonetDBEmbeddedConnection extends MonetConnection { + private final String databaseLocationString; + private final MonetDBEmbedded database; + + public MonetDBEmbeddedConnection(Properties props) throws SQLException, IllegalArgumentException { + super(props); + 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); + } + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + Statement ret = new MonetDBEmbeddedStatement(database); + statements.put(ret, null); + return ret; + } + + @Override + public String getJDBCURL() { + return "jdbc:monetdb://" + databaseLocationString; + } +} 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 @@ -14,9 +14,12 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.Iterator; +import java.util.Properties; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -25,6 +28,7 @@ import org.monetdb.embedded.MonetDBEmbed import org.monetdb.embedded.result.EmbeddedQueryResult; import org.monetdb.embedded.result.column.Column; +import nl.cwi.monetdb.jdbc.MonetDBEmbeddedConnection; import nl.cwi.monetdb.jdbc.MonetDBEmbeddedStatement; public class EmbeddedTest { @@ -286,7 +290,7 @@ public class EmbeddedTest { } @Test - public void simpleStatementJDBCTest() throws SQLException { + public void simpleCreateStatementAndResultSetJDBCTest() throws SQLException { try (MonetDBEmbeddedStatement statement = db.createStatement()) { statement.execute("SELECT * FROM test;"); try (ResultSet result = statement.getResultSet()) { @@ -295,6 +299,25 @@ public class EmbeddedTest { } } + @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()) { + statement.execute("SELECT * FROM test;"); + try (ResultSet result = statement.getResultSet()) { + assertEquals(10, result.getInt(1)); + } + } + } + } + @AfterClass public static void cleanup() throws SQLException { db.query("DROP TABLE test"); 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 @@ -120,7 +120,7 @@ public class MonetConnection extends Mon // See javadoc for documentation about WeakHashMap if you don't know what // it does !!!NOW!!! (only when you deal with it of course) /** A Map containing all (active) Statements created from this Connection */ - private Map<Statement,?> statements = new WeakHashMap<Statement, Object>(); + protected Map<Statement,?> statements = new WeakHashMap<Statement, Object>(); /** The number of results we receive from the server at once */ private int curReplySize = -1; // the server by default uses -1 (all) _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list