Android(DBflow) - 因序列化而更改列名

时间:2017-09-25 19:09:39

标签: android serialization dbflow

我的数据库中有一个名为File的表,我想更改列的名称,因为与此列相关的Json字段已更改,我正在使用自动序列化。 所以这是我的桌子类:

@Table(database = MyDB.class)
public class File extends BaseModel{
  @PrimaryKey()
  @Column(name = "id")
  private String _id;

  @Column
  @SerializedName(NAMES.Server.NAME)
  private String filename;

  @Column
  private String url;

  @Column
  private String path;

  @Column
  @SerializedName(NAMES.Server.MIME_TYPE)
  private String mimeType;

  @Column
  private String caption;

  @Column
  private long length;

  @Column
  private Date createdAt;

  @Column
  private Date takenAt;

  @Column
  private Date updatedAt;

  @Column
  private String createdBy;

  @Column
  private String projectId;

  @Column
  private String collectionId;

  @Column
  @ForeignKey(stubbedRelationship = true)
  transient Collection parentCollection;

  public File() { }

  public File(String caption){
    this.caption = caption;
  }

  public File(String filename, String path, String mimeType, String caption) {
    this._id = new ObjectId().toString();
    this.filename = filename;
    this.path = path;
    this.mimeType = mimeType;
    this.caption = caption;
  }

  public File(@NonNull File file) {
    _id = file._id;
    filename = file.filename;
    url = file.url;
    path = file.path;
    mimeType = file.mimeType;
    caption = file.caption;
    length = file.length;
    createdAt = file.createdAt;
    takenAt = file.takenAt;
    updatedAt = file.updatedAt;
    createdBy = file.createdBy;
    projectId = file.projectId;
    collectionId = file.collectionId;

  }

  public String get_id() {
    return _id;
  }

  public void set_id(String _id) {
    this._id = _id;
  }

  public String getFilename() {
    return filename;
  }

  public void setFilename(String filename) {
    this.filename = filename;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public String getPath() {
    return path;
  }

  public void setPath(String path) {
    this.path = path;
  }

  public String getMimeType() {
    return mimeType;
  }

  public void setMimeType(String mimeType) {
    this.mimeType = mimeType;
  }

  public String getCaption() {
    return caption;
  }

  public void setCaption(String caption) {
    this.caption = caption;
  }

  public long getLength() {
    return length;
  }

  public void setLength(long length) {
    this.length = length;
  }

  public Date getCreatedAt() {
    return createdAt;
  }

  public void setCreatedAt(Date createdAt) {
    this.createdAt = createdAt;
  }

  public Date getUpdatedAt() {
    return updatedAt;
  }

  public void setUpdatedAt(Date updatedAt) {
    this.updatedAt = updatedAt;
  }

  public Date getTakenAt() {
    return takenAt;
  }

  public void setTakenAt(Date takenAt) {
    this.takenAt = takenAt;
  }

  public String getCreatedBy() {
    return createdBy;
  }

  public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
  }

  public String getProjectId() {
    return projectId;
  }

  public void setProjectId(String projectId) {
    this.projectId = projectId;
  }

  public String getCollectionId() {
    return collectionId;
  }

  public void setCollectionId(String collectionId) {
    this.collectionId = collectionId;
  }

  @Override
  public boolean equals(Object obj) {
    if(obj instanceof File){
      return _id.equals(((File) obj)._id);
    }
    return false;
  }

  public void associateParent(Collection collection){
    parentCollection = (collection);
  }

  public boolean isLocal() {
    return path != null && url == null;
  }

  public static List<File> copy(List<File> files) {
    if (files != null && files.size() > 0) {
      List<File> copy = new ArrayList<>(files.size());
      for (File file : files) {
        copy.add(new File(file));
      }
      return copy;
    }else {
      return null;
    }
  }
}

我想更改“文件名”列。但正如您所看到的,我正在使用@Serialization来表示此特定字段。我的问题是:是否可以更改此名称而无需在表中创建新列并删除此列? 我考虑过运行迁移脚本,但是不可能使用迁移脚本更改列的名称,对吗?

编辑,这是我的File_table类:

/**
 * This is generated code. Please do not modify */
public final class File_Table extends ModelAdapter<File> {
  /**
   * Primary Key */
  public static final Property<String> id = new Property<String>(File.class, "id");

  public static final Property<String> filename = new Property<String>(File.class, "filename");

