use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class ProviderUpdateByExampleSelectiveMethodGenerator 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");
} else {
importedTypes.add(NEW_BUILDER_IMPORT);
}
//$NON-NLS-1$
importedTypes.add(new FullyQualifiedJavaType("java.util.Map"));
Method method = new Method(introspectedTable.getUpdateByExampleSelectiveStatementId());
method.setReturnType(FullyQualifiedJavaType.getStringInstance());
method.setVisibility(JavaVisibility.PUBLIC);
method.addParameter(new //$NON-NLS-1$
Parameter(//$NON-NLS-1$
new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$
"parameter"));
FullyQualifiedJavaType record = introspectedTable.getRules().calculateAllFieldsClass();
importedTypes.add(record);
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%s record = (%s) parameter.get(\"record\");", record.getShortName(), record.getShortName()));
FullyQualifiedJavaType example = new FullyQualifiedJavaType(introspectedTable.getExampleType());
importedTypes.add(example);
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%s example = (%s) parameter.get(\"example\");", example.getShortName(), example.getShortName()));
context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
//$NON-NLS-1$
method.addBodyLine("");
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.getAliasedFullyQualifiedTableNameAtRuntime())));
//$NON-NLS-1$
method.addBodyLine("");
for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"if (record.%s() != null) {", getGetterMethodName(introspectedColumn.getJavaProperty(), introspectedColumn.getFullyQualifiedJavaType())));
}
StringBuilder sb = new StringBuilder();
sb.append(getParameterClause(introspectedColumn));
//$NON-NLS-1$
sb.insert(2, "record.");
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%sSET(\"%s = %s\");", builderPrefix, escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)), sb.toString()));
if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
//$NON-NLS-1$
method.addBodyLine("}");
}
//$NON-NLS-1$
method.addBodyLine("");
}
if (useLegacyBuilder) {
//$NON-NLS-1$
method.addBodyLine("applyWhere(example, true);");
//$NON-NLS-1$
method.addBodyLine("return SQL();");
} else {
//$NON-NLS-1$
method.addBodyLine("applyWhere(sql, example, true);");
//$NON-NLS-1$
method.addBodyLine("return sql.toString();");
}
if (context.getPlugins().providerUpdateByExampleSelectiveMethodGenerated(method, topLevelClass, introspectedTable)) {
topLevelClass.addStaticImports(staticImports);
topLevelClass.addImportedTypes(importedTypes);
topLevelClass.addMethod(method);
}
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class ProviderUpdateByExampleWithoutBLOBsMethodGenerator 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");
} else {
importedTypes.add(NEW_BUILDER_IMPORT);
}
//$NON-NLS-1$
importedTypes.add(new FullyQualifiedJavaType("java.util.Map"));
Method method = new Method(getMethodName());
method.setReturnType(FullyQualifiedJavaType.getStringInstance());
method.setVisibility(JavaVisibility.PUBLIC);
method.addParameter(new //$NON-NLS-1$
Parameter(//$NON-NLS-1$
new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$
"parameter"));
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.getAliasedFullyQualifiedTableNameAtRuntime())));
//$NON-NLS-1$
method.addBodyLine("");
for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(getColumns())) {
StringBuilder sb = new StringBuilder();
sb.append(getParameterClause(introspectedColumn));
//$NON-NLS-1$
sb.insert(2, "record.");
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%sSET(\"%s = %s\");", builderPrefix, escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)), sb.toString()));
}
//$NON-NLS-1$
method.addBodyLine("");
FullyQualifiedJavaType example = new FullyQualifiedJavaType(introspectedTable.getExampleType());
importedTypes.add(example);
method.addBodyLine(//$NON-NLS-1$
String.format(//$NON-NLS-1$
"%s example = (%s) parameter.get(\"example\");", example.getShortName(), example.getShortName()));
if (useLegacyBuilder) {
//$NON-NLS-1$
method.addBodyLine("applyWhere(example, true);");
//$NON-NLS-1$
method.addBodyLine("return SQL();");
} else {
//$NON-NLS-1$
method.addBodyLine("applyWhere(sql, example, true);");
//$NON-NLS-1$
method.addBodyLine("return sql.toString();");
}
if (callPlugins(method, topLevelClass)) {
topLevelClass.addStaticImports(staticImports);
topLevelClass.addImportedTypes(importedTypes);
topLevelClass.addMethod(method);
}
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class AnnotatedDeleteByPrimaryKeyMethodGenerator method addMapperAnnotations.
@Override
public void addMapperAnnotations(Method method) {
//$NON-NLS-1$
method.addAnnotation("@Delete({");
StringBuilder sb = new StringBuilder();
javaIndent(sb, 1);
//$NON-NLS-1$
sb.append("\"delete from ");
sb.append(escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime()));
//$NON-NLS-1$
sb.append("\",");
method.addAnnotation(sb.toString());
boolean and = false;
Iterator<IntrospectedColumn> iter = introspectedTable.getPrimaryKeyColumns().iterator();
while (iter.hasNext()) {
IntrospectedColumn introspectedColumn = iter.next();
sb.setLength(0);
javaIndent(sb, 1);
if (and) {
//$NON-NLS-1$
sb.append(" \"and ");
} else {
//$NON-NLS-1$
sb.append("\"where ");
and = true;
}
sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
//$NON-NLS-1$
sb.append(" = ");
sb.append(getParameterClause(introspectedColumn));
sb.append('\"');
if (iter.hasNext()) {
sb.append(',');
}
method.addAnnotation(sb.toString());
}
//$NON-NLS-1$
method.addAnnotation("})");
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class InsertSelectiveElementGenerator method addElements.
@Override
public void addElements(XmlElement parentElement) {
//$NON-NLS-1$
XmlElement answer = new XmlElement("insert");
answer.addAttribute(new Attribute("id", //$NON-NLS-1$
introspectedTable.getInsertSelectiveStatementId()));
FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
answer.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"parameterClass", parameterType.getFullyQualifiedName()));
context.getCommentGenerator().addComment(answer);
GeneratedKey gk = introspectedTable.getGeneratedKey();
if (gk != null && gk.isPlacedBeforeInsertInIbatis2()) {
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
// warning has already been reported
if (introspectedColumn != null) {
// pre-generated key
answer.addElement(getSelectKey(introspectedColumn, gk));
}
}
StringBuilder sb = new StringBuilder();
//$NON-NLS-1$
sb.append("insert into ");
sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
answer.addElement(new TextElement(sb.toString()));
//$NON-NLS-1$
XmlElement insertElement = new XmlElement("dynamic");
//$NON-NLS-1$ //$NON-NLS-2$
insertElement.addAttribute(new Attribute("prepend", "("));
answer.addElement(insertElement);
//$NON-NLS-1$
answer.addElement(new TextElement("values"));
//$NON-NLS-1$
XmlElement valuesElement = new XmlElement("dynamic");
//$NON-NLS-1$ //$NON-NLS-2$
valuesElement.addAttribute(new Attribute("prepend", "("));
answer.addElement(valuesElement);
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
if (introspectedColumn.isIdentity()) {
// cannot set values on identity fields
continue;
}
//$NON-NLS-1$
XmlElement insertNotNullElement = new XmlElement("isNotNull");
//$NON-NLS-1$ //$NON-NLS-2$
insertNotNullElement.addAttribute(new Attribute("prepend", ","));
insertNotNullElement.addAttribute(new Attribute("property", //$NON-NLS-1$
introspectedColumn.getJavaProperty()));
insertNotNullElement.addElement(new TextElement(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn)));
insertElement.addElement(insertNotNullElement);
//$NON-NLS-1$
XmlElement valuesNotNullElement = new XmlElement("isNotNull");
//$NON-NLS-1$ //$NON-NLS-2$
valuesNotNullElement.addAttribute(new Attribute("prepend", ","));
valuesNotNullElement.addAttribute(new Attribute("property", //$NON-NLS-1$
introspectedColumn.getJavaProperty()));
valuesNotNullElement.addElement(new TextElement(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn)));
valuesElement.addElement(valuesNotNullElement);
}
//$NON-NLS-1$
insertElement.addElement(new TextElement(")"));
//$NON-NLS-1$
valuesElement.addElement(new TextElement(")"));
if (gk != null && !gk.isPlacedBeforeInsertInIbatis2()) {
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
// warning has already been reported
if (introspectedColumn != null) {
// pre-generated key
answer.addElement(getSelectKey(introspectedColumn, gk));
}
}
if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(answer, introspectedTable)) {
parentElement.addElement(answer);
}
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class ResultMapWithoutBLOBsElementGenerator method addElements.
@Override
public void addElements(XmlElement parentElement) {
boolean useColumnIndex = isTrue(introspectedTable.getTableConfigurationProperty(PropertyRegistry.TABLE_USE_COLUMN_INDEXES));
//$NON-NLS-1$
XmlElement answer = new XmlElement("resultMap");
answer.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"id", introspectedTable.getBaseResultMapId()));
String returnType;
if (introspectedTable.getRules().generateBaseRecordClass()) {
returnType = introspectedTable.getBaseRecordType();
} else {
returnType = introspectedTable.getPrimaryKeyType();
}
answer.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"class", returnType));
context.getCommentGenerator().addComment(answer);
int i = 1;
if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId()) || stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
i++;
}
for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
//$NON-NLS-1$
XmlElement resultElement = new XmlElement("result");
if (useColumnIndex) {
resultElement.addAttribute(new Attribute("columnIndex", //$NON-NLS-1$
Integer.toString(i++)));
} else {
resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
Ibatis2FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
}
resultElement.addAttribute(new Attribute("property", //$NON-NLS-1$
introspectedColumn.getJavaProperty()));
resultElement.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"jdbcType", introspectedColumn.getJdbcTypeName()));
if (stringHasValue(introspectedColumn.getTypeHandler())) {
resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
introspectedColumn.getTypeHandler()));
}
answer.addElement(resultElement);
}
if (context.getPlugins().sqlMapResultMapWithoutBLOBsElementGenerated(answer, introspectedTable)) {
parentElement.addElement(answer);
}
}
Aggregations