public ResultSet getColumns()

in src/main/java/com/aliyun/odps/jdbc/OdpsDatabaseMetaData.java [1092:1203]


  public ResultSet getColumns(
      String catalog,
      String schemaPattern,
      String tableNamePattern,
      String columnNamePattern) throws SQLException {

    long begin = System.currentTimeMillis();

    if (tableNamePattern == null) {
      throw new SQLException("Table name must be given when getColumns");
    }

    List<Object[]> rows = new ArrayList<Object[]>();

    if (!tableNamePattern.trim().isEmpty() && !"%".equals(tableNamePattern.trim())
        && !"*".equals(tableNamePattern.trim())) {
      try {
        Table table;
        if (conn.isOdpsNamespaceSchema()) {
          table = conn.getOdps().tables().get(conn.getOdps().getDefaultProject(), schemaPattern, tableNamePattern);
        } else {
          if (StringUtils.isNullOrEmpty(catalog)) {
            table = conn.getOdps().tables().get(tableNamePattern);
          } else {
            table = conn.getOdps().tables().get(catalog, tableNamePattern);
          }
        }
        table.reload();

        // Read column & partition column information from table schema
        List<Column> columns = new LinkedList<>();
        columns.addAll(table.getSchema().getColumns());
        columns.addAll(table.getSchema().getPartitionColumns());
        for (int i = 0; i < columns.size(); i++) {
          Column col = columns.get(i);
          JdbcColumn jdbcCol = new JdbcColumn(col.getName(),
                                              tableNamePattern,
                                              table.getProject(),
                                              col.getTypeInfo().getOdpsType(),
                                              col.getTypeInfo(),
                                              col.getComment(),
                                              i + 1);
          Object[] rowVals =
              {catalog, // table catalog (odps project)
               jdbcCol.getTableSchema(), // table schema (odps project)
               jdbcCol.getTableName(), // table name
               jdbcCol.getColumnName(), // column name
               (long) jdbcCol.getType(), // SQL type from java.sql.Types
               jdbcCol.getTypeName(), // Data source dependent type name, actually odps typeInfo name
               null, // column size
               null, // not used
               (long) jdbcCol.getDecimalDigits(), // the number of fractional digits.
               (long) jdbcCol.getNumPercRaidx(), // Radix (typically either 10 or 2)
               (long) jdbcCol.getIsNullable(), // is NULL allowed.
               jdbcCol.getComment(), // comment describing column (may be null)
               null, // default value for the column
               null, // unused
               null, // unused
               null, // for char types the maximum number of bytes in the column
               (long) jdbcCol.getOrdinalPos(), // index of column in table (start at 1)
               jdbcCol.getIsNullableString(), // ISO rules are used to determine the nullability for a column.
               null, // SCOPE_CATALOG
               null, // SCOPE_SCHEMA
               null, // SCOPE_TABLE
               null, // SOURCE_DATA_TYPE
               "NO", // IS_AUTOINCREMENT
               "NO", // IS_GENERATEDCOLUMN
               };

          rows.add(rowVals);
        }
      } catch (OdpsException e) {
        throw new SQLException("catalog=" + catalog + ",schemaPattern=" + schemaPattern
                               + ",tableNamePattern=" + tableNamePattern + ",columnNamePattern"
                               + columnNamePattern, e);
      }
    }

    long end = System.currentTimeMillis();
    log.info("It took me " + (end - begin) + " ms to get " + rows.size() + " columns");

    // Build result set meta data
    OdpsResultSetMetaData meta =
        new OdpsResultSetMetaData(Arrays.asList("TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME",
                                                "COLUMN_NAME", "DATA_TYPE", "TYPE_NAME",
                                                "COLUMN_SIZE", "BUFFER_LENGTH",
                                                "DECIMAL_DIGITS", "NUM_PERC_RADIX", "NULLABLE",
                                                "REMARKS", "COLUMN_DEF",
                                                "SQL_DATA_TYPE", "SQL_DATETIME_SUB",
                                                "CHAR_OCTET_LENGTH", "ORDINAL_POSITION",
                                                "IS_NULLABLE", "SCOPE_CATALOG", "SCOPE_SCHEMA",
                                                "SCOPE_TABLE", "SOURCE_DATA_TYPE",
                                                "IS_AUTOINCREMENT", "IS_GENERATEDCOLUMN"),
                                  Arrays.asList(TypeInfoFactory.STRING, TypeInfoFactory.STRING,
                                                TypeInfoFactory.STRING,
                                                TypeInfoFactory.STRING, TypeInfoFactory.BIGINT,
                                                TypeInfoFactory.STRING,
                                                TypeInfoFactory.BIGINT, TypeInfoFactory.BIGINT,
                                                TypeInfoFactory.BIGINT,
                                                TypeInfoFactory.BIGINT, TypeInfoFactory.BIGINT,
                                                TypeInfoFactory.STRING,
                                                TypeInfoFactory.STRING, TypeInfoFactory.BIGINT,
                                                TypeInfoFactory.BIGINT,
                                                TypeInfoFactory.BIGINT, TypeInfoFactory.BIGINT,
                                                TypeInfoFactory.STRING,
                                                TypeInfoFactory.STRING, TypeInfoFactory.STRING,
                                                TypeInfoFactory.STRING,
                                                TypeInfoFactory.BIGINT, TypeInfoFactory.STRING,
                                                TypeInfoFactory.STRING));

    return new OdpsStaticResultSet(getConnection(), meta, rows.iterator());
  }