Commit 42f883a1f209ab7c8cbbe6eece68d3efbaaacf95

Authored by 张道锋
1 parent 762b1b1d

no message

.idea/modules.xml
... ... @@ -4,8 +4,8 @@
4 4 <modules>
5 5 <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
6 6 <module fileurl="file://$PROJECT_DIR$/mvpsdk/mvpsdk.iml" filepath="$PROJECT_DIR$/mvpsdk/mvpsdk.iml" />
  7 + <module fileurl="file://$PROJECT_DIR$/parentWorkHolper.iml" filepath="$PROJECT_DIR$/parentWorkHolper.iml" />
7 8 <module fileurl="file://E:\parentwork\parentWorkHolper.iml" filepath="E:\parentwork\parentWorkHolper.iml" />
8   - <module fileurl="file://$PROJECT_DIR$/parentwork.iml" filepath="$PROJECT_DIR$/parentwork.iml" />
9 9 <module fileurl="file://F:\parentWorkHolper\parentwork.iml" filepath="F:\parentWorkHolper\parentwork.iml" />
10 10 <module fileurl="file://$PROJECT_DIR$/processor/processor.iml" filepath="$PROJECT_DIR$/processor/processor.iml" />
11 11 <module fileurl="file://$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" filepath="$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" />
... ...
app/libs/processor.jar
No preview for this file type
app/src/main/assets/migrations/2.sql
... ... @@ -1 +0,0 @@
1   -ALTER table test INSERT ADD COLUMN age INTEGER
app/src/main/assets/migrations/3.sql
... ... @@ -1 +0,0 @@
1   -ALTER table test INSERT ADD COLUMN name TEXT
app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java
... ... @@ -2,16 +2,13 @@ package com.shunzhi.parent.annotation;
2 2  
3 3 import org.shunzhi.processorlibs.AddColumn;
4 4  
5   -/**
6   - * Created by 10501 on 2018/3/28.
7   - */
8 5 public class VersionTest {
9   - @AddColumn(version = 3,tableName = "test",type = "String",isDebug = false)
  6 + @AddColumn(version = 3, tableName = "test", type = "String", isDebug = true)
10 7 private String name;
11 8  
12   - @AddColumn(version = 3,tableName = "test",type = "Integer",isDebug = false)
  9 + @AddColumn(version = 3, tableName = "test", type = "Integer", isDebug = true)
13 10 Integer age;
14   -
15   - String area;
  11 + @AddColumn(version = 3, tableName = "test", type = "Boolean", isDebug = true)
  12 + Boolean area;
16 13  
17 14 }
... ...
processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java
... ... @@ -16,4 +16,9 @@ public @interface AddColumn {
16 16 String type();
17 17  
18 18 boolean isDebug() default true;
  19 +
  20 + String type_string = "string";
  21 + String type_boolean = "boolean";
  22 + String type_integer = "integer";
  23 +
19 24 }
... ...
processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java
1 1 package org.shunzhi.processorlibs;
2 2  
3   -import org.greenrobot.greendao.annotation.Entity;
4   -
5 3 import java.io.BufferedWriter;
6 4 import java.io.File;
7   -import java.io.FileOutputStream;
8 5 import java.io.FileWriter;
9 6 import java.io.IOException;
10 7 import java.io.OutputStreamWriter;
11 8 import java.io.PrintStream;
12   -import java.util.ArrayList;
13 9 import java.util.HashMap;
14   -import java.util.List;
15   -import java.util.Map;
  10 +import java.util.HashSet;
16 11 import java.util.Set;
17 12  
18 13 import javax.annotation.processing.AbstractProcessor;
... ... @@ -26,19 +21,27 @@ import javax.lang.model.element.TypeElement;
26 21 @SupportedAnnotationTypes({"org.shunzhi.processorlibs.AddColumn"})
27 22 @SupportedSourceVersion(SourceVersion.RELEASE_7)
28 23 public class VersionProcessor extends AbstractProcessor {
29   - private final String sql_insert = "ALTER table %s INSERT ADD COLUMN %s %s";
30   - private final String sql_update = "UPDATE table message SET %s = '%s' WHERE %s = '%s'";
  24 + //private final String sql_update = "UPDATE table message SET %s = '%s' WHERE %s = '%s'";
31 25  
32   - String path = "app\src\main\assets\migrations".replace("\", File.separator);
  26 + //String path = "app\src\main\assets\migrations".replace("\", File.separator);
33 27  
34 28 @Override
35 29 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
36 30 // File file = new File("test");
37 31 // if (!file.exists())file.mkdir();
38   - int i = 0;
  32 +// File countFile = new File("测试数据.txt");
  33 +// try {
  34 +// BufferedWriter writer = new BufferedWriter(new FileWriter(countFile));
  35 +// writer.write(System.currentTimeMillis() + "");
  36 +// writer.newLine();
  37 +// writer.flush();writer.close();
  38 +// } catch (IOException e) {
  39 +// e.printStackTrace();
  40 +// }
  41 +
39 42 BufferedWriter testBw = null;
40 43  
41   - HashMap<Integer, List<String>> map = new HashMap<>();
  44 + HashMap<Integer, Set<String>> map = new HashMap<>();
42 45  
43 46 try {
44 47 for (Element element : roundEnv.getElementsAnnotatedWith(AddColumn.class)) {
... ... @@ -49,21 +52,32 @@ public class VersionProcessor extends AbstractProcessor {
49 52 int version = addColumn.version();
50 53 String tableName = addColumn.tableName();
51 54 String type = addColumn.type();
52   - List<String> list = map.get(version);
  55 + Set<String> set = map.get(version);
53 56 boolean isDebug = addColumn.isDebug();
54   - if (list == null) list = new ArrayList<>();
  57 + if (set == null) set = new HashSet<>();
55 58 String sql = "null";
  59 + String sql_insert = "ALTER table %s ADD COLUMN %s %s";
56 60 if (type.equalsIgnoreCase("String")) {
57 61 sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "TEXT");
58   - if (!isDebug) list.add(sql);
  62 + if (!isDebug)
  63 + set.add(sql);
59 64 } else if (type.equalsIgnoreCase("Integer")) {
60 65 sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "INTEGER");
61   - if (!isDebug) list.add(sql);
  66 + if (!isDebug)
  67 + set.add(sql);
  68 + } else if (type.equalsIgnoreCase("boolean")){
  69 + sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "INTEGER");
  70 + if (!isDebug)
  71 + set.add(sql);
  72 + }
  73 + if (isDebug) {
  74 + testBw.write(version + " ");
  75 + testBw.write(sql);
  76 + testBw.newLine();
  77 + testBw.flush();
  78 + } else {
  79 + map.put(version, set);
62 80 }
63   - testBw.write(version + " ");
64   - testBw.write(sql);
65   - testBw.newLine();
66   - testBw.flush();
67 81 }
68 82 } catch (Exception e) {
69 83 e.printStackTrace();
... ... @@ -74,18 +88,27 @@ public class VersionProcessor extends AbstractProcessor {
74 88 e.printStackTrace();
75 89 }
76 90 }
77   -
  91 + File tmp = new File("tmp");
  92 +// File migrations = new File(tmp.getParentFile(), "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator));
  93 +// if (migrations.exists() || migrations.mkdirs()) {
  94 +// File[] files = migrations.listFiles();
  95 +// if (files != null)
  96 +// for (File f : files) {
  97 +// if (!f.getName().equals("1.sql") && f.getName().endsWith(".sql")) f.delete();
  98 +// }
  99 +// }
78 100 for (Integer version : map.keySet()) {
79 101 try {
80   - File tmp = new File("tmp");
81 102 String path = "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator) + version + ".sql";
82 103 File file = new File(tmp.getParentFile(), path);
83 104 BufferedWriter bw = new BufferedWriter(new FileWriter(file));
  105 + StringBuilder builder = new StringBuilder();
84 106 for (String str : map.get(version)) {
85   - bw.write(str);
86   - bw.newLine();
87   - bw.flush();
  107 + builder.append(str);
  108 + builder.append("\r\n");
88 109 }
  110 + bw.write(builder.toString());
  111 + bw.flush();
89 112 bw.close();
90 113 } catch (IOException e) {
91 114 e.printStackTrace();
... ... @@ -93,4 +116,4 @@ public class VersionProcessor extends AbstractProcessor {
93 116 }
94 117 return true;
95 118 }
96 119 -}
  120 +}
97 121 \ No newline at end of file
... ...
sql.txt
1   -3 ALTER table test INSERT ADD COLUMN age INTEGER
  1 +3 ALTER table test ADD COLUMN area INTEGER
... ...