Search in sources :

Example 1 with RealmFieldType

use of io.realm.RealmFieldType in project realm-java by realm.

the class Table method add.

/**
     * Appends the specified row to the end of the table. For internal testing usage only.
     *
     * @param values values.
     * @return the row index of the appended row.
     * @deprecated Remove this functions since it doesn't seem to be useful. And this function does deal with tables
     * with primary key defined well. Primary key has to be set with `setXxxUnique` as the first thing to do after row
     * added.
     */
protected long add(Object... values) {
    long rowIndex = addEmptyRow();
    checkImmutable();
    // Checks values types.
    int columns = (int) getColumnCount();
    if (columns != values.length) {
        throw new IllegalArgumentException("The number of value parameters (" + String.valueOf(values.length) + ") does not match the number of columns in the table (" + String.valueOf(columns) + ").");
    }
    RealmFieldType[] colTypes = new RealmFieldType[columns];
    for (int columnIndex = 0; columnIndex < columns; columnIndex++) {
        Object value = values[columnIndex];
        RealmFieldType colType = getColumnType(columnIndex);
        colTypes[columnIndex] = colType;
        if (!colType.isValid(value)) {
            // String representation of the provided value type.
            String providedType;
            if (value == null) {
                providedType = "null";
            } else {
                providedType = value.getClass().toString();
            }
            throw new IllegalArgumentException("Invalid argument no " + String.valueOf(1 + columnIndex) + ". Expected a value compatible with column type " + colType + ", but got " + providedType + ".");
        }
    }
    // Inserts values.
    for (long columnIndex = 0; columnIndex < columns; columnIndex++) {
        Object value = values[(int) columnIndex];
        switch(colTypes[(int) columnIndex]) {
            case BOOLEAN:
                nativeSetBoolean(nativePtr, columnIndex, rowIndex, (Boolean) value, false);
                break;
            case INTEGER:
                if (value == null) {
                    checkDuplicatedNullForPrimaryKeyValue(columnIndex, rowIndex);
                    nativeSetNull(nativePtr, columnIndex, rowIndex, false);
                } else {
                    long intValue = ((Number) value).longValue();
                    checkIntValueIsLegal(columnIndex, rowIndex, intValue);
                    nativeSetLong(nativePtr, columnIndex, rowIndex, intValue, false);
                }
                break;
            case FLOAT:
                nativeSetFloat(nativePtr, columnIndex, rowIndex, (Float) value, false);
                break;
            case DOUBLE:
                nativeSetDouble(nativePtr, columnIndex, rowIndex, (Double) value, false);
                break;
            case STRING:
                if (value == null) {
                    checkDuplicatedNullForPrimaryKeyValue(columnIndex, rowIndex);
                    nativeSetNull(nativePtr, columnIndex, rowIndex, false);
                } else {
                    String stringValue = (String) value;
                    checkStringValueIsLegal(columnIndex, rowIndex, stringValue);
                    nativeSetString(nativePtr, columnIndex, rowIndex, (String) value, false);
                }
                break;
            case DATE:
                if (value == null)
                    throw new IllegalArgumentException("Null Date is not allowed.");
                nativeSetTimestamp(nativePtr, columnIndex, rowIndex, ((Date) value).getTime(), false);
                break;
            case BINARY:
                if (value == null)
                    throw new IllegalArgumentException("Null Array is not allowed");
                nativeSetByteArray(nativePtr, columnIndex, rowIndex, (byte[]) value, false);
                break;
            case UNSUPPORTED_MIXED:
            case UNSUPPORTED_TABLE:
            default:
                throw new RuntimeException("Unexpected columnType: " + String.valueOf(colTypes[(int) columnIndex]));
        }
    }
    return rowIndex;
}
Also used : RealmFieldType(io.realm.RealmFieldType)

Example 2 with RealmFieldType

use of io.realm.RealmFieldType in project realm-java by realm.

the class SortDescriptorTests method getInstanceForDistinct_multipleFields.

@Test
public void getInstanceForDistinct_multipleFields() {
    RealmFieldType stringType = RealmFieldType.STRING;
    long stringColumn = table.addColumn(stringType, stringType.name());
    table.addSearchIndex(stringColumn);
    RealmFieldType intType = RealmFieldType.INTEGER;
    long intColumn = table.addColumn(intType, intType.name());
    table.addSearchIndex(intColumn);
    SortDescriptor sortDescriptor = SortDescriptor.getInstanceForDistinct(table, new String[] { stringType.name(), intType.name() });
    assertEquals(2, sortDescriptor.getColumnIndices().length);
    assertNull(sortDescriptor.getAscendings());
    assertEquals(1, sortDescriptor.getColumnIndices()[0].length);
    assertEquals(stringColumn, sortDescriptor.getColumnIndices()[0][0]);
    assertEquals(1, sortDescriptor.getColumnIndices()[1].length);
    assertEquals(intColumn, sortDescriptor.getColumnIndices()[1][0]);
}
Also used : RealmFieldType(io.realm.RealmFieldType) Test(org.junit.Test)

