use of io.realm.RealmAny in project realm-java by realm.
the class JNIQueryTest method queryWithWrongDataType.
@Test
public void queryWithWrongDataType() {
Table table = TestHelper.createTableWithAllColumnTypes(sharedRealm);
String[] columnKeys = new String[] { "binary", "boolean", "date", "double", "float", "long", "string" };
// Queries the table.
TableQuery query = table.where();
RealmAny L123 = RealmAny.valueOf(123);
RealmAny L321 = RealmAny.valueOf(321);
RealmAny F123 = RealmAny.valueOf(123.5F);
RealmAny F321 = RealmAny.valueOf(321.5F);
RealmAny D123 = RealmAny.valueOf(123.5D);
RealmAny D321 = RealmAny.valueOf(321.5D);
RealmAny date = RealmAny.valueOf(new Date());
// Compares integer in non integer columns.
for (int i = 0; i <= 6; i++) {
if ((i != 5) && (i != 1) && (i != 3) && (i != 4)) {
try {
query.equalTo(null, columnKeys[i], L123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.notEqualTo(null, columnKeys[i], L123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.lessThan(null, columnKeys[i], L123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.lessThanOrEqual(null, columnKeys[i], L123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.greaterThan(null, columnKeys[i], L123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.greaterThanOrEqual(null, columnKeys[i], L123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.between(null, columnKeys[i], L123, L321).find();
fail();
} catch (IllegalArgumentException ignore) {
}
}
}
// Compares float in non float columns.
for (int i = 0; i <= 6; i++) {
if ((i != 5) && (i != 1) && (i != 3) && (i != 4)) {
try {
query.equalTo(null, columnKeys[i], F123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.notEqualTo(null, columnKeys[i], F123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.lessThan(null, columnKeys[i], F123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.lessThanOrEqual(null, columnKeys[i], F123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.greaterThan(null, columnKeys[i], F123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.greaterThanOrEqual(null, columnKeys[i], F123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.between(null, columnKeys[i], F123, F321).find();
fail();
} catch (IllegalArgumentException ignore) {
}
}
}
// Compares double in non double columns.
for (int i = 0; i <= 6; i++) {
if ((i != 5) && (i != 1) && (i != 3) && (i != 4)) {
try {
query.equalTo(null, columnKeys[i], D123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.notEqualTo(null, columnKeys[i], D123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.lessThan(null, columnKeys[i], D123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.lessThanOrEqual(null, columnKeys[i], D123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.greaterThan(null, columnKeys[i], D123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.greaterThanOrEqual(null, columnKeys[i], D123).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.between(null, columnKeys[i], D123, D321).find();
fail();
} catch (IllegalArgumentException ignore) {
}
}
}
// Compares boolean in non boolean columns.
for (int i = 0; i <= 6; i++) {
if ((i != 5) && (i != 1) && (i != 3) && (i != 4)) {
try {
query.equalTo(null, columnKeys[i], RealmAny.valueOf(true)).find();
fail();
} catch (IllegalArgumentException ignore) {
}
}
}
// Compares date.
for (int i = 0; i <= 6; i++) {
if (i != 2) {
try {
query.equalTo(null, columnKeys[i], date).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.lessThan(null, columnKeys[i], date).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.lessThanOrEqual(null, columnKeys[i], date).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.greaterThan(null, columnKeys[i], date).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.greaterThanOrEqual(null, columnKeys[i], date).find();
fail();
} catch (IllegalArgumentException ignore) {
}
try {
query.between(null, columnKeys[i], date, date).find();
fail();
} catch (IllegalArgumentException ignore) {
}
}
}
}
use of io.realm.RealmAny in project realm-java by realm.
the class JNIQueryTest method objectIdQuery.
@Test
public void objectIdQuery() throws Exception {
final RealmAny one = RealmAny.valueOf(new ObjectId(new Date(10)));
final RealmAny two = RealmAny.valueOf(new ObjectId(new Date(20)));
final RealmAny three = RealmAny.valueOf(new ObjectId(new Date(30)));
final AtomicLong columnKey = new AtomicLong(-1);
Table table = TestHelper.createTable(sharedRealm, "temp", new TestHelper.AdditionalTableSetup() {
@Override
public void execute(Table table) {
columnKey.set(table.addColumn(RealmFieldType.OBJECT_ID, "objectid"));
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { one.asObjectId() });
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { two.asObjectId() });
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { three.asObjectId() });
}
});
assertEquals(1L, table.where().equalTo(null, "objectid", one).count());
assertEquals(2L, table.where().notEqualTo(null, "objectid", one).count());
assertEquals(0L, table.where().lessThan(null, "objectid", one).count());
assertEquals(1L, table.where().lessThanOrEqual(null, "objectid", one).count());
assertEquals(2L, table.where().greaterThan(null, "objectid", one).count());
assertEquals(3L, table.where().greaterThanOrEqual(null, "objectid", one).count());
assertEquals(1L, table.where().equalTo(null, "objectid", two).count());
assertEquals(2L, table.where().notEqualTo(null, "objectid", two).count());
assertEquals(1L, table.where().lessThan(null, "objectid", two).count());
assertEquals(2L, table.where().lessThanOrEqual(null, "objectid", two).count());
assertEquals(1L, table.where().greaterThan(null, "objectid", two).count());
assertEquals(2L, table.where().greaterThanOrEqual(null, "objectid", two).count());
assertEquals(1L, table.where().equalTo(null, "objectid", three).count());
assertEquals(2L, table.where().notEqualTo(null, "objectid", three).count());
assertEquals(2L, table.where().lessThan(null, "objectid", three).count());
assertEquals(3L, table.where().lessThanOrEqual(null, "objectid", three).count());
assertEquals(0L, table.where().greaterThan(null, "objectid", three).count());
assertEquals(1L, table.where().greaterThanOrEqual(null, "objectid", three).count());
}
use of io.realm.RealmAny in project realm-java by realm.
the class JNIQueryTest method dateQuery.
@Test
public void dateQuery() throws Exception {
final RealmAny past = RealmAny.valueOf(new Date(TimeUnit.SECONDS.toMillis(Integer.MIN_VALUE - 100L)));
final RealmAny future = RealmAny.valueOf(new Date(TimeUnit.SECONDS.toMillis(Integer.MAX_VALUE + 1L)));
final RealmAny distantPast = RealmAny.valueOf(new Date(Long.MIN_VALUE));
final RealmAny distantFuture = RealmAny.valueOf(new Date(Long.MAX_VALUE));
final RealmAny date0 = RealmAny.valueOf(new Date(0));
final RealmAny date10000 = RealmAny.valueOf(new Date(10000));
final AtomicLong columnKey = new AtomicLong(-1);
Table table = TestHelper.createTable(sharedRealm, "temp", new TestHelper.AdditionalTableSetup() {
@Override
public void execute(Table table) {
columnKey.set(table.addColumn(RealmFieldType.DATE, "date"));
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { new Date(10000) });
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { new Date(0) });
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { new Date(1000) });
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { future.asDate() });
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { distantFuture.asDate() });
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { past.asDate() });
TestHelper.addRowWithValues(table, new long[] { columnKey.get() }, new Object[] { distantPast.asDate() });
}
});
assertEquals(1L, table.where().equalTo(null, "date", distantPast).count());
assertEquals(6L, table.where().notEqualTo(null, "date", distantPast).count());
assertEquals(0L, table.where().lessThan(null, "date", distantPast).count());
assertEquals(1L, table.where().lessThanOrEqual(null, "date", distantPast).count());
assertEquals(6L, table.where().greaterThan(null, "date", distantPast).count());
assertEquals(7L, table.where().greaterThanOrEqual(null, "date", distantPast).count());
assertEquals(1L, table.where().equalTo(null, "date", past).count());
assertEquals(6L, table.where().notEqualTo(null, "date", past).count());
assertEquals(1L, table.where().lessThan(null, "date", past).count());
assertEquals(2L, table.where().lessThanOrEqual(null, "date", past).count());
assertEquals(5L, table.where().greaterThan(null, "date", past).count());
assertEquals(6L, table.where().greaterThanOrEqual(null, "date", past).count());
assertEquals(1L, table.where().equalTo(null, "date", date0).count());
assertEquals(6L, table.where().notEqualTo(null, "date", date0).count());
assertEquals(2L, table.where().lessThan(null, "date", date0).count());
assertEquals(3L, table.where().lessThanOrEqual(null, "date", date0).count());
assertEquals(4L, table.where().greaterThan(null, "date", date0).count());
assertEquals(5L, table.where().greaterThanOrEqual(null, "date", date0).count());
assertEquals(1L, table.where().equalTo(null, "date", future).count());
assertEquals(6L, table.where().notEqualTo(null, "date", future).count());
assertEquals(5L, table.where().lessThan(null, "date", future).count());
assertEquals(6L, table.where().lessThanOrEqual(null, "date", future).count());
assertEquals(1L, table.where().greaterThan(null, "date", future).count());
assertEquals(2L, table.where().greaterThanOrEqual(null, "date", future).count());
assertEquals(1L, table.where().equalTo(null, "date", distantFuture).count());
assertEquals(6L, table.where().notEqualTo(null, "date", distantFuture).count());
assertEquals(6L, table.where().lessThan(null, "date", distantFuture).count());
assertEquals(7L, table.where().lessThanOrEqual(null, "date", distantFuture).count());
assertEquals(0L, table.where().greaterThan(null, "date", distantFuture).count());
assertEquals(1L, table.where().greaterThanOrEqual(null, "date", distantFuture).count());
// between
assertEquals(1L, table.where().between(null, "date", distantPast, distantPast).count());
assertEquals(2L, table.where().between(null, "date", distantPast, past).count());
assertEquals(3L, table.where().between(null, "date", distantPast, date0).count());
assertEquals(5L, table.where().between(null, "date", distantPast, date10000).count());
assertEquals(6L, table.where().between(null, "date", distantPast, future).count());
assertEquals(7L, table.where().between(null, "date", distantPast, distantFuture).count());
assertEquals(0L, table.where().between(null, "date", past, distantPast).count());
assertEquals(1L, table.where().between(null, "date", past, past).count());
assertEquals(2L, table.where().between(null, "date", past, date0).count());
assertEquals(4L, table.where().between(null, "date", past, date10000).count());
assertEquals(5L, table.where().between(null, "date", past, future).count());
assertEquals(6L, table.where().between(null, "date", past, distantFuture).count());
assertEquals(0L, table.where().between(null, "date", date0, distantPast).count());
assertEquals(0L, table.where().between(null, "date", date0, past).count());
assertEquals(1L, table.where().between(null, "date", date0, date0).count());
assertEquals(3L, table.where().between(null, "date", date0, date10000).count());
assertEquals(4L, table.where().between(null, "date", date0, future).count());
assertEquals(5L, table.where().between(null, "date", date0, distantFuture).count());
assertEquals(0L, table.where().between(null, "date", date10000, distantPast).count());
assertEquals(0L, table.where().between(null, "date", date10000, past).count());
assertEquals(0L, table.where().between(null, "date", date10000, date0).count());
assertEquals(1L, table.where().between(null, "date", date10000, date10000).count());
assertEquals(2L, table.where().between(null, "date", date10000, future).count());
assertEquals(3L, table.where().between(null, "date", date10000, distantFuture).count());
assertEquals(0L, table.where().between(null, "date", future, distantPast).count());
assertEquals(0L, table.where().between(null, "date", future, past).count());
assertEquals(0L, table.where().between(null, "date", future, date0).count());
assertEquals(0L, table.where().between(null, "date", future, date10000).count());
assertEquals(1L, table.where().between(null, "date", future, future).count());
assertEquals(2L, table.where().between(null, "date", future, distantFuture).count());
assertEquals(0L, table.where().between(null, "date", distantFuture, distantPast).count());
assertEquals(0L, table.where().between(null, "date", distantFuture, past).count());
assertEquals(0L, table.where().between(null, "date", distantFuture, date0).count());
assertEquals(0L, table.where().between(null, "date", distantFuture, date10000).count());
assertEquals(0L, table.where().between(null, "date", distantFuture, future).count());
assertEquals(1L, table.where().between(null, "date", distantFuture, distantFuture).count());
}
use of io.realm.RealmAny in project realm-java by realm.
the class TableQuery method in.
public TableQuery in(@Nullable OsKeyPathMapping mapping, String fieldName, RealmAny[] values) {
fieldName = escapeFieldName(fieldName);
beginGroup();
boolean first = true;
for (RealmAny value : values) {
if (!first) {
or();
}
if (value == null) {
isNull(mapping, fieldName);
} else {
equalTo(mapping, fieldName, value);
}
first = false;
}
endGroup();
queryValidated = false;
return this;
}
use of io.realm.RealmAny in project realm-java by realm.
the class TableQuery method inInsensitive.
public TableQuery inInsensitive(@Nullable OsKeyPathMapping mapping, String fieldName, RealmAny[] values) {
fieldName = escapeFieldName(fieldName);
beginGroup();
boolean first = true;
for (RealmAny value : values) {
if (!first) {
or();
}
if (value == null) {
isNull(mapping, fieldName);
} else {
equalToInsensitive(mapping, fieldName, value);
}
first = false;
}
endGroup();
queryValidated = false;
return this;
}
Aggregations