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

com.datastax.data.dataset.io.schema.DataSetIOUtility Maven / Gradle / Ivy

The newest version!

package com.datastax.data.dataset.io.schema;

import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.*;
import java.util.logging.Logger;


public class DataSetIOUtility {
    private static final Logger LOG = Logger.getLogger(DataSetIOUtility.class.getName());
    
    
    private DataSetIOUtility() {
    }
    
    
    public static Class getType(int type) {
        switch (type) {
            case Types.ARRAY:
            case Types.DISTINCT:
            case Types.JAVA_OBJECT:
            case Types.NULL:
            case Types.OTHER:
            case Types.REF:
            case Types.STRUCT:
            case Types.DATALINK:
                return Object.class;
                
            case Types.BINARY:
            case Types.BIT:
            case Types.BOOLEAN:
                return Boolean.class;
                
            case Types.BIGINT:
            case Types.SMALLINT:
            case Types.TINYINT:
            case Types.INTEGER:
                return Integer.class;
                
            case Types.BLOB:
            case Types.LONGVARBINARY:
            case Types.VARBINARY:
                return byte[].class;
                
            case Types.CHAR:
                return Character.class;
                
            case Types.CLOB:
                return char[].class;
                
            case Types.DATE:
            case Types.TIME:
            case Types.TIMESTAMP:
                return Date.class;
                
            case Types.DECIMAL:
            case Types.DOUBLE:
            case Types.FLOAT:
            case Types.NUMERIC:
            case Types.REAL:
                return BigDecimal.class;
                
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
                return String.class;
                
            default:
                LOG.warning("Unsupported JDBC type; type is unknown, so mapping to Object.class. Type value was " + type);
                return Object.class;
        }
    }
    
    
    public static Map> extractTableList(String... tableNames) {
        
        
        
        Map> tables = new HashMap>(5);
        
        for (String name : tableNames) {
            int dotIdx = name.indexOf(".");
            
            if (dotIdx != -1) {
                String table = name.substring(0, dotIdx);
                String col = name.substring(dotIdx + 1);
                
                
                
                if (!tables.containsKey(table)) {
                    tables.put(table, new HashSet());
                }
                
                
                
                if (tables.get(table) != null) {
                    tables.get(table).add(col);
                }
            } else {
                tables.put(name, null);
            }
        }
        return tables;
    }

    
    public static String getDefaultSchemaName(DatabaseMetaData databaseMetaData) {
        String defaultSchemaName = null;
        try {
            ResultSet rs = databaseMetaData.getSchemas();
            boolean found = false;
            while ( rs.next() && ! found) {
                found = rs.getBoolean("IS_DEFAULT");
                if ( found ) defaultSchemaName = rs.getString("TABLE_SCHEM");
            }
        } catch (Exception e) {
            defaultSchemaName = null;
        }
        return defaultSchemaName;
    }
    
    public static List extractColumn(ResultSet tableResultSet, String columnName) {
        List tables = new ArrayList();
        
        try {
            while (tableResultSet.next()) {
                tables.add(tableResultSet.getString(columnName));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tables;
    }
    
    public static void dumpCatalogDetails(DatabaseMetaData databaseMetaData, PrintWriter pw) {
        try {
            pw.println(databaseMetaData.getCatalogTerm());
            ResultSet rs = databaseMetaData.getCatalogs();
            while ( rs.next()) {
                int cnt = rs.getMetaData().getColumnCount();
                while ( --cnt >= 0 ) {
                    pw.println("Col: " + rs.getMetaData().getColumnName(cnt));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static PrintWriter getPrintWriter(Writer writer) {
        if ( writer instanceof PrintWriter) {
            return (PrintWriter)writer;
        } else {
            return new PrintWriter(new BufferedWriter(writer));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy