Search in sources :

Example 6 with ColumnModel

use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.

the class Upgrader method generateAddForeignKeySQL.

/**
 * Generate a SQL List for adding foreign keys. Changing constraints job should remain all the
 * existing columns including foreign keys. This method add origin foreign keys after creating
 * table.
 * @return A SQL List for adding foreign keys.
 */
private List<String> generateAddForeignKeySQL() {
    List<String> addForeignKeySQLs = new ArrayList<String>();
    List<String> foreignKeyColumns = getForeignKeyColumns(mTableModel);
    for (String foreignKeyColumn : foreignKeyColumns) {
        if (!mTableModel.containsColumn(foreignKeyColumn)) {
            ColumnModel columnModel = new ColumnModel();
            columnModel.setColumnName(foreignKeyColumn);
            columnModel.setColumnType("integer");
            addForeignKeySQLs.add(generateAddColumnSQL(mTableModel.getTableName(), columnModel));
        }
    }
    return addForeignKeySQLs;
}
Also used : ArrayList(java.util.ArrayList) ColumnModel(org.litepal.tablemanager.model.ColumnModel)

Example 7 with ColumnModel

use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.

the class Upgrader method findColumnsToRemove.

/**
 * This method helps find the difference between table model from class and
 * table model from database. Database should always be synchronized with
 * model class. If there're some fields are removed from class, the table
 * model from database will be compared to find out which fields are
 * removed. But there're still some exceptions. The columns named id or _id
 * won't ever be removed. The foreign key column will be checked some where
 * else, not from here.
 *
 * @return A list with column names need to remove.
 */
private List<String> findColumnsToRemove() {
    String tableName = mTableModel.getTableName();
    List<String> removeColumns = new ArrayList<String>();
    List<ColumnModel> columnModelList = mTableModelDB.getColumnModels();
    for (ColumnModel columnModel : columnModelList) {
        String dbColumnName = columnModel.getColumnName();
        if (isNeedToRemove(dbColumnName)) {
            removeColumns.add(dbColumnName);
        }
    }
    LogUtil.d(TAG, "remove columns from " + tableName + " >> " + removeColumns);
    return removeColumns;
}
Also used : ArrayList(java.util.ArrayList) ColumnModel(org.litepal.tablemanager.model.ColumnModel)

Example 8 with ColumnModel

use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.

the class Upgrader method changeColumnsType.

/**
 * When some fields type are changed in class, the table should drop the
 * before columns and create new columns with same name but new types.
 *
 * @param columnModelList
 *            List with ColumnModel to change column type.
 */
private void changeColumnsType(List<ColumnModel> columnModelList) {
    LogUtil.d(TAG, "do changeColumnsType");
    List<String> columnNames = new ArrayList<String>();
    if (columnModelList != null && !columnModelList.isEmpty()) {
        for (ColumnModel columnModel : columnModelList) {
            columnNames.add(columnModel.getColumnName());
        }
    }
    removeColumns(columnNames);
    addColumns(columnModelList);
}
Also used : ArrayList(java.util.ArrayList) ColumnModel(org.litepal.tablemanager.model.ColumnModel)

Example 9 with ColumnModel

use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.

the class TableStructureActivity method analyzeTableStructure.

private void analyzeTableStructure() {
    TableModel tableMode = DBUtility.findPragmaTableInfo(mTableName, Connector.getDatabase());
    List<ColumnModel> columnModelList = tableMode.getColumnModels();
    mList.addAll(columnModelList);
}
Also used : ColumnModel(org.litepal.tablemanager.model.ColumnModel) TableModel(org.litepal.tablemanager.model.TableModel)

Example 10 with ColumnModel

use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.

the class LitePalBase method convertFieldToColumnModel.

/**
 * Convert a field instance into A ColumnModel instance. ColumnModel can provide information
 * when creating table.
 * @param field
 *          A supported field to map into column.
 * @return ColumnModel instance contains column information.
 */
private ColumnModel convertFieldToColumnModel(Field field) {
    String fieldType = field.getType().getName();
    String columnType = getColumnType(fieldType);
    boolean nullable = true;
    boolean unique = false;
    String defaultValue = "";
    Column annotation = field.getAnnotation(Column.class);
    if (annotation != null) {
        nullable = annotation.nullable();
        unique = annotation.unique();
        defaultValue = annotation.defaultValue();
    }
    ColumnModel columnModel = new ColumnModel();
    columnModel.setColumnName(DBUtility.convertToValidColumnName(field.getName()));
    columnModel.setColumnType(columnType);
    columnModel.setNullable(nullable);
    columnModel.setUnique(unique);
    columnModel.setDefaultValue(defaultValue);
    return columnModel;
}
Also used : Column(org.litepal.annotation.Column) ColumnModel(org.litepal.tablemanager.model.ColumnModel)

Aggregations

ColumnModel (org.litepal.tablemanager.model.ColumnModel)14 ArrayList (java.util.ArrayList)7 TableModel (org.litepal.tablemanager.model.TableModel)3 DatabaseGenerateException (org.litepal.exceptions.DatabaseGenerateException)2 Cursor (android.database.Cursor)1 Field (java.lang.reflect.Field)1 Column (org.litepal.annotation.Column)1