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
27 "create table pattern ("
\r
28 + "id integer primary key, "
\r
29 + "type integer not null, "
\r
30 + "siteswap text not null, "
\r
31 + "height integer, "
\r
38 //db.execSQL("ALTER TABLE pattern ADD COLUMN lang integer;");
\r
39 //db.execSQL("ALTER TABLE pattern ADD COLUMN idx integer;");
\r
43 public void start(SQLiteDatabase db) {
\r
45 insertStmt = db.compileStatement(
\r
46 "insert into pattern ("
\r
47 + "type, siteswap, height, dwell, name, motion, lang, idx"
\r
49 + "?, ?, ?, ?, ?, ?, ?, ?"
\r
51 updateStmt = db.compileStatement(
\r
52 "update pattern set "
\r
53 + "siteswap = ?, height = ?, dwell = ?, name = ?, motion = ? "
\r
54 + "where id = ? and lang = ?;");
\r
55 deleteStmt = db.compileStatement(
\r
56 "delete from pattern where id = ? and lang = ?;");
\r
58 catch (Exception e){
\r
60 e.printStackTrace();
\r
61 throw new RuntimeException();
\r
65 public void add(JmPattern jp, int index) throws JmException{
\r
66 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
67 add(jp, helper.getLangId(), index);
\r
70 public void addNT(JmPattern jp, int index) throws JmException{
\r
71 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
72 addNT(jp, helper.getLangId(), index);
\r
75 public void add(JmPattern jp, int lang, int index) throws JmException{
\r
76 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
77 SQLiteDatabase db = helper.getWritableDatabase();
\r
78 db.beginTransaction();
\r
81 SQLiteStatement stmt = insertStmt;
\r
82 stmt.bindLong(i++, jp.getType());
\r
83 stmt.bindString(i++, jp.getSiteSwap().toString());
\r
84 stmt.bindLong(i++, jp.getHeight());
\r
85 stmt.bindLong(i++, jp.getDwell());
\r
86 stmt.bindString(i++, jp.getName());
\r
87 stmt.bindString(i++, jp.motionToString());
\r
88 stmt.bindLong(i++, lang);
\r
89 stmt.bindLong(i++, index);
\r
90 stmt.executeInsert();
\r
91 db.setTransactionSuccessful();
\r
92 } catch (SQLException e){
\r
93 throw new JmException(e);
\r
95 db.endTransaction();
\r
99 public void addNT(JmPattern jp, int lang, int index) throws JmException{
\r
102 SQLiteStatement stmt = insertStmt;
\r
103 stmt.bindLong(i++, jp.getType());
\r
104 stmt.bindString(i++, jp.getSiteSwap().toString());
\r
105 stmt.bindLong(i++, jp.getHeight());
\r
106 stmt.bindLong(i++, jp.getDwell());
\r
107 stmt.bindString(i++, jp.getName());
\r
108 stmt.bindString(i++, jp.motionToString());
\r
109 stmt.bindLong(i++, lang);
\r
110 stmt.bindLong(i++, index);
\r
111 stmt.executeInsert();
\r
112 } catch (SQLException e){
\r
113 throw new JmException(e);
\r
117 public void set(JmPattern jp) throws JmException{
\r
118 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
119 SQLiteDatabase db = helper.getWritableDatabase();
\r
120 db.beginTransaction();
\r
123 SQLiteStatement stmt = updateStmt;
\r
124 stmt.bindString(i++, jp.getSiteSwap().toString());
\r
125 stmt.bindLong(i++, jp.getHeight());
\r
126 stmt.bindLong(i++, jp.getDwell());
\r
127 stmt.bindString(i++, jp.getName());
\r
128 stmt.bindString(i++, jp.motionToString());
\r
129 stmt.bindLong(i++, jp.getId());
\r
130 stmt.bindLong(i++, helper.getLangId());
\r
132 db.setTransactionSuccessful();
\r
133 } catch (SQLException e){
\r
134 throw new JmException(e);
\r
136 db.endTransaction();
\r
140 public void delete(int id) throws JmException{
\r
141 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
142 SQLiteDatabase db = helper.getWritableDatabase();
\r
143 db.beginTransaction();
\r
146 SQLiteStatement stmt = deleteStmt;
\r
147 stmt.bindLong(i++, id);
\r
148 stmt.bindLong(i++, helper.getLangId());
\r
150 db.setTransactionSuccessful();
\r
151 } catch (SQLException e){
\r
152 throw new JmException(e);
\r
154 db.endTransaction();
\r
158 public JmPattern[] get(int type) throws JmException{
\r
159 JmPattern[] list = null;
\r
161 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
162 SQLiteDatabase db = helper.getReadableDatabase();
\r
163 list = get(db, type);
\r
164 } catch (SQLException e){
\r
165 throw new JmException(e);
\r
170 public JmPattern[] get(SQLiteDatabase db, int type) throws JmException{
\r
171 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
172 String selection = "lang = " + helper.getLangId() + " and type = " + type;
\r
173 String orderBy = "idx";
\r
174 return get(db, selection, orderBy);
\r
177 public JmPattern[] get(SQLiteDatabase db, String selection, String orderBy) throws JmException{
\r
178 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
179 JmPattern[] list = null;
\r
180 Cursor c = db.query(
\r
190 selection, null, null, null, orderBy);
\r
192 list = new JmPattern[c.getCount()];
\r
193 for (int i = 0; i < list.length; i++) {
\r
194 JmPattern jp = new JmPattern(
\r
196 c.getInt(1), // type
\r
197 c.getString(5), // name
\r
198 c.getString(2), // siteswap
\r
199 c.getInt(3), // height
\r
200 c.getInt(4), // dwell
\r
201 JmPattern.getMotion(c.getString(6))); // motions
\r
209 public JmPattern[] getFromId(int id) throws JmException{
\r
210 JmPattern[] list = null;
\r
212 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
213 SQLiteDatabase db = helper.getReadableDatabase();
\r
214 String selection = "id = " + id + " and lang = " + helper.getLangId();
\r
215 Cursor c = db.query(
\r
225 selection, null, null, null, null);
\r
227 list = new JmPattern[c.getCount()];
\r
228 for (int i = 0; i < list.length; i++) {
\r
229 JmPattern jp = new JmPattern(
\r
231 c.getInt(1), // type
\r
232 c.getString(5), // name
\r
233 c.getString(2), // siteswap
\r
234 c.getInt(3), // height
\r
235 c.getInt(4), // dwell
\r
236 JmPattern.getMotion(c.getString(6))); // motions
\r
241 } catch (SQLException e){
\r
242 throw new JmException(e);
\r
247 public int max(int type) throws JmException{
\r
250 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
251 SQLiteDatabase db = helper.getReadableDatabase();
\r
252 String sql = "select max(idx) from pattern where type = " + type + " and lang = " + helper.getLangId();
\r
253 Cursor c = db.rawQuery(sql, null);
\r
257 } catch (SQLException e){
\r
258 throw new JmException(e);
\r
263 public int countAll() throws JmException{
\r
266 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
267 SQLiteDatabase db = helper.getReadableDatabase();
\r
268 String sql = "select count(*) from pattern;";
\r
269 Cursor c = db.rawQuery(sql, null);
\r
271 count = c.getInt(0); // count;
\r
273 } catch (SQLException e){
\r
274 throw new JmException(e);
\r
279 public int count() throws JmException{
\r
282 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
283 SQLiteDatabase db = helper.getReadableDatabase();
\r
284 String sql = "select count(*) from pattern where lang = " + helper.getLangId() + ";";
\r
285 Cursor c = db.rawQuery(sql, null);
\r
287 count = c.getInt(0); // count;
\r
289 } catch (SQLException e){
\r
290 throw new JmException(e);
\r
295 public int count(int type) throws JmException{
\r
298 DatabaseHelper helper = DatabaseHelper.getInstance();
\r
299 SQLiteDatabase db = helper.getReadableDatabase();
\r
300 String sql = "select count(*) from pattern where type = " + type + " and lang = " + helper.getLangId() + ";";
\r
301 Cursor c = db.rawQuery(sql, null);
\r
303 count = c.getInt(0); // count;
\r
305 } catch (SQLException e){
\r
306 throw new JmException(e);
\r