Skip to content

Commit

Permalink
refactor: update to switch case
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed May 2, 2023
1 parent 0c8df38 commit cd678d5
Show file tree
Hide file tree
Showing 22 changed files with 301 additions and 349 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,16 @@


/**
* The result of {@link ColumnDeleteQueryParams} that has {@link ColumnDeleteQuery} and {@link Params}.
* The result of {@link ColumnDeleteQueryParams} that has {@link ColumnDeleteQuery} and {@link Params}.
*/
public final class ColumnDeleteQueryParams {

private final ColumnDeleteQuery query;

private final Params params;

public ColumnDeleteQueryParams(ColumnDeleteQuery query, Params params) {
this.query = query;
this.params = params;
}
public record ColumnDeleteQueryParams(ColumnDeleteQuery query, Params params) {

/**
* The {@link ColumnDeleteQuery}
*
* @return a {@link ColumnDeleteQuery} instance
*/
@Override
public ColumnDeleteQuery query() {
return query;
}
Expand All @@ -50,25 +42,10 @@ public ColumnDeleteQuery query() {
*
* @return a {@link Params} instance
*/
@Override
public Params params() {
return params;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ColumnDeleteQueryParams that = (ColumnDeleteQueryParams) o;
return Objects.equals(query, that.query) &&
Objects.equals(params, that.params);
}

@Override
public int hashCode() {
return Objects.hash(query, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,24 @@ public Stream<ColumnEntity> result() {
throw new QueryException("Check all the parameters before execute the query, params left: " + paramsLeft);
}
switch (type) {
case SELECT:
case SELECT -> {
return manager.select(columnQuery);
case DELETE:
}
case DELETE -> {
manager.delete(columnDeleteQuery);
return Stream.empty();
case UPDATE:
}
case UPDATE -> {
return Stream.of(manager.update(entity));
case INSERT:
}
case INSERT -> {
if (Objects.isNull(duration)) {
return Stream.of(manager.insert(entity));
} else {
return Stream.of(manager.insert(entity, duration));
}
default:
throw new UnsupportedOperationException("there is not support to operation type: " + type);

}
default -> throw new UnsupportedOperationException("there is not support to operation type: " + type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,14 @@ public final class ColumnQueryParser {
public Stream<ColumnEntity> query(String query, ColumnManager manager, ColumnObserverParser observer) {
validation(query, manager, observer);
String command = query.substring(0, 6);
switch (command) {
case "select":
return select.query(query, manager, observer);
case "delete":
return delete.query(query, manager, observer);
case "insert":
return insert.query(query, manager, observer);
case "update":
return update.query(query, manager, observer);
default:
throw new QueryException(String.format("The command was not recognized at the query %s ", query));
}
return switch (command) {
case "select" -> select.query(query, manager, observer);
case "delete" -> delete.query(query, manager, observer);
case "insert" -> insert.query(query, manager, observer);
case "update" -> update.query(query, manager, observer);
default ->
throw new QueryException(String.format("The command was not recognized at the query %s ", query));
};
}

/**
Expand All @@ -76,18 +72,14 @@ public ColumnPreparedStatement prepare(String query, ColumnManager manager, Colu
validation(query, manager, observer);
String command = query.substring(0, 6);

switch (command) {
case "select":
return select.prepare(query, manager, observer);
case "delete":
return delete.prepare(query, manager, observer);
case "insert":
return insert.prepare(query, manager, observer);
case "update":
return update.prepare(query, manager, observer);
default:
throw new QueryException(String.format("The command was not recognized at the query %s ", query));
}
return switch (command) {
case "select" -> select.prepare(query, manager, observer);
case "delete" -> delete.prepare(query, manager, observer);
case "insert" -> insert.prepare(query, manager, observer);
case "update" -> update.prepare(query, manager, observer);
default ->
throw new QueryException(String.format("The command was not recognized at the query %s ", query));
};
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,38 @@ static ColumnCondition getCondition(Where where, Params params, ColumnObserverPa
}

static ColumnCondition getCondition(QueryCondition condition, Params parameters, ColumnObserverParser observer, String entity) {
switch (condition.condition()) {
case EQUALS:
return ColumnCondition.eq(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case GREATER_THAN:
return ColumnCondition.gt(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case GREATER_EQUALS_THAN:
return ColumnCondition.gte(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LESSER_THAN:
return ColumnCondition.lt(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LESSER_EQUALS_THAN:
return ColumnCondition.lte(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case IN:
return ColumnCondition.in(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LIKE:
return ColumnCondition.like(Column.of(getName(condition, observer, entity),
Values.get(condition.value(),
parameters)));
case BETWEEN:
return ColumnCondition.between(Column.of(getName(condition, observer, entity),
Values.get(condition.value(),
parameters)));
case NOT:
return getCondition(ConditionQueryValue.class.cast(condition.value()).get().get(0),
parameters, observer,
entity).negate();
case OR:
return ColumnCondition.or(ConditionQueryValue.class.cast(condition.value())
.get()
.stream().map(v -> getCondition(v, parameters, observer, entity))
.toArray(ColumnCondition[]::new));
case AND:
return ColumnCondition.and(ConditionQueryValue.class.cast(condition.value())
.get()
.stream().map(v -> getCondition(v, parameters, observer, entity))
.toArray(ColumnCondition[]::new));
default:
throw new QueryException("There is not support the type: " + condition.condition());


}
return switch (condition.condition()) {
case EQUALS -> ColumnCondition.eq(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case GREATER_THAN -> ColumnCondition.gt(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case GREATER_EQUALS_THAN -> ColumnCondition.gte(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LESSER_THAN -> ColumnCondition.lt(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LESSER_EQUALS_THAN -> ColumnCondition.lte(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case IN -> ColumnCondition.in(Column.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LIKE -> ColumnCondition.like(Column.of(getName(condition, observer, entity),
Values.get(condition.value(),
parameters)));
case BETWEEN -> ColumnCondition.between(Column.of(getName(condition, observer, entity),
Values.get(condition.value(),
parameters)));
case NOT -> getCondition(ConditionQueryValue.class.cast(condition.value()).get().get(0),
parameters, observer,
entity).negate();
case OR -> ColumnCondition.or(ConditionQueryValue.class.cast(condition.value())
.get()
.stream().map(v -> getCondition(v, parameters, observer, entity))
.toArray(ColumnCondition[]::new));
case AND -> ColumnCondition.and(ConditionQueryValue.class.cast(condition.value())
.get()
.stream().map(v -> getCondition(v, parameters, observer, entity))
.toArray(ColumnCondition[]::new));
default -> throw new QueryException("There is not support the type: " + condition.condition());
};
}

private static String getName(QueryCondition condition, ColumnObserverParser observer, String entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ static Object get(QueryValue<?> value, Params parameters) {

ValueType type = value.type();
switch (type) {
case NUMBER:
case STRING:
case BOOLEAN:
case NUMBER, STRING, BOOLEAN -> {
return value.get();
case PARAMETER:
}
case PARAMETER -> {
return parameters.add(ParamQueryValue.class.cast(value).get());
case ARRAY:
}
case ARRAY -> {
return Stream.of(ArrayQueryValue.class.cast(value).get())
.map(v -> get(v, parameters))
.collect(toList());
case FUNCTION:
}
case FUNCTION -> {
Function function = FunctionQueryValue.class.cast(value).get();
String name = function.name();
Object[] params = function.params();
Expand All @@ -62,12 +63,11 @@ static Object get(QueryValue<?> value, Params parameters) {
String message = String.format("There is not support to the function: %s with parameters %s", name,
Arrays.toString(params));
throw new QueryException(message);
case JSON:
}
case JSON -> {
return JsonObjects.getColumns(JSONQueryValue.class.cast(value).get());
case CONDITION:
default:
throw new QueryException("There is not support to the value: " + type);

}
default -> throw new QueryException("There is not support to the value: " + type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,36 @@ static DocumentCondition getCondition(Where where, Params params, DocumentObserv
}

static DocumentCondition getCondition(QueryCondition condition, Params parameters, DocumentObserverParser observer, String entity) {
switch (condition.condition()) {
case EQUALS:
return eq(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case GREATER_THAN:
return gt(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case GREATER_EQUALS_THAN:
return gte(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LESSER_THAN:
return lt(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LESSER_EQUALS_THAN:
return lte(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case IN:
return in(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LIKE:
return like(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case BETWEEN:
return between(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case NOT:
return getCondition(ConditionQueryValue.class.cast(condition.value()).get().get(0),
parameters, observer,
entity).negate();
case OR:
return or(ConditionQueryValue.class.cast(condition.value())
.get()
.stream().map(v -> getCondition(v, parameters, observer, entity))
.toArray(DocumentCondition[]::new));
case AND:
return and(ConditionQueryValue.class.cast(condition.value())
.get()
.stream().map(v -> getCondition(v, parameters, observer, entity))
.toArray(DocumentCondition[]::new));
default:
throw new QueryException("There is not support the type: " + condition.condition());


}
return switch (condition.condition()) {
case EQUALS -> eq(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case GREATER_THAN -> gt(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case GREATER_EQUALS_THAN -> gte(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LESSER_THAN -> lt(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LESSER_EQUALS_THAN -> lte(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case IN -> in(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case LIKE -> like(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case BETWEEN -> between(Document.of(getName(condition, observer, entity),
Values.get(condition.value(), parameters)));
case NOT -> getCondition(ConditionQueryValue.class.cast(condition.value()).get().get(0),
parameters, observer,
entity).negate();
case OR -> or(ConditionQueryValue.class.cast(condition.value())
.get()
.stream().map(v -> getCondition(v, parameters, observer, entity))
.toArray(DocumentCondition[]::new));
case AND -> and(ConditionQueryValue.class.cast(condition.value())
.get()
.stream().map(v -> getCondition(v, parameters, observer, entity))
.toArray(DocumentCondition[]::new));
default -> throw new QueryException("There is not support the type: " + condition.condition());
};
}

private static String getName(QueryCondition condition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,24 @@ public Stream<DocumentEntity> result() {
throw new QueryException("Check all the parameters before execute the query, params left: " + paramsLeft);
}
switch (type) {
case SELECT:
case SELECT -> {
return manager.select(documentQuery);
case DELETE:
}
case DELETE -> {
manager.delete(documentDeleteQuery);
return Stream.empty();
case UPDATE:
}
case UPDATE -> {
return Stream.of(manager.update(entity));
case INSERT:
}
case INSERT -> {
if (Objects.isNull(duration)) {
return Stream.of(manager.insert(entity));
} else {
return Stream.of(manager.insert(entity, duration));
}
default:
throw new UnsupportedOperationException("there is not support to operation type: " + type);

}
default -> throw new UnsupportedOperationException("there is not support to operation type: " + type);
}
}

Expand Down
Loading

0 comments on commit cd678d5

Please sign in to comment.