use of org.mybatis.generator.api.dom.java.Parameter in project generator by mybatis.
the class ProviderUpdateByPrimaryKeySelectiveMethodGenerator method addClassElements.
@Override
public void addClassElements(TopLevelClass topLevelClass) {
Set<String> staticImports = new TreeSet<String>();
Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
if (useLegacyBuilder) {
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.UPDATE");
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SET");
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.WHERE");
} else {
importedTypes.add(NEW_BUILDER_IMPORT);
}
FullyQualifiedJavaType fqjt = introspectedTable.getRules().calculateAllFieldsClass();
importedTypes.add(fqjt);
Method method = new Method(introspectedTable.getUpdateByPrimaryKeySelectiveStatementId());
method.setReturnType(FullyQualifiedJavaType.getStringInstance());
method.setVisibility(JavaVisibility.PUBLIC);
//$NON-NLS-1$
method.addParameter(new Parameter(fqjt, "record"));
context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
if (useLegacyBuilder) {
//$NON-NLS-1$
method.addBodyLine("BEGIN();");
} else {
//$NON-NLS-1$
method.addBodyLine("SQL sql = new SQL();");
}
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%sUPDATE(\"%s\");", builderPrefix, escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime())));
//$NON-NLS-1$
method.addBodyLine("");
for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns())) {
if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"if (record.%s() != null) {", getGetterMethodName(introspectedColumn.getJavaProperty(), introspectedColumn.getFullyQualifiedJavaType())));
}
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%sSET(\"%s = %s\");", builderPrefix, escapeStringForJava(getEscapedColumnName(introspectedColumn)), getParameterClause(introspectedColumn)));
if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
//$NON-NLS-1$
method.addBodyLine("}");
}
//$NON-NLS-1$
method.addBodyLine("");
}
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%sWHERE(\"%s = %s\");", builderPrefix, escapeStringForJava(getEscapedColumnName(introspectedColumn)), getParameterClause(introspectedColumn)));
}
//$NON-NLS-1$
method.addBodyLine("");
if (useLegacyBuilder) {
//$NON-NLS-1$
method.addBodyLine("return SQL();");
} else {
//$NON-NLS-1$
method.addBodyLine("return sql.toString();");
}
if (context.getPlugins().providerUpdateByPrimaryKeySelectiveMethodGenerated(method, topLevelClass, introspectedTable)) {
topLevelClass.addStaticImports(staticImports);
topLevelClass.addImportedTypes(importedTypes);
topLevelClass.addMethod(method);
}
}
use of org.mybatis.generator.api.dom.java.Parameter in project generator by mybatis.
the class ExampleGenerator method getSetBetweenOrNotBetweenMethod.
/**
* Generates methods that set between and not between conditions
*
* @param introspectedColumn
* @param betweenMethod
* @return a generated method for the between or not between method
*/
private Method getSetBetweenOrNotBetweenMethod(IntrospectedColumn introspectedColumn, boolean betweenMethod) {
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType();
//$NON-NLS-1$
method.addParameter(new Parameter(type, "value1"));
//$NON-NLS-1$
method.addParameter(new Parameter(type, "value2"));
StringBuilder sb = new StringBuilder();
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
//$NON-NLS-1$
sb.insert(0, "and");
if (betweenMethod) {
//$NON-NLS-1$
sb.append("Between");
} else {
//$NON-NLS-1$
sb.append("NotBetween");
}
method.setName(sb.toString());
method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
sb.setLength(0);
if (introspectedColumn.isJDBCDateColumn()) {
//$NON-NLS-1$
sb.append("addCriterionForJDBCDate(\"");
} else if (introspectedColumn.isJDBCTimeColumn()) {
//$NON-NLS-1$
sb.append("addCriterionForJDBCTime(\"");
} else if (stringHasValue(introspectedColumn.getTypeHandler())) {
//$NON-NLS-1$
sb.append("add");
sb.append(introspectedColumn.getJavaProperty());
sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
//$NON-NLS-1$
sb.append("Criterion(\"");
} else {
//$NON-NLS-1$
sb.append("addCriterion(\"");
}
sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
if (betweenMethod) {
//$NON-NLS-1$
sb.append(" between");
} else {
//$NON-NLS-1$
sb.append(" not between");
}
//$NON-NLS-1$
sb.append("\", ");
//$NON-NLS-1$
sb.append("value1, value2");
//$NON-NLS-1$
sb.append(", \"");
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("\");");
method.addBodyLine(sb.toString());
//$NON-NLS-1$
method.addBodyLine("return (Criteria) this;");
return method;
}
use of org.mybatis.generator.api.dom.java.Parameter in project generator by mybatis.
the class ExampleGenerator method getCriterionInnerClass.
private InnerClass getCriterionInnerClass() {
Field field;
Method method;
InnerClass answer = new InnerClass(new FullyQualifiedJavaType(//$NON-NLS-1$
"Criterion"));
answer.setVisibility(JavaVisibility.PUBLIC);
answer.setStatic(true);
context.getCommentGenerator().addClassComment(answer, introspectedTable);
field = new Field();
//$NON-NLS-1$
field.setName("condition");
field.setType(FullyQualifiedJavaType.getStringInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
//$NON-NLS-1$
field.setName("value");
field.setType(FullyQualifiedJavaType.getObjectInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
//$NON-NLS-1$
field.setName("secondValue");
field.setType(FullyQualifiedJavaType.getObjectInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
//$NON-NLS-1$
field.setName("noValue");
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
//$NON-NLS-1$
field.setName("singleValue");
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
//$NON-NLS-1$
field.setName("betweenValue");
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
//$NON-NLS-1$
field.setName("listValue");
field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
field = new Field();
//$NON-NLS-1$
field.setName("typeHandler");
field.setType(FullyQualifiedJavaType.getStringInstance());
field.setVisibility(JavaVisibility.PRIVATE);
answer.addField(field);
answer.addMethod(getGetter(field));
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("Criterion");
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
//$NON-NLS-1$
method.addBodyLine("super();");
//$NON-NLS-1$
method.addBodyLine("this.condition = condition;");
//$NON-NLS-1$
method.addBodyLine("this.typeHandler = null;");
//$NON-NLS-1$
method.addBodyLine("this.noValue = true;");
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("Criterion");
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"value"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"typeHandler"));
//$NON-NLS-1$
method.addBodyLine("super();");
//$NON-NLS-1$
method.addBodyLine("this.condition = condition;");
//$NON-NLS-1$
method.addBodyLine("this.value = value;");
//$NON-NLS-1$
method.addBodyLine("this.typeHandler = typeHandler;");
//$NON-NLS-1$
method.addBodyLine("if (value instanceof List<?>) {");
//$NON-NLS-1$
method.addBodyLine("this.listValue = true;");
//$NON-NLS-1$
method.addBodyLine("} else {");
//$NON-NLS-1$
method.addBodyLine("this.singleValue = true;");
//$NON-NLS-1$
method.addBodyLine("}");
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("Criterion");
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"value"));
//$NON-NLS-1$
method.addBodyLine("this(condition, value, null);");
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("Criterion");
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"value"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"secondValue"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"typeHandler"));
//$NON-NLS-1$
method.addBodyLine("super();");
//$NON-NLS-1$
method.addBodyLine("this.condition = condition;");
//$NON-NLS-1$
method.addBodyLine("this.value = value;");
//$NON-NLS-1$
method.addBodyLine("this.secondValue = secondValue;");
//$NON-NLS-1$
method.addBodyLine("this.typeHandler = typeHandler;");
//$NON-NLS-1$
method.addBodyLine("this.betweenValue = true;");
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
//$NON-NLS-1$
method.setName("Criterion");
method.setConstructor(true);
method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), //$NON-NLS-1$
"condition"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"value"));
method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
"secondValue"));
//$NON-NLS-1$
method.addBodyLine("this(condition, value, secondValue, null);");
answer.addMethod(method);
return answer;
}
use of org.mybatis.generator.api.dom.java.Parameter in project generator by mybatis.
the class ProviderApplyWhereMethodGenerator method addClassElements.
@Override
public void addClassElements(TopLevelClass topLevelClass) {
Set<String> staticImports = new TreeSet<String>();
Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
if (useLegacyBuilder) {
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.WHERE");
} else {
importedTypes.add(NEW_BUILDER_IMPORT);
}
importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
"java.util.List"));
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
importedTypes.add(fqjt);
importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
String.format("%s.Criteria", fqjt.getFullyQualifiedName())));
importedTypes.add(new FullyQualifiedJavaType(//$NON-NLS-1$
String.format("%s.Criterion", fqjt.getFullyQualifiedName())));
//$NON-NLS-1$
Method method = new Method("applyWhere");
method.setVisibility(JavaVisibility.PROTECTED);
if (!useLegacyBuilder) {
//$NON-NLS-1$
method.addParameter(new Parameter(NEW_BUILDER_IMPORT, "sql"));
}
//$NON-NLS-1$
method.addParameter(new Parameter(fqjt, "example"));
//$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "includeExamplePhrase"));
context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
for (String methodLine : BEGINNING_METHOD_LINES) {
method.addBodyLine(methodLine);
}
if (useLegacyBuilder) {
for (String methodLine : LEGACY_ENDING_METHOD_LINES) {
method.addBodyLine(methodLine);
}
} else {
for (String methodLine : ENDING_METHOD_LINES) {
method.addBodyLine(methodLine);
}
}
if (context.getPlugins().providerApplyWhereMethodGenerated(method, topLevelClass, introspectedTable)) {
topLevelClass.addStaticImports(staticImports);
topLevelClass.addImportedTypes(importedTypes);
topLevelClass.addMethod(method);
}
}
use of org.mybatis.generator.api.dom.java.Parameter in project generator by mybatis.
the class ProviderDeleteByExampleMethodGenerator method addClassElements.
@Override
public void addClassElements(TopLevelClass topLevelClass) {
Set<String> staticImports = new TreeSet<String>();
Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
if (useLegacyBuilder) {
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM");
//$NON-NLS-1$
staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
} else {
importedTypes.add(NEW_BUILDER_IMPORT);
}
FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getExampleType());
importedTypes.add(fqjt);
Method method = new Method(introspectedTable.getDeleteByExampleStatementId());
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(FullyQualifiedJavaType.getStringInstance());
//$NON-NLS-1$
method.addParameter(new Parameter(fqjt, "example"));
context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
if (useLegacyBuilder) {
//$NON-NLS-1$
method.addBodyLine("BEGIN();");
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"DELETE_FROM(\"%s\");", escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
//$NON-NLS-1$
method.addBodyLine("applyWhere(example, false);");
//$NON-NLS-1$
method.addBodyLine("return SQL();");
} else {
//$NON-NLS-1$
method.addBodyLine("SQL sql = new SQL();");
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"sql.DELETE_FROM(\"%s\");", escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
//$NON-NLS-1$
method.addBodyLine("applyWhere(sql, example, false);");
//$NON-NLS-1$
method.addBodyLine("return sql.toString();");
}
if (context.getPlugins().providerDeleteByExampleMethodGenerated(method, topLevelClass, introspectedTable)) {
topLevelClass.addStaticImports(staticImports);
topLevelClass.addImportedTypes(importedTypes);
topLevelClass.addMethod(method);
}
}
Aggregations