Java Code Examples for org.apache.calcite.avatica.Meta#DatabaseProperty

The following examples show how to use org.apache.calcite.avatica.Meta#DatabaseProperty . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalciteRemoteDriverTest.java    From Quicksql with MIT License 5 votes vote down vote up
@Test public void testMetaFunctionsLocal() throws Exception {
  final Connection connection =
      CalciteAssert.hr().connect();
  assertThat(connection.isClosed(), is(false));
  for (Meta.DatabaseProperty p : Meta.DatabaseProperty.values()) {
    switch (p) {
    case GET_NUMERIC_FUNCTIONS:
      assertThat(connection.getMetaData().getNumericFunctions(),
          not(equalTo("")));
      break;
    case GET_SYSTEM_FUNCTIONS:
      assertThat(connection.getMetaData().getSystemFunctions(),
          CoreMatchers.notNullValue());
      break;
    case GET_TIME_DATE_FUNCTIONS:
      assertThat(connection.getMetaData().getTimeDateFunctions(),
          not(equalTo("")));
      break;
    case GET_S_Q_L_KEYWORDS:
      assertThat(connection.getMetaData().getSQLKeywords(),
          not(equalTo("")));
      break;
    case GET_STRING_FUNCTIONS:
      assertThat(connection.getMetaData().getStringFunctions(),
          not(equalTo("")));
      break;
    default:
    }
  }
  connection.close();
  assertThat(connection.isClosed(), is(true));
}
 
Example 2
Source File: CalciteRemoteDriverTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testMetaFunctionsLocal() throws Exception {
  final Connection connection =
      CalciteAssert.hr().connect();
  assertThat(connection.isClosed(), is(false));
  for (Meta.DatabaseProperty p : Meta.DatabaseProperty.values()) {
    switch (p) {
    case GET_NUMERIC_FUNCTIONS:
      assertThat(connection.getMetaData().getNumericFunctions(),
          not(equalTo("")));
      break;
    case GET_SYSTEM_FUNCTIONS:
      assertThat(connection.getMetaData().getSystemFunctions(),
          CoreMatchers.notNullValue());
      break;
    case GET_TIME_DATE_FUNCTIONS:
      assertThat(connection.getMetaData().getTimeDateFunctions(),
          not(equalTo("")));
      break;
    case GET_S_Q_L_KEYWORDS:
      assertThat(connection.getMetaData().getSQLKeywords(),
          not(equalTo("")));
      break;
    case GET_STRING_FUNCTIONS:
      assertThat(connection.getMetaData().getStringFunctions(),
          not(equalTo("")));
      break;
    default:
    }
  }
  connection.close();
  assertThat(connection.isClosed(), is(true));
}
 
Example 3
Source File: Service.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
@JsonCreator
public DatabasePropertyResponse(@JsonProperty("map") Map<Meta.DatabaseProperty, Object> map,
    @JsonProperty("rpcMetadata") RpcMetadataResponse rpcMetadata) {
  this.map = map;
  this.rpcMetadata = rpcMetadata;
}
 
