aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/persistance/DatabaseBackend.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/siacs/conversations/persistance/DatabaseBackend.java')
-rw-r--r--src/eu/siacs/conversations/persistance/DatabaseBackend.java152
1 files changed, 48 insertions, 104 deletions
diff --git a/src/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/eu/siacs/conversations/persistance/DatabaseBackend.java
index 5a34dac6..63e050e3 100644
--- a/src/eu/siacs/conversations/persistance/DatabaseBackend.java
+++ b/src/eu/siacs/conversations/persistance/DatabaseBackend.java
@@ -10,6 +10,7 @@ import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.Presences;
+import eu.siacs.conversations.entities.Roster;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
@@ -23,7 +24,16 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static DatabaseBackend instance = null;
private static final String DATABASE_NAME = "history";
- private static final int DATABASE_VERSION = 3;
+ private static final int DATABASE_VERSION = 5;
+
+ private static String CREATE_CONTATCS_STATEMENT = "create table "
+ + Contact.TABLENAME + "(" + Contact.UUID + " TEXT PRIMARY KEY, "
+ + Contact.ACCOUNT + " TEXT, " + Contact.SERVERNAME + " TEXT, "
+ + Contact.SYSTEMNAME + " TEXT," + Contact.JID + " TEXT,"
+ + Contact.KEYS + " TEXT," + Contact.PHOTOURI + " TEXT,"
+ + Contact.OPTIONS + " NUMBER," + Contact.SYSTEMACCOUNT
+ + " NUMBER, " + "FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
+ + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE);";
public DatabaseBackend(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@@ -36,7 +46,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ " TEXT PRIMARY KEY," + Account.USERNAME + " TEXT,"
+ Account.SERVER + " TEXT," + Account.PASSWORD + " TEXT,"
+ Account.ROSTERVERSION + " TEXT," + Account.OPTIONS
- + " NUMBER, "+Account.KEYS+" TEXT)");
+ + " NUMBER, " + Account.KEYS + " TEXT)");
db.execSQL("create table " + Conversation.TABLENAME + " ("
+ Conversation.UUID + " TEXT PRIMARY KEY, " + Conversation.NAME
+ " TEXT, " + Conversation.CONTACT + " TEXT, "
@@ -50,31 +60,28 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, "
+ Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART
+ " TEXT, " + Message.BODY + " TEXT, " + Message.ENCRYPTION
- + " NUMBER, " + Message.STATUS + " NUMBER," +Message.TYPE +" NUMBER, FOREIGN KEY("
- + Message.CONVERSATION + ") REFERENCES "
- + Conversation.TABLENAME + "(" + Conversation.UUID
- + ") ON DELETE CASCADE);");
- db.execSQL("create table " + Contact.TABLENAME + "(" + Contact.UUID
- + " TEXT PRIMARY KEY, " + Contact.ACCOUNT + " TEXT, "
- + Contact.DISPLAYNAME + " TEXT," + Contact.JID + " TEXT,"
- + Contact.PRESENCES + " TEXT, " + Contact.KEYS
- + " TEXT," + Contact.PHOTOURI + " TEXT," + Contact.SUBSCRIPTION
- + " NUMBER," + Contact.SYSTEMACCOUNT + " NUMBER, "
- + "FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
- + Account.TABLENAME + "(" + Account.UUID
- + ") ON DELETE CASCADE);");
+ + " NUMBER, " + Message.STATUS + " NUMBER," + Message.TYPE
+ + " NUMBER, FOREIGN KEY(" + Message.CONVERSATION
+ + ") REFERENCES " + Conversation.TABLENAME + "("
+ + Conversation.UUID + ") ON DELETE CASCADE);");
+
+ db.execSQL(CREATE_CONTATCS_STATEMENT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2 && newVersion >= 2) {
- // enable compression by default.
- db.execSQL("update " + Account.TABLENAME
- + " set " + Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
+ db.execSQL("update " + Account.TABLENAME + " set "
+ + Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
}
if (oldVersion < 3 && newVersion >= 3) {
- //add field type to message
- db.execSQL("ALTER TABLE "+Message.TABLENAME+" ADD COLUMN "+Message.TYPE+" NUMBER");;
+ db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
+ + Message.TYPE + " NUMBER");
+ }
+ if (oldVersion < 5 && newVersion >= 5) {
+ db.execSQL("DROP TABLE "+Contact.TABLENAME);
+ db.execSQL(CREATE_CONTATCS_STATEMENT);
+ db.execSQL("UPDATE "+Account.TABLENAME+ " SET "+Account.ROSTERVERSION+" = NULL");
}
}
@@ -99,7 +106,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
SQLiteDatabase db = this.getWritableDatabase();
db.insert(Account.TABLENAME, null, account.getContentValues());
}
-
+
public void createContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.insert(Contact.TABLENAME, null, contact.getContentValues());
@@ -145,10 +152,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
public Conversation findConversation(Account account, String contactJid) {
SQLiteDatabase db = this.getReadableDatabase();
- String[] selectionArgs = { account.getUuid(), contactJid+"%" };
+ String[] selectionArgs = { account.getUuid(), contactJid + "%" };
Cursor cursor = db.query(Conversation.TABLENAME, null,
- Conversation.ACCOUNT + "=? AND " + Conversation.CONTACTJID + " like ?",
- selectionArgs, null, null, null);
+ Conversation.ACCOUNT + "=? AND " + Conversation.CONTACTJID
+ + " like ?", selectionArgs, null, null, null);
if (cursor.getCount() == 0)
return null;
cursor.moveToFirst();
@@ -200,87 +207,20 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.update(Message.TABLENAME, message.getContentValues(), Message.UUID
+ "=?", args);
}
-
- public void updateContact(Contact contact, boolean updatePresences) {
- SQLiteDatabase db = this.getWritableDatabase();
- String[] args = { contact.getUuid() };
- ContentValues values = contact.getContentValues();
- if (!updatePresences) {
- values.remove(Contact.PRESENCES);
- } else {
- values.remove(Contact.DISPLAYNAME);
- values.remove(Contact.PHOTOURI);
- values.remove(Contact.SYSTEMACCOUNT);
- }
- db.update(Contact.TABLENAME, contact.getContentValues(), Contact.UUID
- + "=?", args);
- }
-
- public void clearPresences(Account account) {
- SQLiteDatabase db = this.getWritableDatabase();
- String[] args = { account.getUuid() };
- ContentValues values = new ContentValues();
- values.put(Contact.PRESENCES,"[]");
- db.update(Contact.TABLENAME, values, Contact.ACCOUNT
- + "=?", args);
- }
-
- public void mergeContacts(List<Contact> contacts) {
- SQLiteDatabase db = this.getWritableDatabase();
- for (int i = 0; i < contacts.size(); i++) {
- Contact contact = contacts.get(i);
- String[] columns = {Contact.UUID, Contact.PRESENCES};
- String[] args = {contact.getAccount().getUuid(), contact.getJid()};
- Cursor cursor = db.query(Contact.TABLENAME, columns,Contact.ACCOUNT+"=? AND "+Contact.JID+"=?", args, null, null, null);
- if (cursor.getCount()>=1) {
- cursor.moveToFirst();
- contact.setUuid(cursor.getString(0));
- updateContact(contact,false);
- } else {
- contact.setUuid(UUID.randomUUID().toString());
- createContact(contact);
- }
- }
- }
- public List<Contact> getContactsByAccount(Account account) {
- List<Contact> list = new ArrayList<Contact>();
+ public void readRoster(Roster roster) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
- if (account==null) {
- cursor = db.query(Contact.TABLENAME, null, null, null, null,
- null, null);
- } else {
- String args[] = {account.getUuid()};
- cursor = db.query(Contact.TABLENAME, null, Contact.ACCOUNT+"=?", args, null,
- null, null);
- }
+ String args[] = { roster.getAccount().getUuid() };
+ cursor = db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?",
+ args, null, null, null);
while (cursor.moveToNext()) {
- list.add(Contact.fromCursor(cursor));
+ roster.initContact(Contact.fromCursor(cursor));
}
- return list;
}
- public List<Contact> getContacts(String where) {
- List<Contact> list = new ArrayList<Contact>();
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.query(Contact.TABLENAME, null, where, null, null, null, null);
- while (cursor.moveToNext()) {
- list.add(Contact.fromCursor(cursor));
- }
- return list;
- }
-
- public Contact findContact(Account account, String jid) {
- SQLiteDatabase db = this.getReadableDatabase();
- String[] selectionArgs = { account.getUuid(), jid };
- Cursor cursor = db.query(Contact.TABLENAME, null,
- Contact.ACCOUNT + "=? AND " + Contact.JID + "=?",
- selectionArgs, null, null, null);
- if (cursor.getCount() == 0)
- return null;
- cursor.moveToFirst();
- return Contact.fromCursor(cursor);
+ public void writeRoster(Roster roster) {
+
}
public void deleteMessage(Message message) {
@@ -288,7 +228,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
String[] args = { message.getUuid() };
db.delete(Message.TABLENAME, Message.UUID + "=?", args);
}
-
+
public void deleteMessagesInConversation(Conversation conversation) {
SQLiteDatabase db = this.getWritableDatabase();
String[] args = { conversation.getUuid() };
@@ -304,7 +244,8 @@ public class DatabaseBackend extends SQLiteOpenHelper {
public Contact getContact(String uuid) {
SQLiteDatabase db = this.getWritableDatabase();
String[] args = { uuid };
- Cursor cursor = db.query(Contact.TABLENAME, null, Contact.UUID + "=?", args, null, null, null);
+ Cursor cursor = db.query(Contact.TABLENAME, null, Contact.UUID + "=?",
+ args, null, null, null);
if (cursor.getCount() == 0) {
return null;
}
@@ -315,7 +256,8 @@ public class DatabaseBackend extends SQLiteOpenHelper {
public Conversation findConversationByUuid(String conversationUuid) {
SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { conversationUuid };
- Cursor cursor = db.query(Conversation.TABLENAME, null, Conversation.UUID + "=?", selectionArgs, null, null, null);
+ Cursor cursor = db.query(Conversation.TABLENAME, null,
+ Conversation.UUID + "=?", selectionArgs, null, null, null);
if (cursor.getCount() == 0) {
return null;
}
@@ -326,18 +268,20 @@ public class DatabaseBackend extends SQLiteOpenHelper {
public Message findMessageByUuid(String messageUuid) {
SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { messageUuid };
- Cursor cursor = db.query(Message.TABLENAME, null, Message.UUID + "=?", selectionArgs, null, null, null);
+ Cursor cursor = db.query(Message.TABLENAME, null, Message.UUID + "=?",
+ selectionArgs, null, null, null);
if (cursor.getCount() == 0) {
return null;
}
cursor.moveToFirst();
return Message.fromCursor(cursor);
}
-
+
public Account findAccountByUuid(String accountUuid) {
SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = { accountUuid };
- Cursor cursor = db.query(Account.TABLENAME, null, Account.UUID + "=?", selectionArgs, null, null, null);
+ Cursor cursor = db.query(Account.TABLENAME, null, Account.UUID + "=?",
+ selectionArgs, null, null, null);
if (cursor.getCount() == 0) {
return null;
}