Search in sources :

Example 1 with Result

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

the class MySQLDatabase method getRoutines0.

@Override
protected List<RoutineDefinition> getRoutines0() throws SQLException {
    List<RoutineDefinition> result = new ArrayList<RoutineDefinition>();
    try {
        create(true).fetchCount(PROC);
    } catch (DataAccessException e) {
        log.warn("Table unavailable", "The `mysql`.`proc` table is unavailable. Stored procedures cannot be loaded. Check if you have sufficient grants");
        return result;
    }
    Result<Record6<String, String, String, byte[], byte[], ProcType>> records = create().select(Proc.DB, Proc.NAME, Proc.COMMENT, Proc.PARAM_LIST, Proc.RETURNS, Proc.TYPE).from(PROC).where(DB.in(getInputSchemata())).orderBy(DB, Proc.NAME).fetch();
    Map<Record, Result<Record6<String, String, String, byte[], byte[], ProcType>>> groups = records.intoGroups(new Field[] { Proc.DB, Proc.NAME });
    // procedures and functions with the same signature.
    for (Entry<Record, Result<Record6<String, String, String, byte[], byte[], ProcType>>> entry : groups.entrySet()) {
        Result<?> overloads = entry.getValue();
        for (int i = 0; i < overloads.size(); i++) {
            Record record = overloads.get(i);
            SchemaDefinition schema = getSchema(record.get(DB));
            String name = record.get(Proc.NAME);
            String comment = record.get(Proc.COMMENT);
            String params = new String(record.get(Proc.PARAM_LIST));
            String returns = new String(record.get(Proc.RETURNS));
            ProcType type = record.get(Proc.TYPE);
            if (overloads.size() > 1) {
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, "_" + type.name()));
            } else {
                result.add(new MySQLRoutineDefinition(schema, name, comment, params, returns, type, null));
            }
        }
    }
    return result;
}
Also used : RoutineDefinition(org.jooq.util.RoutineDefinition) SchemaDefinition(org.jooq.util.SchemaDefinition) ArrayList(java.util.ArrayList) Result(org.jooq.Result) ProcType(org.jooq.util.mysql.mysql.enums.ProcType) Record6(org.jooq.Record6) Record(org.jooq.Record) DataAccessException(org.jooq.exception.DataAccessException)

Example 2 with Result

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

the class DefaultBinding method get.