Example 4
Source File: Service.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
@Override DatabasePropertyResponse deserialize(Message genericMsg) {
  final Responses.DatabasePropertyResponse msg = ProtobufService.castProtobufMessage(genericMsg,
      Responses.DatabasePropertyResponse.class);
  HashMap<Meta.DatabaseProperty, Object> properties = new HashMap<>();
  for (Responses.DatabasePropertyElement property : msg.getPropsList()) {
    final Meta.DatabaseProperty dbProp = Meta.DatabaseProperty.fromProto(property.getKey());
    final Common.TypedValue value = property.getValue();

    Object obj;
    switch (dbProp) {
    // Just need to keep parity with the exposed values on DatabaseProperty
    case GET_NUMERIC_FUNCTIONS:
    case GET_STRING_FUNCTIONS:
    case GET_SYSTEM_FUNCTIONS:
    case GET_TIME_DATE_FUNCTIONS:
    case GET_S_Q_L_KEYWORDS:
      // String
      if (Common.Rep.STRING != value.getType()) {
        throw new IllegalArgumentException("Expected STRING, but got " + value.getType());
      }

      obj = value.getStringValue();
      break;
    case GET_DEFAULT_TRANSACTION_ISOLATION:
      // int
      if (Common.Rep.INTEGER != value.getType()) {
        throw new IllegalArgumentException("Expected INTEGER, but got " + value.getType());
      }

      obj = (int) value.getNumberValue();
      break;
    default:
      switch (value.getType()) {
      case INTEGER:
        obj = Long.valueOf(value.getNumberValue()).intValue();
        break;
      case STRING:
        obj = value.getStringValue();
        break;
      default:
        throw new IllegalArgumentException("Unhandled value type, " + value.getType());
      }

      break;
    }

    properties.put(dbProp, obj);
  }

  RpcMetadataResponse metadata = null;
  if (msg.hasField(METADATA_DESCRIPTOR)) {
    metadata = RpcMetadataResponse.fromProto(msg.getMetadata());
  }

  return new DatabasePropertyResponse(properties, metadata);
}
 
Example 5
Source File: Service.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
@Override Responses.DatabasePropertyResponse serialize() {
  Responses.DatabasePropertyResponse.Builder builder = Responses.DatabasePropertyResponse
      .newBuilder();

  if (null != map) {
    for (Entry<Meta.DatabaseProperty, Object> entry : map.entrySet()) {
      Object obj = entry.getValue();

      Common.TypedValue.Builder valueBuilder = Common.TypedValue.newBuilder();
      switch (entry.getKey()) {
      // Just need to keep parity with the exposed values on DatabaseProperty
      case GET_NUMERIC_FUNCTIONS:
      case GET_STRING_FUNCTIONS:
      case GET_SYSTEM_FUNCTIONS:
      case GET_TIME_DATE_FUNCTIONS:
      case GET_S_Q_L_KEYWORDS:
        // String
        if (!(obj instanceof String)) {
          throw new RuntimeException("Expected a String, but got " + obj.getClass());
        }

        valueBuilder.setType(Common.Rep.STRING).setStringValue((String) obj);
        break;
      case GET_DEFAULT_TRANSACTION_ISOLATION:
        // int
        if (!(obj instanceof Integer)) {
          throw new RuntimeException("Expected an Integer, but got " + obj.getClass());
        }

        valueBuilder.setType(Common.Rep.INTEGER).setNumberValue(((Integer) obj).longValue());
        break;
      default:
        if (obj instanceof Integer) {
          valueBuilder.setType(Common.Rep.INTEGER).setNumberValue((Integer) obj);
        } else {
          String value;
          if (obj instanceof String) {
            value = (String) obj;
          } else {
            value = obj.toString();
          }
          valueBuilder.setType(Common.Rep.STRING).setStringValue(value);
        }

        break;
      }

      builder.addProps(Responses.DatabasePropertyElement.newBuilder()
          .setKey(entry.getKey().toProto()).setValue(valueBuilder.build()));
    }
  }

  if (null != rpcMetadata) {
    builder.setMetadata(rpcMetadata.serialize());
  }

  return builder.build();
}
 
Example 6
Source File: ProtobufTranslationImplTest.java    From calcite-avatica with Apache License 2.0 4 votes vote down vote up
/**
 * Generates a collection of Responses whose serialization will be tested.
 */
private static List<Response> getResponses() {
  final RpcMetadataResponse rpcMetadata = new RpcMetadataResponse("localhost:8765");
  LinkedList<Response> responses = new LinkedList<>();

  // Nested classes (Signature, ColumnMetaData, CursorFactory, etc) are implicitly getting tested)

  // Stub out the metadata for a row
  ScalarType arrayComponentType = ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER);
  ColumnMetaData arrayColumnMetaData = getArrayColumnMetaData(arrayComponentType, 2, "counts");
  List<ColumnMetaData> columns =
      Arrays.asList(MetaImpl.columnMetaData("str", 0, String.class, true),
          MetaImpl.columnMetaData("count", 1, Integer.class, true),
          arrayColumnMetaData);
  List<AvaticaParameter> params =
      Arrays.asList(
          new AvaticaParameter(false, 10, 0, Types.VARCHAR, "VARCHAR",
              String.class.getName(), "str"));
  Meta.CursorFactory cursorFactory = Meta.CursorFactory.create(Style.LIST, Object.class,
      Arrays.asList("str", "count", "counts"));
  // The row values
  List<Object> rows = new ArrayList<>();
  rows.add(new Object[] {"str_value1", 50, Arrays.asList(1, 2, 3)});
  rows.add(new Object[] {"str_value2", 100, Arrays.asList(1)});

  // Create the signature and frame using the metadata and values
  Signature signature = Signature.create(columns, "sql", params, cursorFactory,
      Meta.StatementType.SELECT);
  Frame frame = Frame.create(Integer.MAX_VALUE, true, rows);

  // And then create a ResultSetResponse
  ResultSetResponse results1 = new ResultSetResponse("connectionId", Integer.MAX_VALUE, true,
      signature, frame, Long.MAX_VALUE, rpcMetadata);
  responses.add(results1);

  responses.add(new CloseStatementResponse(rpcMetadata));

  ConnectionPropertiesImpl connProps = new ConnectionPropertiesImpl(false, true,
      Integer.MAX_VALUE, "catalog", "schema");
  responses.add(new ConnectionSyncResponse(connProps, rpcMetadata));

  responses.add(new OpenConnectionResponse(rpcMetadata));
  responses.add(new CloseConnectionResponse(rpcMetadata));

  responses.add(new CreateStatementResponse("connectionId", Integer.MAX_VALUE, rpcMetadata));

  Map<Meta.DatabaseProperty, Object> propertyMap = new HashMap<>();
  for (Meta.DatabaseProperty prop : Meta.DatabaseProperty.values()) {
    propertyMap.put(prop, prop.defaultValue);
  }
  responses.add(new DatabasePropertyResponse(propertyMap, rpcMetadata));

  responses.add(
      new ExecuteResponse(Arrays.asList(results1, results1, results1), false, rpcMetadata));
  responses.add(new FetchResponse(frame, false, false, rpcMetadata));
  responses.add(new FetchResponse(frame, true, true, rpcMetadata));
  responses.add(new FetchResponse(frame, false, true, rpcMetadata));
  responses.add(
      new PrepareResponse(
          new Meta.StatementHandle("connectionId", Integer.MAX_VALUE, signature),
          rpcMetadata));

  StringWriter sw = new StringWriter();
  new Exception().printStackTrace(new PrintWriter(sw));
  responses.add(
      new ErrorResponse(Collections.singletonList(sw.toString()), "Test Error Message",
          ErrorResponse.UNKNOWN_ERROR_CODE, ErrorResponse.UNKNOWN_SQL_STATE,
          AvaticaSeverity.WARNING, rpcMetadata));

  // No more results, statement not missing
  responses.add(new SyncResultsResponse(false, false, rpcMetadata));
  // Missing statement, no results
  responses.add(new SyncResultsResponse(false, true, rpcMetadata));
  // More results, no missing statement
  responses.add(new SyncResultsResponse(true, false, rpcMetadata));

  // Some tests to make sure ErrorResponse doesn't fail.
  responses.add(new ErrorResponse((List<String>) null, null, 0, null, null, null));
  responses.add(
      new ErrorResponse(Arrays.asList("stacktrace1", "stacktrace2"), null, 0, null, null, null));

  responses.add(new CommitResponse());
  responses.add(new RollbackResponse());

  long[] updateCounts = new long[]{1, 0, 1, 1};
  responses.add(
      new ExecuteBatchResponse("connectionId", 12345, updateCounts, false, rpcMetadata));

  return responses;
}