Skip to content

Commit

Permalink
V0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
darmi_zhang committed Dec 14, 2022
1 parent ab63215 commit 85969e3
Show file tree
Hide file tree
Showing 14 changed files with 643 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>mongo-lambda-query</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>

</project>
11 changes: 11 additions & 0 deletions src/main/java/com/darmi/plugin/EnableMongoLambdaQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.darmi.plugin;

import com.darmi.plugin.spring.MongoLambdaQueryConfig;
import org.springframework.context.annotation.Import;

/**
* @author darmi
*/
@Import(value = {MongoLambdaQueryConfig.class})
public @interface EnableMongoLambdaQuery {
}
126 changes: 126 additions & 0 deletions src/main/java/com/darmi/plugin/core/AbstractQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.darmi.plugin.core;


import org.springframework.util.ObjectUtils;

import java.util.List;

import static com.darmi.plugin.core.SqlKeyword.*;


/**
* @author darmi
*/
public abstract class AbstractQuery<T, R, Children extends AbstractQuery<T, R, Children>>
implements Func<Children, R>, Query<T> {

protected final Children typedThis = (Children) this;

protected Class<T> entityClass;

public void setEntityClass(Class<T> entityClass) {
this.entityClass = entityClass;
}

@Override
public Children is(R column, Object val) {
return addCondition(!ObjectUtils.isEmpty(val), column, IS, val);
}

@Override
public Children like(R column, Object val) {
return addCondition(!ObjectUtils.isEmpty(val), column, LIKE, val);
}

@Override
public Children ne(R column, Object val) {
return addCondition(!ObjectUtils.isEmpty(val), column, NE, val);
}

@Override
public Children orFront() {
return addCondition(true, null, OR_FRONT, null);
}


@Override
public Children andBack() {
return addCondition(true, null, AND_BACK, null);
}

@Override
public Children in(String column, List<?> values) {
if (ObjectUtils.isEmpty(values)) return typedThis;
columnToSqlSegment(column, values, IN);
return typedThis;
}

@Override
public Children lt(R column, Object val) {
return addCondition(!ObjectUtils.isEmpty(val), column, LT, val);
}

@Override
public Children le(R column, Object val) {
return addCondition(!ObjectUtils.isEmpty(val), column, LE, val);
}

@Override
public Children gt(R column, Object val) {
return addCondition(!ObjectUtils.isEmpty(val), column, GT, val);
}

@Override
public Children in(R column, List<?> values) {
return addCondition(!ObjectUtils.isEmpty(values), column, IN, values);
}

@Override
public Children reg(R column, Object val) {
return addCondition(!ObjectUtils.isEmpty(val), column, REG, val);
}

@Override
public Children reg(String column, Object val) {
if (ObjectUtils.isEmpty(val)) return typedThis;
columnToSqlSegment(column, val, REG);
return typedThis;
}

@Override
public Children is(String column, Object val) {
if (ObjectUtils.isEmpty(val)) return typedThis;
columnToSqlSegment(column, val, IS);
return typedThis;
}

@Override
public Children elemMatch(R column, Object val, Object... key) {
return addCondition(!ObjectUtils.isEmpty(val), column, ELEMMATCH, val, key);
}

protected Children addCondition(
boolean condition, R column, SqlKeyword keyWord, Object val, Object... key) {
return maybeDo(condition, () -> columnToSqlSegment(column, val, keyWord, key));
}

protected abstract void columnToSqlSegment(
R column, Object val, SqlKeyword keyWord, Object... key);

protected abstract void columnToSqlSegment(
String column, Object val, SqlKeyword keyWord, Object... key);

protected final Children maybeDo(boolean condition, DoSomething something) {
if (condition) {
something.doIt();
}
return typedThis;
}

protected abstract String columnToString(SFunction<T, ?> column) throws Exception;

@FunctionalInterface
public interface DoSomething {
void doIt();
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/darmi/plugin/core/Func.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.darmi.plugin.core;


import org.springframework.data.domain.Sort;

import java.io.Serializable;
import java.util.List;

/**
* @author darmi
*/
public interface Func<Children, R> extends Serializable {
Children andBack();

Children orFront();

Children like(R column, Object val);

Children is(String column, Object val);

Children is(R column, Object val);

Children lt(R column, Object val);

Children le(R column, Object val);

Children gt(R column, Object val);

Children ne(R column, Object val);

Children in(R column, List<?> values);

Children in(String column, List<?> values);

Children sort(R column, Sort.Direction direction);

Children elemMatch(R column,Object val, Object... key) ;

Children reg(R column, Object val);

Children reg(String column, Object val);
}
63 changes: 63 additions & 0 deletions src/main/java/com/darmi/plugin/core/LambdaUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.darmi.plugin.core;


import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author darmi
*/
public class LambdaUtils {

public static <T> String getField(SFunction<T, ?> fn) {
if (fn == null) return null;
return convertToFieldName(fn);
}

/**
* get filed name
*/
private static <T> String convertToFieldName(SFunction<T, ?> fn) {
SerializedLambda lambda = null;
try {
lambda = getSerializedLambda(fn);
} catch (Exception e) {
e.printStackTrace();
}
String methodName = lambda.getImplMethodName();
String prefix = null;
if (methodName.startsWith("get")) {
prefix = "get";
} else if (methodName.startsWith("is")) {
prefix = "is";
}
if (prefix == null) {
throw new RuntimeException("LambdaUtils ERROR");
}

return toLowerCaseFirstOne(methodName.replace(prefix, ""));
}

/**
* get Serialized Lambda
*/
private static SerializedLambda getSerializedLambda(Serializable fn) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = fn.getClass().getDeclaredMethod("writeReplace");
method.setAccessible(Boolean.TRUE);
return (SerializedLambda) method.invoke(fn);
}

/**
* toLowerCaseFirstOne
*/
private static String toLowerCaseFirstOne(String field) {
if (Character.isLowerCase(field.charAt(0))) return field;
else {
char firstOne = Character.toLowerCase(field.charAt(0));
String other = field.substring(1);
return firstOne + other;
}
}
}
Loading

0 comments on commit 85969e3

Please sign in to comment.