All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.ignite.schema.parser.DatabaseMetadataParser Maven / Gradle / Ivy

Go to download

Java-based middleware for in-memory processing of big data in a distributed environment.

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.ignite.schema.parser;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.apache.ignite.cache.QueryIndex;
import org.apache.ignite.schema.model.PojoDescriptor;
import org.apache.ignite.schema.model.SchemaDescriptor;

/**
 * Database metadata parser.
 */
public class DatabaseMetadataParser {
    /**
     * Get list of schemas from database.
     *
     * @param conn Connection to database.
     * @return List of schema descriptors.
     * @throws SQLException If schemas loading failed.
     */
    public static ObservableList schemas(Connection conn) throws SQLException  {
        Collection dbSchemas = DbMetadataReader.getInstance().schemas(conn);

        List uiSchemas = new ArrayList<>(dbSchemas.size());

        for (String schema : dbSchemas)
            uiSchemas.add(new SchemaDescriptor(schema, false));

        return FXCollections.observableList(uiSchemas);
    }

    /**
     * Parse database metadata.
     *
     * @param conn Connection to database.
     * @param schemas Collection of schema names to process.
     * @param tblsOnly If {@code true} then process tables only else process tables and views.
     * @return Collection of POJO descriptors.
     * @throws SQLException If parsing failed.
     */
    public static ObservableList parse(Connection conn, List schemas, boolean tblsOnly)
        throws SQLException {
        Map parents = new HashMap<>();

        Map> childrens = new HashMap<>();

        for (DbTable tbl : DbMetadataReader.getInstance().metadata(conn, schemas, tblsOnly)) {
            String schema = tbl.schema();

            PojoDescriptor parent = parents.get(schema);
            Collection children = childrens.get(schema);

            if (parent == null) {
                parent = new PojoDescriptor(null, new DbTable(schema, "", Collections.emptyList(),
                    Collections.emptyList()));

                children = new ArrayList<>();

                parents.put(schema, parent);
                childrens.put(schema, children);
            }

            children.add(new PojoDescriptor(parent, tbl));
        }

        List res = new ArrayList<>();

        for (Map.Entry item : parents.entrySet()) {
            String schema = item.getKey();
            PojoDescriptor parent = item.getValue();

            Collection children = childrens.get(schema);

            if (!children.isEmpty()) {
                parent.children(children);

                res.add(parent);
                res.addAll(children);
            }
        }

        Collections.sort(res, new Comparator() {
            @Override public int compare(PojoDescriptor o1, PojoDescriptor o2) {
                return o1.fullDbName().compareTo(o2.fullDbName());
            }
        });

        return FXCollections.observableList(res);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy