
br.com.objectos.way.sql.compiler.Pk 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 static com.google.common.collect.Lists.transform;
import java.util.List;
import br.com.objectos.way.code.ClassInfo;
import br.com.objectos.way.code.ImportInfoSet;
import br.com.objectos.way.core.tmpl.mustache.IsMustacheSerializable;
import br.com.objectos.way.core.tmpl.mustache.MustacheObject;
import br.com.objectos.way.core.tmpl.mustache.Mustaches;
import br.com.objectos.way.core.util.WayIterables;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
/**
* @author [email protected] (Marcio Endo)
*/
class Pk implements IsMustacheSerializable {
private final List columnList;
private Pk(List columnList) {
this.columnList = columnList;
}
public static Pk of(ClassInfo classInfo) {
List fullList = SqlColumn.columnListOf(classInfo);
List columnList = WayIterables.from(fullList)
.filter(SqlColumnIsPrimaryKey.get())
.toImmutableList();
return new Pk(columnList);
}
public boolean shouldGenerate() {
return columnList.size() == 1;
}
public ImportInfoSet toImportInfoSet() {
ImportInfoSet importInfoSet = ImportInfoSet.setOf();
for (SqlColumn column : columnList) {
column.addTo(importInfoSet);
}
return importInfoSet;
}
@Override
public MustacheObject toMustache() {
List fields = fields();
return Mustaches.toMustacheHelper()
.add("fields", fields)
.add("keyName", keyName())
.add("parameters", parameters(fields))
.add("parameterNames", parameterNames(fields))
.toMustache();
}
private List fields() {
return ImmutableList. builder()
.add(new PkField("Transaction", "trx"))
.addAll(transform(columnList, new ToField()))
.build();
}
private String keyName() {
String keyName = null;
for (SqlColumn sqlColumn : columnList) {
keyName = sqlColumn.getKeyName();
break;
}
return keyName;
}
private String parameters(List fields) {
List parts = transform(fields, new ToParameter());
return Joiner.on(", ").join(parts);
}
private String parameterNames(List fields) {
List parts = transform(fields, new ToParameterName());
return Joiner.on(", ").join(parts);
}
private class ToField implements Function {
@Override
public PkField apply(SqlColumn input) {
return input.toPkField();
}
}
private class ToParameter implements Function {
@Override
public String apply(PkField input) {
return input.declaration();
}
}
private class ToParameterName implements Function {
@Override
public String apply(PkField input) {
return input.name();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy