Search in sources :

Example 36 with SchemaDefinition

use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.

the class MariaDBDatabase method getSequences0.

@Override
protected List<SequenceDefinition> getSequences0() throws SQLException {
    List<SequenceDefinition> result = new ArrayList<>();
    for (Record record : create().select(TABLES.TABLE_SCHEMA, TABLES.TABLE_NAME).from(TABLES).where(TABLES.TABLE_TYPE.eq("SEQUENCE"))) {
        SchemaDefinition schema = getSchema(record.get(TABLES.TABLE_SCHEMA));
        if (schema != null) {
            String name = record.get(TABLES.TABLE_NAME);
            DefaultDataTypeDefinition type = new DefaultDataTypeDefinition(this, schema, BIGINT.getTypeName());
            Field<Long> startWith = nullif(field("start_value", BIGINT), inline(1L));
            Field<Long> incrementBy = nullif(field("increment", BIGINT), inline(1L));
            Field<Long> minValue = inline(field("minimum_value", BIGINT), inline(1L));
            Field<Long> maxValue = nullif(field("maximum_value", BIGINT), inline(DEFAULT_SEQUENCE_MAXVALUE));
            Field<Boolean> cycle = field("cycle_option", BOOLEAN);
            Field<Long> cache = nullif(field("cache_size", BIGINT), inline(DEFAULT_SEQUENCE_CACHE));
            Record flags = create().select(startWith, incrementBy, minValue, maxValue, cycle, cache).from(name(schema.getName(), name)).fetchOne();
            result.add(new DefaultSequenceDefinition(schema, name, type, null, flags.get(startWith), flags.get(incrementBy), flags.get(minValue), flags.get(maxValue), flags.get(cycle), flags.get(cache)));
        }
    }
    return result;
}
Also used : DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) SchemaDefinition(org.jooq.meta.SchemaDefinition) SequenceDefinition(org.jooq.meta.SequenceDefinition) DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) ArrayList(java.util.ArrayList) Record(org.jooq.Record)

Example 37 with SchemaDefinition

use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.

the class XMLDatabase method getSchemata0.

