com.raizlabs.android.dbflow.structure.database.DatabaseWrapper Java Examples

The following examples show how to use com.raizlabs.android.dbflow.structure.database.DatabaseWrapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CacheableListModelSaver.java    From Meteorite with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void saveAll(@NonNull Collection<TModel> tableCollection,
                                 @NonNull DatabaseWrapper wrapper) {
    // skip if empty.
    if (tableCollection.isEmpty()) {
        return;
    }

    ModelSaver<TModel> modelSaver = getModelSaver();
    ModelAdapter<TModel> modelAdapter = modelSaver.getModelAdapter();
    DatabaseStatement statement = modelAdapter.getInsertStatement(wrapper);
    DatabaseStatement updateStatement = modelAdapter.getUpdateStatement(wrapper);
    try {
        for (TModel model : tableCollection) {
            if (modelSaver.save(model, wrapper, statement, updateStatement)) {
                modelAdapter.storeModelInCache(model);
            }
        }
    } finally {
        updateStatement.close();
        statement.close();
    }
}
 
Example #2
Source File: ProcessModelTransaction.java    From Meteorite with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(DatabaseWrapper databaseWrapper) {
    if (models != null) {
        final int size = models.size();
        for (int i = 0; i < size; i++) {
            final TModel model = models.get(i);
            processModel.processModel(model, databaseWrapper);

            if (processListener != null) {
                if (runProcessListenerOnSameThread) {
                    processListener.onModelProcessed(i, size, model);
                } else {
                    final int finalI = i;
                    Transaction.getTransactionHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            processListener.onModelProcessed(finalI, size, model);
                        }
                    });
                }
            }
        }
    }
}
 
Example #3
Source File: ModelSaver.java    From Meteorite with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public synchronized boolean save(@NonNull TModel model,
                                 @NonNull DatabaseWrapper wrapper,
                                 @NonNull DatabaseStatement insertStatement,
                                 @NonNull DatabaseStatement updateStatement) {
    boolean exists = modelAdapter.exists(model, wrapper);

    if (exists) {
        exists = update(model, wrapper, updateStatement);
    }

    if (!exists) {
        exists = insert(model, insertStatement, wrapper) > INSERT_FAILED;
    }

    if (exists) {
        NotifyDistributor.get().notifyModelChanged(model, modelAdapter, BaseModel.Action.SAVE);
    }

    // return successful store into db.
    return exists;
}
 
Example #4
Source File: CacheableListModelSaver.java    From Meteorite with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void insertAll(@NonNull Collection<TModel> tableCollection,
                                   @NonNull DatabaseWrapper wrapper) {
    // skip if empty.
    if (tableCollection.isEmpty()) {
        return;
    }

    ModelSaver<TModel> modelSaver = getModelSaver();
    ModelAdapter<TModel> modelAdapter = modelSaver.getModelAdapter();
    DatabaseStatement statement = modelAdapter.getInsertStatement(wrapper);
    try {
        for (TModel model : tableCollection) {
            if (modelSaver.insert(model, statement, wrapper) > 0) {
                modelAdapter.storeModelInCache(model);
            }
        }
    } finally {
        statement.close();
    }
}
 
Example #5
Source File: CacheableListModelSaver.java    From Meteorite with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void updateAll(@NonNull Collection<TModel> tableCollection,
                                   @NonNull DatabaseWrapper wrapper) {
    // skip if empty.
    if (tableCollection.isEmpty()) {
        return;
    }
    ModelSaver<TModel> modelSaver = getModelSaver();
    ModelAdapter<TModel> modelAdapter = modelSaver.getModelAdapter();
    DatabaseStatement statement = modelAdapter.getUpdateStatement(wrapper);
    try {
        for (TModel model : tableCollection) {
            if (modelSaver.update(model, wrapper, statement)) {
                modelAdapter.storeModelInCache(model);
            }
        }
    } finally {
        statement.close();
    }
}
 
Example #6
Source File: ModelSaver.java    From Meteorite with Apache License 2.0 6 votes vote down vote up
/**
 * @see #update(Object, DatabaseWrapper, DatabaseStatement)
 */
@Deprecated
@SuppressWarnings("unchecked")
public synchronized boolean update(@NonNull TModel model,
                                   @NonNull DatabaseWrapper wrapper,
                                   @NonNull ContentValues contentValues) {
    modelAdapter.saveForeignKeys(model, wrapper);
    modelAdapter.bindToContentValues(contentValues, model);
    boolean successful = wrapper.updateWithOnConflict(modelAdapter.getTableName(), contentValues,
        modelAdapter.getPrimaryConditionClause(model).getQuery(), null,
        ConflictAction.getSQLiteDatabaseAlgorithmInt(modelAdapter.getUpdateOnConflictAction())) != 0;
    if (successful) {
        NotifyDistributor.get().notifyModelChanged(model, modelAdapter, BaseModel.Action.UPDATE);
    }
    return successful;
}
 
