类结构
model相关结构说明
- Model提供表,字段等基本信息,
- ModelAdapter根据Model提供了一些功能职责:增删改查。dbflow根据你提供的信息生成具体的职责处理的代码。
- ModeSaver是ModelAdapter的功能职责的维护者,它汇聚ModelAdapter的功能,再根据ModelAdapter提供的信息执行具体的操作。
举个例子
保存model信息到数据库。
定义model信息:
@Table(name = "OtherModel", database = AppDatabase.class)
public class OtherModel extends BaseModel {
@Column
@PrimaryKey
String name;
}
编译后,生成的Adapter:OtherModel_Table
/**
* This is generated code. Please do not modify */
public final class OtherModel_Table extends ModelAdapter<OtherModel> {
//依据model信息,生成的字段信息:name
/**
* Primary Key */
public static final Property<String> name = new Property<String>(OtherModel.class, "name");
public static final IProperty[] ALL_COLUMN_PROPERTIES = new IProperty[]{name};
public OtherModel_Table(DatabaseHolder holder, DatabaseDefinition databaseDefinition) {
super(databaseDefinition);
}
@Override
public final Class<OtherModel> getModelClass() {
return OtherModel.class;
}
// 依据model信息,生成的表的名字:OtherModel
public final String getTableName() {
return "`OtherModel`";
}
@Override
public final BaseProperty getProperty(String columnName) {
columnName = QueryBuilder.quoteIfNeeded(columnName);
switch (columnName) {
case "`name`": {
return name;
}
default: {
throw new IllegalArgumentException("Invalid column name passed. Ensure you are calling the correct table's column");
}
}
}
@Override
public final IProperty[] getAllColumnProperties() {
return ALL_COLUMN_PROPERTIES;
}
@Override
public final void bindToInsertValues(ContentValues values, OtherModel model) {
values.put("name", model.name != null ? model.name : null);
}
@Override
public final void bindToContentValues(ContentValues values, OtherModel model) {
bindToInsertValues(values, model);
}
@Override
public final void bindToInsertStatement(DatabaseStatement statement, OtherModel model, int start) {
if (model.name != null) {
statement.bindString(1 + start, model.name);
} else {
statement.bindNull(1 + start);
}
}
@Override
public final void bindToStatement(DatabaseStatement statement, OtherModel model) {
bindToInsertStatement(statement, model, 0);
}
@Override
public final String getInsertStatementQuery() {
return "INSERT INTO `OtherModel`(`name`) VALUES (?)";
}
@Override
public final String getCompiledStatementQuery() {
return "INSERT INTO `OtherModel`(`name`) VALUES (?)";
}
@Override
public final String getCreationQuery() {
return "CREATE TABLE IF NOT EXISTS `OtherModel`(`name` TEXT, PRIMARY KEY(`name`)" + ");";
}
@Override
public final void loadFromCursor(Cursor cursor, OtherModel model) {
int index_name = cursor.getColumnIndex("name");
if (index_name != -1 && !cursor.isNull(index_name)) {
model.name = cursor.getString(index_name);
} else {
model.name = null;
}
}
@Override
public final boolean exists(OtherModel model, DatabaseWrapper wrapper) {
return SQLite.selectCountOf()
.from(OtherModel.class)
.where(getPrimaryConditionClause(model))
.hasData(wrapper);
}
@Override
public final ConditionGroup getPrimaryConditionClause(OtherModel model) {
ConditionGroup clause = ConditionGroup.clause();
clause.and(name.eq(model.name));
return clause;
}
@Override
public final OtherModel newInstance() {
return new OtherModel();
}
保存这个model:
OtherModel otherModel= new OtherModel();
otherModel.name="wang";
otherModel.save();
交给adapter保存:
public void save() {
getModelAdapter().save(this);
}
adapter交给modelSaver处理保存:
public synchronized boolean save(@NonNull TModel model, DatabaseWrapper wrapper) {
return save(model, wrapper, modelAdapter.getInsertStatement(wrapper), new ContentValues());
}
save
-
exist
OtherModel_Table中生成的代码:
- update
网友评论