Mercurial > hg > monetdb-java
changeset 34:068ec5964f28 embedded
Major cleaning on the Embedded code. Added support for prepared statements, fetching rows, mapping results into Java Classes incrementally and future async support.
author | Pedro Ferreira <pedro.ferreira@monetdbsolutions.com> |
---|---|
date | Tue, 01 Nov 2016 17:35:36 +0100 (2016-11-01) |
parents | e67d58485172 |
children | 5e58809cfbed |
files | src/main/java/nl/cwi/monetdb/embedded/AbstractColumn.java src/main/java/nl/cwi/monetdb/embedded/AbstractQueryResultSet.java src/main/java/nl/cwi/monetdb/embedded/AbstractStatementResult.java src/main/java/nl/cwi/monetdb/embedded/EmbeddedPreparedStatement.java 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/MonetDBEmbeddedException.java src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java src/main/java/nl/cwi/monetdb/embedded/MonetDBToJavaMapping.java src/main/java/nl/cwi/monetdb/embedded/QueryResultSet.java src/main/java/nl/cwi/monetdb/embedded/QueryResultSetColumn.java src/main/java/nl/cwi/monetdb/embedded/QueryRowsResultSet.java src/main/java/nl/cwi/monetdb/embedded/UpdateResultSet.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 |
diffstat | 39 files changed, 1908 insertions(+), 1424 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/AbstractColumn.java @@ -0,0 +1,116 @@ +/* + * 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; + +/** + * A single Java representation of a MonetDB column. + * + * @param <T> A Java class mapped to a MonetDB data type + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public abstract class AbstractColumn<T> implements Iterable<T> { + + /** + * The pointer to the corresponding AbstractQueryResultSet + */ + protected final long resultSetPointer; + + /** + * Index on the AbstractQueryResultSet + */ + protected final int resultSetIndex; + + /** + * The number of rows in this column + */ + protected final int numberOfRows; + + /** + * The name of the columns in the query result + */ + protected final String columnName; + + /** + * The Mapping between MonetDB type and the Java Class + */ + protected final MonetDBToJavaMapping mapping; + + /** + * The number of digits (radix 2) for numeric types or max length for character/binary strings. + */ + protected final int columnDigits; + + /** + * The precision after decimal point. Only applicable for decimal/numeric types. + */ + protected final int columnScale; + + protected AbstractColumn(long resultSetPointer, int resultSetIndex, int numberOfRows, String columnName, + String columnType, int columnDigits, int columnScale) { + this.resultSetPointer = resultSetPointer; + this.resultSetIndex = resultSetIndex; + this.numberOfRows = numberOfRows; + this.columnName = columnName; + this.mapping = Enum.valueOf(MonetDBToJavaMapping.class, columnType); + this.columnDigits = columnDigits; + this.columnScale = columnScale; + } + + /** + * Get the number of rows in this column + * + * @return The number of rows + */ + public int getNumberOfRows() { return numberOfRows; } + + /** + * Get the result set index of the column. + * + * @return The index number + */ + public int getResultSetIndex() { return resultSetIndex; } + + /** + * Get the name of the column. + * + * @return The column name + */ + public String getColumnName() { + return columnName; + } + + /** + * Get the type of the column. + * + * @return The Column type + */ + public String getColumnType() { return mapping.toString(); } + + /** + * Get the Java mapping of the column. + * + * @return A enum constant of the Java mapping + */ + public MonetDBToJavaMapping getMapping() { return mapping; } + + /** + * Get column digits of the column. + * + * @return The number of digits + */ + public int getColumnDigits() { return columnDigits; } + + /** + * Get scale of the column. + * + * @return The scale + */ + public int getColumnScale() { return columnScale; } + +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/AbstractQueryResultSet.java @@ -0,0 +1,155 @@ +/* + * 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; + +/** + * The result set from a sendQuery method from a connection. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public abstract class AbstractQueryResultSet extends AbstractStatementResult implements Iterable { + + /** + * The number of columns in the query result. + */ + protected final int numberOfColumns; + + /** + * The number of rows in the query result. + */ + protected final int numberOfRows; + + protected AbstractQueryResultSet(MonetDBEmbeddedConnection connection, long resultPointer, int numberOfColumns, + int numberOfRows) { + super(connection, resultPointer); + this.numberOfColumns = numberOfColumns; + this.numberOfRows = numberOfRows; + } + + /** + * Get the query set column values as an Iterable. + * + * @return An Iterable over the columns + */ + protected abstract Iterable<AbstractColumn<?>> getIterable(); + + /** + * Returns the number of columns in the result set. + * + * @return Number of columns + */ + public int getNumberOfColumns() { + return this.numberOfColumns; + } + + /** + * Returns the number of rows in the result set. + * + * @return Number of rows + */ + public int getNumberOfRows() { + return this.numberOfRows; + } + + /** + * Get the columns names as a string array. + * + * @return The columns names array + */ + public String[] getColumnNames() { + int i = 0; + String[] result = new String[this.numberOfColumns]; + for(AbstractColumn col : this.getIterable()) { + result[i] = col.getColumnName(); + } + return result; + } + + /** + * Get the columns types as a string array. + * + * @return The columns types array + */ + public String[] getColumnTypes() { + int i = 0; + String[] result = new String[this.numberOfColumns]; + for(AbstractColumn col : this.getIterable()) { + result[i] = col.getColumnType(); + } + return result; + } + + /** + * Get the Java mappings as a MonetDBToJavaMapping array. + * + * @return The columns MonetDBToJavaMapping array + */ + public MonetDBToJavaMapping[] getMappings() { + int i = 0; + MonetDBToJavaMapping[] result = new MonetDBToJavaMapping[this.numberOfColumns]; + for(AbstractColumn col : this.getIterable()) { + result[i] = col.getMapping(); + } + return result; + } + + /** + * Get the columns digits as a int array. + * + * @return The columns digits array + */ + public int[] getColumnDigits() { + int i = 0; + int[] result = new int[this.numberOfColumns]; + for(AbstractColumn col : this.getIterable()) { + result[i] = col.getColumnDigits(); + } + return result; + } + + /** + * Get the columns scales as a int array. + * + * @return The columns scales array + */ + public int[] getColumnScales() { + int i = 0; + int[] result = new int[this.numberOfColumns]; + for(AbstractColumn col : this.getIterable()) { + result[i] = col.getColumnScale(); + } + return result; + } + + /** + * Get a columns' values from the result set by index. + * + * @param index QueryResultSetColumn index (starting from 0) + * @return The columns, {@code null} if index not in bounds + */ + public abstract <T> QueryResultSetColumn<T> getColumn(int index); + + /** + * Get a columns from the result set by name. + * + * @param name QueryResultSetColumn name + * @return The columns + */ + public <T> QueryResultSetColumn<T> getColumn(String name) { + int index = 0; + for (AbstractColumn col : this.getIterable()) { + if (col.getColumnName().equals(name)) { + return this.getColumn(index); + } + index++; + } + throw new ArrayIndexOutOfBoundsException("The columns is not present in the result set!"); + } + +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/AbstractStatementResult.java @@ -0,0 +1,64 @@ +/* + * 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; + +import java.io.Closeable; + +/** + * The base class for a query result. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public abstract class AbstractStatementResult implements Closeable { + + /** + * The corresponding connection of this result. + */ + private final MonetDBEmbeddedConnection connection; + + /** + * Pointer to the native result set. + * We need to keep it around for getting columns. + * The native result set is kept until the {@link super.close()} is called. + */ + protected long resultPointer; + + protected AbstractStatementResult(MonetDBEmbeddedConnection connection, long resultPointer) { + this.resultPointer = resultPointer; + this.connection = connection; + } + + /** + * Get the corresponding connection to this statement result + * + * @return A MonetDBEmbeddedConnection instance + */ + public MonetDBEmbeddedConnection getConnection() { return connection;} + + /** + * Tells if the connection of this statement result has been closed or not + * + * @return A boolean indicating if the statement result has been cleaned or not + */ + public boolean isStatementClosed() { + return this.resultPointer == 0; + } + + /** + * Close the query data so no more new results can be retrieved + */ + @Override + public void close() { + this.cleanupResult(this.resultPointer); + this.resultPointer = 0; + this.connection.removeQueryResult(this); + } + + private native void cleanupResult(long resultPointer); +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/EmbeddedPreparedStatement.java @@ -0,0 +1,426 @@ +/* + * 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; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.RoundingMode; +import java.net.InetAddress; +import java.net.URI; +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 simpler 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 List<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 ArrayList<>(); + 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(Byte[].class); + DirectMappingClasses.add(URI.class); + DirectMappingClasses.add(InetAddress.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] = "'" + valueToSubmit.replaceAll("\\\\", "\\\\\\\\") + .replaceAll("'", "\\\\'") + "'"; + } + } + + /** + * 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). + * + * @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 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 QueryResultSet sendQueryAsync() throws MonetDBEmbeddedException { + return this.connection.sendQueryAsync(this.applyParameters()); + } +}
--- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedConnection.java +++ b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedConnection.java @@ -3,146 +3,261 @@ * 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. + * Copyright 2016 MonetDB B.V. */ package nl.cwi.monetdb.embedded; -import nl.cwi.monetdb.embedded.column.Column; - -import java.io.*; -import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; /** * A single connection to a MonetDB database instance * Communication between Java and native C is done via JNI. * <br/> - * <strong>Note</strong>: You can have only one nl.cwi.monetdb.embedded MonetDB database running per JVM process. + * <strong>Note</strong>: You can have only one Embedded MonetDB database running per JVM process. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> */ public class MonetDBEmbeddedConnection { + private final MonetDBEmbeddedDatabase database; + private final long connectionPointer; - public MonetDBEmbeddedConnection(long connectionPointer) { - this.connectionPointer = connectionPointer; - } - - protected long getConnectionPointer() { - return connectionPointer; - } + private final List<AbstractStatementResult> results = new ArrayList<>(); - /** - * Execute an SQL query in an nl.cwi.monetdb.embedded database. - * - * @param query The SQL query string - * @return The query result object, {@code null} if the database is not running - * @throws SQLException - */ - public EmbeddedQueryResult createQuery(String query) throws SQLException { - String queryString = query; - if (!queryString.endsWith(";")) { - queryString += ";"; - } - 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(); + //TODO add autocommit + protected MonetDBEmbeddedConnection(MonetDBEmbeddedDatabase database, long connectionPointer) { + this.database = database; + this.connectionPointer = connectionPointer; } /** - * Rollbacks a transaction in nl.cwi.monetdb.embedded database. + * Gets the current schema set on the connection. * - * @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 + * @return A Java String with the name of the schema + * @throws MonetDBEmbeddedException If an error in the database occurred */ - 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; - } - } + public String getCurrentSchema() throws MonetDBEmbeddedException { + QueryResultSet eqr = this.sendQuery("select current_schema from sys.var();"); + QueryResultSetColumn<String> col = eqr.getColumn(0); + String res = col.fetchFirstNColumnValues(1)[0]; eqr.close(); return res; } /** - * Lists the table fields and types in nl.cwi.monetdb.embedded database. + * Sets the current schema on the connection. * - * @param tableName The name of the table - * @return - * @throws SQLException + * @param currentSchema Java String with the name of the schema + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public void setCurrentSchema(String currentSchema) throws MonetDBEmbeddedException { + String valueToSubmit = "'" + currentSchema.replaceAll("\\\\", "\\\\\\\\").replaceAll("'", "\\\\'") + "';"; + this.sendUpdate("SET SCHEMA " + valueToSubmit).close(); + } + + /** + * Begins a transaction. + * + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public void startTransaction() throws MonetDBEmbeddedException { + this.sendUpdate("START TRANSACTION;").close(); + } + + /** + * Commits the current transaction. + * + * @throws MonetDBEmbeddedException If an error in the database occurred */ - public String[] listFields(String tableName) throws SQLException { - if(!this.checkTableExists(tableName)) { - throw new SQLException("The table " + tableName + " doesn't exist!!"); + public void commit() throws MonetDBEmbeddedException { + this.sendUpdate("COMMIT;").close(); + } + + /** + * Rollbacks the current transaction. + * + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public void rollback() throws MonetDBEmbeddedException { + this.sendUpdate("ROLLBACK;").close(); + } + + /** + * Executes a SQL query without a result set. + * + * @param query The SQL query string + * @return The update result object + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public UpdateResultSet sendUpdate(String query) throws MonetDBEmbeddedException { + if (!query.endsWith(";")) { + query += ";"; } - 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(); + UpdateResultSet res = this.createEmptyResultSetInternal(this.connectionPointer, query, true); + results.add(res); return res; } - /** - * Execute an SQL query in an nl.cwi.monetdb.embedded database. + /** + * Executes a SQL query without a result set asynchronously. + * + * @param query The SQL query string + * @return The update result object + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public UpdateResultSet sendUpdateAsync(String query) throws MonetDBEmbeddedException { + /* CompletableFuture.supplyAsync(() -> this.sendUpdate(query)); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } + + /** + * Executes a SQL query with a result set. * * @param query The SQL query string - * @return The query result object, {@code null} if the database is not running - * @throws SQLException + * @return The query result object + * @throws MonetDBEmbeddedException If an error in the database occurred */ - private native EmbeddedQueryResult queryWrapper(String query, boolean execute, long connectionPointer) throws SQLException; + public QueryResultSet sendQuery(String query) throws MonetDBEmbeddedException { + if (!query.endsWith(";")) { + query += ";"; + } + QueryResultSet res = this.createNonEmptyResultSetInternal(this.connectionPointer, query, true); + results.add(res); + return res; + } + + /** + * Executes an SQL query with a result set asynchronously. + * + * @param query The SQL query string + * @return The query result object + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public QueryResultSet sendQueryAsync(String query) throws MonetDBEmbeddedException { + /* CompletableFuture.supplyAsync(() -> this.sendQuery(query)); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } + + /** + * Creates a prepared query statement likewise the PreparedStatement in JDBC. + * + * @param query The SQL query with ? indicating the parameters to replace in the query + * @return An instance of EmbeddedPreparedStatement + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public EmbeddedPreparedStatement createPreparedStatement(String query) throws MonetDBEmbeddedException { + if (!query.endsWith(";")) { + query += ";"; + } + return this.createPreparedStatementInternal(this.connectionPointer, query); + } + + /** + * Creates a prepared query statement likewise the PreparedStatement in JDBC asynchronously. + * + * @param query The SQL query with ? indicating the parameters to replace in the query + * @return An instance of EmbeddedPreparedStatement + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public EmbeddedPreparedStatement createPreparedStatementAsync(String query) throws MonetDBEmbeddedException { + /* CompletableFuture.supplyAsync(() -> this.createPreparedStatement(query)); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } + + /*public MonetDBTable getMonetDBTable(String schemaName, String tableName) throws MonetDBEmbeddedException { + MonetDBTable res = this.getMonetDBTableInternal(schemaName, tableName, this.connectionPointer); + //results.add(res); + return res; + } + add the method from current schema + public MonetDBTable getMonetDBTableAsync(String schema, String tableName) throws MonetDBEmbeddedException { + throw new UnsupportedOperationException("Must wait for Java 8 :("); + }*/ + /** + * Performs a listing of the existing tables with schemas. + * + * @param listSystemTables List system's tables as well (default true) + * @return The query result object, {@code null} if the database is not running + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public QueryResultSet listTables(boolean listSystemTables) throws MonetDBEmbeddedException { + 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.sendQuery(query + ";"); + } + + /** + * Check if a table it exists. + * + * @param schemaName The schema of the table + * @param tableName The name of the table + * @return If a the table exists or not + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public boolean checkIfTableExists(String schemaName, String tableName) throws MonetDBEmbeddedException { + String query = + "select schemas.name as sn, tables.name as tn from sys.tables join sys.schemas on tables.schema_id=schemas.id where tables.system=true order by sn, tn and schemas.name ='" + + schemaName + "' and tables.name ='" + tableName + "';"; + QueryResultSet eqr = this.sendQuery(query); + eqr.close(); + return eqr.getNumberOfRows() > 0; + } + + /** + * Deletes a table if it exists. + * + * @param schemaName The schema of the table + * @param tableName The name of the table + * @throws MonetDBEmbeddedException + */ + public void removeTable(String schemaName, String tableName) throws MonetDBEmbeddedException { + String query = "drop table " + schemaName + "." + tableName + ";"; + this.sendUpdate(query).close(); + } + + /** + * Shuts down this connection. Any pending queries connections will be immediately closed as well. + */ + public void shutdownConnection() { + for(AbstractStatementResult res : this.results) { + res.close(); + } + this.shutdownConnectionInternal(this.connectionPointer); + this.database.removeConnection(this); + } + + /** + * Shuts down this connection asynchronously. Any pending queries connections will be immediately closed as well. + */ + public void shutdownConnectionAsync() { + /* CompletableFuture.supplyAsync(() -> this.shutdownConnection()); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } + + /** + * Removes a query result from this connection. + */ + protected void removeQueryResult(AbstractStatementResult res) { + this.results.remove(res); + } + + private native UpdateResultSet createEmptyResultSetInternal(long connectionPointer, String query, boolean execute) + throws MonetDBEmbeddedException; + + private native QueryResultSet createNonEmptyResultSetInternal(long connectionPointer, String query, boolean execute) + throws MonetDBEmbeddedException; + + private native EmbeddedPreparedStatement createPreparedStatementInternal(long connectionPointer, String query) + throws MonetDBEmbeddedException; + + /*private native MonetDBTable getMonetDBTableInternal(long connectionPointer, String schemaName, String tableName) + throws MonetDBEmbeddedException;*/ + + private native void shutdownConnectionInternal(long connectionPointer); }
--- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedDatabase.java +++ b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedDatabase.java @@ -3,138 +3,195 @@ * 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. + * Copyright 2016 MonetDB B.V. */ package nl.cwi.monetdb.embedded; -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Path; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.locks.ReentrantReadWriteLock; /** - * Embedded version of MonetDB. + * An embedded version of a MonetDB database. * Communication between Java and native C is done via JNI. * <br/> - * <strong>Note</strong>: You can have only one nl.cwi.monetdb.embedded MonetDB database running per JVM process. + * <strong>Note</strong>: You can have only one Embedded MonetDB database running per JVM process. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> */ public class MonetDBEmbeddedDatabase { - 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!"); + /** + * 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 + * @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) + throws MonetDBEmbeddedException { + if(!MonetDBEmbeddedInstance.IsEmbeddedInstanceInitialized()) { + throw new MonetDBEmbeddedException("The embedded instance has not been loaded yet!"); } else { return StartDatabaseInternal(dbDirectory, silentFlag, sequentialFlag); } } - private static native MonetDBEmbeddedDatabase StartDatabaseInternal(String dbDirectory, boolean silentFlag, boolean sequentialFlag) throws SQLException; + /** + * Starts a MonetDB database on the given farm asynchronously. + * + * @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 + * @throws MonetDBEmbeddedException If the JNI library has not been loaded yet or an error in the database occurred + */ + public static MonetDBEmbeddedDatabase StartDatabaseAsync(String dbDirectory, boolean silentFlag, + boolean sequentialFlag) throws MonetDBEmbeddedException { + /* CompletableFuture.supplyAsync(() -> StartDatabase(dbDirectory, silentFlag, sequentialFlag); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } - private final File databaseDirectory; + private final String databaseDirectory; private final boolean silentFlag; private final boolean sequentialFlag; - private boolean isRunning; + private boolean isRunning = true; private final List<MonetDBEmbeddedConnection> connections = new ArrayList<>(); - private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - - public MonetDBEmbeddedDatabase(String dbDirectory, boolean silentFlag, boolean sequentialFlag, boolean isRunning) { - this.databaseDirectory = new File(dbDirectory); + private MonetDBEmbeddedDatabase(String dbDirectory, boolean silentFlag, boolean sequentialFlag) { + this.databaseDirectory = dbDirectory; this.silentFlag = silentFlag; this.sequentialFlag = sequentialFlag; - this.isRunning = isRunning; } - public File getDatabaseDirectory() { + /** + * 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; } - public boolean isRunning() { - boolean result; - lock.readLock().lock(); - result = isRunning; - lock.readLock().unlock(); - return result; + /** + * 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.shutdownConnection(); + } + this.connections.clear(); + this.stopDatabaseInternal(); + this.isRunning = false; + } else { + throw new MonetDBEmbeddedException("The database is not running!"); + } } - private void setRunning(boolean running) { - lock.writeLock().lock(); - isRunning = running; - lock.writeLock().unlock(); + /** + * 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 void stopDatabaseAsync() throws MonetDBEmbeddedException { + /* CompletableFuture.supplyAsync(() -> this.stopDatabase()); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } + + /** + * 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.createConnectionOnSchema(null); } - public void stopDatabase() throws SQLException { - lock.writeLock().lock(); - try { - if(this.isRunning) { - for(MonetDBEmbeddedConnection mdbec : connections) { - this.shutdownConnection(mdbec); - } - this.connections.clear(); - this.stopDatabaseInternal(); - this.isRunning = false; - } else { - throw new SQLException("The database is not running!"); - } - } catch (SQLException ex) { - lock.writeLock().unlock(); - throw ex; + /** + * Creates a connection on the database, set on the default schema asynchronously. + * + * @return A MonetDBEmbeddedConnection instance + * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred + */ + public MonetDBEmbeddedConnection createConnectionAsync() throws MonetDBEmbeddedException { + return this.createConnectionOnSchemaAsync(null); + } + + /** + * Creates a connection on the database on the given schema. + * + * @param schema A String with the schema to be set + * @return A MonetDBEmbeddedConnection instance + * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred + */ + public MonetDBEmbeddedConnection createConnectionOnSchema(String schema) throws MonetDBEmbeddedException { + if(this.isRunning) { + MonetDBEmbeddedConnection mdbec = this.createConnectionInternal(schema); + connections.add(mdbec); + return mdbec; + } else { + throw new MonetDBEmbeddedException("The database is not running!"); } - lock.writeLock().unlock(); } + /** + * Creates a connection on the database on the given schema asynchronously. + * + * @param schema A String with the schema to be set + * @return A MonetDBEmbeddedConnection instance + * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred + */ + public MonetDBEmbeddedConnection createConnectionOnSchemaAsync(String schema) throws MonetDBEmbeddedException { + /* CompletableFuture.supplyAsync(() -> this.createConnectionOnSchema(schema)); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } + + /** + * Removes a connection from this database. + */ + protected void removeConnection(MonetDBEmbeddedConnection con) { + this.connections.remove(con); + } + + private static native MonetDBEmbeddedDatabase StartDatabaseInternal(String dbDirectory, boolean silentFlag, + boolean sequentialFlag) throws MonetDBEmbeddedException; + private native void stopDatabaseInternal(); - public MonetDBEmbeddedConnection createConnection() throws SQLException { - MonetDBEmbeddedConnection mdbec; - lock.writeLock().lock(); - try { - if(this.isRunning) { - mdbec = this.createConnectionInternal(); - connections.add(mdbec); - lock.writeLock().unlock(); - return mdbec; - } else { - throw new SQLException("The database is not running!"); - } - } catch (SQLException ex) { - lock.writeLock().unlock(); - throw ex; - } - } - - private native MonetDBEmbeddedConnection createConnectionInternal() throws SQLException; - - public void shutdownConnection(MonetDBEmbeddedConnection mdbec) throws SQLException { - lock.writeLock().lock(); - try { - if(this.isRunning) { - this.shutdownConnectionInternal(mdbec.getConnectionPointer()); - this.connections.remove(mdbec); - } else { - throw new SQLException("The database is not running!"); - } - } catch (SQLException ex) { - lock.writeLock().unlock(); - throw ex; - } - } - - private native void shutdownConnectionInternal(long connectionPointer); + private native MonetDBEmbeddedConnection createConnectionInternal(String schema) throws MonetDBEmbeddedException; }
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedException.java @@ -0,0 +1,21 @@ +/* + * 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; + +/** + * The exception fired from embedded methods. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public class MonetDBEmbeddedException extends Exception { + + public MonetDBEmbeddedException(String message) { + super(message); + } +}
--- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java +++ b/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java @@ -1,7 +1,19 @@ +/* + * 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; /** - * Created by ferreira on 10/27/16. + * 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 { @@ -9,8 +21,14 @@ public class MonetDBEmbeddedInstance { 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 == false) { + if(!isEmbeddedInstanceInitialized) { if(libraryName == null) { libraryName = NATIVE_LIB_NAME; } @@ -20,10 +38,16 @@ public class MonetDBEmbeddedInstance { 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 == false) { + if(!isEmbeddedInstanceInitialized) { if(libraryPath == null) { - return false; + System.load(NATIVE_LIB_NAME); } System.load(libraryPath); isEmbeddedInstanceInitialized = true; @@ -31,6 +55,11 @@ public class MonetDBEmbeddedInstance { 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; }
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/MonetDBToJavaMapping.java @@ -0,0 +1,96 @@ +/* + * 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; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.InetAddress; +import java.net.URI; +import java.sql.Date; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.HashMap; +import java.util.UUID; + +/** + * A Java enum representing the mappings between MonetDB data types and Java classes. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public enum MonetDBToJavaMapping { + + Boolean(Boolean.class), Char(String.class), Varchar(String.class), Clob(String.class), Tinyint(Byte.class), + Smallint(Short.class), Int(Integer.class), Bigint(Long.class), Hugeint(BigInteger.class), Decimal(BigDecimal.class), + Real(Float.class), Double(Double.class), MonthInterval(Integer.class), SecondInterval(Long.class), Time(Time.class), + TimeTz(Time.class), Date(Date.class), Timestamp(Timestamp.class), TimestampTz(Timestamp.class), Blob(Byte[].class), + Geometry(Byte[].class), GeometryA(Byte[].class), URL(URI.class), Inet(InetAddress.class), JSON(Byte[].class), + UUID(UUID.class); + + /** + * The mapping between MonetDB data types and enum values. + */ + private static final HashMap<String, MonetDBToJavaMapping> MonetDBMappings; + + static { + MonetDBMappings = new HashMap<>(); + MonetDBMappings.put("boolean", Boolean); + MonetDBMappings.put("char", Char); + MonetDBMappings.put("varchar", Varchar); + MonetDBMappings.put("clob", Clob); + MonetDBMappings.put("tinyint", Tinyint); + MonetDBMappings.put("smallint", Smallint); + MonetDBMappings.put("int", Int); + MonetDBMappings.put("bigint", Bigint); + MonetDBMappings.put("hugeint", Hugeint); + MonetDBMappings.put("decimal", Decimal); + MonetDBMappings.put("real", Real); + MonetDBMappings.put("double", Double); + MonetDBMappings.put("month_interval", MonthInterval); + MonetDBMappings.put("sec_interval", SecondInterval); + MonetDBMappings.put("time", Time); + MonetDBMappings.put("timetz", TimeTz); + MonetDBMappings.put("date", Date); + MonetDBMappings.put("timestamp", Timestamp); + MonetDBMappings.put("timestamptz", TimestampTz); + MonetDBMappings.put("blob", Blob); + MonetDBMappings.put("geometry", Geometry); + MonetDBMappings.put("geometrya", GeometryA); + MonetDBMappings.put("url", URL); + MonetDBMappings.put("inet", Inet); + MonetDBMappings.put("json", JSON); + MonetDBMappings.put("uuid", UUID); + } + + /** + * Get the corresponding MonetDBToJavaMapping from MonetDB internal data type. + * + * @return A MonetDBToJavaMapping enum value + */ + public static MonetDBToJavaMapping GetJavaMappingFromMonetDBString(String sqlname) { + return MonetDBMappings.get(sqlname); + } + + /** + * The corresponding Java class for the enum value. + */ + private final Class<?> javaClass; + + MonetDBToJavaMapping(Class<?> javaClass) { this.javaClass = javaClass;} + + /** + * Gets the corresponding Java class for the enum value. + * + * @return The corresponding Java class for the enum value + */ + @SuppressWarnings("unchecked") + public <T> Class<T> getJavaClass() { + return (Class<T>) this.javaClass; + } + +}
rename from src/main/java/nl/cwi/monetdb/embedded/EmbeddedQueryResult.java rename to src/main/java/nl/cwi/monetdb/embedded/QueryResultSet.java --- a/src/main/java/nl/cwi/monetdb/embedded/EmbeddedQueryResult.java +++ b/src/main/java/nl/cwi/monetdb/embedded/QueryResultSet.java @@ -3,16 +3,13 @@ * 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. + * Copyright 2016 MonetDB B.V. */ package nl.cwi.monetdb.embedded; -import java.io.Closeable; -import java.sql.SQLException; -import java.util.Iterator; - -import nl.cwi.monetdb.embedded.column.Column; +import java.util.Arrays; +import java.util.ListIterator; /** * Embedded MonetDB query result. @@ -20,191 +17,137 @@ import nl.cwi.monetdb.embedded.column.Co * Instead, they are kept around at MonetDB native C-level, materialised in Java * on demand and freed on {@code super.close()}. * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> */ -public class EmbeddedQueryResult implements Closeable, Iterable<Column<?>> { - /** - * The names of the columns in the query result. - */ - protected final String[] columnNames; - /** - * The types of the columns in the query result. - */ - protected final String[] columnTypes; - /** - * The sizes of the columns in the query result. - */ - protected final int[] columnSizes; - /** - * The number of columns in the query result. - */ - protected final int numberOfColumns; - /** - * The number of rows in the query result. - */ - protected final int numberOfRows; - /** - * Pointer to the native result set. - * We need to keep it around for getting columns. - * The native result set is kept until the {@link super.close()} is called. - */ - protected long resultPointer; - /** - * To avoid reconstructing the columns a second time, we will use this cache - */ - protected final Column<?>[] columnsCache; - - public EmbeddedQueryResult(String[] columnNames, String[] columnTypes, int[] columnSizes, int numberOfColumns, int numberOfRows, long resultPointer) { - this.columnNames = columnNames; - this.columnTypes = columnTypes; - this.columnSizes = columnSizes; - this.numberOfColumns = numberOfColumns; - this.numberOfRows = numberOfRows; - this.resultPointer = resultPointer; - this.columnsCache = new Column<?>[numberOfColumns]; - } +public class QueryResultSet extends AbstractQueryResultSet { /** - * Get the column names as a string array. - * - * @return The column names array + * The query result set columns listing */ - public String[] getColumnNames() { - return columnNames; - } + private final QueryResultSetColumn<?>[] columns; - /** - * Get the column types as a string array. - * - * @return The column types array - */ - public String[] getColumnTypes() { - return columnTypes; + protected QueryResultSet(MonetDBEmbeddedConnection connection, long resultPointer, + QueryResultSetColumn<?>[] columns, int numberOfRows) { + super(connection, resultPointer, columns.length, numberOfRows); + this.columns = columns; + this.resultPointer = resultPointer; } /** - * Get the column sizes as a int array. + * Get the query set column values as an Iterable. * - * @return The column sizes array + * @return An Iterable over the columns */ - public int[] getColumnSizes() { - return columnSizes; + @Override + protected Iterable<AbstractColumn<?>> getIterable() { + return Arrays.asList(columns); + } + + /** + * Get a columns' values from the result set by index. + * + * @param index QueryResultSetColumn index (starting from 0) + * @return The columns, {@code null} if index not in bounds + */ + @Override + @SuppressWarnings("unchecked") + public <T> QueryResultSetColumn<T> getColumn(int index) { + return (QueryResultSetColumn<T>) columns[index]; } - /** - * Returns the number of columns in the result set. - * - * @return Number of columns - */ - public int getNumberOfColumns() { - return numberOfColumns; - } - - /** - * Returns the number of rows in the result set. - * - * @return Number of rows - */ - public int getNumberOfRows() { - return numberOfRows; - } - - /** - * Get a column from the result set by index. - * - * @param index Column index (starting from 0) - * @return The column, {@code null} if index not in bounds - */ - public Column<?> getColumn(int index) throws SQLException { - if (index < 0) { - throw new ArrayIndexOutOfBoundsException("The index must be larger than 0!"); - } else if (index >= this.numberOfColumns) { - throw new ArrayIndexOutOfBoundsException("The index must be smaller than the number of columns"); + /** + * Fetches rows from the result set. + * + * @param startIndex The first row index to retrieve + * @param endIndex The last row index to retrieve + * @return The rows as {@code QueryRowsResultSet} + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public QueryRowsResultSet fetchResultSetRows(int startIndex, int endIndex) throws MonetDBEmbeddedException { + if(endIndex < startIndex) { + int aux = startIndex; + startIndex = endIndex; + endIndex = aux; + } + if (startIndex < 0) { + throw new ArrayIndexOutOfBoundsException("The start index must be larger than 0!"); + } else if (endIndex > this.numberOfRows) { + throw new ArrayIndexOutOfBoundsException("The index must be smaller than the number of elements in the columns!"); + } else if(startIndex == endIndex) { + throw new ArrayIndexOutOfBoundsException("Retrieving 0 rows?"); + } + int numberOfRowsToRetrieve = endIndex - startIndex; + Object[][] temp = new Object[numberOfRowsToRetrieve][this.numberOfColumns]; + for (int i = 0 ; i < this.numberOfColumns; i++) { + Object[] nextColumn = this.columns[i].fetchColumnValues(startIndex, endIndex); + for(int j = 0; j < numberOfRowsToRetrieve ; j++) { + temp[j][i] = nextColumn[j]; + } } - if(this.columnsCache[index] != null) { - return this.columnsCache[index]; - } - if (this.resultPointer == 0) { - // The object was closed and result was cleaned-up. Calling the can produce a native Segfault (and crash the JVM) - throw new NullPointerException("The result set has been already cleaned!"); - } - Column<?> result = this.getColumnWrapper(index, this.resultPointer); - this.columnsCache[index] = result; - return result; + return new QueryRowsResultSet(this, this.getMappings(), temp); } - /** - * Get a column from the result set by name. - * - * @param name Column name - * @return The column, {@code null} if not found - */ - public Column<?> getColumn(String name) throws SQLException { - int index = 0; - for (String columnName : this.columnNames) { - if (name.equals(columnName)) { - return this.getColumn(index); - } - index++; - } - throw new ArrayIndexOutOfBoundsException("The column is not present in the result set!"); - } + /** + * Fetches rows from the result set asynchronously. + * + * @param startIndex The first row index to retrieve + * @param endIndex The last row index to retrieve + * @return The rows as {@code QueryRowsResultSet} + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public QueryRowsResultSet fetchResultSetRowsAsync(int startIndex, int endIndex) throws MonetDBEmbeddedException { + /* CompletableFuture.supplyAsync(() -> this.fetchResultSetRows(startIndex, endIndex)); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } - /** - * A native C function that returns a {@code Column} object. - * - * @param index Column index (starting from 0) - * @return - */ - private native Column<?> getColumnWrapper(int index, long resultPointer) throws SQLException; - - @Override - public Iterator<Column<?>> iterator() { - return new Iterator<Column<?>>() { - private int currentIndex = 0; - - @Override - public boolean hasNext() { - return (currentIndex < getNumberOfColumns()); - } + /** + * Fetches the first N rows from the result set. + * + * @param n The last row index to retrieve + * @return The rows as {@code QueryRowsResultSet} + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public QueryRowsResultSet fetchFirstNRowValues(int n) throws MonetDBEmbeddedException { + return this.fetchResultSetRows(0, n); + } - @Override - public Column<?> next() { - try { - return getColumn(currentIndex++); - } catch (SQLException ex) { - return null; - } - } - }; - } + /** + * Fetches the first N rows from the result set asynchronously. + * + * @param n The last row index to retrieve + * @return The rows as {@code QueryRowsResultSet} + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public QueryRowsResultSet fetchFirstNRowValuesAsync(int n) throws MonetDBEmbeddedException { + return this.fetchResultSetRowsAsync(0, n); + } - /** - * Get a matrix of objects representing the rows and columns of the query - * - * @return The rows as {@code Object[][]} - */ - public Object[][] getRows() throws SQLException { - Object[][] result = new Object[this.numberOfRows][this.numberOfColumns]; - Column<?> column; - for (int i = 0 ; i < this.numberOfColumns; i++) { - column = this.getColumn(i); - for (int j = 0 ; j < this.numberOfRows; j++) { - result[j][i] = column.getValue(i); - } - } - return result; - } + /** + * Fetches all rows from the result set. + * + * @return The rows as {@code QueryRowsResultSet} + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public QueryRowsResultSet fetchAllRowValues() throws MonetDBEmbeddedException { + return this.fetchResultSetRows(0, this.numberOfRows); + } - @Override - public void close() { - if(this.resultPointer > 0) { - this.cleanupResult(this.resultPointer); - this.resultPointer = 0; - } - } + /** + * Fetches all rows from the result set asynchronously. + * + * @return The rows as {@code QueryRowsResultSet} + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public QueryRowsResultSet fetchAllRowValuesAsync() throws MonetDBEmbeddedException { + return this.fetchResultSetRowsAsync(0, this.numberOfRows); + } - /** - * Free the C-level result structure. - */ - private native void cleanupResult(long resultPointer); + @Override + public ListIterator<QueryRowsResultSet.QueryResulSetSingleRow> iterator() { + try { + return Arrays.asList(this.fetchAllRowValues().getAllRows()).listIterator(); + } catch (MonetDBEmbeddedException ex) { + return null; + } + } }
rename from src/main/java/nl/cwi/monetdb/embedded/column/Column.java rename to src/main/java/nl/cwi/monetdb/embedded/QueryResultSetColumn.java --- a/src/main/java/nl/cwi/monetdb/embedded/column/Column.java +++ b/src/main/java/nl/cwi/monetdb/embedded/QueryResultSetColumn.java @@ -3,118 +3,233 @@ * 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. + * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded.column; +package nl.cwi.monetdb.embedded; -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -import java.util.Iterator; +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.ListIterator; /** * Am abstract class for accessing, * materialised (Java-level) query result columns. * - * @param <T> A primitive or String type + * @param <T> A Java class mapped to a MonetDB data type + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> */ -public abstract class Column<T> implements Iterable<T> { - - /** - * The name of the column in the query result - */ - protected final String columnName; +public class QueryResultSetColumn<T> extends AbstractColumn<T> { - /** - * The type of the column in the query result - */ - protected final String columnType; - - /** - * The size/length of the column. - */ - protected final int columnSize; + /** + * Array with the retrieved values. + */ + private final T[] values; /** - * Array with null values; + * The index of the first value mapped to a Java class. */ - protected final boolean[] nullIndex; + private int firstRetrievedIndex; - public Column(EmbeddedQueryResult result, int index, boolean[] nullIndex) { - this.columnName = result.getColumnNames()[index]; - this.columnType = result.getColumnTypes()[index]; - this.columnSize = result.getColumnSizes()[index]; - this.nullIndex = nullIndex; + /** + * The index of the last value mapped to a Java class. + */ + private int lastRetrievedIndex; + + @SuppressWarnings("unchecked") + protected QueryResultSetColumn(long resultSetPointer, int resultSetIndex, int numberOfRows, String columnName, + String columnType, int columnDigits, int columnScale) { + super(resultSetPointer, resultSetIndex, numberOfRows, columnName, columnType, columnDigits, columnScale); + this.firstRetrievedIndex = numberOfRows; + this.lastRetrievedIndex = 0; + this.values = (T[]) Array.newInstance(this.mapping.getJavaClass(), numberOfRows); } - /** - * Get the name of the column. - * - * @return Column name - */ - public String getColumnName() { - return columnName; - } - - /** - * Get the type of the column. - * - * @return Column type - */ - public String getColumnType() { return columnType; } - - /** - * Get the size of the column. - * - * @return Column size - */ - public int getColumnSize() { return columnSize; } + /** + * Maps columns values into the Java representation. + * + * @param startIndex The first column index to retrieve + * @param endIndex The last column index to retrieve + * @param javaClass The Java class to map + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + @SuppressWarnings("unchecked") + public T[] fetchColumnValues(int startIndex, int endIndex, Class<T> javaClass) throws MonetDBEmbeddedException { + if(endIndex < startIndex) { + int aux = startIndex; + startIndex = endIndex; + endIndex = aux; + } + int numberOfRowsToRetrieve = endIndex - startIndex; + if (startIndex < 0) { + throw new ArrayIndexOutOfBoundsException("The start index must be larger than 0!"); + } else if (endIndex > this.numberOfRows) { + throw new ArrayIndexOutOfBoundsException("The index must be smaller than the number of elements in the columns!"); + } else if(startIndex == endIndex) { + throw new ArrayIndexOutOfBoundsException("Retrieving 0 values?"); + } + if(startIndex < this.firstRetrievedIndex) { + if(this.resultSetPointer == 0) { + throw new MonetDBEmbeddedException("Connection closed!"); + } + if(startIndex < this.firstRetrievedIndex) { + T[] new_start_batch = this.getValuesBatch(this.resultSetPointer, this.resultSetIndex, + this.mapping.ordinal(), startIndex, this.firstRetrievedIndex); + System.arraycopy(new_start_batch, 0, this.values, startIndex, new_start_batch.length); + this.firstRetrievedIndex = startIndex; + } + } + if(endIndex > this.lastRetrievedIndex) { + if(this.resultSetPointer == 0) { + throw new MonetDBEmbeddedException("Connection closed!"); + } + if(endIndex > this.lastRetrievedIndex) { + T[] new_end_batch = this.getValuesBatch(this.resultSetPointer, this.resultSetIndex, + this.mapping.ordinal(), this.lastRetrievedIndex, endIndex); + System.arraycopy(new_end_batch, 0, this.values, this.lastRetrievedIndex, new_end_batch.length); + this.lastRetrievedIndex = endIndex; + } + } + T[] result = (T[]) Array.newInstance(javaClass, numberOfRowsToRetrieve); + System.arraycopy(this.values, startIndex, result, 0, numberOfRowsToRetrieve); + return result; + } /** - * Get the array mapping of null values + * Maps columns values into the Java representation asynchronously. + * + * @param startIndex The first column index to retrieve + * @param endIndex The last column index to retrieve + * @param javaClass The Java class to map + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public T[] fetchColumnValuesAsync(int startIndex, int endIndex, Class<T> javaClass) throws MonetDBEmbeddedException { + /* CompletableFuture.supplyAsync(() -> this.fetchColumnValues(startIndex, endIndex, javaClass)); */ + throw new UnsupportedOperationException("Must wait for Java 8 :("); + } + + /** + * Maps the first N column values. * - * @return Null values + * @param n The last column index to map + * @param javaClass The Java class to map + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred */ - public boolean[] getNullindex() { return nullIndex; } + public T[] fetchFirstNColumnValues(int n, Class<T> javaClass) throws MonetDBEmbeddedException { + return this.fetchColumnValues(0, n, javaClass); + } + + /** + * Maps the first N column values asynchronously. + * + * @param n The last column index to map + * @param javaClass The Java class to map + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public T[] fetchFirstNColumnValuesAsync(int n, Class<T> javaClass) throws MonetDBEmbeddedException { + return this.fetchColumnValuesAsync(0, n, javaClass); + } /** - * Get a (non-primary-type) value at index of a column. - * - * @param index Column index for the value - * @return Value, cloud be {@code null} - */ - public T getValue(int index) { - if (index < 0) { - throw new ArrayIndexOutOfBoundsException("The index must be larger than 0!"); - } else if (index >= this.columnSize) { - throw new ArrayIndexOutOfBoundsException("The index must be smaller than the number of elements in the column!"); - } - return this.getValueImplementation(index); - } + * Maps all column values. + * + * @param javaClass The Java class to map + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public T[] fetchAllColumnValues(Class<T> javaClass) throws MonetDBEmbeddedException { + return this.fetchColumnValues(0, this.numberOfRows, javaClass); + } + + /** + * Maps all column values asynchronously. + * + * @param javaClass The Java class to map + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public T[] fetchAllColumnValuesAsync(Class<T> javaClass) throws MonetDBEmbeddedException { + return this.fetchColumnValuesAsync(0, this.numberOfRows, javaClass); + } + + /** + * Maps columns values using the provided Java representation by the query. + * + * @param startIndex The first column index to retrieve + * @param endIndex The last column index to retrieve + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + @SuppressWarnings("unchecked") + public T[] fetchColumnValues(int startIndex, int endIndex) throws MonetDBEmbeddedException { + return this.fetchColumnValues(startIndex, endIndex, this.mapping.getJavaClass()); + } + + /** + * Maps columns values using the provided Java representation by the query asynchronously. + * + * @param startIndex The first column index to retrieve + * @param endIndex The last column index to retrieve + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + @SuppressWarnings("unchecked") + public T[] fetchColumnValuesAsync(int startIndex, int endIndex) throws MonetDBEmbeddedException { + return this.fetchColumnValuesAsync(startIndex, endIndex, this.mapping.getJavaClass()); + } - /** - * Get all values of the column - * - * @return All values of the column - */ - public abstract T[] getAllValues(); + /** + * Maps the first N column values using the provided Java representation by the query. + * + * @param n The last column index to map + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public T[] fetchFirstNColumnValues(int n) throws MonetDBEmbeddedException { + return this.fetchColumnValues(0, n); + } - protected abstract T getValueImplementation(int index); + /** + * Maps the first N column values using the provided Java representation by the query asynchronously. + * + * @param n The last column index to map + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public T[] fetchFirstNColumnValuesAsync(int n) throws MonetDBEmbeddedException { + return this.fetchColumnValuesAsync(0, n); + } - @Override - public Iterator<T> iterator() { - return new Iterator<T>() { - private int currentIndex = 0; + /** + * Maps all column values using the provided Java representation by the query. + * + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public T[] fetchAllColumnValues() throws MonetDBEmbeddedException { + return this.fetchColumnValues(0, this.numberOfRows); + } - @Override - public boolean hasNext() { - return (currentIndex < getColumnSize()); - } + /** + * Maps all column values using the provided Java representation by the query asynchronously. + * + * @return The column values as a Java array + * @throws MonetDBEmbeddedException If an error in the database occurred + */ + public T[] fetchAllColumnValuesAsync() throws MonetDBEmbeddedException { + return this.fetchColumnValuesAsync(0, this.numberOfRows); + } - @Override - public T next() { - return getValue(currentIndex++); - } - }; - } + @Override + public ListIterator<T> iterator() { + return Arrays.asList(this.values).listIterator(); + } + + private native T[] getValuesBatch(long resultPointer, int resultSetIndex, int mappingType, int first, int last) + throws MonetDBEmbeddedException; + }
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/QueryRowsResultSet.java @@ -0,0 +1,226 @@ +/* + * 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; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.ListIterator; + +/** + * A row set retrieved from an embedded MonetDB query result. All the values in this set are already mapped + * to Java classes a priori. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public class QueryRowsResultSet implements Iterable { + + /** + * A single row in a result set. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ + static public class QueryResulSetSingleRow implements Iterable { + + /** + * The original row result set from this row. + */ + private final QueryRowsResultSet resultSet; + + /** + * The columns values as Java objects. + */ + private final Object[] columns; + + protected QueryResulSetSingleRow(QueryRowsResultSet resultSet, Object[] columns) { + this.resultSet = resultSet; + this.columns = columns; + } + + /** + * Gets the original row result set from this row. + * + * @return The original row result set from this row + */ + public QueryRowsResultSet getRowResultSet() { return resultSet; } + + /** + * Gets the columns values as Java objects. + * + * @return The columns values as Java objects + */ + public Object[] getAllColumns() { + return columns; + } + + /** + * Gets the number of columns. + * + * @return The number of columns + */ + public int getNumberOfColumns() { return columns.length; } + + /** + * Gets a column value as a Java class. + * + * @param <T> A Java class mapped to a MonetDB data type + * @param index The index of the column + * @param javaClass The Java class + * @return The column value as a Java class + */ + public <T> T getColumn(int index, Class<T> javaClass) { + return javaClass.cast(columns[index]); + } + + /** + * Gets a column value as a Java class using the default mapping. + * + * @param <T> A Java class mapped to a MonetDB data type + * @param index The index of the column + * @return The column value as a Java class + */ + public <T> T getColumn(int index) { + Class<T> javaClass = this.resultSet.mappings[index].getJavaClass(); + return javaClass.cast(columns[index]); + } + + @Override + public ListIterator<Object> iterator() { + return Arrays.asList(this.columns).listIterator(); + } + } + + /** + * The original query result set this row set belongs. + */ + private final AbstractQueryResultSet queryResultSet; + + /** + * The MonetDB-To-Java mappings of the columns. + */ + private final MonetDBToJavaMapping[] mappings; + + /** + * The rows of this set. + */ + private final QueryResulSetSingleRow[] rows; + + protected QueryRowsResultSet(AbstractQueryResultSet queryResultSet, MonetDBToJavaMapping[] mappings, + Object[][] rows) { + this.queryResultSet = queryResultSet; + this.mappings = mappings; + this.rows = new QueryResulSetSingleRow[mappings.length]; + for(int i = 0 ; i < mappings.length ; i++) { + this.rows[i] = new QueryResulSetSingleRow(this, rows[i]); + } + } + + /** + * Gets the original query result set this row set belongs. + * + * @return The original query result set this row set belongs + */ + public AbstractQueryResultSet getQueryResultSet() { + return queryResultSet; + } + + /** + * Gets all rows of this set. + * + * @return All rows of this set + */ + public QueryResulSetSingleRow[] getAllRows() { return rows; } + + /** + * Gets the number of rows in this set. + * + * @return The number of rows in this set + */ + public int getNumberOfRows() { return rows.length; } + + /** + * Gets the number of columns in this set. + * + * @return The number of columns in this set + */ + public int getNumberOfColumns() { return mappings.length; } + + /** + * Gets a single row in this set. + * + * @param row The index of the row to retrieve + * @return A single row in this set + */ + public QueryResulSetSingleRow getSingleRow(int row) { + return rows[row]; + } + + /** + * Gets a single value in this set as a Java class. + * + * @param <T> A Java class mapped to a MonetDB data type + * @param row The index of the row to retrieve + * @param column The index of the column to retrieve + * @param javaClass The Java class to map + * @return The value mapped to a instance of the provided class + */ + public <T> T getSingleValue(int row, int column, Class<T> javaClass) { + return javaClass.cast(this.rows[row].getColumn(column)); + } + + /** + * Gets a single value in this set as a Java class using the default mapping. + * + * @param <T> A Java class mapped to a MonetDB data type + * @param row The index of the row to retrieve + * @param column The index of the column to retrieve + * @return The value mapped to a instance of the provided class + */ + public <T> T getSingleValue(int row, int column) { + Class<T> javaClass = this.mappings[column].getJavaClass(); + return javaClass.cast(this.rows[row].getColumn(column)); + } + + /** + * Gets a column in this set as a Java class. + * + * @param <T> A Java class mapped to a MonetDB data type + * @param column The index of the column to retrieve + * @param javaClass The Java class + * @return The value mapped to a instance of the provided class + */ + @SuppressWarnings("unchecked") + public <T> T[] getColumn(int column, Class<T> javaClass) { + T[] res = (T[]) Array.newInstance(javaClass, this.rows.length); + for(int i = 0 ; i < this.rows.length ; i++) { + res[i] = this.rows[i].getColumn(column); + } + return res; + } + + /** + * Gets a column in this set as a Java class using the default mapping. + * + * @param <T> A Java class mapped to a MonetDB data type + * @param column The index of the column to retrieve + * @return The value mapped to a instance of the provided class + */ + @SuppressWarnings("unchecked") + public <T> T[] getColumn(int column) { + T[] res = (T[]) Array.newInstance(this.mappings[column].getJavaClass(), this.rows.length); + for(int i = 0 ; i < this.rows.length ; i++) { + res[i] = this.rows[i].getColumn(column); + } + return res; + } + + @Override + public ListIterator<QueryResulSetSingleRow> iterator() { + return Arrays.asList(this.rows).listIterator(); + } +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/UpdateResultSet.java @@ -0,0 +1,21 @@ +/* + * 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; + +/** + * The result set from a sendUpdate method from a connection. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public class UpdateResultSet extends AbstractStatementResult { + + protected UpdateResultSet(MonetDBEmbeddedConnection connection, long resultPointer) { + super(connection, resultPointer); + } +}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/BigintColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB BIGINT data type - */ -public class BigintColumn extends Column<Long> { - - private final Long[] values; - - public BigintColumn(EmbeddedQueryResult result, int index, long[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Long[] newArray = new Long[values.length]; - int j = newArray.length; - for(int i = 0 ; i < j ; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Long.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Long[] getAllValues() { - return this.values; - } - - @Override - protected Long getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/BlobColumn.java +++ /dev/null @@ -1,35 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.types.MonetDBEmbeddedBlob; -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB BLOB data type - */ -public class BlobColumn extends Column<MonetDBEmbeddedBlob> { - - private final MonetDBEmbeddedBlob[] values; - - public BlobColumn(EmbeddedQueryResult result, int index, MonetDBEmbeddedBlob[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public MonetDBEmbeddedBlob[] getAllValues() { - return this.values; - } - - @Override - protected MonetDBEmbeddedBlob getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/BooleanColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB BOOLEAN data type - */ -public class BooleanColumn extends Column<Boolean> { - - private final Boolean[] values; - - public BooleanColumn(EmbeddedQueryResult result, int index, boolean[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Boolean[] newArray = new Boolean[values.length]; - int j = newArray.length; - for(int i = 0 ; i < j ; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Boolean.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Boolean[] getAllValues() { - return this.values; - } - - @Override - protected Boolean getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/CharColumn.java +++ /dev/null @@ -1,34 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB CHAR data type - */ -public class CharColumn extends Column<String> { - - private final String[] values; - - public CharColumn(EmbeddedQueryResult result, int index, String[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public String[] getAllValues() { - return this.values; - } - - @Override - protected String getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/ClobColumn.java +++ /dev/null @@ -1,35 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.jdbc.MonetClob; -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB CLOB data type - */ -public class ClobColumn extends Column<String> { - - private final String[] values; - - public ClobColumn(EmbeddedQueryResult result, int index, String[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public String[] getAllValues() { - return this.values; - } - - @Override - protected String getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/DateColumn.java +++ /dev/null @@ -1,36 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -import java.sql.Date; - -/** - * Mapping for MonetDB DATE data type - */ -public class DateColumn extends Column<Date> { - - private final Date[] values; - - public DateColumn(EmbeddedQueryResult result, int index, Date[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public Date[] getAllValues() { - return this.values; - } - - @Override - protected Date getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/DecimalColumn.java +++ /dev/null @@ -1,50 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -import java.math.BigDecimal; - -/** - * Mapping for MonetDB DECIMAL data type - */ -public class DecimalColumn extends Column<BigDecimal> { - - private final BigDecimal[] values; - - private final int precision; - - private final int scale; - - public DecimalColumn(EmbeddedQueryResult result, int index, BigDecimal[] values, boolean[] nullIndex, int precision, int scale) { - super(result, index, nullIndex); - this.values = values; - this.precision = precision; - this.scale = scale; - } - - public int getPrecision() { - return precision; - } - - public int getScale() { - return scale; - } - - @Override - public BigDecimal[] getAllValues() { - return this.values; - } - - @Override - protected BigDecimal getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/DoubleColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB FLOAT data type - */ -public class DoubleColumn extends Column<Double> { - - private final Double[] values; - - public DoubleColumn(EmbeddedQueryResult result, int index, double[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Double[] newArray = new Double[values.length]; - int j = newArray.length; - for(int i = 0 ; i < j ; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Double.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Double[] getAllValues() { - return this.values; - } - - @Override - protected Double getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/GeometryColumn.java +++ /dev/null @@ -1,34 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB GEOMETRY data type - */ -public class GeometryColumn extends Column<String> { - - private final String[] values; - - public GeometryColumn(EmbeddedQueryResult result, int index, String[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public String[] getAllValues() { - return this.values; - } - - @Override - protected String getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/HugeintColumn.java +++ /dev/null @@ -1,36 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -import java.math.BigInteger; - -/** - * Mapping for MonetDB HUGEINT data type - */ -public class HugeintColumn extends Column<BigInteger> { - - private final BigInteger[] values; - - public HugeintColumn(EmbeddedQueryResult result, int index, BigInteger[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public BigInteger[] getAllValues() { - return this.values; - } - - @Override - protected BigInteger getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/InetColumn.java +++ /dev/null @@ -1,36 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -import java.net.InetAddress; - -/** - * Mapping for MonetDB INET data type - */ -public class InetColumn extends Column<InetAddress> { - - private final InetAddress[] values; - - public InetColumn(EmbeddedQueryResult result, int index, InetAddress[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public InetAddress[] getAllValues() { - return this.values; - } - - @Override - protected InetAddress getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/IntColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB Integer data type - */ -public class IntColumn extends Column<Integer> { - - private final Integer[] values; - - public IntColumn(EmbeddedQueryResult result, int index, int[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Integer[] newArray = new Integer[values.length]; - int j = newArray.length; - for(int i = 0 ; i < j ; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Integer.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Integer[] getAllValues() { - return this.values; - } - - @Override - protected Integer getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/JSONColumn.java +++ /dev/null @@ -1,34 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB JSON data type - */ -public class JSONColumn extends Column<String> { - - private final String[] values; - - public JSONColumn(EmbeddedQueryResult result, int index, String[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public String[] getAllValues() { - return this.values; - } - - @Override - protected String getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/MonthIntervalColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB MONTH_INTERVAL data type - */ -public class MonthIntervalColumn extends Column<Integer> { - - private final Integer[] values; - - public MonthIntervalColumn(EmbeddedQueryResult result, int index, int[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Integer[] newArray = new Integer[values.length]; - int j = newArray.length; - for (int i = 0; i < j; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Integer.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Integer[] getAllValues() { - return this.values; - } - - @Override - protected Integer getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/RealColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB REAL data type - */ -public class RealColumn extends Column<Float> { - - private final Float[] values; - - public RealColumn(EmbeddedQueryResult result, int index, float[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Float[] newArray = new Float[values.length]; - int j = newArray.length; - for(int i = 0 ; i < j ; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Float.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Float[] getAllValues() { - return this.values; - } - - @Override - protected Float getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/SecondIntervalColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB SECOND_INTERVAL data type - */ -public class SecondIntervalColumn extends Column<Long> { - - private final Long[] values; - - public SecondIntervalColumn(EmbeddedQueryResult result, int index, long[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Long[] newArray = new Long[values.length]; - int j = newArray.length; - for (int i = 0; i < j; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Long.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Long[] getAllValues() { - return this.values; - } - - @Override - protected Long getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/SmallintColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB SMALLINT data type - */ -public class SmallintColumn extends Column<Short> { - - private final Short[] values; - - public SmallintColumn(EmbeddedQueryResult result, int index, short[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Short[] newArray = new Short[values.length]; - int j = newArray.length; - for(int i = 0 ; i < j ; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Short.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Short[] getAllValues() { - return this.values; - } - - @Override - protected Short getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/TimeColumn.java +++ /dev/null @@ -1,36 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -import java.sql.Time; - -/** - * Mapping for MonetDB TIME data type - */ -public class TimeColumn extends Column<Time> { - - private final Time[] values; - - public TimeColumn(EmbeddedQueryResult result, int index, Time[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public Time[] getAllValues() { - return this.values; - } - - @Override - protected Time getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/TimestampColumn.java +++ /dev/null @@ -1,36 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -import java.sql.Timestamp; - -/** - * Mapping for MonetDB TIMESTAMP data type - */ -public class TimestampColumn extends Column<Timestamp> { - - private final Timestamp[] values; - - public TimestampColumn(EmbeddedQueryResult result, int index, Timestamp[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public Timestamp[] getAllValues() { - return this.values; - } - - @Override - protected Timestamp getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/TinyintColumn.java +++ /dev/null @@ -1,43 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB TINYINT data type - */ -public class TinyintColumn extends Column<Byte> { - - private final Byte[] values; - - public TinyintColumn(EmbeddedQueryResult result, int index, byte[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - Byte[] newArray = new Byte[values.length]; - int j = newArray.length; - for(int i = 0 ; i < j ; i++) { - if (nullIndex[i]) { - newArray[i] = null; - } else { - newArray[i] = Byte.valueOf(values[i]); - } - } - this.values = newArray; - } - - @Override - public Byte[] getAllValues() { - return this.values; - } - - @Override - protected Byte getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/URLColumn.java +++ /dev/null @@ -1,35 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; -import java.net.URL; - -/** - * Mapping for MonetDB URL data type - */ -public class URLColumn extends Column<URL> { - - private final URL[] values; - - public URLColumn(EmbeddedQueryResult result, int index, URL[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public URL[] getAllValues() { - return this.values; - } - - @Override - protected URL getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/UUIDColumn.java +++ /dev/null @@ -1,36 +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 2008-2015 MonetDB B.V. - */ - -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -import java.util.UUID; - -/** - * Mapping for MonetDB UUID data type - */ -public class UUIDColumn extends Column<UUID> { - - private final UUID[] values; - - public UUIDColumn(EmbeddedQueryResult result, int index, UUID[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public UUID[] getAllValues() { - return this.values; - } - - @Override - protected UUID getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/column/VarcharColumn.java +++ /dev/null @@ -1,26 +0,0 @@ -package nl.cwi.monetdb.embedded.column; - -import nl.cwi.monetdb.embedded.EmbeddedQueryResult; - -/** - * Mapping for MonetDB VARCHAR data type - */ -public class VarcharColumn extends Column<String> { - - private final String[] values; - - public VarcharColumn(EmbeddedQueryResult result, int index, String[] values, boolean[] nullIndex) { - super(result, index, nullIndex); - this.values = values; - } - - @Override - public String[] getAllValues() { - return this.values; - } - - @Override - protected String getValueImplementation(int index) { - return this.values[index]; - } -}
deleted file mode 100644 --- a/src/main/java/nl/cwi/monetdb/embedded/types/MonetDBEmbeddedBlob.java +++ /dev/null @@ -1,14 +0,0 @@ -package nl.cwi.monetdb.embedded.types; - -/** - * Created by ferreira on 10/27/16. - */ -public class MonetDBEmbeddedBlob { - protected final byte[] blob; - - public MonetDBEmbeddedBlob(byte[] blob) { - this.blob = blob; - } - - public byte[] getBlob() { return blob; } -}