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); }
@Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); Method method = new Method(); FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance(); importedTypes.add(returnType); FullyQualifiedJavaType listType; listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); importedTypes.add(listType); returnType.addTypeArgument(listType); method.setReturnType(returnType); method.setVisibility(JavaVisibility.PUBLIC); method.setName(introspectedTable.getSelectSelectiveStatementId()); FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass(); importedTypes.add(parameterType); method.addParameter(new Parameter(parameterType, "record")); // $NON-NLS-1$ context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); addMapperAnnotations(interfaze, method); if (context .getPlugins() .clientSelectSelectiveMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } }
/** 添加方法 */ protected void addMethod(TopLevelClass topLevelClass) { Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("setSuccess"); method.addParameter( new Parameter(FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "success")); method.addBodyLine("this.success = success;"); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); method.setName("isSuccess"); method.addBodyLine("return success;"); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("setMessage"); method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "message")); method.addBodyLine("this.message = message;"); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getStringInstance()); method.setName("getMessage"); method.addBodyLine("return message;"); topLevelClass.addMethod(method); }
@Override public void addClassElements(TopLevelClass topLevelClass) { Set<String> staticImports = new TreeSet<String>(); Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); 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"); // $NON-NLS-1$ importedTypes.add(new FullyQualifiedJavaType("java.util.Map")); // $NON-NLS-1$ Method method = new Method(getMethodName()); method.setReturnType(FullyQualifiedJavaType.getStringInstance()); method.setVisibility(JavaVisibility.PUBLIC); method.addParameter( new Parameter( new FullyQualifiedJavaType( "java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$ "parameter")); //$NON-NLS-1$ // method.addJavaDocLine("根据主键更新所有字段的数据,不含BLOB字段。"); context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); method.addBodyLine("BEGIN();"); // $NON-NLS-1$ method.addBodyLine( String.format( "UPDATE(\"%s\");", //$NON-NLS-1$ escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime()))); method.addBodyLine(""); // $NON-NLS-1$ for (IntrospectedColumn introspectedColumn : getColumns()) { StringBuilder sb = new StringBuilder(); sb.append(getParameterClause(introspectedColumn)); sb.insert(2, "record."); // $NON-NLS-1$ method.addBodyLine( String.format( "SET(\"%s = %s\");", //$NON-NLS-1$ escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)), sb.toString())); } method.addBodyLine(""); // $NON-NLS-1$ FullyQualifiedJavaType example = new FullyQualifiedJavaType(introspectedTable.getExampleType()); importedTypes.add(example); method.addBodyLine( String.format( "%s example = (%s) parameter.get(\"example\");", //$NON-NLS-1$ example.getShortName(), example.getShortName())); method.addBodyLine("applyWhere(example, true);"); // $NON-NLS-1$ method.addBodyLine("return SQL();"); // $NON-NLS-1$ if (callPlugins(method, topLevelClass)) { topLevelClass.addStaticImports(staticImports); topLevelClass.addImportedTypes(importedTypes); topLevelClass.addMethod(method); } }
private void addLimitParam( TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) { CommentGenerator commentGenerator = context.getCommentGenerator(); Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(FullyQualifiedJavaType.getIntInstance()); field.setName(name); field.setInitializationString("0"); commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); char c = name.charAt(0); String camel = Character.toUpperCase(c) + name.substring(1); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("set" + camel); method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), name)); method.addBodyLine("this." + name + "=" + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getIntInstance()); method.setName("get" + camel); method.addBodyLine("return " + name + ";"); commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); }
/** * @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; }
@Override public void addImplementationElements(TopLevelClass topLevelClass) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); Method method = getMethodShell(importedTypes); FullyQualifiedJavaType returnType = method.getReturnType(); StringBuilder sb = new StringBuilder(); if (returnType != null) { sb.append("Object newKey = "); // $NON-NLS-1$ } sb.append( daoTemplate.getInsertMethod( introspectedTable.getIbatis2SqlMapNamespace(), introspectedTable.getInsertSelectiveStatementId(), "record")); //$NON-NLS-1$ method.addBodyLine(sb.toString()); if (returnType != null) { if ("Object".equals(returnType.getShortName())) { // $NON-NLS-1$ // no need to cast if the return type is Object method.addBodyLine("return newKey;"); // $NON-NLS-1$ } else { sb.setLength(0); if (returnType.isPrimitive()) { PrimitiveTypeWrapper ptw = returnType.getPrimitiveTypeWrapper(); sb.append("return (("); // $NON-NLS-1$ sb.append(ptw.getShortName()); sb.append(") newKey"); // $NON-NLS-1$ sb.append(")."); // $NON-NLS-1$ sb.append(ptw.getToPrimitiveMethod()); sb.append(';'); } else { sb.append("return ("); // $NON-NLS-1$ sb.append(returnType.getShortName()); sb.append(") newKey;"); // $NON-NLS-1$ } method.addBodyLine(sb.toString()); } } if (context .getPlugins() .clientInsertSelectiveMethodGenerated(method, topLevelClass, introspectedTable)) { topLevelClass.addImportedTypes(importedTypes); topLevelClass.addMethod(method); } }
@Override public void addMapperAnnotations(Interface interfaze, Method method) { FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(introspectedTable.getMyBatis3SqlProviderType()); interfaze.addImportedType( new FullyQualifiedJavaType("org.apache.ibatis.annotations.DeleteProvider")); // $NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append("@DeleteProvider(type="); // $NON-NLS-1$ sb.append(fqjt.getShortName()); sb.append(".class, method=\""); // $NON-NLS-1$ sb.append(introspectedTable.getDeleteByExampleStatementId()); sb.append("\")"); // $NON-NLS-1$ method.addAnnotation(sb.toString()); }
/** * 添加字段 * * @param topLevelClass */ protected void addField(TopLevelClass topLevelClass) { // 添加 success Field field = new Field(); field.setName("success"); // 设置变量名 field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); // 类型 field.setVisibility(JavaVisibility.PRIVATE); addComment(field, "执行结果"); topLevelClass.addField(field); // 设置结果 field = new Field(); field.setName("message"); // 设置变量名 field.setType(FullyQualifiedJavaType.getStringInstance()); // 类型 field.setVisibility(JavaVisibility.PRIVATE); addComment(field, "消息结果"); topLevelClass.addField(field); }
/** 添加方法 */ protected Method getOtherInteger( String methodName, IntrospectedTable introspectedTable, String tableName, int type) { Method method = new Method(); method.setName(methodName); method.setReturnType(FullyQualifiedJavaType.getIntInstance()); String params = addParams(introspectedTable, method, type); method.setVisibility(JavaVisibility.PUBLIC); StringBuilder sb = new StringBuilder(); // method.addBodyLine("try {"); sb.append("return this."); sb.append(getDaoShort()); if (introspectedTable.hasBLOBColumns() && (!"updateByPrimaryKeySelective".equals(methodName) && !"deleteByPrimaryKey".equals(methodName) && !"deleteByExample".equals(methodName) && !"updateByExampleSelective".equals(methodName))) { sb.append(methodName + "WithoutBLOBs"); } else { sb.append(methodName); } sb.append("("); sb.append(params); sb.append(");"); method.addBodyLine(sb.toString()); return method; }
private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) { FullyQualifiedJavaType parameterType; if (introspectedTable.getRules().generateRecordWithBLOBsClass()) { parameterType = new FullyQualifiedJavaType(introspectedTable.getRecordWithBLOBsType()); } else { parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); } importedTypes.add(parameterType); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getIntInstance()); method.setName( getDAOMethodNameCalculator().getUpdateByPrimaryKeyWithBLOBsMethodName(introspectedTable)); 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 InnerClass getCriteriaInnerClass(TopLevelClass topLevelClass) { Method method; InnerClass answer = new InnerClass(FullyQualifiedJavaType.getCriteriaInstance()); answer.setVisibility(JavaVisibility.PUBLIC); answer.setStatic(true); answer.setSuperClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance()); context.getCommentGenerator().addClassComment(answer, introspectedTable, true); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("Criteria"); // $NON-NLS-1$ method.setConstructor(true); method.addBodyLine("super();"); // $NON-NLS-1$ answer.addMethod(method); return answer; }
/** * 添加字段 * * @param topLevelClass */ protected void addField(TopLevelClass topLevelClass, String tableName) { // 添加 dao Field field = new Field(); field.setName(toLowerCase(daoType.getShortName())); // 设置变量名 topLevelClass.addImportedType(daoType); field.setType(daoType); // 类型 field.setVisibility(JavaVisibility.PRIVATE); if (enableAnnotation) { field.addAnnotation("@Autowired"); } topLevelClass.addField(field); }
private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) { FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType()); importedTypes.add(type); importedTypes.add(FullyQualifiedJavaType.getNewListInstance()); Method method = new Method(); method.setVisibility(getExampleMethodVisibility()); FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance(); ; if (generateForJava5) { FullyQualifiedJavaType fqjt; if (introspectedTable.getRules().generateBaseRecordClass()) { fqjt = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); } else if (introspectedTable.getRules().generatePrimaryKeyClass()) { fqjt = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType()); } else { throw new RuntimeException(getString("RuntimeError.12")); // $NON-NLS-1$ } importedTypes.add(fqjt); returnType.addTypeArgument(fqjt); } method.setReturnType(returnType); method.setName( getDAOMethodNameCalculator().getSelectByExampleWithoutBLOBsMethodName(introspectedTable)); method.addParameter(new Parameter(type, "example")); // $NON-NLS-1$ for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) { method.addException(fqjt); importedTypes.add(fqjt); } context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); return method; }
@Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType()); importedTypes.add(type); importedTypes.add(FullyQualifiedJavaType.getNewListInstance()); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance(); FullyQualifiedJavaType listType; if (introspectedTable.getRules().generateBaseRecordClass()) { listType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()); } else if (introspectedTable.getRules().generatePrimaryKeyClass()) { listType = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyType()); } else { throw new RuntimeException(getString("RuntimeError.12")); // $NON-NLS-1$ } importedTypes.add(listType); returnType.addTypeArgument(listType); method.setReturnType(returnType); method.setName(introspectedTable.getSelectByExampleStatementId()); method.addParameter(new Parameter(type, "example")); // $NON-NLS-1$ context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); addMapperAnnotations(interfaze, method); if (context .getPlugins() .clientSelectByExampleWithoutBLOBsMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(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; }
/** 添加方法 */ protected Method countByExample(IntrospectedTable introspectedTable, String tableName) { Method method = new Method(); method.setName("countByExample"); method.setReturnType(FullyQualifiedJavaType.getIntInstance()); method.addParameter(new Parameter(pojoCriteriaType, "example")); method.setVisibility(JavaVisibility.PUBLIC); StringBuilder sb = new StringBuilder(); sb.append("int count = this."); sb.append(getDaoShort()); sb.append("countByExample"); sb.append("("); sb.append("example"); sb.append(");"); method.addBodyLine(sb.toString()); method.addBodyLine("logger.debug(\"count: {}\", count);"); method.addBodyLine("return count;"); return 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; }
@Override public void addInterfaceElements(Interface interfaze) { Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>(); FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType()); importedTypes.add(type); Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getIntInstance()); method.setName(introspectedTable.getDeleteByExampleStatementId()); method.addParameter(new Parameter(type, "condition")); // $NON-NLS-1$ context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable); addMapperAnnotations(interfaze, method); if (context .getPlugins() .clientDeleteByExampleMethodGenerated(method, interfaze, introspectedTable)) { interfaze.addImportedTypes(importedTypes); interfaze.addMethod(method); } }
private InnerClass getCriterionInnerClass(TopLevelClass topLevelClass) { Field field; Method method; InnerClass answer = new InnerClass(new FullyQualifiedJavaType("Criterion")); // $NON-NLS-1$ answer.setVisibility(JavaVisibility.PUBLIC); answer.setStatic(true); context.getCommentGenerator().addClassComment(answer, introspectedTable); field = new Field(); field.setName("condition"); // $NON-NLS-1$ field.setType(FullyQualifiedJavaType.getStringInstance()); field.setVisibility(JavaVisibility.PRIVATE); answer.addField(field); answer.addMethod(getGetter(field)); field = new Field(); field.setName("value"); // $NON-NLS-1$ field.setType(FullyQualifiedJavaType.getObjectInstance()); field.setVisibility(JavaVisibility.PRIVATE); answer.addField(field); answer.addMethod(getGetter(field)); field = new Field(); field.setName("secondValue"); // $NON-NLS-1$ field.setType(FullyQualifiedJavaType.getObjectInstance()); field.setVisibility(JavaVisibility.PRIVATE); answer.addField(field); answer.addMethod(getGetter(field)); field = new Field(); field.setName("noValue"); // $NON-NLS-1$ field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); field.setVisibility(JavaVisibility.PRIVATE); answer.addField(field); answer.addMethod(getGetter(field)); field = new Field(); field.setName("singleValue"); // $NON-NLS-1$ field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); field.setVisibility(JavaVisibility.PRIVATE); answer.addField(field); answer.addMethod(getGetter(field)); field = new Field(); field.setName("betweenValue"); // $NON-NLS-1$ field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); field.setVisibility(JavaVisibility.PRIVATE); answer.addField(field); answer.addMethod(getGetter(field)); field = new Field(); field.setName("listValue"); // $NON-NLS-1$ field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); field.setVisibility(JavaVisibility.PRIVATE); answer.addField(field); answer.addMethod(getGetter(field)); field = new Field(); field.setName("typeHandler"); // $NON-NLS-1$ field.setType(FullyQualifiedJavaType.getStringInstance()); field.setVisibility(JavaVisibility.PRIVATE); answer.addField(field); answer.addMethod(getGetter(field)); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("Criterion"); // $NON-NLS-1$ method.setConstructor(true); method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addBodyLine("super();"); // $NON-NLS-1$ method.addBodyLine("this.condition = condition;"); // $NON-NLS-1$ method.addBodyLine("this.typeHandler = null;"); // $NON-NLS-1$ method.addBodyLine("this.noValue = true;"); // $NON-NLS-1$ answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("Criterion"); // $NON-NLS-1$ method.setConstructor(true); 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(), "typeHandler")); // $NON-NLS-1$ method.addBodyLine("super();"); // $NON-NLS-1$ method.addBodyLine("this.condition = condition;"); // $NON-NLS-1$ method.addBodyLine("this.value = value;"); // $NON-NLS-1$ method.addBodyLine("this.typeHandler = typeHandler;"); // $NON-NLS-1$ method.addBodyLine("if (value instanceof List<?>) {"); // $NON-NLS-1$ method.addBodyLine("this.listValue = true;"); // $NON-NLS-1$ method.addBodyLine("} else {"); // $NON-NLS-1$ method.addBodyLine("this.singleValue = true;"); // $NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("Criterion"); // $NON-NLS-1$ method.setConstructor(true); method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "condition")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getObjectInstance(), "value")); // $NON-NLS-1$ method.addBodyLine("this(condition, value, null);"); // $NON-NLS-1$ answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("Criterion"); // $NON-NLS-1$ method.setConstructor(true); 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.getObjectInstance(), "secondValue")); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "typeHandler")); // $NON-NLS-1$ method.addBodyLine("super();"); // $NON-NLS-1$ method.addBodyLine("this.condition = condition;"); // $NON-NLS-1$ method.addBodyLine("this.value = value;"); // $NON-NLS-1$ method.addBodyLine("this.secondValue = secondValue;"); // $NON-NLS-1$ method.addBodyLine("this.typeHandler = typeHandler;"); // $NON-NLS-1$ method.addBodyLine("this.betweenValue = true;"); // $NON-NLS-1$ answer.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("Criterion"); // $NON-NLS-1$ method.setConstructor(true); 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.getObjectInstance(), "secondValue")); // $NON-NLS-1$ method.addBodyLine("this(condition, value, secondValue, null);"); // $NON-NLS-1$ answer.addMethod(method); return 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; }
@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); } }
@Override public List<CompilationUnit> getCompilationUnits() { FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable(); progressCallback.startTask(getString("Progress.6", table.toString())); // $NON-NLS-1$ CommentGenerator commentGenerator = context.getCommentGenerator(); FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType()); TopLevelClass topLevelClass = new TopLevelClass(type); topLevelClass.setVisibility(JavaVisibility.PUBLIC); commentGenerator.addJavaFileComment(topLevelClass); // add default constructor Method method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setConstructor(true); method.setName(type.getShortName()); method.addBodyLine("oredCriteria = new ArrayList<Criteria>();"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); // add field, getter, setter for orderby clause Field field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(FullyQualifiedJavaType.getStringInstance()); field.setName("orderByClause"); // $NON-NLS-1$ commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("setOrderByClause"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClause")); // $NON-NLS-1$ method.addBodyLine("this.orderByClause = orderByClause;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getStringInstance()); method.setName("getOrderByClause"); // $NON-NLS-1$ method.addBodyLine("return orderByClause;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); // add field, getter, setter for distinct field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); field.setName("distinct"); // $NON-NLS-1$ commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("setDistinct"); // $NON-NLS-1$ method.addParameter( new Parameter( FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "distinct")); // $NON-NLS-1$ method.addBodyLine("this.distinct = distinct;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance()); method.setName("isDistinct"); // $NON-NLS-1$ method.addBodyLine("return distinct;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); // add field and methods for the list of ored criteria field = new Field(); field.setVisibility(JavaVisibility.PROTECTED); FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("java.util.List<Criteria>"); // $NON-NLS-1$ field.setType(fqjt); field.setName("oredCriteria"); // $NON-NLS-1$ commentGenerator.addFieldComment(field, introspectedTable); topLevelClass.addField(field); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setReturnType(fqjt); method.setName("getOredCriteria"); // $NON-NLS-1$ method.addBodyLine("return oredCriteria;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("or"); // $NON-NLS-1$ method.addParameter( new Parameter(FullyQualifiedJavaType.getCriteriaInstance(), "criteria")); // $NON-NLS-1$ method.addBodyLine("oredCriteria.add(criteria);"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("or"); // $NON-NLS-1$ method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); method.addBodyLine("Criteria criteria = createCriteriaInternal();"); // $NON-NLS-1$ method.addBodyLine("oredCriteria.add(criteria);"); // $NON-NLS-1$ method.addBodyLine("return criteria;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("createCriteria"); // $NON-NLS-1$ method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); method.addBodyLine("Criteria criteria = createCriteriaInternal();"); // $NON-NLS-1$ method.addBodyLine("if (oredCriteria.size() == 0) {"); // $NON-NLS-1$ method.addBodyLine("oredCriteria.add(criteria);"); // $NON-NLS-1$ method.addBodyLine("}"); // $NON-NLS-1$ method.addBodyLine("return criteria;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PROTECTED); method.setName("createCriteriaInternal"); // $NON-NLS-1$ method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance()); method.addBodyLine("Criteria criteria = new Criteria();"); // $NON-NLS-1$ method.addBodyLine("return criteria;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); method = new Method(); method.setVisibility(JavaVisibility.PUBLIC); method.setName("clear"); // $NON-NLS-1$ method.addBodyLine("oredCriteria.clear();"); // $NON-NLS-1$ method.addBodyLine("orderByClause = null;"); // $NON-NLS-1$ method.addBodyLine("distinct = false;"); // $NON-NLS-1$ commentGenerator.addGeneralMethodComment(method, introspectedTable); topLevelClass.addMethod(method); // now generate the inner class that holds the AND conditions topLevelClass.addInnerClass(getGeneratedCriteriaInnerClass(topLevelClass)); topLevelClass.addInnerClass(getCriteriaInnerClass(topLevelClass)); topLevelClass.addInnerClass(getCriterionInnerClass(topLevelClass)); List<CompilationUnit> answer = new ArrayList<CompilationUnit>(); if (context.getPlugins().modelExampleClassGenerated(topLevelClass, introspectedTable)) { answer.add(topLevelClass); } 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; }
private String getDaoShort() { return toLowerCase(daoType.getShortName()) + "."; }