DbHandler.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* ownCloud Android client application
  2. * Copyright (C) 2011 Bartek Przybylski
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package eu.alefzero.owncloud.db;
  19. import java.util.Vector;
  20. import eu.alefzero.owncloud.OwnCloudSession;
  21. import android.content.ContentValues;
  22. import android.content.Context;
  23. import android.database.Cursor;
  24. import android.database.sqlite.SQLiteDatabase;
  25. import android.database.sqlite.SQLiteOpenHelper;
  26. /**
  27. * Custom database helper for ownCloud
  28. * @author Bartek Przybylski
  29. *
  30. */
  31. public class DbHandler {
  32. private SQLiteDatabase mDB;
  33. private OpenerHepler mHelper;
  34. private final String mDatabaseName = "ownCloud";
  35. private final String TABLE_SESSIONS = "sessions";
  36. private final int mDatabaseVersion = 1;
  37. public DbHandler(Context context) {
  38. mHelper = new OpenerHepler(context);
  39. mDB = mHelper.getWritableDatabase();
  40. }
  41. public Vector<OwnCloudSession> getSessionList() {
  42. Cursor c = mDB.query(TABLE_SESSIONS, null, null, null, null, null, null);
  43. Vector<OwnCloudSession> v = new Vector<OwnCloudSession>();
  44. if (!c.moveToFirst()) {
  45. return v;
  46. }
  47. while (!c.isAfterLast()) {
  48. v.add(new OwnCloudSession(c.getString(c.getColumnIndex("sessionName")),
  49. c.getString(c.getColumnIndex("sessionUrl")),
  50. c.getInt(c.getColumnIndex("_id"))));
  51. c.moveToNext();
  52. }
  53. c.close();
  54. return v;
  55. }
  56. public void addSession(String sessionName, String uri) {
  57. ContentValues cv = new ContentValues();
  58. cv.put("sessionName", sessionName);
  59. cv.put("sessionUrl", uri);
  60. mDB.insert(TABLE_SESSIONS, null, cv);
  61. }
  62. public void removeSessionWithId(int sessionId) {
  63. mDB.delete(TABLE_SESSIONS, "_id = ?", new String[] {String.valueOf(sessionId)});
  64. }
  65. public void changeSessionFields(int id, String hostname, String uri) {
  66. ContentValues cv = new ContentValues();
  67. cv.put("sessionName", hostname);
  68. cv.put("sessionUrl", uri);
  69. mDB.update(TABLE_SESSIONS, cv, "_id = ?", new String[] {String.valueOf(id)});
  70. }
  71. public void close() {
  72. mDB.close();
  73. }
  74. private class OpenerHepler extends SQLiteOpenHelper {
  75. public OpenerHepler(Context context) {
  76. super(context, mDatabaseName, null, mDatabaseVersion);
  77. }
  78. @Override
  79. public void onCreate(SQLiteDatabase db) {
  80. db.execSQL("CREATE TABLE " + TABLE_SESSIONS + " (" +
  81. " _id INTEGER PRIMARY KEY, " +
  82. " sessionName TEXT, " +
  83. " sessionUrl TEXT);");
  84. }
  85. @Override
  86. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  87. }
  88. }
  89. }