@SuppressWarnings("unchecked")
@Override
public void get(BindingGetResultSetContext<U> ctx) throws SQLException {
    T result = null;
    if (type == Blob.class) {
        result = (T) ctx.resultSet().getBlob(ctx.index());
    } else if (type == Boolean.class) {
        result = (T) wasNull(ctx.resultSet(), Boolean.valueOf(ctx.resultSet().getBoolean(ctx.index())));
    } else if (type == BigInteger.class) {
        // The SQLite JDBC driver doesn't support BigDecimals
        if (ctx.configuration().dialect() == SQLDialect.SQLITE) {
            result = Convert.convert(ctx.resultSet().getString(ctx.index()), (Class<T>) BigInteger.class);
        } else {
            BigDecimal b = ctx.resultSet().getBigDecimal(ctx.index());
            result = (T) (b == null ? null : b.toBigInteger());
        }
    } else if (type == BigDecimal.class) {
        // The SQLite JDBC driver doesn't support BigDecimals
        if (ctx.configuration().dialect() == SQLDialect.SQLITE) {
            result = Convert.convert(ctx.resultSet().getString(ctx.index()), (Class<T>) BigDecimal.class);
        } else {
            result = (T) ctx.resultSet().getBigDecimal(ctx.index());
        }
    } else if (type == Byte.class) {
        result = (T) wasNull(ctx.resultSet(), Byte.valueOf(ctx.resultSet().getByte(ctx.index())));
    } else if (type == byte[].class) {
        result = (T) ctx.resultSet().getBytes(ctx.index());
    } else if (type == Clob.class) {
        result = (T) ctx.resultSet().getClob(ctx.index());
    } else if (type == Date.class) {
        result = (T) getDate(ctx.family(), ctx.resultSet(), ctx.index());
    } else if (type == Double.class) {
        result = (T) wasNull(ctx.resultSet(), Double.valueOf(ctx.resultSet().getDouble(ctx.index())));
    } else if (type == Float.class) {
        result = (T) wasNull(ctx.resultSet(), Float.valueOf(ctx.resultSet().getFloat(ctx.index())));
    } else if (type == Integer.class) {
        result = (T) wasNull(ctx.resultSet(), Integer.valueOf(ctx.resultSet().getInt(ctx.index())));
    } else if (type == LocalDate.class) {
        result = (T) localDate(getDate(ctx.family(), ctx.resultSet(), ctx.index()));
    } else if (type == LocalTime.class) {
        result = (T) localTime(getTime(ctx.family(), ctx.resultSet(), ctx.index()));
    } else if (type == LocalDateTime.class) {
        result = (T) localDateTime(getTimestamp(ctx.family(), ctx.resultSet(), ctx.index()));
    } else if (type == Long.class) {
        result = (T) wasNull(ctx.resultSet(), Long.valueOf(ctx.resultSet().getLong(ctx.index())));
    } else if (type == OffsetTime.class) {
        result = (T) offsetTime(ctx.resultSet().getString(ctx.index()));
    } else if (type == OffsetDateTime.class) {
        result = (T) offsetDateTime(ctx.resultSet().getString(ctx.index()));
    } else if (type == Short.class) {
        result = (T) wasNull(ctx.resultSet(), Short.valueOf(ctx.resultSet().getShort(ctx.index())));
    } else if (type == String.class) {
        result = (T) ctx.resultSet().getString(ctx.index());
    } else if (type == Time.class) {
        result = (T) getTime(ctx.family(), ctx.resultSet(), ctx.index());
    } else if (type == Timestamp.class) {
        result = (T) getTimestamp(ctx.family(), ctx.resultSet(), ctx.index());
    } else if (type == YearToMonth.class) {
        if (ctx.family() == POSTGRES) {
            Object object = ctx.resultSet().getObject(ctx.index());
            result = (T) (object == null ? null : PostgresUtils.toYearToMonth(object));
        } else {
            String string = ctx.resultSet().getString(ctx.index());
            result = (T) (string == null ? null : YearToMonth.valueOf(string));
        }
    } else if (type == DayToSecond.class) {
        if (ctx.family() == POSTGRES) {
            Object object = ctx.resultSet().getObject(ctx.index());
            result = (T) (object == null ? null : PostgresUtils.toDayToSecond(object));
        } else {
            String string = ctx.resultSet().getString(ctx.index());
            result = (T) (string == null ? null : DayToSecond.valueOf(string));
        }
    } else if (type == UByte.class) {
        result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), UByte.class);
    } else if (type == UShort.class) {
        result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), UShort.class);
    } else if (type == UInteger.class) {
        result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), UInteger.class);
    } else if (type == ULong.class) {
        result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), ULong.class);
    } else if (type == UUID.class) {
        switch(ctx.family()) {
            // java.util.UUID data type
            case H2:
            case POSTGRES:
                {
                    result = (T) ctx.resultSet().getObject(ctx.index());
                    break;
                }
            // emulates the type
            default:
                {
                    result = (T) Convert.convert(ctx.resultSet().getString(ctx.index()), UUID.class);
                    break;
                }
        }
    } else // The type byte[] is handled earlier. byte[][] can be handled here
    if (type.isArray()) {
        switch(ctx.family()) {
            case POSTGRES:
                {
                    result = pgGetArray(ctx, ctx.resultSet(), type, ctx.index());
                    break;
                }
            default:
                // Note: due to a HSQLDB bug, it is not recommended to call rs.getObject() here:
                // See https://sourceforge.net/tracker/?func=detail&aid=3181365&group_id=23316&atid=378131
                result = (T) convertArray(ctx.resultSet().getArray(ctx.index()), (Class<? extends Object[]>) type);
                break;
        }
    } else if (EnumType.class.isAssignableFrom(type)) {
        result = (T) getEnumType((Class<EnumType>) type, ctx.resultSet().getString(ctx.index()));
    } else if (Record.class.isAssignableFrom(type)) {
        switch(ctx.family()) {
            case POSTGRES:
                result = (T) pgNewRecord(type, null, ctx.resultSet().getObject(ctx.index()));
                break;
            default:
                result = (T) ctx.resultSet().getObject(ctx.index(), typeMap(type, ctx.configuration()));
                break;
        }
    } else if (Result.class.isAssignableFrom(type)) {
        ResultSet nested = (ResultSet) ctx.resultSet().getObject(ctx.index());
        result = (T) DSL.using(ctx.configuration()).fetch(nested);
    } else {
        result = (T) unlob(ctx.resultSet().getObject(ctx.index()));
    }
    // [#4372] Attach records if possible / required
    if (result instanceof Attachable && attachRecords(ctx.configuration()))
        ((Attachable) result).attach(ctx.configuration());
    ctx.value(converter.from(result));
}
Also used : LocalDateTime(java.time.LocalDateTime) Time(java.sql.Time) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) OffsetDateTime(java.time.OffsetDateTime) LocalDateTime(java.time.LocalDateTime) PostgresUtils.toPGArrayString(org.jooq.util.postgres.PostgresUtils.toPGArrayString) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) LocalDate(java.time.LocalDate) Date(java.sql.Date) Result(org.jooq.Result) UByte(org.jooq.types.UByte) OffsetTime(java.time.OffsetTime) EnumType(org.jooq.EnumType) UInteger(org.jooq.types.UInteger) ResultSet(java.sql.ResultSet) MockResultSet(org.jooq.tools.jdbc.MockResultSet) UUID(java.util.UUID) Attachable(org.jooq.Attachable) UShort(org.jooq.types.UShort) YearToMonth(org.jooq.types.YearToMonth)

