Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added test
  • Loading branch information
RoyMontoya committed Oct 20, 2015
commit da0a7a62a940a270b0bee2c70ba1c118d23bd001
59 changes: 35 additions & 24 deletions library/src/main/java/com/orm/query/Select.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.orm.query;

import android.database.sqlite.SQLiteQueryBuilder;

import com.orm.SugarRecord;
import com.orm.util.NamingHelper;

Expand All @@ -12,10 +14,10 @@ public class Select<T> implements Iterable {
private Class<T> record;
private String[] arguments;
private String whereClause = "";
private String orderBy;
private String groupBy;
private String limit;
private String offset;
private String orderBy = "";
private String groupBy = "";
private String limit = "";
private String offset = "";
private List<Object> args = new ArrayList<Object>();

public Select(Class<T> record) {
Expand All @@ -41,6 +43,11 @@ public Select<T> limit(String limit) {
return this;
}

public Select<T> offset(String offset) {
this.offset = offset;
return this;
}

public Select<T> where(String whereClause) {
this.whereClause = whereClause;
return this;
Expand All @@ -63,25 +70,25 @@ private void mergeConditions(Condition[] conditions, Condition.Type type) {
if (Condition.Check.LIKE.equals(condition.getCheck()) ||
Condition.Check.NOT_LIKE.equals(condition.getCheck())) {
toAppend
.append(condition.getProperty())
.append(condition.getCheckSymbol())
.append("'")
.append(condition.getValue().toString())
.append("'");
.append(condition.getProperty())
.append(condition.getCheckSymbol())
.append("'")
.append(condition.getValue().toString())
.append("'");
} else if (Condition.Check.IS_NULL.equals(condition.getCheck()) ||
Condition.Check.IS_NOT_NULL.equals(condition.getCheck())) {
toAppend
.append(condition.getProperty())
.append(condition.getCheckSymbol());
.append(condition.getProperty())
.append(condition.getCheckSymbol());
} else {
toAppend
.append(condition.getProperty())
.append(condition.getCheckSymbol())
.append("? ");
.append(condition.getProperty())
.append(condition.getCheckSymbol())
.append("? ");
args.add(condition.getValue());
}
}

if (!"".equals(whereClause)) {
whereClause += " " + type.name() + " ";
}
Expand Down Expand Up @@ -117,12 +124,12 @@ public List<T> list() {

return SugarRecord.find(record, whereClause, arguments, groupBy, orderBy, limit);
}

public long count() {
if (arguments == null) {
arguments = convertArgs(args);
}

return SugarRecord.count(record, whereClause, arguments, groupBy, orderBy, limit);
}

Expand All @@ -134,24 +141,28 @@ public T first() {
List<T> list = SugarRecord.find(record, whereClause, arguments, groupBy, orderBy, "1");
return list.size() > 0 ? list.get(0) : null;
}

String toSql() {
StringBuilder sql = new StringBuilder();
sql.append("SELECT * FROM ").append(NamingHelper.toSQLName(this.record)).append(" ");

if (whereClause != null) {
if (!whereClause.isEmpty()) {
sql.append("WHERE ").append(whereClause).append(" ");
}

if (orderBy != null) {
if (!orderBy.isEmpty()) {
sql.append("ORDER BY ").append(orderBy).append(" ");
}

if (limit != null) {
if (!groupBy.isEmpty()) {
sql.append("GROUP BY ").append(groupBy).append(" ");
}

if (!limit.isEmpty()) {
sql.append("LIMIT ").append(limit).append(" ");
}

if (offset != null) {
if (!offset.isEmpty()) {
sql.append("OFFSET ").append(offset).append(" ");
}

Expand All @@ -170,7 +181,7 @@ private String[] convertArgs(List<Object> argsList) {
String[] argsArray = new String[argsList.size()];

for (int i = 0; i < argsList.size(); i++) {
argsArray[i] = argsList.get(i).toString();
argsArray[i] = argsList.get(i).toString();
}

return argsArray;
Expand All @@ -185,4 +196,4 @@ public Iterator<T> iterator() {
return SugarRecord.findAsIterator(record, whereClause, arguments, groupBy, orderBy, limit);
}

}
}
30 changes: 29 additions & 1 deletion library/src/test/java/com/orm/query/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ public void testWhere(){
assertEquals("2", where.getArgs()[1]);
}

@Test
public void toSqlAllClauses(){
String toSql = Select.from(TestRecord.class)
.where("test")
.groupBy("test")
.orderBy("test")
.limit("test")
.offset("test")
.toSql();
assertEquals("SELECT * FROM TEST_RECORD WHERE test ORDER BY test GROUP BY test LIMIT test OFFSET test ", toSql);
}

@Test
public void toSqlNoClauses(){
String toSql = Select.from(TestRecord.class)
.toSql();
assertEquals("SELECT * FROM TEST_RECORD ", toSql);
}

@Test
public void toSqlWhereLimitClauses(){
String toSql = Select.from(TestRecord.class)
.where("test")
.limit("test")
.toSql();
assertEquals("SELECT * FROM TEST_RECORD WHERE test LIMIT test ", toSql);
}


@Test
public void testWhereOr(){
Expand Down Expand Up @@ -101,4 +129,4 @@ public void testIsNotNull() {
assertEquals("(test IS NOT NULL )", where.getWhereCond());
assertEquals(0, where.getArgs().length);
}
}
}