44 lines
1.2 KiB
Java
44 lines
1.2 KiB
Java
package com.benchmark.fixtures;
|
|
|
|
import com.benchmark.model.User;
|
|
import com.j256.ormlite.dao.Dao;
|
|
import com.j256.ormlite.dao.DaoManager;
|
|
import com.j256.ormlite.jdbc.JdbcConnectionSource;
|
|
import com.j256.ormlite.table.TableUtils;
|
|
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
public class SqliteInMemoryOrmLiteFixture implements DataFixtures {
|
|
|
|
private JdbcConnectionSource connectionSource;
|
|
private Dao<User, Long> dao;
|
|
|
|
@Override
|
|
public void setup(List<User> users) throws Exception {
|
|
String dbUrl = "jdbc:sqlite:file:benchmark-ormlite-memory-" + UUID.randomUUID() + "?mode=memory&cache=shared";
|
|
connectionSource = new JdbcConnectionSource(dbUrl);
|
|
TableUtils.createTableIfNotExists(connectionSource, User.class);
|
|
dao = DaoManager.createDao(connectionSource, User.class);
|
|
|
|
dao.callBatchTasks(() -> {
|
|
for (User user : users) {
|
|
dao.create(user);
|
|
}
|
|
return null;
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public User findById(long id) throws Exception {
|
|
return dao.queryForId(id);
|
|
}
|
|
|
|
@Override
|
|
public void teardown() throws Exception {
|
|
if (connectionSource != null) {
|
|
connectionSource.close();
|
|
}
|
|
}
|
|
}
|