Advertisement
Guest User

Untitled

a guest
May 25th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. import android.database.Cursor;
  2. import android.database.sqlite.SQLiteDatabase;
  3. import android.database.sqlite.SQLiteException;
  4. import android.database.sqlite.SQLiteStatement;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Collection;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11.  
  12. public class DatabaseMigrator {
  13.  
  14. private abstract static class Parser<T> {
  15.  
  16. Class<T> classOfT;
  17.  
  18. Parser(Class<T> classOfT) {
  19. this.classOfT = classOfT;
  20. }
  21.  
  22. public abstract T get(Cursor c, int index);
  23.  
  24. public abstract void bind(SQLiteStatement statement, int index, Object value);
  25.  
  26.  
  27. }
  28.  
  29. static List<Parser> supportedParsers = new ArrayList<>();
  30.  
  31. static {
  32. supportedParsers.add(new Parser<Integer>(Integer.class) {
  33. @Override
  34. public Integer get(Cursor c, int index) {
  35. return c.getInt(index);
  36. }
  37.  
  38. @Override
  39. public void bind(SQLiteStatement statement, int index, Object value) {
  40. statement.bindLong(index, (Long) value);
  41. }
  42. });
  43. supportedParsers.add(new Parser<Long>(Long.class){
  44. @Override
  45. public Long get(Cursor c, int index) {
  46. return c.getLong(index);
  47. }
  48.  
  49. @Override
  50. public void bind(SQLiteStatement statement, int index, Object value) {
  51. statement.bindLong(index, (Long) value);
  52. }
  53. });
  54. supportedParsers.add(new Parser<String>(String.class){
  55. @Override
  56. public String get(Cursor c, int index) {
  57. return c.getString(index);
  58. }
  59.  
  60. @Override
  61. public void bind(SQLiteStatement statement, int index, Object value) {
  62. statement.bindString(index, (String) value);
  63. }
  64. });
  65. supportedParsers.add(new Parser<Double>(Double.class){
  66. @Override
  67. public Double get(Cursor c, int index) {
  68. return c.getDouble(index);
  69. }
  70.  
  71. @Override
  72. public void bind(SQLiteStatement statement, int index, Object value) {
  73. statement.bindDouble(index, (Double) value);
  74. }
  75. });
  76. supportedParsers.add(new Parser<Float>(Float.class){
  77. @Override
  78. public Float get(Cursor c, int index) {
  79. return c.getFloat(index);
  80. }
  81.  
  82. @Override
  83. public void bind(SQLiteStatement statement, int index, Object value) {
  84. statement.bindDouble(index, (Double) value);
  85. }
  86. });
  87. }
  88.  
  89. public static void migrate(SQLiteDatabase db, String oldTable, String newTable, Map<String, String> columnMap, List<Class> oldClasses, List<Class> newClasses) {
  90. Cursor c = getAllValues(db, oldTable, getCollectionAsArray(columnMap.keySet()));
  91. if(c.moveToFirst()) {
  92. do {
  93. Object []values = getObjectsFromCursor(c, oldClasses);
  94. String argsSymbol = getCollectionAsArgsSymbol(values);
  95. String insertStatement = "INSERT INTO " + newTable + "(" + getCollectionAsString(columnMap.values()) + ") VALUES(" + argsSymbol + ")";
  96. SQLiteStatement mStatement = db.compileStatement(insertStatement);
  97. insert(mStatement, newClasses, values);
  98. } while (c.moveToNext());
  99. }
  100. c.close();
  101. }
  102.  
  103. protected static Object [] getObjectsFromCursor(Cursor c, List<Class> classes) {
  104. Object []objects = new Object[c.getColumnCount()];
  105. for(int i = 0; i < c.getColumnCount(); i++) {
  106. objects[i] = getAs(i, c, classes.get(i));
  107. }
  108. return objects;
  109. }
  110.  
  111. protected static void bindValues(SQLiteStatement mStatement, Object []values, List<Class> newClasses) {
  112. mStatement.clearBindings();
  113. for(int i = 0; i < values.length; i++) {
  114. bindAs(mStatement, i, values[i], newClasses.get(i));
  115. }
  116. }
  117.  
  118. private static Object getAs(int index, Cursor c, Class clazz) {
  119. Object obj = null;
  120. for(Parser parser : supportedParsers) {
  121. if(parser.classOfT == clazz) {
  122. obj = parser.get(c, index);
  123. }
  124. }
  125. return obj;
  126. }
  127.  
  128. private static void bindAs(SQLiteStatement mStatement, int index, Object value, Class clazz) {
  129. if(value==null) {
  130. mStatement.bindNull(index);
  131. return;
  132. }
  133. for(Parser<?> parser : supportedParsers) {
  134. if(parser.classOfT == clazz) {
  135. parser.bind(mStatement, index, value);
  136. }
  137. }
  138. }
  139.  
  140. private static Cursor getAllValues(SQLiteDatabase db, String table, String []columns) {
  141. return db.query(table, columns, null, null, null, null, null);
  142. }
  143.  
  144. private static void insert(SQLiteStatement mStatement, List<Class> newClasses, Object[] values) {
  145. try {
  146. bindValues(mStatement, values, newClasses);
  147. mStatement.executeInsert();
  148. } catch (SQLiteException sqle) {
  149. sqle.printStackTrace();
  150. } catch (Throwable t) {
  151. t.printStackTrace();
  152. }
  153. }
  154.  
  155. public static Map<String, String> createMap(String... columns) {
  156. if(columns==null || columns.length % 2 != 0 || columns.length == 0) {
  157. throw new IllegalArgumentException("There should be even non-zero number of columns");
  158. }
  159. Map<String, String> columnMap = new HashMap<>();
  160. for(int i = 0; i < columns.length; i = i + 2) {
  161. columnMap.put(columns[i], columns[i+1]);
  162. }
  163. return columnMap;
  164. }
  165.  
  166. private static String getCollectionAsString(Collection<String> set) {
  167. StringBuilder sb = new StringBuilder();
  168. int i = 0;
  169. for(String str : set) {
  170. sb.append(str);
  171. if(i != set.size()-1) {
  172. sb.append(", ");
  173. }
  174. i++;
  175. }
  176. return sb.toString();
  177. }
  178.  
  179. private static String []getCollectionAsArray(Collection<String> set) {
  180. String []strings = new String[set.size()];
  181. int i = 0;
  182. for(String str : set) {
  183. strings[i] = str;
  184. i++;
  185. }
  186. return strings;
  187. }
  188.  
  189. private static String getCollectionAsArgsSymbol(Object []values) {
  190. StringBuilder sb = new StringBuilder();
  191. for(int i = 0; i < values.length; i++) {
  192. sb.append("?");
  193. if(i != values.length-1) {
  194. sb.append(", ");
  195. }
  196. i++;
  197. }
  198. return sb.toString();
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement