Search in sources :

Example 11 with RealmFieldType

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

the class SortDescriptorTests method getInstanceForSort_linkField.

@Test
public void getInstanceForSort_linkField() {
    for (RealmFieldType type : SortDescriptor.validFieldTypesForDistinct) {
        long column = table.addColumn(type, type.name());
        table.addSearchIndex(column);
    }
    RealmFieldType objectType = RealmFieldType.OBJECT;
    long columnLink = table.addColumnLink(objectType, objectType.name(), table);
    long i = 0;
    for (RealmFieldType type : SortDescriptor.validFieldTypesForDistinct) {
        SortDescriptor sortDescriptor = SortDescriptor.getInstanceForSort(table, String.format("%s.%s", objectType.name(), type.name()), Sort.ASCENDING);
        assertEquals(2, sortDescriptor.getColumnIndices()[0].length);
        assertEquals(columnLink, sortDescriptor.getColumnIndices()[0][0]);
        assertEquals(i, sortDescriptor.getColumnIndices()[0][1]);
        assertTrue(sortDescriptor.getAscendings()[0]);
        i++;
    }
}
Also used : RealmFieldType(io.realm.RealmFieldType) Test(org.junit.Test)

Example 12 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 13 with RealmFieldType

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

the class Table method addEmptyRowWithPrimaryKey.

/**
     * Adds an empty row to the table and set the primary key with the given value.
     *
     * @param primaryKeyValue the primary key value.
     * @param validation set to {@code false} to skip all validations. This is currently used by bulk insert which
     *                     has its own validations.
     * @return the row index.
     */
public long addEmptyRowWithPrimaryKey(Object primaryKeyValue, boolean validation) {
    if (validation) {
        checkImmutable();
        checkHasPrimaryKey();
    }
    long primaryKeyColumnIndex = getPrimaryKey();
    RealmFieldType type = getColumnType(primaryKeyColumnIndex);
    long rowIndex;
    // Adds with primary key initially set.
    if (primaryKeyValue == null) {
        switch(type) {
            case STRING:
            case INTEGER:
                if (validation && findFirstNull(primaryKeyColumnIndex) != NO_MATCH) {
                    throwDuplicatePrimaryKeyException("null");
                }
                rowIndex = nativeAddEmptyRow(nativePtr, 1);
                if (type == RealmFieldType.STRING) {
                    nativeSetStringUnique(nativePtr, primaryKeyColumnIndex, rowIndex, null);
                } else {
                    nativeSetNullUnique(nativePtr, primaryKeyColumnIndex, rowIndex);
                }
                break;
            default:
                throw new RealmException("Cannot check for duplicate rows for unsupported primary key type: " + type);
        }
    } else {
        switch(type) {
            case STRING:
                if (!(primaryKeyValue instanceof String)) {
                    throw new IllegalArgumentException("Primary key value is not a String: " + primaryKeyValue);
                }
                if (validation && findFirstString(primaryKeyColumnIndex, (String) primaryKeyValue) != NO_MATCH) {
                    throwDuplicatePrimaryKeyException(primaryKeyValue);
                }
                rowIndex = nativeAddEmptyRow(nativePtr, 1);
                nativeSetStringUnique(nativePtr, primaryKeyColumnIndex, rowIndex, (String) primaryKeyValue);
                break;
            case INTEGER:
                long pkValue;
                try {
                    pkValue = Long.parseLong(primaryKeyValue.toString());
                } catch (RuntimeException e) {
                    throw new IllegalArgumentException("Primary key value is not a long: " + primaryKeyValue);
                }
                if (validation && findFirstLong(primaryKeyColumnIndex, pkValue) != NO_MATCH) {
                    throwDuplicatePrimaryKeyException(pkValue);
                }
                rowIndex = nativeAddEmptyRow(nativePtr, 1);
                nativeSetLongUnique(nativePtr, primaryKeyColumnIndex, rowIndex, pkValue);
                break;
            default:
                throw new RealmException("Cannot check for duplicate rows for unsupported primary key type: " + type);
        }
    }
    return rowIndex;
}
Also used : RealmFieldType(io.realm.RealmFieldType) RealmException(io.realm.exceptions.RealmException)

Example 14 with RealmFieldType

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

the class JNITableTest method convertToNullable.