Example 3 with Result

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

the class ResultImpl method intoGroups.

@Override
public final <S extends Record> Map<S, Result<R>> intoGroups(Table<S> table) {
    Map<S, Result<R>> map = new LinkedHashMap<S, Result<R>>();
    for (R record : this) {
        S key = record.into(table);
        Result<R> result = map.get(key);
        if (result == null) {
            result = new ResultImpl<R>(configuration(), this.fields);
            map.put(key, result);
        }
        result.add(record);
    }
    return map;
}
Also used : COLUMN_NAME_ELEMENTS(org.jooq.XMLFormat.RecordFormat.COLUMN_NAME_ELEMENTS) LinkedHashMap(java.util.LinkedHashMap) Result(org.jooq.Result)

Example 4 with Result

use of org.jooq.Result in project torodb by torodb.

the class AbstractMetaDataReadInterface method getIndexSize.

@Override
public Long getIndexSize(@Nonnull DSLContext dsl, @Nonnull MetaDatabase database, @Nonnull MetaCollection collection, @Nonnull String indexName) {
    long result = 0;
    MetaIndex index = collection.getMetaIndexByName(indexName);
    Iterator<TableRef> tableRefIterator = index.streamTableRefs().iterator();
    while (tableRefIterator.hasNext()) {
        TableRef tableRef = tableRefIterator.next();
        MetaDocPart docPart = collection.getMetaDocPartByTableRef(tableRef);
        Iterator<? extends MetaIdentifiedDocPartIndex> docPartIndexIterator = docPart.streamIndexes().iterator();
        while (docPartIndexIterator.hasNext()) {
            MetaIdentifiedDocPartIndex docPartIndex = docPartIndexIterator.next();
            if (index.isCompatible(docPart, docPartIndex)) {
                long relatedIndexCount = collection.streamContainedMetaIndexes().filter(i -> i.isCompatible(docPart, docPartIndex)).count();
                String statement = getReadIndexSizeStatement(database.getIdentifier(), docPart.getIdentifier(), docPartIndex.getIdentifier());
                result += sqlHelper.executeStatementWithResult(dsl, statement, Context.FETCH).get(0).into(Long.class) / relatedIndexCount;
            }
        }
    }
    return result;
}
Also used : MetaIndex(com.torodb.core.transaction.metainf.MetaIndex) KvTable(com.torodb.backend.tables.KvTable) Singleton(javax.inject.Singleton) Condition(org.jooq.Condition) MetaDocPartTable(com.torodb.backend.tables.MetaDocPartTable) MetaIndex(com.torodb.core.transaction.metainf.MetaIndex) Inject(javax.inject.Inject) MetaDatabase(com.torodb.core.transaction.metainf.MetaDatabase) Record1(org.jooq.Record1) DSLContext(org.jooq.DSLContext) Context(com.torodb.backend.ErrorHandler.Context) Nonnull(javax.annotation.Nonnull) MetaCollection(com.torodb.core.transaction.metainf.MetaCollection) Record(org.jooq.Record) Iterator(java.util.Iterator) KvRecord(com.torodb.backend.tables.records.KvRecord) Collection(java.util.Collection) MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) Result(org.jooq.Result) TableRef(com.torodb.core.TableRef) Stream(java.util.stream.Stream) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) MetaInfoKey(com.torodb.core.backend.MetaInfoKey) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) MetaDatabaseRecord(com.torodb.backend.tables.records.MetaDatabaseRecord) MetaIdentifiedDocPartIndex(com.torodb.core.transaction.metainf.MetaIdentifiedDocPartIndex) MetaDocPart(com.torodb.core.transaction.metainf.MetaDocPart) TableRef(com.torodb.core.TableRef)