  public static final Property<String> url = new Property<String>(File.class, "url");

  public static final Property<String> path = new Property<String>(File.class, "path");

  public static final Property<String> mimeType = new Property<String>(File.class, "mimeType");

  public static final Property<String> caption = new Property<String>(File.class, "caption");

  public static final Property<Long> length = new Property<Long>(File.class, "length");

  public static final TypeConvertedProperty<Long, Date> createdAt = new TypeConvertedProperty<Long, Date>(File.class, "createdAt", true,
  new TypeConvertedProperty.TypeConverterGetter() {
  @Override
  public TypeConverter getTypeConverter(Class<?> modelClass) {
    File_Table adapter = (File_Table) FlowManager.getInstanceAdapter(modelClass);
  return adapter.global_typeConverterDateConverter;
  }
  });

  public static final TypeConvertedProperty<Long, Date> takenAt = new TypeConvertedProperty<Long, Date>(File.class, "takenAt", true,
  new TypeConvertedProperty.TypeConverterGetter() {
  @Override
  public TypeConverter getTypeConverter(Class<?> modelClass) {
    File_Table adapter = (File_Table) FlowManager.getInstanceAdapter(modelClass);
  return adapter.global_typeConverterDateConverter;
  }
  });

  public static final TypeConvertedProperty<Long, Date> updatedAt = new TypeConvertedProperty<Long, Date>(File.class, "updatedAt", true,
  new TypeConvertedProperty.TypeConverterGetter() {
  @Override
  public TypeConverter getTypeConverter(Class<?> modelClass) {
    File_Table adapter = (File_Table) FlowManager.getInstanceAdapter(modelClass);
  return adapter.global_typeConverterDateConverter;
  }
  });

  public static final Property<String> createdBy = new Property<String>(File.class, "createdBy");

  public static final Property<String> projectId = new Property<String>(File.class, "projectId");

  public static final Property<String> collectionId = new Property<String>(File.class, "collectionId");

  /**
   * Foreign Key */
  public static final Property<String> parentCollection_id = new Property<String>(File.class, "parentCollection_id");

  public static final IProperty[] ALL_COLUMN_PROPERTIES = new IProperty[]{id,filename,url,path,mimeType,caption,length,createdAt,takenAt,updatedAt,createdBy,projectId,collectionId,parentCollection_id};

  private final DateConverter global_typeConverterDateConverter;

  public File_Table(DatabaseHolder holder, DatabaseDefinition databaseDefinition) {
    super(databaseDefinition);
    global_typeConverterDateConverter = (DateConverter) holder.getTypeConverterForClass(Date.class);
  }

  @Override
  public final Class<File> getModelClass() {
    return File.class;
  }

  @Override
  public final String getTableName() {
    return "`File`";
  }

  @Override
  public final File newInstance() {
    return new File();
  }

