3 import android.database.Cursor;
\r
4 import android.database.SQLException;
\r
5 import android.database.sqlite.SQLiteDatabase;
\r
6 import android.database.sqlite.SQLiteStatement;
\r
8 import com.jm.JmPattern;
\r
9 import com.jm.utility.JmException;
\r
12 private static Dao instance = new Dao();
\r
13 private SQLiteStatement insertStmt = null;
\r
14 private SQLiteStatement updateStmt = null;
\r
15 private SQLiteStatement deleteStmt = null;
\r
17 public static Dao getInstance(){
\r
25 public void init(SQLiteDatabase db)
\r
28 "create table pattern ("
\r
29 + "id integer primary key, "
\r
30 + "type integer not null, "
\r
31 + "siteswap text not null, "
\r
32 + "height integer, "
\r
40 public void start(SQLiteDatabase db){
\r
41 insertStmt = db.compileStatement(
\r
42 "insert into pattern ("
\r
43 + "type, siteswap, height, dwell, name, motion"
\r
45 + "?, ?, ?, ?, ?, ?"
\r
47 updateStmt = db.compileStatement(
\r
48 "update pattern set "
\r
49 + "siteswap = ?, height = ?, dwell = ?, name = ?, motion = ? "
\r
51 deleteStmt = db.compileStatement(
\r
52 "delete from pattern where id = ?;");
\r
55 public void add(JmPattern jp) throws JmException{
\r
56 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
57 SQLiteDatabase db = helper.getWritableDatabase();
\r
58 db.beginTransaction();
\r
61 SQLiteStatement stmt = insertStmt;
\r
62 stmt.bindLong(i++, jp.getType());
\r
63 stmt.bindString(i++, jp.getSiteSwap().toString());
\r
64 stmt.bindLong(i++, jp.getHeight());
\r
65 stmt.bindLong(i++, jp.getDwell());
\r
66 stmt.bindString(i++, jp.getName());
\r
67 stmt.bindString(i++, jp.motionToString());
\r
68 stmt.executeInsert();
\r
69 db.setTransactionSuccessful();
\r
70 } catch (SQLException e){
\r
71 throw new JmException(e);
\r
73 db.endTransaction();
\r
77 public void set(JmPattern jp) throws JmException{
\r
78 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
79 SQLiteDatabase db = helper.getWritableDatabase();
\r
80 db.beginTransaction();
\r
83 SQLiteStatement stmt = updateStmt;
\r
84 stmt.bindString(i++, jp.getSiteSwap().toString());
\r
85 stmt.bindLong(i++, jp.getHeight());
\r
86 stmt.bindLong(i++, jp.getDwell());
\r
87 stmt.bindString(i++, jp.getName());
\r
88 stmt.bindString(i++, jp.motionToString());
\r
89 stmt.bindLong(i++, jp.getId());
\r
91 db.setTransactionSuccessful();
\r
92 } catch (SQLException e){
\r
93 throw new JmException(e);
\r
95 db.endTransaction();
\r
99 public void delete(int id) throws JmException{
\r
100 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
101 SQLiteDatabase db = helper.getWritableDatabase();
\r
102 db.beginTransaction();
\r
105 SQLiteStatement stmt = deleteStmt;
\r
106 stmt.bindLong(i++, id);
\r
108 db.setTransactionSuccessful();
\r
109 } catch (SQLException e){
\r
110 throw new JmException(e);
\r
112 db.endTransaction();
\r
116 public JmPattern[] get(int type) throws JmException{
\r
117 JmPattern[] list = null;
\r
119 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
120 SQLiteDatabase db = helper.getReadableDatabase();
\r
121 String selection = "type = " + type;
\r
122 Cursor c = db.query(
\r
132 selection, null, null, null, null);
\r
134 list = new JmPattern[c.getCount()];
\r
135 for (int i = 0; i < list.length; i++) {
\r
136 JmPattern jp = new JmPattern(
\r
138 c.getInt(1), // type
\r
139 c.getString(5), // name
\r
140 c.getString(2), // siteswap
\r
141 c.getInt(3), // height
\r
142 c.getInt(4), // dwell
\r
143 JmPattern.getMotion(c.getString(6))); // motions
\r
148 } catch (SQLException e){
\r
149 throw new JmException(e);
\r
154 public JmPattern[] getFromId(int id) throws JmException{
\r
155 JmPattern[] list = null;
\r
157 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
158 SQLiteDatabase db = helper.getReadableDatabase();
\r
159 String selection = "id = " + id;
\r
160 Cursor c = db.query(
\r
170 selection, null, null, null, null);
\r
172 list = new JmPattern[c.getCount()];
\r
173 for (int i = 0; i < list.length; i++) {
\r
174 JmPattern jp = new JmPattern(
\r
176 c.getInt(1), // type
\r
177 c.getString(5), // name
\r
178 c.getString(2), // siteswap
\r
179 c.getInt(3), // height
\r
180 c.getInt(4), // dwell
\r
181 JmPattern.getMotion(c.getString(6))); // motions
\r
186 } catch (SQLException e){
\r
187 throw new JmException(e);
\r
192 public int count() throws JmException{
\r
195 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
196 SQLiteDatabase db = helper.getReadableDatabase();
\r
197 String sql = "select count(*) from pattern;";
\r
198 Cursor c = db.rawQuery(sql, null);
\r
200 count = c.getInt(0); // count;
\r
202 } catch (SQLException e){
\r
203 throw new JmException(e);
\r