use of android.database.sqlite.SQLiteConstraintException in project Remindy by abicelis.
the class RemindyDAO method getPlace.
/**
* Returns a Place given a placeId.
* @param placeId The id of the place
*/
public Place getPlace(int placeId) throws PlaceNotFoundException, SQLiteConstraintException {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query(RemindyContract.PlaceTable.TABLE_NAME, null, RemindyContract.PlaceTable._ID + "=?", new String[] { String.valueOf(placeId) }, null, null, null);
if (cursor.getCount() == 0)
throw new PlaceNotFoundException("Specified Place not found in the database. Passed id=" + placeId);
if (cursor.getCount() > 1)
throw new SQLiteConstraintException("Database UNIQUE constraint failure, more than one record found. Passed value=" + placeId);
cursor.moveToNext();
return getPlaceFromCursor(cursor);
}
use of android.database.sqlite.SQLiteConstraintException in project Remindy by abicelis.
the class RemindyDAO method getNextTaskToTrigger.
/**
* Returns the next PROGRAMMED task(With ONE-TIME or REPEATING reminder) to occur
* @param alreadyTriggeredTaskList an optional task list to not include in the search
* @return A single TaskTriggerViewModel or null of there are no tasks
*/
public TaskTriggerViewModel getNextTaskToTrigger(@NonNull List<Integer> alreadyTriggeredTaskList) throws CouldNotGetDataException {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
Task nextTaskToTrigger = null;
Calendar triggerDate = null;
Time triggerTime = null;
Cursor cursor = db.query(RemindyContract.TaskTable.TABLE_NAME, null, RemindyContract.TaskTable.COLUMN_NAME_STATUS.getName() + "=?", new String[] { TaskStatus.PROGRAMMED.name() }, null, null, null);
try {
while (cursor.moveToNext()) {
Task current = getTaskFromCursor(cursor);
if (//Skip
alreadyTriggeredTaskList.contains(current.getId()))
continue;
try {
current.setReminder(getReminderOfTask(current.getId(), current.getReminderType()));
} catch (CouldNotGetDataException | SQLiteConstraintException e) {
throw new CouldNotGetDataException("Error fetching reminder for task ID" + current.getId(), e);
}
//TODO: this filter could be made on db query.
if (current.getReminderType().equals(ReminderType.ONE_TIME) || current.getReminderType().equals(ReminderType.REPEATING)) {
if (//Skip overdue reminders
TaskUtil.checkIfOverdue(current.getReminder()))
continue;
if (nextTaskToTrigger == null) {
nextTaskToTrigger = current;
triggerDate = (current.getReminderType().equals(ReminderType.ONE_TIME) ? ((OneTimeReminder) current.getReminder()).getDate() : TaskUtil.getRepeatingReminderNextCalendar((RepeatingReminder) current.getReminder()));
triggerTime = (current.getReminderType().equals(ReminderType.ONE_TIME) ? ((OneTimeReminder) current.getReminder()).getTime() : ((RepeatingReminder) current.getReminder()).getTime());
continue;
}
if (current.getReminderType().equals(ReminderType.ONE_TIME)) {
OneTimeReminder otr = (OneTimeReminder) current.getReminder();
Calendar currentDate = CalendarUtil.getCalendarFromDateAndTime(otr.getDate(), otr.getTime());
if (currentDate.compareTo(triggerDate) < 0) {
nextTaskToTrigger = current;
triggerDate = currentDate;
triggerTime = otr.getTime();
continue;
}
}
if (current.getReminderType().equals(ReminderType.REPEATING)) {
RepeatingReminder rr = (RepeatingReminder) current.getReminder();
Calendar currentDate = TaskUtil.getRepeatingReminderNextCalendar(rr);
//Overdue
if (currentDate == null)
continue;
if (currentDate.compareTo(triggerDate) < 0) {
nextTaskToTrigger = current;
triggerDate = currentDate;
triggerTime = rr.getTime();
continue;
}
}
}
}
} finally {
cursor.close();
}
if (nextTaskToTrigger == null)
return null;
return new TaskTriggerViewModel(nextTaskToTrigger, triggerDate, triggerTime);
}
use of android.database.sqlite.SQLiteConstraintException in project Remindy by abicelis.
the class RemindyDAO method getReminderOfTask.
/**
* Returns a Reminder given its taskId and reminderType
* @param taskId The ID of the task
* @param reminderType The Type of reminder
*/
public Reminder getReminderOfTask(int taskId, @NonNull ReminderType reminderType) throws CouldNotGetDataException, SQLiteConstraintException {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
Reminder reminder;
String tableName, whereClause;
switch(reminderType) {
case ONE_TIME:
whereClause = RemindyContract.OneTimeReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
tableName = RemindyContract.OneTimeReminderTable.TABLE_NAME;
break;
case REPEATING:
whereClause = RemindyContract.RepeatingReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
tableName = RemindyContract.RepeatingReminderTable.TABLE_NAME;
break;
case LOCATION_BASED:
whereClause = RemindyContract.LocationBasedReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
tableName = RemindyContract.LocationBasedReminderTable.TABLE_NAME;
break;
default:
throw new CouldNotGetDataException("ReminderType is invalid. Type=" + reminderType);
}
Cursor cursor = db.query(tableName, null, whereClause, new String[] { String.valueOf(taskId) }, null, null, null);
try {
if (cursor.getCount() == 0)
throw new CouldNotGetDataException("Specified Reminder not found in the database. Passed id=" + taskId);
if (cursor.getCount() > 1)
throw new SQLiteConstraintException("Database UNIQUE constraint failure, more than one Reminder found. Passed id=" + taskId);
cursor.moveToNext();
switch(reminderType) {
case ONE_TIME:
reminder = getOneTimeReminderFromCursor(cursor);
break;
case REPEATING:
reminder = getRepeatingReminderFromCursor(cursor);
break;
case LOCATION_BASED:
reminder = getLocationBasedReminderFromCursor(cursor);
int placeId = ((LocationBasedReminder) reminder).getPlaceId();
try {
((LocationBasedReminder) reminder).setPlace(getPlace(placeId));
} catch (PlaceNotFoundException | SQLiteConstraintException e) {
throw new CouldNotGetDataException("Error trying to get Place for Location-based Reminder", e);
}
break;
default:
throw new CouldNotGetDataException("ReminderType is invalid. Type=" + reminderType);
}
} finally {
cursor.close();
}
return reminder;
}
use of android.database.sqlite.SQLiteConstraintException in project android_frameworks_base by crdroidandroid.
the class DatabaseStatementTest method testStatementConstraint.
@MediumTest
public void testStatementConstraint() throws Exception {
mDatabase.execSQL("CREATE TABLE test (num INTEGER NOT NULL);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
// Try to insert NULL, which violates the constraint
try {
statement.clearBindings();
statement.execute();
fail("expected exception not thrown");
} catch (SQLiteConstraintException e) {
// expected
}
// Make sure the statement can still be used
statement.bindLong(1, 1);
statement.execute();
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
long num = c.getLong(numCol);
assertEquals(1, num);
c.close();
}
use of android.database.sqlite.SQLiteConstraintException in project 360-Engine-for-Android by 360.
the class ContactsTable method syncSetServerIds.
/**
* Updates the server and user IDs for a list of contacts. Also prepares a
* list of duplicates which will be filled with the Ids for contacts already
* present in the table (i.e. server ID has already been used). In the case
* that a duplicate is found, the ContactIdInfo object will also include the
* local ID of the original contact (see {@link ContactIdInfo#mergedLocalId}
* ).
*
* @param serverIdList A list of ServerIdInfo objects. For each object, the
* local ID must match a local contact ID in the table. The
* Server ID and User ID will be used for the update.
* @param dupList On return this will be populated with a list of contacts
* which have server IDs already present in the table.
* @param writeableDb Writeable SQLite database
* @return SUCCESS or a suitable error code
*/
public static ServiceStatus syncSetServerIds(List<ServerIdInfo> serverIdList, List<ContactIdInfo> dupList, SQLiteDatabase writableDb) {
DatabaseHelper.trace(true, "ContactsTable.syncSetServerIds()");
if (serverIdList.size() == 0) {
return ServiceStatus.SUCCESS;
}
SQLiteStatement statement1 = null;
SQLiteStatement statement2 = null;
try {
writableDb.beginTransaction();
for (int i = 0; i < serverIdList.size(); i++) {
final ServerIdInfo info = serverIdList.get(i);
try {
if (info.serverId != null) {
if (info.userId == null) {
if (statement2 == null) {
statement2 = writableDb.compileStatement("UPDATE " + TABLE_NAME + " SET " + Field.SERVERID + "=? WHERE " + Field.LOCALID + "=?");
}
statement2.bindLong(1, info.serverId);
statement2.bindLong(2, info.localId);
statement2.execute();
} else {
if (statement1 == null) {
statement1 = writableDb.compileStatement("UPDATE " + TABLE_NAME + " SET " + Field.SERVERID + "=?," + Field.USERID + "=? WHERE " + Field.LOCALID + "=?");
}
statement1.bindLong(1, info.serverId);
statement1.bindLong(2, info.userId);
statement1.bindLong(3, info.localId);
statement1.execute();
}
}
} catch (SQLiteConstraintException e) {
// server ID is not unique
ContactIdInfo contactInfo = new ContactIdInfo();
contactInfo.localId = info.localId;
contactInfo.serverId = info.serverId;
if (!fetchLocalIDFromServerID(writableDb, contactInfo)) {
writableDb.endTransaction();
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
dupList.add(contactInfo);
} catch (SQLException e) {
LogUtils.logE("ContactsTable.syncSetServerIds() SQLException - " + "Unable to update contact server Ids", e);
return ServiceStatus.ERROR_DATABASE_CORRUPT;
}
}
writableDb.setTransactionSuccessful();
} finally {
writableDb.endTransaction();
if (statement1 != null) {
statement1.close();
statement1 = null;
}
if (statement2 != null) {
statement2.close();
statement2 = null;
}
}
return ServiceStatus.SUCCESS;
}
Aggregations