com.activeandroid.query.Select Java Examples

The following examples show how to use com.activeandroid.query.Select. 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: UploaderQueue.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private static int getLegacyCount(Class which, Boolean rest, Boolean mongo, Boolean and) {
    try {
        String where = "";
        if (rest != null) where += " success = " + (rest ? "1 " : "0 ");
        if (and != null) where += (and ? " and " : " or ");
        if (mongo != null) where += " mongo_success = " + (mongo ? "1 " : "0 ");
        final String query = new Select("COUNT(*) as total").from(which).toSql();
        final Cursor resultCursor = Cache.openDatabase().rawQuery(query + ((where.length() > 0) ? " where " + where : ""), null);
        if (resultCursor.moveToNext()) {
            final int total = resultCursor.getInt(0);
            resultCursor.close();
            return total;
        } else {
            return 0;
        }

    } catch (Exception e) {
        Log.d(TAG, "Got exception getting count: " + e);
        return -1;
    }
}
 
Example #2
Source File: TransmitterData.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static TransmitterData getForTimestamp(double timestamp) {//KS
    try {
        Sensor sensor = Sensor.currentSensor();
        if (sensor != null) {
            TransmitterData bgReading = new Select()
                    .from(TransmitterData.class)
                    .where("timestamp <= ?", (timestamp + (60 * 1000))) // 1 minute padding (should never be that far off, but why not)
                    .orderBy("timestamp desc")
                    .executeSingle();
            if (bgReading != null && Math.abs(bgReading.timestamp - timestamp) < (3 * 60 * 1000)) { //cool, so was it actually within 4 minutes of that bg reading?
                Log.i(TAG, "getForTimestamp: Found a BG timestamp match");
                return bgReading;
            }
        }
    } catch (Exception e) {
        Log.e(TAG,"getForTimestamp() Got exception on Select : "+e.toString());
        return null;
    }
    Log.d(TAG, "getForTimestamp: No luck finding a BG timestamp match");
    return null;
}
 
Example #3
Source File: AlertType.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static boolean toSettings(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    List<AlertType> alerts  = new Select()
        .from(AlertType.class)
        .execute();

    Gson gson = new GsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .registerTypeAdapter(Date.class, new DateTypeAdapter())
            .serializeSpecialFloatingPointValues()
            .create();
    String output =  gson.toJson(alerts);
    Log.e(TAG, "Created the string " + output);
    prefs.edit().putString("saved_alerts", output).commit(); // always leave this as commit

    return true;

}
 
Example #4
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static List<Calibration> latestValid(int number, long until) {
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        return null;
    }
    // we don't filter invalid intercepts here as they will be filtered in the plugin itself
    return new Select()
            .from(Calibration.class)
            .where("Sensor = ? ", sensor.getId())
            .where("slope_confidence != 0")
            .where("sensor_confidence != 0")
            .where("slope != 0")
            .where("timestamp <= ?", until)
            .orderBy("timestamp desc")
            .limit(number)
            .execute();
}
 
Example #5
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static double max_recent() {
    Sensor sensor = Sensor.currentSensor();
    Calibration calibration = new Select()
            .from(Calibration.class)
            .where("Sensor = ? ", sensor.getId())
            .where("slope_confidence != 0")
            .where("sensor_confidence != 0")
            .where("timestamp > ?", (new Date().getTime() - (60000 * 60 * 24 * 4)))
            .orderBy("bg desc")
            .executeSingle();
    if (calibration != null) {
        return calibration.bg;
    } else {
        return 120;
    }
}
 
Example #6
Source File: Reminder.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static Reminder getNextActiveReminder() {
    fixUpTable(schema);
    final boolean onHomeWifi = !HomeWifi.isSet() || HomeWifi.isConnected();
    final long now = JoH.tsl();
    final Reminder reminder = new Select()
            .from(Reminder.class)
            .where("enabled = ?", true)
            .where("next_due < ?", now)
            .where("snoozed_till < ?", now)
            .where("last_fired < (? - (600000 * alerted_times))", now)
            // if on home wifi or not set then anything otherwise only home only = false
            .where(onHomeWifi ? "homeonly > -1 " : "homeonly = 0")
            .orderBy("enabled desc, priority desc, next_due asc")
            .executeSingle();
    return reminder;
}
 