Example 5 with Result

use of org.jooq.Result in project hmftools by hartwigmedical.

the class MySQLAnnotator method annotateBreakend.

@NotNull
private List<GeneAnnotation> annotateBreakend(@NotNull EnrichedStructuralVariant variant, final boolean isStart, @NotNull String chromosome, final long position) {
    final List<GeneAnnotation> result = Lists.newArrayList();
    final Result<?> genes = queryGenesOnChromosomeAndPosition(chromosome, position);
    for (final Record gene : genes) {
        final UInteger geneId = gene.get(GENE.GENE_ID);
        final String geneName = gene.get(XREF.DISPLAY_LABEL);
        final String geneStableId = gene.get(GENE.STABLE_ID);
        final UInteger canonicalTranscriptId = gene.get(GENE.CANONICAL_TRANSCRIPT_ID);
        final int geneStrand = gene.get(GENE.SEQ_REGION_STRAND);
        final List<Integer> entrezIds = Arrays.stream(gene.get(ENTREZ_IDS, String.class).split(",")).map(Integer::parseInt).collect(Collectors.toList());
        final String karyotypeBand = gene.get(KARYOTYPE_BAND, String.class);
        final List<String> synonyms = context.select(XREF.DBPRIMARY_ACC).from(XREF).innerJoin(OBJECT_XREF).on(OBJECT_XREF.XREF_ID.eq(XREF.XREF_ID)).and(OBJECT_XREF.ENSEMBL_ID.eq(geneId)).and(OBJECT_XREF.ENSEMBL_OBJECT_TYPE.eq(ObjectXrefEnsemblObjectType.Gene)).fetch().stream().map(r -> r.get(XREF.DBPRIMARY_ACC)).collect(Collectors.toList());
        final GeneAnnotation geneAnnotation = new GeneAnnotation(variant, isStart, geneName, geneStableId, geneStrand, synonyms, entrezIds, karyotypeBand);
        final Result<?> transcripts = context.select(TRANSCRIPT.TRANSCRIPT_ID, TRANSCRIPT.STABLE_ID).from(TRANSCRIPT).where(TRANSCRIPT.GENE_ID.eq(geneId)).fetch();
        for (final Record transcriptRecord : transcripts) {
            Transcript transcript = buildTranscript(geneAnnotation, transcriptRecord, position, canonicalTranscriptId, geneStrand > 0);
            if (transcript != null) {
                geneAnnotation.addTranscript(transcript);
            }
        }
        if (!geneAnnotation.transcripts().isEmpty()) {
            result.add(geneAnnotation);
        }
    }
    return result;
}
Also used : UInteger(org.jooq.types.UInteger) EXON_TRANSCRIPT(org.ensembl.database.homo_sapiens_core.Tables.EXON_TRANSCRIPT) Arrays(java.util.Arrays) Connection(java.sql.Connection) DSL(org.jooq.impl.DSL) Xref(org.ensembl.database.homo_sapiens_core.tables.Xref) StructuralVariantAnnotation(com.hartwig.hmftools.svannotation.annotations.StructuralVariantAnnotation) SEQ_REGION(org.ensembl.database.homo_sapiens_core.Tables.SEQ_REGION) DSL.decode(org.jooq.impl.DSL.decode) ObjectXrefEnsemblObjectType(org.ensembl.database.homo_sapiens_core.enums.ObjectXrefEnsemblObjectType) TRANSCRIPT(org.ensembl.database.homo_sapiens_core.Tables.TRANSCRIPT) Condition(org.jooq.Condition) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) KARYOTYPE(org.ensembl.database.homo_sapiens_core.Tables.KARYOTYPE) UInteger(org.jooq.types.UInteger) GeneStatus(org.ensembl.database.homo_sapiens_core.enums.GeneStatus) DSLContext(org.jooq.DSLContext) SQLDialect(org.jooq.SQLDialect) EnrichedStructuralVariant(com.hartwig.hmftools.common.variant.structural.EnrichedStructuralVariant) Record(org.jooq.Record) GeneAnnotation(com.hartwig.hmftools.svannotation.annotations.GeneAnnotation) OBJECT_XREF(org.ensembl.database.homo_sapiens_core.Tables.OBJECT_XREF) Result(org.jooq.Result) Collectors(java.util.stream.Collectors) Transcript(com.hartwig.hmftools.svannotation.annotations.Transcript) XREF(org.ensembl.database.homo_sapiens_core.Tables.XREF) COORD_SYSTEM(org.ensembl.database.homo_sapiens_core.Tables.COORD_SYSTEM) Nullable(org.jetbrains.annotations.Nullable) EXON(org.ensembl.database.homo_sapiens_core.Tables.EXON) List(java.util.List) DSL.groupConcatDistinct(org.jooq.impl.DSL.groupConcatDistinct) GENE(org.ensembl.database.homo_sapiens_core.Tables.GENE) NotNull(org.jetbrains.annotations.NotNull) DriverManager(java.sql.DriverManager) GeneAnnotation(com.hartwig.hmftools.svannotation.annotations.GeneAnnotation) Transcript(com.hartwig.hmftools.svannotation.annotations.Transcript) UInteger(org.jooq.types.UInteger) Record(org.jooq.Record) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Result (org.jooq.Result)31 Record (org.jooq.Record)24 ArrayList (java.util.ArrayList)23 List (java.util.List)22 Collectors (java.util.stream.Collectors)17 DSLContext (org.jooq.DSLContext)17 BigDecimal (java.math.BigDecimal)13 SQLException (java.sql.SQLException)13 Map (java.util.Map)13 Timestamp (java.sql.Timestamp)12 Optional (java.util.Optional)12 Stream (java.util.stream.Stream)12 Lists (com.google.common.collect.Lists)11 HashMap (java.util.HashMap)11 UUID (java.util.UUID)11 IOException (java.io.IOException)10 Iterator (java.util.Iterator)10 Singleton (javax.inject.Singleton)10 Almura (com.almuradev.almura.Almura)9 ServerNotificationManager (com.almuradev.almura.feature.notification.ServerNotificationManager)9