  @Override
  public final Property getProperty(String columnName) {
    columnName = QueryBuilder.quoteIfNeeded(columnName);
    switch ((columnName)) {
      case "`id`":  {
        return id;
      }
      case "`filename`":  {
        return filename;
      }
      case "`url`":  {
        return url;
      }
      case "`path`":  {
        return path;
      }
      case "`mimeType`":  {
        return mimeType;
      }
      case "`caption`":  {
        return caption;
      }
      case "`length`":  {
        return length;
      }
      case "`createdAt`":  {
        return createdAt;
      }
      case "`takenAt`":  {
        return takenAt;
      }
      case "`updatedAt`":  {
        return updatedAt;
      }
      case "`createdBy`":  {
        return createdBy;
      }
      case "`projectId`":  {
        return projectId;
      }
      case "`collectionId`":  {
        return collectionId;
      }
      case "`parentCollection_id`": {
        return parentCollection_id;
      }
      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, File model) {
    values.put("`id`", model.get_id() != null ? model.get_id() : null);
    values.put("`filename`", model.getFilename() != null ? model.getFilename() : null);
    values.put("`url`", model.getUrl() != null ? model.getUrl() : null);
    values.put("`path`", model.getPath() != null ? model.getPath() : null);
    values.put("`mimeType`", model.getMimeType() != null ? model.getMimeType() : null);
    values.put("`caption`", model.getCaption() != null ? model.getCaption() : null);
    values.put("`length`", model.getLength());
    Long refcreatedAt = model.getCreatedAt() != null ? global_typeConverterDateConverter.getDBValue(model.getCreatedAt()) : null;
    values.put("`createdAt`", refcreatedAt != null ? refcreatedAt : null);
    Long reftakenAt = model.getTakenAt() != null ? global_typeConverterDateConverter.getDBValue(model.getTakenAt()) : null;
    values.put("`takenAt`", reftakenAt != null ? reftakenAt : null);
    Long refupdatedAt = model.getUpdatedAt() != null ? global_typeConverterDateConverter.getDBValue(model.getUpdatedAt()) : null;
    values.put("`updatedAt`", refupdatedAt != null ? refupdatedAt : null);
    values.put("`createdBy`", model.getCreatedBy() != null ? model.getCreatedBy() : null);
    values.put("`projectId`", model.getProjectId() != null ? model.getProjectId() : null);
    values.put("`collectionId`", model.getCollectionId() != null ? model.getCollectionId() : null);
    if (model.parentCollection != null) {
      values.put("`parentCollection_id`", model.parentCollection.getId());
    } else {
      values.putNull("`parentCollection_id`");
    }
  }

  @Override
  public final void bindToInsertStatement(DatabaseStatement statement, File model, int start) {
    statement.bindStringOrNull(1 + start, model.get_id());
    statement.bindStringOrNull(2 + start, model.getFilename());
    statement.bindStringOrNull(3 + start, model.getUrl());
    statement.bindStringOrNull(4 + start, model.getPath());
    statement.bindStringOrNull(5 + start, model.getMimeType());
    statement.bindStringOrNull(6 + start, model.getCaption());
    statement.bindLong(7 + start, model.getLength());
    Long refcreatedAt = model.getCreatedAt() != null ? global_typeConverterDateConverter.getDBValue(model.getCreatedAt()) : null;
    statement.bindNumberOrNull(8 + start, refcreatedAt);
    Long reftakenAt = model.getTakenAt() != null ? global_typeConverterDateConverter.getDBValue(model.getTakenAt()) : null;
    statement.bindNumberOrNull(9 + start, reftakenAt);
    Long refupdatedAt = model.getUpdatedAt() != null ? global_typeConverterDateConverter.getDBValue(model.getUpdatedAt()) : null;
    statement.bindNumberOrNull(10 + start, refupdatedAt);
    statement.bindStringOrNull(11 + start, model.getCreatedBy());
    statement.bindStringOrNull(12 + start, model.getProjectId());
    statement.bindStringOrNull(13 + start, model.getCollectionId());
    if (model.parentCollection != null) {
      statement.bindStringOrNull(14 + start, model.parentCollection.getId());
    } else {
      statement.bindNull(14 + start);
    }
  }

  @Override
  public final void bindToUpdateStatement(DatabaseStatement statement, File model) {
    statement.bindStringOrNull(1, model.get_id());
    statement.bindStringOrNull(2, model.getFilename());
    statement.bindStringOrNull(3, model.getUrl());
    statement.bindStringOrNull(4, model.getPath());
    statement.bindStringOrNull(5, model.getMimeType());
    statement.bindStringOrNull(6, model.getCaption());
    statement.bindLong(7, model.getLength());
    Long refcreatedAt = model.getCreatedAt() != null ? global_typeConverterDateConverter.getDBValue(model.getCreatedAt()) : null;
    statement.bindNumberOrNull(8, refcreatedAt);
    Long reftakenAt = model.getTakenAt() != null ? global_typeConverterDateConverter.getDBValue(model.getTakenAt()) : null;
    statement.bindNumberOrNull(9, reftakenAt);
    Long refupdatedAt = model.getUpdatedAt() != null ? global_typeConverterDateConverter.getDBValue(model.getUpdatedAt()) : null;
    statement.bindNumberOrNull(10, refupdatedAt);
    statement.bindStringOrNull(11, model.getCreatedBy());
    statement.bindStringOrNull(12, model.getProjectId());
    statement.bindStringOrNull(13, model.getCollectionId());
    if (model.parentCollection != null) {
      statement.bindStringOrNull(14, model.parentCollection.getId());
    } else {
      statement.bindNull(14);
    }
    statement.bindStringOrNull(15, model.get_id());
  }

  @Override
  public final void bindToDeleteStatement(DatabaseStatement statement, File model) {
    statement.bindStringOrNull(1, model.get_id());
  }

  @Override
  public final String getCompiledStatementQuery() {
    return "INSERT INTO `File`(`id`,`filename`,`url`,`path`,`mimeType`,`caption`,`length`,`createdAt`,`takenAt`,`updatedAt`,`createdBy`,`projectId`,`collectionId`,`parentCollection_id`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
  }

  @Override
  public final String getUpdateStatementQuery() {
    return "UPDATE `File` SET `id`=?,`filename`=?,`url`=?,`path`=?,`mimeType`=?,`caption`=?,`length`=?,`createdAt`=?,`takenAt`=?,`updatedAt`=?,`createdBy`=?,`projectId`=?,`collectionId`=?,`parentCollection_id`=? WHERE `id`=?";
  }

  @Override
  public final String getDeleteStatementQuery() {
    return "DELETE FROM `File` WHERE `id`=?";
  }

  @Override
  public final String getCreationQuery() {
    return "CREATE TABLE IF NOT EXISTS `File`(`id` TEXT, `filename` TEXT, `url` TEXT, `path` TEXT, `mimeType` TEXT, `caption` TEXT, `length` INTEGER, `createdAt` TEXT, `takenAt` TEXT, `updatedAt` TEXT, `createdBy` TEXT, `projectId` TEXT, `collectionId` TEXT, `parentCollection_id` TEXT, PRIMARY KEY(`id`)"+ ", FOREIGN KEY(`parentCollection_id`) REFERENCES " + com.raizlabs.android.dbflow.config.FlowManager.getTableName(com.construct.v2.models.Collection.class) + "(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION" + ");";
  }

  @Override
  public final void loadFromCursor(FlowCursor cursor, File model) {
    model.set_id(cursor.getStringOrDefault("id"));
    model.setFilename(cursor.getStringOrDefault("filename"));
    model.setUrl(cursor.getStringOrDefault("url"));
    model.setPath(cursor.getStringOrDefault("path"));
    model.setMimeType(cursor.getStringOrDefault("mimeType"));
    model.setCaption(cursor.getStringOrDefault("caption"));
    model.setLength(cursor.getLongOrDefault("length"));
    int index_createdAt = cursor.getColumnIndex("createdAt");
    if (index_createdAt != -1 && !cursor.isNull(index_createdAt)) {
      model.setCreatedAt(global_typeConverterDateConverter.getModelValue(cursor.getLong(index_createdAt)));
    } else {
      model.setCreatedAt(global_typeConverterDateConverter.getModelValue(null));
    }
    int index_takenAt = cursor.getColumnIndex("takenAt");
    if (index_takenAt != -1 && !cursor.isNull(index_takenAt)) {
      model.setTakenAt(global_typeConverterDateConverter.getModelValue(cursor.getLong(index_takenAt)));
    } else {
      model.setTakenAt(global_typeConverterDateConverter.getModelValue(null));
    }
    int index_updatedAt = cursor.getColumnIndex("updatedAt");
    if (index_updatedAt != -1 && !cursor.isNull(index_updatedAt)) {
      model.setUpdatedAt(global_typeConverterDateConverter.getModelValue(cursor.getLong(index_updatedAt)));
    } else {
      model.setUpdatedAt(global_typeConverterDateConverter.getModelValue(null));
    }
    model.setCreatedBy(cursor.getStringOrDefault("createdBy"));
    model.setProjectId(cursor.getStringOrDefault("projectId"));
    model.setCollectionId(cursor.getStringOrDefault("collectionId"));
    int index_parentCollection_id_Collection_Table = cursor.getColumnIndex("parentCollection_id");
    if (index_parentCollection_id_Collection_Table != -1 && !cursor.isNull(index_parentCollection_id_Collection_Table)) {
      model.parentCollection = new Collection();
      model.parentCollection.setId(cursor.getString(index_parentCollection_id_Collection_Table));
    } else {
      model.parentCollection = null;
    }
  }

  @Override
  public final boolean exists(File model, DatabaseWrapper wrapper) {
    return SQLite.selectCountOf()
    .from(File.class)
    .where(getPrimaryConditionClause(model))
    .hasData(wrapper);
  }

  @Override
  public final OperatorGroup getPrimaryConditionClause(File model) {
    OperatorGroup clause = OperatorGroup.clause();
    clause.and(id.eq(model.get_id()));
    return clause;
  }
}

0 个答案:

没有答案