1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.database.sqlite.SQLiteQueryBuilder;
4 import com.xtremelabs.robolectric.internal.Implementation;
5 import com.xtremelabs.robolectric.internal.Implements;
6 import com.xtremelabs.robolectric.util.Join;
7 
8 /**
9  * Shadow for {@code SQLiteQueryBuilder}.
10  */
11 @Implements(SQLiteQueryBuilder.class)
12 public class ShadowSQLiteQueryBuilder {
13 
14     @Implementation
buildQueryString(boolean distinct, String tables, String[] columns, String where, String groupBy, String having, String orderBy, String limit)15     public static String buildQueryString(boolean distinct, String tables,
16                                           String[] columns, String where, String groupBy, String having,
17                                           String orderBy, String limit) {
18 
19         StringBuilder sb = new StringBuilder("SELECT ");
20 
21         if (distinct) {
22             sb.append("DISTINCT ");
23         }
24 
25         if (columns != null) {
26             sb.append(Join.join(", ", (Object[]) columns));
27         } else {
28             sb.append("*");
29         }
30 
31         sb.append(" FROM ");
32         sb.append(tables);
33 
34         conditionallyAppend(sb, " WHERE ", where);
35         conditionallyAppend(sb, " GROUP BY ", groupBy);
36         conditionallyAppend(sb, " HAVING ", having);
37         conditionallyAppend(sb, " ORDER BY ", orderBy);
38         conditionallyAppend(sb, " LIMIT ", limit);
39 
40         return sb.toString();
41     }
42 
conditionallyAppend(StringBuilder sb, String keyword, String value)43     private static void conditionallyAppend(StringBuilder sb, String keyword, String value) {
44         if (value != null) {
45             sb.append(keyword);
46             sb.append(value);
47         }
48     }
49 
50 }
51