Browse files

Refactored for initial select API

  • Loading branch information...
1 parent f96c372 commit 146379c3fd651516666820a457114edddc3ac0dc @alta189 committed Apr 29, 2012
View
6 src/main/java/com/alta189/simplesave/Database.java
@@ -56,15 +56,15 @@ public void close() throws ConnectionException {
lock = false;
}
- public SelectQuery select(Class<?> tableClass) {
+ public <T> SelectQuery<T> select(Class<T> tableClass) {
if (getTableRegistration(tableClass) == null)
throw new UnknownTableException("Cannot select from an unregistered table!");
- return new SelectQuery(this, tableClass);
+ return new SelectQuery<T>(this, tableClass);
}
public abstract boolean isConnected();
- public abstract QueryResult execute(Query query);
+ public abstract <T> QueryResult<T> execute(Query<T> query);
public abstract void save(Class<?> tableClass, Object o);
View
4 src/main/java/com/alta189/simplesave/query/Query.java
@@ -1,6 +1,6 @@
package com.alta189.simplesave.query;
-public abstract class Query {
+public abstract class Query<T> {
private final QueryType type;
@@ -12,6 +12,6 @@ public QueryType getType() {
return type;
}
- public abstract QueryResult execute();
+ public abstract QueryResult<T> execute();
}
View
17 src/main/java/com/alta189/simplesave/query/QueryResult.java
@@ -1,7 +1,22 @@
package com.alta189.simplesave.query;
-public class QueryResult {
+import java.util.List;
+public class QueryResult<T> {
+
+ private final List<T> results;
+
+ public QueryResult(List<T> results) {
+ this.results = results;
+ }
+
+ public List<T> find() {
+ return results;
+ }
+
+ public T findOne() {
+ return results.get(0);
+ }
}
View
14 src/main/java/com/alta189/simplesave/query/SelectQuery.java
@@ -2,28 +2,28 @@
import com.alta189.simplesave.Database;
-public class SelectQuery extends Query {
+public class SelectQuery<T> extends Query<T> {
private final Database db;
- private final Class<?> tableClass;
- private final WhereQuery where = new WhereQuery(this);
+ private final Class<T> tableClass;
+ private final WhereQuery<T> where = new WhereQuery<T>(this);
- public SelectQuery(Database db, Class<?> tableClass) {
+ public SelectQuery(Database db, Class<T> tableClass) {
super(QueryType.SELECT);
this.db = db;
this.tableClass = tableClass;
}
- public WhereQuery where() {
+ public WhereQuery<T> where() {
return where;
}
- public Class<?> getTableClass() {
+ public Class<T> getTableClass() {
return tableClass;
}
@Override
- public QueryResult execute() {
+ public QueryResult<T> execute() {
return db.execute(this);
}
View
10 src/main/java/com/alta189/simplesave/query/WhereEntry.java
@@ -1,14 +1,14 @@
package com.alta189.simplesave.query;
-public class WhereEntry {
+public class WhereEntry<T> {
private final Comparator comparator;
private final String field;
private final String comparison;
- private final WhereQuery parent;
+ private final WhereQuery<T> parent;
private Operator operator;
- public WhereEntry(Comparator comparator, String field, String comparison, WhereQuery parent) {
+ public WhereEntry(Comparator comparator, String field, String comparison, WhereQuery<T> parent) {
this.comparator = comparator;
this.field = field;
this.comparison = comparison;
@@ -27,12 +27,12 @@ public String getComparison() {
return comparison;
}
- public WhereQuery setOperator(Operator operator) {
+ public WhereQuery<T> setOperator(Operator operator) {
this.operator = operator;
return parent;
}
- public QueryResult execute() {
+ public QueryResult<T> execute() {
return parent.execute();
}
}
View
37 src/main/java/com/alta189/simplesave/query/WhereQuery.java
@@ -3,57 +3,58 @@
import java.util.ArrayList;
import java.util.List;
-public class WhereQuery extends Query {
+public class WhereQuery<T> extends Query<T> {
- private final List<WhereEntry> entries = new ArrayList<WhereEntry>();
- private final Query parent;
+ private final List<WhereEntry<T>> entries;
+ private final Query<T> parent;
- public WhereQuery(Query parent) {
+ public WhereQuery(Query<T> parent) {
super(QueryType.WHERE);
this.parent = parent;
+ entries = new ArrayList<WhereEntry<T>>();
}
- public WhereEntry equal(String field, String comparison) {
- WhereEntry entry = new WhereEntry(Comparator.EQUAL, field, comparison, this);
+ public WhereEntry<T> equal(String field, String comparison) {
+ WhereEntry<T> entry = new WhereEntry<T>(Comparator.EQUAL, field, comparison, this);
entries.add(entry);
return entry;
}
- public WhereEntry notEqual(String field, String comparison) {
- WhereEntry entry = new WhereEntry(Comparator.NOT_EQUAL, field, comparison, this);
+ public WhereEntry<T> notEqual(String field, String comparison) {
+ WhereEntry<T> entry = new WhereEntry<T>(Comparator.NOT_EQUAL, field, comparison, this);
entries.add(entry);
return entry;
}
- public WhereEntry greaterThan(String field, String comparison) {
- WhereEntry entry = new WhereEntry(Comparator.GREATER_THAN, field, comparison, this);
+ public WhereEntry<T> greaterThan(String field, String comparison) {
+ WhereEntry<T> entry = new WhereEntry<T>(Comparator.GREATER_THAN, field, comparison, this);
entries.add(entry);
return entry;
}
- public WhereEntry lessThan(String field, String comparison) {
- WhereEntry entry = new WhereEntry(Comparator.LESS_THAN, field, comparison, this);
+ public WhereEntry<T> lessThan(String field, String comparison) {
+ WhereEntry<T> entry = new WhereEntry<T>(Comparator.LESS_THAN, field, comparison, this);
entries.add(entry);
return entry;
}
- public WhereEntry greaterThanOrEqual(String field, String comparison) {
- WhereEntry entry = new WhereEntry(Comparator.GREATER_THAN_OR_EQUAL, field, comparison, this);
+ public WhereEntry<T> greaterThanOrEqual(String field, String comparison) {
+ WhereEntry<T> entry = new WhereEntry<T>(Comparator.GREATER_THAN_OR_EQUAL, field, comparison, this);
entries.add(entry);
return entry;
}
- public WhereEntry lessThanOrEqual(String field, String comparison) {
- WhereEntry entry = new WhereEntry(Comparator.LESS_THAN_OR_EQUAL, field, comparison, this);
+ public WhereEntry<T> lessThanOrEqual(String field, String comparison) {
+ WhereEntry<T> entry = new WhereEntry<T>(Comparator.LESS_THAN_OR_EQUAL, field, comparison, this);
entries.add(entry);
return entry;
}
- public List<WhereEntry> getEntries() {
+ public List<WhereEntry<T>> getEntries() {
return entries;
}
- public QueryResult execute() {
+ public QueryResult<T> execute() {
return parent.execute();
}
}

0 comments on commit 146379c

Please sign in to comment.