@Override
protected List<SchemaDefinition> getSchemata0() {
    List<SchemaDefinition> result = new ArrayList<>();
    for (Schema schema : info().getSchemata()) {
        String schemaName = schema.getSchemaName();
        result.add(new SchemaDefinition(this, StringUtils.defaultIfNull(schemaName, ""), schema.getComment()));
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) Schema(org.jooq.util.xml.jaxb.Schema) ArrayList(java.util.ArrayList)

Example 38 with SchemaDefinition

use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.

the class XMLDatabase method getTables0.

@Override
protected List<TableDefinition> getTables0() {
    List<TableDefinition> result = new ArrayList<>();
    for (Table table : info().getTables()) {
        if (getInputSchemata().contains(table.getTableSchema())) {
            SchemaDefinition schema = getSchema(table.getTableSchema());
            TableType tableType;
            switch(table.getTableType()) {
                case GLOBAL_TEMPORARY:
                    tableType = TableType.TEMPORARY;
                    break;
                case VIEW:
                    tableType = TableType.VIEW;
                    break;
                case BASE_TABLE:
                default:
                    tableType = TableType.TABLE;
                    break;
            }
            String source = null;
            if (tableType == TableType.VIEW) {
                viewLoop: for (View view : info().getViews()) {
                    if (StringUtils.equals(defaultIfNull(table.getTableCatalog(), ""), defaultIfNull(view.getTableCatalog(), "")) && StringUtils.equals(defaultIfNull(table.getTableSchema(), ""), defaultIfNull(view.getTableSchema(), "")) && StringUtils.equals(defaultIfNull(table.getTableName(), ""), defaultIfNull(view.getTableName(), ""))) {
                        source = view.getViewDefinition();
                        break viewLoop;
                    }
                }
            }
            result.add(new XMLTableDefinition(schema, info(), table, table.getComment(), tableType, source));
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) Table(org.jooq.util.xml.jaxb.Table) TableType(org.jooq.TableOptions.TableType) ArrayList(java.util.ArrayList) TableDefinition(org.jooq.meta.TableDefinition) View(org.jooq.util.xml.jaxb.View)

Example 39 with SchemaDefinition

use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.

the class XMLDatabase method getPackages0.

@Override
protected List<PackageDefinition> getPackages0() {
    List<PackageDefinition> result = new ArrayList<>();
    Set<String> packages = new HashSet<>();
    for (Routine routine : info().getRoutines()) {
        String schemaName = defaultIfBlank(routine.getSpecificSchema(), routine.getRoutineSchema());
        if (getInputSchemata().contains(schemaName)) {
            SchemaDefinition schema = getSchema(schemaName);
            String packageName = defaultIfBlank(routine.getSpecificPackage(), routine.getRoutinePackage());
            if (!isBlank(packageName) && packages.add(packageName)) {
                result.add(new XMLPackageDefinition(schema, info(), packageName));
            }
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.meta.SchemaDefinition) PackageDefinition(org.jooq.meta.PackageDefinition) ArrayList(java.util.ArrayList) Routine(org.jooq.util.xml.jaxb.Routine) HashSet(java.util.HashSet)

Example 40 with SchemaDefinition

use of org.jooq.meta.SchemaDefinition in project jOOQ by jOOQ.

the class XMLDatabase method getIndexes0.

@Override
protected List<IndexDefinition> getIndexes0() throws SQLException {
    List<IndexDefinition> result = new ArrayList<>();
    final Map<Name, SortedSet<IndexColumnUsage>> indexColumnUsage = new HashMap<>();
    for (IndexColumnUsage ic : info().getIndexColumnUsages()) {
        Name name = name(ic.getIndexCatalog(), ic.getIndexSchema(), ic.getTableName(), ic.getIndexName());
        SortedSet<IndexColumnUsage> list = indexColumnUsage.computeIfAbsent(name, k -> new TreeSet<>((o1, o2) -> {
            int r = 0;
            r = defaultIfNull(o1.getIndexCatalog(), "").compareTo(defaultIfNull(o2.getIndexCatalog(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getIndexSchema(), "").compareTo(defaultIfNull(o2.getIndexSchema(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getTableName(), "").compareTo(defaultIfNull(o2.getTableName(), ""));
            if (r != 0)
                return r;
            r = defaultIfNull(o1.getIndexName(), "").compareTo(defaultIfNull(o2.getIndexName(), ""));
            if (r != 0)
                return r;
            return Integer.compare(o1.getOrdinalPosition(), o2.getOrdinalPosition());
        }));
        list.add(ic);
    }
    indexLoop: for (Index i : info().getIndexes()) {
        if (getInputSchemata().contains(i.getTableSchema())) {
            final SchemaDefinition schema = getSchema(i.getTableSchema());
            final TableDefinition table = getTable(schema, i.getTableName());
            if (table == null)
                continue indexLoop;
            final Name name = name(i.getIndexCatalog(), i.getIndexSchema(), i.getTableName(), i.getIndexName());
            IndexDefinition index = new AbstractIndexDefinition(schema, i.getIndexName(), table, Boolean.TRUE.equals(i.isIsUnique()), i.getComment()) {

                private final List<IndexColumnDefinition> indexColumns;

                {
                    indexColumns = new ArrayList<>();
                    SortedSet<IndexColumnUsage> list = indexColumnUsage.get(name);
                    if (list != null)
                        for (IndexColumnUsage ic : list) {
                            ColumnDefinition column = table.getColumn(ic.getColumnName());
                            if (column != null)
                                indexColumns.add(new DefaultIndexColumnDefinition(this, column, Boolean.TRUE.equals(ic.isIsDescending()) ? SortOrder.DESC : SortOrder.ASC, ic.getOrdinalPosition()));
                            else
                                log.error(String.format("Column %s not found in table %s.", ic.getColumnName(), table));
                        }
                    else
                        log.error(String.format("No columns found for index %s.", name));
                }

                @Override
                protected List<IndexColumnDefinition> getIndexColumns0() {
                    return indexColumns;
                }
            };
            result.add(index);
        }
    }
    return result;
}
Also used : Transformer(javax.xml.transform.Transformer) DSL(org.jooq.impl.DSL) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) SortedSet(java.util.SortedSet) StringUtils.isBlank(org.jooq.tools.StringUtils.isBlank) XMLInputFactory(javax.xml.stream.XMLInputFactory) StreamResult(javax.xml.transform.stream.StreamResult) MiniJAXB(org.jooq.util.jaxb.tools.MiniJAXB) DefaultSequenceDefinition(org.jooq.meta.DefaultSequenceDefinition) Table(org.jooq.util.xml.jaxb.Table) DataTypeDefinition(org.jooq.meta.DataTypeDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) RoutineDefinition(org.jooq.meta.RoutineDefinition) PackageDefinition(org.jooq.meta.PackageDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) IOException(org.jooq.exception.IOException) InformationSchema(org.jooq.util.xml.jaxb.InformationSchema) XMLStreamReader(javax.xml.stream.XMLStreamReader) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) DSLContext(org.jooq.DSLContext) SQLDialect(org.jooq.SQLDialect) SortOrder(org.jooq.SortOrder) DSL.name(org.jooq.impl.DSL.name) JooqLogger(org.jooq.tools.JooqLogger) Routine(org.jooq.util.xml.jaxb.Routine) View(org.jooq.util.xml.jaxb.View) Name(org.jooq.Name) CheckConstraint(org.jooq.util.xml.jaxb.CheckConstraint) Set(java.util.Set) Constants(org.jooq.Constants) UNIQUE(org.jooq.util.xml.jaxb.TableConstraintType.UNIQUE) Reader(java.io.Reader) Schema(org.jooq.util.xml.jaxb.Schema) TableConstraintType(org.jooq.util.xml.jaxb.TableConstraintType) PRIMARY_KEY(org.jooq.util.xml.jaxb.TableConstraintType.PRIMARY_KEY) JDBCUtils(org.jooq.tools.jdbc.JDBCUtils) FilePattern(org.jooq.FilePattern) TableType(org.jooq.TableOptions.TableType) DomainDefinition(org.jooq.meta.DomainDefinition) List(java.util.List) StringUtils.defaultIfNull(org.jooq.tools.StringUtils.defaultIfNull) IndexColumnUsage(org.jooq.util.xml.jaxb.IndexColumnUsage) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DefaultCheckConstraintDefinition(org.jooq.meta.DefaultCheckConstraintDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultRelations(org.jooq.meta.DefaultRelations) SequenceDefinition(org.jooq.meta.SequenceDefinition) TransformerException(javax.xml.transform.TransformerException) StreamSource(javax.xml.transform.stream.StreamSource) DefaultDataTypeDefinition(org.jooq.meta.DefaultDataTypeDefinition) TableDefinition(org.jooq.meta.TableDefinition) HashMap(java.util.HashMap) Sequence(org.jooq.util.xml.jaxb.Sequence) IndexDefinition(org.jooq.meta.IndexDefinition) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) ArrayDefinition(org.jooq.meta.ArrayDefinition) UDTDefinition(org.jooq.meta.UDTDefinition) KeyColumnUsage(org.jooq.util.xml.jaxb.KeyColumnUsage) FALSE(java.lang.Boolean.FALSE) StringUtils.defaultIfBlank(org.jooq.tools.StringUtils.defaultIfBlank) ReferentialConstraint(org.jooq.util.xml.jaxb.ReferentialConstraint) StringWriter(java.io.StringWriter) FileInputStream(java.io.FileInputStream) StringUtils(org.jooq.tools.StringUtils) File(java.io.File) Index(org.jooq.util.xml.jaxb.Index) Sort(org.jooq.FilePattern.Sort) StringReader(java.io.StringReader) SchemaDefinition(org.jooq.meta.SchemaDefinition) TableConstraint(org.jooq.util.xml.jaxb.TableConstraint) EnumDefinition(org.jooq.meta.EnumDefinition) TransformerFactory(javax.xml.transform.TransformerFactory) Collections(java.util.Collections) AbstractDatabase(org.jooq.meta.AbstractDatabase) CatalogDefinition(org.jooq.meta.CatalogDefinition) InputStream(java.io.InputStream) SchemaDefinition(org.jooq.meta.SchemaDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Index(org.jooq.util.xml.jaxb.Index) SortedSet(java.util.SortedSet) IndexColumnUsage(org.jooq.util.xml.jaxb.IndexColumnUsage) Name(org.jooq.Name) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) ColumnDefinition(org.jooq.meta.ColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) IndexColumnDefinition(org.jooq.meta.IndexColumnDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) IndexDefinition(org.jooq.meta.IndexDefinition) AbstractIndexDefinition(org.jooq.meta.AbstractIndexDefinition) DefaultIndexColumnDefinition(org.jooq.meta.DefaultIndexColumnDefinition) TableDefinition(org.jooq.meta.TableDefinition) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

SchemaDefinition (org.jooq.meta.SchemaDefinition)81 Record (org.jooq.Record)51 ArrayList (java.util.ArrayList)46 TableDefinition (org.jooq.meta.TableDefinition)44 DefaultDataTypeDefinition (org.jooq.meta.DefaultDataTypeDefinition)22 DataTypeDefinition (org.jooq.meta.DataTypeDefinition)21 ColumnDefinition (org.jooq.meta.ColumnDefinition)14 IndexColumnDefinition (org.jooq.meta.IndexColumnDefinition)14 IndexDefinition (org.jooq.meta.IndexDefinition)12 SequenceDefinition (org.jooq.meta.SequenceDefinition)12 List (java.util.List)10 DefaultIndexColumnDefinition (org.jooq.meta.DefaultIndexColumnDefinition)10 DomainDefinition (org.jooq.meta.DomainDefinition)9 RoutineDefinition (org.jooq.meta.RoutineDefinition)9 Result (org.jooq.Result)8 AbstractIndexDefinition (org.jooq.meta.AbstractIndexDefinition)8 DefaultCheckConstraintDefinition (org.jooq.meta.DefaultCheckConstraintDefinition)8 DefaultSequenceDefinition (org.jooq.meta.DefaultSequenceDefinition)8 PackageDefinition (org.jooq.meta.PackageDefinition)8 TableType (org.jooq.TableOptions.TableType)7