Example #1
0
  private void createTable(SQLiteDatabase database, List<Column> existingColumns, String name) {

    ColumnConsumer consumer = new ColumnConsumer();

    SQL sql = new SQL();

    sql.raw("CREATE TABLE ");
    sql.escaped(name);
    sql.raw(" (");
    for (Column column : existingColumns) {
      Column consumed = consumer.alter(column);
      if (consumed != null) {
        sql.separate(", ");
        sql.raw(consumed.ddl());
      }
    }
    for (AlterColumn alter : consumer.alters) {
      if (alter instanceof CreateColumn) {
        sql.separate(", ");
        sql.raw(alter.alter(null).ddl());
      } else {
        throw new SQLException(alter.toString());
      }
    }
    sql.raw(")");

    database.execSQL(sql.toString());
  }
Example #2
0
  @Override
  public void apply(SQLiteDatabase database) throws SQLException {

    List<Column> existingColumns = Column.get(oldName, database);

    if (alters.isEmpty()) {
      if (!existingColumns.isEmpty()) {
        renameTable(database, oldName, newName);
      }
    } else {
      if (existingColumns.isEmpty()) {
        createTable(database, existingColumns, newName);
      } else {
        String temp = oldName + "_" + newName;

        createTable(database, existingColumns, temp);

        moveRows(database, existingColumns, oldName, temp);

        dropTable(database, oldName);

        renameTable(database, temp, newName);
      }
    }
  }