Skip to content

Commit

Permalink
feat: support query builder (openGemini#121)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <[email protected]>
  • Loading branch information
ZhangJian He authored Sep 21, 2024
1 parent c57bef7 commit c05cf1d
Show file tree
Hide file tree
Showing 18 changed files with 610 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ci/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@

<!-- we don't need package-info on tests -->
<suppress checks="JavadocPackage" files=".*[\\/]src[\\/]test[\\/].*"/>

<!-- Suppress LineLength rule for QueryBuilderTest.java -->
<suppress checks="LineLength" files=".*QueryBuilderTest\.java"/>
</suppressions>
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() + ")";
}
}
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;
}
}
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 + "\"";
}
}
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();
}
}
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;
}
}
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) + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.opengemini.client.api;

public interface Condition {
String build();
}
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.opengemini.client.api;

public interface Expression {
String build();
}
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 + "\"";
}
}
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,
}
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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.opengemini.client.api;

public enum LogicalOperator {
AND,
OR,
}
Loading

0 comments on commit c05cf1d

Please sign in to comment.