io.requery.Persistable Java Examples

The following examples show how to use io.requery.Persistable. 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: DatabaseConfiguration.java    From requery with Apache License 2.0 6 votes vote down vote up
@Bean
public EntityDataStore<Persistable> provideDataStore() {
    ConnectionProvider connectionProvider = new ConnectionProvider() {
        @Override
        public Connection getConnection() throws SQLException {
            return DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
        }
    };
    io.requery.sql.Configuration configuration = new ConfigurationBuilder(connectionProvider, Models.DEFAULT)
            .useDefaultLogging()
            .setEntityCache(new WeakEntityCache())
            .setWriteExecutor(Executors.newSingleThreadExecutor())
            .build();

    SchemaModifier tables = new SchemaModifier(configuration);
    tables.createTables(TableCreationMode.DROP_CREATE);
    return new EntityDataStore<>(configuration);
}
 
Example #2
Source File: ReactorTest.java    From requery with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws SQLException {
    Platform platform = new HSQL();
    CommonDataSource dataSource = DatabaseType.getDataSource(platform);
    EntityModel model = io.requery.test.model.Models.DEFAULT;

    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();
    Configuration configuration = new ConfigurationBuilder(dataSource, model)
        .useDefaultLogging()
        .setWriteExecutor(Executors.newSingleThreadExecutor())
        .setEntityCache(new EntityCacheBuilder(model)
            .useReferenceCache(true)
            .useSerializableCache(true)
            .useCacheManager(cacheManager)
            .build())
        .build();

    SchemaModifier tables = new SchemaModifier(configuration);
    tables.createTables(TableCreationMode.DROP_CREATE);
    data = new ReactorEntityStore<>(new EntityDataStore<Persistable>(configuration));
}
 
Example #3
Source File: ReactiveTest.java    From requery with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws SQLException {
    Platform platform = new HSQL();
    CommonDataSource dataSource = DatabaseType.getDataSource(platform);
    EntityModel model = io.requery.test.model.Models.DEFAULT;

    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();
    Configuration configuration = new ConfigurationBuilder(dataSource, model)
        .useDefaultLogging()
        .setWriteExecutor(Executors.newSingleThreadExecutor())
        .setEntityCache(new EntityCacheBuilder(model)
            .useReferenceCache(true)
            .useSerializableCache(true)
            .useCacheManager(cacheManager)
            .build())
        .build();

    SchemaModifier tables = new SchemaModifier(configuration);
    tables.createTables(TableCreationMode.DROP_CREATE);
    data = ReactiveSupport.toReactiveStore(new EntityDataStore<Persistable>(configuration));
}
 
Example #4
Source File: ReactiveTest.java    From requery with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunInTransactionFromBlocking() {
    final BlockingEntityStore<Persistable> blocking = data.toBlocking();
    Completable.fromCallable(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            blocking.runInTransaction(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    final Person person = randomPerson();
                    blocking.insert(person);
                    blocking.update(person);
                    return null;
                }
            });
            return null;
        }
    }).subscribe();
    assertEquals(1, data.count(Person.class).get().value().intValue());
}
 
Example #5
Source File: ModuleDatabase.java    From AndroidStarterAlt with Apache License 2.0 5 votes vote down vote up
@Provides
    @Singleton
    public SingleEntityStore<Persistable> provideDataStore(@NonNull final Context poContext) {
//        final DatabaseProvider<SQLiteDatabase> loSource = new DatabaseSource(poContext, Models.DEFAULT, "android_starter_alt.sqlite", 1);
        final DatabaseProvider<SQLiteDatabase> loSource = new SqlCipherDatabaseSource(poContext, Models.DEFAULT, "android_start_alt_requery_sqlcipher.sqlite", "android_starter_alt", 1);
        final Configuration loConfiguration = loSource.getConfiguration();
        final SingleEntityStore<Persistable> loDataStore = RxSupport.toReactiveStore(new EntityDataStore<>(loConfiguration));
        return loDataStore;
    }
 
Example #6
Source File: MockModuleDatabase.java    From AndroidStarterAlt with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
@Override
public SingleEntityStore<Persistable> provideDataStore(@NonNull final Context poContext) {
    final DatabaseSource loSource = new DatabaseSource(poContext, Models.DEFAULT, "mock_android_starter_alt.sqlite", 1);
    final Configuration loConfiguration = loSource.getConfiguration();
    final SingleEntityStore<Persistable> loDataStore = RxSupport.toReactiveStore(new EntityDataStore<>(loConfiguration));
    return loDataStore;
}
 
Example #7
Source File: PretixDroid.java    From pretixdroid with GNU General Public License v3.0 5 votes vote down vote up
public BlockingEntityStore<Persistable> getData() {
    if (dataStore == null) {
        // override onUpgrade to handle migrating to a new version
        DatabaseSource source = new DatabaseSource(this, Models.DEFAULT, 6);
        Configuration configuration = source.getConfiguration();
        dataStore = new EntityDataStore<Persistable>(configuration);
    }
    return dataStore;
}
 
Example #8
Source File: PeopleApplication.java    From requery with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@link EntityDataStore} single instance for the application.
 * <p/>
 * Note if you're using Dagger you can make this part of your application level module returning
 * {@code @Provides @Singleton}.
 */
ReactiveEntityStore<Persistable> getData() {
    if (dataStore == null) {
        // override onUpgrade to handle migrating to a new version
        DatabaseSource source = new DatabaseSource(this, Models.DEFAULT, 1);
        if (BuildConfig.DEBUG) {
            // use this in development mode to drop and recreate the tables on every upgrade
            source.setTableCreationMode(TableCreationMode.DROP_CREATE);
        }
        Configuration configuration = source.getConfiguration();
        dataStore = ReactiveSupport.toReactiveStore(
            new EntityDataStore<Persistable>(configuration));
    }
    return dataStore;
}
 
Example #9
Source File: RequeryExecutor.java    From android-orm-benchmark-updated with Apache License 2.0 4 votes vote down vote up
@Override
public long writeWholeData() throws SQLException {
    final List<UserEntity> users = new LinkedList<UserEntity>();
    for (int i = 0; i < NUM_USER_INSERTS; i++) {
        UserEntity newUser = new UserEntity();
        newUser.lastName = (Util.getRandomString(10));
        newUser.firstName = (Util.getRandomString(10));

        users.add(newUser);
    }

    final List<MessageEntity> messages = new LinkedList<MessageEntity>();
    for (int i = 0; i < NUM_MESSAGE_INSERTS; i++) {
        MessageEntity newMessage = new MessageEntity();
        newMessage.commandId = (i);
        newMessage.sortedBy = (System.nanoTime());
        newMessage.content = (Util.getRandomString(100));
        newMessage.clientId = (System.currentTimeMillis());
        newMessage
                .senderId = (Math.round(Math.random() * NUM_USER_INSERTS));
        newMessage
                .channelId = (Math.round(Math.random() * NUM_USER_INSERTS));
        newMessage.createdAt = ((int) (System.currentTimeMillis() / 1000L));

        messages.add(newMessage);
    }

    final EntityDataStore<Persistable> userStore = new EntityDataStore<>(mHelper.getConfiguration());

    long start = System.nanoTime();

    userStore.runInTransaction(new Callable<Object>() {
        @Override
        public Object call() throws Exception {
            userStore.insert(users, User.class);

            Log.d(TAG, "Done, wrote " + NUM_USER_INSERTS + " users");

            userStore.insert(messages, Message.class);

            Log.d(TAG, "Done, wrote " + NUM_MESSAGE_INSERTS + " messages");

            return null;
        }
    });

    userStore.close();

    return System.nanoTime() - start;
}
 
Example #10
Source File: RequeryExecutor.java    From android-orm-benchmark-updated with Apache License 2.0 4 votes vote down vote up
@Override
public long readWholeData() throws SQLException {
    long start = System.nanoTime();

    final EntityDataStore<Persistable> userStore = new EntityDataStore<>(
            mHelper.getConfiguration());

    final List<UserEntity> userEntities = userStore.select(UserEntity.class).get().toList();

    String userLog = "Read " + userEntities.size() + " users in " + (System.nanoTime() - start);

    long messageStart = System.nanoTime();

    final List<MessageEntity> messageEntities = userStore.select(MessageEntity.class).get().toList();

    Log.d(TAG, userLog);
    Log.d(TAG, "Read " + messageEntities.size() + " messages in "
            + (System.nanoTime() - messageStart));

    userStore.close();

    return System.nanoTime() - start;
}
 
Example #11
Source File: EntityGenerator.java    From requery with Apache License 2.0 4 votes vote down vote up
@Override
public void generate() throws IOException {
    ClassName entityTypeName = entity.isEmbedded() ?
        nameResolver.embeddedTypeNameOf(entity, parent) : typeName;
    TypeSpec.Builder builder = TypeSpec.classBuilder(entityTypeName)
        .addModifiers(Modifier.PUBLIC)
        .addOriginatingElement(typeElement);
    boolean metadataOnly = entity.isImmutable() || entity.isUnimplementable();
    if (typeElement.getKind().isInterface()) {
        builder.addSuperinterface(ClassName.get(typeElement));
        builder.addSuperinterface(ClassName.get(Persistable.class));

    } else if (!metadataOnly) {
        builder.superclass(ClassName.get(typeElement));
        builder.addSuperinterface(ClassName.get(Persistable.class));
    }
    CodeGeneration.addGeneratedAnnotation(processingEnv, builder);
    if (!entity.isEmbedded()) {
        EntityMetaGenerator meta = new EntityMetaGenerator(processingEnv, graph, entity);
        meta.generate(builder);
    }
    if (!metadataOnly) {
        generateConstructors(builder);
        generateMembers(builder);
        generateProxyMethods(builder);
        if (!entity.isEmbedded()) {
            generateEquals(builder);
            generateHashCode(builder);
            generateToString(builder);
        }
        if (entity.isCopyable()) {
            generateCopy(builder);
        }
    } else {
        // private constructor
        builder.addMethod(MethodSpec.constructorBuilder()
               .addModifiers(Modifier.PRIVATE).build());
        generateMembers(builder); // members for builder if needed
        generateImmutableTypeBuildMethod(builder);
    }
    typeExtensions.forEach(extension -> extension.generate(entity, builder));
    CodeGeneration.writeType(processingEnv, typeName.packageName(), builder.build());
}
 
Example #12
Source File: CreatePeople.java    From requery with Apache License 2.0 4 votes vote down vote up
CreatePeople(ReactiveEntityStore<Persistable> data) {
    this.data = data;
}