use of com.raizlabs.android.dbflow.structure.ModelAdapter in project DBFlow by Raizlabs.
the class DatabaseDefinition method reset.
/**
* Performs a full deletion of this database. Reopens the {@link FlowSQLiteOpenHelper} as well.
*
* @param context Where the database resides
*/
public void reset(Context context) {
if (!isResetting) {
isResetting = true;
getTransactionManager().stopQueue();
getHelper().closeDB();
for (ModelAdapter modelAdapter : modelAdapters.values()) {
modelAdapter.closeInsertStatement();
modelAdapter.closeCompiledStatement();
}
context.deleteDatabase(getDatabaseFileName());
// recreate queue after interrupting it.
if (databaseConfig == null || databaseConfig.transactionManagerCreator() == null) {
transactionManager = new DefaultTransactionManager(this);
} else {
transactionManager = databaseConfig.transactionManagerCreator().createManager(this);
}
openHelper = null;
isResetting = false;
getHelper().getDatabase();
}
}
use of com.raizlabs.android.dbflow.structure.ModelAdapter in project DBFlow by Raizlabs.
the class FlowManager method getTableName.
/**
* Returns the table name for the specific model class
*
* @param table The class that implements {@link Model}
* @return The table name, which can be different than the {@link Model} class name
*/
@SuppressWarnings("unchecked")
public static String getTableName(Class<?> table) {
ModelAdapter modelAdapter = getModelAdapter(table);
String tableName = null;
if (modelAdapter == null) {
ModelViewAdapter modelViewAdapter = getDatabaseForTable(table).getModelViewAdapterForTable(table);
if (modelViewAdapter != null) {
tableName = modelViewAdapter.getViewName();
}
} else {
tableName = modelAdapter.getTableName();
}
return tableName;
}
use of com.raizlabs.android.dbflow.structure.ModelAdapter in project DBFlow by Raizlabs.
the class ContentUtils method insert.
/**
* Inserts the model into the {@link android.content.ContentResolver}. Uses the insertUri to resolve
* the reference and the model to convert its data into {@link android.content.ContentValues}
*
* @param contentResolver The content resolver to use (if different from {@link FlowManager#getContext()})
* @param insertUri A {@link android.net.Uri} from the {@link ContentProvider} class definition.
* @param model The model to insert.
* @param <TableClass> The class that implements {@link Model}
* @return The Uri of the inserted data.
*/
@SuppressWarnings("unchecked")
public static <TableClass> Uri insert(ContentResolver contentResolver, Uri insertUri, TableClass model) {
ModelAdapter<TableClass> adapter = (ModelAdapter<TableClass>) FlowManager.getModelAdapter(model.getClass());
ContentValues contentValues = new ContentValues();
adapter.bindToInsertValues(contentValues, model);
Uri uri = contentResolver.insert(insertUri, contentValues);
adapter.updateAutoIncrement(model, Long.valueOf(uri.getPathSegments().get(uri.getPathSegments().size() - 1)));
return uri;
}
use of com.raizlabs.android.dbflow.structure.ModelAdapter in project DBFlow by Raizlabs.
the class BaseDatabaseHelper method executeCreations.
/**
* This method executes CREATE TABLE statements as well as CREATE VIEW on the database passed.
*/
protected void executeCreations(final DatabaseWrapper database) {
try {
database.beginTransaction();
List<ModelAdapter> modelAdapters = databaseDefinition.getModelAdapters();
for (ModelAdapter modelAdapter : modelAdapters) {
try {
database.execSQL(modelAdapter.getCreationQuery());
} catch (SQLiteException e) {
FlowLog.logError(e);
}
}
// create our model views
List<ModelViewAdapter> modelViews = databaseDefinition.getModelViewAdapters();
for (ModelViewAdapter modelView : modelViews) {
QueryBuilder queryBuilder = new QueryBuilder().append("CREATE VIEW IF NOT EXISTS").appendSpaceSeparated(modelView.getViewName()).append("AS ").append(modelView.getCreationQuery());
try {
database.execSQL(queryBuilder.getQuery());
} catch (SQLiteException e) {
FlowLog.logError(e);
}
}
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}
use of com.raizlabs.android.dbflow.structure.ModelAdapter in project DBFlow by Raizlabs.
the class ContentUtils method update.
/**
* Updates the model through the {@link android.content.ContentResolver}. Uses the updateUri to
* resolve the reference and the model to convert its data in {@link android.content.ContentValues}
*
* @param contentResolver The content resolver to use (if different from {@link FlowManager#getContext()})
* @param updateUri A {@link android.net.Uri} from the {@link ContentProvider}
* @param model The model to update
* @param <TableClass> The class that implements {@link Model}
* @return The number of rows updated.
*/
@SuppressWarnings("unchecked")
public static <TableClass> int update(ContentResolver contentResolver, Uri updateUri, TableClass model) {
ModelAdapter<TableClass> adapter = (ModelAdapter<TableClass>) FlowManager.getModelAdapter(model.getClass());
ContentValues contentValues = new ContentValues();
adapter.bindToContentValues(contentValues, model);
int count = contentResolver.update(updateUri, contentValues, adapter.getPrimaryConditionClause(model).getQuery(), null);
if (count == 0) {
FlowLog.log(FlowLog.Level.W, "Updated failed of: " + model.getClass());
}
return count;
}
Aggregations