From 34a6404008e0dacc51642cf7f35c8af731bcf598 Mon Sep 17 00:00:00 2001 From: Bruno de Lima Date: Mon, 22 Sep 2014 15:58:36 -0300 Subject: [PATCH 1/4] Function calls. --- src/com/activeandroid/query/From.java | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/com/activeandroid/query/From.java b/src/com/activeandroid/query/From.java index ab3837a90..40e8fd2cf 100644 --- a/src/com/activeandroid/query/From.java +++ b/src/com/activeandroid/query/From.java @@ -343,4 +343,35 @@ public String[] getArguments() { return args; } + + + public int avg(String column) { + return SQLiteUtils.intQuery(toFunctiongSql("AVG", column), getArguments()); + } + public int max(String column) { + return SQLiteUtils.intQuery(toFunctiongSql("max", column), getArguments()); + } + public int min(String column) { + return SQLiteUtils.intQuery(toFunctiongSql("min", column), getArguments()); + } + + private String toFunctiongSql(String functionName, String column) { + + final StringBuilder sql = new StringBuilder(); + sql.append("SELECT "); + sql.append(functionName); + sql.append("("); + sql.append(column); + sql.append(") "); + + addFrom(sql); + addJoins(sql); + addWhere(sql); + addGroupBy(sql); + addHaving(sql); + addLimit(sql); + addOffset(sql); + + return sqlString(sql); + } } From 249bfc83bd4e4241f3ee5262381d411f403e763b Mon Sep 17 00:00:00 2001 From: Bruno de Lima Date: Mon, 22 Sep 2014 16:01:48 -0300 Subject: [PATCH 2/4] Correct function name --- src/com/activeandroid/query/From.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/com/activeandroid/query/From.java b/src/com/activeandroid/query/From.java index 40e8fd2cf..7ad46ca23 100644 --- a/src/com/activeandroid/query/From.java +++ b/src/com/activeandroid/query/From.java @@ -346,16 +346,16 @@ public String[] getArguments() { public int avg(String column) { - return SQLiteUtils.intQuery(toFunctiongSql("AVG", column), getArguments()); + return SQLiteUtils.intQuery(toFunctionSql("avg", column), getArguments()); } public int max(String column) { - return SQLiteUtils.intQuery(toFunctiongSql("max", column), getArguments()); + return SQLiteUtils.intQuery(toFunctionSql("max", column), getArguments()); } public int min(String column) { - return SQLiteUtils.intQuery(toFunctiongSql("min", column), getArguments()); + return SQLiteUtils.intQuery(toFunctionSql("min", column), getArguments()); } - private String toFunctiongSql(String functionName, String column) { + private String toFunctionSql(String functionName, String column) { final StringBuilder sql = new StringBuilder(); sql.append("SELECT "); From b7759e968de701400f82d9c44585c4fb9ad2bfef Mon Sep 17 00:00:00 2001 From: Bruno de Lima Date: Tue, 23 Sep 2014 11:10:12 -0300 Subject: [PATCH 3/4] Function to get last --- src/com/activeandroid/query/From.java | 283 ++++++++++++++------------ 1 file changed, 155 insertions(+), 128 deletions(-) diff --git a/src/com/activeandroid/query/From.java b/src/com/activeandroid/query/From.java index 7ad46ca23..4f80da14e 100644 --- a/src/com/activeandroid/query/From.java +++ b/src/com/activeandroid/query/From.java @@ -29,63 +29,63 @@ import java.util.List; public final class From implements Sqlable { - private Sqlable mQueryBase; - - private Class mType; - private String mAlias; - private List mJoins; - private final StringBuilder mWhere = new StringBuilder(); - private String mGroupBy; - private String mHaving; - private String mOrderBy; - private String mLimit; - private String mOffset; - - private List mArguments; - - public From(Class table, Sqlable queryBase) { - mType = table; - mJoins = new ArrayList(); - mQueryBase = queryBase; - - mJoins = new ArrayList(); - mArguments = new ArrayList(); - } - - public From as(String alias) { - mAlias = alias; - return this; - } - - public Join join(Class table) { - Join join = new Join(this, table, null); - mJoins.add(join); - return join; - } - - public Join leftJoin(Class table) { - Join join = new Join(this, table, JoinType.LEFT); - mJoins.add(join); - return join; - } - - public Join outerJoin(Class table) { - Join join = new Join(this, table, JoinType.OUTER); - mJoins.add(join); - return join; - } - - public Join innerJoin(Class table) { - Join join = new Join(this, table, JoinType.INNER); - mJoins.add(join); - return join; - } - - public Join crossJoin(Class table) { - Join join = new Join(this, table, JoinType.CROSS); - mJoins.add(join); - return join; - } + private Sqlable mQueryBase; + + private Class mType; + private String mAlias; + private List mJoins; + private final StringBuilder mWhere = new StringBuilder(); + private String mGroupBy; + private String mHaving; + private String mOrderBy; + private String mLimit; + private String mOffset; + + private List mArguments; + + public From(Class table, Sqlable queryBase) { + mType = table; + mJoins = new ArrayList(); + mQueryBase = queryBase; + + mJoins = new ArrayList(); + mArguments = new ArrayList(); + } + + public From as(String alias) { + mAlias = alias; + return this; + } + + public Join join(Class table) { + Join join = new Join(this, table, null); + mJoins.add(join); + return join; + } + + public Join leftJoin(Class table) { + Join join = new Join(this, table, JoinType.LEFT); + mJoins.add(join); + return join; + } + + public Join outerJoin(Class table) { + Join join = new Join(this, table, JoinType.OUTER); + mJoins.add(join); + return join; + } + + public Join innerJoin(Class table) { + Join join = new Join(this, table, JoinType.INNER); + mJoins.add(join); + return join; + } + + public Join crossJoin(Class table) { + Join join = new Join(this, table, JoinType.CROSS); + mJoins.add(join); + return join; + } public From where(String clause) { // Chain conditions if a previous condition exists. @@ -121,48 +121,48 @@ public From or(String clause, Object... args) { or(clause).addArguments(args); return this; } - - public From groupBy(String groupBy) { - mGroupBy = groupBy; - return this; - } - - public From having(String having) { - mHaving = having; - return this; - } - - public From orderBy(String orderBy) { - mOrderBy = orderBy; - return this; - } - - public From limit(int limit) { - return limit(String.valueOf(limit)); - } - - public From limit(String limit) { - mLimit = limit; - return this; - } - - public From offset(int offset) { - return offset(String.valueOf(offset)); - } - - public From offset(String offset) { - mOffset = offset; - return this; - } - - void addArguments(Object[] args) { - for(Object arg : args) { + + public From groupBy(String groupBy) { + mGroupBy = groupBy; + return this; + } + + public From having(String having) { + mHaving = having; + return this; + } + + public From orderBy(String orderBy) { + mOrderBy = orderBy; + return this; + } + + public From limit(int limit) { + return limit(String.valueOf(limit)); + } + + public From limit(String limit) { + mLimit = limit; + return this; + } + + public From offset(int offset) { + return offset(String.valueOf(offset)); + } + + public From offset(String offset) { + mOffset = offset; + return this; + } + + void addArguments(Object[] args) { + for (Object arg : args) { if (arg.getClass() == boolean.class || arg.getClass() == Boolean.class) { arg = (arg.equals(true) ? 1 : 0); } mArguments.add(arg); } - } + } private void addFrom(final StringBuilder sql) { sql.append("FROM "); @@ -293,33 +293,34 @@ public String toCountSql() { return sqlString(sql); } - public List execute() { - if (mQueryBase instanceof Select) { - return SQLiteUtils.rawQuery(mType, toSql(), getArguments()); - - } else { - SQLiteUtils.execSql(toSql(), getArguments()); - Cache.getContext().getContentResolver().notifyChange(ContentProvider.createUri(mType, null), null); - return null; - - } - } - - public T executeSingle() { - if (mQueryBase instanceof Select) { - limit(1); - return (T) SQLiteUtils.rawQuerySingle(mType, toSql(), getArguments()); - - } else { - limit(1); - SQLiteUtils.rawQuerySingle(mType, toSql(), getArguments()).delete(); - return null; - - } - } - + public List execute() { + if (mQueryBase instanceof Select) { + return SQLiteUtils.rawQuery(mType, toSql(), getArguments()); + + } else { + SQLiteUtils.execSql(toSql(), getArguments()); + Cache.getContext().getContentResolver().notifyChange(ContentProvider.createUri(mType, null), null); + return null; + + } + } + + public T executeSingle() { + if (mQueryBase instanceof Select) { + limit(1); + return (T) SQLiteUtils.rawQuerySingle(mType, toSql(), getArguments()); + + } else { + limit(1); + SQLiteUtils.rawQuerySingle(mType, toSql(), getArguments()).delete(); + return null; + + } + } + /** * Gets a value indicating whether the query returns any rows. + * * @return true if the query returns at least one row; otherwise, false. */ public boolean exists() { @@ -333,30 +334,31 @@ public int count() { return SQLiteUtils.intQuery(toCountSql(), getArguments()); } - public String[] getArguments() { - final int size = mArguments.size(); - final String[] args = new String[size]; + public String[] getArguments() { + final int size = mArguments.size(); + final String[] args = new String[size]; - for (int i = 0; i < size; i++) { - args[i] = mArguments.get(i).toString(); - } + for (int i = 0; i < size; i++) { + args[i] = mArguments.get(i).toString(); + } - return args; - } + return args; + } public int avg(String column) { return SQLiteUtils.intQuery(toFunctionSql("avg", column), getArguments()); } + public int max(String column) { return SQLiteUtils.intQuery(toFunctionSql("max", column), getArguments()); } + public int min(String column) { return SQLiteUtils.intQuery(toFunctionSql("min", column), getArguments()); } private String toFunctionSql(String functionName, String column) { - final StringBuilder sql = new StringBuilder(); sql.append("SELECT "); sql.append(functionName); @@ -374,4 +376,29 @@ private String toFunctionSql(String functionName, String column) { return sqlString(sql); } + + public T last() { + final StringBuilder sql = new StringBuilder(); + sql.append(mQueryBase.toSql()); + + addFrom(sql); + addJoins(sql); + addWhere(sql); + addGroupBy(sql); + addHaving(sql); + + sql.append("ORDER BY "); + if (mOrderBy == null) + sql.append(1); + else + sql.append(mOrderBy); + sql.append(" DESC "); + +// addLimit(sql); + sql.append("LIMIT "); + sql.append(1); + sql.append(" "); +// addOffset(sql); + return (T) SQLiteUtils.rawQuerySingle(mType, sql.toString(), getArguments()); + } } From 61076ccb4896b81e8e774d7bc07059d0a32cba40 Mon Sep 17 00:00:00 2001 From: Bruno de Lima Date: Thu, 16 Oct 2014 17:46:54 -0300 Subject: [PATCH 4/4] Method to get first object. --- src/com/activeandroid/query/From.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/com/activeandroid/query/From.java b/src/com/activeandroid/query/From.java index 4f80da14e..31a10851f 100644 --- a/src/com/activeandroid/query/From.java +++ b/src/com/activeandroid/query/From.java @@ -378,6 +378,16 @@ private String toFunctionSql(String functionName, String column) { } public T last() { + String mode = " DESC "; + return getSingleOrdered(mode); + } + + public T first() { + String mode = " ACS "; + return getSingleOrdered(mode); + } + + private T getSingleOrdered(String mode) { final StringBuilder sql = new StringBuilder(); sql.append(mQueryBase.toSql()); @@ -392,7 +402,7 @@ public T last() { sql.append(1); else sql.append(mOrderBy); - sql.append(" DESC "); + sql.append(mode); // addLimit(sql); sql.append("LIMIT ");