Example #7
Source File: DatabaseDefinition.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
public void executeTransaction(@NonNull ITransaction transaction) {
    DatabaseWrapper database = getWritableDatabase();
    try {
        database.beginTransaction();
        transaction.execute(database);
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}
 
Example #8
Source File: FastStoreModelTransaction.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@NonNull
public static <TModel> Builder<TModel> updateBuilder(@NonNull InternalAdapter<TModel> internalAdapter) {
    return new Builder<>(new ProcessModelList<TModel>() {
        @Override
        public void processModel(@NonNull List<TModel> tModels, InternalAdapter<TModel> adapter, DatabaseWrapper wrapper) {
            adapter.updateAll(tModels, wrapper);
        }
    }, internalAdapter);
}
 
Example #9
Source File: Wind_Table.java    From WeatherStream with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean exists(WeatherData.Wind model, DatabaseWrapper wrapper) {
  return (model.getId() != null && model.getId() > 0 || model.getId() == null)
  && SQLite.selectCountOf()
  .from(WeatherData.Wind.class)
  .where(getPrimaryConditionClause(model))
  .hasData(wrapper);
}
 
Example #10
Source File: Sys_Table.java    From WeatherStream with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean exists(WeatherData.Sys model, DatabaseWrapper wrapper) {
  return (model.getSysId() != null && model.getSysId() > 0 || model.getSysId() == null)
  && SQLite.selectCountOf()
  .from(WeatherData.Sys.class)
  .where(getPrimaryConditionClause(model))
  .hasData(wrapper);
}
 
Example #11
Source File: Index.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
public void enable(@NonNull DatabaseWrapper databaseWrapper) {
    if (table == null) {
        throw new IllegalStateException("Please call on() to set a table to use this index on.");
    } else if (columns == null || columns.isEmpty()) {
        throw new IllegalStateException("There should be at least one column in this index");
    }
    databaseWrapper.execSQL(getQuery());
}
 
Example #12
Source File: IndexPropertyMigration.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@Override
public void migrate(@NonNull DatabaseWrapper database) {
    if (shouldCreate()) {
        getIndexProperty().createIfNotExists(database);
    } else {
        getIndexProperty().drop(database);
    }
}
 
Example #13
Source File: CacheableListModelSaver.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void deleteAll(@NonNull Collection<TModel> tableCollection,
                                   @NonNull DatabaseWrapper wrapper) {
    // skip if empty.
    if (tableCollection.isEmpty()) {
        return;
    }

    ModelSaver<TModel> modelSaver = getModelSaver();
    for (TModel model : tableCollection) {
        if (modelSaver.delete(model, wrapper)) {
            getModelSaver().getModelAdapter().removeModelFromCache(model);
        }
    }
}
 
Example #14
Source File: FastStoreModelTransaction.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@NonNull
public static <TModel> Builder<TModel> saveBuilder(@NonNull InternalAdapter<TModel> internalAdapter) {
    return new Builder<>(new ProcessModelList<TModel>() {
        @Override
        public void processModel(@NonNull List<TModel> tModels, InternalAdapter<TModel> adapter, DatabaseWrapper wrapper) {
            adapter.saveAll(tModels, wrapper);
        }
    }, internalAdapter);
}
 
Example #15
Source File: ForecastList_Table.java    From WeatherStream with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean exists(WeatherForecastData.ForecastList model, DatabaseWrapper wrapper) {
  return (model.getId() != null && model.getId() > 0 || model.getId() == null)
  && SQLite.selectCountOf()
  .from(WeatherForecastData.ForecastList.class)
  .where(getPrimaryConditionClause(model))
  .hasData(wrapper);
}
 
Example #16
Source File: Weather_Table.java    From WeatherStream with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean exists(WeatherData.Weather model, DatabaseWrapper wrapper) {
  return (model.getWId() != null && model.getWId() > 0 || model.getWId() == null)
  && SQLite.selectCountOf()
  .from(WeatherData.Weather.class)
  .where(getPrimaryConditionClause(model))
  .hasData(wrapper);
}
 
Example #17
Source File: WeatherForecastData_Table.java    From WeatherStream with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean exists(WeatherForecastData model, DatabaseWrapper wrapper) {
  return SQLite.selectCountOf()
  .from(WeatherForecastData.class)
  .where(getPrimaryConditionClause(model))
  .hasData(wrapper);
}
 
Example #18
Source File: ModelSaver.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public synchronized boolean delete(@NonNull TModel model, @NonNull DatabaseWrapper wrapper) {
    DatabaseStatement deleteStatement = modelAdapter.getDeleteStatement(wrapper);
    boolean success = false;
    try {
        success = delete(model, deleteStatement, wrapper);
    } finally {
        // since we generate an insert every time, we can safely close the statement here.
        deleteStatement.close();
    }
    return success;
}
 
Example #19
Source File: RetrievalAdapter.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
/**
 * Force loads the model from the DB. Even if caching is enabled it will requery the object.
 */
public void load(@NonNull TModel model, DatabaseWrapper databaseWrapper) {
    getNonCacheableSingleModelLoader().load(databaseWrapper,
        SQLite.select()
            .from(getModelClass())
            .where(getPrimaryConditionClause(model)).getQuery(),
        model);
}
 
Example #20
Source File: FastStoreModelTransaction.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@NonNull
public static <TModel> Builder<TModel> insertBuilder(@NonNull InternalAdapter<TModel> internalAdapter) {
    return new Builder<>(new ProcessModelList<TModel>() {
        @Override
        public void processModel(@NonNull List<TModel> tModels, InternalAdapter<TModel> adapter, DatabaseWrapper wrapper) {
            adapter.insertAll(tModels, wrapper);
        }
    }, internalAdapter);
}
 
Example #21
Source File: BaseQueriable.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public DatabaseStatement compileStatement(@NonNull DatabaseWrapper databaseWrapper) {
    String query = getQuery();
    FlowLog.log(FlowLog.Level.V, "Compiling Query Into Statement: " + query);
    return new DatabaseStatementWrapper<>(databaseWrapper.compileStatement(query), this);
}
 
Example #22
Source File: BaseQueriable.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(@NonNull DatabaseWrapper databaseWrapper) {
    Cursor cursor = query(databaseWrapper);
    if (cursor != null) {
        cursor.close();
    } else {
        // we dont query, we're executing something here.
        NotifyDistributor.get().notifyTableChanged(getTable(), getPrimaryAction());
    }
}
 
Example #23
Source File: Clouds_Table.java    From WeatherStream with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean exists(WeatherData.Clouds model, DatabaseWrapper wrapper) {
  return (model.getId() != null && model.getId() > 0 || model.getId() == null)
  && SQLite.selectCountOf()
  .from(WeatherData.Clouds.class)
  .where(getPrimaryConditionClause(model))
  .hasData(wrapper);
}
 
Example #24
Source File: BaseContentProvider.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@Override
public int bulkInsert(@NonNull final Uri uri, @NonNull final ContentValues[] values) {
    final int[] count = {0};
    getDatabase().executeTransaction(new ITransaction() {
        @Override
        public void execute(DatabaseWrapper databaseWrapper) {
            for (ContentValues contentValues : values) {
                count[0] += bulkInsert(uri, contentValues);
            }
        }
    });
    //noinspection ConstantConditions
    getContext().getContentResolver().notifyChange(uri, null);
    return count[0];
}
 
Example #25
Source File: AsyncModel.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@Override
public boolean update() {
    executeTransaction(new ProcessModelTransaction.Builder<>(
        new ProcessModelTransaction.ProcessModel<TModel>() {
            @Override
            public void processModel(TModel model, DatabaseWrapper wrapper) {
                getModelAdapter().update(model, wrapper);
            }
        }).add(model).build());
    return false;
}
 
Example #26
Source File: WeatherData_Table.java    From WeatherStream with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean exists(WeatherData model, DatabaseWrapper wrapper) {
  return SQLite.selectCountOf()
  .from(WeatherData.class)
  .where(getPrimaryConditionClause(model))
  .hasData(wrapper);
}
 
Example #27
Source File: DBBatchSaveQueue.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@Override
public void processModel(Object model, DatabaseWrapper wrapper) {
    if (model instanceof Model) {
        ((Model) model).save();
    } else if (model != null) {
        Class modelClass = model.getClass();
        //noinspection unchecked
        FlowManager.getModelAdapter(modelClass).save(model);
    }
}
 
Example #28
Source File: AsyncModel.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@Override
public boolean delete() {
    executeTransaction(new ProcessModelTransaction.Builder<>(
        new ProcessModelTransaction.ProcessModel<TModel>() {
            @Override
            public void processModel(TModel model, DatabaseWrapper wrapper) {
                getModelAdapter().delete(model, wrapper);
            }
        }).add(model).build());
    return false;
}
 
Example #29
Source File: SqlUtils.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
public static long longForQuery(@NonNull DatabaseWrapper wrapper,
                                @NonNull String query) {
    DatabaseStatement statement = wrapper.compileStatement(query);
    try {
        return statement.simpleQueryForLong();
    } finally {
        statement.close();
    }
}
 
Example #30
Source File: ModelAdapter.java    From Meteorite with Apache License 2.0 4 votes vote down vote up
@Override
public void saveAll(@NonNull Collection<TModel> models, @NonNull DatabaseWrapper databaseWrapper) {
    getListModelSaver().saveAll(models, databaseWrapper);
}