Java Code Examples for java.sql.PreparedStatement#isWrapperFor()

The following examples show how to use java.sql.PreparedStatement#isWrapperFor() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@Override
public final void setNonNullParameter(
    final PreparedStatement ps,
    final int parameterIndex,
    final T parameter,
    final JdbcType jdbcType) throws SQLException
{
  byte[] json = writeToJson(parameter);
  if (ps.isWrapperFor(org.h2.jdbc.JdbcPreparedStatement.class)) {
    // H2 only accepts JSON passed as UTF8 byte array
    ps.setBytes(parameterIndex, json);
  }
  else {
    // while PostgreSQL and other DBs want a UTF8 string
    ps.setString(parameterIndex, new String(json, UTF_8));
  }
}
 
Example 2
Source Project: presto   File: MySqlClient.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public PreparedStatement getPreparedStatement(Connection connection, String sql)
        throws SQLException
{
    PreparedStatement statement = connection.prepareStatement(sql);
    if (statement.isWrapperFor(Statement.class)) {
        statement.unwrap(Statement.class).enableStreamingResults();
    }
    return statement;
}
 
Example 3
Source Project: calcite-avatica   File: JdbcMeta.java    License: Apache License 2.0 4 votes vote down vote up
@Override public ExecuteResult execute(StatementHandle h,
    List<TypedValue> parameterValues, int maxRowsInFirstFrame) throws NoSuchStatementException {
  try {
    if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
      throw new SQLException("exception while executing query: unbound parameter");
    }

    final StatementInfo statementInfo = statementCache.getIfPresent(h.id);
    if (null == statementInfo) {
      throw new NoSuchStatementException(h);
    }
    final List<MetaResultSet> resultSets;
    final PreparedStatement preparedStatement =
        (PreparedStatement) statementInfo.statement;

    if (parameterValues != null) {
      for (int i = 0; i < parameterValues.size(); i++) {
        TypedValue o = parameterValues.get(i);
        preparedStatement.setObject(i + 1, o.toJdbc(calendar));
      }
    }

    if (preparedStatement.execute()) {
      final Signature signature2;
      if (preparedStatement.isWrapperFor(AvaticaPreparedStatement.class)) {
        signature2 = h.signature;
      } else {
        h.signature = signature(preparedStatement.getMetaData(),
            preparedStatement.getParameterMetaData(), h.signature.sql,
            Meta.StatementType.SELECT);
        signature2 = h.signature;
      }

      // Make sure we set this for subsequent fetch()'s to find the result set.
      statementInfo.setResultSet(preparedStatement.getResultSet());

      if (statementInfo.getResultSet() == null) {
        resultSets = Collections.<MetaResultSet>singletonList(
            JdbcResultSet.empty(h.connectionId, h.id, signature2));
      } else {
        resultSets = Collections.<MetaResultSet>singletonList(
            JdbcResultSet.create(h.connectionId, h.id, statementInfo.getResultSet(),
                maxRowsInFirstFrame, signature2));
      }
    } else {
      resultSets = Collections.<MetaResultSet>singletonList(
          JdbcResultSet.count(h.connectionId, h.id, preparedStatement.getUpdateCount()));
    }

    return new ExecuteResult(resultSets);
  } catch (SQLException e) {
    throw propagate(e);
  }
}
 
Example 4
Source Project: quark   File: QuarkMetaImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ExecuteResult execute(StatementHandle h,
                             List<TypedValue> parameterValues, long maxRowCount) {
  try {
    if (MetaImpl.checkParameterValueHasNull(parameterValues)) {
      throw new SQLException("exception while executing query: unbound parameter");
    }

    final StatementInfo statementInfo = Objects.requireNonNull(
        statementCache.getIfPresent(h.id),
        "Statement not found, potentially expired. " + h);
    final List<MetaResultSet> resultSets = new ArrayList<>();
    final PreparedStatement preparedStatement =
        (PreparedStatement) statementInfo.statement;

    if (parameterValues != null) {
      for (int i = 0; i < parameterValues.size(); i++) {
        TypedValue o = parameterValues.get(i);
        preparedStatement.setObject(i + 1, o.toJdbc(calendar));
      }
    }

    if (preparedStatement.execute()) {
      final Meta.Frame frame;
      final Signature signature2;
      if (preparedStatement.isWrapperFor(AvaticaPreparedStatement.class)) {
        signature2 = h.signature;
      } else {
        h.signature = signature(preparedStatement.getMetaData(),
            preparedStatement.getParameterMetaData(), h.signature.sql,
            Meta.StatementType.SELECT);
        signature2 = h.signature;
      }

      statementInfo.resultSet = preparedStatement.getResultSet();
      if (statementInfo.resultSet == null) {
        frame = Frame.EMPTY;
        resultSets.add(QuarkMetaResultSet.empty(h.connectionId, h.id, signature2));
      } else {
        resultSets.add(
            QuarkMetaResultSet.create(h.connectionId, h.id,
                statementInfo.resultSet, maxRowCount, signature2));
      }
    } else {
      resultSets.add(
          QuarkMetaResultSet.count(
              h.connectionId, h.id, preparedStatement.getUpdateCount()));
    }

    return new ExecuteResult(resultSets);
  } catch (SQLException e) {
    throw propagate(e);
  }
}
 
Example 5
Source Project: database   File: StatementAdaptor.java    License: Apache License 2.0 4 votes vote down vote up
public void addParameters(PreparedStatement ps, Object[] parameters) throws SQLException {
  for (int i = 0; i < parameters.length; i++) {
    Object parameter = parameters[i];

    // Unwrap secret args here so we can use them
    if (parameter instanceof SecretArg) {
      parameter = ((SecretArg) parameter).getArg();
    }

    if (parameter == null) {
      ParameterMetaData metaData;
      int parameterType;
      try {
        metaData = ps.getParameterMetaData();
        parameterType = metaData.getParameterType(i + 1);
      } catch (SQLException e) {
        throw new DatabaseException("Parameter " + (i + 1)
            + " was null and the JDBC driver could not report the type of this column."
            + " Please update the JDBC driver to support PreparedStatement.getParameterMetaData()"
            + " or use SqlNull in place of null values to this query.", e);
      }
      ps.setNull(i + 1, parameterType);
    } else if (parameter instanceof SqlNull) {
      SqlNull sqlNull = (SqlNull) parameter;
      if (options.useBytesForBlob() && sqlNull.getType() == Types.BLOB) {
        // The setNull() seems more correct, but PostgreSQL chokes on it
        ps.setBytes(i + 1, null);
      } else {
        ps.setNull(i + 1, sqlNull.getType());
      }
    } else if (parameter instanceof java.sql.Date) {
      ps.setDate( i + 1, (java.sql.Date) parameter);
    } else if (parameter instanceof Date) {
      // this will correct the millis and nanos according to the JDBC spec
      // if a correct Timestamp is passed in, this will detect that and leave it alone
      ps.setTimestamp(i + 1, toSqlTimestamp((Date) parameter), options.calendarForTimestamps());
    } else if (parameter instanceof Reader) {
      if (options.useStringForClob()) {
        ps.setString(i + 1, readerToString((Reader) parameter));
      } else {
        ps.setCharacterStream(i + 1, (Reader) parameter);
      }
    } else if (parameter instanceof InputStream) {
      if (options.useBytesForBlob()) {
        ps.setBytes(i + 1, streamToBytes((InputStream) parameter));
      } else {
        ps.setBinaryStream(i + 1, (InputStream) parameter);
      }
    } else if (parameter instanceof Float) {
      if (options.flavor() == Flavor.oracle && ps.isWrapperFor(OraclePreparedStatement.class)) {
        // The Oracle 11 driver setDouble() first converts the double to NUMBER, causing underflow
        // for small values so we need to use the proprietary mechanism
        ps.unwrap(OraclePreparedStatement.class).setBinaryFloat(i + 1, (Float) parameter);
      } else {
        ps.setFloat(i + 1, (Float) parameter);
      }
    } else if (parameter instanceof Double) {
      if (options.flavor() == Flavor.oracle && ps.isWrapperFor(OraclePreparedStatement.class)) {
        // The Oracle 11 driver setDouble() first converts the double to NUMBER, causing underflow
        // for small values so we need to use the proprietary mechanism
        ps.unwrap(OraclePreparedStatement.class).setBinaryDouble(i + 1, (Double) parameter);
      } else {
        ps.setDouble(i + 1, (Double) parameter);
      }
    } else {
      ps.setObject(i + 1, parameter);
    }
  }
}