// Tests the migration of a string column to be nullable.
@Test
public void convertToNullable() {
    RealmFieldType[] columnTypes = { RealmFieldType.BOOLEAN, RealmFieldType.DATE, RealmFieldType.DOUBLE, RealmFieldType.FLOAT, RealmFieldType.INTEGER, RealmFieldType.BINARY, RealmFieldType.STRING };
    for (RealmFieldType columnType : columnTypes) {
        // Tests various combinations of column names and nullability.
        String[] columnNames = { "foobar", "__TMP__0" };
        for (boolean nullable : new boolean[] { Table.NOT_NULLABLE, Table.NULLABLE }) {
            for (String columnName : columnNames) {
                Table table = new Table();
                long colIndex = table.addColumn(columnType, columnName, nullable);
                table.addColumn(RealmFieldType.BOOLEAN, "bool");
                table.addEmptyRow();
                if (columnType == RealmFieldType.BOOLEAN) {
                    table.setBoolean(colIndex, 0, true, false);
                } else if (columnType == RealmFieldType.DATE) {
                    table.setDate(colIndex, 0, new Date(0), false);
                } else if (columnType == RealmFieldType.DOUBLE) {
                    table.setDouble(colIndex, 0, 1.0, false);
                } else if (columnType == RealmFieldType.FLOAT) {
                    table.setFloat(colIndex, 0, 1.0f, false);
                } else if (columnType == RealmFieldType.INTEGER) {
                    table.setLong(colIndex, 0, 1, false);
                } else if (columnType == RealmFieldType.BINARY) {
                    table.setBinaryByteArray(colIndex, 0, new byte[] { 0 }, false);
                } else if (columnType == RealmFieldType.STRING) {
                    table.setString(colIndex, 0, "Foo", false);
                }
                try {
                    table.addEmptyRow();
                    if (columnType == RealmFieldType.BINARY) {
                        table.setBinaryByteArray(colIndex, 1, null, false);
                    } else if (columnType == RealmFieldType.STRING) {
                        table.setString(colIndex, 1, null, false);
                    } else {
                        table.getCheckedRow(1).setNull(colIndex);
                    }
                    if (!nullable) {
                        fail();
                    }
                } catch (IllegalArgumentException ignored) {
                }
                table.removeLast();
                assertEquals(1, table.size());
                table.convertColumnToNullable(colIndex);
                assertTrue(table.isColumnNullable(colIndex));
                assertEquals(1, table.size());
                assertEquals(2, table.getColumnCount());
                assertTrue(table.getColumnIndex(columnName) >= 0);
                assertEquals(colIndex, table.getColumnIndex(columnName));
                table.addEmptyRow();
                if (columnType == RealmFieldType.BINARY) {
                    table.setBinaryByteArray(colIndex, 0, null, false);
                } else if (columnType == RealmFieldType.STRING) {
                    table.setString(colIndex, 0, null, false);
                } else {
                    table.getCheckedRow(0).setNull(colIndex);
                }
                assertEquals(2, table.size());
                if (columnType == RealmFieldType.BINARY) {
                    assertNull(table.getBinaryByteArray(colIndex, 1));
                } else if (columnType == RealmFieldType.STRING) {
                    assertNull(table.getString(colIndex, 1));
                } else {
                    assertTrue(table.getUncheckedRow(1).isNull(colIndex));
                }
            }
        }
    }
}
Also used : RealmFieldType(io.realm.RealmFieldType) Date(java.util.Date) Test(org.junit.Test)

Example 15 with RealmFieldType

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

the class JNITableTest method defaultValue_setMultipleTimes.

