Java Code Examples for io.vertx.sqlclient.Tuple#getValue()

The following examples show how to use io.vertx.sqlclient.Tuple#getValue() . 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: PgClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void jsonExample() {

    // Create a tuple
    Tuple tuple = Tuple.of(
      Tuple.JSON_NULL,
      new JsonObject().put("foo", "bar"),
      3);

    // Retrieving json
    Object value = tuple.getValue(0); // Expect JSON_NULL

    //
    value = tuple.get(JsonObject.class, 1); // Expect JSON object

    //
    value = tuple.get(Integer.class, 2); // Expect 3
    value = tuple.getInteger(2); // Expect 3
  }
 
Example 2
Source File: MySQLClientExamples.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
public void jsonExample() {

    // Create a tuple
    Tuple tuple = Tuple.of(
      Tuple.JSON_NULL,
      new JsonObject().put("foo", "bar"),
      3);

    // Retrieving json
    Object value = tuple.getValue(0); // Expect JSON_NULL

    //
    value = tuple.get(JsonObject.class, 1); // Expect JSON object

    //
    value = tuple.get(Integer.class, 2); // Expect 3
    value = tuple.getInteger(2); // Expect 3
  }
 
Example 3
Source File: ExtendedQueryCommandBaseCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private static Object[] sanitize(Tuple params) {
  Object[] inputs = new Object[params.size()];
  for (int i = 0; i < params.size(); i++) {
    Object val = params.getValue(i);
    if (val instanceof Numeric)
      val = ((Numeric) val).bigDecimalValue();
    if (val instanceof Buffer)
      val = ((Buffer) val).getByteBuf();
    inputs[i] = val;
  }
  return inputs;
}
 
Example 4
Source File: ExtendedQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
private String parseParamDefinitions(Tuple params) {
  StringBuilder stringBuilder = new StringBuilder();
  for (int i = 0; i < params.size(); i++) {
    Object param = params.getValue(i);
    stringBuilder.append("@P").append(i + 1).append(" ");
    stringBuilder.append(inferenceParamDefinitionByValueType(param));
    if (i != params.size() - 1) {
      stringBuilder.append(",");
    }
  }
  return stringBuilder.toString();
}
 
Example 5
Source File: ArrayTuple.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
public ArrayTuple(Tuple tuple) {
  values = new Object[tuple.size()];
  size = values.length;
  for (int idx = 0;idx < size;idx++) {
    values[idx] = tuple.getValue(idx);
  }
}
 
Example 6
Source File: PgEncoder.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
/**
 * <p>
 * The message gives the name of the prepared statement, the name of portal,
 * and the values to use for any parameter values present in the prepared statement.
 * The supplied parameter set must match those needed by the prepared statement.
 * <p>
 * The response is either {@link BindComplete} or {@link ErrorResponse}.
 */
void writeBind(Bind bind, String portal, Tuple paramValues) {
  ensureBuffer();
  int pos = out.writerIndex();
  out.writeByte(BIND);
  out.writeInt(0);
  if (portal != null) {
    out.writeCharSequence(portal, UTF_8);
  }
  out.writeByte(0);
  if (bind.statement == 0) {
    out.writeByte(0);
  } else {
    out.writeLong(bind.statement);
  }
  int paramLen = paramValues.size();
  out.writeShort(paramLen);
  // Parameter formats
  for (int c = 0;c < paramLen;c++) {
    // for now each format is Binary
    out.writeShort(bind.paramTypes[c].supportsBinary ? 1 : 0);
  }
  out.writeShort(paramLen);
  for (int c = 0;c < paramLen;c++) {
    Object param = paramValues.getValue(c);
    if (param == null) {
      // NULL value
      out.writeInt(-1);
    } else {
      DataType dataType = bind.paramTypes[c];
      if (dataType.supportsBinary) {
        int idx = out.writerIndex();
        out.writeInt(0);
        DataTypeCodec.encodeBinary(dataType, param, out);
        out.setInt(idx, out.writerIndex() - idx - 4);
      } else {
        DataTypeCodec.encodeText(dataType, param, out);
      }
    }
  }

  // MAKE resultColumsn non null to avoid null check

  // Result columns are all in Binary format
  if (bind.resultColumns.length > 0) {
    out.writeShort(bind.resultColumns.length);
    for (PgColumnDesc resultColumn : bind.resultColumns) {
      out.writeShort(resultColumn.dataType.supportsBinary ? 1 : 0);
    }
  } else {
    out.writeShort(1);
    out.writeShort(1);
  }
  out.setInt(pos + 1, out.writerIndex() - pos - 1);
}
 