Example #7
Source File: UserNotification.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static UserNotification GetNotificationByType(String type) {
    if (legacy_types.contains(type)) {
        type = type + " = ?";
        return new Select()
                .from(UserNotification.class)
                .where(type, true)
                .orderBy("_ID desc")
                .executeSingle();
    } else {
        final String timestamp = PersistentStore.getString("UserNotification:timestamp:" + type);
        if (timestamp.equals("")) return null;
        final String message = PersistentStore.getString("UserNotification:message:" + type);
        if (message.equals("")) return null;
        UserNotification userNotification = new UserNotification();
        userNotification.timestamp = JoH.tolerantParseDouble(timestamp, -1);
        if (userNotification.timestamp == -1) return null; // bad data
        userNotification.message = message;
        Log.d(TAG, "Workaround for: " + type + " " + userNotification.message + " timestamp: " + userNotification.timestamp);
        return userNotification;
    }
}
 
Example #8
Source File: AlertType.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static boolean toSettings(Context context) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    List<AlertType> alerts  = new Select()
        .from(AlertType.class)
        .execute();

    Gson gson = new GsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .registerTypeAdapter(Date.class, new DateTypeAdapter())
            .serializeSpecialFloatingPointValues()
            .create();
    String output =  gson.toJson(alerts);
    Log.e(TAG, "Created the string " + output);
    prefs.edit().putString("saved_alerts", output).commit();

    return true;

}
 
Example #9
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static BgReading getForPreciseTimestamp(long timestamp, long precision, boolean lock_to_sensor) {
    final Sensor sensor = Sensor.currentSensor();
    if ((sensor != null) || !lock_to_sensor) {
        final BgReading bgReading = new Select()
                .from(BgReading.class)
                .where(lock_to_sensor ? "Sensor = ?" : "timestamp > ?", (lock_to_sensor ? sensor.getId() : 0))
                .where("timestamp <= ?", (timestamp + precision))
                .where("timestamp >= ?", (timestamp - precision))
                .orderBy("abs(timestamp - " + timestamp + ") asc")
                .executeSingle();
        if (bgReading != null && Math.abs(bgReading.timestamp - timestamp) < precision) { //cool, so was it actually within precision of that bg reading?
            //Log.d(TAG, "getForPreciseTimestamp: Found a BG timestamp match");
            return bgReading;
        }
    }
    Log.d(TAG, "getForPreciseTimestamp: No luck finding a BG timestamp match: " + JoH.dateTimeText((long) timestamp) + " precision:" + precision + " Sensor: " + ((sensor == null) ? "null" : sensor.getId()));
    return null;
}
 
Example #10
Source File: LibreBlock.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static List<LibreBlock> getForTrend(long start_time, long end_time) {

        return new Select()
                .from(LibreBlock.class)
                .where("bytestart == 0")
                .where("byteend >= 344")
                .where("timestamp >= ?", start_time)
                .where("timestamp <= ?", end_time)
                .orderBy("timestamp asc")
                .execute();
    }
 
Example #11
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static List<BgReading> latestUnCalculated(int number) {
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) { return null; }
    return new Select()
            .from(BgReading.class)
            .where("Sensor = ? ", sensor.getId())
            .where("raw_data != 0")
            .orderBy("timestamp desc")
            .limit(number)
            .execute();
}
 
Example #12
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static Calibration lastValid() {
    Sensor sensor = Sensor.currentSensor();
    if (sensor == null) {
        return null;
    }
    return new Select()
            .from(Calibration.class)
            .where("Sensor = ? ", sensor.getId())
            .where("slope_confidence != 0")
            .where("sensor_confidence != 0")
            .where("slope != 0")
            .where("intercept <= ?", CalibrationAbstract.getHighestSaneIntercept())
            .orderBy("timestamp desc")
            .executeSingle();
}
 
