Esempio n. 1
0
  final DatabaseModel read(Connection connection) throws SQLException {
    DatabaseMetaData metaData = connection.getMetaData();

    DatabaseModel m = DatabaseModel.create();

    try (ResultSet resultSet = metaData.getTables(null, null, null, null)) {
      while (resultSet.next()) {
        String c = resultSet.getString("TABLE_CAT");
        String s = resultSet.getString("TABLE_SCHEM");
        String n = resultSet.getString("TABLE_NAME");

        if (!Objects.equals(resultSet.getString("TABLE_TYPE"), "TABLE")) continue;

        if (ignore(c, s, n)) continue;

        m = m.addTable(TableModel.create(TableId.create(SchemaId.create(s), n)));
      }
    }

    try (ResultSet resultSet = metaData.getColumns(null, null, null, null)) {
      while (resultSet.next()) {
        String c = resultSet.getString("TABLE_CAT");
        String s = resultSet.getString("TABLE_SCHEM");
        String n = resultSet.getString("TABLE_NAME");

        TableId tableId = TableId.create(SchemaId.create(s), n);
        if (!m.hasTable(tableId)) continue;

        String col = resultSet.getString("COLUMN_NAME");

        int dataType = resultSet.getInt("DATA_TYPE");
        int columnSize = resultSet.getInt("COLUMN_SIZE");
        int decimalDigits = resultSet.getInt("DECIMAL_DIGITS");
        int numPrecRadix = resultSet.getInt("NUM_PREC_RADIX");
        ColumnModel.Builder cm = ColumnModel.newBuilder();

        cm = cm.withId(ColumnId.create(tableId, col));

        JDBCType dataType2 = JDBCType.valueOf(dataType);

        switch (dataType2) {
          case BIGINT:
            cm = cm.withType(BigIntType.create());
            break;
          case BOOLEAN:
            cm = cm.withType(BooleanType.create());
            break;
          case CHAR:
            cm = cm.withType(CharType.create());
            break;
          case BLOB:
            cm = cm.withType(BLOBType.create());
            break;
          case CLOB:
            cm = cm.withType(CLOBType.create());
            break;
          case DATE:
            cm = cm.withType(DateType.create());
            break;
          case DECIMAL:
            cm = cm.withType(DecimalType.create());
            break;
          case DOUBLE:
            cm = cm.withType(DoubleType.create());
            break;
          case INTEGER:
            cm = cm.withType(IntType.create());
            break;
          case SMALLINT:
            cm = cm.withType(SmallIntType.create());
            break;
          case VARCHAR:
            cm = cm.withType(VarcharType.create());
            break;
          case TIME:
            cm = cm.withType(TimeType.create());
            break;
          case TIMESTAMP:
            cm = cm.withType(TimestampType.create());
            break;
          case TINYINT:
            cm = cm.withType(TinyIntType.create());
            break;
          case VARBINARY:
            cm = cm.withType(VarbinaryType.create());
            break;
          case LONGVARCHAR:
            cm = cm.withType(LongVarcharType.create());
            break;
          default:
            throw new Error("missing type " + dataType + " " + dataType2);
        }
        m = m.addColumn(cm.build());
      }
    }

    m = postProcess(connection, m);

    return m;
  }