Example 7
Source File: ExtendedQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private void sendStatementExecuteCommand(MySQLPreparedStatement statement, boolean sendTypesToServer, Tuple params, byte cursorType) {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_STMT_EXECUTE);
  packet.writeIntLE((int) statement.statementId);
  packet.writeByte(cursorType);
  // iteration count, always 1
  packet.writeIntLE(1);

  int numOfParams = statement.bindingTypes().length;
  int bitmapLength = (numOfParams + 7) / 8;
  byte[] nullBitmap = new byte[bitmapLength];

  int pos = packet.writerIndex();

  if (numOfParams > 0) {
    // write a dummy bitmap first
    packet.writeBytes(nullBitmap);
    packet.writeBoolean(sendTypesToServer);

    if (sendTypesToServer) {
      for (DataType bindingType : statement.bindingTypes()) {
        packet.writeByte(bindingType.id);
        packet.writeByte(0); // parameter flag: signed
      }
    }

    for (int i = 0; i < numOfParams; i++) {
      Object value = params.getValue(i);
      if (value != null) {
        DataTypeCodec.encodeBinary(statement.bindingTypes()[i], value, encoder.encodingCharset, packet);
      } else {
        nullBitmap[i / 8] |= (1 << (i & 7));
      }
    }

    // padding null-bitmap content
    packet.setBytes(pos, nullBitmap);
  }

  // set payload length
  int payloadLength = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, payloadLength);

  sendPacket(packet, payloadLength);
}
 
Example 8
Source File: ExtendedBatchQueryCommandCodec.java    From vertx-sql-client with Apache License 2.0 4 votes vote down vote up
private void sendBatchStatementExecuteCommand(MySQLPreparedStatement statement, Tuple params) {
  ByteBuf packet = allocateBuffer();
  // encode packet header
  int packetStartIdx = packet.writerIndex();
  packet.writeMediumLE(0); // will set payload length later by calculation
  packet.writeByte(sequenceId);

  // encode packet payload
  packet.writeByte(CommandType.COM_STMT_EXECUTE);
  packet.writeIntLE((int) statement.statementId);
  packet.writeByte(CURSOR_TYPE_NO_CURSOR);
  // iteration count, always 1
  packet.writeIntLE(1);

  /*
   * Null-bit map and type should always be reconstructed for every batch of parameters here
   */
  int numOfParams = statement.bindingTypes().length;
  int bitmapLength = (numOfParams + 7) / 8;
  byte[] nullBitmap = new byte[bitmapLength];

  int pos = packet.writerIndex();

  if (numOfParams > 0) {
    // write a dummy bitmap first
    packet.writeBytes(nullBitmap);
    packet.writeByte(1);
    for (int i = 0; i < params.size(); i++) {
      Object param = params.getValue(i);
      DataType dataType = DataTypeCodec.inferDataTypeByEncodingValue(param);
      packet.writeByte(dataType.id);
      packet.writeByte(0); // parameter flag: signed
    }

    for (int i = 0; i < numOfParams; i++) {
      Object value = params.getValue(i);
      if (value != null) {
        DataTypeCodec.encodeBinary(DataTypeCodec.inferDataTypeByEncodingValue(value), value, encoder.encodingCharset, packet);
      } else {
        nullBitmap[i / 8] |= (1 << (i & 7));
      }
    }

    // padding null-bitmap content
    packet.setBytes(pos, nullBitmap);
  }

  // set payload length
  int payloadLength = packet.writerIndex() - packetStartIdx - 4;
  packet.setMediumLE(packetStartIdx, payloadLength);

  sendPacket(packet, payloadLength);
}