Skip to content

Commit

Permalink
Implemented a resource object to intuitivly handle query resource rel…
Browse files Browse the repository at this point in the history
…easing
  • Loading branch information
Francismb committed May 9, 2016
1 parent c644d0f commit c47c585
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 32 deletions.
8 changes: 3 additions & 5 deletions src/org/activeorm/database/Database.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.activeorm.database;

import org.activeorm.database.configuration.DatabaseConfiguration;
import org.activeorm.database.configuration.H2DatabaseConfiguration;
import org.activeorm.database.configuration.PostgreSQLDatabaseConfiguration;
import org.activeorm.database.configuration.SQLiteDatabaseConfiguration;
import org.activeorm.database.datahandler.BooleanHandler;
Expand All @@ -19,6 +18,7 @@
import org.activeorm.database.sql.SQLProducer;
import org.activeorm.exceptions.UnsupportedDataTypeException;
import org.activeorm.mapping.FieldMapping;
import org.activeorm.utility.Resource;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
Expand All @@ -33,9 +33,7 @@
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -280,13 +278,13 @@ public boolean disconnect() {
* @param parameters the parameters to prepare.
* @return The result of the query or null if the query failed.
*/
public synchronized ResultSet query(final String sql, final Object[] parameters) {
public synchronized Resource query(final String sql, final Object[] parameters) {
final PreparedStatement statement = prepare(sql, parameters, false);
if (statement != null) {
try {
final ResultSet result = statement.executeQuery();
connection.commit();
return result;
return new Resource(statement, result);
} catch (SQLException e) {
e.printStackTrace();
attemptRollback();
Expand Down
31 changes: 13 additions & 18 deletions src/org/activeorm/query/Query.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.activeorm.query;

import org.activeorm.database.Database;
import org.activeorm.utility.Resource;
import org.activeorm.exceptions.UnsupportedDataTypeException;
import org.activeorm.mapping.ActiveRecord;
import org.activeorm.mapping.FieldMapping;
Expand Down Expand Up @@ -176,7 +177,10 @@ public List<T> results() {
final String sql = database.sql.select(mapping.table.name(), null, whereColumns, whereOperators, orderColumns, orderDirections, limit != -1);

// Execute the query
final ResultSet resultSet = database.query(sql, parameters);
final Resource resource = database.query(sql, parameters);

// Get the result set
final ResultSet resultSet = resource.getResult();

// The results converted into objects
final List<T> results = new ArrayList<>();
Expand Down Expand Up @@ -212,14 +216,8 @@ public List<T> results() {
} catch (SQLException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
} finally {
try {
// Close the results resource
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
// Close the results resource
resource.release();
}
return results;
}
Expand Down Expand Up @@ -279,7 +277,10 @@ public T first() {
final String sql = database.sql.select(mapping.table.name(), null, whereColumns, whereOperators, orderColumns, orderDirections, limit != -1);

// Execute the query
final ResultSet resultSet = database.query(sql, parameters);
final Resource resource = database.query(sql, parameters);

// Get the result set
final ResultSet resultSet = resource.getResult();

try {
// Iterate through the result set
Expand Down Expand Up @@ -312,14 +313,8 @@ public T first() {
} catch (SQLException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
} finally {
try {
// Close the results resource
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
// Close the results resource
resource.release();
}
return null;
}
Expand Down
65 changes: 65 additions & 0 deletions src/org/activeorm/utility/Resource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.activeorm.utility;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* Created by Activate2 on 5/10/2016.
* <p>
* Resource is a object return from the query method in database.
* It contains the results of the query and the ability to close them.
*/
public class Resource {

/**
* The results of the resource.
*/
private final ResultSet result;

/**
* The statement that made the resource.
*/
private final Statement statement;

/**
* Constructs a new {@link Resource}.
*
* @param statement The statement that made the result.
* @param result the result from the query.
*/
public Resource(final Statement statement, final ResultSet result) {
this.statement = statement;
this.result = result;
}

/**
* @return the result of this resource.
*/
public ResultSet getResult() {
return this.result;
}

/**
* @return the statement of this resource
*/
public Statement getStatment() {
return this.statement;
}

/**
* Releases all of the resources in this object.
*
* @return true if it releases without error.
*/
public boolean release() {
try {
result.close();
statement.close();
return true;
} catch (final SQLException e) {
e.printStackTrace();
}
return false;
}
}
7 changes: 4 additions & 3 deletions test/org/activeorm/h2/database/DatabaseTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.activeorm.h2.database;

import org.activeorm.database.Database;
import org.activeorm.utility.Resource;
import org.junit.Test;

import java.sql.ResultSet;
Expand All @@ -27,9 +28,9 @@ public void testH2RawQuery() throws SQLException {
final Database database = Database.fromYaml("./h2-config.yml");
database.execute("CREATE MEMORY TABLE users(user_id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50), password VARCHAR(50))", null);
assertEquals(1, database.execute(database.sql.insert("users", new String[]{"username", "password"}), new Object[]{"Jackson", "super secret password"}));
final ResultSet rs = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(rs);
rs.close();
final Resource resource = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(resource);
resource.release();
database.execute("DROP TABLE users", null);
database.disconnect();
}
Expand Down
7 changes: 4 additions & 3 deletions test/org/activeorm/postgresql/database/DatabaseTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.activeorm.postgresql.database;

import org.activeorm.database.Database;
import org.activeorm.utility.Resource;
import org.junit.Test;

import java.sql.ResultSet;
Expand All @@ -26,9 +27,9 @@ public void testPostgreSQLRawQuery() throws SQLException {
final Database database = Database.fromYaml("./postgre-config.yml");
database.execute("CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR(50), password VARCHAR(50))", null);
assertEquals(1, database.execute(database.sql.insert("users", new String[]{"username", "password"}), new Object[]{"Jackson", "super secret password"}));
final ResultSet rs = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(rs);
rs.close();
final Resource resource = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(resource);
resource.release();
database.execute("DROP TABLE users", null);
database.disconnect();
}
Expand Down
7 changes: 4 additions & 3 deletions test/org/activeorm/sqlite/database/DatabaseTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.activeorm.sqlite.database;

import org.activeorm.database.Database;
import org.activeorm.utility.Resource;
import org.junit.Test;

import java.sql.ResultSet;
Expand All @@ -26,9 +27,9 @@ public void testSQLiteRawQuery() throws SQLException {
final Database database = Database.fromYaml("./sqllite-config.yml");
database.execute("CREATE TABLE users(user_id INTEGER PRIMARY KEY, username TEXT, password TEXT)", null);
assertEquals(1, database.execute(database.sql.insert("users", new String[]{"username", "password"}), new Object[]{"Jackson", "super secret password"}));
final ResultSet rs = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(rs);
rs.close();
final Resource resource = database.query(database.sql.select("users", null, new String[]{"username"}, new String[]{"="}, null, null, false), new Object[]{"Jackson"});
assertNotNull(resource);
resource.release();
database.execute("DROP TABLE users", null);
database.disconnect();
}
Expand Down

0 comments on commit c47c585

Please sign in to comment.