private Optional<? extends Table> loadTable(DatabaseAndTable databaseAndTable) { try (Connection conn = dataSource.getConnection()) { try (PreparedStatement stmt = conn.prepareStatement(TABLE_SCHEMA_SQL)) { stmt.setString(1, databaseAndTable.getDatabase()); stmt.setString(2, databaseAndTable.getTable()); ResultSet rs = stmt.executeQuery(); List<Column> columns = new ArrayList<>(); while (rs.next()) { String name = rs.getString(1); String type = rs.getString(2); columns.add(new Column(name, MysqlType.of(type))); } if (columns.isEmpty()) { return Optional.absent(); } else { return Optional.of( new TableImpl(databaseAndTable.getDatabase(), databaseAndTable.getTable(), columns)); } } } catch (SQLException e) { String err = String.format( "Error getting '%s.%s' schema", databaseAndTable.getDatabase(), databaseAndTable.getTable()); LOG.error(err, e); throw new SchemaLoadException(err, e); } }
public static MysqlType of(String name) { // at least now we are not interested in precision - cut it off String typeName = name; int i = typeName.indexOf('('); if (i > -1) { typeName = typeName.substring(0, i); } for (MysqlType t : MysqlType.values()) { if (t.name.toLowerCase().equals(typeName.toLowerCase())) { return t; } } LOG.warn("Encountered unsupported mysql type {}", name); return UNSUPPORTED; }