use of org.mybatis.generator.api.dom.xml.XmlElement in project generator by mybatis.
the class SqlMapConfigPlugin method contextGenerateAdditionalXmlFiles.
@Override
public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() {
Document document = new Document(XmlConstants.IBATIS2_SQL_MAP_CONFIG_PUBLIC_ID, XmlConstants.IBATIS2_SQL_MAP_CONFIG_SYSTEM_ID);
//$NON-NLS-1$
XmlElement root = new XmlElement("sqlMapConfig");
document.setRootElement(root);
//$NON-NLS-1$
root.addElement(new TextElement("<!--"));
root.addElement(new TextElement(//$NON-NLS-1$
" This file is generated by MyBatis Generator."));
root.addElement(new TextElement(//$NON-NLS-1$
" This file is the shell of an SqlMapConfig file - in many cases you will need to add"));
root.addElement(new TextElement(//$NON-NLS-1$
" to this file before it is usable by iBATIS."));
StringBuilder sb = new StringBuilder();
//$NON-NLS-1$
sb.append(" This file was generated on ");
sb.append(new Date());
sb.append('.');
root.addElement(new TextElement(sb.toString()));
//$NON-NLS-1$
root.addElement(new TextElement("-->"));
//$NON-NLS-1$
XmlElement settings = new XmlElement("settings");
//$NON-NLS-1$ //$NON-NLS-2$
settings.addAttribute(new Attribute("useStatementNamespaces", "true"));
root.addElement(settings);
XmlElement sqlMap;
for (String sqlMapFile : sqlMapFiles) {
//$NON-NLS-1$
sqlMap = new XmlElement("sqlMap");
//$NON-NLS-1$
sqlMap.addAttribute(new Attribute("resource", sqlMapFile));
root.addElement(sqlMap);
}
GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties.getProperty("fileName", //$NON-NLS-1$ //$NON-NLS-2$
"SqlMapConfig.xml"), //$NON-NLS-1$
properties.getProperty("targetPackage"), //$NON-NLS-1$
properties.getProperty("targetProject"), false, context.getXmlFormatter());
List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>(1);
answer.add(gxf);
return answer;
}
use of org.mybatis.generator.api.dom.xml.XmlElement 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.XmlElement in project generator by mybatis.
the class AntFileGenerator method addSqlTaskIfNecessary.
private void addSqlTaskIfNecessary(XmlElement parent) {
String sqlFileFullPath = getSqlFile();
if (sqlFileFullPath != null) {
//$NON-NLS-1$
XmlElement sqlTask = new XmlElement("sql");
//$NON-NLS-1$
sqlTask.addAttribute(new Attribute("driver", getTextOrBlank(configuration, ATTR_SQL_SCRIPT_DRIVER_CLASS)));
//$NON-NLS-1$
sqlTask.addAttribute(new Attribute("url", getTextOrBlank(configuration, ATTR_SQL_SCRIPT_CONNECTION_URL)));
//$NON-NLS-1$
sqlTask.addAttribute(new Attribute("userid", LauncherUtils.getUserId(configuration)));
//$NON-NLS-1$
sqlTask.addAttribute(new Attribute("password", LauncherUtils.getPassword(configuration)));
//$NON-NLS-1$
sqlTask.addAttribute(new Attribute("src", sqlFileFullPath));
parent.addElement(sqlTask);
}
}
use of org.mybatis.generator.api.dom.xml.XmlElement 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.XmlElement 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);
}
}
Aggregations