private void queryInformationSchema()

in src/main/java/com/google/cloud/spanner/pgadapter/statements/CopyStatement.java [332:388]


  private void queryInformationSchema(BackendConnection backendConnection) {
    Map<String, Type> tableColumns = new LinkedHashMap<>();
    String sql =
        "SELECT "
            + COLUMN_NAME
            + ", "
            + DATA_TYPE
            + " FROM information_schema.columns "
            + "WHERE table_schema = $1 "
            + "AND table_name = $2 ";
    // We can't use ANY (and similar) with an array parameter (yet).
    if (getCopyColumnNames() != null && !getCopyColumnNames().isEmpty()) {
      sql +=
          "and column_name in "
              + IntStream.rangeClosed(3, getCopyColumnNames().size() + 2)
                  .mapToObj(i -> String.format("$%d", i))
                  .collect(Collectors.joining(", ", "(", ")"));
    }
    sql += " ORDER BY ordinal_position";
    Statement.Builder builder =
        Statement.newBuilder(sql)
            .bind("p1")
            .to(
                getTableName().schema == null
                    ? backendConnection.getCurrentSchema()
                    : getTableName().getUnquotedSchema())
            .bind("p2")
            .to(getTableName().getUnquotedName());
    if (getCopyColumnNames() != null && !getCopyColumnNames().isEmpty()) {
      int paramIndex = 3;
      for (TableOrIndexName columnName : getCopyColumnNames()) {
        builder.bind(String.format("p%d", paramIndex)).to(columnName.getUnquotedName());
        paramIndex++;
      }
    }
    Statement statement = builder.build();
    try (ResultSet result = connection.getDatabaseClient().singleUse().executeQuery(statement)) {
      while (result.next()) {
        String columnName = result.getString(COLUMN_NAME);
        Type type = parsePostgreSQLDataType(result.getString(DATA_TYPE));
        tableColumns.put(columnName, type);
      }
    }

    if (tableColumns.isEmpty()) {
      throw SpannerExceptionFactory.newSpannerException(
          ErrorCode.INVALID_ARGUMENT,
          "Table " + getTableName() + " is not found in information_schema");
    }

    this.tableColumns = tableColumns;

    if (getCopyColumnNames() != null) {
      verifyCopyColumns();
    }
    this.indexedColumnsCount = queryIndexedColumnsCount(backendConnection, tableColumns.keySet());
  }