All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jaregu.database.queries.compiling.AssignmentFeature Maven / Gradle / Ivy

Go to download

Java based SQL templating project. Store your queries in *.sql files and build queries for execution. Supports simple expressions and conditional clauses and interface proxying for java-sql bridge.

There is a newer version: 1.4.1
Show newest version
package com.jaregu.database.queries.compiling;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import com.jaregu.database.queries.building.ParametersResolver;
import com.jaregu.database.queries.compiling.expr.Expression;
import com.jaregu.database.queries.compiling.expr.Expression.ExpressionResult;
import com.jaregu.database.queries.compiling.expr.ExpressionParser;
import com.jaregu.database.queries.parsing.ParsedQueryPart;

/**
 * Assignment feature
 * 
 * Example SQL:
 * 
 * 
 * -- attr_name_1 = value_expression_1[; attr_name_2 = value_expression_2 ...]
 * 
*/ final class AssignmentFeature implements QueryCompilerFeature { private final ExpressionParser expressionParser; AssignmentFeature(ExpressionParser expressionParser) { this.expressionParser = expressionParser; } @Override public boolean isCompilable(Source source) { List parts = source.getParts(); ParsedQueryPart part; return parts.size() == 1 && (part = parts.get(0)).isComment() && expressionParser.isLikeExpression(part.getCommentContent()); } @Override public Result compile(Source source, Compiler compiler) { List expressions = expressionParser.parse(source.getParts().get(0).getCommentContent()); return new Result() { @Override public List getParts() { return Collections.singletonList(new AssignmentVariablePart(expressions)); } }; } private static final class AssignmentVariablePart implements PreparedQueryPart { private List expressions; private AssignmentVariablePart(List expressions) { this.expressions = expressions; } @Override public Result build(ParametersResolver resolver) { Map attributes = new LinkedHashMap<>(); for (Expression expression : expressions) { ExpressionResult expressionResult = expression.eval(resolver); attributes.putAll(expressionResult.getOutputVariables()); } return new PreparedQueryPartResultImpl(Optional.empty(), Collections.emptyList(), attributes); } @Override public String toString() { return "AssignmentVariablePart" + expressions; } @Override public boolean equals(Object obj) { if (obj == this) return true; return false; } @Override public int hashCode() { return Objects.hash(expressions); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy