Mercurial > hg > monetdb-java
changeset 52:5f12b8a08204 embedded
Static implementation for the Embedded Database, removed unused code.
author | Pedro Ferreira <pedro.ferreira@monetdbsolutions.com> |
---|---|
date | Thu, 17 Nov 2016 16:09:13 +0100 (2016-11-17) |
parents | c592d8a72627 |
children | 6617eaf808cb |
files | src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedConnection.java src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedDatabase.java src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedInstance.java src/main/java/nl/cwi/monetdb/embedded/mapping/MonetDBEmbeddedBlob.java src/main/java/nl/cwi/monetdb/embedded/resultset/EmbeddedPreparedStatement.java src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSetColumn.java src/main/java/nl/cwi/monetdb/embedded/tables/IMonetDBTableRemover.java src/main/java/nl/cwi/monetdb/embedded/tables/IMonetDBTableUpdater.java src/main/java/nl/cwi/monetdb/embedded/tables/MonetDBTable.java src/main/java/nl/cwi/monetdb/embedded/tables/RowRemover.java src/main/java/nl/cwi/monetdb/embedded/tables/RowUpdater.java src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java |
diffstat | 12 files changed, 197 insertions(+), 889 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedConnection.java +++ b/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedConnection.java @@ -25,16 +25,11 @@ import java.util.Set; */ public class MonetDBEmbeddedConnection { - private final MonetDBEmbeddedDatabase database; - protected final long connectionPointer; private final Set<AbstractConnectionResult> results = new HashSet<>(); - protected MonetDBEmbeddedConnection(MonetDBEmbeddedDatabase database, long connectionPointer) { - this.database = database; - this.connectionPointer = connectionPointer; - } + protected MonetDBEmbeddedConnection(long connectionPointer) { this.connectionPointer = connectionPointer; } /** * Gets the current schema set on the connection. @@ -256,7 +251,7 @@ public class MonetDBEmbeddedConnection { */ public void closeConnection() { this.closeConnectionImplementation(); - this.database.removeConnection(this); + MonetDBEmbeddedDatabase.RemoveConnection(this); } /**
--- a/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedDatabase.java +++ b/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedDatabase.java @@ -13,7 +13,8 @@ import java.util.Set; /** * An embedded version of a MonetDB database. - * Communication between Java and native C is done via JNI. + * Communication between Java and native C is done via JNI. The MonetDB's JNI library must be successfully loaded in + * order to the other methods work. * <br/> * <strong>Note</strong>: You can have only one Embedded MonetDB database running per JVM process. * @@ -21,22 +22,33 @@ import java.util.Set; */ public class MonetDBEmbeddedDatabase { + private static MonetDBEmbeddedDatabase MonetDBEmbeddedDatabase = null; + + /** + * Check if the database is still running or not. + * + * @return A boolean indicating if the database is running + */ + public static boolean IsDatabaseRunning() { return MonetDBEmbeddedDatabase != null; } + /** * Starts a MonetDB database on the given farm. * * @param dbDirectory The full path of the farm * @param silentFlag A boolean if silent mode will be turned on or not * @param sequentialFlag A boolean indicating if the sequential pipeline will be set or not - * @return A MonetDBEmbeddedDatabase instance + * @return Returns true if the load was successful. * @throws MonetDBEmbeddedException If the JNI library has not been loaded yet or an error in the database occurred */ - public static MonetDBEmbeddedDatabase StartDatabase(String dbDirectory, boolean silentFlag, boolean sequentialFlag) + public static boolean StartDatabase(String dbDirectory, boolean silentFlag, boolean sequentialFlag) throws MonetDBEmbeddedException { - if(!MonetDBEmbeddedInstance.IsEmbeddedInstanceInitialized()) { - throw new MonetDBEmbeddedException("The embedded instance has not been loaded yet!"); + if(MonetDBEmbeddedDatabase != null) { + throw new MonetDBEmbeddedException("The database is still running!"); } else { - return StartDatabaseInternal(dbDirectory, silentFlag, sequentialFlag); + System.loadLibrary("monetdb5"); + MonetDBEmbeddedDatabase = StartDatabaseInternal(dbDirectory, silentFlag, sequentialFlag); } + return true; } /** @@ -45,7 +57,7 @@ public class MonetDBEmbeddedDatabase { * @param dbDirectory The full path of the farm * @param silentFlag A boolean if silent mode will be turned on or not * @param sequentialFlag A boolean indicating if the sequential pipeline will be set or not - * @return A MonetDBEmbeddedDatabase instance + * @return Returns true if the load was successful * @throws MonetDBEmbeddedException If the JNI library has not been loaded yet or an error in the database occurred */ /*public static CompletableFuture<MonetDBEmbeddedDatabase> StartDatabaseAsync(String dbDirectory, boolean silentFlag, @@ -53,14 +65,60 @@ public class MonetDBEmbeddedDatabase { return CompletableFuture.supplyAsync(() -> StartDatabase(dbDirectory, silentFlag, sequentialFlag)); }*/ + /** + * Get the database farm directory. + * + * @return A String representing the farm directory + */ + public static String GetDatabaseDirectory() { return MonetDBEmbeddedDatabase.databaseDirectory; } + + /** + * Check if the Silent Flag was set while creating the database. + * + * @return The Silent Flag + */ + public static boolean IsSilentFlagSet() { return MonetDBEmbeddedDatabase.silentFlag; } + + /** + * Check if the Sequential Flag was set while creating the database. + * + * @return The Sequential Flag + */ + public static boolean IsSequentialFlagSet() { return MonetDBEmbeddedDatabase.sequentialFlag; } + + /** + * Stops the database. All the pending connections will be shut down as well. + * + * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred + */ + public static void StopDatabase() throws MonetDBEmbeddedException { + if(MonetDBEmbeddedDatabase == null) { + throw new MonetDBEmbeddedException("The database is not running!"); + } else { + for(MonetDBEmbeddedConnection mdbec : MonetDBEmbeddedDatabase.connections) { + mdbec.closeConnectionImplementation(); + } + MonetDBEmbeddedDatabase.connections.clear(); + MonetDBEmbeddedDatabase.stopDatabaseInternal(); + MonetDBEmbeddedDatabase = null; + } + } + + /** + * Stops the database asynchronously. All the pending connections will be shut down as well. + * + * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred + */ + /*public static CompletableFuture<Void> StopDatabaseAsync() throws MonetDBEmbeddedException { + return CompletableFuture.runAsync(() -> this.stopDatabase()); + }*/ + private final String databaseDirectory; private final boolean silentFlag; private final boolean sequentialFlag; - private boolean isRunning = true; - private final Set<MonetDBEmbeddedConnection> connections = new HashSet<>(); private MonetDBEmbeddedDatabase(String dbDirectory, boolean silentFlag, boolean sequentialFlag) { @@ -70,74 +128,13 @@ public class MonetDBEmbeddedDatabase { } /** - * Get the database farm directory. - * - * @return A String representing the farm directory - */ - public String getDatabaseDirectory() { - return databaseDirectory; - } - - /** - * Check if the Silent Flag was set while creating the database. - * - * @return The Silent Flag - */ - public boolean isSilentFlagSet() { - return silentFlag; - } - - /** - * Check if the Sequential Flag was set while creating the database. - * - * @return The Sequential Flag - */ - public boolean isSequentialFlagSet() { - return sequentialFlag; - } - - /** - * Check if the database is still running or not. - * - * @return A boolean indicating if the database is running - */ - public boolean isRunning() { return isRunning; } - - /** - * Stops the database. All the pending connections will be shut down as well. - * - * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred - */ - public void stopDatabase() throws MonetDBEmbeddedException { - if(this.isRunning) { - for(MonetDBEmbeddedConnection mdbec : connections) { - mdbec.closeConnectionImplementation(); - } - this.connections.clear(); - this.stopDatabaseInternal(); - this.isRunning = false; - } else { - throw new MonetDBEmbeddedException("The database is not running!"); - } - } - - /** - * Stops the database asynchronously. All the pending connections will be shut down as well. - * - * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred - */ - /*public CompletableFuture<Void> stopDatabaseAsync() throws MonetDBEmbeddedException { - return CompletableFuture.runAsync(() -> this.stopDatabase()); - }*/ - - /** * Creates a connection on the database, set on the default schema. * * @return A MonetDBEmbeddedConnection instance * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred */ - public MonetDBEmbeddedConnection createConnection() throws MonetDBEmbeddedException { - return this.createConnectionInternal(); + public static MonetDBEmbeddedConnection CreateConnection() throws MonetDBEmbeddedException { + return MonetDBEmbeddedDatabase.createConnectionInternal(); } /** @@ -153,8 +150,8 @@ public class MonetDBEmbeddedDatabase { /** * Removes a connection from this database. */ - protected void removeConnection(MonetDBEmbeddedConnection con) { - this.connections.remove(con); + protected static void RemoveConnection(MonetDBEmbeddedConnection con) { + MonetDBEmbeddedDatabase.connections.remove(con); } /**
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedInstance.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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 2016 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.env; - -/** - * The MonetDB's JNI library loader for Java. - * <br/> - * <strong>Note</strong>: The MonetDB's JNI library must be successfully loaded in order to the other methods work. - * - * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> - */ -public class MonetDBEmbeddedInstance { - - private static boolean isEmbeddedInstanceInitialized = false; - - private static final String NATIVE_LIB_NAME = "monetdb5"; - - /** - * Tries to load the JNI library with MonetDBLite from the current Java Classpath. - * - * @param libraryName The library name, if null will load the default name "monetdb5" - * @return A boolean indicating if the load was successful - */ - public static boolean TryLoadEmbeddedInstanceFromName(String libraryName) { - if(!isEmbeddedInstanceInitialized) { - if(libraryName == null) { - libraryName = NATIVE_LIB_NAME; - } - System.loadLibrary(libraryName); - isEmbeddedInstanceInitialized = true; - } - return true; - } - - /** - * Tries to load the JNI library with MonetDBLite from the given path. - * - * @param libraryPath The full library path name - * @return A boolean indicating if the load was successful - */ - public static boolean TryLoadEmbeddedInstanceFromPath(String libraryPath) { - if(!isEmbeddedInstanceInitialized) { - if(libraryPath == null) { - System.load(NATIVE_LIB_NAME); - } - System.load(libraryPath); - isEmbeddedInstanceInitialized = true; - } - return true; - } - - /** - * Check if the JNI library with MonetDBLite has been loaded yet or not. - * - * @return A boolean indicating if it is loaded - */ - public static boolean IsEmbeddedInstanceInitialized() { - return isEmbeddedInstanceInitialized; - } -}
--- a/src/main/java/nl/cwi/monetdb/embedded/mapping/MonetDBEmbeddedBlob.java +++ b/src/main/java/nl/cwi/monetdb/embedded/mapping/MonetDBEmbeddedBlob.java @@ -1,19 +1,22 @@ package nl.cwi.monetdb.embedded.mapping; -import java.io.Serializable; +import java.io.*; +import java.sql.Blob; +import java.sql.SQLException; import java.util.Arrays; +import java.util.Collections; /** * A Java representation for the BLOB data type. Added for more efficient data mapping when fetching from the database. * * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> */ -public class MonetDBEmbeddedBlob implements Serializable { +public class MonetDBEmbeddedBlob implements Serializable, Blob { /** * The BLOB's content as a Java byte array. */ - private final byte[] blob; + private byte[] blob; public MonetDBEmbeddedBlob(byte[] blob) { this.blob = blob; } @@ -43,4 +46,106 @@ public class MonetDBEmbeddedBlob impleme */ @Override public String toString() { return Arrays.toString(blob); } + + private void checkFreed() throws SQLException { + if(this.blob == null) { + throw new SQLException("Thsi blob was freed!"); + } + } + + @Override + public long length() throws SQLException { + this.checkFreed(); + return this.blob.length; + } + + @Override + public byte[] getBytes(long pos, int length) throws SQLException { + this.checkFreed(); + return Arrays.copyOfRange(this.blob, (int) pos, length); + } + + @Override + public InputStream getBinaryStream() throws SQLException { + this.checkFreed(); + return new ByteArrayInputStream(this.blob); + } + + @Override + public long position(byte[] pattern, long start) throws SQLException { + this.checkFreed(); + byte[] subArray = Arrays.copyOfRange(this.blob, (int)start, pattern.length); + return Collections.indexOfSubList(Arrays.asList(subArray), Arrays.asList(pattern)); + } + + @Override + public long position(Blob pattern, long start) throws SQLException { + this.checkFreed(); + byte[] subArray = Arrays.copyOfRange(this.blob, (int)start, (int) pattern.length()); + return Collections.indexOfSubList(Arrays.asList(subArray), Arrays.asList(pattern.getBytes(0, (int)pattern.length()))); + } + + @Override + public int setBytes(long pos, byte[] bytes) throws SQLException { + this.checkFreed(); + int newFinalLength = (int) pos + bytes.length; + byte[] newblob; + + if(newFinalLength > this.blob.length) { + newblob = new byte[newFinalLength]; + } else { + newblob = this.blob; + } + System.arraycopy(bytes, 0, newblob, (int) pos, bytes.length); + this.blob = newblob; + return bytes.length; + } + + @Override + public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { + this.checkFreed(); + int newFinalLength = (int) pos + len; + byte[] newblob; + + if(newFinalLength > this.blob.length) { + newblob = new byte[newFinalLength]; + } else { + newblob = this.blob; + } + System.arraycopy(bytes, offset, newblob, (int) pos, len); + this.blob = newblob; + return bytes.length; + } + + @Override + public OutputStream setBinaryStream(long pos) throws SQLException { + this.checkFreed(); + ByteArrayOutputStream res = null; + try { + res = new ByteArrayOutputStream(); + res.write(this.blob); + } catch (IOException e) { + throw new SQLException(e); + } + return res; + } + + @Override + public void truncate(long len) throws SQLException { + this.checkFreed(); + byte[] newblob = new byte[(int)len]; + System.arraycopy(this.blob, 0, newblob, 0, (int)len); + this.blob = newblob; + } + + @Override + public void free() throws SQLException { + this.blob = null; + } + + @Override + public InputStream getBinaryStream(long pos, long length) throws SQLException { + this.checkFreed(); + return new ByteArrayInputStream(Arrays.copyOfRange(this.blob, (int) pos, (int) length)); + } }
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/resultset/EmbeddedPreparedStatement.java +++ /dev/null @@ -1,430 +0,0 @@ -/* - * 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 2016 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.resultset; - -import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedConnection; -import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedException; -import nl.cwi.monetdb.embedded.mapping.MonetDBEmbeddedBlob; -import nl.cwi.monetdb.embedded.mapping.MonetDBToJavaMapping; -import nl.cwi.monetdb.embedded.utils.StringEscaper; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.math.RoundingMode; -import java.net.Inet4Address; -import java.net.URL; -import java.sql.PreparedStatement; -import java.sql.Time; -import java.sql.Timestamp; -import java.text.SimpleDateFormat; -import java.util.*; - -/** - * The embedded version of the {@link PreparedStatement} interface from JDBC (not inheriting for easier implementation). - * - * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a>, Fabian Groffen, Martin van Dinther - */ -public class EmbeddedPreparedStatement { - - /** - * A formatter for Time columns without timezone. - */ - private static final SimpleDateFormat TimeFormatter; - - /** - * A formatter for Time columns with timezone. - */ - private static final SimpleDateFormat TimeTzFormatter; - - /** - * A formatter for Timestamp columns without timezone. - */ - private static final SimpleDateFormat TimestampFormatter; - - /** - * A formatter for Timestamp columns with timezone. - */ - private static final SimpleDateFormat TimestampTzFormatter; - - /** - * A list of Java classes that don't need special parsing of values (jsut call toString() method). - */ - private static final Set<Class<?>> DirectMappingClasses; - - static { - TimeFormatter = new SimpleDateFormat("HH:mm:ss.SSS"); - TimeTzFormatter = new SimpleDateFormat("HH:mm:ss.SSSZ"); - TimestampFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); - TimestampTzFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ"); - DirectMappingClasses = new HashSet<>(); - DirectMappingClasses.add(Boolean.class); - DirectMappingClasses.add(Byte.class); - DirectMappingClasses.add(Short.class); - DirectMappingClasses.add(Integer.class); - DirectMappingClasses.add(Long.class); - DirectMappingClasses.add(BigInteger.class); - DirectMappingClasses.add(Float.class); - DirectMappingClasses.add(Double.class); - DirectMappingClasses.add(MonetDBEmbeddedBlob.class); - DirectMappingClasses.add(URL.class); - DirectMappingClasses.add(Inet4Address.class); - DirectMappingClasses.add(UUID.class); - } - - /** - * The corresponding connection for this Prepared statement. - */ - private final MonetDBEmbeddedConnection connection; - - /** - * The id of the generated prepare statement. - */ - private final int prepareId; - - /** - * The number of parsed parameters (?) in the statement. - */ - private final int numberOfParameters; - - /** - * The list of MonetDB-to-Java mappings of the parameters. - */ - private final MonetDBToJavaMapping[] parametersMappings; - - /** - * The number of digits allowed for each parameter. - */ - private final int[] parametersDigits; - - /** - * The scale for each parameter. - */ - private final int[] parametersScales; - - /** - * The schema name corresponding for each parameter (or null if not corresponding). - */ - private final String[] parametersSchemas; - - /** - * The table name corresponding for each parameter (or null if not corresponding). - */ - private final String[] parametersTables; - - /** - * The column name corresponding for each parameter (or null if not corresponding). - */ - private final String[] parametersNames; - - /** - * The parsed value for each parameter (or null if not parsed). - */ - private final String[] parsedValues; - - protected EmbeddedPreparedStatement(MonetDBEmbeddedConnection connection, int prepareId, - MonetDBToJavaMapping[] parametersMappings, int[] parametersDigits, - int[] parametersScales, String[] parametersSchemas, String[] parametersTables, - String[] parametersNames, String[] parsedValues) throws MonetDBEmbeddedException { - this.connection = connection; - this.prepareId = prepareId; - this.numberOfParameters = parametersMappings.length; - this.parametersMappings = parametersMappings; - this.parametersDigits = parametersDigits; - this.parametersScales = parametersScales; - this.parametersSchemas = parametersSchemas; - this.parametersTables = parametersTables; - this.parametersNames = parametersNames; - this.parsedValues = parsedValues; - } - - /** - * Get the corresponding connection for this statement. - * - * @return A MonetDBEmbeddedConnection instance - */ - public MonetDBEmbeddedConnection getConnection() { - return connection; - } - - /** - * Get the number of parsed parameters (?) in the statement. - * - * @return The number of parsed parameters (?) in the statement - */ - public int getNumberOfParameters() { return numberOfParameters; } - - /** - * Gets the list of MonetDB-to-Java mappings of the parameters. - * - * @return The list of MonetDB-to-Java mappings of the parameters - */ - public MonetDBToJavaMapping[] getParametersMappings() { - return parametersMappings; - } - - /** - * Gets the number of digits allowed for each parameter. - * - * @return The number of digits allowed for each parameter - */ - public int[] getParametersDigits() { - return parametersDigits; - } - - /** - * Gets the scale for each parameter. - * - * @return The scale for each parameter - */ - public int[] getParametersScales() { - return parametersScales; - } - - /** - * Gets the schema name corresponding for each parameter (or null if not corresponding). - * - * @return The schema name corresponding for each parameter (or null if not corresponding) - */ - public String[] getParametersSchemas() { - return parametersSchemas; - } - - /** - * Gets the table name corresponding for each parameter (or null if not corresponding). - * - * @return The table name corresponding for each parameter (or null if not corresponding) - */ - public String[] getParametersTables() { - return parametersTables; - } - - /** - * Gets the column name corresponding for each parameter (or null if not corresponding). - * - * @return The column name corresponding for each parameter (or null if not corresponding) - */ - public String[] getParametersNames() { - return parametersNames; - } - - /** - * Gets the parsed value for each parameter (or null if not parsed). - * - * @return The parsed value for each parameter (or null if not parsed) - */ - public String[] getParsedValues() { - return parsedValues; - } - - /** - * Clears the current parsed values. - */ - public void clearParameters() { - for (int i = 0; i < parsedValues.length; i++) { - parsedValues[i] = null; - } - } - - /** - * Converts a Java String into the MonetDB representation according to its type. - * - * @param parameter The index of the parameter - * @param value A String value to parse - * @return The parsed String as a String - * @throws MonetDBEmbeddedException If the type is a char or varchar an the length is longer than allowed - */ - private String setString(int parameter, String value) throws MonetDBEmbeddedException { - String type = this.parametersMappings[parameter].toString(); - if((type.equals("Char") || type.equals("Varchar")) && value.length() > this.parametersDigits[parameter]) { - throw new MonetDBEmbeddedException("The length is higher than allowed: " + value.length() + " > " - + this.parametersDigits[parameter] + "!"); - } else { - return value; - } - } - - /** - * Converts a Java BigDecimal into a MonetDB decimal representation (adapted from the JDBC driver implementation). - * - * @param parameter The index of the parameter - * @param value A BigDecimal value to parse - * @return The parsed BigDecimal as a String - * @throws MonetDBEmbeddedException If the value exceeds the allowed digits or scale - */ - private String setBigDecimal(int parameter, BigDecimal value) throws MonetDBEmbeddedException { - // round to the scale of the DB: - value = value.setScale(this.parametersScales[parameter], RoundingMode.HALF_UP); - if (value.precision() > this.parametersDigits[parameter]) { - throw new MonetDBEmbeddedException("DECIMAL value exceeds allowed digits/scale: " + value.toPlainString() - + " (" + this.parametersDigits[parameter] + "/" + this.parametersScales[parameter] + ")"); - } - - // MonetDB doesn't like leading 0's, since it counts them as part of - // the precision, so let's strip them off. (But be careful not to do - // this to the exact number "0".) Also strip off trailing - // numbers that are inherent to the double representation. - String xStr = value.toPlainString(); - int dot = xStr.indexOf('.'); - if (dot >= 0) - xStr = xStr.substring(0, Math.min(xStr.length(), dot + 1 + this.parametersScales[parameter])); - while (xStr.startsWith("0") && xStr.length() > 1) - xStr = xStr.substring(1); - return xStr; - } - - /** - * Converts a Java SQL Time into a MonetDB time representation (adapted from the JDBC driver implementation). - * - * @param parameter The index of the parameter - * @param value A Java SQL Time value to parse - * @return The parsed Java SQL Time as a String - */ - private String setTime(int parameter, Time value) { - boolean hasTimeZone = this.parametersMappings[parameter].toString().endsWith("Tz"); - if(hasTimeZone) { - String RFC822 = TimeTzFormatter.format(value); - return RFC822.substring(0, 15) + ":" + RFC822.substring(15); - } else { - return TimeFormatter.format(value); - } - } - - /** - * Converts a Java SQL Timestamp into a MonetDB time representation (adapted from the JDBC driver implementation). - * - * @param parameter The index of the parameter - * @param value A Java SQL Timestamp value to parse - * @return The parsed Java SQL Timestamp as a String - */ - private String setTimestamp(int parameter, Timestamp value) { - boolean hasTimeZone = this.parametersMappings[parameter].toString().endsWith("Tz"); - if(hasTimeZone) { - String RFC822 = TimestampTzFormatter.format(value); - return RFC822.substring(0, 26) + ":" + RFC822.substring(26); - } else { - return TimestampFormatter.format(value); - } - } - - /** - * Converts a Java Class into the corresponding MonetDB representation in a parameter. - * - * @param <T> The Java Class to map to MonetDB - * @param parameter The index of the parameter - * @param value The instance of the Java class to map - * @throws MonetDBEmbeddedException If the Java class has no mapping in a MonetDB datatype - */ - public <T> void setParameterValue(int parameter, T value) throws MonetDBEmbeddedException { - this.setParameterValue(parameter, value, this.parametersMappings[parameter].getJavaClass()); - } - - /** - * Converts a Java Class into the corresponding MonetDB representation in a parameter. - * - * @param <T> The Java Class to map to MonetDB - * @param parameter The index of the parameter - * @param value The instance of the Java class to map - * @param javaClass The class of the instance - * @throws MonetDBEmbeddedException If the Java class has no mapping in a MonetDB datatype - */ - public <T> void setParameterValue(int parameter, T value, Class<T> javaClass) throws MonetDBEmbeddedException { - if(value == null) { - this.setParameterNull(parameter); - } else { - String valueToSubmit; - if(DirectMappingClasses.contains(javaClass)) { - valueToSubmit = value.toString(); - } else if(javaClass.equals(String.class)) { - valueToSubmit = this.setString(parameter, (String) value); - } else if(javaClass.equals(BigDecimal.class)) { - valueToSubmit = this.setBigDecimal(parameter, (BigDecimal) value); - } else if(javaClass.equals(Time.class)) { - valueToSubmit = this.setTime(parameter, (Time) value); - } else if(javaClass.equals(Timestamp.class)) { - valueToSubmit = this.setTimestamp(parameter, (Timestamp) value); - } else { - throw new MonetDBEmbeddedException("The class " + javaClass.getSimpleName() + - " is not supported by the mapping!"); - } - this.parsedValues[parameter] = StringEscaper.SQLStringEscape(valueToSubmit); - } - } - - /** - * Set a parameter as a null value. - * - * @param parameter The index of the parameter - */ - public void setParameterNull(int parameter) { this.parsedValues[parameter] = "NULL"; } - - /** - * Creates the SQL String from the parsed parameters (adapted from the JDBC driver implementation). - * - * @return The SQL statement with all parameters replaced - * @throws MonetDBEmbeddedException If a parameter has not been set yet - */ - private String applyParameters() throws MonetDBEmbeddedException { - StringBuilder buf = new StringBuilder(8 + 12 * this.numberOfParameters).append("exec ") - .append(this.prepareId).append('('); - int col = 0; - for (int i = 0; i < this.numberOfParameters; i++) { - if (this.parametersNames[i] != null) - continue; - col++; - if (col > 1) - buf.append(','); - if (this.parsedValues[i] == null) { - throw new MonetDBEmbeddedException("Cannot execute, parameter " + col + " is missing!"); - } - buf.append(this.parsedValues[i]); - } - buf.append(')'); - return buf.toString(); - } - - /** - * Executes this statement as a SQL query without a result set. - * - * @return The update result object - * @throws MonetDBEmbeddedException If an error in the database occurred or if a parameter has not been set yet - */ - public UpdateResultSet sendUpdate() throws MonetDBEmbeddedException { - return this.connection.sendUpdate(this.applyParameters()); - } - - /** - * Executes this statement as a SQL query with a result set. - * - * @return The query result object - * @throws MonetDBEmbeddedException If an error in the database occurred or if a parameter has not been set yet - */ - public QueryResultSet sendQuery() throws MonetDBEmbeddedException { - return this.connection.sendQuery(this.applyParameters()); - } - - /** - * Executes this statement as a SQL query without a result set asynchronously. - * - * @return The update result object - * @throws MonetDBEmbeddedException If an error in the database occurred or if a parameter has not been set yet - */ - /*public CompletableFuture<UpdateResultSet> sendUpdateAsync() throws MonetDBEmbeddedException { - return this.connection.sendUpdateAsync(this.applyParameters()); - }*/ - - /** - * Executes this statement as a SQL query with a result set asynchronously. - * - * @return The query result object - * @throws MonetDBEmbeddedException If an error in the database occurred or if a parameter has not been set yet - */ - /*public CompletableFuture<QueryResultSet> sendQueryAsync() throws MonetDBEmbeddedException { - return this.connection.sendQueryAsync(this.applyParameters()); - }*/ -}
--- a/src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSetColumn.java +++ b/src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSetColumn.java @@ -33,12 +33,24 @@ public class QueryResultSetColumn<T> ext */ private final T[] values; + /** + * The index of the column. + */ private final int resultSetIndex; + /** + * The column name. + */ private final String columnName; + /** + * The column digits. + */ private final int columnDigits; + /** + * The column scale. + */ private final int columnScale; /**
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/tables/IMonetDBTableRemover.java +++ /dev/null @@ -1,18 +0,0 @@ -package nl.cwi.monetdb.embedded.tables; - -/** - * A row removal iterator for a MonetDB table. - * - * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> - */ -public interface IMonetDBTableRemover extends IMonetDBTableBaseIterator { - - /** - * The business logic for the iterator. Use the - * {@link nl.cwi.monetdb.embedded.tables.RowRemover#setCurrentRowToRemove(boolean) setToRemove} - * method in <code>processNextRow</code> to set the current row to remove. - * - * @param nextRow The next row in the iteration. - */ - void processNextRow(RowRemover nextRow); -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/tables/IMonetDBTableUpdater.java +++ /dev/null @@ -1,18 +0,0 @@ -package nl.cwi.monetdb.embedded.tables; - -/** - * A row update iterator for a MonetDB table. - * - * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> - */ -public interface IMonetDBTableUpdater extends IMonetDBTableBaseIterator { - - /** - * The business logic for the iterator. Use the - * {@link nl.cwi.monetdb.embedded.tables.RowUpdater#updateColumnByIndex(int, Class, Object)} - * method in <code>processNextRow</code> to update the current row. - * - * @param nextRow The next row in the iteration. - */ - void processNextRow(RowUpdater nextRow); -}
--- a/src/main/java/nl/cwi/monetdb/embedded/tables/MonetDBTable.java +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/MonetDBTable.java @@ -168,98 +168,6 @@ public class MonetDBTable extends Abstra } /** - * Iterate over the table using a {@link IMonetDBTableCursor} - * instance asynchronously. - * - * @param iterator The iterator with the business logic - * @return The number of rows iterated - * @throws MonetDBEmbeddedException If an error in the database occurred - */ - /*public CompletableFuture<Integer> iterateTable(IMonetDBTableCursor iterator) throws MonetDBEmbeddedException { - return CompletableFuture.supplyAsync(() -> this.iterateTable(iterator)); - }*/ - - /** - * Perform an update iteration over the table using a {@link nl.cwi.monetdb.embedded.tables.IMonetDBTableUpdater} - * instance. - * - * @param updater The iterator with the business logic - * @return The number of rows updated - * @throws MonetDBEmbeddedException If an error in the database occurred - */ - /*public int updateRows(IMonetDBTableUpdater updater) throws MonetDBEmbeddedException { - int[] limits = this.prepareIterator(updater); - RowUpdater ru = this.getRowUpdaterInternal(this.connectionPointer, this.schemaName, this.tableName, - limits[0], limits[1]); - while(ru.tryContinueIteration()) { - updater.processNextRow(ru); - } - return ru.submitUpdates(); - }*/ - - /** - * Perform an update iteration over the table using a {@link nl.cwi.monetdb.embedded.tables.IMonetDBTableUpdater} - * instance asynchronously. - * - * @param updater The iterator with the business logic - * @return The number of rows updated - * @throws MonetDBEmbeddedException If an error in the database occurred - */ - /*public CompletableFuture<Integer> updateRowsAsync(IMonetDBTableUpdater updater) throws MonetDBEmbeddedException { - return CompletableFuture.supplyAsync(() -> this.updateRows(updater)); - }*/ - - /** - * Perform a removal iteration over the table using a {@link nl.cwi.monetdb.embedded.tables.IMonetDBTableRemover} - * instance. - * - * @param remover The iterator with the business logic - * @return The number of rows removed - * @throws MonetDBEmbeddedException If an error in the database occurred - */ - /*public int removeRows(IMonetDBTableRemover remover) throws MonetDBEmbeddedException { - int[] limits = this.prepareIterator(remover); - RowRemover rr = this.getRowRemoverInternal(this.connectionPointer, this.schemaName, this.tableName, - limits[0], limits[1]); - while(rr.tryContinueIteration()) { - remover.processNextRow(rr); - } - return rr.submitDeletes(); - }*/ - - /** - * Perform a removal iteration over the table using a {@link nl.cwi.monetdb.embedded.tables.IMonetDBTableRemover} - * instance asynchronously. - * - * @param remover The iterator with the business logic - * @return The number of rows removed - * @throws MonetDBEmbeddedException If an error in the database occurred - */ - /*public CompletableFuture<Integer> removeRowsAsync(IMonetDBTableRemover remover) throws MonetDBEmbeddedException { - return CompletableFuture.supplyAsync(() -> this.removeRows(remover)); - }*/ - - /** - * Deletes all rows in the table. - * - * @return The number of rows removed - * @throws MonetDBEmbeddedException If an error in the database occurred - */ - /*public int truncateTable() throws MonetDBEmbeddedException { - return this.truncateTableInternal(this.connectionPointer, this.schemaName, this.tableName); - }*/ - - /** - * Deletes all rows in the table asynchronously. - * - * @return The number of rows removed - * @throws MonetDBEmbeddedException If an error in the database occurred - */ - /*public CompletableFuture<Integer> truncateTableAsync() throws MonetDBEmbeddedException { - return CompletableFuture.supplyAsync(() -> this.truncateTable()); - }*/ - - /** * Appends new rows to the table. As MonetDB's storage is column-wise, the method * {@link nl.cwi.monetdb.embedded.tables.MonetDBTable#appendColumns(Object[][]) appendColumns} is preferable * over this one. @@ -328,23 +236,17 @@ public class MonetDBTable extends Abstra return this.appendColumnsInternal(jClasses, javaIndexes, data, numberOfRowsToInsert); } - /** + /* * Appends new rows to the table column-wise and asynchronously. * - * @param columns An array of columns to append + * @param data An array of columns to append * @return The number of rows appended * @throws MonetDBEmbeddedException If an error in the database occurred */ - /*public CompletableFuture<Integer> appendColumnsAsync(Object[][] columns) throws MonetDBEmbeddedException { + /*public CompletableFuture<Integer> appendColumnsAsync(Object[][] data) throws MonetDBEmbeddedException { return CompletableFuture.supplyAsync(() -> this.appendColumns(schemaName, tableName)); }*/ - /** - * Internal implementation of table truncation. - */ - /*private native int truncateTableInternal(long connectionPointer, String schemaName, String tableName) - throws MonetDBEmbeddedException;*/ - @Override protected void closeImplementation() {}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/tables/RowRemover.java +++ /dev/null @@ -1,42 +0,0 @@ -package nl.cwi.monetdb.embedded.tables; - -import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedException; - -/** - * The removal iterator for a MonetDB table. - * - * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> - */ -public class RowRemover extends RowIterator { - - /** - * If the next row is going to be removed. - */ - private boolean[] removeIndexes; - - protected RowRemover(MonetDBTable table, Object[][] rows, int firstIndex, int lastIndex) { - super(table, rows, firstIndex, lastIndex); - this.removeIndexes = new boolean[lastIndex - firstIndex]; - } - - /** - * Checks if the next row is going to be removed. - * - * @return If the next row is going to be removed - */ - public boolean isCurrentRowSetToRemove() { return this.removeIndexes[this.getCurrentIterationNumber()]; } - - /** - * Sets the current row to remove or not. - * - * @param toRemove A boolean indicating if the next row will be removed - */ - public void setCurrentRowToRemove(boolean toRemove) { this.removeIndexes[this.getCurrentIterationNumber()] = toRemove; } - - /** - * Removes the rows. - * - * @return The number of rows deleted - */ - //protected native int submitDeletes() throws MonetDBEmbeddedException; -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/tables/RowUpdater.java +++ /dev/null @@ -1,129 +0,0 @@ -package nl.cwi.monetdb.embedded.tables; - -import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedException; - -import java.util.Arrays; - -/** - * The update iterator for a MonetDB table. - * - * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> - */ -public class RowUpdater extends RowIterator { - - /** - * A boolean array to check the columns to be updated. - */ - private final boolean[][] updatedIndexes; - - protected RowUpdater(MonetDBTable table, Object[][] rows, int firstIndex, int lastIndex) { - super(table, rows, firstIndex, lastIndex); - this.updatedIndexes = new boolean[lastIndex - firstIndex][table.getNumberOfColumns()]; - } - - /** - * Updates a column value. - * - * @param <T> A Java class mapped to a MonetDB data type - * @param index The index of the column - * @param value The value to set - */ - public <T> void updateColumnByIndex(int index, T value) { - this.getCurrentRow().setColumnByIndex(index, value); - this.updatedIndexes[this.getCurrentIterationNumber()][index] = true; - } - - /** - * Updates a column value. - * - * @param <T> A Java class mapped to a MonetDB data type - * @param index The index of the column - * @param javaClass The Java class - * @param value The value to set - */ - public <T> void updateColumnByIndex(int index, Class<T> javaClass, T value) { - this.getCurrentRow().setColumnByIndex(index, javaClass, value); - this.updatedIndexes[this.getCurrentIterationNumber()][index] = true; - } - - /** - * Updates a column value. - * - * @param <T> A Java class mapped to a MonetDB data type - * @param name The name of the column - * @param value The value to set - */ - public <T> void updateColumnByName(String name, T value) { - String[] colNames = this.getTable().getColumnNames(); - int index = 0; - for (String colName : colNames) { - if (name.equals(colName)) { - this.updateColumnByIndex(index, value); - return; - } - index++; - } - throw new ArrayIndexOutOfBoundsException("The column is not present in the result set!"); - } - - /** - * Updates a column value. - * - * @param <T> A Java class mapped to a MonetDB data type - * @param name The name of the column - * @param javaClass The Java class - * @param value The value to set - */ - public <T> void updateColumnByName(String name, Class<T> javaClass, T value) { - String[] colNames = this.getTable().getColumnNames(); - int index = 0; - for (String colName : colNames) { - if (name.equals(colName)) { - this.updateColumnByIndex(index, javaClass, value); - return; - } - index++; - } - throw new ArrayIndexOutOfBoundsException("The column is not present in the result set!"); - } - - /** - * Updates all column values. - * - * @param values The values to set - */ - public void updateAllColumns(Object[] values) { - this.getCurrentRow().setAllColumns(values); - Arrays.fill(this.updatedIndexes[this.getCurrentIterationNumber()], true); - } - - /** - * Gets a boolean array of the column indexes to be updated in the current iteration. - * - * @return A boolean array of the column indexes to be updated in the current iteration - */ - public boolean[] getCurrentUpdatedIndexes() { - return Arrays.copyOf(this.updatedIndexes[this.getCurrentIterationNumber()], this.updatedIndexes.length); - } - - /** - * Check if the current row is set to be updated. - * - * @return A boolean indicating if the current row is to be updated - */ - public boolean toUpdate() { - for (boolean bol : this.updatedIndexes[this.getCurrentIterationNumber()]) { - if(bol) { - return true; - } - } - return false; - } - - /** - * Updates the row set. - * - * @return The number of rows updated - */ - //protected native int submitUpdates() throws MonetDBEmbeddedException; -}
--- a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @@ -172,7 +172,7 @@ public class MonetConnection extends Mon boolean debug = Boolean.valueOf(props.getProperty("debug")); String hash = props.getProperty("hash"); blobIsBinary = Boolean.valueOf(props.getProperty("treat_blob_as_binary")); - int sockTimeout = 0; + int sockTimeout; try { sockTimeout = Integer.parseInt(props.getProperty("so_timeout")); } catch (NumberFormatException e) {