create changelog entry
[debian/openrocket] / android / src / net / sf / openrocket / android / db / DbAdapter.java
1 package net.sf.openrocket.android.db;\r
2 \r
3 import net.sf.openrocket.android.util.AndroidLogWrapper;\r
4 import android.content.Context;\r
5 import android.database.SQLException;\r
6 import android.database.sqlite.SQLiteDatabase;\r
7 import android.database.sqlite.SQLiteOpenHelper;\r
8 \r
9 public class DbAdapter {\r
10 \r
11     private static final String TAG = "DbAdapter";\r
12     private DatabaseHelper mDbHelper;\r
13     private SQLiteDatabase mDb;\r
14 \r
15     private static final String DATABASE_NAME = "openrocket";\r
16     private static final int DATABASE_VERSION = 3;\r
17 \r
18     private final Context mCtx;\r
19 \r
20     private MotorDao motorDao;\r
21     \r
22     public MotorDao getMotorDao() {\r
23                 return motorDao;\r
24         }\r
25     \r
26         private class DatabaseHelper extends SQLiteOpenHelper {\r
27         DatabaseHelper(Context context) {\r
28             super(context, DATABASE_NAME, null, DATABASE_VERSION);\r
29         }\r
30 \r
31         @Override\r
32         public void onCreate(SQLiteDatabase db) {\r
33                 executeSQL( db, MotorDao.create());\r
34         }\r
35         \r
36         @Override\r
37         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\r
38             AndroidLogWrapper.w(DbAdapter.class, "Upgrading database from version " + oldVersion + " to "\r
39                     + newVersion + ", which will destroy all old data");\r
40             executeSQL(db, MotorDao.update(oldVersion, newVersion));\r
41         }\r
42 \r
43         private void executeSQL( SQLiteDatabase db, String[] sqls ) {\r
44                 for(String s: sqls ) {\r
45                         db.execSQL(s);\r
46                 }\r
47         }\r
48 \r
49     }\r
50  \r
51     /**\r
52      * Constructor - takes the context to allow the database to be\r
53      * opened/created\r
54      * \r
55      * @param ctx the Context within which to work\r
56      */\r
57     public DbAdapter(Context ctx) {\r
58         this.mCtx = ctx;\r
59     }\r
60 \r
61     /**\r
62      * Open the database. If it cannot be opened, try to create a new\r
63      * instance of the database. If it cannot be created, throw an exception to\r
64      * signal the failure\r
65      * \r
66      * @return this (self reference, allowing this to be chained in an\r
67      *         initialization call)\r
68      * @throws SQLException if the database could be neither opened or created\r
69      */\r
70     public DbAdapter open() throws SQLException {\r
71         mDbHelper = new DatabaseHelper(mCtx);\r
72         mDb = mDbHelper.getWritableDatabase();\r
73         motorDao = new MotorDao(mDb);\r
74         return this;\r
75     }\r
76 \r
77     public void close() {\r
78         mDbHelper.close();\r
79     }\r
80 \r
81 }