@SuppressWarnings("unchecked") private <TYPE> TYPE getFromValues(Property<TYPE> property, ValuesStorage values) { Object value = values.get(property.getName()); // Will throw a ClassCastException if the value could not be coerced to the correct type return (TYPE) property.accept(valueCastingVisitor, value); }
/** * @param property the {@link Property} to check * @return true if a value for this property has been read from the database or set by the user, and the "active" * value stored (i.e. the value that would be returned by a call to {@link #get(Property)}) is not null. Does not * take into account column default values. */ public boolean containsNonNullValue(Property<?> property) { if (valuesContainsKey(setValues, property)) { return setValues.get(property.getName()) != null; } else if (valuesContainsKey(values, property)) { return values.get(property.getName()) != null; } return false; }
protected boolean shouldSaveValue(String name, Object newValue) { // we've already decided to save it, so overwrite old value if (setValues.containsKey(name)) { return true; } // values contains this key, we should check it out if (values != null && values.containsKey(name)) { Object value = values.get(name); if (value == null) { if (newValue == null) { return false; } } else if (value.equals(newValue)) { return false; } } // otherwise, good to save return true; }
/** * Utility method to get the rowid of the model, if it exists. * * @return {@value #NO_ID} if this model was not added to the database */ public long getRowId() { Long id = null; String idPropertyName = getRowIdProperty().getName(); if (setValues != null && setValues.containsKey(idPropertyName)) { id = (Long) setValues.get(idPropertyName); } else if (values != null && values.containsKey(idPropertyName)) { id = (Long) values.get(idPropertyName); } if (id != null) { return id; } return NO_ID; }
sql.append((i > 0) ? "," : ""); sql.append(colName); bindArgs[i++] = initialValues.get(colName);
sql.append((i > 0) ? "," : ""); sql.append(colName); bindArgs[i++] = values.get(colName); sql.append("=?");
/** * Analogous to {@link #readPropertiesFromValuesStorage(ValuesStorage, Property[])} but adds the values to the * model as set values, i.e. marks the model as dirty with these values. */ public void setPropertiesFromValuesStorage(ValuesStorage values, Property<?>... properties) { if (values != null) { if (setValues == null) { setValues = newValuesStorage(); } for (Property<?> property : properties) { String key = property.getName(); if (values.containsKey(key)) { Object value = property.accept(valueCastingVisitor, values.get(key)); if (shouldSaveValue(key, value)) { this.setValues.put(property.getName(), value, true); } } } } }
assertTrue(checkTypesOn.get(TestModel.FIRST_NAME.getName()) instanceof String); assertTrue(checkTypesOn.get(TestModel.LAST_NAME.getName()) instanceof String); assertTrue(checkTypesOn.get(TestModel.BIRTHDAY.getName()) instanceof Long); assertTrue(checkTypesOn.get(TestModel.IS_HAPPY.getName()) instanceof Boolean); assertTrue(checkTypesOn.get(TestModel.SOME_DOUBLE.getName()) instanceof Double); assertTrue(checkTypesOn.get(TestModel.$_123_ABC.getName()) instanceof Integer);
@SuppressWarnings("unchecked") private <TYPE> TYPE getFromValues(Property<TYPE> property, ValuesStorage values) { Object value = values.get(property.getName()); // Will throw a ClassCastException if the value could not be coerced to the correct type return (TYPE) property.accept(valueCastingVisitor, value); }
/** * @param property the {@link Property} to check * @return true if a value for this property has been read from the database or set by the user, and the "active" * value stored (i.e. the value that would be returned by a call to {@link #get(Property)}) is not null. Does not * take into account column default values. */ public boolean containsNonNullValue(Property<?> property) { if (valuesContainsKey(setValues, property)) { return setValues.get(property.getName()) != null; } else if (valuesContainsKey(values, property)) { return values.get(property.getName()) != null; } return false; }
protected boolean shouldSaveValue(String name, Object newValue) { // we've already decided to save it, so overwrite old value if (setValues.containsKey(name)) { return true; } // values contains this key, we should check it out if (values != null && values.containsKey(name)) { Object value = values.get(name); if (value == null) { if (newValue == null) { return false; } } else if (value.equals(newValue)) { return false; } } // otherwise, good to save return true; }
public void testEnumProperties() { final TestEnum enumValue = TestEnum.APPLE; final String enumAsString = enumValue.name(); TestModel model = new TestModel() .setFirstName("A") .setLastName("Z") .setBirthday(System.currentTimeMillis()) .setSomeEnum(enumValue); ValuesStorage setValues = model.getSetValues(); assertEquals(enumAsString, setValues.get(TestModel.SOME_ENUM.getName())); database.persist(model); SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select() .where(TestModel.SOME_ENUM.eq(TestEnum.APPLE))); assertEquals(1, cursor.getCount()); assertTrue(cursor.moveToFirst()); assertEquals(enumAsString, cursor.get(TestModel.SOME_ENUM)); TestModel fromDatabase = new TestModel(cursor); assertEquals(enumValue, fromDatabase.getSomeEnum()); }
/** * Utility method to get the rowid of the model, if it exists. * * @return {@value #NO_ID} if this model was not added to the database */ public long getRowId() { Long id = null; String idPropertyName = getRowIdProperty().getName(); if (setValues != null && setValues.containsKey(idPropertyName)) { id = (Long) setValues.get(idPropertyName); } else if (values != null && values.containsKey(idPropertyName)) { id = (Long) values.get(idPropertyName); } if (id != null) { return id; } return NO_ID; }
public void testContainsNonNullValueMethod() { TestModel model = new TestModel(); model.setFirstName("Test"); model.markSaved(); // Move values from set values into database values assertEquals("Test", model.getFirstName()); assertFalse(model.fieldIsDirty(TestModel.FIRST_NAME)); assertNull(model.getSetValues()); assertTrue(model.containsNonNullValue(TestModel.FIRST_NAME)); model.setFirstName(null); // Assert that despite the presence of a non-null value in the database values, containsNonNullValue // defers to the "active" setValue. assertNotNull(model.getDatabaseValues().get(TestModel.FIRST_NAME.getName())); assertNull(model.getFirstName()); assertFalse(model.containsNonNullValue(TestModel.FIRST_NAME)); }
/** * Analogous to {@link #readPropertiesFromValuesStorage(ValuesStorage, Property[])} but adds the values to the * model as set values, i.e. marks the model as dirty with these values. */ public void setPropertiesFromValuesStorage(ValuesStorage values, Property<?>... properties) { if (values != null) { if (setValues == null) { setValues = newValuesStorage(); } for (Property<?> property : properties) { String key = property.getName(); if (values.containsKey(key)) { Object value = property.accept(valueCastingVisitor, values.get(key)); if (shouldSaveValue(key, value)) { this.setValues.put(property.getName(), value, true); } } } } }