Skip to content

Commit 0b65e85

Browse files
committed
sketching another alternative to byte[] queries
1 parent b94b223 commit 0b65e85

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,16 @@ public static String createSqlUpdate(String tablename, String[] updateColumns, S
148148
}
149149

150150
public static String escapeBlobArgument(byte[] bytes) {
151+
return "X'" + toHex(bytes) + '\'';
152+
}
153+
154+
public static String toHex(byte[] bytes) {
151155
char[] hexChars = new char[bytes.length * 2];
152156
for (int i = 0; i < bytes.length; i++) {
153157
int byteValue = bytes[i] & 0xFF;
154158
hexChars[i * 2] = HEX_ARRAY[byteValue >>> 4];
155159
hexChars[i * 2 + 1] = HEX_ARRAY[byteValue & 0x0F];
156160
}
157-
return "X'" + new String(hexChars) + '\'';
161+
return new String(hexChars);
158162
}
159163
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ public void testEqByteArray() {
182182
dao.update(testEntity);
183183

184184
// Unsupported: Query<TestEntity> query = dao.queryBuilder().where(Properties.SimpleByteArray.eq(byteArray)).build();
185+
186+
// Works, but probably voids any index on BLOBs (Note: there's no hex2blob function and X'?' is bad syntax):
187+
// String conditionString = "HEX(" + Properties.SimpleByteArray.columnName + ")=?";
188+
// WhereCondition condition = new WhereCondition.StringCondition(conditionString, SqlUtils.toHex(byteArray));
189+
185190
String conditionString = Properties.SimpleByteArray.columnName + '=' + SqlUtils.escapeBlobArgument(byteArray);
186191
WhereCondition condition = new WhereCondition.StringCondition(conditionString);
187192
Query<TestEntity> query = dao.queryBuilder().where(condition).build();

0 commit comments

Comments
 (0)