Search in sources :

Example 1 with PostgresRoutineDefinition

use of org.jooq.meta.postgres.PostgresRoutineDefinition in project jOOQ by jOOQ.

the class JavaGenerator method generateRoutine.

protected void generateRoutine(RoutineDefinition routine, JavaWriter out) {
    final SchemaDefinition schema = routine.getSchema();
    final String className = getStrategy().getJavaClassName(routine);
    final String returnTypeFull = (routine.getReturnValue() == null) ? Void.class.getName() : getJavaType(routine.getReturnType(resolver(out)), out);
    final String returnType = (routine.getReturnValue() == null) ? Void.class.getName() : out.ref(returnTypeFull);
    final List<String> returnTypeRef = list((routine.getReturnValue() != null) ? getJavaTypeReference(database, routine.getReturnType(resolver(out)), out) : null);
    final List<String> returnConverter = out.ref(list((routine.getReturnValue() != null) ? routine.getReturnType(resolver(out)).getConverter() : null));
    final List<String> returnBinding = out.ref(list((routine.getReturnValue() != null) ? routine.getReturnType(resolver(out)).getBinding() : null));
    final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(routine, Mode.DEFAULT));
    final String schemaId = out.ref(getStrategy().getFullJavaIdentifier(schema), 2);
    final List<String> packageId = out.ref(getStrategy().getFullJavaIdentifiers(routine.getPackage()), 2);
    printPackage(out, routine);
    if (scala) {
        out.println("object %s {", className);
        for (ParameterDefinition parameter : routine.getAllParameters()) {
            final String paramTypeFull = getJavaType(parameter.getType(resolver(out)), out);
            final String paramType = out.ref(paramTypeFull);
            final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
            final String paramId = out.ref(getStrategy().getJavaIdentifier(parameter), 2);
            final String paramName = parameter.getName();
            final String isDefaulted = parameter.isDefaulted() ? "true" : "false";
            final String isUnnamed = parameter.isUnnamed() ? "true" : "false";
            final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
            final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
            if (!printDeprecationIfUnknownType(out, paramTypeFull))
                out.javadoc("The parameter <code>%s</code>.[[before= ][%s]]", parameter.getQualifiedOutputName(), list(escapeEntities(comment(parameter))));
            out.println("val %s: %s[%s] = %s.createParameter(\"%s\", %s, %s, %s" + converterTemplate(converter) + converterTemplate(binding) + ")", scalaWhitespaceSuffix(paramId), Parameter.class, paramType, Internal.class, escapeString(paramName), paramTypeRef, isDefaulted, isUnnamed, converter, binding);
        }
        out.println("}");
        out.println();
    }
    if (!printDeprecationIfUnknownType(out, returnTypeFull))
        generateRoutineClassJavadoc(routine, out);
    printClassAnnotations(out, routine, Mode.DEFAULT);
    if (scala) {
        out.println("%sclass %s extends %s[%s](\"%s\", %s[[before=, ][%s]][[before=, ][%s]]" + converterTemplate(returnConverter) + converterTemplate(returnBinding) + ")[[before= with ][separator= with ][%s]] {", visibility(), className, AbstractRoutine.class, returnType, escapeString(routine.getName()), schemaId, packageId, returnTypeRef, returnConverter, returnBinding, interfaces);
    } else {
        if (kotlin) {
            out.println("%sopen class %s : %s<%s>(\"%s\", %s[[before=, ][%s]][[before=, ][%s]]" + converterTemplate(returnConverter) + converterTemplate(returnBinding) + ")[[before=, ][%s]] {", visibility(), className, AbstractRoutine.class, returnType, escapeString(routine.getName()), schemaId, packageId, returnTypeRef, returnConverter, returnBinding, interfaces);
        } else {
            out.println("%sclass %s extends %s<%s>[[before= implements ][%s]] {", visibility(), className, AbstractRoutine.class, returnType, interfaces);
            out.printSerial();
        }
        if (kotlin)
            out.println("%scompanion object {", visibility());
        for (ParameterDefinition parameter : routine.getAllParameters()) {
            final String paramTypeFull = getJavaType(parameter.getType(resolver(out)), out);
            final String paramType = out.ref(paramTypeFull);
            final String paramTypeRef = getJavaTypeReference(parameter.getDatabase(), parameter.getType(resolver(out)), out);
            final String paramId = getStrategy().getJavaIdentifier(parameter);
            final String paramName = parameter.getName();
            final String isDefaulted = parameter.isDefaulted() ? "true" : "false";
            final String isUnnamed = parameter.isUnnamed() ? "true" : "false";
            final List<String> converter = out.ref(list(parameter.getType(resolver(out)).getConverter()));
            final List<String> binding = out.ref(list(parameter.getType(resolver(out)).getBinding()));
            if (!printDeprecationIfUnknownType(out, paramTypeFull))
                out.javadoc("The parameter <code>%s</code>.[[before= ][%s]]", parameter.getQualifiedOutputName(), list(escapeEntities(comment(parameter))));
            if (kotlin)
                out.println("%sval %s: %s<%s?> = %s.createParameter(\"%s\", %s, %s, %s" + converterTemplate(converter) + converterTemplate(binding) + ")", visibility(), scalaWhitespaceSuffix(paramId), Parameter.class, paramType, Internal.class, escapeString(paramName), paramTypeRef, isDefaulted, isUnnamed, converter, binding);
            else
                out.println("%sstatic final %s<%s> %s = %s.createParameter(\"%s\", %s, %s, %s" + converterTemplate(converter) + converterTemplate(binding) + ");", visibility(), Parameter.class, paramType, paramId, Internal.class, escapeString(paramName), paramTypeRef, isDefaulted, isUnnamed, converter, binding);
        }
        if (kotlin)
            out.println("}").println();
    }
    if (scala) {
        out.println("{");
    } else if (kotlin) {
        out.println("init {");
    } else {
        out.javadoc("Create a new routine call instance");
        out.println("%s%s() {", visibility(), className);
        out.println("super(\"%s\", %s[[before=, ][%s]][[before=, ][%s]]" + converterTemplate(returnConverter) + converterTemplate(returnBinding) + ");", routine.getName(), schemaId, packageId, returnTypeRef, returnConverter, returnBinding);
        if (routine.getAllParameters().size() > 0)
            out.println();
    }
    for (ParameterDefinition parameter : routine.getAllParameters()) {
        final String paramId = getStrategy().getJavaIdentifier(parameter);
        if (parameter.equals(routine.getReturnValue())) {
            if (parameter.isSynthetic()) {
                if (scala)
                    out.println("setSyntheticReturnParameter(%s.%s)", className, paramId);
                else if (kotlin)
                    out.println("syntheticReturnParameter = %s", paramId);
                else
                    out.println("setSyntheticReturnParameter(%s);", paramId);
            } else {
                if (scala)
                    out.println("setReturnParameter(%s.%s)", className, paramId);
                else if (kotlin)
                    out.println("returnParameter = %s", paramId);
                else
                    out.println("setReturnParameter(%s);", paramId);
            }
        } else if (routine.getInParameters().contains(parameter)) {
            if (routine.getOutParameters().contains(parameter)) {
                if (scala)
                    out.println("addInOutParameter(%s.%s)", className, paramId);
                else if (kotlin)
                    out.println("addInOutParameter(%s)", paramId);
                else
                    out.println("addInOutParameter(%s);", paramId);
            } else {
                if (scala)
                    out.println("addInParameter(%s.%s)", className, paramId);
                else if (kotlin)
                    out.println("addInParameter(%s)", paramId);
                else
                    out.println("addInParameter(%s);", paramId);
            }
        } else {
            if (scala)
                out.println("addOutParameter(%s.%s)", className, paramId);
            else if (kotlin)
                out.println("addOutParameter(%s)", paramId);
            else
                out.println("addOutParameter(%s);", paramId);
        }
    }
    if (routine.getOverload() != null)
        out.println("setOverloaded(true)%s", semicolon);
    if (routine instanceof PostgresRoutineDefinition)
        if (((PostgresRoutineDefinition) routine).isProcedure())
            out.println("setSQLUsable(false)%s", semicolon);
    out.println("}");
    for (ParameterDefinition parameter : routine.getInParameters()) {
        final String setterReturnType = generateFluentSetters() ? className : tokenVoid;
        final String setter = getStrategy().getJavaSetterName(parameter, Mode.DEFAULT);
        final String numberValue = parameter.getType(resolver(out)).isGenericNumberType() ? "Number" : "Value";
        final String numberField = parameter.getType(resolver(out)).isGenericNumberType() ? "Number" : "Field";
        final String paramId = getStrategy().getJavaIdentifier(parameter);
        final String paramName = "value".equals(paramId) ? "value_" : "value";
        out.javadoc("Set the <code>%s</code> parameter IN value to the routine", parameter.getOutputName());
        if (scala) {
            out.println("%sdef %s(%s: %s) : Unit = set%s(%s.%s, %s)", visibility(), setter, scalaWhitespaceSuffix(paramName), refNumberType(out, parameter.getType(resolver(out))), numberValue, className, paramId, paramName);
        } else if (kotlin) {
            out.println("%sfun %s(%s: %s?): Unit = set%s(%s, %s)", visibility(), setter, paramName, refNumberType(out, parameter.getType(resolver(out))), numberValue, paramId, paramName);
        } else {
            out.println("%svoid %s(%s %s) {", visibility(), setter, varargsIfArray(refNumberType(out, parameter.getType(resolver(out)))), paramName);
            out.println("set%s(%s, %s);", numberValue, paramId, paramName);
            out.println("}");
        }
        if (routine.isSQLUsable()) {
            out.javadoc("Set the <code>%s</code> parameter to the function to be used with a {@link org.jooq.Select} statement", parameter.getOutputName());
            if (scala) {
                out.println("%sdef %s(field: %s[%s]): %s = {", visibility(), setter, Field.class, refExtendsNumberType(out, parameter.getType(resolver(out))), setterReturnType);
                out.println("set%s(%s.%s, field)", numberField, className, paramId);
                if (generateFluentSetters())
                    out.println("this");
                out.println("}");
            } else if (kotlin) {
                out.println("%sfun %s(field: %s<%s?>): %s {", visibility(), setter, Field.class, refExtendsNumberType(out, parameter.getType(resolver(out))), setterReturnType);
                out.println("set%s(%s, field)", numberField, paramId);
                if (generateFluentSetters())
                    out.println("return this");
                out.println("}");
            } else {
                out.println("%s%s %s(%s<%s> field) {", visibility(), setterReturnType, setter, Field.class, refExtendsNumberType(out, parameter.getType(resolver(out))));
                out.println("set%s(%s, field);", numberField, paramId);
                if (generateFluentSetters())
                    out.println("return this;");
                out.println("}");
            }
        }
    }
    for (ParameterDefinition parameter : routine.getAllParameters()) {
        boolean isReturnValue = parameter.equals(routine.getReturnValue());
        boolean isOutParameter = routine.getOutParameters().contains(parameter);
        if (isOutParameter && !isReturnValue) {
            final String paramName = parameter.getOutputName();
            final String paramTypeFull = getJavaType(parameter.getType(resolver(out)), out);
            final String paramType = out.ref(paramTypeFull);
            final String paramGetter = getStrategy().getJavaGetterName(parameter, Mode.DEFAULT);
            final String paramId = getStrategy().getJavaIdentifier(parameter);
            if (!printDeprecationIfUnknownType(out, paramTypeFull))
                out.javadoc("Get the <code>%s</code> parameter OUT value from the routine", paramName);
            if (scala) {
                out.println("%sdef %s: %s = get(%s.%s)", visibility(), scalaWhitespaceSuffix(paramGetter), paramType, className, paramId);
            } else if (kotlin) {
                out.println("%sfun %s(): %s? = get(%s)", visibility(), paramGetter, paramType, paramId);
            } else {
                out.println("%s%s %s() {", visibility(), paramType, paramGetter);
                out.println("return get(%s);", paramId);
                out.println("}");
            }
        }
    }
    generateRoutineClassFooter(routine, out);
    out.println("}");
}
Also used : SelectField(org.jooq.SelectField) TableField(org.jooq.TableField) Field(org.jooq.Field) UDTField(org.jooq.UDTField) OrderField(org.jooq.OrderField) SchemaDefinition(org.jooq.meta.SchemaDefinition) PostgresRoutineDefinition(org.jooq.meta.postgres.PostgresRoutineDefinition) Internal(org.jooq.impl.Internal) Parameter(org.jooq.Parameter) ParameterDefinition(org.jooq.meta.ParameterDefinition)

Aggregations

Field (org.jooq.Field)1 OrderField (org.jooq.OrderField)1 Parameter (org.jooq.Parameter)1 SelectField (org.jooq.SelectField)1 TableField (org.jooq.TableField)1 UDTField (org.jooq.UDTField)1 Internal (org.jooq.impl.Internal)1 ParameterDefinition (org.jooq.meta.ParameterDefinition)1 SchemaDefinition (org.jooq.meta.SchemaDefinition)1 PostgresRoutineDefinition (org.jooq.meta.postgres.PostgresRoutineDefinition)1