use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class AssociationUpdater method getForeignKeyColumns.
/**
* This method looks around all the columns in the table, and judge which of
* them are foreign key columns.
*
* @param tableModel
* Use the TableModel to get table name and columns name to
* generate SQL.
* @return All the foreign key columns in a list.
*/
protected List<String> getForeignKeyColumns(TableModel tableModel) {
List<String> foreignKeyColumns = new ArrayList<String>();
List<ColumnModel> columnModelList = getTableModelFromDB(tableModel.getTableName()).getColumnModels();
for (ColumnModel columnModel : columnModelList) {
String columnName = columnModel.getColumnName();
if (isForeignKeyColumnFormat(columnModel.getColumnName())) {
if (!tableModel.containsColumn(columnName)) {
// Now this is a foreign key column.
LogUtil.d(TAG, "getForeignKeyColumnNames >> foreign key column is " + columnName);
foreignKeyColumns.add(columnName);
}
}
}
return foreignKeyColumns;
}
use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class AssociationUpdater method generateDataMigrationSQL.
/**
* Generate a SQL to do the data migration job to avoid losing data.
*
* @param tableModel
* Which contains table name use to migrate data.
* @return SQL to migrate data.
*/
protected String generateDataMigrationSQL(TableModel tableModel) {
String tableName = tableModel.getTableName();
List<ColumnModel> columnModels = tableModel.getColumnModels();
if (!columnModels.isEmpty()) {
StringBuilder sql = new StringBuilder();
sql.append("insert into ").append(tableName).append("(");
boolean needComma = false;
for (ColumnModel columnModel : columnModels) {
if (needComma) {
sql.append(", ");
}
needComma = true;
sql.append(columnModel.getColumnName());
}
sql.append(") ");
sql.append("select ");
needComma = false;
for (ColumnModel columnModel : columnModels) {
if (needComma) {
sql.append(", ");
}
needComma = true;
sql.append(columnModel.getColumnName());
}
sql.append(" from ").append(getTempTableName(tableName));
return sql.toString();
} else {
return null;
}
}
use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class Upgrader method addColumns.
/**
* When some fields are added into the class after last upgrade, the table
* should synchronize the changes by adding the corresponding columns.
*
* @param columnModelList
* List with ColumnModel to add new column.
*/
private void addColumns(List<ColumnModel> columnModelList) {
LogUtil.d(TAG, "do addColumn");
execute(getAddColumnSQLs(columnModelList), mDb);
for (ColumnModel columnModel : columnModelList) {
mTableModelDB.addColumnModel(columnModel);
}
}
use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class DBUtility method findPragmaTableInfo.
/**
* Look from the database to find a table named same as the table name in
* table model. Then iterate the columns and types of this table to create a
* new instance of table model. If there's no such a table in the database,
* then throw DatabaseGenerateException.
*
* @param tableName
* Table name.
* @param db
* Instance of SQLiteDatabase.
* @return A table model object with values from database table.
* @throws org.litepal.exceptions.DatabaseGenerateException
*/
public static TableModel findPragmaTableInfo(String tableName, SQLiteDatabase db) {
if (isTableExists(tableName, db)) {
List<String> uniqueColumns = findUniqueColumns(tableName, db);
TableModel tableModelDB = new TableModel();
tableModelDB.setTableName(tableName);
String checkingColumnSQL = "pragma table_info(" + tableName + ")";
Cursor cursor = null;
try {
cursor = db.rawQuery(checkingColumnSQL, null);
if (cursor.moveToFirst()) {
do {
ColumnModel columnModel = new ColumnModel();
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
boolean nullable = cursor.getInt(cursor.getColumnIndexOrThrow("notnull")) != 1;
boolean unique = uniqueColumns.contains(name);
String defaultValue = cursor.getString(cursor.getColumnIndexOrThrow("dflt_value"));
columnModel.setColumnName(name);
columnModel.setColumnType(type);
columnModel.setNullable(nullable);
columnModel.setUnique(unique);
if (defaultValue != null) {
defaultValue = defaultValue.replace("'", "");
} else {
defaultValue = "";
}
columnModel.setDefaultValue(defaultValue);
tableModelDB.addColumnModel(columnModel);
} while (cursor.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
throw new DatabaseGenerateException(e.getMessage());
} finally {
if (cursor != null) {
cursor.close();
}
}
return tableModelDB;
} else {
throw new DatabaseGenerateException(DatabaseGenerateException.TABLE_DOES_NOT_EXIST_WHEN_EXECUTING + tableName);
}
}
Aggregations