Changeset: 787a4fdba56e for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java/rev/787a4fdba56e
Added Files:
src/main/java/nl/cwi/monetdb/embedded/EmbeddedQueryResult.java
src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedConnection.java
src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedDatabase.java
src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java
src/main/java/nl/cwi/monetdb/embedded/column/BigintColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/BlobColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/BooleanColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/CharColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/ClobColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/Column.java
src/main/java/nl/cwi/monetdb/embedded/column/DateColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/DecimalColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/DoubleColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/GeometryColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/HugeintColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/InetColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/IntColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/JSONColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/MonthIntervalColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/RealColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/SecondIntervalColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/SmallintColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/TimeColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/TimestampColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/TinyintColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/URLColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/UUIDColumn.java
src/main/java/nl/cwi/monetdb/embedded/column/VarcharColumn.java
src/main/java/nl/cwi/monetdb/embedded/types/MonetDBEmbeddedBlob.java
Removed Files:
src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedConnection.java
src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedInstance.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/EmbeddedQueryResult.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BigintColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BlobColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BooleanColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/CharColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/ClobColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/Column.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/DateColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/DecimalColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/DoubleColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/GeometryColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/HugeintColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/InetColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/IntColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/JSONColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/MonthIntervalColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/RealColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/SecondIntervalColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/SmallintColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/TimeColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/TimestampColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/TinyintColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/URLColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/UUIDColumn.java
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/VarcharColumn.java
Branch: embedded
Log Message:
More cleaning
diffs (truncated from 792 to 300 lines):
diff --git
a/src/main/java/nl/cwi/monetdb/mcl/embedded/result/EmbeddedQueryResult.java
b/src/main/java/nl/cwi/monetdb/embedded/EmbeddedQueryResult.java
rename from
src/main/java/nl/cwi/monetdb/mcl/embedded/result/EmbeddedQueryResult.java
rename to src/main/java/nl/cwi/monetdb/embedded/EmbeddedQueryResult.java
--- a/src/main/java/nl/cwi/monetdb/mcl/embedded/result/EmbeddedQueryResult.java
+++ b/src/main/java/nl/cwi/monetdb/embedded/EmbeddedQueryResult.java
@@ -6,13 +6,13 @@
* Copyright 2008-2015 MonetDB B.V.
*/
-package nl.cwi.monetdb.mcl.embedded.result;
+package nl.cwi.monetdb.embedded;
import java.io.Closeable;
import java.sql.SQLException;
import java.util.Iterator;
-import nl.cwi.monetdb.mcl.embedded.result.column.Column;
+import nl.cwi.monetdb.embedded.column.Column;
/**
* Embedded MonetDB query result.
diff --git
a/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedConnection.java
b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedConnection.java
rename from
src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedConnection.java
rename to src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedConnection.java
--- a/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedConnection.java
+++ b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedConnection.java
@@ -6,13 +6,13 @@
* Copyright 2008-2015 MonetDB B.V.
*/
-package nl.cwi.monetdb.mcl.embedded;
+package nl.cwi.monetdb.embedded;
+
+import nl.cwi.monetdb.embedded.column.Column;
import java.io.*;
import java.sql.SQLException;
-import nl.cwi.monetdb.mcl.embedded.result.EmbeddedQueryResult;
-
/**
* A single connection to a MonetDB database instance
* Communication between Java and native C is done via JNI.
@@ -38,7 +38,7 @@ public class MonetDBEmbeddedConnection {
* @return The query result object, {@code null} if the database is not
running
* @throws SQLException
*/
- public EmbeddedQueryResult createQuery(String query) throws
SQLException, IOException {
+ public EmbeddedQueryResult createQuery(String query) throws
SQLException {
String queryString = query;
if (!queryString.endsWith(";")) {
queryString += ";";
@@ -46,6 +46,96 @@ public class MonetDBEmbeddedConnection {
return queryWrapper(queryString, true, this.connectionPointer);
}
+ /**
+ * Begins a transaction in nl.cwi.monetdb.embedded database.
+ *
+ * @throws SQLException
+ */
+ public void startTransaction() throws SQLException {
+ this.createQuery("START TRANSACTION;").close();
+ }
+
+ /**
+ * Commits a transaction in nl.cwi.monetdb.embedded database.
+ *
+ * @throws SQLException
+ */
+ public void commit() throws SQLException {
+ this.createQuery("COMMIT;").close();
+ }
+
+ /**
+ * Rollbacks a transaction in nl.cwi.monetdb.embedded database.
+ *
+ * @throws SQLException
+ */
+ public void rollback() throws SQLException {
+ this.createQuery("ROLLBACK;").close();
+ }
+
+ /**
+ * Performs a Lists the the existing tables with schemas on the system
+ *
+ * @param listSystemTables List system's tables as well (default true)
+ * @return The query result object, {@code null} if the database is not
running
+ * @throws SQLException
+ */
+ public EmbeddedQueryResult listTables(boolean listSystemTables) throws
SQLException {
+ String query = "select schemas.name as sn, tables.name as tn from
sys.tables join sys.schemas on tables.schema_id=schemas.id";
+ if (!listSystemTables) {
+ query += " where tables.system=false order by sn, tn";
+ }
+ return this.createQuery(query + ";");
+ }
+
+ /**
+ * Performs a SELECT * FROM a table in nl.cwi.monetdb.embedded database.
+ *
+ * @param tableName The name of the table
+ * @return The query result object, {@code null} if the database is not
running
+ * @throws SQLException
+ */
+ public EmbeddedQueryResult readTable(String tableName) throws
SQLException {
+ return this.createQuery("SELECT * FROM " + tableName + ";");
+ }
+
+ /**
+ * Check if a table exists in nl.cwi.monetdb.embedded database.
+ *
+ * @param tableName The name of the table
+ * @return If a the table exists or not
+ * @throws SQLException
+ */
+ public boolean checkTableExists(String tableName) throws SQLException {
+ EmbeddedQueryResult eqr = this.listTables(true);
+ Column<String> tablenames = (Column<String>) eqr.getColumn(0);
+ boolean res = false;
+ for (String str: tablenames.getAllValues()) {
+ if(str.equals(tableName)) {
+ res = true;
+ }
+ }
+ eqr.close();
+ return res;
+ }
+
+ /**
+ * Lists the table fields and types in nl.cwi.monetdb.embedded database.
+ *
+ * @param tableName The name of the table
+ * @return
+ * @throws SQLException
+ */
+ public String[] listFields(String tableName) throws SQLException {
+ if(!this.checkTableExists(tableName)) {
+ throw new SQLException("The table " + tableName + " doesn't
exist!!");
+ }
+ EmbeddedQueryResult eqr = this.createQuery("select columns.name as
name from sys.columns join sys.tables on columns.table_id=tables.id where
tables.name='" + tableName + "';");
+ String[] res = (String[]) eqr.getColumn(0).getAllValues();
+ eqr.close();
+ return res;
+ }
+
/**
* Execute an SQL query in an nl.cwi.monetdb.embedded database.
*
@@ -53,6 +143,6 @@ public class MonetDBEmbeddedConnection {
* @return The query result object, {@code null} if the database is not
running
* @throws SQLException
*/
- private native EmbeddedQueryResult queryWrapper(String query, boolean
execute, long connectionPointer) throws SQLException, IOException;
+ private native EmbeddedQueryResult queryWrapper(String query, boolean
execute, long connectionPointer) throws SQLException;
}
diff --git
a/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedInstance.java
b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedDatabase.java
rename from
src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedInstance.java
rename to src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedDatabase.java
--- a/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedInstance.java
+++ b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedDatabase.java
@@ -6,7 +6,7 @@
* Copyright 2008-2015 MonetDB B.V.
*/
-package nl.cwi.monetdb.mcl.embedded;
+package nl.cwi.monetdb.embedded;
import java.io.*;
import java.nio.file.Files;
@@ -22,54 +22,17 @@ import java.util.concurrent.locks.Reentr
* <br/>
* <strong>Note</strong>: You can have only one nl.cwi.monetdb.embedded
MonetDB database running per JVM process.
*/
-public class MonetDBEmbeddedInstance {
-
- private final static String NATIVE_LIB_PATH_IN_JAR = "src" +
File.separatorChar + "main" +
- File.separatorChar + "resources";
- private final static String NATIVE_LIB_NAME = "libmonetdb5.so";
+public class MonetDBEmbeddedDatabase {
- /**
- * The native nl.cwi.monetdb.embedded MonetDB library.
- */
- static {
- try {
- // Try load the nl.cwi.monetdb.embedded library
- System.loadLibrary("monetdb5");
- } catch (UnsatisfiedLinkError e) {
- // Still no, then get the resources.lib bundled in the jar
- loadLibFromJar(NATIVE_LIB_NAME);
+ public static MonetDBEmbeddedDatabase StartDatabase(String dbDirectory,
boolean silentFlag, boolean sequentialFlag) throws SQLException {
+ if(MonetDBEmbeddedInstance.IsEmbeddedInstanceInitialized() == false) {
+ throw new SQLException("The embedded instance has not been loaded
yet!");
+ } else {
+ return StartDatabaseInternal(dbDirectory, silentFlag,
sequentialFlag);
}
}
- private static void loadLibFromJar(String fileName) {
- String pathToLib = NATIVE_LIB_PATH_IN_JAR + File.separatorChar +
fileName;
- try {
- InputStream in =
MonetDBEmbeddedInstance.class.getResourceAsStream(File.separatorChar +
pathToLib);
- if (in == null) {
- // OK, the input stream is null, hence no .jar
- // This was probably a test and/or in an IDE
- // Just read the files from the src/main/resources dir
- in = new FileInputStream(new File(pathToLib));
- }
- // Set a temp location to extract (and load from later)
- final Path tempLibsDir =
Files.createTempDirectory("nl.cwi.monetdb.embedded");
- File fileOut = new File(tempLibsDir.toString() +
File.separatorChar + fileName);
- try (OutputStream out = new FileOutputStream(fileOut)) {
- byte[] buffer = new byte[in.available()];
- while (in.read(buffer) != -1) {
- out.write(buffer);
- }
- out.flush();
- in.close();
- // Load the resources.lib from the extracted file
- System.load(fileOut.toString());
- }
- } catch (IOException e) {
- throw new UnsatisfiedLinkError("Unable to extract native library
from JAR:" + e.getMessage());
- }
- }
-
- public static native MonetDBEmbeddedInstance StartDatabase(String
dbDirectory, boolean silentFlag, boolean sequentialFlag) throws SQLException;
+ private static native MonetDBEmbeddedDatabase StartDatabaseInternal(String
dbDirectory, boolean silentFlag, boolean sequentialFlag) throws SQLException;
private final File databaseDirectory;
@@ -83,7 +46,7 @@ public class MonetDBEmbeddedInstance {
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
- public MonetDBEmbeddedInstance(String dbDirectory, boolean silentFlag,
boolean sequentialFlag, boolean isRunning) {
+ public MonetDBEmbeddedDatabase(String dbDirectory, boolean silentFlag,
boolean sequentialFlag, boolean isRunning) {
this.databaseDirectory = new File(dbDirectory);
this.silentFlag = silentFlag;
this.sequentialFlag = sequentialFlag;
diff --git a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java
b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java
new file mode 100644
--- /dev/null
+++ b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java
@@ -0,0 +1,37 @@
+package nl.cwi.monetdb.embedded;
+
+/**
+ * Created by ferreira on 10/27/16.
+ */
+public class MonetDBEmbeddedInstance {
+
+ private static boolean isEmbeddedInstanceInitialized = false;
+
+ private static final String NATIVE_LIB_NAME = "monetdb5";
+
+ public static boolean TryLoadEmbeddedInstanceFromName(String libraryName) {
+ if(isEmbeddedInstanceInitialized == false) {
+ if(libraryName == null) {
+ libraryName = NATIVE_LIB_NAME;
+ }
+ System.loadLibrary(libraryName);
+ isEmbeddedInstanceInitialized = true;
+ }
+ return true;
+ }
+
+ public static boolean TryLoadEmbeddedInstanceFromPath(String libraryPath) {
+ if(isEmbeddedInstanceInitialized == false) {
+ if(libraryPath == null) {
+ return false;
+ }
+ System.load(libraryPath);
+ isEmbeddedInstanceInitialized = true;
+ }
+ return true;
+ }
+
+ public static boolean IsEmbeddedInstanceInitialized() {
+ return isEmbeddedInstanceInitialized;
+ }
+}
diff --git
a/src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BigintColumn.java
b/src/main/java/nl/cwi/monetdb/embedded/column/BigintColumn.java
rename from
src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BigintColumn.java
rename to src/main/java/nl/cwi/monetdb/embedded/column/BigintColumn.java
--- a/src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BigintColumn.java
+++ b/src/main/java/nl/cwi/monetdb/embedded/column/BigintColumn.java
@@ -6,9 +6,9 @@
* Copyright 2008-2015 MonetDB B.V.
*/
-package nl.cwi.monetdb.mcl.embedded.result.column;
+package nl.cwi.monetdb.embedded.column;
-import nl.cwi.monetdb.mcl.embedded.result.EmbeddedQueryResult;
+import nl.cwi.monetdb.embedded.EmbeddedQueryResult;
/**
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list