/** 添加方法 */ protected Method selectByPrimaryKey(IntrospectedTable introspectedTable, String tableName) { Method method = new Method(); method.setName("selectByPrimaryKey"); method.setReturnType(pojoType); if (introspectedTable.getRules().generatePrimaryKeyClass()) { FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType()); method.addParameter(new Parameter(type, "key")); } else { for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) { FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType(); method.addParameter(new Parameter(type, introspectedColumn.getJavaProperty())); } } method.setVisibility(JavaVisibility.PUBLIC); StringBuilder sb = new StringBuilder(); // method.addBodyLine("try {"); sb.append("return this."); sb.append(getDaoShort()); sb.append("selectByPrimaryKey"); sb.append("("); for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) { sb.append(introspectedColumn.getJavaProperty()); sb.append(","); } sb.setLength(sb.length() - 1); sb.append(");"); method.addBodyLine(sb.toString()); // method.addBodyLine("} catch (Exception e) {"); // method.addBodyLine("logger.error(\"Exception: \", e);"); // method.addBodyLine("return null;"); // method.addBodyLine("}"); return method; }
public void addSetterComment( Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); // $NON-NLS-1$ method.addJavaDocLine(" * This method was generated by MyBatis Generator."); // $NON-NLS-1$ sb.append(" * This method sets the value of the database column "); // $NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName()); method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" *"); // $NON-NLS-1$ Parameter parm = method.getParameters().get(0); sb.setLength(0); sb.append(" * @param "); // $NON-NLS-1$ sb.append(parm.getName()); sb.append(" the value for "); // $NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); sb.append('.'); sb.append(introspectedColumn.getActualColumnName()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); // $NON-NLS-1$ }
@Override public void initialized(IntrospectedTable introspectedTable) { List<IntrospectedColumn> columns = introspectedTable.getAllColumns(); for (IntrospectedColumn introspectedColumn : columns) { String javaProperty = this.getJavaName( introspectedColumn.getActualColumnName(), introspectedColumn.getJavaProperty()); introspectedColumn.setJavaProperty(javaProperty); } super.initialized(introspectedTable); }
@Override public void addElements(XmlElement parentElement) { XmlElement answer = new XmlElement("sql"); // $NON-NLS-1$ if (isForUpdateByExample) { answer.addAttribute( new Attribute( "id", introspectedTable.getMyBatis3UpdateByExampleWhereClauseId())); // $NON-NLS-1$ } else { answer.addAttribute( new Attribute("id", introspectedTable.getExampleWhereClauseId())); // $NON-NLS-1$ } context.getCommentGenerator().addComment(answer); XmlElement whereElement = new XmlElement("where"); // $NON-NLS-1$ answer.addElement(whereElement); XmlElement outerForEachElement = new XmlElement("foreach"); // $NON-NLS-1$ if (isForUpdateByExample) { outerForEachElement.addAttribute( new Attribute("collection", "example.oredCriteria")); // $NON-NLS-1$ //$NON-NLS-2$ } else { outerForEachElement.addAttribute( new Attribute("collection", "oredCriteria")); // $NON-NLS-1$ //$NON-NLS-2$ } outerForEachElement.addAttribute( new Attribute("item", "criteria")); // $NON-NLS-1$ //$NON-NLS-2$ outerForEachElement.addAttribute(new Attribute("separator", "or")); // $NON-NLS-1$ //$NON-NLS-2$ whereElement.addElement(outerForEachElement); XmlElement ifElement = new XmlElement("if"); // $NON-NLS-1$ ifElement.addAttribute(new Attribute("test", "criteria.valid")); // $NON-NLS-1$ //$NON-NLS-2$ outerForEachElement.addElement(ifElement); XmlElement trimElement = new XmlElement("trim"); // $NON-NLS-1$ trimElement.addAttribute(new Attribute("prefix", "(")); // $NON-NLS-1$ //$NON-NLS-2$ trimElement.addAttribute(new Attribute("suffix", ")")); // $NON-NLS-1$ //$NON-NLS-2$ trimElement.addAttribute(new Attribute("prefixOverrides", "and")); // $NON-NLS-1$ //$NON-NLS-2$ ifElement.addElement(trimElement); trimElement.addElement(getMiddleForEachElement(null)); for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) { if (StringUtility.stringHasValue(introspectedColumn.getTypeHandler())) { trimElement.addElement(getMiddleForEachElement(introspectedColumn)); } } if (context.getPlugins().sqlMapExampleWhereClauseElementGenerated(answer, introspectedTable)) { parentElement.addElement(answer); } }
public void addFieldComment( Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { StringBuilder sb = new StringBuilder(); field.addJavaDocLine("/**"); field.addJavaDocLine(" * " + introspectedColumn.getRemarks()); if (detailComment) { field.addJavaDocLine(" * 默认值: " + introspectedColumn.getDefaultValue()); field.addJavaDocLine(" * 长 度: " + introspectedColumn.getLength()); } field.addJavaDocLine(" */"); // $NON-NLS-1$ }
/** * @param introspectedColumn * @param inMethod if true generates an "in" method, else generates a "not in" method * @return a generated method for the in or not in method */ private Method getSetInOrNotInMethod(IntrospectedColumn introspectedColumn, boolean inMethod) { Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); FullyQualifiedJavaType type = FullyQualifiedJavaType.getNewListInstance(); if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) { type.addTypeArgument( introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper()); } else { type.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType()); } method.addParameter(new Parameter(type, "values")); // $NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(introspectedColumn.getJavaProperty()); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); sb.insert(0, "and"); // $NON-NLS-1$ if (inMethod) { sb.append("In"); // $NON-NLS-1$ } else { sb.append("NotIn"); // $NON-NLS-1$ } method.setName(sb.toString()); method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); sb.setLength(0); if (introspectedColumn.isJDBCDateColumn()) { sb.append("addCriterionForJDBCDate(\""); // $NON-NLS-1$ } else if (introspectedColumn.isJDBCTimeColumn()) { sb.append("addCriterionForJDBCTime(\""); // $NON-NLS-1$ } else if (stringHasValue(introspectedColumn.getTypeHandler())) { sb.append("add"); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.setCharAt(3, Character.toUpperCase(sb.charAt(3))); sb.append("Criterion(\""); // $NON-NLS-1$ } else { sb.append("addCriterion(\""); // $NON-NLS-1$ } sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn)); if (inMethod) { sb.append(" in"); // $NON-NLS-1$ } else { sb.append(" not in"); // $NON-NLS-1$ } sb.append("\", values, \""); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.append("\");"); // $NON-NLS-1$ method.addBodyLine(sb.toString()); method.addBodyLine("return (Criteria) this;"); // $NON-NLS-1$ return method; }
private Method getSingleValueMethod( IntrospectedColumn introspectedColumn, String nameFragment, String operator) { Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.addParameter( new Parameter(introspectedColumn.getFullyQualifiedJavaType(), "value")); // $NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(introspectedColumn.getJavaProperty()); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); sb.insert(0, "and"); // $NON-NLS-1$ sb.append(nameFragment); method.setName(sb.toString()); method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); sb.setLength(0); if (introspectedColumn.isJDBCDateColumn()) { sb.append("addCriterionForJDBCDate(\""); // $NON-NLS-1$ } else if (introspectedColumn.isJDBCTimeColumn()) { sb.append("addCriterionForJDBCTime(\""); // $NON-NLS-1$ } else if (stringHasValue(introspectedColumn.getTypeHandler())) { sb.append("add"); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.setCharAt(3, Character.toUpperCase(sb.charAt(3))); sb.append("Criterion(\""); // $NON-NLS-1$ } else { sb.append("addCriterion(\""); // $NON-NLS-1$ } sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn)); sb.append(' '); sb.append(operator); sb.append("\", "); // $NON-NLS-1$ if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) { sb.append("new "); // $NON-NLS-1$ sb.append( introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper().getShortName()); sb.append("(value)"); // $NON-NLS-1$ } else { sb.append("value"); // $NON-NLS-1$ } sb.append(", \""); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.append("\");"); // $NON-NLS-1$ method.addBodyLine(sb.toString()); method.addBodyLine("return (Criteria) this;"); // $NON-NLS-1$ return method; }
/** * getter方法注释 * * @param method * @param introspectedTable * @param introspectedColumn */ public void addGetterComment( Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { sb.append(" * 获取"); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" *"); } sb.setLength(0); sb.append(" * @return "); sb.append(introspectedColumn.getActualColumnName()); if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { sb.append(" - "); sb.append(introspectedColumn.getRemarks()); } method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" */"); }
/** * setter方法注释 * * @param method * @param introspectedTable * @param introspectedColumn */ public void addSetterComment( Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { sb.append(" * 设置"); sb.append(introspectedColumn.getRemarks()); method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" *"); } Parameter parm = method.getParameters().get(0); sb.setLength(0); sb.append(" * @param "); sb.append(parm.getName()); if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { sb.append(" "); sb.append(introspectedColumn.getRemarks()); } method.addJavaDocLine(sb.toString()); method.addJavaDocLine(" */"); }
/** * 给字段添加数据库备注 * * @param field * @param introspectedTable * @param introspectedColumn */ public void addFieldComment( Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) { field.addJavaDocLine("/**"); StringBuilder sb = new StringBuilder(); sb.append(" * "); sb.append(introspectedColumn.getRemarks()); field.addJavaDocLine(sb.toString()); field.addJavaDocLine(" */"); } // 添加注解 if (field.isTransient()) { // @Column field.addAnnotation("@Transient"); } for (IntrospectedColumn column : introspectedTable.getPrimaryKeyColumns()) { if (introspectedColumn == column) { field.addAnnotation("@Id"); break; } } String column = introspectedColumn.getActualColumnName(); if (StringUtility.stringContainsSpace(column) || introspectedTable.getTableConfiguration().isAllColumnDelimitingEnabled()) { column = introspectedColumn.getContext().getBeginningDelimiter() + column + introspectedColumn.getContext().getEndingDelimiter(); } if (!column.equals(introspectedColumn.getJavaProperty())) { // @Column field.addAnnotation("@Column(name = \"" + column + "\")"); } if (introspectedColumn.isIdentity()) { if (introspectedTable .getTableConfiguration() .getGeneratedKey() .getRuntimeSqlStatement() .equals("JDBC")) { field.addAnnotation("@GeneratedValue(generator = \"JDBC\")"); } else { field.addAnnotation("@GeneratedValue(strategy = GenerationType.IDENTITY)"); } } else if (introspectedColumn.isSequenceColumn()) { field.addAnnotation( "@SequenceGenerator(name=\"\",sequenceName=\"" + introspectedTable.getTableConfiguration().getGeneratedKey().getRuntimeSqlStatement() + "\")"); } }
/** type 的意义 pojo 1 key 2 example 3 pojo+example 4 */ protected String addParams(IntrospectedTable introspectedTable, Method method, int type1) { switch (type1) { case 1: method.addParameter(new Parameter(pojoType, "record")); return "record"; case 2: if (introspectedTable.getRules().generatePrimaryKeyClass()) { FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType()); method.addParameter(new Parameter(type, "key")); } else { for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) { FullyQualifiedJavaType type = introspectedColumn.getFullyQualifiedJavaType(); method.addParameter(new Parameter(type, introspectedColumn.getJavaProperty())); } } StringBuffer sb = new StringBuffer(); for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) { sb.append(introspectedColumn.getJavaProperty()); sb.append(","); } sb.setLength(sb.length() - 1); return sb.toString(); case 3: method.addParameter(new Parameter(pojoCriteriaType, "example")); return "example"; case 4: method.addParameter(0, new Parameter(pojoType, "record")); method.addParameter(1, new Parameter(pojoCriteriaType, "example")); if (method.getName().equals("updateByExampleSelective") || method.getName().equals("updateByExample")) { return "record, example.getCondition()"; } return "record, example"; default: break; } return null; }
@Override public boolean modelFieldGenerated( Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { String remarks = introspectedColumn.getRemarks(); if (remarks != null) { field.addJavaDocLine("//" + remarks); } return super.modelFieldGenerated( field, topLevelClass, introspectedColumn, introspectedTable, modelClassType); }
private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) { Method method = new Method(); FullyQualifiedJavaType returnType; if (introspectedTable.getGeneratedKey() != null) { IntrospectedColumn introspectedColumn = introspectedTable.getColumn(introspectedTable.getGeneratedKey().getColumn()); if (introspectedColumn == null) { // the specified column doesn't exist, so don't do the generated // key // (the warning has already been reported) returnType = null; } else { returnType = introspectedColumn.getFullyQualifiedJavaType(); importedTypes.add(returnType); } } else { returnType = null; } method.setReturnType(returnType); method.setVisibility(JavaVisibility.PUBLIC); method.setName(getDAOMethodNameCalculator().getInsertSelectiveMethodName(introspectedTable)); FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass(); importedTypes.add(parameterType); method.addParameter(new Parameter(parameterType, "record")); // $NON-NLS-1$ for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) { method.addException(fqjt); importedTypes.add(fqjt); } context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); return method; }
private void addParameterizedConstructor(TopLevelClass topLevelClass) { Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setConstructor(true); method.setName(topLevelClass.getType().getShortName()); context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) { method.addParameter( new Parameter( introspectedColumn.getFullyQualifiedJavaType(), introspectedColumn.getJavaProperty())); } boolean comma = false; StringBuilder sb = new StringBuilder(); sb.append("super("); // $NON-NLS-1$ for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) { if (comma) { sb.append(", "); // $NON-NLS-1$ } else { comma = true; } sb.append(introspectedColumn.getJavaProperty()); } sb.append(");"); // $NON-NLS-1$ method.addBodyLine(sb.toString()); for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) { sb.setLength(0); sb.append("this."); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.append(" = "); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.append(';'); method.addBodyLine(sb.toString()); } topLevelClass.addMethod(method); }
private Method getNoValueMethod( IntrospectedColumn introspectedColumn, String nameFragment, String operator) { Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); StringBuilder sb = new StringBuilder(); sb.append(introspectedColumn.getJavaProperty()); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); sb.insert(0, "and"); // $NON-NLS-1$ sb.append(nameFragment); method.setName(sb.toString()); method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); sb.setLength(0); sb.append("addCriterion(\""); // $NON-NLS-1$ sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn)); sb.append(' '); sb.append(operator); sb.append("\");"); // $NON-NLS-1$ method.addBodyLine(sb.toString()); method.addBodyLine("return (Criteria) this;"); // $NON-NLS-1$ return method; }
private XmlElement getMiddleForEachElement(IntrospectedColumn introspectedColumn) { StringBuilder sb = new StringBuilder(); String criteriaAttribute; boolean typeHandled; String typeHandlerString; if (introspectedColumn == null) { criteriaAttribute = "criteria.criteria"; // $NON-NLS-1$ typeHandled = false; typeHandlerString = null; } else { sb.setLength(0); sb.append("criteria."); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.append("Criteria"); // $NON-NLS-1$ criteriaAttribute = sb.toString(); typeHandled = true; sb.setLength(0); sb.append(",typeHandler="); // $NON-NLS-1$ sb.append(introspectedColumn.getTypeHandler()); typeHandlerString = sb.toString(); } XmlElement middleForEachElement = new XmlElement("foreach"); // $NON-NLS-1$ middleForEachElement.addAttribute( new Attribute("collection", criteriaAttribute)); // $NON-NLS-1$ middleForEachElement.addAttribute( new Attribute("item", "criterion")); // $NON-NLS-1$ //$NON-NLS-2$ XmlElement chooseElement = new XmlElement("choose"); // $NON-NLS-1$ middleForEachElement.addElement(chooseElement); XmlElement when = new XmlElement("when"); // $NON-NLS-1$ when.addAttribute(new Attribute("test", "criterion.noValue")); // $NON-NLS-1$ //$NON-NLS-2$ when.addElement(new TextElement("and ${criterion.condition}")); // $NON-NLS-1$ chooseElement.addElement(when); when = new XmlElement("when"); // $NON-NLS-1$ when.addAttribute(new Attribute("test", "criterion.singleValue")); // $NON-NLS-1$ //$NON-NLS-2$ sb.setLength(0); sb.append("and ${criterion.condition} #{criterion.value"); // $NON-NLS-1$ if (typeHandled) { sb.append(typeHandlerString); } sb.append('}'); when.addElement(new TextElement(sb.toString())); chooseElement.addElement(when); when = new XmlElement("when"); // $NON-NLS-1$ when.addAttribute(new Attribute("test", "criterion.betweenValue")); // $NON-NLS-1$ //$NON-NLS-2$ sb.setLength(0); sb.append("and ${criterion.condition} #{criterion.value"); // $NON-NLS-1$ if (typeHandled) { sb.append(typeHandlerString); } sb.append("} and #{criterion.secondValue"); // $NON-NLS-1$ if (typeHandled) { sb.append(typeHandlerString); } sb.append('}'); when.addElement(new TextElement(sb.toString())); chooseElement.addElement(when); when = new XmlElement("when"); // $NON-NLS-1$ when.addAttribute(new Attribute("test", "criterion.listValue")); // $NON-NLS-1$ //$NON-NLS-2$ when.addElement(new TextElement("and ${criterion.condition}")); // $NON-NLS-1$ XmlElement innerForEach = new XmlElement("foreach"); // $NON-NLS-1$ innerForEach.addAttribute( 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", ",")); // $NON-NLS-1$ //$NON-NLS-2$ sb.setLength(0); sb.append("#{listItem"); // $NON-NLS-1$ if (typeHandled) { sb.append(typeHandlerString); } sb.append('}'); innerForEach.addElement(new TextElement(sb.toString())); when.addElement(innerForEach); chooseElement.addElement(when); return middleForEachElement; }
@Override public void addElements(XmlElement parentElement) { XmlElement answer = new XmlElement("insert"); // $NON-NLS-1$ answer.addAttribute( new Attribute("id", introspectedTable.getInsertSelectiveStatementId())); // $NON-NLS-1$ FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass(); answer.addAttribute( new Attribute( "parameterClass", //$NON-NLS-1$ parameterType.getFullyQualifiedName())); context.getCommentGenerator().addComment(answer); GeneratedKey gk = introspectedTable.getGeneratedKey(); if (gk != null && gk.isPlacedBeforeInsertInIbatis2()) { IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn()); // if the column is null, then it's a configuration error. The // warning has already been reported if (introspectedColumn != null) { // pre-generated key answer.addElement(getSelectKey(introspectedColumn, gk)); } } StringBuilder sb = new StringBuilder(); sb.append("insert into "); // $NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime()); answer.addElement(new TextElement(sb.toString())); XmlElement insertElement = new XmlElement("dynamic"); // $NON-NLS-1$ insertElement.addAttribute(new Attribute("prepend", "(")); // $NON-NLS-1$ //$NON-NLS-2$ answer.addElement(insertElement); answer.addElement(new TextElement("values")); // $NON-NLS-1$ XmlElement valuesElement = new XmlElement("dynamic"); // $NON-NLS-1$ valuesElement.addAttribute(new Attribute("prepend", "(")); // $NON-NLS-1$ //$NON-NLS-2$ answer.addElement(valuesElement); for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) { if (introspectedColumn.isIdentity()) { // cannot set values on identity fields continue; } XmlElement insertNotNullElement = new XmlElement("isNotNull"); // $NON-NLS-1$ insertNotNullElement.addAttribute(new Attribute("prepend", ",")); // $NON-NLS-1$ //$NON-NLS-2$ insertNotNullElement.addAttribute( new Attribute("property", introspectedColumn.getJavaProperty())); // $NON-NLS-1$ insertNotNullElement.addElement( new TextElement(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn))); insertElement.addElement(insertNotNullElement); XmlElement valuesNotNullElement = new XmlElement("isNotNull"); // $NON-NLS-1$ valuesNotNullElement.addAttribute(new Attribute("prepend", ",")); // $NON-NLS-1$ //$NON-NLS-2$ valuesNotNullElement.addAttribute( new Attribute("property", introspectedColumn.getJavaProperty())); // $NON-NLS-1$ valuesNotNullElement.addElement( new TextElement(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn))); valuesElement.addElement(valuesNotNullElement); } insertElement.addElement(new TextElement(")")); // $NON-NLS-1$ valuesElement.addElement(new TextElement(")")); // $NON-NLS-1$ if (gk != null && !gk.isPlacedBeforeInsertInIbatis2()) { IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn()); // if the column is null, then it's a configuration error. The // 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); } }
private InnerClass getGeneratedCriteriaInnerClass(TopLevelClass topLevelClass) { Field field; Method method; InnerClass answer = new InnerClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance()); answer.setVisibility(JavaVisibility.PROTECTED); answer.setStatic(true); answer.setAbstract(true); context.getCommentGenerator().addClassComment(answer, introspectedTable); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("GeneratedCriteria"); // $NON-NLS-1$ method.setConstructor(true); method.addBodyLine("super();"); // $NON-NLS-1$ method.addBodyLine("criteria = new ArrayList<Criterion>();"); // $NON-NLS-1$ answer.addMethod(method); List<String> criteriaLists = new ArrayList<String>(); criteriaLists.add("criteria"); // $NON-NLS-1$ for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) { if (stringHasValue(introspectedColumn.getTypeHandler())) { String name = addtypeHandledObjectsAndMethods(introspectedColumn, method, answer); criteriaLists.add(name); } } // now generate the isValid method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("isValid"); // $NON-NLS-1$ method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); StringBuilder sb = new StringBuilder(); Iterator<String> strIter = criteriaLists.iterator(); sb.append("return "); // $NON-NLS-1$ sb.append(strIter.next()); sb.append(".size() > 0"); // $NON-NLS-1$ if (!strIter.hasNext()) { sb.append(';'); } method.addBodyLine(sb.toString()); while (strIter.hasNext()) { sb.setLength(0); OutputUtilities.javaIndent(sb, 1); sb.append("|| "); // $NON-NLS-1$ sb.append(strIter.next()); sb.append(".size() > 0"); // $NON-NLS-1$ if (!strIter.hasNext()) { sb.append(';'); } method.addBodyLine(sb.toString()); } answer.addMethod(method); // now generate the getAllCriteria method if (criteriaLists.size() > 1) { field = new Field(); field.setName("allCriteria"); // $NON-NLS-1$ field.setType(new FullyQualifiedJavaType("List<Criterion>")); // $NON-NLS-1$ field.setVisibility(JavaVisibility.PROTECTED); answer.addField(field); } method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("getAllCriteria"); // $NON-NLS-1$ method.setReturnType(new FullyQualifiedJavaType("List<Criterion>")); // $NON-NLS-1$ if (criteriaLists.size() < 2) { method.addBodyLine("return criteria;"); // $NON-NLS-1$ } else { method.addBodyLine("if (allCriteria == null) {"); // $NON-NLS-1$ method.addBodyLine("allCriteria = new ArrayList<Criterion>();"); // $NON-NLS-1$ strIter = criteriaLists.iterator(); while (strIter.hasNext()) { method.addBodyLine(String.format("allCriteria.addAll(%s);", strIter.next())); // $NON-NLS-1$ } method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine("return allCriteria;"); // $NON-NLS-1$ } answer.addMethod(method); // now we need to generate the methods that will be used in the SqlMap // to generate the dynamic where clause topLevelClass.addImportedType(FullyQualifiedJavaType.getNewListInstance()); topLevelClass.addImportedType(FullyQualifiedJavaType.getNewArrayListInstance()); field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); FullyQualifiedJavaType listOfCriterion = new FullyQualifiedJavaType("java.util.List<Criterion>"); // $NON-NLS-1$ field.setType(listOfCriterion); field.setName("criteria"); // $NON-NLS-1$ answer.addField(field); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(field.getType()); method.setName(getGetterMethodName(field.getName(), field.getType())); method.addBodyLine("return criteria;"); // $NON-NLS-1$ answer.addMethod(method); // now add the methods for simplifying the individual field set methods method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterion"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addBodyLine("if (condition == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Value for condition cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine("criteria.add(new Criterion(condition));"); // $NON-NLS-1$ if (criteriaLists.size() > 1) { method.addBodyLine("allCriteria = null;"); // $NON-NLS-1$ } answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterion"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (value == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine("criteria.add(new Criterion(condition, value));"); // $NON-NLS-1$ if (criteriaLists.size() > 1) { method.addBodyLine("allCriteria = null;"); // $NON-NLS-1$ } answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterion"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value1")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value2")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (value1 == null || value2 == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine("criteria.add(new Criterion(condition, value1, value2));"); // $NON-NLS-1$ if (criteriaLists.size() > 1) { method.addBodyLine("allCriteria = null;"); // $NON-NLS-1$ } answer.addMethod(method); FullyQualifiedJavaType listOfDates = new FullyQualifiedJavaType("java.util.List<java.util.Date>"); // $NON-NLS-1$ if (introspectedTable.hasJDBCDateColumns()) { topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance()); topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance()); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterionForJDBCDate"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getDateInstance(), "value")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (value == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine( "addCriterion(condition, new java.sql.Date(value.getTime()), property);"); //$NON-NLS-1$ answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterionForJDBCDate"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter(new Parameter(listOfDates, "values")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (values == null || values.size() == 0) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine( "List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();"); //$NON-NLS-1$ method.addBodyLine("Iterator<Date> iter = values.iterator();"); // $NON-NLS-1$ method.addBodyLine("while (iter.hasNext()) {"); // $NON-NLS-1$ method.addBodyLine("dateList.add(new java.sql.Date(iter.next().getTime()));"); // $NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine("addCriterion(condition, dateList, property);"); // $NON-NLS-1$ answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterionForJDBCDate"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getDateInstance(), "value1")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getDateInstance(), "value2")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (value1 == null || value2 == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine( "addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);"); //$NON-NLS-1$ answer.addMethod(method); } if (introspectedTable.hasJDBCTimeColumns()) { topLevelClass.addImportedType(FullyQualifiedJavaType.getDateInstance()); topLevelClass.addImportedType(FullyQualifiedJavaType.getNewIteratorInstance()); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterionForJDBCTime"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getDateInstance(), "value")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (value == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine( "addCriterion(condition, new java.sql.Time(value.getTime()), property);"); //$NON-NLS-1$ answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterionForJDBCTime"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter(new Parameter(listOfDates, "values")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (values == null || values.size() == 0) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine( "List<java.sql.Time> timeList = new ArrayList<java.sql.Time>();"); //$NON-NLS-1$ method.addBodyLine("Iterator<Date> iter = values.iterator();"); // $NON-NLS-1$ method.addBodyLine("while (iter.hasNext()) {"); // $NON-NLS-1$ method.addBodyLine("timeList.add(new java.sql.Time(iter.next().getTime()));"); // $NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine("addCriterion(condition, timeList, property);"); // $NON-NLS-1$ answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("addCriterionForJDBCTime"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getDateInstance(), "value1")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getDateInstance(), "value2")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (value1 == null || value2 == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine( "addCriterion(condition, new java.sql.Time(value1.getTime()), new java.sql.Time(value2.getTime()), property);"); //$NON-NLS-1$ answer.addMethod(method); } for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) { topLevelClass.addImportedType(introspectedColumn.getFullyQualifiedJavaType()); // here we need to add the individual methods for setting the // conditions for a field answer.addMethod(getSetNullMethod(introspectedColumn)); answer.addMethod(getSetNotNullMethod(introspectedColumn)); answer.addMethod(getSetEqualMethod(introspectedColumn)); answer.addMethod(getSetNotEqualMethod(introspectedColumn)); answer.addMethod(getSetGreaterThanMethod(introspectedColumn)); answer.addMethod(getSetGreaterThenOrEqualMethod(introspectedColumn)); answer.addMethod(getSetLessThanMethod(introspectedColumn)); answer.addMethod(getSetLessThanOrEqualMethod(introspectedColumn)); if (introspectedColumn.isJdbcCharacterColumn()) { answer.addMethod(getSetLikeMethod(introspectedColumn)); answer.addMethod(getSetNotLikeMethod(introspectedColumn)); } answer.addMethod(getSetInOrNotInMethod(introspectedColumn, true)); answer.addMethod(getSetInOrNotInMethod(introspectedColumn, false)); answer.addMethod(getSetBetweenOrNotBetweenMethod(introspectedColumn, true)); answer.addMethod(getSetBetweenOrNotBetweenMethod(introspectedColumn, false)); } return answer; }
/** * This method adds all the extra methods and fields required to support a user defined type * handler on some column. * * @param introspectedColumn * @param constructor * @param innerClass * @return the name of the List added to the class by this method */ private String addtypeHandledObjectsAndMethods( IntrospectedColumn introspectedColumn, Method constructor, InnerClass innerClass) { String answer; StringBuilder sb = new StringBuilder(); // add new private field and public accessor in the class sb.setLength(0); sb.append(introspectedColumn.getJavaProperty()); sb.append("Criteria"); // $NON-NLS-1$ answer = sb.toString(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(new FullyQualifiedJavaType("java.util.List<Criterion>")); // $NON-NLS-1$ field.setName(answer); innerClass.addField(field); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(field.getType()); method.setName(getGetterMethodName(field.getName(), field.getType())); sb.insert(0, "return "); // $NON-NLS-1$ sb.append(';'); method.addBodyLine(sb.toString()); innerClass.addMethod(method); // add constructor initialization sb.setLength(0); sb.append(field.getName()); sb.append(" = new ArrayList<Criterion>();"); // $NON-NLS-1$; constructor.addBodyLine(sb.toString()); // now add the methods for simplifying the individual field set methods method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); sb.setLength(0); sb.append("add"); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.setCharAt(3, Character.toUpperCase(sb.charAt(3))); sb.append("Criterion"); // $NON-NLS-1$ method.setName(sb.toString()); method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (value == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine( String.format( "%s.add(new Criterion(condition, value, \"%s\"));", //$NON-NLS-1$ field.getName(), introspectedColumn.getTypeHandler())); method.addBodyLine("allCriteria = null;"); // $NON-NLS-1$ innerClass.addMethod(method); sb.setLength(0); sb.append("add"); // $NON-NLS-1$ sb.append(introspectedColumn.getJavaProperty()); sb.setCharAt(3, Character.toUpperCase(sb.charAt(3))); sb.append("Criterion"); // $NON-NLS-1$ method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName(sb.toString()); method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(introspectedColumn.getFullyQualifiedJavaType(), "value1")); // $NON-NLS-1$ method.addParameter( new Parameter(introspectedColumn.getFullyQualifiedJavaType(), "value2")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "property")); // $NON-NLS-1$ method.addBodyLine("if (value1 == null || value2 == null) {"); // $NON-NLS-1$ method.addBodyLine( "throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine( String.format( "%s.add(new Criterion(condition, value1, value2, \"%s\"));", //$NON-NLS-1$ field.getName(), introspectedColumn.getTypeHandler())); method.addBodyLine("allCriteria = null;"); // $NON-NLS-1$ innerClass.addMethod(method); return answer; }