Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 0fdb994

Browse files
Sam1301timabbott
authored andcommitted
Use ormlite queryRaw instead of queryBuilder.
Ormlite queryBuilder gives wrong results for equality occasionly. So, a raw sql query should be used instead.
1 parent 6f3f92c commit 0fdb994

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

app/src/main/java/com/zulip/android/models/Person.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import com.google.gson.annotations.SerializedName;
44
import com.j256.ormlite.dao.Dao;
5+
import com.j256.ormlite.dao.GenericRawResults;
6+
import com.j256.ormlite.dao.RawRowMapper;
57
import com.j256.ormlite.dao.RuntimeExceptionDao;
68
import com.j256.ormlite.field.DatabaseField;
7-
import com.j256.ormlite.stmt.SelectArg;
89
import com.j256.ormlite.table.DatabaseTable;
910
import com.zulip.android.ZulipApp;
1011
import com.zulip.android.util.ZLog;
1112

1213
import org.apache.commons.lang.builder.HashCodeBuilder;
1314

15+
import java.io.IOException;
1416
import java.sql.SQLException;
1517
import java.util.Collections;
1618
import java.util.Comparator;
@@ -73,6 +75,15 @@ public Person(String name, String email, String avatarURL) {
7375
this.isActive = false;
7476
}
7577

78+
public Person(int id, String name, String email, String avatarURL, boolean isBot, boolean isActive) {
79+
this.setId(id);
80+
this.setName(name);
81+
this.setEmail(email);
82+
this.setAvatarURL(avatarURL);
83+
this.setActive(isActive);
84+
this.setBot(isBot);
85+
}
86+
7687
/**
7788
* Construct an empty Person object.
7889
*/
@@ -163,16 +174,42 @@ public static Person getByEmail(ZulipApp app, String email) {
163174
return null;
164175
}
165176

177+
@SuppressWarnings("WeakerAccess")
166178
public static Person getByEmail(Dao<Person, ?> dao, String email) {
167179
try {
168-
return dao.queryBuilder().where()
169-
.eq(Person.EMAIL_FIELD, new SelectArg(email.toLowerCase()))
170-
.queryForFirst();
180+
// Using raw query to avoid errors with queryBuilder in ormlite.
181+
// Listing of column names is necessary to avoid haphazard ordering of attributes
182+
// in rawResults.
183+
GenericRawResults<Person> rawResults =
184+
dao.queryRaw(
185+
"select " + Person.ID_FIELD + "," + Person.NAME_FIELD + ","
186+
+ Person.EMAIL_FIELD + "," + Person.AVATARURL_FIELD + ","
187+
+ Person.ISBOT_FIELD + "," + Person.ISACTIVE_FIELD + " from " + "people"
188+
+ " where " + Person.EMAIL_FIELD + " = ? ;",
189+
new RawRowMapper<Person>() {
190+
public Person mapRow(String[] columnNames,
191+
String[] resultColumns) {
192+
return new Person(Integer.parseInt(resultColumns[0]),
193+
resultColumns[1], resultColumns[2], resultColumns[3],
194+
Boolean.parseBoolean(resultColumns[4]),
195+
Boolean.parseBoolean(resultColumns[5]));
196+
}
197+
}, email.toLowerCase());
198+
199+
// we only care about the first result
200+
Person returnValue = rawResults.getFirstResult();
201+
rawResults.close();
202+
return returnValue;
171203
} catch (SQLException e) {
172204
throw new RuntimeException(e);
205+
} catch (IOException e) {
206+
ZLog.logException(e);
173207
}
208+
209+
return null;
174210
}
175211

212+
@SuppressWarnings("WeakerAccess")
176213
public static Person getOrUpdate(ZulipApp app, String email, String name,
177214
String avatarURL, Map<String, Person> personCache) {
178215

@@ -214,6 +251,11 @@ public static Person getOrUpdate(ZulipApp app, String email, String name,
214251
return getOrUpdate(app, email, name, avatarURL, null);
215252
}
216253

254+
@SuppressWarnings("WeakerAccess")
255+
public void setBot(boolean isBot) {
256+
this.isBot = isBot;
257+
}
258+
217259
public void setActive(boolean active) {
218260
isActive = active;
219261
}

0 commit comments

Comments
 (0)