Example 3 with RealmFieldType

use of io.realm.RealmFieldType in project realm-java by realm.

the class SortDescriptorTests method getInstanceForSort_multipleFields.

@Test
public void getInstanceForSort_multipleFields() {
    RealmFieldType stringType = RealmFieldType.STRING;
    long stringColumn = table.addColumn(stringType, stringType.name());
    RealmFieldType intType = RealmFieldType.INTEGER;
    long intColumn = table.addColumn(intType, intType.name());
    SortDescriptor sortDescriptor = SortDescriptor.getInstanceForSort(table, new String[] { stringType.name(), intType.name() }, new Sort[] { Sort.ASCENDING, Sort.DESCENDING });
    assertEquals(2, sortDescriptor.getAscendings().length);
    assertEquals(2, sortDescriptor.getColumnIndices().length);
    assertEquals(1, sortDescriptor.getColumnIndices()[0].length);
    assertEquals(stringColumn, sortDescriptor.getColumnIndices()[0][0]);
    assertTrue(sortDescriptor.getAscendings()[0]);
    assertEquals(1, sortDescriptor.getColumnIndices()[1].length);
    assertEquals(intColumn, sortDescriptor.getColumnIndices()[1][0]);
    assertFalse(sortDescriptor.getAscendings()[1]);
}
Also used : RealmFieldType(io.realm.RealmFieldType) Test(org.junit.Test)

Example 4 with RealmFieldType

use of io.realm.RealmFieldType in project realm-java by realm.

the class SortDescriptorTests method getInstanceForDistinct_shouldThrowOnLinkAndListListField.

@Test
public void getInstanceForDistinct_shouldThrowOnLinkAndListListField() {
    RealmFieldType type = RealmFieldType.STRING;
    RealmFieldType objectType = RealmFieldType.OBJECT;
    RealmFieldType listType = RealmFieldType.LIST;
    table.addColumn(type, type.name());
    table.addColumnLink(objectType, objectType.name(), table);
    table.addColumnLink(listType, listType.name(), table);
    try {
        SortDescriptor.getInstanceForDistinct(table, String.format("%s.%s", listType.name(), type.name()));
        fail();
    } catch (IllegalArgumentException ignored) {
    }
    try {
        SortDescriptor.getInstanceForDistinct(table, String.format("%s.%s", objectType.name(), type.name()));
        fail();
    } catch (IllegalArgumentException ignored) {
    }
}
Also used : RealmFieldType(io.realm.RealmFieldType) Test(org.junit.Test)

Example 5 with RealmFieldType

use of io.realm.RealmFieldType in project realm-java by realm.

the class SortDescriptorTests method getInstanceForDistinct_shouldThrowOnInvalidField.

@Test
public void getInstanceForDistinct_shouldThrowOnInvalidField() {
    List<RealmFieldType> types = new ArrayList<RealmFieldType>();
    for (RealmFieldType type : RealmFieldType.values()) {
        if (!SortDescriptor.validFieldTypesForDistinct.contains(type) && type != RealmFieldType.UNSUPPORTED_DATE && type != RealmFieldType.UNSUPPORTED_TABLE && type != RealmFieldType.UNSUPPORTED_MIXED) {
            if (type == RealmFieldType.LIST || type == RealmFieldType.OBJECT) {
                table.addColumnLink(type, type.name(), table);
            } else {
                table.addColumn(type, type.name());
            }
            types.add(type);
        }
    }
    for (RealmFieldType type : types) {
        try {
            SortDescriptor.getInstanceForDistinct(table, type.name());
            fail();
        } catch (IllegalArgumentException ignored) {
            assertTrue(ignored.getMessage().contains("Distinct is not supported"));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) RealmFieldType(io.realm.RealmFieldType) Test(org.junit.Test)

Aggregations

RealmFieldType (io.realm.RealmFieldType)18 Test (org.junit.Test)16 Date (java.util.Date)5 Pair (android.util.Pair)3 ArrayList (java.util.ArrayList)2 RealmException (io.realm.exceptions.RealmException)1