Skip to content

Commit

Permalink
added insertIgnore
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodong authored and xiaodong committed Oct 30, 2017
1 parent 9c7f298 commit df91bcc
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 38 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
>
> }
<br />
Expand All @@ -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);

Expand Down Expand Up @@ -85,4 +85,9 @@ Gradle:
//
.assertEquals(Assertions::fail, s);

s = "insert ignore User values ('abc')";
insertIgnore(User.class).values("abc")
//
.assertEquals(Assertions::fail, s);

```
6 changes: 4 additions & 2 deletions lambda-parser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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);
Expand All @@ -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()));
Expand Down Expand Up @@ -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("星号", "*");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> list = new ArrayList<>();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class Where extends Execute {

Class c;
//Class c;
String beforeWhere;
DML dml;

Expand Down Expand Up @@ -48,8 +48,9 @@ protected Where(DML dml, Class c, String tableName, List<Map<Function, ?>> 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;
}
Expand All @@ -58,7 +59,7 @@ protected Where(DML dml, Class c, String tableName, Function[] fs) {
if (fs != null && fs.length > 0) {
List<String> 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", "");
}
Expand Down
30 changes: 26 additions & 4 deletions lambda-parser/src/main/java/com/openle/source/expression/sql.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,17 @@ public static <T> From select(final Function<T, ?>... getter) {
* @return SQL链式对象
*/
public static <T> Values insert(Class c) {
return new Values(c);
return new Values(c, false);
}

/**
* insert数据已存在时忽略不抛异常
* @param <T>
* @param c 实体Class User.class
* @return SQL链式对象
*/
public static <T> Values insertIgnore(Class c) {
return new Values(c, true);
}

/**
Expand All @@ -121,7 +131,19 @@ public static <T> Values insert(Class c) {
*/
@SafeVarargs
public static <T> Values insert(Class c, final Function<T, ?>... getter) {
return new Values(c, getter);
return new Values(c, getter, false);
}

/**
*
* @param <T>
* @param c 实体Class User.class
* @param getter
* @return SQL链式对象
*/
@SafeVarargs
public static <T> Values insertIgnore(Class c, final Function<T, ?>... getter) {
return new Values(c, getter, true);
}

/**
Expand All @@ -133,8 +155,8 @@ public static <T> Values insert(Class c, final Function<T, ?>... getter) {
public static <T> Set update(Class c) {
return new Set(c);
}
/**

/**
*
* @param <T>
* @param tableName 表名
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.openle.source.expression;

import javax.persistence.Table;

@Table(name = "User")
public class User {

private String name;
Expand Down

0 comments on commit df91bcc

Please sign in to comment.