Example #13
Source File: APStatus.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static List<APStatus> latestForGraph(int number, long startTime, long endTime) {
    try {
        final List<APStatus> results = new Select()
                .from(APStatus.class)
                .where("timestamp >= " + Math.max(startTime, 0))
                .where("timestamp <= " + endTime)
                .orderBy("timestamp asc") // warn asc!
                .limit(number)
                .execute();
        // extend line to now if we have current data but it is continuation of last record
        // so not generating a new efficient record.
        if (results != null && (results.size() > 0)) {
            final APStatus last = results.get(results.size() - 1);
            final long last_raw_record_timestamp = ExternalStatusService.getLastStatusLineTime();
            // check are not already using the latest.
            if (last_raw_record_timestamp > last.timestamp) {
                final Integer last_recorded_tbr = ExternalStatusService.getTBRInt();
                if (last_recorded_tbr != null) {
                    if ((last.basal_percent == last_recorded_tbr)
                            && (JoH.msSince(last.timestamp) < Constants.HOUR_IN_MS * 3)
                            && (JoH.msSince(ExternalStatusService.getLastStatusLineTime()) < Constants.MINUTE_IN_MS * 20)) {
                        results.add(new APStatus(JoH.tsl(), last_recorded_tbr));
                        UserError.Log.d(TAG, "Adding extension record");
                    }
                }
            }
        }
        return results;
    } catch (android.database.sqlite.SQLiteException e) {
        updateDB();
        return new ArrayList<>();
    }
}
 
Example #14
Source File: APStatus.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static APStatus last() {
    try {
        return new Select()
                .from(APStatus.class)
                .orderBy("timestamp desc")
                .executeSingle();
    } catch (android.database.sqlite.SQLiteException e) {
        updateDB();
        return null;
    }
}
 
Example #15
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static List<BgReading> latest_by_size(int number) {
    final Sensor sensor = Sensor.currentSensor();
    if (sensor == null) return null;
    return new Select()
            .from(BgReading.class)
            .where("Sensor = ? ", sensor.getId())
            .where("raw_data != 0")
            .orderBy("timestamp desc")
            .limit(number)
            .execute();
}
 
Example #16
Source File: UserNotification.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static UserNotification lastCalibrationAlert() {
    return new Select()
            .from(UserNotification.class)
            .where("calibration_alert = ?", true)
            .orderBy("_ID desc")
            .executeSingle();
}
 
Example #17
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
private static List<Calibration> calibrations_for_sensor(Sensor sensor) {
    return new Select()
            .from(Calibration.class)
            .where("Sensor = ?", sensor.getId())
            .where("slope_confidence != 0")
            .where("sensor_confidence != 0")
            .orderBy("timestamp desc")
            .execute();
}
 
Example #18
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static List<Calibration> getCalibrationsForSensor(Sensor sensor, int limit) {
    return new Select()
            .from(Calibration.class)
            .where("sensor_uuid = ? ", sensor.uuid)
             .orderBy("timestamp desc")
             .limit(limit)
            .execute();
}
 
Example #19
Source File: SensorSendQueue.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static SensorSendQueue nextSensorJob() {
    SensorSendQueue job = new Select()
            .from(SensorSendQueue.class)
            .where("success =", false)
            .orderBy("_ID desc")
            .limit(1)
            .executeSingle();
    return job;
}
 
