use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class LitePalBase method getTableModel.
/**
* This method is used to get the table model by the class name passed
* in. The principle to generate table model is that each field in the class
* with non-static modifier and has a type among int/Integer, long/Long,
* short/Short, float/Float, double/Double, char/Character, boolean/Boolean
* or String, would generate a column with same name as corresponding field.
* If users don't want some of the fields map a column, declare an ignore
* annotation with {@link Column#ignore()}.
*
* @param className
* The full name of the class to map in database.
* @return A table model with table name, class name and the map of column
* name and column type.
*/
protected TableModel getTableModel(String className) {
String tableName = DBUtility.getTableNameByClassName(className);
TableModel tableModel = new TableModel();
tableModel.setTableName(tableName);
tableModel.setClassName(className);
List<Field> supportedFields = getSupportedFields(className);
for (Field field : supportedFields) {
ColumnModel columnModel = convertFieldToColumnModel(field);
tableModel.addColumnModel(columnModel);
}
return tableModel;
}
use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class AssociationCreator method createIntermediateTable.
/**
* When it comes to many2many associations. Database need to create an
* intermediate table for mapping this association. This method helps create
* such a table, and the table name follows the concatenation of the two
* target table names in alphabetical order with underline in the middle.
*
* @param tableName
* The table name.
* @param associatedTableName
* The associated table name.
* @param db
* Instance of SQLiteDatabase.
* @param force
* Drop the table first if it already exists.
*/
private void createIntermediateTable(String tableName, String associatedTableName, SQLiteDatabase db, boolean force) {
List<ColumnModel> columnModelList = new ArrayList<ColumnModel>();
ColumnModel column1 = new ColumnModel();
column1.setColumnName(tableName + "_id");
column1.setColumnType("integer");
ColumnModel column2 = new ColumnModel();
column2.setColumnName(associatedTableName + "_id");
column2.setColumnType("integer");
columnModelList.add(column1);
columnModelList.add(column2);
String intermediateTableName = DBUtility.getIntermediateTableName(tableName, associatedTableName);
List<String> sqls = new ArrayList<String>();
if (DBUtility.isTableExists(intermediateTableName, db)) {
if (force) {
sqls.add(generateDropTableSQL(intermediateTableName));
sqls.add(generateCreateTableSQL(intermediateTableName, columnModelList, false));
}
} else {
sqls.add(generateCreateTableSQL(intermediateTableName, columnModelList, false));
}
execute(sqls, db);
giveTableSchemaACopy(intermediateTableName, Const.TableSchema.INTERMEDIATE_JOIN_TABLE, db);
}
use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class AssociationCreator method addForeignKeyColumn.
/**
* This method is used to add many to one association or one to one
* association on tables. It will automatically build a SQL to add foreign
* key to a table. If the passed in table name or associated table name
* doesn't exist, it will throw an exception.
*
* @param tableName
* The table name.
* @param associatedTableName
* The associated table name.
* @param tableHoldsForeignKey
* The table which holds the foreign key.
* @param db
* Instance of SQLiteDatabase.
*
* @throws org.litepal.exceptions.DatabaseGenerateException
*/
protected void addForeignKeyColumn(String tableName, String associatedTableName, String tableHoldsForeignKey, SQLiteDatabase db) {
if (DBUtility.isTableExists(tableName, db)) {
if (DBUtility.isTableExists(associatedTableName, db)) {
String foreignKeyColumn = null;
if (tableName.equals(tableHoldsForeignKey)) {
foreignKeyColumn = getForeignKeyColumnName(associatedTableName);
} else if (associatedTableName.equals(tableHoldsForeignKey)) {
foreignKeyColumn = getForeignKeyColumnName(tableName);
}
if (!DBUtility.isColumnExists(foreignKeyColumn, tableHoldsForeignKey, db)) {
ColumnModel columnModel = new ColumnModel();
columnModel.setColumnName(foreignKeyColumn);
columnModel.setColumnType("integer");
List<String> sqls = new ArrayList<String>();
sqls.add(generateAddColumnSQL(tableHoldsForeignKey, columnModel));
execute(sqls, db);
} else {
LogUtil.d(TAG, "column " + foreignKeyColumn + " is already exist, no need to add one");
}
} else {
throw new DatabaseGenerateException(DatabaseGenerateException.TABLE_DOES_NOT_EXIST + associatedTableName);
}
} else {
throw new DatabaseGenerateException(DatabaseGenerateException.TABLE_DOES_NOT_EXIST + tableName);
}
}
use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class AssociationCreator method createGenericTable.
/**
* When declared generic collection fields in model class. Database need to create
* generic tables for mapping these fields. This method helps create such a table.
*
* @param genericModel
* The GenericModel instance.
* @param db
* Instance of SQLiteDatabase.
* @param force
* Drop the table first if it already exists.
*/
private void createGenericTable(GenericModel genericModel, SQLiteDatabase db, boolean force) {
String tableName = genericModel.getTableName();
String valueColumnName = genericModel.getValueColumnName();
String valueColumnType = genericModel.getValueColumnType();
String valueIdColumnName = genericModel.getValueIdColumnName();
List<ColumnModel> columnModelList = new ArrayList<ColumnModel>();
ColumnModel column1 = new ColumnModel();
column1.setColumnName(valueColumnName);
column1.setColumnType(valueColumnType);
ColumnModel column2 = new ColumnModel();
column2.setColumnName(valueIdColumnName);
column2.setColumnType("integer");
columnModelList.add(column1);
columnModelList.add(column2);
List<String> sqls = new ArrayList<String>();
if (DBUtility.isTableExists(tableName, db)) {
if (force) {
sqls.add(generateDropTableSQL(tableName));
sqls.add(generateCreateTableSQL(tableName, columnModelList, false));
}
} else {
sqls.add(generateCreateTableSQL(tableName, columnModelList, false));
}
execute(sqls, db);
giveTableSchemaACopy(tableName, Const.TableSchema.GENERIC_TABLE, db);
}
use of org.litepal.tablemanager.model.ColumnModel in project LitePal by LitePalFramework.
the class AssociationCreator method generateCreateTableSQL.
/**
* Generate a create table SQL by the passed in parameters. Note that it
* will always generate a SQL with id/_id column in it as primary key and
* this id is auto increment as integer if the autoIncrementId is true, or
* no primary key will be added.
*
* @param tableName
* The table name.
* @param columnModels
* A list contains all column models with column info.
* @param autoIncrementId
* Generate an auto increment id or not. Only intermediate join table doesn't need
* an auto increment id.
* @return A generated create table SQL.
*/
protected String generateCreateTableSQL(String tableName, List<ColumnModel> columnModels, boolean autoIncrementId) {
StringBuilder createTableSQL = new StringBuilder("create table ");
createTableSQL.append(tableName).append(" (");
if (autoIncrementId) {
createTableSQL.append("id integer primary key autoincrement,");
}
if (isContainsOnlyIdField(columnModels)) {
// Remove the last comma when only have id field in model.
createTableSQL.deleteCharAt(createTableSQL.length() - 1);
}
boolean needSeparator = false;
for (ColumnModel columnModel : columnModels) {
if (columnModel.isIdColumn()) {
continue;
}
if (needSeparator) {
createTableSQL.append(", ");
}
needSeparator = true;
createTableSQL.append(columnModel.getColumnName()).append(" ").append(columnModel.getColumnType());
if (!columnModel.isNullable()) {
createTableSQL.append(" not null");
}
if (columnModel.isUnique()) {
createTableSQL.append(" unique");
}
String defaultValue = columnModel.getDefaultValue();
if (!TextUtils.isEmpty(defaultValue)) {
createTableSQL.append(" default ").append(defaultValue);
}
}
createTableSQL.append(")");
LogUtil.d(TAG, "create table sql is >> " + createTableSQL);
return createTableSQL.toString();
}
Aggregations