/** * Update the object in the database. */ publicint update(DatabaseConnection databaseConnection, T data, ObjectCache objectCache) throws SQLException { try { // there is always and id field as an argument so just return 0 lines updated if (argFieldTypes.length <= 1) { return0; } Object[] args = getFieldObjects(data); Object newVersion = null; if (versionFieldType != null) { newVersion = versionFieldType.extractJavaFieldValue(data); newVersion = versionFieldType.moveToNextValue(newVersion); args[versionFieldTypeIndex] = versionFieldType.convertJavaFieldToSqlArgValue(newVersion); } int rowC = databaseConnection.update(statement, args, argFieldTypes); if (rowC > 0) { if (newVersion != null) { // if we have updated a row then update the version field in our object to the new value versionFieldType.assignField(data, newVersion, false, null); } if (objectCache != null) { // if we've changed something then see if we need to update our cache Object id = idField.extractJavaFieldValue(data); T cachedData = objectCache.get(clazz, id); if (cachedData != null && cachedData != data) { // copy each field from the updated data into the cached object for (FieldType fieldType : tableInfo.getFieldTypes()) { if (fieldType != idField) { fieldType.assignField(cachedData, fieldType.extractJavaFieldValue(data), false, objectCache); } } } } } logger.debug("update data with statement '{}' and {} args, changed {} rows", statement, args.length, rowC); if (args.length > 0) { // need to do the (Object) cast to force args to be a single object logger.trace("update arguments: {}", (Object) args); } return rowC; } catch (SQLException e) { throw SqlExceptionUtil.create("Unable to run update stmt on object " + data + ": " + statement, e); } }
首先看其中的getFieldObjects方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/** * Return the array of field objects pulled from the data object. */ protectedObject[] getFieldObjects(Object data) throws SQLException { Object[] objects = newObject[argFieldTypes.length]; for (int i = 0; i < argFieldTypes.length; i++) { FieldType fieldType = argFieldTypes[i]; if (fieldType.isAllowGeneratedIdInsert()) { objects[i] = fieldType.getFieldValueIfNotDefault(data); } else { objects[i] = fieldType.extractJavaFieldToSqlArgValue(data); } if (objects[i] == null && fieldType.getDefaultValue() != null) { objects[i] = fieldType.getDefaultValue(); } } return objects; }
public Object extractJavaFieldValue(Object object) throws SQLException {
Object val = extractRawJavaFieldValue(object);
// if this is a foreign object then we want its id field if (foreignIdField != null && val != null) { val = foreignIdField.extractRawJavaFieldValue(val); }
/** * Return the value from the field in the object that is defined by this FieldType. */ public <FV> FV extractRawJavaFieldValue(Objectobject) throwsSQLException { Objectval; if (fieldGetMethod == null) { try { // field object may not be a T yet val = field.get(object); } catch (Exception e) { throwSqlExceptionUtil.create("Could not get field value for " + this, e); } } else { try { val = fieldGetMethod.invoke(object); } catch (Exception e) { throwSqlExceptionUtil.create("Could not call " + fieldGetMethod + " for " + this, e); } }
/** * Convert a field value to something suitable to be stored in the database. */ publicObject convertJavaFieldToSqlArgValue(Object fieldVal) throws SQLException { if (fieldVal == null) { returnnull; } else { return fieldConverter.javaToSqlArg(this, fieldVal); } }
随后来看代码
1
int rowC = databaseConnection.update(statement, args, argFieldTypes);
其中会调用到我设置的ConnectionProxy的update方法
1 2 3 4 5 6 7 8 9
@Override publicintupdate(String statement, Object[] args, FieldType[] argfieldTypes)throws SQLException { int ret = super.update(statement, args, argfieldTypes); Matcher matcher = updatePattern.matcher(statement); if (!getDbHelper().getWritableDatabase().inTransaction() && matcher.find()) { postChangeEvent(matcher.group(1)); } return ret; }