61b07689c86fbbb0af3079d70a3e3fa6c8e9c645
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / db / DbHandler.java
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
20 import java.util.Vector;
21
22 import eu.alefzero.owncloud.OwnCloudSession;
23
24 import android.content.ContentValues;
25 import android.content.Context;
26 import android.database.Cursor;
27 import android.database.sqlite.SQLiteDatabase;
28 import android.database.sqlite.SQLiteOpenHelper;
29
30 /**
31 * Custom database helper for ownCloud
32 * @author Bartek Przybylski
33 *
34 */
35 public class DbHandler {
36 private SQLiteDatabase mDB;
37 private OpenerHepler mHelper;
38 private final String mDatabaseName = "ownCloud";
39 private final String TABLE_SESSIONS = "sessions";
40 private final int mDatabaseVersion = 1;
41
42 public DbHandler(Context context) {
43 mHelper = new OpenerHepler(context);
44 mDB = mHelper.getWritableDatabase();
45 }
46
47 public Vector<OwnCloudSession> getSessionList() {
48 Cursor c = mDB.query(TABLE_SESSIONS, null, null, null, null, null, null);
49 Vector<OwnCloudSession> v = new Vector<OwnCloudSession>();
50 if (!c.moveToFirst()) {
51 return v;
52 }
53 while (!c.isAfterLast()) {
54 v.add(new OwnCloudSession(c.getString(c.getColumnIndex("sessionName")),
55 c.getString(c.getColumnIndex("sessionUrl")),
56 c.getInt(c.getColumnIndex("_id"))));
57 c.moveToNext();
58 }
59 c.close();
60 return v;
61 }
62
63 public void addSession(String sessionName, String uri) {
64 ContentValues cv = new ContentValues();
65 cv.put("sessionName", sessionName);
66 cv.put("sessionUrl", uri);
67 mDB.insert(TABLE_SESSIONS, null, cv);
68 }
69
70 public void removeSessionWithId(int sessionId) {
71 mDB.delete(TABLE_SESSIONS, "_id = ?", new String[] {String.valueOf(sessionId)});
72 }
73
74 public void changeSessionFields(int id, String hostname, String uri) {
75 ContentValues cv = new ContentValues();
76 cv.put("sessionName", hostname);
77 cv.put("sessionUrl", uri);
78 mDB.update(TABLE_SESSIONS, cv, "_id = ?", new String[] {String.valueOf(id)});
79 }
80
81 public void close() {
82 mDB.close();
83 }
84
85 private class OpenerHepler extends SQLiteOpenHelper {
86 public OpenerHepler(Context context) {
87 super(context, mDatabaseName, null, mDatabaseVersion);
88 }
89
90 @Override
91 public void onCreate(SQLiteDatabase db) {
92 db.execSQL("CREATE TABLE " + TABLE_SESSIONS + " (" +
93 " _id INTEGER PRIMARY KEY, " +
94 " sessionName TEXT, " +
95 " sessionUrl TEXT);");
96 }
97
98 @Override
99 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
100 }
101 }
102 }