Example #20
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static List<BgReading> latestForSensorAsc(int number, long startTime, long endTime, boolean follower) {
    if (follower) {
        return new Select()
                .from(BgReading.class)
                .where("timestamp >= ?", Math.max(startTime, 0))
                .where("timestamp <= ?", endTime)
                .where("calculated_value != 0")
                .where("raw_data != 0")
                .orderBy("timestamp asc")
                .limit(number)
                .execute();
    } else {
        final Sensor sensor = Sensor.currentSensor();
        if (sensor == null) {
            return null;
        }
        return new Select()
                .from(BgReading.class)
                .where("Sensor = ? ", sensor.getId())
                .where("timestamp >= ?", Math.max(startTime, 0))
                .where("timestamp <= ?", endTime)
                .where("calculated_value != 0")
                .where("raw_data != 0")
                .orderBy("timestamp asc")
                .limit(number)
                .execute();
    }
}
 
Example #21
Source File: UserNotification.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static UserNotification lastBgAlert() {
    return new Select()
            .from(UserNotification.class)
            .where("bg_alert = ?", true)
            .orderBy("_ID desc")
            .executeSingle();
}
 
Example #22
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static List<Calibration> latestForGraph(int number, long startTime, long endTime) {
    return new Select()
            .from(Calibration.class)
            .where("timestamp >= " + Math.max(startTime, 0))
            .where("timestamp <= " + endTime)
            .where("(slope != 0 or slope_confidence = ?)", note_only_marker)
            .orderBy("timestamp desc")
            .limit(number)
            .execute();
}
 
Example #23
Source File: Calibration.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static Calibration byuuid(String uuid) {
    if (uuid == null) return null;
    return new Select()
            .from(Calibration.class)
            .where("uuid = ?", uuid)
            .orderBy("_ID desc")
            .executeSingle();
}
 
Example #24
Source File: Treatments.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static List<Treatments> latestForGraph(int number, double startTime, double endTime) {
    fixUpTable();
    DecimalFormat df = new DecimalFormat("#");
    df.setMaximumFractionDigits(1); // are there decimal points in the database??
    return new Select()
            .from(Treatments.class)
            .where("timestamp >= ? and timestamp <= ?", df.format(startTime), df.format(endTime))
            .orderBy("timestamp asc")
            .limit(number)
            .execute();
}
 
Example #25
Source File: BloodTest.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static BloodTest getForPreciseTimestamp(long timestamp, long precision) {
    BloodTest bloodTest = new Select()
        .from(BloodTest.class)
        .where("timestamp <= ?", (timestamp + precision))
        .where("timestamp >= ?", (timestamp - precision))
        .orderBy("abs(timestamp - " + timestamp + ") asc")
        .executeSingle();
    if ((bloodTest != null) && (Math.abs(bloodTest.timestamp - timestamp) < precision)) {
        return bloodTest;
    }
    return null;
}
 
Example #26
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static BgReading byUUID(String uuid) {
    if (uuid == null) return null;
    return new Select()
            .from(BgReading.class)
            .where("uuid = ?", uuid)
            .executeSingle();
}
 
Example #27
Source File: Calibration.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static List<Calibration> getCalibrationsForSensor(Sensor sensor, int limit) {
    return new Select()
            .from(Calibration.class)
            .where("sensor_uuid = ? ", sensor.uuid)
             .orderBy("timestamp desc")
             .limit(limit)
            .execute();
}
 
Example #28
Source File: CalibrationRequest.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
static void clearAll(){
    List<CalibrationRequest> calibrationRequests =  new Select()
                                                        .from(CalibrationRequest.class)
                                                        .execute();
    if (calibrationRequests.size() >=1) {
        for (CalibrationRequest calibrationRequest : calibrationRequests) {
            calibrationRequest.delete();
        }
    }
}
 
Example #29
Source File: NotificationActivity.java    From MagicPrint-ECommerce-App-Android with MIT License 5 votes vote down vote up
private List<Notification> getAll() {
    //Getting all items stored in Inventory table
    return new Select()
            .from(Notification.class)
            .limit(10)
            .execute();
}
 
Example #30
Source File: BgReading.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static List<BgReading> futureReadings() {
    double timestamp = new Date().getTime();
    return new Select()
            .from(BgReading.class)
            .where("timestamp > " + timestamp)
            .orderBy("timestamp desc")
            .execute();
}