use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.
the class XmlFileMergerTest method addInsertElement.
private void addInsertElement(CommentGenerator commentGenerator, XmlElement parentElement) {
XmlElement answer = new XmlElement("insert");
answer.addAttribute(new Attribute("id", "insert"));
FullyQualifiedJavaType parameterType;
parameterType = new FullyQualifiedJavaType("org.mybatis.test.TestRecord");
answer.addAttribute(new Attribute("parameterType", parameterType.getFullyQualifiedName()));
commentGenerator.addComment(answer);
StringBuilder insertClause = new StringBuilder();
StringBuilder valuesClause = new StringBuilder();
insertClause.append("insert into ");
insertClause.append("myschema.mytable");
insertClause.append(" (id, description)");
valuesClause.append("values (#{id}, #{description})");
answer.addElement(new TextElement(insertClause.toString()));
answer.addElement(new TextElement(valuesClause.toString()));
parentElement.addElement(answer);
}
use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.
the class UpdateByPrimaryKeyWithBLOBsElementGenerator method addElements.
@Override
public void addElements(XmlElement parentElement) {
//$NON-NLS-1$
XmlElement answer = new XmlElement("update");
answer.addAttribute(new Attribute("id", //$NON-NLS-1$
introspectedTable.getUpdateByPrimaryKeyWithBLOBsStatementId()));
String parameterType;
if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
parameterType = introspectedTable.getRecordWithBLOBsType();
} else {
parameterType = introspectedTable.getBaseRecordType();
}
answer.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"parameterClass", parameterType));
context.getCommentGenerator().addComment(answer);
StringBuilder sb = new StringBuilder();
//$NON-NLS-1$
sb.append("update ");
sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
answer.addElement(new TextElement(sb.toString()));
// set up for first column
sb.setLength(0);
//$NON-NLS-1$
sb.append("set ");
Iterator<IntrospectedColumn> iter = introspectedTable.getNonPrimaryKeyColumns().iterator();
while (iter.hasNext()) {
IntrospectedColumn introspectedColumn = iter.next();
sb.append(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
//$NON-NLS-1$
sb.append(" = ");
sb.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
if (iter.hasNext()) {
sb.append(',');
}
answer.addElement(new TextElement(sb.toString()));
// set up for the next column
if (iter.hasNext()) {
sb.setLength(0);
OutputUtilities.xmlIndent(sb, 1);
}
}
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(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
//$NON-NLS-1$
sb.append(" = ");
sb.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
answer.addElement(new TextElement(sb.toString()));
}
if (context.getPlugins().sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(answer, introspectedTable)) {
parentElement.addElement(answer);
}
}
use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.
the class InsertElementGenerator 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.getInsertStatementId()));
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 insertClause = new StringBuilder();
StringBuilder valuesClause = new StringBuilder();
//$NON-NLS-1$
insertClause.append("insert into ");
insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
//$NON-NLS-1$
insertClause.append(" (");
//$NON-NLS-1$
valuesClause.append("values (");
List<String> valuesClauses = new ArrayList<String>();
Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns().iterator();
while (iter.hasNext()) {
IntrospectedColumn introspectedColumn = iter.next();
if (introspectedColumn.isIdentity()) {
// cannot set values on identity fields
continue;
}
insertClause.append(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
valuesClause.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
if (iter.hasNext()) {
//$NON-NLS-1$
insertClause.append(", ");
//$NON-NLS-1$
valuesClause.append(", ");
}
if (valuesClause.length() > 80) {
answer.addElement(new TextElement(insertClause.toString()));
insertClause.setLength(0);
OutputUtilities.xmlIndent(insertClause, 1);
valuesClauses.add(valuesClause.toString());
valuesClause.setLength(0);
OutputUtilities.xmlIndent(valuesClause, 1);
}
}
insertClause.append(')');
answer.addElement(new TextElement(insertClause.toString()));
valuesClause.append(')');
valuesClauses.add(valuesClause.toString());
for (String clause : valuesClauses) {
answer.addElement(new TextElement(clause));
}
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().sqlMapInsertElementGenerated(answer, introspectedTable)) {
parentElement.addElement(answer);
}
}
use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.
the class SelectByExampleWithBLOBsElementGenerator method addElements.
@Override
public void addElements(XmlElement parentElement) {
//$NON-NLS-1$
XmlElement answer = new XmlElement("select");
answer.addAttribute(new Attribute("id", //$NON-NLS-1$
introspectedTable.getSelectByExampleWithBLOBsStatementId()));
answer.addAttribute(new Attribute("resultMap", //$NON-NLS-1$
introspectedTable.getResultMapWithBLOBsId()));
answer.addAttribute(new Attribute("parameterClass", //$NON-NLS-1$
introspectedTable.getExampleType()));
context.getCommentGenerator().addComment(answer);
//$NON-NLS-1$
answer.addElement(new TextElement("select"));
//$NON-NLS-1$
XmlElement isParameterPresent = new XmlElement("isParameterPresent");
//$NON-NLS-1$
XmlElement isEqualElement = new XmlElement("isEqual");
//$NON-NLS-1$ //$NON-NLS-2$
isEqualElement.addAttribute(new Attribute("property", "distinct"));
//$NON-NLS-1$ //$NON-NLS-2$
isEqualElement.addAttribute(new Attribute("compareValue", "true"));
//$NON-NLS-1$
isEqualElement.addElement(new TextElement("distinct"));
isParameterPresent.addElement(isEqualElement);
answer.addElement(isParameterPresent);
StringBuilder sb = new StringBuilder();
if (stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
sb.append('\'');
sb.append(introspectedTable.getSelectByExampleQueryId());
//$NON-NLS-1$
sb.append("' as QUERYID,");
answer.addElement(new TextElement(sb.toString()));
}
answer.addElement(getBaseColumnListElement());
//$NON-NLS-1$
answer.addElement(new TextElement(","));
answer.addElement(getBlobColumnListElement());
sb.setLength(0);
//$NON-NLS-1$
sb.append("from ");
sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
answer.addElement(new TextElement(sb.toString()));
XmlElement isParameterPresenteElement = new XmlElement(//$NON-NLS-1$
"isParameterPresent");
answer.addElement(isParameterPresenteElement);
//$NON-NLS-1$
XmlElement includeElement = new XmlElement("include");
includeElement.addAttribute(new //$NON-NLS-1$
Attribute(//$NON-NLS-1$
"refid", introspectedTable.getIbatis2SqlMapNamespace() + "." + //$NON-NLS-1$
introspectedTable.getExampleWhereClauseId()));
isParameterPresenteElement.addElement(includeElement);
//$NON-NLS-1$
XmlElement isNotNullElement = new XmlElement("isNotNull");
isNotNullElement.addAttribute(//$NON-NLS-1$ //$NON-NLS-2$
new Attribute("property", "orderByClause"));
isNotNullElement.addElement(//$NON-NLS-1$
new TextElement("order by $orderByClause$"));
isParameterPresenteElement.addElement(isNotNullElement);
if (context.getPlugins().sqlMapSelectByExampleWithBLOBsElementGenerated(answer, introspectedTable)) {
parentElement.addElement(answer);
}
}
use of org.mybatis.generator.api.dom.xml.TextElement in project mybatis-generator-gui-extension by spawpaw.
the class PagePlugin method sqlMapSelectByExampleWithoutBLOBsElementGenerated.
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
XmlElement ifSetLimitSqlStatement = new XmlElement("if");
ifSetLimitSqlStatement.addAttribute(new Attribute("test", "limit != null"));
XmlElement ifSetOffsetSqlStatement = new XmlElement("if");
ifSetOffsetSqlStatement.addAttribute(new Attribute("test", "offset != null"));
ifSetOffsetSqlStatement.addElement(new TextElement("limit ${offset}, ${limit}"));
ifSetLimitSqlStatement.addElement(ifSetOffsetSqlStatement);
XmlElement ifOffsetNullElement = new XmlElement("if");
ifOffsetNullElement.addAttribute(new Attribute("test", "offset == null"));
ifOffsetNullElement.addElement(new TextElement("limit ${limit}"));
ifSetLimitSqlStatement.addElement(ifOffsetNullElement);
element.addElement(ifSetLimitSqlStatement);
return true;
}
Aggregations