forked from openGemini/opengemini-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support query builder (openGemini#121)
Signed-off-by: ZhangJian He <[email protected]>
- Loading branch information
ZhangJian He
authored
Sep 21, 2024
1 parent
c57bef7
commit c05cf1d
Showing
18 changed files
with
610 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
opengemini-client-api/src/main/java/io/opengemini/client/api/ArithmeticExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.opengemini.client.api; | ||
|
||
/** | ||
* ArithmeticExpression represents an arithmetic operation between two expressions | ||
*/ | ||
public class ArithmeticExpression implements Expression { | ||
private final Expression left; | ||
|
||
private final Expression right; | ||
|
||
private final ArithmeticOperator operator; | ||
|
||
public ArithmeticExpression(Expression left, Expression right, ArithmeticOperator operator) { | ||
this.left = left; | ||
this.right = right; | ||
this.operator = operator; | ||
} | ||
|
||
public Expression left() { | ||
return left; | ||
} | ||
|
||
public Expression right() { | ||
return right; | ||
} | ||
|
||
public ArithmeticOperator operator() { | ||
return operator; | ||
} | ||
|
||
@Override | ||
public String build() { | ||
return "(" + left.build() + " " + operator.symbol() + " " + right.build() + ")"; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
opengemini-client-api/src/main/java/io/opengemini/client/api/ArithmeticOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.opengemini.client.api; | ||
|
||
public enum ArithmeticOperator { | ||
ADD("+"), | ||
SUBTRACT("-"), | ||
MULTIPLY("*"), | ||
DIVIDE("/"), | ||
MODULO("%"); | ||
|
||
private final String symbol; | ||
|
||
ArithmeticOperator(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
|
||
public String symbol() { | ||
return symbol; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
opengemini-client-api/src/main/java/io/opengemini/client/api/AsExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.opengemini.client.api; | ||
|
||
/** | ||
* AsExpression represents an alias for an expression (e.g., SELECT field AS alias) | ||
*/ | ||
public class AsExpression implements Expression { | ||
private final String alias; | ||
|
||
private final Expression expression; | ||
|
||
public AsExpression(String alias, Expression expression) { | ||
this.alias = alias; | ||
this.expression = expression; | ||
} | ||
|
||
public String alias() { | ||
return alias; | ||
} | ||
|
||
public Expression expression() { | ||
return expression; | ||
} | ||
|
||
@Override | ||
public String build() { | ||
return expression.build() + " AS \"" + alias + "\""; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
opengemini-client-api/src/main/java/io/opengemini/client/api/ComparisonCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.opengemini.client.api; | ||
|
||
public class ComparisonCondition implements Condition { | ||
private final String column; | ||
|
||
private final ComparisonOperator operator; | ||
|
||
private final Object value; | ||
|
||
public ComparisonCondition(String column, ComparisonOperator operator, Object value) { | ||
this.column = column; | ||
this.operator = operator; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String build() { | ||
StringBuilder conditionBuilder = new StringBuilder(); | ||
conditionBuilder.append('"'); | ||
conditionBuilder.append(column); | ||
conditionBuilder.append('"'); | ||
conditionBuilder.append(" "); | ||
conditionBuilder.append(operator.symbol()); | ||
conditionBuilder.append(" "); | ||
if (value instanceof String) { | ||
conditionBuilder.append("'").append(value).append("'"); | ||
} else { | ||
conditionBuilder.append(value); | ||
} | ||
|
||
return conditionBuilder.toString(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
opengemini-client-api/src/main/java/io/opengemini/client/api/ComparisonOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.opengemini.client.api; | ||
|
||
public enum ComparisonOperator { | ||
EQUALS("="), | ||
NOT_EQUALS("<>"), | ||
GREATER_THAN(">"), | ||
LESS_THAN("<"), | ||
GREATER_THAN_OR_EQUALS(">="), | ||
LESS_THAN_OR_EQUALS("<="); | ||
|
||
private final String symbol; | ||
|
||
ComparisonOperator(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
|
||
public String symbol() { | ||
return symbol; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
opengemini-client-api/src/main/java/io/opengemini/client/api/CompositeCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.opengemini.client.api; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CompositeCondition implements Condition { | ||
private final LogicalOperator logicalOperator; | ||
|
||
private final Condition[] conditions; | ||
|
||
public CompositeCondition(LogicalOperator logicalOperator, Condition... conditions) { | ||
this.logicalOperator = logicalOperator; | ||
this.conditions = conditions; | ||
} | ||
|
||
@Override | ||
public String build() { | ||
List<String> parts = new ArrayList<>(); | ||
for (Condition condition : conditions) { | ||
parts.add(condition.build()); | ||
} | ||
return "(" + String.join(" " + logicalOperator.name() + " ", parts) + ")"; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
opengemini-client-api/src/main/java/io/opengemini/client/api/Condition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.opengemini.client.api; | ||
|
||
public interface Condition { | ||
String build(); | ||
} |
21 changes: 21 additions & 0 deletions
21
opengemini-client-api/src/main/java/io/opengemini/client/api/ConstantExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.opengemini.client.api; | ||
|
||
/** | ||
* ConstantExpression represents a constant value in the query | ||
*/ | ||
public class ConstantExpression implements Expression { | ||
private final Object value; | ||
|
||
public ConstantExpression(Object value) { | ||
this.value = value; | ||
} | ||
|
||
public Object value() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public String build() { | ||
return value.toString(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
opengemini-client-api/src/main/java/io/opengemini/client/api/Expression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.opengemini.client.api; | ||
|
||
public interface Expression { | ||
String build(); | ||
} |
21 changes: 21 additions & 0 deletions
21
opengemini-client-api/src/main/java/io/opengemini/client/api/FieldExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.opengemini.client.api; | ||
|
||
/** | ||
* FieldExpression represents a column or field in the query | ||
*/ | ||
public class FieldExpression implements Expression { | ||
private final String field; | ||
|
||
public FieldExpression(String field) { | ||
this.field = field; | ||
} | ||
|
||
public String field() { | ||
return field; | ||
} | ||
|
||
@Override | ||
public String build() { | ||
return "\"" + field + "\""; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
opengemini-client-api/src/main/java/io/opengemini/client/api/FunctionEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.opengemini.client.api; | ||
|
||
public enum FunctionEnum { | ||
MEAN, | ||
COUNT, | ||
SUM, | ||
MIN, | ||
MAX, | ||
TIME, | ||
TOP, | ||
LAST, | ||
} |
39 changes: 39 additions & 0 deletions
39
opengemini-client-api/src/main/java/io/opengemini/client/api/FunctionExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.opengemini.client.api; | ||
|
||
/** | ||
* FunctionExpression represents a function call with arguments (e.g., SUM, COUNT) | ||
*/ | ||
public class FunctionExpression implements Expression { | ||
private final FunctionEnum function; | ||
|
||
private final Expression[] arguments; | ||
|
||
public FunctionExpression(FunctionEnum function, Expression[] arguments) { | ||
this.function = function; | ||
this.arguments = arguments; | ||
} | ||
|
||
public FunctionEnum function() { | ||
return function; | ||
} | ||
|
||
public Expression[] arguments() { | ||
return arguments; | ||
} | ||
|
||
@Override | ||
public String build() { | ||
StringBuilder builder = new StringBuilder(); | ||
builder.append(function.name()).append("("); | ||
|
||
for (int i = 0; i < arguments.length; i++) { | ||
if (i > 0) { | ||
builder.append(", "); | ||
} | ||
builder.append(arguments[i].build()); | ||
} | ||
|
||
builder.append(")"); | ||
return builder.toString(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
opengemini-client-api/src/main/java/io/opengemini/client/api/LogicalOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.opengemini.client.api; | ||
|
||
public enum LogicalOperator { | ||
AND, | ||
OR, | ||
} |
Oops, something went wrong.