@Test
public void defaultValue_setMultipleTimes() {
    // t is not used in this test.
    t = null;
    final SharedRealm sharedRealm = SharedRealm.getInstance(configFactory.createConfiguration());
    //noinspection TryFinallyCanBeTryWithResources
    try {
        sharedRealm.beginTransaction();
        final Table table = sharedRealm.getTable(Table.TABLE_PREFIX + "DefaultValueTest");
        sharedRealm.commitTransaction();
        List<Pair<RealmFieldType, Object>> columnInfoList = Arrays.asList(new Pair<RealmFieldType, Object>(RealmFieldType.STRING, new String[] { "string value1", "string value2" }), new Pair<RealmFieldType, Object>(RealmFieldType.INTEGER, new Long[] { 100L, 102L }), new Pair<RealmFieldType, Object>(RealmFieldType.BOOLEAN, new Boolean[] { false, true }), new Pair<RealmFieldType, Object>(RealmFieldType.BINARY, new byte[][] { new byte[] { 123 }, new byte[] { -123 } }), new Pair<RealmFieldType, Object>(RealmFieldType.DATE, new Date[] { new Date(123456), new Date(13579) }), new Pair<RealmFieldType, Object>(RealmFieldType.FLOAT, new Float[] { 1.234f, 100f }), new Pair<RealmFieldType, Object>(RealmFieldType.DOUBLE, new Double[] { Math.PI, Math.E }), new Pair<RealmFieldType, Object>(RealmFieldType.OBJECT, new Long[] { 0L, 1L }));
        for (Pair<RealmFieldType, Object> columnInfo : columnInfoList) {
            final RealmFieldType type = columnInfo.first;
            if (type == RealmFieldType.OBJECT || type == RealmFieldType.LIST) {
                table.addColumnLink(type, type.name().toLowerCase(Locale.ENGLISH) + "Col", table);
            } else {
                table.addColumn(type, type.name().toLowerCase(Locale.ENGLISH) + "Col");
            }
        }
        sharedRealm.beginTransaction();
        table.addEmptyRow();
        // For link field update.
        table.addEmptyRow();
        ListIterator<Pair<RealmFieldType, Object>> it = columnInfoList.listIterator();
        for (int columnIndex = 0; columnIndex < columnInfoList.size(); columnIndex++) {
            Pair<RealmFieldType, Object> columnInfo = it.next();
            final RealmFieldType type = columnInfo.first;
            final Object value1 = ((Object[]) columnInfo.second)[0];
            final Object value2 = ((Object[]) columnInfo.second)[1];
            switch(type) {
                case STRING:
                    table.setString(columnIndex, 0, (String) value1, true);
                    table.setString(columnIndex, 0, (String) value2, true);
                    assertEquals(value2, table.getString(columnIndex, 0));
                    break;
                case INTEGER:
                    table.setLong(columnIndex, 0, (long) value1, true);
                    table.setLong(columnIndex, 0, (long) value2, true);
                    assertEquals(value2, table.getLong(columnIndex, 0));
                    break;
                case BOOLEAN:
                    table.setBoolean(columnIndex, 0, (boolean) value1, true);
                    table.setBoolean(columnIndex, 0, (boolean) value2, true);
                    assertEquals(value2, table.getBoolean(columnIndex, 0));
                    break;
                case BINARY:
                    table.setBinaryByteArray(columnIndex, 0, (byte[]) value1, true);
                    table.setBinaryByteArray(columnIndex, 0, (byte[]) value2, true);
                    assertTrue(Arrays.equals((byte[]) value2, table.getBinaryByteArray(columnIndex, 0)));
                    break;
                case DATE:
                    table.setDate(columnIndex, 0, (Date) value1, true);
                    table.setDate(columnIndex, 0, (Date) value2, true);
                    assertEquals(value2, table.getDate(columnIndex, 0));
                    break;
                case FLOAT:
                    table.setFloat(columnIndex, 0, (float) value1, true);
                    table.setFloat(columnIndex, 0, (float) value2, true);
                    assertEquals(value2, table.getFloat(columnIndex, 0));
                    break;
                case DOUBLE:
                    table.setDouble(columnIndex, 0, (double) value1, true);
                    table.setDouble(columnIndex, 0, (double) value2, true);
                    assertEquals(value2, table.getDouble(columnIndex, 0));
                    break;
                case OBJECT:
                    table.setLink(columnIndex, 0, (long) value1, true);
                    table.setLink(columnIndex, 0, (long) value2, true);
                    assertEquals(value2, table.getLink(columnIndex, 0));
                    break;
                default:
                    throw new RuntimeException("unexpected field type: " + type);
            }
        }
        sharedRealm.commitTransaction();
        // Checks if the value can be read after committing transaction.
        it = columnInfoList.listIterator();
        for (int columnIndex = 0; columnIndex < columnInfoList.size(); columnIndex++) {
            Pair<RealmFieldType, Object> columnInfo = it.next();
            final RealmFieldType type = columnInfo.first;
            final Object value2 = ((Object[]) columnInfo.second)[1];
            switch(type) {
                case STRING:
                    assertEquals(value2, table.getString(columnIndex, 0));
                    break;
                case INTEGER:
                    assertEquals(value2, table.getLong(columnIndex, 0));
                    break;
                case BOOLEAN:
                    assertEquals(value2, table.getBoolean(columnIndex, 0));
                    break;
                case BINARY:
                    assertTrue(Arrays.equals((byte[]) value2, table.getBinaryByteArray(columnIndex, 0)));
                    break;
                case DATE:
                    assertEquals(value2, table.getDate(columnIndex, 0));
                    break;
                case FLOAT:
                    assertEquals(value2, table.getFloat(columnIndex, 0));
                    break;
                case DOUBLE:
                    assertEquals(value2, table.getDouble(columnIndex, 0));
                    break;
                case OBJECT:
                    assertEquals(value2, table.getLink(columnIndex, 0));
                    break;
                default:
                    throw new RuntimeException("unexpected field type: " + type);
            }
        }
    } finally {
        sharedRealm.close();
    }
}
Also used : RealmFieldType(io.realm.RealmFieldType) Date(java.util.Date) Pair(android.util.Pair) 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