Skip to content

Commit da0cae4

Browse files
committed
added escapeBlobArgument, just checking a work around for byte[] selection arg in testEqByteArray
1 parent acc1a80 commit da0cae4

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

DaoCore/src/main/java/de/greenrobot/dao/internal/SqlUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
/** Helper class to create SQL statements as used by greenDAO internally. */
2222
public class SqlUtils {
23+
private final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
2324

2425
public static StringBuilder appendProperty(StringBuilder builder, String tablePrefix, Property property) {
2526
if (tablePrefix != null) {
@@ -146,4 +147,13 @@ public static String createSqlUpdate(String tablename, String[] updateColumns, S
146147
return builder.toString();
147148
}
148149

150+
public static String escapeBlobArgument(byte[] bytes) {
151+
char[] hexChars = new char[bytes.length * 2];
152+
for (int i = 0; i < bytes.length; i++) {
153+
int byteValue = bytes[i] & 0xFF;
154+
hexChars[i * 2] = HEX_ARRAY[byteValue >>> 4];
155+
hexChars[i * 2 + 1] = HEX_ARRAY[byteValue & 0x0F];
156+
}
157+
return "X'" + new String(hexChars) + '\'';
158+
}
149159
}

DaoTest/src/de/greenrobot/daotest/query/QueryBuilderSimpleTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
import java.util.Date;
2222
import java.util.List;
2323

24+
import de.greenrobot.dao.internal.SqlUtils;
2425
import de.greenrobot.dao.query.Query;
2526
import de.greenrobot.dao.query.QueryBuilder;
27+
import de.greenrobot.dao.query.WhereCondition;
2628
import de.greenrobot.daotest.TestEntity;
27-
import de.greenrobot.daotest.TestEntityDao;
2829
import de.greenrobot.daotest.TestEntityDao.Properties;
2930
import de.greenrobot.daotest.entity.TestEntityTestBase;
3031

@@ -172,20 +173,23 @@ public void testEqBoolean() {
172173
}
173174

174175
// TODO fix byte arrays? Android is doing String args everywhere
175-
public void _testEqByteArray() {
176+
public void testEqByteArray() {
176177
ArrayList<TestEntity> inserted = insert(3);
177178
TestEntity testEntity = inserted.get(1);
178179

179180
byte[] byteArray = {96, 77, 37, -21};
180181
testEntity.setSimpleByteArray(byteArray);
181182
dao.update(testEntity);
182183

183-
Query<TestEntity> queryBoolean = dao.queryBuilder().where(Properties.SimpleByteArray.eq(byteArray)).build();
184-
TestEntity testEntity2 = queryBoolean.uniqueOrThrow();
184+
// Unsupported: Query<TestEntity> query = dao.queryBuilder().where(Properties.SimpleByteArray.eq(byteArray)).build();
185+
String conditionString = Properties.SimpleByteArray.columnName + '=' + SqlUtils.escapeBlobArgument(byteArray);
186+
WhereCondition condition = new WhereCondition.StringCondition(conditionString);
187+
Query<TestEntity> query = dao.queryBuilder().where(condition).build();
188+
TestEntity testEntity2 = query.uniqueOrThrow();
185189
assertEquals(testEntity.getId(), testEntity2.getId());
186190

187-
queryBoolean.setParameter(0, new byte[]{96, 77, 37, -21, 99});
188-
assertNull(queryBoolean.unique());
191+
// Unsupported: query.setParameter(0, new byte[]{96, 77, 37, -21, 99});
192+
// Unsupported: assertNull(query.unique());
189193
}
190194

191195
public void testIsNullIsNotNull() {

0 commit comments

Comments
 (0)