Search in sources :

Example 1 with Name

use of org.jooq.Name in project jOOQ by jOOQ.

the class JavaGenerator method getType.

protected String getType(Database db, SchemaDefinition schema, String t, int p, int s, Name u, String javaType, String defaultType, Mode udtMode) {
    String type = defaultType;
    // Custom types
    if (javaType != null) {
        type = javaType;
    } else // Array types
    if (db.isArrayType(t)) {
        // [#4388] TODO: Improve array handling
        Name baseType = GenerationUtil.getArrayBaseType(db.getDialect(), t, u);
        if (scala)
            type = "scala.Array[" + getType(db, schema, baseType.last(), p, s, baseType, javaType, defaultType, udtMode) + "]";
        else
            type = getType(db, schema, baseType.last(), p, s, baseType, javaType, defaultType, udtMode) + "[]";
    } else // Check for Oracle-style VARRAY types
    if (db.getArray(schema, u) != null) {
        boolean udtArray = db.getArray(schema, u).getElementType().isUDT();
        if (udtMode == Mode.POJO || (udtMode == Mode.INTERFACE && !udtArray)) {
            if (scala)
                type = "java.util.List[" + getJavaType(db.getArray(schema, u).getElementType(), udtMode) + "]";
            else
                type = "java.util.List<" + getJavaType(db.getArray(schema, u).getElementType(), udtMode) + ">";
        } else if (udtMode == Mode.INTERFACE) {
            if (scala)
                type = "java.util.List[_ <:" + getJavaType(db.getArray(schema, u).getElementType(), udtMode) + "]";
            else
                type = "java.util.List<? extends " + getJavaType(db.getArray(schema, u).getElementType(), udtMode) + ">";
        } else {
            type = getStrategy().getFullJavaClassName(db.getArray(schema, u), Mode.RECORD);
        }
    } else // Check for ENUM types
    if (db.getEnum(schema, u) != null) {
        type = getStrategy().getFullJavaClassName(db.getEnum(schema, u));
    } else // Check for UDTs
    if (db.getUDT(schema, u) != null) {
        type = getStrategy().getFullJavaClassName(db.getUDT(schema, u), udtMode);
    } else // [#5334] In MySQL, the user type is (ab)used for synthetic enum types. This can lead to accidental matches here
    if (db.getDialect().family() == POSTGRES && db.getTable(schema, u) != null) {
        type = getStrategy().getFullJavaClassName(db.getTable(schema, u), udtMode);
    } else // Check for custom types
    if (u != null && db.getConfiguredCustomType(u.last()) != null) {
        type = u.last();
    } else // Try finding a basic standard SQL type according to the current dialect
    {
        try {
            Class<?> clazz = mapJavaTimeTypes(DefaultDataType.getDataType(db.getDialect(), t, p, s)).getType();
            if (scala && clazz == byte[].class)
                type = "scala.Array[scala.Byte]";
            else
                type = clazz.getCanonicalName();
            if (clazz.getTypeParameters().length > 0) {
                type += (scala ? "[" : "<");
                String separator = "";
                for (TypeVariable<?> var : clazz.getTypeParameters()) {
                    type += separator;
                    type += ((Class<?>) var.getBounds()[0]).getCanonicalName();
                    separator = ", ";
                }
                type += (scala ? "]" : ">");
            }
        } catch (SQLDialectNotSupportedException e) {
            if (defaultType == null) {
                throw e;
            }
        }
    }
    return type;
}
Also used : SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) StringUtils.defaultString(org.jooq.tools.StringUtils.defaultString) Name(org.jooq.Name)

Example 2 with Name

use of org.jooq.Name in project jOOQ by jOOQ.

