
br.com.objectos.way.sql.compiler.QueryParameter Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.sql.compiler;
import java.util.List;
import java.util.Set;
import br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.AnnotationInfoMap;
import br.com.objectos.way.code.AnnotationValueInfo;
import br.com.objectos.way.code.ImportInfo;
import br.com.objectos.way.code.ImportInfoSet;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.ParameterInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.code.WayCode;
import br.com.objectos.way.core.tmpl.mustache.IsMustacheSerializable;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import br.com.objectos.way.core.util.WayIterables;
import br.com.objectos.way.sql.Column;
import br.com.objectos.way.sql.ComparisonOperator;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
/**
* @author [email protected] (Marcio Endo)
*/
class QueryParameter implements IsMustacheSerializable {
private final ParameterInfo parameterInfo;
private final int index;
private final int size;
private final Optional columnAnnotation;
private QueryParameter(ParameterInfo parameterInfo, int index, int size) {
this.parameterInfo = parameterInfo;
this.index = index;
this.size = size;
AnnotationInfoMap annotationInfoMap = parameterInfo.getAnnotationInfoMap();
columnAnnotation = annotationInfoMap.getFirstAnnotatedWith(Column.class);
}
public static Optional parameterOf(MethodInfo methodInfo) {
QueryParameter param = null;
List parameterList = parameterListOf(methodInfo);
if (parameterList.size() == 1) {
param = parameterList.get(0);
}
return Optional.fromNullable(param);
}
public static List parameterListOf(MethodInfo methodInfo) {
List parameterInfoList = methodInfo.getParameterInfoList();
int size = parameterInfoList.size();
return WayIterables.from(parameterInfoList)
.transform(new ToQueryParameter(size))
.filter(new Valid())
.toImmutableList();
}
public String getCacheKeyType() {
SimpleTypeInfo simpleTypeInfo = parameterInfo.getSimpleTypeInfo();
return simpleTypeInfo.getSimpleName();
}
public String getCacheKeyVar() {
return parameterInfo.getName();
}
public boolean isValid() {
return isNotTrx() && hasColumnAnnotation();
}
@Override
public MustacheObject toMustache() {
boolean first = index == 0;
boolean last = index + 1 == size;
boolean middle = !first && !last;
return parameterInfo.toMustacheHelper()
.add("first", first)
.add("last", last)
.add("middle", middle)
.add("binder", binder())
.add("column", columnAnnotation.get())
.add("operator", operator())
.add("parameter", parameter())
.toMustache();
}
public void addTo(ImportInfoSet importInfoSet) {
Set importInfo = parameterInfo.toImportInfo();
importInfoSet.addAll(importInfo);
}
private String binder() {
SimpleTypeInfo returnTypeInfo = parameterInfo.getSimpleTypeInfo();
SimpleTypeInfo autobox = returnTypeInfo.autobox();
return WayCode.lowerCaseFirstChar(autobox.getSimpleName()) + "(" + parameterInfo.getName() + ")";
}
private String operator() {
AnnotationInfo annotationInfo = columnAnnotation.get();
AnnotationValueInfo annotationValueInfo = annotationInfo.getValue("comparison").get();
ComparisonOperator operator = annotationValueInfo.getEnumValue(ComparisonOperator.class);
return operator.name().toLowerCase();
}
private boolean hasColumnAnnotation() {
return columnAnnotation.isPresent();
}
private boolean isNotTrx() {
return index != 0;
}
private String parameter() {
SimpleTypeInfo returnTypeInfo = parameterInfo.getSimpleTypeInfo();
return SqlParameter.mustacheString(returnTypeInfo);
}
private static class ToQueryParameter implements Function {
private final int size;
private int index;
public ToQueryParameter(int size) {
this.size = size;
}
@Override
public QueryParameter apply(ParameterInfo input) {
return new QueryParameter(input, index++, size);
}
}
private static class Valid implements Predicate {
@Override
public boolean apply(QueryParameter input) {
return input.isValid();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy