use of org.mybatis.generator.api.dom.xml.XmlElement in project generator by mybatis.
the class DeleteByPrimaryKeyElementGenerator method addElements.
@Override
public void addElements(XmlElement parentElement) {
//$NON-NLS-1$
XmlElement answer = new XmlElement("delete");
answer.addAttribute(new Attribute("id", //$NON-NLS-1$
introspectedTable.getDeleteByPrimaryKeyStatementId()));
String parameterClass;
if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
parameterClass = introspectedTable.getPrimaryKeyType();
} else {
// field, then they are coming in a map.
if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
//$NON-NLS-1$
parameterClass = "map";
} else {
parameterClass = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
}
}
answer.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"parameterType", parameterClass));
context.getCommentGenerator().addComment(answer);
StringBuilder sb = new StringBuilder();
//$NON-NLS-1$
sb.append("delete from ");
sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
answer.addElement(new TextElement(sb.toString()));
boolean and = false;
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
sb.setLength(0);
if (and) {
//$NON-NLS-1$
sb.append(" and ");
} else {
//$NON-NLS-1$
sb.append("where ");
and = true;
}
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
//$NON-NLS-1$
sb.append(" = ");
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
answer.addElement(new TextElement(sb.toString()));
}
if (context.getPlugins().sqlMapDeleteByPrimaryKeyElementGenerated(answer, introspectedTable)) {
parentElement.addElement(answer);
}
}
use of org.mybatis.generator.api.dom.xml.XmlElement in project generator by mybatis.
the class ExampleWhereClauseElementGenerator method getMiddleForEachElement.
private XmlElement getMiddleForEachElement(IntrospectedColumn introspectedColumn) {
StringBuilder sb = new StringBuilder();
String criteriaAttribute;
boolean typeHandled;
String typeHandlerString;
if (introspectedColumn == null) {
//$NON-NLS-1$
criteriaAttribute = "criteria.criteria";
typeHandled = false;
typeHandlerString = null;
} else {
sb.setLength(0);
//$NON-NLS-1$
sb.append("criteria.");
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append("Criteria");
criteriaAttribute = sb.toString();
typeHandled = true;
sb.setLength(0);
//$NON-NLS-1$
sb.append(",typeHandler=");
sb.append(introspectedColumn.getTypeHandler());
typeHandlerString = sb.toString();
}
//$NON-NLS-1$
XmlElement middleForEachElement = new XmlElement("foreach");
middleForEachElement.addAttribute(new Attribute("collection", //$NON-NLS-1$
criteriaAttribute));
//$NON-NLS-1$ //$NON-NLS-2$
middleForEachElement.addAttribute(new Attribute("item", "criterion"));
//$NON-NLS-1$
XmlElement chooseElement = new XmlElement("choose");
middleForEachElement.addElement(chooseElement);
//$NON-NLS-1$
XmlElement when = new XmlElement("when");
//$NON-NLS-1$ //$NON-NLS-2$
when.addAttribute(new Attribute("test", "criterion.noValue"));
//$NON-NLS-1$
when.addElement(new TextElement("and ${criterion.condition}"));
chooseElement.addElement(when);
//$NON-NLS-1$
when = new XmlElement("when");
//$NON-NLS-1$ //$NON-NLS-2$
when.addAttribute(new Attribute("test", "criterion.singleValue"));
sb.setLength(0);
//$NON-NLS-1$
sb.append("and ${criterion.condition} #{criterion.value");
if (typeHandled) {
sb.append(typeHandlerString);
}
sb.append('}');
when.addElement(new TextElement(sb.toString()));
chooseElement.addElement(when);
//$NON-NLS-1$
when = new XmlElement("when");
//$NON-NLS-1$ //$NON-NLS-2$
when.addAttribute(new Attribute("test", "criterion.betweenValue"));
sb.setLength(0);
//$NON-NLS-1$
sb.append("and ${criterion.condition} #{criterion.value");
if (typeHandled) {
sb.append(typeHandlerString);
}
//$NON-NLS-1$
sb.append("} and #{criterion.secondValue");
if (typeHandled) {
sb.append(typeHandlerString);
}
sb.append('}');
when.addElement(new TextElement(sb.toString()));
chooseElement.addElement(when);
//$NON-NLS-1$
when = new XmlElement("when");
//$NON-NLS-1$ //$NON-NLS-2$
when.addAttribute(new Attribute("test", "criterion.listValue"));
//$NON-NLS-1$
when.addElement(new TextElement("and ${criterion.condition}"));
//$NON-NLS-1$
XmlElement innerForEach = new XmlElement("foreach");
innerForEach.addAttribute(//$NON-NLS-1$ //$NON-NLS-2$
new Attribute("collection", "criterion.value"));
//$NON-NLS-1$ //$NON-NLS-2$
innerForEach.addAttribute(new Attribute("item", "listItem"));
//$NON-NLS-1$ //$NON-NLS-2$
innerForEach.addAttribute(new Attribute("open", "("));
//$NON-NLS-1$ //$NON-NLS-2$
innerForEach.addAttribute(new Attribute("close", ")"));
//$NON-NLS-1$ //$NON-NLS-2$
innerForEach.addAttribute(new Attribute("separator", ","));
sb.setLength(0);
//$NON-NLS-1$
sb.append("#{listItem");
if (typeHandled) {
sb.append(typeHandlerString);
}
sb.append('}');
innerForEach.addElement(new TextElement(sb.toString()));
when.addElement(innerForEach);
chooseElement.addElement(when);
return middleForEachElement;
}
use of org.mybatis.generator.api.dom.xml.XmlElement 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$
"parameterType", parameterType.getFullyQualifiedName()));
context.getCommentGenerator().addComment(answer);
GeneratedKey gk = introspectedTable.getGeneratedKey();
if (gk != null) {
IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
// warning has already been reported
if (introspectedColumn != null) {
if (gk.isJdbcStandard()) {
//$NON-NLS-1$ //$NON-NLS-2$
answer.addAttribute(new Attribute("useGeneratedKeys", "true"));
//$NON-NLS-1$
answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
//$NON-NLS-1$
answer.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
} else {
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 insertTrimElement = new XmlElement("trim");
//$NON-NLS-1$ //$NON-NLS-2$
insertTrimElement.addAttribute(new Attribute("prefix", "("));
//$NON-NLS-1$ //$NON-NLS-2$
insertTrimElement.addAttribute(new Attribute("suffix", ")"));
//$NON-NLS-1$ //$NON-NLS-2$
insertTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
answer.addElement(insertTrimElement);
//$NON-NLS-1$
XmlElement valuesTrimElement = new XmlElement("trim");
//$NON-NLS-1$ //$NON-NLS-2$
valuesTrimElement.addAttribute(new Attribute("prefix", "values ("));
//$NON-NLS-1$ //$NON-NLS-2$
valuesTrimElement.addAttribute(new Attribute("suffix", ")"));
//$NON-NLS-1$ //$NON-NLS-2$
valuesTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
answer.addElement(valuesTrimElement);
for (IntrospectedColumn introspectedColumn : ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
if (introspectedColumn.isSequenceColumn() || introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
// if it is a sequence column, it is not optional
// This is required for MyBatis3 because MyBatis3 parses
// and calculates the SQL before executing the selectKey
// if it is primitive, we cannot do a null check
sb.setLength(0);
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
sb.append(',');
insertTrimElement.addElement(new TextElement(sb.toString()));
sb.setLength(0);
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
sb.append(',');
valuesTrimElement.addElement(new TextElement(sb.toString()));
continue;
}
//$NON-NLS-1$
XmlElement insertNotNullElement = new XmlElement("if");
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append(" != null");
insertNotNullElement.addAttribute(new Attribute("test", //$NON-NLS-1$
sb.toString()));
sb.setLength(0);
sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
sb.append(',');
insertNotNullElement.addElement(new TextElement(sb.toString()));
insertTrimElement.addElement(insertNotNullElement);
//$NON-NLS-1$
XmlElement valuesNotNullElement = new XmlElement("if");
sb.setLength(0);
sb.append(introspectedColumn.getJavaProperty());
//$NON-NLS-1$
sb.append(" != null");
valuesNotNullElement.addAttribute(new Attribute("test", //$NON-NLS-1$
sb.toString()));
sb.setLength(0);
sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
sb.append(',');
valuesNotNullElement.addElement(new TextElement(sb.toString()));
valuesTrimElement.addElement(valuesNotNullElement);
}
if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(answer, introspectedTable)) {
parentElement.addElement(answer);
}
}
use of org.mybatis.generator.api.dom.xml.XmlElement in project generator by mybatis.
the class ResultMapWithBLOBsElementGenerator method addResultMapElements.
private void addResultMapElements(XmlElement answer) {
for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) {
//$NON-NLS-1$
XmlElement resultElement = new XmlElement("result");
resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
resultElement.addAttribute(new Attribute("property", //$NON-NLS-1$
introspectedColumn.getJavaProperty()));
resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
introspectedColumn.getJdbcTypeName()));
if (stringHasValue(introspectedColumn.getTypeHandler())) {
resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
introspectedColumn.getTypeHandler()));
}
answer.addElement(resultElement);
}
}
use of org.mybatis.generator.api.dom.xml.XmlElement in project generator by mybatis.
the class ResultMapWithBLOBsElementGenerator method addResultMapConstructorElements.
private void addResultMapConstructorElements(XmlElement answer) {
//$NON-NLS-1$
XmlElement constructor = new XmlElement("constructor");
for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
//$NON-NLS-1$
XmlElement resultElement = new XmlElement("idArg");
resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
introspectedColumn.getJdbcTypeName()));
resultElement.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"javaType", introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName()));
if (stringHasValue(introspectedColumn.getTypeHandler())) {
resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
introspectedColumn.getTypeHandler()));
}
constructor.addElement(resultElement);
}
for (IntrospectedColumn introspectedColumn : introspectedTable.getNonPrimaryKeyColumns()) {
//$NON-NLS-1$
XmlElement resultElement = new XmlElement("arg");
resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
introspectedColumn.getJdbcTypeName()));
if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
// need to use the MyBatis type alias for a primitive byte
StringBuilder sb = new StringBuilder();
sb.append('_');
sb.append(introspectedColumn.getFullyQualifiedJavaType().getShortName());
resultElement.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"javaType", sb.toString()));
} else if ("byte[]".equals(//$NON-NLS-1$
introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName())) {
// need to use the MyBatis type alias for a primitive byte arry
resultElement.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"javaType", //$NON-NLS-1$
"_byte[]"));
} else {
resultElement.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"javaType", introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName()));
}
if (stringHasValue(introspectedColumn.getTypeHandler())) {
resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
introspectedColumn.getTypeHandler()));
}
constructor.addElement(resultElement);
}
answer.addElement(constructor);
}
Aggregations