4 import android.content.Context;
\r
5 import android.database.Cursor;
\r
6 import android.database.SQLException;
\r
7 import android.database.sqlite.SQLiteDatabase;
\r
8 import android.database.sqlite.SQLiteStatement;
\r
10 import com.jm.JmPattern;
\r
11 import com.jm.utility.JmException;
\r
14 private static Dao instance = new Dao();
\r
15 private SQLiteStatement insertStmt = null;
\r
16 private SQLiteStatement updateStmt = null;
\r
17 private SQLiteStatement deleteStmt = null;
\r
19 public static Dao getInstance(){
\r
27 public void init(SQLiteDatabase db){
\r
29 "create table pattern ("
\r
30 + "id integer primary key, "
\r
31 + "type integer not null, "
\r
32 + "siteswap text not null, "
\r
33 + "height integer, "
\r
42 public void start(SQLiteDatabase db){
\r
43 insertStmt = db.compileStatement(
\r
44 "insert into pattern ("
\r
45 + "type, siteswap, height, dwell, name, motion, lang"
\r
47 + "?, ?, ?, ?, ?, ?, ?"
\r
49 updateStmt = db.compileStatement(
\r
50 "update pattern set "
\r
51 + "siteswap = ?, height = ?, dwell = ?, name = ?, motion = ? "
\r
52 + "where id = ? and lang = ?;");
\r
53 deleteStmt = db.compileStatement(
\r
54 "delete from pattern where id = ? and lang = ?;");
\r
57 public void add(JmPattern jp) throws JmException{
\r
58 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
59 SQLiteDatabase db = helper.getWritableDatabase();
\r
60 db.beginTransaction();
\r
63 SQLiteStatement stmt = insertStmt;
\r
64 stmt.bindLong(i++, jp.getType());
\r
65 stmt.bindString(i++, jp.getSiteSwap().toString());
\r
66 stmt.bindLong(i++, jp.getHeight());
\r
67 stmt.bindLong(i++, jp.getDwell());
\r
68 stmt.bindString(i++, jp.getName());
\r
69 stmt.bindString(i++, jp.motionToString());
\r
70 stmt.bindLong(i++, helper.getLangId());
\r
71 stmt.executeInsert();
\r
72 db.setTransactionSuccessful();
\r
73 } catch (SQLException e){
\r
74 throw new JmException(e);
\r
76 db.endTransaction();
\r
80 public void set(JmPattern jp) throws JmException{
\r
81 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
82 SQLiteDatabase db = helper.getWritableDatabase();
\r
83 db.beginTransaction();
\r
86 SQLiteStatement stmt = updateStmt;
\r
87 stmt.bindString(i++, jp.getSiteSwap().toString());
\r
88 stmt.bindLong(i++, jp.getHeight());
\r
89 stmt.bindLong(i++, jp.getDwell());
\r
90 stmt.bindString(i++, jp.getName());
\r
91 stmt.bindString(i++, jp.motionToString());
\r
92 stmt.bindLong(i++, jp.getId());
\r
93 stmt.bindLong(i++, helper.getLangId());
\r
95 db.setTransactionSuccessful();
\r
96 } catch (SQLException e){
\r
97 throw new JmException(e);
\r
99 db.endTransaction();
\r
103 public void delete(int id) throws JmException{
\r
104 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
105 SQLiteDatabase db = helper.getWritableDatabase();
\r
106 db.beginTransaction();
\r
109 SQLiteStatement stmt = deleteStmt;
\r
110 stmt.bindLong(i++, id);
\r
111 stmt.bindLong(i++, helper.getLangId());
\r
113 db.setTransactionSuccessful();
\r
114 } catch (SQLException e){
\r
115 throw new JmException(e);
\r
117 db.endTransaction();
\r
121 public JmPattern[] get(int type) throws JmException{
\r
122 JmPattern[] list = null;
\r
124 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
125 SQLiteDatabase db = helper.getReadableDatabase();
\r
126 String selection = "lang = " + helper.getLangId() + " and type = " + type;
\r
127 Cursor c = db.query(
\r
137 selection, null, null, null, null);
\r
139 list = new JmPattern[c.getCount()];
\r
140 for (int i = 0; i < list.length; i++) {
\r
141 JmPattern jp = new JmPattern(
\r
143 c.getInt(1), // type
\r
144 c.getString(5), // name
\r
145 c.getString(2), // siteswap
\r
146 c.getInt(3), // height
\r
147 c.getInt(4), // dwell
\r
148 JmPattern.getMotion(c.getString(6))); // motions
\r
153 } catch (SQLException e){
\r
154 throw new JmException(e);
\r
159 public JmPattern[] getFromId(int id) throws JmException{
\r
160 JmPattern[] list = null;
\r
162 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
163 SQLiteDatabase db = helper.getReadableDatabase();
\r
164 String selection = "id = " + id + " and lang = " + helper.getLangId();
\r
165 Cursor c = db.query(
\r
175 selection, null, null, null, null);
\r
177 list = new JmPattern[c.getCount()];
\r
178 for (int i = 0; i < list.length; i++) {
\r
179 JmPattern jp = new JmPattern(
\r
181 c.getInt(1), // type
\r
182 c.getString(5), // name
\r
183 c.getString(2), // siteswap
\r
184 c.getInt(3), // height
\r
185 c.getInt(4), // dwell
\r
186 JmPattern.getMotion(c.getString(6))); // motions
\r
191 } catch (SQLException e){
\r
192 throw new JmException(e);
\r
197 public int count() throws JmException{
\r
200 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
201 SQLiteDatabase db = helper.getReadableDatabase();
\r
202 String sql = "select count(*) from pattern where lang = " + helper.getLangId() + ";";
\r
203 Cursor c = db.rawQuery(sql, null);
\r
205 count = c.getInt(0); // count;
\r
207 } catch (SQLException e){
\r
208 throw new JmException(e);
\r
213 public int count(int type) throws JmException{
\r
216 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
217 SQLiteDatabase db = helper.getReadableDatabase();
\r
218 String sql = "select count(*) from pattern where type = " + type + " and lang = " + helper.getLangId() + ";";
\r
219 Cursor c = db.rawQuery(sql, null);
\r
221 count = c.getInt(0); // count;
\r
223 } catch (SQLException e){
\r
224 throw new JmException(e);
\r