AndroidAPS/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java

204 lines
8.3 KiB
Java
Raw Normal View History

2016-06-05 01:40:35 +02:00
package info.nightscout.androidaps.db;
import android.content.Context;
import android.database.DatabaseUtils;
2016-06-05 01:40:35 +02:00
import android.database.sqlite.SQLiteDatabase;
2016-06-12 14:18:21 +02:00
import android.support.annotation.Nullable;
2016-06-05 01:40:35 +02:00
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
2016-06-07 21:48:17 +02:00
import com.j256.ormlite.stmt.PreparedQuery;
2016-06-05 01:40:35 +02:00
import com.j256.ormlite.stmt.QueryBuilder;
2016-06-09 00:01:28 +02:00
import com.j256.ormlite.stmt.Where;
2016-06-05 01:40:35 +02:00
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
2016-06-07 21:48:17 +02:00
2016-06-05 01:40:35 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2016-07-11 18:07:54 +02:00
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import info.nightscout.androidaps.Constants;
2016-06-07 21:48:17 +02:00
import info.nightscout.androidaps.MainApp;
2017-01-04 22:33:17 +01:00
import info.nightscout.androidaps.data.GlucoseStatus;
2016-06-07 21:48:17 +02:00
2016-06-05 01:40:35 +02:00
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
2016-06-07 21:48:17 +02:00
private static Logger log = LoggerFactory.getLogger(DatabaseHelper.class);
2016-06-05 01:40:35 +02:00
2016-06-07 21:48:17 +02:00
public static final String DATABASE_NAME = "AndroidAPSDb";
2016-07-21 15:10:42 +02:00
public static final String DATABASE_BGREADINGS = "BgReadings";
public static final String DATABASE_TEMPBASALS = "TempBasals";
public static final String DATABASE_TREATMENTS = "Treatments";
public static final String DATABASE_DANARHISTORY = "DanaRHistory";
2016-06-05 01:40:35 +02:00
2016-12-27 20:41:28 +01:00
private static final int DATABASE_VERSION = 5;
2016-06-05 01:40:35 +02:00
2016-06-07 21:48:17 +02:00
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
2016-06-05 01:40:35 +02:00
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
2016-06-07 21:48:17 +02:00
try {
log.info("onCreate");
TableUtils.createTableIfNotExists(connectionSource, TempBasal.class);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
2016-07-21 15:10:42 +02:00
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
2016-06-07 21:48:17 +02:00
} catch (SQLException e) {
log.error(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
2016-06-05 01:40:35 +02:00
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
2016-06-07 21:48:17 +02:00
try {
log.info(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, TempBasal.class, true);
TableUtils.dropTable(connectionSource, Treatment.class, true);
TableUtils.dropTable(connectionSource, BgReading.class, true);
2016-07-21 15:10:42 +02:00
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
2016-06-07 21:48:17 +02:00
onCreate(database, connectionSource);
} catch (SQLException e) {
log.error(DatabaseHelper.class.getName(), "Can't drop databases", e);
throw new RuntimeException(e);
}
2016-06-05 01:40:35 +02:00
}
/**
2016-06-07 21:48:17 +02:00
* Close the database connections and clear any cached DAOs.
*/
@Override
public void close() {
super.close();
}
public void cleanUpDatabases() {
// TODO: call it somewhere
2016-07-21 15:10:42 +02:00
log.debug("Before BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_BGREADINGS));
2016-06-24 17:30:25 +02:00
getWritableDatabase().delete("BgReadings", "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
2016-07-21 15:10:42 +02:00
log.debug("After BgReadings size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_BGREADINGS));
2016-07-21 15:10:42 +02:00
log.debug("Before TempBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPBASALS));
2016-06-24 17:30:25 +02:00
getWritableDatabase().delete("TempBasals", "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
2016-07-21 15:10:42 +02:00
log.debug("After TempBasals size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TEMPBASALS));
2016-07-21 15:10:42 +02:00
log.debug("Before Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TREATMENTS));
2016-06-24 17:30:25 +02:00
getWritableDatabase().delete("Treatments", "timeIndex" + " < '" + (new Date().getTime() - Constants.hoursToKeepInDatabase * 60 * 60 * 1000L) + "'", null);
2016-07-21 15:10:42 +02:00
log.debug("After Treatments size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), DATABASE_TREATMENTS));
2016-07-08 00:17:02 +02:00
2016-07-27 17:49:56 +02:00
log.debug("Before History size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "DanaRHistory"));
2016-07-21 15:10:42 +02:00
getWritableDatabase().delete("History", "recordDate" + " < '" + (new Date().getTime() - Constants.daysToKeepHistoryInDatabase * 24 * 60 * 60 * 1000L) + "'", null);
2016-07-27 17:49:56 +02:00
log.debug("After History size: " + DatabaseUtils.queryNumEntries(getReadableDatabase(), "DanaRHistory"));
}
2016-06-07 21:48:17 +02:00
public void resetDatabases() {
try {
TableUtils.dropTable(connectionSource, TempBasal.class, true);
TableUtils.dropTable(connectionSource, Treatment.class, true);
TableUtils.dropTable(connectionSource, BgReading.class, true);
2016-07-21 15:10:42 +02:00
TableUtils.dropTable(connectionSource, DanaRHistoryRecord.class, true);
2016-06-07 21:48:17 +02:00
TableUtils.createTableIfNotExists(connectionSource, TempBasal.class);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
TableUtils.createTableIfNotExists(connectionSource, BgReading.class);
2016-07-21 15:10:42 +02:00
TableUtils.createTableIfNotExists(connectionSource, DanaRHistoryRecord.class);
2016-07-27 17:49:56 +02:00
// MainApp.bus().post(new EventNewBG());
// MainApp.bus().post(new EventTreatmentChange());
// MainApp.bus().post(new EventTempBasalChange());
2016-06-07 21:48:17 +02:00
} catch (SQLException e) {
e.printStackTrace();
}
}
public void resetTreatments() {
try {
TableUtils.dropTable(connectionSource, Treatment.class, true);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
} catch (SQLException e) {
e.printStackTrace();
}
}
2016-06-05 01:40:35 +02:00
public Dao<TempBasal, Long> getDaoTempBasals() throws SQLException {
2016-06-07 21:48:17 +02:00
return getDao(TempBasal.class);
2016-06-05 01:40:35 +02:00
}
2016-06-07 21:48:17 +02:00
public Dao<Treatment, Long> getDaoTreatments() throws SQLException {
return getDao(Treatment.class);
}
public Dao<BgReading, Long> getDaoBgReadings() throws SQLException {
return getDao(BgReading.class);
}
2016-07-27 17:49:56 +02:00
public Dao<DanaRHistoryRecord, String> getDaoDanaRHistory() throws SQLException {
2016-07-21 15:10:42 +02:00
return getDao(DanaRHistoryRecord.class);
2016-07-08 00:17:02 +02:00
}
2016-06-07 21:48:17 +02:00
/*
* Return last BgReading from database or null if db is empty
*/
2016-06-12 14:18:21 +02:00
@Nullable
2016-06-07 21:48:17 +02:00
public BgReading lastBg() {
List<BgReading> bgList = null;
try {
Dao<BgReading, Long> daoBgReadings = MainApp.getDbHelper().getDaoBgReadings();
QueryBuilder<BgReading, Long> queryBuilder = daoBgReadings.queryBuilder();
queryBuilder.orderBy("timeIndex", false);
2016-06-24 17:30:25 +02:00
queryBuilder.limit(1L);
2016-12-05 17:47:03 +01:00
queryBuilder.where().gt("value", 38);
2016-06-07 21:48:17 +02:00
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
bgList = daoBgReadings.query(preparedQuery);
} catch (SQLException e) {
log.debug(e.getMessage(), e);
}
2016-06-24 17:30:25 +02:00
if (bgList != null && bgList.size() > 0)
2016-06-07 21:48:17 +02:00
return bgList.get(0);
else
return null;
}
2016-06-05 01:40:35 +02:00
2016-06-07 21:48:17 +02:00
/*
* Return bg reading if not old ( <9 min )
* or null if older
*/
2016-06-12 14:18:21 +02:00
@Nullable
2016-06-07 21:48:17 +02:00
public BgReading actualBg() {
BgReading lastBg = lastBg();
if (lastBg == null)
return null;
2016-06-24 17:30:25 +02:00
if (lastBg.timeIndex > new Date().getTime() - 9 * 60 * 1000)
2016-06-07 21:48:17 +02:00
return lastBg;
return null;
}
2016-06-09 00:01:28 +02:00
2017-01-06 22:42:37 +01:00
public List<BgReading> getDataFromTime(long mills, boolean ascending) {
2016-06-09 00:01:28 +02:00
try {
Dao<BgReading, Long> daoBgreadings = getDaoBgReadings();
List<BgReading> bgReadings;
QueryBuilder<BgReading, Long> queryBuilder = daoBgreadings.queryBuilder();
2017-01-06 22:42:37 +01:00
queryBuilder.orderBy("timeIndex", ascending);
2016-06-09 00:01:28 +02:00
Where where = queryBuilder.where();
2016-12-07 01:22:47 +01:00
where.ge("timeIndex", mills).and().gt("value", 38);
2016-06-09 00:01:28 +02:00
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
bgReadings = daoBgreadings.query(preparedQuery);
return bgReadings;
} catch (SQLException e) {
e.printStackTrace();
}
return new ArrayList<BgReading>();
}
2016-06-05 01:40:35 +02:00
}