package org.greenrobot.greendao.query; import java.util.ArrayList; import java.util.List; import org.greenrobot.greendao.AbstractDao; import org.greenrobot.greendao.DaoLog; import org.greenrobot.greendao.Property; import org.greenrobot.greendao.internal.SqlUtils; /* loaded from: classes2.dex */ public class QueryBuilder { public static boolean k; public static boolean l; private final WhereCollector a; private StringBuilder b; private final List c; private final List> d; private final AbstractDao e; private final String f; private Integer g; private Integer h; private boolean i; private String j; protected QueryBuilder(AbstractDao abstractDao) { this(abstractDao, "T"); } public static QueryBuilder a(AbstractDao abstractDao) { return new QueryBuilder<>(abstractDao); } private void d() { StringBuilder sb = this.b; if (sb == null) { this.b = new StringBuilder(); } else if (sb.length() > 0) { this.b.append(","); } } private StringBuilder e() { StringBuilder sb = new StringBuilder(SqlUtils.a(this.e.h(), this.f, this.e.c(), this.i)); a(sb, this.f); StringBuilder sb2 = this.b; if (sb2 != null && sb2.length() > 0) { sb.append(" ORDER BY "); sb.append((CharSequence) this.b); } return sb; } public QueryBuilder b(Property... propertyArr) { a(" DESC", propertyArr); return this; } public T c() { return a().d(); } protected QueryBuilder(AbstractDao abstractDao, String str) { this.e = abstractDao; this.f = str; this.c = new ArrayList(); this.d = new ArrayList(); this.a = new WhereCollector<>(abstractDao, str); this.j = " COLLATE NOCASE"; } private int b(StringBuilder sb) { if (this.h == null) { return -1; } if (this.g == null) { throw new IllegalStateException("Offset cannot be set without limit"); } sb.append(" OFFSET ?"); this.c.add(this.h); return this.c.size() - 1; } public QueryBuilder a(WhereCondition whereCondition, WhereCondition... whereConditionArr) { this.a.a(whereCondition, whereConditionArr); return this; } public Join a(Class cls, Property property) { return a(this.e.e(), cls, property); } public Join a(Property property, Class cls, Property property2) { return a(this.f, property, this.e.g().a((Class) cls), property2); } private Join a(String str, Property property, AbstractDao abstractDao, Property property2) { Join join = new Join<>(str, property, abstractDao, property2, "J" + (this.d.size() + 1)); this.d.add(join); return join; } public List b() { return a().c(); } public QueryBuilder a(Property... propertyArr) { a(" ASC", propertyArr); return this; } private void a(String str, Property... propertyArr) { String str2; for (Property property : propertyArr) { d(); a(this.b, property); if (String.class.equals(property.b) && (str2 = this.j) != null) { this.b.append(str2); } this.b.append(str); } } protected StringBuilder a(StringBuilder sb, Property property) { this.a.a(property); sb.append(this.f); sb.append('.'); sb.append('\''); sb.append(property.e); sb.append('\''); return sb; } public QueryBuilder a(int i) { this.g = Integer.valueOf(i); return this; } public Query a() { StringBuilder e = e(); int a = a(e); int b = b(e); String sb = e.toString(); a(sb); return Query.a(this.e, sb, this.c.toArray(), a, b); } private int a(StringBuilder sb) { if (this.g == null) { return -1; } sb.append(" LIMIT ?"); this.c.add(this.g); return this.c.size() - 1; } private void a(String str) { if (k) { DaoLog.a("Built SQL for query: " + str); } if (l) { DaoLog.a("Values for query: " + this.c); } } private void a(StringBuilder sb, String str) { this.c.clear(); for (Join join : this.d) { sb.append(" JOIN "); sb.append('\"'); sb.append(join.b.h()); sb.append('\"'); sb.append(' '); sb.append(join.e); sb.append(" ON "); SqlUtils.a(sb, join.a, join.c); sb.append('='); SqlUtils.a(sb, join.e, join.d); } boolean z = !this.a.a(); if (z) { sb.append(" WHERE "); this.a.a(sb, str, this.c); } for (Join join2 : this.d) { if (!join2.f.a()) { if (!z) { sb.append(" WHERE "); z = true; } else { sb.append(" AND "); } join2.f.a(sb, join2.e, this.c); } } } }