OSDN Git Service

debug
[jugglemaster/source.git] / workspace / JuggleMaster / src / com / jm / db / Dao.java
index 4108411..ab134be 100644 (file)
@@ -13,66 +13,45 @@ public class Dao {
        private SQLiteStatement insertStmt = null;\r
        private SQLiteStatement updateStmt = null;\r
        private SQLiteStatement deleteStmt = null;\r
-       \r
-       public static Dao getInstance(){\r
+\r
+       public static Dao getInstance() {\r
                return instance;\r
        }\r
-       \r
-       private Dao(){\r
+\r
+       private Dao() {\r
 \r
        }\r
-       \r
-       public void init(SQLiteDatabase db){\r
-               db.execSQL(\r
-                               "create table pattern ("\r
-                               + "id integer primary key, "\r
-                               + "type integer not null, "\r
-                               + "siteswap text not null, "\r
-                               + "height integer, "\r
-                               + "dwell integer, "\r
-                               + "name integer, "\r
-                               + "motion text,"\r
-                               + "lang integer,"\r
-                               + "idx integer);"\r
-                       );\r
-               //db.execSQL("ALTER TABLE pattern ADD COLUMN lang integer;");\r
-               //db.execSQL("ALTER TABLE pattern ADD COLUMN idx integer;");\r
+\r
+       public void init(SQLiteDatabase db) {\r
+               db.execSQL("create table pattern (" + "id integer primary key, "\r
+                               + "type integer not null, " + "siteswap text not null, "\r
+                               + "height integer, " + "dwell integer, " + "name integer, "\r
+                               + "motion text," + "lang integer," + "idx integer);");\r
                start(db);\r
        }\r
-       \r
+\r
        public void start(SQLiteDatabase db) {\r
-               try {\r
-                       insertStmt = db.compileStatement(\r
-                                       "insert into pattern ("\r
-                                       + "type, siteswap, height, dwell, name, motion, lang, idx"\r
-                                       + ") values ("\r
-                                       + "?, ?, ?, ?, ?, ?, ?, ?"\r
-                                       + ");");        \r
-                       updateStmt = db.compileStatement(\r
-                                       "update pattern set "\r
-                                       + "siteswap = ?, height = ?, dwell = ?, name = ?, motion = ? "\r
-                                       + "where id = ? and lang = ?;");\r
-                       deleteStmt = db.compileStatement(\r
-                                       "delete from pattern where id = ? and lang = ?;");\r
-               }\r
-               catch (Exception e){\r
-                       // TODO\r
-                       e.printStackTrace();\r
-                       throw new RuntimeException();\r
-               }\r
+               insertStmt = db.compileStatement("insert into pattern ("\r
+                               + "type, siteswap, height, dwell, name, motion, lang, idx"\r
+                               + ") values (" + "?, ?, ?, ?, ?, ?, ?, ?" + ");");\r
+               updateStmt = db.compileStatement("update pattern set "\r
+                               + "siteswap = ?, height = ?, dwell = ?, name = ?, motion = ? "\r
+                               + "where id = ? and lang = ?;");\r
+               deleteStmt = db\r
+                               .compileStatement("delete from pattern where id = ? and lang = ?;");\r
        }\r
-       \r
-       public void add(JmPattern jp, int index) throws JmException{\r
+\r
+       public void add(JmPattern jp, int index) throws JmException {\r
                DatabaseHelper helper = DatabaseHelper.getInstance();\r
                add(jp, helper.getLangId(), index);\r
        }\r
-       \r
-       public void addNT(JmPattern jp, int index) throws JmException{\r
+\r
+       public void addNT(JmPattern jp, int index) throws JmException {\r
                DatabaseHelper helper = DatabaseHelper.getInstance();\r
                addNT(jp, helper.getLangId(), index);\r
        }\r
-       \r
-       public void add(JmPattern jp, int lang, int index) throws JmException{\r
+\r
+       public void add(JmPattern jp, int lang, int index) throws JmException {\r
                DatabaseHelper helper = DatabaseHelper.getInstance();\r
                SQLiteDatabase db = helper.getWritableDatabase();\r
                db.beginTransaction();\r
@@ -89,14 +68,14 @@ public class Dao {
                        stmt.bindLong(i++, index);\r
                        stmt.executeInsert();\r
                        db.setTransactionSuccessful();\r
-               } catch (SQLException e){\r
+               } catch (SQLException e) {\r
                        throw new JmException(e);\r
                } finally {\r
                        db.endTransaction();\r
                }\r
        }\r
-       \r
-       public void addNT(JmPattern jp, int lang, int index) throws JmException{\r
+\r
+       public void addNT(JmPattern jp, int lang, int index) throws JmException {\r
                try {\r
                        int i = 1;\r
                        SQLiteStatement stmt = insertStmt;\r
@@ -109,12 +88,12 @@ public class Dao {
                        stmt.bindLong(i++, lang);\r
                        stmt.bindLong(i++, index);\r
                        stmt.executeInsert();\r
-               } catch (SQLException e){\r
+               } catch (SQLException e) {\r
                        throw new JmException(e);\r
                }\r
        }\r
 \r
-       public void set(JmPattern jp) throws JmException{\r
+       public void set(JmPattern jp) throws JmException {\r
                DatabaseHelper helper = DatabaseHelper.getInstance();\r
                SQLiteDatabase db = helper.getWritableDatabase();\r
                db.beginTransaction();\r
@@ -130,14 +109,14 @@ public class Dao {
                        stmt.bindLong(i++, helper.getLangId());\r
                        stmt.execute();\r
                        db.setTransactionSuccessful();\r
-               } catch (SQLException e){\r
-                               throw new JmException(e);\r
+               } catch (SQLException e) {\r
+                       throw new JmException(e);\r
                } finally {\r
                        db.endTransaction();\r
                }\r
        }\r
 \r
-       public void delete(int id) throws JmException{\r
+       public void delete(int id) throws JmException {\r
                DatabaseHelper helper = DatabaseHelper.getInstance();\r
                SQLiteDatabase db = helper.getWritableDatabase();\r
                db.beginTransaction();\r
@@ -148,119 +127,105 @@ public class Dao {
                        stmt.bindLong(i++, helper.getLangId());\r
                        stmt.execute();\r
                        db.setTransactionSuccessful();\r
-               } catch (SQLException e){\r
-                               throw new JmException(e);\r
+               } catch (SQLException e) {\r
+                       throw new JmException(e);\r
                } finally {\r
                        db.endTransaction();\r
                }\r
        }\r
 \r
-       public JmPattern[] get(int type) throws JmException{\r
+       public JmPattern[] get(int type) throws JmException {\r
                JmPattern[] list = null;\r
                try {\r
                        DatabaseHelper helper = DatabaseHelper.getInstance();\r
                        SQLiteDatabase db = helper.getReadableDatabase();\r
                        list = get(db, type);\r
-               } catch (SQLException e){\r
+               } catch (SQLException e) {\r
                        throw new JmException(e);\r
                }\r
                return list;\r
        }\r
 \r
-       public JmPattern[] get(SQLiteDatabase db, int type) throws JmException{\r
+       public JmPattern[] get(SQLiteDatabase db, int type) throws JmException {\r
                DatabaseHelper helper = DatabaseHelper.getInstance();\r
-               String selection = "lang = " + helper.getLangId() + " and type = " + type;\r
+               String selection = "lang = " + helper.getLangId() + " and type = "\r
+                               + type;\r
                String orderBy = "idx";\r
                return get(db, selection, orderBy);\r
        }\r
-       \r
-       public JmPattern[] get(SQLiteDatabase db, String selection, String orderBy) throws JmException{\r
+\r
+       public JmPattern[] get(SQLiteDatabase db, String selection, String orderBy)\r
+                       throws JmException {\r
                DatabaseHelper helper = DatabaseHelper.getInstance();\r
                JmPattern[] list = null;\r
-               Cursor c = db.query(\r
-                               "pattern",\r
-                               new String[] {\r
-                                               "id",\r
-                                               "type",\r
-                                               "siteswap",\r
-                                               "height",\r
-                                               "dwell",\r
-                                               "name",\r
-                                               "motion"},\r
-                                               selection, null, null, null, orderBy);\r
+               Cursor c = db.query("pattern", new String[] { "id", "type", "siteswap",\r
+                               "height", "dwell", "name", "motion" }, selection, null, null,\r
+                               null, orderBy);\r
                c.moveToFirst();\r
                list = new JmPattern[c.getCount()];\r
                for (int i = 0; i < list.length; i++) {\r
-                       JmPattern jp = new JmPattern(\r
-                                       c.getInt(0), // id\r
+                       JmPattern jp = new JmPattern(c.getInt(0), // id\r
                                        c.getInt(1), // type\r
                                        c.getString(5), // name\r
                                        c.getString(2), // siteswap\r
                                        c.getInt(3), // height\r
                                        c.getInt(4), // dwell\r
                                        JmPattern.getMotion(c.getString(6))); // motions\r
-                   list[i] = jp;\r
-                   c.moveToNext();\r
+                       list[i] = jp;\r
+                       c.moveToNext();\r
                }\r
                c.close();\r
                return list;\r
        }\r
-       \r
-       public JmPattern[] getFromId(int id) throws JmException{\r
+\r
+       public JmPattern[] getFromId(int id) throws JmException {\r
                JmPattern[] list = null;\r
                try {\r
                        DatabaseHelper helper = DatabaseHelper.getInstance();\r
                        SQLiteDatabase db = helper.getReadableDatabase();\r
-                       String selection = "id = " + id + " and lang = " + helper.getLangId();\r
-                       Cursor c = db.query(\r
-                                       "pattern",\r
-                                       new String[] {\r
-                                                       "id",\r
-                                                       "type",\r
-                                                       "siteswap",\r
-                                                       "height",\r
-                                                       "dwell",\r
-                                                       "name",\r
-                                                       "motion"},\r
-                                                       selection, null, null, null, null);\r
+                       String selection = "id = " + id + " and lang = "\r
+                                       + helper.getLangId();\r
+                       Cursor c = db.query("pattern", new String[] { "id", "type",\r
+                                       "siteswap", "height", "dwell", "name", "motion" },\r
+                                       selection, null, null, null, null);\r
                        c.moveToFirst();\r
                        list = new JmPattern[c.getCount()];\r
                        for (int i = 0; i < list.length; i++) {\r
-                               JmPattern jp = new JmPattern(\r
-                                               c.getInt(0), // id\r
+                               JmPattern jp = new JmPattern(c.getInt(0), // id\r
                                                c.getInt(1), // type\r
                                                c.getString(5), // name\r
                                                c.getString(2), // siteswap\r
                                                c.getInt(3), // height\r
                                                c.getInt(4), // dwell\r
                                                JmPattern.getMotion(c.getString(6))); // motions\r
-                           list[i] = jp;\r
-                           c.moveToNext();\r
+                               list[i] = jp;\r
+                               c.moveToNext();\r
                        }\r
                        c.close();\r
-               } catch (SQLException e){\r
+               } catch (SQLException e) {\r
                        throw new JmException(e);\r
                }\r
                return list;\r
        }\r
 \r
-       public int max(int type) throws JmException{\r
+       public int max(int type) throws JmException {\r
                int m = -1;\r
                try {\r
                        DatabaseHelper helper = DatabaseHelper.getInstance();\r
                        SQLiteDatabase db = helper.getReadableDatabase();\r
-                       String sql = "select max(idx) from pattern where type = " + type + " and lang = " + helper.getLangId();\r
+                       String sql = "select max(idx) from pattern where type = " + type\r
+                                       + " and lang = " + helper.getLangId();\r
                        Cursor c = db.rawQuery(sql, null);\r
                        c.moveToFirst();\r
                        m = c.getInt(0);\r
                        c.close();\r
-               } catch (SQLException e){\r
+               } catch (SQLException e) {\r
                        throw new JmException(e);\r
                }\r
                return m;\r
        }\r
-       \r
-       public int countAll() throws JmException{\r
+\r
+       public int countAll() throws JmException {\r
                int count = -1;\r
                try {\r
                        DatabaseHelper helper = DatabaseHelper.getInstance();\r
@@ -270,39 +235,41 @@ public class Dao {
                        c.moveToFirst();\r
                        count = c.getInt(0); // count;\r
                        c.close();\r
-               } catch (SQLException e){\r
+               } catch (SQLException e) {\r
                        throw new JmException(e);\r
                }\r
                return count;\r
        }\r
 \r
-       public int count() throws JmException{\r
+       public int count() throws JmException {\r
                int count = -1;\r
                try {\r
                        DatabaseHelper helper = DatabaseHelper.getInstance();\r
                        SQLiteDatabase db = helper.getReadableDatabase();\r
-                       String sql = "select count(*) from pattern where lang = " + helper.getLangId() + ";";\r
+                       String sql = "select count(*) from pattern where lang = "\r
+                                       + helper.getLangId() + ";";\r
                        Cursor c = db.rawQuery(sql, null);\r
                        c.moveToFirst();\r
                        count = c.getInt(0); // count;\r
                        c.close();\r
-               } catch (SQLException e){\r
+               } catch (SQLException e) {\r
                        throw new JmException(e);\r
                }\r
                return count;\r
        }\r
 \r
-       public int count(int type) throws JmException{\r
+       public int count(int type) throws JmException {\r
                int count = -1;\r
                try {\r
                        DatabaseHelper helper = DatabaseHelper.getInstance();\r
                        SQLiteDatabase db = helper.getReadableDatabase();\r
-                       String sql = "select count(*) from pattern where type = " + type + " and lang = " + helper.getLangId() + ";";\r
+                       String sql = "select count(*) from pattern where type = " + type\r
+                                       + " and lang = " + helper.getLangId() + ";";\r
                        Cursor c = db.rawQuery(sql, null);\r
                        c.moveToFirst();\r
                        count = c.getInt(0); // count;\r
                        c.close();\r
-               } catch (SQLException e){\r
+               } catch (SQLException e) {\r
                        throw new JmException(e);\r
                }\r
                return count;\r