Mercurial > hg > monetdb-java
changeset 41:3a19ebf83af6 embedded
Arranged code in packages and starting tables integration.
line wrap: on
line diff
rename from src/main/java/nl/cwi/monetdb/embedded/AbstractStatementResult.java rename to src/main/java/nl/cwi/monetdb/embedded/env/AbstractConnectionResult.java --- a/src/main/java/nl/cwi/monetdb/embedded/AbstractStatementResult.java +++ b/src/main/java/nl/cwi/monetdb/embedded/env/AbstractConnectionResult.java @@ -6,23 +6,23 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.env; import java.io.Closeable; /** - * The base class for a query result. + * The base class for a pending statement to a connection. * * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> */ -public abstract class AbstractStatementResult implements Closeable { +public abstract class AbstractConnectionResult implements Closeable { /** * The corresponding connection of this result. */ private final MonetDBEmbeddedConnection connection; - protected AbstractStatementResult(MonetDBEmbeddedConnection connection) { + protected AbstractConnectionResult(MonetDBEmbeddedConnection connection) { this.connection = connection; }
rename from src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedConnection.java rename to src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedConnection.java --- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedConnection.java +++ b/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedConnection.java @@ -6,7 +6,12 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.env; + +import nl.cwi.monetdb.embedded.resultset.EmbeddedPreparedStatement; +import nl.cwi.monetdb.embedded.resultset.QueryResultSet; +import nl.cwi.monetdb.embedded.resultset.QueryResultSetColumn; +import nl.cwi.monetdb.embedded.resultset.UpdateResultSet; import java.util.HashSet; import java.util.Set; @@ -25,7 +30,7 @@ public class MonetDBEmbeddedConnection { private final long connectionPointer; - private final Set<AbstractStatementResult> results = new HashSet<>(); + private final Set<AbstractConnectionResult> results = new HashSet<>(); protected MonetDBEmbeddedConnection(MonetDBEmbeddedDatabase database, long connectionPointer) { this.database = database; @@ -107,10 +112,10 @@ public class MonetDBEmbeddedConnection { * @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)); */ + /*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. @@ -135,10 +140,10 @@ public class MonetDBEmbeddedConnection { * @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)); */ + /*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. @@ -161,10 +166,10 @@ public class MonetDBEmbeddedConnection { * @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)); */ + /*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); @@ -224,7 +229,7 @@ public class MonetDBEmbeddedConnection { * When the database is shuts down, this method is called instead */ protected void closeConnectionImplementation() { - for(AbstractStatementResult res : this.results) { + for(AbstractConnectionResult res : this.results) { res.closeImplementation(); } this.closeConnectionInternal(this.connectionPointer); @@ -241,15 +246,15 @@ public class MonetDBEmbeddedConnection { /** * Shuts down this connection asynchronously. Any pending queries connections will be immediately closed as well. */ - public void closeConnectionAsync() { - /* CompletableFuture.supplyAsync(() -> this.closeConnection()); */ + /*public void closeConnectionAsync() { + CompletableFuture.supplyAsync(() -> this.closeConnection()); throw new UnsupportedOperationException("Must wait for Java 8 :("); - } + }*/ /** * Removes a query result from this connection. */ - protected void removeQueryResult(AbstractStatementResult res) { + protected void removeQueryResult(AbstractConnectionResult res) { this.results.remove(res); }
rename from src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedDatabase.java rename to src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedDatabase.java --- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedDatabase.java +++ b/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedDatabase.java @@ -6,7 +6,7 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.env; import java.util.HashSet; import java.util.Set; @@ -48,11 +48,11 @@ public class MonetDBEmbeddedDatabase { * @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, + /*public static MonetDBEmbeddedDatabase StartDatabaseAsync(String dbDirectory, boolean silentFlag, boolean sequentialFlag) throws MonetDBEmbeddedException { - /* CompletableFuture.supplyAsync(() -> StartDatabase(dbDirectory, silentFlag, sequentialFlag)); */ + CompletableFuture.supplyAsync(() -> StartDatabase(dbDirectory, silentFlag, sequentialFlag)); throw new UnsupportedOperationException("Must wait for Java 8 :("); - } + }*/ private final String databaseDirectory; @@ -127,10 +127,10 @@ public class MonetDBEmbeddedDatabase { * * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred */ - public void stopDatabaseAsync() throws MonetDBEmbeddedException { - /* CompletableFuture.supplyAsync(() -> this.stopDatabase()); */ + /*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. @@ -148,10 +148,10 @@ public class MonetDBEmbeddedDatabase { * @return A MonetDBEmbeddedConnection instance * @throws MonetDBEmbeddedException If the database is not running or an error in the database occurred */ - public MonetDBEmbeddedConnection createConnectionAsync() throws MonetDBEmbeddedException { - /* CompletableFuture.supplyAsync(() -> this.createConnectionInternal()); */ + /*public MonetDBEmbeddedConnection createConnectionAsync() throws MonetDBEmbeddedException { + CompletableFuture.supplyAsync(() -> this.createConnectionInternal()); throw new UnsupportedOperationException("Must wait for Java 8 :("); - } + }*/ /** * Removes a connection from this database.
rename from src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedException.java rename to src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedException.java --- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedException.java +++ b/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedException.java @@ -6,7 +6,7 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.env; /** * The exception fired from embedded methods.
rename from src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java rename to src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedInstance.java --- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedInstance.java +++ b/src/main/java/nl/cwi/monetdb/embedded/env/MonetDBEmbeddedInstance.java @@ -6,7 +6,7 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.env; /** * The MonetDB's JNI library loader for Java.
rename from src/main/java/nl/cwi/monetdb/embedded/AbstractColumn.java rename to src/main/java/nl/cwi/monetdb/embedded/mapping/AbstractColumn.java --- a/src/main/java/nl/cwi/monetdb/embedded/AbstractColumn.java +++ b/src/main/java/nl/cwi/monetdb/embedded/mapping/AbstractColumn.java @@ -6,7 +6,9 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.mapping; + +import nl.cwi.monetdb.embedded.mapping.MonetDBToJavaMapping; /** * A single Java representation of a MonetDB column. @@ -14,7 +16,7 @@ package nl.cwi.monetdb.embedded; * @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> { +public abstract class AbstractColumn<T> { /** * Index on the result set. @@ -22,11 +24,6 @@ public abstract class AbstractColumn<T> 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; @@ -46,10 +43,9 @@ public abstract class AbstractColumn<T> */ protected final int columnScale; - protected AbstractColumn(int resultSetIndex, int numberOfRows, String columnName, String columnType, - int columnDigits, int columnScale) { + protected AbstractColumn(int resultSetIndex, String columnName, String columnType, int columnDigits, + int columnScale) { this.resultSetIndex = resultSetIndex; - this.numberOfRows = numberOfRows; this.columnName = columnName; this.mapping = MonetDBToJavaMapping.GetJavaMappingFromMonetDBString(columnType); this.columnDigits = columnDigits; @@ -57,13 +53,6 @@ public abstract class AbstractColumn<T> } /** - * 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
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/mapping/AbstractResultTable.java @@ -0,0 +1,107 @@ +package nl.cwi.monetdb.embedded.mapping; + +import nl.cwi.monetdb.embedded.env.AbstractConnectionResult; +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedConnection; + +/** + * Base class for statement results with data + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public abstract class AbstractResultTable extends AbstractConnectionResult { + + public AbstractResultTable(MonetDBEmbeddedConnection connection) { + super(connection); + } + + /** + * Returns an array of columns in the result set; + * + * @return An array of columns in the result set; + */ + protected abstract AbstractColumn<?>[] getColumns(); + + /** + * Returns the number of columns in the result set. + * + * @return Number of columns + */ + public abstract int getNumberOfColumns(); + + /** + * Returns the number of rows in the result set. + * + * @return Number of rows + */ + public abstract int getNumberOfRows(); + + /** + * Get the columns names as a string array. + * + * @return The columns names array + */ + public String[] getColumnNames() { + int i = 0; + String[] result = new String[this.getNumberOfColumns()]; + for(AbstractColumn col : this.getColumns()) { + 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.getNumberOfColumns()]; + for(AbstractColumn col : this.getColumns()) { + 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.getNumberOfColumns()]; + for(AbstractColumn col : this.getColumns()) { + 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.getNumberOfColumns()]; + for(AbstractColumn col : this.getColumns()) { + 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.getNumberOfColumns()]; + for(AbstractColumn col :this.getColumns()) { + result[i] = col.getColumnScale(); + } + return result; + } +}
rename from src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedBlob.java rename to src/main/java/nl/cwi/monetdb/embedded/mapping/MonetDBEmbeddedBlob.java --- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBEmbeddedBlob.java +++ b/src/main/java/nl/cwi/monetdb/embedded/mapping/MonetDBEmbeddedBlob.java @@ -1,4 +1,4 @@ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.mapping; /** * A Java representation for Blob data type. Added for more efficient data mapping when fetching from the database.
rename from src/main/java/nl/cwi/monetdb/embedded/MonetDBToJavaMapping.java rename to src/main/java/nl/cwi/monetdb/embedded/mapping/MonetDBToJavaMapping.java --- a/src/main/java/nl/cwi/monetdb/embedded/MonetDBToJavaMapping.java +++ b/src/main/java/nl/cwi/monetdb/embedded/mapping/MonetDBToJavaMapping.java @@ -6,7 +6,7 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.mapping; import java.math.BigDecimal; import java.math.BigInteger;
rename from src/main/java/nl/cwi/monetdb/embedded/EmbeddedPreparedStatement.java rename to src/main/java/nl/cwi/monetdb/embedded/resultset/EmbeddedPreparedStatement.java --- a/src/main/java/nl/cwi/monetdb/embedded/EmbeddedPreparedStatement.java +++ b/src/main/java/nl/cwi/monetdb/embedded/resultset/EmbeddedPreparedStatement.java @@ -6,8 +6,12 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.resultset; +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedConnection; +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedException; +import nl.cwi.monetdb.embedded.mapping.MonetDBEmbeddedBlob; +import nl.cwi.monetdb.embedded.mapping.MonetDBToJavaMapping; import java.math.BigDecimal; import java.math.BigInteger; import java.math.RoundingMode; @@ -410,9 +414,9 @@ public class EmbeddedPreparedStatement { * @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 { + /*public UpdateResultSet sendUpdateAsync() throws MonetDBEmbeddedException { return this.connection.sendUpdateAsync(this.applyParameters()); - } + }*/ /** * Executes this statement as a SQL query with a result set asynchronously. @@ -420,7 +424,7 @@ public class EmbeddedPreparedStatement { * @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 { + /*public QueryResultSet sendQueryAsync() throws MonetDBEmbeddedException { return this.connection.sendQueryAsync(this.applyParameters()); - } + }*/ }
rename from src/main/java/nl/cwi/monetdb/embedded/QueryResultSet.java rename to src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSet.java --- a/src/main/java/nl/cwi/monetdb/embedded/QueryResultSet.java +++ b/src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSet.java @@ -6,7 +6,12 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.resultset; + +import nl.cwi.monetdb.embedded.mapping.AbstractColumn; +import nl.cwi.monetdb.embedded.mapping.AbstractResultTable; +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedConnection; +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedException; import java.util.Arrays; import java.util.ListIterator; @@ -19,7 +24,7 @@ import java.util.ListIterator; * * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> */ -public class QueryResultSet extends AbstractStatementResult implements Iterable { +public class QueryResultSet extends AbstractResultTable implements Iterable { /** * Pointer to the native result set. @@ -29,30 +34,44 @@ public class QueryResultSet extends Abst protected long resultPointer; /** - * The number of columns in the query result. + * The query result set columns listing. */ - protected final int numberOfColumns; + private final QueryResultSetColumn<?>[] columns; /** * The number of rows in the query result. */ protected final int numberOfRows; - /** - * The query result set columns listing. - */ - private final QueryResultSetColumn<?>[] columns; - protected QueryResultSet(MonetDBEmbeddedConnection connection, long resultPointer, QueryResultSetColumn<?>[] columns, int numberOfRows) { super(connection); this.resultPointer = resultPointer; - this.numberOfColumns = columns.length; this.numberOfRows = numberOfRows; this.columns = columns; } /** + * Close the query data so no more new results can be retrieved. + */ + @Override + public void closeImplementation() { + this.cleanupResultInternal(this.resultPointer); + this.resultPointer = 0; + } + + @Override + protected AbstractColumn<?>[] getColumns() { return columns; } + + @Override + public int getNumberOfRows() { + return this.numberOfRows; + } + + @Override + public int getNumberOfColumns() { return this.columns.length; } + + /** * 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 @@ -60,94 +79,6 @@ public class QueryResultSet extends Abst public boolean isStatementClosed() { return this.resultPointer == 0; } /** - * 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.columns) { - 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.columns) { - 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.columns) { - 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.columns) { - 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.columns) { - result[i] = col.getColumnScale(); - } - return result; - } - - /** * Get a columns' values from the result set by index. * * @param index QueryResultSetColumn index (starting from 0) @@ -197,8 +128,8 @@ public class QueryResultSet extends Abst 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[][] temp = new Object[numberOfRowsToRetrieve][this.getNumberOfColumns()]; + for (int i = 0 ; i < this.getNumberOfColumns(); i++) { Object[] nextColumn = this.columns[i].fetchColumnValues(startIndex, endIndex); for(int j = 0; j < numberOfRowsToRetrieve ; j++) { temp[j][i] = nextColumn[j]; @@ -215,10 +146,10 @@ public class QueryResultSet extends Abst * @return The rows as {@code QueryResultSetRows} * @throws MonetDBEmbeddedException If an error in the database occurred */ - public QueryResultSetRows fetchResultSetRowsAsync(int startIndex, int endIndex) throws MonetDBEmbeddedException { - /* CompletableFuture.supplyAsync(() -> this.fetchResultSetRows(startIndex, endIndex)); */ + /*public QueryResultSetRows fetchResultSetRowsAsync(int startIndex, int endIndex) throws MonetDBEmbeddedException { + CompletableFuture.supplyAsync(() -> this.fetchResultSetRows(startIndex, endIndex)); throw new UnsupportedOperationException("Must wait for Java 8 :("); - } + }*/ /** * Fetches the first N rows from the result set. @@ -238,9 +169,9 @@ public class QueryResultSet extends Abst * @return The rows as {@code QueryResultSetRows} * @throws MonetDBEmbeddedException If an error in the database occurred */ - public QueryResultSetRows fetchFirstNRowValuesAsync(int n) throws MonetDBEmbeddedException { + /*public QueryResultSetRows fetchFirstNRowValuesAsync(int n) throws MonetDBEmbeddedException { return this.fetchResultSetRowsAsync(0, n); - } + }*/ /** * Fetches all rows from the result set. @@ -258,9 +189,9 @@ public class QueryResultSet extends Abst * @return The rows as {@code QueryResultSetRows} * @throws MonetDBEmbeddedException If an error in the database occurred */ - public QueryResultSetRows fetchAllRowValuesAsync() throws MonetDBEmbeddedException { + /*public QueryResultSetRows fetchAllRowValuesAsync() throws MonetDBEmbeddedException { return this.fetchResultSetRowsAsync(0, this.numberOfRows); - } + }*/ @Override public ListIterator<QueryResultSetRows.QueryResulSetRow> iterator() { @@ -271,14 +202,5 @@ public class QueryResultSet extends Abst } } - /** - * Close the query data so no more new results can be retrieved. - */ - @Override - public void closeImplementation() { - this.cleanupResultInternal(this.resultPointer); - this.resultPointer = 0; - } - private native void cleanupResultInternal(long resultPointer); }
rename from src/main/java/nl/cwi/monetdb/embedded/QueryResultSetColumn.java rename to src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSetColumn.java --- a/src/main/java/nl/cwi/monetdb/embedded/QueryResultSetColumn.java +++ b/src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSetColumn.java @@ -6,7 +6,10 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.resultset; + +import nl.cwi.monetdb.embedded.mapping.AbstractColumn; +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedException; import java.lang.reflect.Array; import java.util.Arrays; @@ -19,7 +22,7 @@ import java.util.ListIterator; * @param <T> A Java class mapped to a MonetDB data type * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> */ -public class QueryResultSetColumn<T> extends AbstractColumn<T> { +public class QueryResultSetColumn<T> extends AbstractColumn<T> implements Iterable<T> { /** * The C pointer of the result set of the column. @@ -32,6 +35,11 @@ public class QueryResultSetColumn<T> ext private final T[] values; /** + * The number of rows in this column. + */ + protected final int numberOfRows; + + /** * The index of the first value mapped to a Java class. */ private int firstRetrievedIndex; @@ -42,16 +50,24 @@ public class QueryResultSetColumn<T> ext private int lastRetrievedIndex; @SuppressWarnings("unchecked") - protected QueryResultSetColumn(int resultSetIndex, int numberOfRows, String columnName, String columnType, - int columnDigits, int columnScale, long resultSetPointer) { - super(resultSetIndex, numberOfRows, columnName, columnType, columnDigits, columnScale); + protected QueryResultSetColumn(int resultSetIndex, String columnName, String columnType, int columnDigits, + int columnScale, long resultSetPointer, int numberOfRows) { + super(resultSetIndex, columnName, columnType, columnDigits, columnScale); this.resultSetPointer = resultSetPointer; + this.numberOfRows = numberOfRows; this.firstRetrievedIndex = numberOfRows; this.lastRetrievedIndex = 0; this.values = (T[]) Array.newInstance(this.mapping.getJavaClass(), numberOfRows); } /** + * Get the number of rows in this column. + * + * @return The number of rows + */ + public int getNumberOfRows() { return numberOfRows; } + + /** * Maps columns values into the Java representation. * * @param startIndex The first column index to retrieve @@ -111,10 +127,10 @@ public class QueryResultSetColumn<T> ext * @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)); */ + /*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. @@ -136,9 +152,9 @@ public class QueryResultSetColumn<T> ext * @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 { + /*public T[] fetchFirstNColumnValuesAsync(int n, Class<T> javaClass) throws MonetDBEmbeddedException { return this.fetchColumnValuesAsync(0, n, javaClass); - } + }*/ /** * Maps all column values. @@ -158,9 +174,9 @@ public class QueryResultSetColumn<T> ext * @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 { + /*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. @@ -184,9 +200,9 @@ public class QueryResultSetColumn<T> ext * @throws MonetDBEmbeddedException If an error in the database occurred */ @SuppressWarnings("unchecked") - public T[] fetchColumnValuesAsync(int startIndex, int endIndex) throws MonetDBEmbeddedException { + /*public T[] fetchColumnValuesAsync(int startIndex, int endIndex) throws MonetDBEmbeddedException { return this.fetchColumnValuesAsync(startIndex, endIndex, (Class<T>) this.mapping.getJavaClass()); - } + }*/ /** * Maps the first N column values using the provided Java representation by the query. @@ -206,9 +222,9 @@ public class QueryResultSetColumn<T> ext * @return The column values as a Java array * @throws MonetDBEmbeddedException If an error in the database occurred */ - public T[] fetchFirstNColumnValuesAsync(int n) throws MonetDBEmbeddedException { + /*public T[] fetchFirstNColumnValuesAsync(int n) throws MonetDBEmbeddedException { return this.fetchColumnValuesAsync(0, n); - } + }*/ /** * Maps all column values using the provided Java representation by the query. @@ -226,16 +242,19 @@ public class QueryResultSetColumn<T> ext * @return The column values as a Java array * @throws MonetDBEmbeddedException If an error in the database occurred */ - public T[] fetchAllColumnValuesAsync() throws MonetDBEmbeddedException { + /*public T[] fetchAllColumnValuesAsync() throws MonetDBEmbeddedException { return this.fetchColumnValuesAsync(0, this.numberOfRows); - } + }*/ @Override public ListIterator<T> iterator() { - return Arrays.asList(this.values).listIterator(); + try { + return Arrays.asList(this.fetchAllColumnValues()).listIterator(); + } catch (Exception ex) { + return null; + } } private native T[] fetchValuesInternal(long resultPointer, int resultSetIndex, Class<T> jclass, String className, int enumEntry, int first, int last) throws MonetDBEmbeddedException; - }
rename from src/main/java/nl/cwi/monetdb/embedded/QueryResultSetRows.java rename to src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSetRows.java --- a/src/main/java/nl/cwi/monetdb/embedded/QueryResultSetRows.java +++ b/src/main/java/nl/cwi/monetdb/embedded/resultset/QueryResultSetRows.java @@ -6,7 +6,9 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.resultset; + +import nl.cwi.monetdb.embedded.mapping.MonetDBToJavaMapping; import java.lang.reflect.Array; import java.util.Arrays;
rename from src/main/java/nl/cwi/monetdb/embedded/UpdateResultSet.java rename to src/main/java/nl/cwi/monetdb/embedded/resultset/UpdateResultSet.java --- a/src/main/java/nl/cwi/monetdb/embedded/UpdateResultSet.java +++ b/src/main/java/nl/cwi/monetdb/embedded/resultset/UpdateResultSet.java @@ -6,14 +6,17 @@ * Copyright 2016 MonetDB B.V. */ -package nl.cwi.monetdb.embedded; +package nl.cwi.monetdb.embedded.resultset; + +import nl.cwi.monetdb.embedded.env.AbstractConnectionResult; +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedConnection; /** * 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 { +public class UpdateResultSet extends AbstractConnectionResult { protected UpdateResultSet(MonetDBEmbeddedConnection connection) { super(connection);
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/IMonetDBTableBaseIterator.java @@ -0,0 +1,11 @@ +package nl.cwi.monetdb.embedded.tables; + +/** + * Created by ferreira on 11/7/16. + */ +public interface IMonetDBTableBaseIterator { + + int getFirstRowToIterate(); + + int getLastRowToIterate(); +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/IMonetDBTableIterator.java @@ -0,0 +1,9 @@ +package nl.cwi.monetdb.embedded.tables; + +/** + * Created by ferreira on 11/7/16. + */ +public interface IMonetDBTableIterator extends IMonetDBTableBaseIterator { + + void nextRow(RowIterator nextRow); +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/IMonetDBTableRemover.java @@ -0,0 +1,9 @@ +package nl.cwi.monetdb.embedded.tables; + +/** + * Created by ferreira on 11/7/16. + */ +public interface IMonetDBTableRemover extends IMonetDBTableBaseIterator { + + void removeNextRow(RowRemover nextRow); +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/IMonetDBTableUpdater.java @@ -0,0 +1,9 @@ +package nl.cwi.monetdb.embedded.tables; + +/** + * Created by ferreira on 11/7/16. + */ +public interface IMonetDBTableUpdater extends IMonetDBTableBaseIterator { + + void updateNextRow(RowUpdater nextRow); +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/MonetDBTable.java @@ -0,0 +1,114 @@ +package nl.cwi.monetdb.embedded.tables; + +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedException; +import nl.cwi.monetdb.embedded.mapping.AbstractColumn; +import nl.cwi.monetdb.embedded.mapping.AbstractResultTable; +import nl.cwi.monetdb.embedded.env.MonetDBEmbeddedConnection; + +/** + * Java representation of a MonetDB table. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public class MonetDBTable extends AbstractResultTable { + + /** + * The table schema + */ + private final String tableSchema; + + /** + * The table name + */ + private final String tableName; + + /** + * The table columns + */ + private final MonetDBTableColumn<?>[] columns; + + public MonetDBTable(MonetDBEmbeddedConnection connection, String tableSchema, String tableName, + MonetDBTableColumn<?>[] columns) { + super(connection); + this.tableSchema = tableSchema; + this.tableName = tableName; + this.columns = columns; + } + + /** + * Let's see... + */ + @Override + protected void closeImplementation() {} + + @Override + protected AbstractColumn<?>[] getColumns() { return columns; } + + @Override + public int getNumberOfColumns() { return columns.length; } + + @Override + public int getNumberOfRows() { + return 0; + } + + /** + * Get the table schema name + * + * @return The table schema name + */ + public String getTableSchema() { return tableSchema; } + + /** + * Get the table name + * + * @return The table name + */ + public String getTableName() { return tableName; } + + public void iterateTable(IMonetDBTableIterator iterator) throws MonetDBEmbeddedException { + RowIterator ri = new RowIterator(this, iterator.getFirstRowToIterate(), iterator.getLastRowToIterate()); + while(this.getNextRow(ri)) { + iterator.nextRow(ri); + } + } + + public int updateRows(IMonetDBTableUpdater updater) throws MonetDBEmbeddedException { + int res = 0; + RowUpdater ru = new RowUpdater(this, updater.getFirstRowToIterate(), updater.getLastRowToIterate()); + while(this.getNextRow(ru)) { + updater.updateNextRow(ru); + if(ru.toUpdate()) { + res++; + this.updateNextRow(ru); + } + + } + return res; + } + + public int removeRows(IMonetDBTableRemover remover) throws MonetDBEmbeddedException { + int res = 0; + RowRemover rr = new RowRemover(this, remover.getFirstRowToIterate(), remover.getLastRowToIterate()); + while(this.getNextRow(rr)) { + remover.removeNextRow(rr); + if(rr.isToRemove()) { + res++; + this.removeNextRow(rr); + } + } + return res; + } + + public native int truncate(); + + public int appendRows(Object[][] rows) { + return 0; + } + + private native boolean getNextRow(RowIterator ri) throws MonetDBEmbeddedException; + + private native boolean updateNextRow(RowUpdater ri) throws MonetDBEmbeddedException; + + private native boolean removeNextRow(RowRemover rr) throws MonetDBEmbeddedException; +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/MonetDBTableColumn.java @@ -0,0 +1,17 @@ +package nl.cwi.monetdb.embedded.tables; + +import nl.cwi.monetdb.embedded.mapping.AbstractColumn; + +/** + * Java representation of a MonetDB table column. + * + * @author <a href="mailto:pedro.ferreira@monetdbsolutions.com">Pedro Ferreira</a> + */ +public class MonetDBTableColumn<T> extends AbstractColumn<T> { + + public MonetDBTableColumn(int resultSetIndex, String columnName, String columnType, int columnDigits, + int columnScale) { + super(resultSetIndex, columnName, columnType, columnDigits, columnScale); + } + +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/RowIterator.java @@ -0,0 +1,92 @@ +package nl.cwi.monetdb.embedded.tables; + +import nl.cwi.monetdb.embedded.mapping.MonetDBToJavaMapping; + +/** + * Created by ferreira on 11/7/16. + */ +public class RowIterator { + + /** + * The original table of this iterator. + */ + protected final MonetDBTable table; + + /** + * The mappings of the columns. + */ + protected final MonetDBToJavaMapping[] mappings; + + /** + * The columns values as Java objects. + */ + protected Object[] columns; + + /** + * The current row number. + */ + protected int rowNumber; + + private final int firstIndex; + + private final int lastIndex; + + public RowIterator(MonetDBTable table, int firstIndex, int lastIndex) { + this.table = table; + this.mappings = table.getMappings(); + this.firstIndex = Math.max(firstIndex, 0); + this.lastIndex = Math.min(lastIndex, table.getNumberOfRows()); + } + + /** + * Gets the original table of this iterator. + * + * @return The original table of this iterator + */ + public MonetDBTable getTable() { return table; } + + /** + * Gets the columns values as Java objects. + * + * @return The columns values as Java objects + */ + public Object[] getColumns() { + return columns; + } + + /** + * Gets the current row number in the iteration. + * + * @return The current row number in the iteration + */ + public int getRowNumber() { return rowNumber; } + + /** + * 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.mappings[index].getJavaClass(); + return javaClass.cast(columns[index]); + } + + protected void setNextIteration(Object[] columns, int rowNumber) { + this.columns = columns; + this.rowNumber = rowNumber; + } +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/RowRemover.java @@ -0,0 +1,28 @@ +package nl.cwi.monetdb.embedded.tables; + +/** + * Created by ferreira on 11/7/16. + */ +public class RowRemover extends RowIterator { + + private boolean toRemove; + + public RowRemover(MonetDBTable table, int firstIndex, int lastIndex) { + super(table, firstIndex, lastIndex); + this.toRemove = false; + } + + public boolean isToRemove() { + return toRemove; + } + + public void setToRemove(boolean toRemove) { + this.toRemove = toRemove; + } + + @Override + protected void setNextIteration(Object[] columns, int rowNumber) { + super.setNextIteration(columns, rowNumber); + this.toRemove = false; + } +}
new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/embedded/tables/RowUpdater.java @@ -0,0 +1,46 @@ +package nl.cwi.monetdb.embedded.tables; + +import java.util.Arrays; + +/** + * Created by ferreira on 11/7/16. + */ +public class RowUpdater extends RowIterator { + + private final boolean[] updatedIndexes; + + public RowUpdater(MonetDBTable table, int firstIndex, int lastIndex) { + super(table, firstIndex, lastIndex); + this.updatedIndexes = new boolean[table.getNumberOfColumns()]; + } + + public <T> void setColumn(int index, T value) { + this.columns[index] = value; + this.updatedIndexes[index] = true; + } + + public <T> void setColumn(int index, Class<T> javaClass, T value) { + this.columns[index] = value; + this.updatedIndexes[index] = true; + } + + public void setAllColumns(Object[] values) { + this.columns = values; + Arrays.fill(this.updatedIndexes, true); + } + + public boolean toUpdate() { + for (boolean bol : updatedIndexes) { + if(bol) { + return true; + } + } + return false; + } + + @Override + protected void setNextIteration(Object[] columns, int rowNumber) { + super.setNextIteration(columns, rowNumber); + Arrays.fill(this.updatedIndexes, false); + } +}