diff --git a/.idea/modules.xml b/.idea/modules.xml index f586709..889474f 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,8 +4,8 @@ + - diff --git a/app/libs/processor.jar b/app/libs/processor.jar index 3ae4550..f4074fc 100644 Binary files a/app/libs/processor.jar and b/app/libs/processor.jar differ diff --git a/app/src/main/assets/migrations/2.sql b/app/src/main/assets/migrations/2.sql deleted file mode 100644 index d0d505f..0000000 --- a/app/src/main/assets/migrations/2.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER table test INSERT ADD COLUMN age INTEGER diff --git a/app/src/main/assets/migrations/3.sql b/app/src/main/assets/migrations/3.sql deleted file mode 100644 index 468cdbf..0000000 --- a/app/src/main/assets/migrations/3.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER table test INSERT ADD COLUMN name TEXT diff --git a/app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java b/app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java index 4f7f8a6..4e171da 100644 --- a/app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java +++ b/app/src/main/java/com/shunzhi/parent/annotation/VersionTest.java @@ -2,16 +2,13 @@ package com.shunzhi.parent.annotation; import org.shunzhi.processorlibs.AddColumn; -/** - * Created by 10501 on 2018/3/28. - */ public class VersionTest { - @AddColumn(version = 3,tableName = "test",type = "String",isDebug = false) + @AddColumn(version = 3, tableName = "test", type = "String", isDebug = true) private String name; - @AddColumn(version = 3,tableName = "test",type = "Integer",isDebug = false) + @AddColumn(version = 3, tableName = "test", type = "Integer", isDebug = true) Integer age; - - String area; + @AddColumn(version = 3, tableName = "test", type = "Boolean", isDebug = true) + Boolean area; } diff --git a/processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java b/processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java index fa11f55..c02f58c 100644 --- a/processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java +++ b/processor/src/main/java/org/shunzhi/processorlibs/AddColumn.java @@ -16,4 +16,9 @@ public @interface AddColumn { String type(); boolean isDebug() default true; + + String type_string = "string"; + String type_boolean = "boolean"; + String type_integer = "integer"; + } diff --git a/processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java b/processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java index cfba19d..8cbffc8 100644 --- a/processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java +++ b/processor/src/main/java/org/shunzhi/processorlibs/VersionProcessor.java @@ -1,18 +1,13 @@ package org.shunzhi.processorlibs; -import org.greenrobot.greendao.annotation.Entity; - import java.io.BufferedWriter; import java.io.File; -import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintStream; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.HashSet; import java.util.Set; import javax.annotation.processing.AbstractProcessor; @@ -26,19 +21,27 @@ import javax.lang.model.element.TypeElement; @SupportedAnnotationTypes({"org.shunzhi.processorlibs.AddColumn"}) @SupportedSourceVersion(SourceVersion.RELEASE_7) public class VersionProcessor extends AbstractProcessor { - private final String sql_insert = "ALTER table %s INSERT ADD COLUMN %s %s"; - private final String sql_update = "UPDATE table message SET %s = '%s' WHERE %s = '%s'"; + //private final String sql_update = "UPDATE table message SET %s = '%s' WHERE %s = '%s'"; - String path = "app\\src\\main\\assets\\migrations".replace("\\", File.separator); + //String path = "app\\src\\main\\assets\\migrations".replace("\\", File.separator); @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { // File file = new File("test"); // if (!file.exists())file.mkdir(); - int i = 0; +// File countFile = new File("测试数据.txt"); +// try { +// BufferedWriter writer = new BufferedWriter(new FileWriter(countFile)); +// writer.write(System.currentTimeMillis() + ""); +// writer.newLine(); +// writer.flush();writer.close(); +// } catch (IOException e) { +// e.printStackTrace(); +// } + BufferedWriter testBw = null; - HashMap> map = new HashMap<>(); + HashMap> map = new HashMap<>(); try { for (Element element : roundEnv.getElementsAnnotatedWith(AddColumn.class)) { @@ -49,21 +52,32 @@ public class VersionProcessor extends AbstractProcessor { int version = addColumn.version(); String tableName = addColumn.tableName(); String type = addColumn.type(); - List list = map.get(version); + Set set = map.get(version); boolean isDebug = addColumn.isDebug(); - if (list == null) list = new ArrayList<>(); + if (set == null) set = new HashSet<>(); String sql = "null"; + String sql_insert = "ALTER table %s ADD COLUMN %s %s"; if (type.equalsIgnoreCase("String")) { sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "TEXT"); - if (!isDebug) list.add(sql); + if (!isDebug) + set.add(sql); } else if (type.equalsIgnoreCase("Integer")) { sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "INTEGER"); - if (!isDebug) list.add(sql); + if (!isDebug) + set.add(sql); + } else if (type.equalsIgnoreCase("boolean")){ + sql = String.format(sql_insert, tableName, element.getSimpleName().toString(), "INTEGER"); + if (!isDebug) + set.add(sql); + } + if (isDebug) { + testBw.write(version + " "); + testBw.write(sql); + testBw.newLine(); + testBw.flush(); + } else { + map.put(version, set); } - testBw.write(version + " "); - testBw.write(sql); - testBw.newLine(); - testBw.flush(); } } catch (Exception e) { e.printStackTrace(); @@ -74,18 +88,27 @@ public class VersionProcessor extends AbstractProcessor { e.printStackTrace(); } } - + File tmp = new File("tmp"); +// File migrations = new File(tmp.getParentFile(), "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator)); +// if (migrations.exists() || migrations.mkdirs()) { +// File[] files = migrations.listFiles(); +// if (files != null) +// for (File f : files) { +// if (!f.getName().equals("1.sql") && f.getName().endsWith(".sql")) f.delete(); +// } +// } for (Integer version : map.keySet()) { try { - File tmp = new File("tmp"); String path = "app\\src\\main\\assets\\migrations\\".replace("\\", File.separator) + version + ".sql"; File file = new File(tmp.getParentFile(), path); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + StringBuilder builder = new StringBuilder(); for (String str : map.get(version)) { - bw.write(str); - bw.newLine(); - bw.flush(); + builder.append(str); + builder.append("\r\n"); } + bw.write(builder.toString()); + bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); @@ -93,4 +116,4 @@ public class VersionProcessor extends AbstractProcessor { } return true; } -} +} \ No newline at end of file diff --git a/sql.txt b/sql.txt index 89e0026..5354dfd 100644 --- a/sql.txt +++ b/sql.txt @@ -1 +1 @@ -3 ALTER table test INSERT ADD COLUMN age INTEGER +3 ALTER table test ADD COLUMN area INTEGER -- libgit2 0.21.0