From df91bcc06c236ddd7d3ca14b099ccc1acc3cde07 Mon Sep 17 00:00:00 2001 From: xiaodong Date: Mon, 30 Oct 2017 17:37:15 +0800 Subject: [PATCH] added insertIgnore --- README.md | 11 +++++-- lambda-parser/build.gradle | 6 ++-- .../source/expression/LambdaHelper.java | 27 +++++++---------- .../com/openle/source/expression/Utils.java | 3 +- .../com/openle/source/expression/Values.java | 13 ++++---- .../com/openle/source/expression/Where.java | 7 +++-- .../com/openle/source/expression/sql.java | 30 ++++++++++++++++--- .../openle/source/expression/LambdaTest.java | 9 ++++-- .../com/openle/source/expression/User.java | 3 ++ 9 files changed, 71 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index a58bbfa..bbf3462 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Gradle: > > dependencies { > -> > compile 'com.openle.source.expression:lambda-parser:1.0.6' +> > compile 'com.openle.source.expression:lambda-parser:1.0.7' > > }
@@ -38,8 +38,8 @@ Gradle: // .assertEquals(Assertions::fail, s); - s = "select max(id) from User"; - select(kf("max(id)")).from(User.class) + s = "select max(id),count(*) from User"; + select(kf("max(id)"), kf("count(*)")).from(User.class) // .assertEquals(Assertions::fail, s); @@ -85,4 +85,9 @@ Gradle: // .assertEquals(Assertions::fail, s); + s = "insert ignore User values ('abc')"; + insertIgnore(User.class).values("abc") + // + .assertEquals(Assertions::fail, s); + ``` diff --git a/lambda-parser/build.gradle b/lambda-parser/build.gradle index 4e7bb6f..1fcac13 100644 --- a/lambda-parser/build.gradle +++ b/lambda-parser/build.gradle @@ -7,7 +7,7 @@ apply plugin: 'com.jfrog.bintray' sourceCompatibility = 1.8 targetCompatibility = 1.8 -version '1.0.6' +version '1.0.7' group 'com.openle.source.expression' // 解决中文注释导致的Javadoc generation failed @@ -45,7 +45,9 @@ dependencies { testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.1") compile group: 'org.ow2.asm', name: 'asm-util', version: '6.0' - + + // for @javax.persistence.Table and @Embeddable + compile group: 'javax', name: 'javaee-web-api', version: '8.0' } junitPlatform { diff --git a/lambda-parser/src/main/java/com/openle/source/expression/LambdaHelper.java b/lambda-parser/src/main/java/com/openle/source/expression/LambdaHelper.java index 70237c9..34781fb 100644 --- a/lambda-parser/src/main/java/com/openle/source/expression/LambdaHelper.java +++ b/lambda-parser/src/main/java/com/openle/source/expression/LambdaHelper.java @@ -12,9 +12,6 @@ import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; -import net.bytebuddy.ByteBuddy; -import net.bytebuddy.description.modifier.Visibility; -import net.bytebuddy.implementation.FixedValue; /** * @@ -30,17 +27,15 @@ public class LambdaHelper { public static Function getFunctionByName(String methodName) { methodName = replaceSymbol(methodName); //System.setProperty("jdk.internal.lambda.dumpProxyClasses", "D:\\temp"); - Function f = null; - - // 方法引用的getter方法不存在时,临时创建一个类来模拟。 - Class c = new ByteBuddy() - .subclass(Object.class) - .defineMethod(methodName, String.class, Visibility.PUBLIC) - .intercept(FixedValue.value(methodName)) - .make() - .load(LambdaHelper.class.getClassLoader()) - .getLoaded(); +// // 方法引用的getter方法不存在时,临时创建一个类来模拟。 +// Class c = new ByteBuddy() +// .subclass(Object.class) +// .defineMethod(methodName, String.class, Visibility.PUBLIC) +// .intercept(FixedValue.value(methodName)) +// .make() +// .load(LambdaHelper.class.getClassLoader()) +// .getLoaded(); try { MethodHandles.Lookup caller = MethodHandles.lookup(); MethodType getter = MethodType.methodType(String.class); @@ -66,7 +61,7 @@ public static Function getFunctionByName(String methodName) { // m.setAccessible(false); // MethodHandle factory = site.getTarget(); - f = (Function) factory.invoke(); + Function f = (Function) factory.invoke(); //System.out.println(new Utils().getSelectName(c, f)); //System.out.println(f.apply(c.getConstructor().newInstance())); @@ -134,11 +129,11 @@ public static void createSupplier(String[] args) throws Throwable { // 后续通过Base32或Base36处理 public static String replaceSymbol(String s) { - return s.replace("(", "左括号").replace(")", "右括号"); + return s.replace("(", "左括号").replace(")", "右括号").replace("*", "星号"); } public static String restoreSymbol(String s) { - return s.replace("左括号", "(").replace("右括号", ")"); + return s.replace("左括号", "(").replace("右括号", ")").replace("星号", "*"); } } diff --git a/lambda-parser/src/main/java/com/openle/source/expression/Utils.java b/lambda-parser/src/main/java/com/openle/source/expression/Utils.java index 6464e45..7eb222d 100644 --- a/lambda-parser/src/main/java/com/openle/source/expression/Utils.java +++ b/lambda-parser/src/main/java/com/openle/source/expression/Utils.java @@ -84,8 +84,9 @@ public static String camelToUnderline(String param) { } public static String getTableName(Class c) { - //System.out.println("Entity " + c.getName()); + System.out.println("getTableName Entity " + c.getName() + "|" + c.getAnnotations().length); String tableName = c.getSimpleName(); + for (Annotation a : c.getAnnotations()) { //System.out.println(a.annotationType().getName()); if (a.annotationType().getName().equals("javax.persistence.Table")) { diff --git a/lambda-parser/src/main/java/com/openle/source/expression/Values.java b/lambda-parser/src/main/java/com/openle/source/expression/Values.java index 0c15927..cdeddca 100644 --- a/lambda-parser/src/main/java/com/openle/source/expression/Values.java +++ b/lambda-parser/src/main/java/com/openle/source/expression/Values.java @@ -7,14 +7,14 @@ public class Values { - Class c; + //Class c; Function[] fs; String sName = ""; - protected Values(Class c, Function[] fs) { - this.c = c; + protected Values(Class c, Function[] fs, boolean isIgnore) { + //this.c = c; this.fs = fs; - sName = "insert " + Utils.getTableName(c) + " "; + sName = "insert " + (isIgnore ? "ignore " : "") + Utils.getTableName(c) + " "; if (fs.length > 0) { List list = new ArrayList<>(); @@ -39,9 +39,8 @@ protected Values(Class c, Function[] fs) { } - protected Values(Class c) { - sName = "insert " + Utils.getTableName(c) + " "; - + protected Values(Class c, boolean isIgnore) { + sName = "insert " + (isIgnore ? "ignore " : "") + Utils.getTableName(c) + " "; } public Execute values(Object... objArray) { diff --git a/lambda-parser/src/main/java/com/openle/source/expression/Where.java b/lambda-parser/src/main/java/com/openle/source/expression/Where.java index 2dff1a5..aab189c 100644 --- a/lambda-parser/src/main/java/com/openle/source/expression/Where.java +++ b/lambda-parser/src/main/java/com/openle/source/expression/Where.java @@ -10,7 +10,7 @@ public class Where extends Execute { - Class c; + //Class c; String beforeWhere; DML dml; @@ -48,8 +48,9 @@ protected Where(DML dml, Class c, String tableName, List> setMa //for select delete protected Where(DML dml, Class c, String tableName, Function[] fs) { + //this.c = c; tableName = c != null ? Utils.getTableName(c) : tableName; - this.c = c; + if (dml.equals(DML.DELETE)) { beforeWhere = "delete from " + tableName; } @@ -58,7 +59,7 @@ protected Where(DML dml, Class c, String tableName, Function[] fs) { if (fs != null && fs.length > 0) { List list = new ArrayList<>(); for (Function f : fs) { - String name = new Utils().getSelectName(c, f); + String name = new Utils().getSelectName(c != null ? c : Object.class, f); if (name.startsWith("get")) { name = name.replaceFirst("get", ""); } diff --git a/lambda-parser/src/main/java/com/openle/source/expression/sql.java b/lambda-parser/src/main/java/com/openle/source/expression/sql.java index 92e5c02..d5b4ddc 100644 --- a/lambda-parser/src/main/java/com/openle/source/expression/sql.java +++ b/lambda-parser/src/main/java/com/openle/source/expression/sql.java @@ -109,7 +109,17 @@ public static From select(final Function... getter) { * @return SQL链式对象 */ public static Values insert(Class c) { - return new Values(c); + return new Values(c, false); + } + + /** + * insert数据已存在时忽略不抛异常 + * @param + * @param c 实体Class User.class + * @return SQL链式对象 + */ + public static Values insertIgnore(Class c) { + return new Values(c, true); } /** @@ -121,7 +131,19 @@ public static Values insert(Class c) { */ @SafeVarargs public static Values insert(Class c, final Function... getter) { - return new Values(c, getter); + return new Values(c, getter, false); + } + + /** + * + * @param + * @param c 实体Class User.class + * @param getter + * @return SQL链式对象 + */ + @SafeVarargs + public static Values insertIgnore(Class c, final Function... getter) { + return new Values(c, getter, true); } /** @@ -133,8 +155,8 @@ public static Values insert(Class c, final Function... getter) { public static Set update(Class c) { return new Set(c); } - - /** + + /** * * @param * @param tableName 表名 diff --git a/lambda-parser/src/test/java/com/openle/source/expression/LambdaTest.java b/lambda-parser/src/test/java/com/openle/source/expression/LambdaTest.java index 355c55c..9f33f83 100644 --- a/lambda-parser/src/test/java/com/openle/source/expression/LambdaTest.java +++ b/lambda-parser/src/test/java/com/openle/source/expression/LambdaTest.java @@ -39,8 +39,8 @@ public void testMain() { // .assertEquals(Assertions::fail, s); - s = "select max(id) from User"; - select(kf("max(id)")).from(User.class) + s = "select max(id),count(*) from User"; + select(kf("max(id)"), kf("count(*)")).from(User.class) // .assertEquals(Assertions::fail, s); @@ -86,6 +86,11 @@ public void testMain() { // .assertEquals(Assertions::fail, s); + s = "insert ignore User values ('abc')"; + insertIgnore(User.class).values("abc") + // + .assertEquals(Assertions::fail, s); + } @Disabled diff --git a/lambda-parser/src/test/java/com/openle/source/expression/User.java b/lambda-parser/src/test/java/com/openle/source/expression/User.java index 7046e4b..bdaf006 100644 --- a/lambda-parser/src/test/java/com/openle/source/expression/User.java +++ b/lambda-parser/src/test/java/com/openle/source/expression/User.java @@ -1,5 +1,8 @@ package com.openle.source.expression; +import javax.persistence.Table; + +@Table(name = "User") public class User { private String name;