the class PostgresDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() throws SQLException {
    List<TableDefinition> result = new ArrayList<TableDefinition>();
    Map<Name, PostgresTableDefinition> map = new HashMap<Name, PostgresTableDefinition>();
    Select<Record6<String, String, String, Boolean, Boolean, String>> empty = select(inline(""), inline(""), inline(""), inline(false), inline(false), inline("")).where(falseCondition());
    for (Record record : create().select().from(select(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME, TABLES.TABLE_NAME.as("specific_name"), inline(false).as("table_valued_function"), inline(false).as("materialized_view"), PG_DESCRIPTION.DESCRIPTION).from(TABLES).join(PG_NAMESPACE).on(TABLES.TABLE_SCHEMA.eq(PG_NAMESPACE.NSPNAME)).join(PG_CLASS).on(PG_CLASS.RELNAME.eq(TABLES.TABLE_NAME)).and(PG_CLASS.RELNAMESPACE.eq(oid(PG_NAMESPACE))).leftOuterJoin(PG_DESCRIPTION).on(PG_DESCRIPTION.OBJOID.eq(oid(PG_CLASS))).and(PG_DESCRIPTION.OBJSUBID.eq(0)).where(TABLES.TABLE_SCHEMA.in(getInputSchemata())).and(row(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME).notIn(select(PG_NAMESPACE.NSPNAME, PG_CLASS.RELNAME).from(PG_CLASS).join(PG_NAMESPACE).on(PG_CLASS.RELNAMESPACE.eq(oid(PG_NAMESPACE))).where(PG_CLASS.RELKIND.eq(inline("m"))))).unionAll(select(PG_NAMESPACE.NSPNAME, PG_CLASS.RELNAME, PG_CLASS.RELNAME, inline(false).as("table_valued_function"), inline(true).as("materialized_view"), PG_DESCRIPTION.DESCRIPTION).from(PG_CLASS).join(PG_NAMESPACE).on(PG_CLASS.RELNAMESPACE.eq(oid(PG_NAMESPACE))).leftOuterJoin(PG_DESCRIPTION).on(PG_DESCRIPTION.OBJOID.eq(oid(PG_CLASS))).and(PG_DESCRIPTION.OBJSUBID.eq(0)).where(PG_NAMESPACE.NSPNAME.in(getInputSchemata())).and(PG_CLASS.RELKIND.eq(inline("m")))).unionAll(tableValuedFunctions() ? select(ROUTINES.ROUTINE_SCHEMA, ROUTINES.ROUTINE_NAME, ROUTINES.SPECIFIC_NAME, inline(true).as("table_valued_function"), inline(false).as("materialized_view"), inline("")).from(ROUTINES).join(PG_NAMESPACE).on(ROUTINES.SPECIFIC_SCHEMA.eq(PG_NAMESPACE.NSPNAME)).join(PG_PROC).on(PG_PROC.PRONAMESPACE.eq(oid(PG_NAMESPACE))).and(PG_PROC.PRONAME.concat("_").concat(oid(PG_PROC)).eq(ROUTINES.SPECIFIC_NAME)).where(ROUTINES.ROUTINE_SCHEMA.in(getInputSchemata())).and(PG_PROC.PRORETSET) : empty).asTable("tables")).orderBy(1, 2).fetch()) {
        SchemaDefinition schema = getSchema(record.get(TABLES.TABLE_SCHEMA));
        String name = record.get(TABLES.TABLE_NAME);
        boolean tableValuedFunction = record.get("table_valued_function", boolean.class);
        boolean materializedView = record.get("materialized_view", boolean.class);
        String comment = record.get(PG_DESCRIPTION.DESCRIPTION, String.class);
        if (tableValuedFunction) {
            result.add(new PostgresTableValuedFunction(schema, name, record.get(ROUTINES.SPECIFIC_NAME), comment));
        } else if (materializedView) {
            result.add(new PostgresMaterializedViewDefinition(schema, name, comment));
        } else {
            PostgresTableDefinition t = new PostgresTableDefinition(schema, name, comment);
            result.add(t);
            map.put(name(schema.getName(), name), t);
        }
    }
    PgClass ct = PG_CLASS.as("ct");
    PgNamespace cn = PG_NAMESPACE.as("cn");
    PgInherits i = PG_INHERITS.as("i");
    PgClass pt = PG_CLASS.as("pt");
    PgNamespace pn = PG_NAMESPACE.as("pn");
    // don't execute the following query:
    if (is84()) {
        for (Record5<String, String, String, String, Integer> inheritance : create().select(cn.NSPNAME, ct.RELNAME, pn.NSPNAME, pt.RELNAME, max(i.INHSEQNO).over().partitionBy(i.INHRELID).as("m")).from(ct).join(cn).on(ct.RELNAMESPACE.eq(oid(cn))).join(i).on(i.INHRELID.eq(oid(ct))).join(pt).on(i.INHPARENT.eq(oid(pt))).join(pn).on(pt.RELNAMESPACE.eq(oid(pn))).where(cn.NSPNAME.in(getInputSchemata())).and(pn.NSPNAME.in(getInputSchemata())).fetch()) {
            Name child = name(inheritance.value1(), inheritance.value2());
            Name parent = name(inheritance.value3(), inheritance.value4());
            if (inheritance.value5() > 1) {
                log.info("Multiple inheritance", "Multiple inheritance is not supported by jOOQ: " + child + " inherits from " + parent);
            } else {
                PostgresTableDefinition childTable = map.get(child);
                PostgresTableDefinition parentTable = map.get(parent);
                if (childTable != null && parentTable != null) {
                    childTable.setParentTable(parentTable);
                    parentTable.getChildTables().add(childTable);
                }
            }
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Name(org.jooq.Name) PgInherits(org.jooq.util.postgres.pg_catalog.tables.PgInherits) TableDefinition(org.jooq.util.TableDefinition) PgClass(org.jooq.util.postgres.pg_catalog.tables.PgClass) Record6(org.jooq.Record6) Record(org.jooq.Record) PgNamespace(org.jooq.util.postgres.pg_catalog.tables.PgNamespace)

Example 3 with Name

use of org.jooq.Name in project jOOQ by jOOQ.

the class XMLRoutineDefinition method overload.

private static String overload(InformationSchema info, Routine routine) {
    Name routineName = name(routine.getRoutineCatalog(), routine.getRoutineSchema(), routine.getRoutinePackage(), routine.getRoutineName());
    String result = null;
    int count = 0;
    // TODO: Better algorithm by pre-calculating
    for (Routine r : info.getRoutines()) {
        Name rName = name(r.getRoutineCatalog(), r.getRoutineSchema(), r.getRoutinePackage(), r.getRoutineName());
        if (routineName.equals(rName)) {
            count++;
            if (routine == r) {
                result = "" + count;
            }
            if (count > 1 && result != null)
                break;
        }
    }
    return count > 1 ? result : null;
}
Also used : Routine(org.jooq.util.xml.jaxb.Routine) Name(org.jooq.Name)

Example 4 with Name

use of org.jooq.Name in project jOOQ by jOOQ.

the class XMLRoutineDefinition method init0.

@Override
protected void init0() {
    for (Parameter parameter : info.getParameters()) {
        Name parameterRoutineName = name(parameter.getSpecificCatalog(), parameter.getSpecificSchema(), parameter.getSpecificPackage(), parameter.getSpecificName());
        if (getQualifiedNamePart().equals(parameterRoutineName)) {
            DataTypeDefinition type = new DefaultDataTypeDefinition(getDatabase(), getSchema(), parameter.getDataType(), parameter.getCharacterMaximumLength(), parameter.getNumericPrecision(), parameter.getNumericScale(), null, parameter.getParameterDefault());
            ParameterDefinition p = new DefaultParameterDefinition(this, parameter.getParameterName(), parameter.getOrdinalPosition(), type, !StringUtils.isBlank(parameter.getParameterDefault()), StringUtils.isBlank(parameter.getParameterName()));
            switch(parameter.getParameterMode()) {
                case IN:
                    addParameter(InOutDefinition.IN, p);
                    break;
                case INOUT:
                    addParameter(InOutDefinition.INOUT, p);
                    break;
                case OUT:
                    addParameter(InOutDefinition.OUT, p);
                    break;
            }
        }
    }
}
Also used : DefaultDataTypeDefinition(org.jooq.util.DefaultDataTypeDefinition) Parameter(org.jooq.util.xml.jaxb.Parameter) DefaultParameterDefinition(org.jooq.util.DefaultParameterDefinition) DataTypeDefinition(org.jooq.util.DataTypeDefinition) DefaultDataTypeDefinition(org.jooq.util.DefaultDataTypeDefinition) Name(org.jooq.Name) DefaultParameterDefinition(org.jooq.util.DefaultParameterDefinition) ParameterDefinition(org.jooq.util.ParameterDefinition)

Example 5 with Name

use of org.jooq.Name in project jOOQ by jOOQ.

the class MetaDataFieldProvider method init.

private Fields<Record> init(Configuration configuration, ResultSetMetaData meta) {
    Field<?>[] fields;
    int columnCount = 0;
    try {
        columnCount = meta.getColumnCount();
        fields = new Field[columnCount];
    }// procedures / functions
     catch (SQLException e) {
        log.info("Cannot fetch column count for cursor : " + e.getMessage());
        fields = new Field[] { field("dummy") };
    }
    try {
        for (int i = 1; i <= columnCount; i++) {
            Name name;
            String columnLabel = meta.getColumnLabel(i);
            String columnName = meta.getColumnName(i);
            if (columnName.equals(columnLabel)) {
                try {
                    String columnSchema = meta.getSchemaName(i);
                    String columnTable = meta.getTableName(i);
                    name = name(columnSchema, columnTable, columnName);
                }// ResultSetMetaData.getSchemaName and/or ResultSetMetaData.getTableName methods
                 catch (SQLException e) {
                    name = name(columnLabel);
                }
            } else {
                name = name(columnLabel);
            }
            int precision = meta.getPrecision(i);
            int scale = meta.getScale(i);
            DataType<?> dataType = SQLDataType.OTHER;
            String type = meta.getColumnTypeName(i);
            try {
                dataType = DefaultDataType.getDataType(configuration.family(), type, precision, scale);
                if (dataType.hasPrecision()) {
                    dataType = dataType.precision(precision);
                }
                if (dataType.hasScale()) {
                    dataType = dataType.scale(scale);
                }
                if (dataType.hasLength()) {
                    // JDBC doesn't distinguish between precision and length
                    dataType = dataType.length(precision);
                }
            }// types (e.g. such as PostgreSQL's json type) will cause this exception.
             catch (SQLDialectNotSupportedException ignore) {
                log.debug("Not supported by dialect", ignore.getMessage());
            }
            fields[i - 1] = field(name, dataType);
        }
    } catch (SQLException e) {
        throw Tools.translate(null, e);
    }
    return new Fields<Record>(fields);
}
Also used : Field(org.jooq.Field) SQLDialectNotSupportedException(org.jooq.exception.SQLDialectNotSupportedException) SQLException(java.sql.SQLException) Name(org.jooq.Name)

Aggregations

Name (org.jooq.Name)13 SQLDialectNotSupportedException (org.jooq.exception.SQLDialectNotSupportedException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Condition (org.jooq.Condition)2 Field (org.jooq.Field)2 Record (org.jooq.Record)2 Schema (org.jooq.Schema)2 Sequence (org.jooq.Sequence)2 TableField (org.jooq.TableField)2 StringWriter (java.io.StringWriter)1 SQLException (java.sql.SQLException)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 AlterIndexFinalStep (org.jooq.AlterIndexFinalStep)1 AlterIndexStep (org.jooq.AlterIndexStep)1 Catalog (org.jooq.Catalog)1 CreateIndexFinalStep (org.jooq.CreateIndexFinalStep)1 CreateIndexStep (org.jooq.CreateIndexStep)1