private void getTableMetadata()

in src/main/java/com/uber/uberscriptquery/jdbc/SingleTableJdbcWriter.java [160:254]


    private void getTableMetadata() {
        primaryKeys.clear();
        columnNames.clear();
        columnNamesLowerCase.clear();
        columnTypes.clear();

        Connection con = connectionProvider.getConnection();
        try {
            DatabaseMetaData metaData = con.getMetaData();

            String matchedDbName = null;

            ResultSet getTablesResultSet = metaData.getTables(null, null, "%", null);
            while (getTablesResultSet.next()) {
                String table = getTablesResultSet.getString(3);
                LOG.info("Checking table: " + table);
                if (this.tableName.equalsIgnoreCase(table)) {
                    matchedDbName = table;
                    ResultSet getColumnsResultSet = metaData.getColumns(null, null, table, null);
                    while (getColumnsResultSet.next()) {
                        String columnName = getColumnsResultSet.getString(4);
                        String columnType = getColumnsResultSet.getString(6);
                        columnNames.add(columnName);
                        columnNamesLowerCase.add(columnName.toLowerCase());
                        columnTypes.put(columnName.toLowerCase(), columnType);
                        LOG.info(String.format("Found column: %s, %s", columnName, columnType));
                    }
                }
            }

            if (matchedDbName != null) {
                ResultSet getExportedKeysResultSet = metaData.getPrimaryKeys(null, null, tableName);
                while (getExportedKeysResultSet.next()) {
                    String columnName = getExportedKeysResultSet.getString(4);
                    primaryKeys.add(columnName);
                    LOG.info("Found primay key: " + columnName);
                }
            }

            if (!primaryKeys.isEmpty() && !columnNames.isEmpty()) {
                return;
            }

            ResultSet getCatalogsResult = metaData.getCatalogs();
            while (getCatalogsResult.next()) {
                String catalog = String.valueOf(getCatalogsResult.getObject(1));
                LOG.info("Checking catalog: " + catalog);
                ResultSet getSchemasResult = metaData.getSchemas(catalog, null);
                while (getSchemasResult.next()) {
                    String schema = String.valueOf(getSchemasResult.getObject(1));
                    LOG.info("Checking schema: " + schema);
                    ResultSet getTablesResult = metaData.getTables(catalog, schema, null, null);
                    while (getTablesResult.next()) {
                        String table = getTablesResult.getString(3);
                        LOG.info("Checking table: " + table);
                        if (this.tableName.equalsIgnoreCase(table)) {
                            if (matchedDbName == null) {
                                matchedDbName = table;
                            }
                            if (columnNames.isEmpty()) {
                                ResultSet getColumnsResultSet = metaData.getColumns(catalog, schema, table, null);
                                while (getColumnsResultSet.next()) {
                                    String columnName = getColumnsResultSet.getString(4);
                                    String columnType = getColumnsResultSet.getString(6);
                                    columnNames.add(columnName);
                                    columnNamesLowerCase.add(columnName.toLowerCase());
                                    columnTypes.put(columnName.toLowerCase(), columnType);
                                    LOG.info(String.format("Found column: %s, %s", columnName, columnType));
                                }
                            }
                        }
                    }
                }
            }

            if (matchedDbName != null && primaryKeys.isEmpty()) {
                ResultSet getExportedKeysResultSet = metaData.getPrimaryKeys(null, null, tableName);
                while (getExportedKeysResultSet.next()) {
                    String columnName = getExportedKeysResultSet.getString(4);
                    primaryKeys.add(columnName);
                    LOG.info("Found primay key: " + columnName);
                }
            }

            if (primaryKeys.isEmpty()) {
                LOG.warn(String.format("Jdbc table %s has no primary key. Writing to the table may cause duplicate rows.", tableName));
            }

            if (columnNames.isEmpty()) {
                throw new RuntimeException(String.format("Failed to get columns for jdbc table %s", tableName));
            }
        } catch (SQLException e) {
            throw new RuntimeException(String.format("Failed to get meta data for %s table %s", connectionProvider.getJdbcConnectionString(), tableName), e);
        }
    }