Skip to content

Commit

Permalink
database fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Oct 26, 2023
1 parent 34adfca commit 5f5445e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ protected Result doInBackground(@NonNull Param param) {
case Param.MODE_REQUEST:
if (settings.isLoggedIn()) {
Account login = settings.getLogin();
if (!database.containsLogin(login.getId())) {
database.saveLogin(login);
}
database.saveLogin(login);
}
ConnectionResult result = connection.getAuthorisationLink(param.connection);
return new Result(Result.MODE_REQUEST, null, result, null);
Expand Down
27 changes: 7 additions & 20 deletions app/src/main/java/org/nuclearfog/twidda/database/AppDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public class AppDatabase {
/**
* selection for account entry
*/
private static final String ACCOUNT_SELECTION = AccountTable.ID + "=?";
private static final String ACCOUNT_SELECTION = AccountTable.ID + "=? AND " + AccountTable.HOSTNAME + "=?";

/**
* selection for poll entry
Expand Down Expand Up @@ -512,6 +512,11 @@ public void saveInstance(Instance instance) {
*/
public void saveLogin(Account account) {
synchronized (LOCK) {
SQLiteDatabase db = adapter.getDbWrite();
// delete login entry if exists
String[] accountArgs = {Long.toString(account.getId()), account.getHostname()};
db.delete(AccountTable.NAME, ACCOUNT_SELECTION, accountArgs);
// insert/update login
ContentValues column = new ContentValues(9);
column.put(AccountTable.ID, account.getId());
column.put(AccountTable.DATE, account.getTimestamp());
Expand All @@ -522,7 +527,6 @@ public void saveLogin(Account account) {
column.put(AccountTable.ACCESS_TOKEN, account.getOauthToken());
column.put(AccountTable.TOKEN_SECRET, account.getOauthSecret());
column.put(AccountTable.BEARER, account.getBearerToken());
SQLiteDatabase db = adapter.getDbWrite();
db.insertWithOnConflict(AccountTable.NAME, "", column, SQLiteDatabase.CONFLICT_REPLACE);
if (account.getUser() != null) {
saveUser(account.getUser(), db, SQLiteDatabase.CONFLICT_IGNORE);
Expand Down Expand Up @@ -976,7 +980,7 @@ public void removeFromBookmarks(Status status) {
*/
public void removeLogin(Account account) {
synchronized (LOCK) {
String[] accountArgs = {Long.toString(account.getId())};
String[] accountArgs = {Long.toString(account.getId()), account.getHostname()};
String[] pushArgs = {account.getId() + '@' + account.getHostname()};

SQLiteDatabase db = adapter.getDbWrite();
Expand All @@ -1003,23 +1007,6 @@ public boolean containsStatus(long id) {
}
}

/**
* check if status exists in database
*
* @param id status ID
* @return true if found
*/
public boolean containsLogin(long id) {
synchronized (LOCK) {
String[] args = {Long.toString(id)};
SQLiteDatabase db = adapter.getDbRead();
Cursor c = db.query(AccountTable.NAME, null, ACCOUNT_SELECTION, args, null, null, SINGLE_ITEM);
boolean result = c.moveToFirst();
c.close();
return result;
}
}

/**
* remove user from notification results
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public class DatabaseAdapter {
*/
private static final String TABLE_ACCOUNTS = "CREATE TABLE IF NOT EXISTS "
+ AccountTable.NAME + "("
+ AccountTable.ID + " INTEGER PRIMARY KEY,"
+ AccountTable.ID + " INTEGER,"
+ AccountTable.DATE + " INTEGER,"
+ AccountTable.ACCESS_TOKEN + " TEXT,"
+ AccountTable.TOKEN_SECRET + " TEXT,"
Expand Down

0 comments on commit 5f5445e

Please sign in to comment.