/** * Sets a connection wide query timeout. * * It can be over written at any time and becomes active on the next query call. * * @param timeoutInSeconds the max amount of seconds the query can take to execute. */ @Fluent @Deprecated default SQLConnection setQueryTimeout(int timeoutInSeconds) { setOptions(new SQLOptions().setQueryTimeout(timeoutInSeconds)); return this; }
/** * Copy constructor * * @param other the result to copy */ public SQLOptions(SQLOptions other) { this.readOnly = other.isReadOnly(); this.catalog = other.getCatalog(); this.transactionIsolation = other.getTransactionIsolation(); this.resultSetType = other.getResultSetType(); this.resultSetConcurrency = other.getResultSetConcurrency(); this.autoGeneratedKeys = other.isAutoGeneratedKeys(); this.autoGeneratedKeysIndexes = other.getAutoGeneratedKeysIndexes(); this.schema = other.getSchema(); this.queryTimeout = other.getQueryTimeout(); this.fetchDirection = other.getFetchDirection(); this.fetchSize = other.getFetchSize(); }
private void applyConnectionOptions(Connection conn) throws SQLException { if (options != null) { if (options.isReadOnly()) { conn.setReadOnly(true); } if (options.getCatalog() != null) { conn.setCatalog(options.getCatalog()); } if (options.getSchema() != null) { conn.setSchema(options.getSchema()); } } }
void applyStatementOptions(Statement statement) throws SQLException { if (options != null) { if (options.getQueryTimeout() > 0) { statement.setQueryTimeout(options.getQueryTimeout()); } if (options.getFetchDirection() != null) { statement.setFetchDirection(options.getFetchDirection().getType()); } if (options.getFetchSize() > 0) { statement.setFetchSize(options.getFetchSize()); } } }
private Single<SQLConnection> connect() { return jdbc.rxGetConnection() .map(c -> c.setOptions(new SQLOptions().setAutoGeneratedKeys(true))); }
case "autoGeneratedKeys": if (member.getValue() instanceof Boolean) { obj.setAutoGeneratedKeys((Boolean)member.getValue()); obj.setAutoGeneratedKeysIndexes(((JsonArray)member.getValue()).copy()); obj.setCatalog((String)member.getValue()); obj.setFetchDirection(io.vertx.ext.sql.FetchDirection.valueOf((String)member.getValue())); obj.setFetchSize(((Number)member.getValue()).intValue()); obj.setQueryTimeout(((Number)member.getValue()).intValue()); obj.setReadOnly((Boolean)member.getValue()); obj.setResultSetConcurrency(io.vertx.ext.sql.ResultSetConcurrency.valueOf((String)member.getValue())); obj.setResultSetType(io.vertx.ext.sql.ResultSetType.valueOf((String)member.getValue())); obj.setSchema((String)member.getValue()); obj.setTransactionIsolation(io.vertx.ext.sql.TransactionIsolation.valueOf((String)member.getValue()));
if (!options.isAutoGeneratedKeys()) { generateKeys = false; return conn.prepareStatement(sql, Statement.NO_GENERATED_KEYS); if (options.getAutoGeneratedKeysIndexes() == null) { generateKeys = true; return conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); if (options.getAutoGeneratedKeysIndexes().getValue(0) instanceof String) { String[] columnNames = new String[options.getAutoGeneratedKeysIndexes().size()]; for (int i = 0; i < columnNames.length; i++) { columnNames[i] = options.getAutoGeneratedKeysIndexes().getString(i); if (options.getAutoGeneratedKeysIndexes().getValue(0) instanceof Number) { int[] columnIndexes = new int[options.getAutoGeneratedKeysIndexes().size()]; for (int i = 0; i < columnIndexes.length; i++) { columnIndexes[i] = options.getAutoGeneratedKeysIndexes().getInteger(i);
public static io.vertx.ext.sql.SQLConnection setOptions(io.vertx.ext.sql.SQLConnection j_receiver, java.util.Map<String, Object> options) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.setOptions(options != null ? new io.vertx.ext.sql.SQLOptions(io.vertx.core.impl.ConversionHelper.toJsonObject(options)) : null)); return j_receiver; } public static io.vertx.ext.sql.SQLConnection query(io.vertx.ext.sql.SQLConnection j_receiver, java.lang.String sql, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.Map<String, Object>>> resultHandler) {
@Override public Void execute(Connection conn) throws SQLException { conn.setAutoCommit(autoCommit); // setting the auto commit to false triggers a new transaction to begin if (!autoCommit) { if (options != null && options.getTransactionIsolation() != null) { conn.setTransactionIsolation(options.getTransactionIsolation().getType()); } } return null; }
if (options != null && options.getFetchSize() > 0) { fetchSize = options.getFetchSize(); } else { fetchSize = DEFAULT_ROW_STREAM_FETCH_SIZE;
@Test public void testInsertWithParameters() { final TimeZone tz = TimeZone.getDefault(); TimeZone.setDefault(TimeZone.getTimeZone("UTC")); SQLConnection conn = connection(); String sql = "INSERT INTO insert_table VALUES (?, ?, ?, ?);"; JsonArray params = new JsonArray().addNull().add("doe").add("jane").add("2002-02-02"); conn .setOptions(new SQLOptions().setAutoGeneratedKeys(true)) .updateWithParams(sql, params, onSuccess(result -> { assertUpdate(result, 1); int id = result.getKeys().getInteger(0); conn.queryWithParams("SElECT DOB FROM insert_table WHERE id=?;", new JsonArray().add(id), onSuccess(resultSet -> { assertNotNull(resultSet); assertEquals(1, resultSet.getResults().size()); assertEquals("2002-02-02", resultSet.getResults().get(0).getString(0)); TimeZone.setDefault(tz); testComplete(); })); })); await(); }
public static void toJson(SQLOptions obj, java.util.Map<String, Object> json) { json.put("autoGeneratedKeys", obj.isAutoGeneratedKeys()); if (obj.getAutoGeneratedKeysIndexes() != null) { json.put("autoGeneratedKeysIndexes", obj.getAutoGeneratedKeysIndexes()); } if (obj.getCatalog() != null) { json.put("catalog", obj.getCatalog()); } if (obj.getFetchDirection() != null) { json.put("fetchDirection", obj.getFetchDirection().name()); } json.put("fetchSize", obj.getFetchSize()); json.put("queryTimeout", obj.getQueryTimeout()); json.put("readOnly", obj.isReadOnly()); if (obj.getResultSetConcurrency() != null) { json.put("resultSetConcurrency", obj.getResultSetConcurrency().name()); } if (obj.getResultSetType() != null) { json.put("resultSetType", obj.getResultSetType().name()); } if (obj.getSchema() != null) { json.put("schema", obj.getSchema()); } if (obj.getTransactionIsolation() != null) { json.put("transactionIsolation", obj.getTransactionIsolation().name()); } } }
@Test public void testInsertWithNullParameters() { SQLConnection conn = connection(); String sql = "INSERT INTO insert_table (lname, fname, dob) VALUES (?, ?, ?)"; JsonArray params = new JsonArray().addNull().addNull().add("2002-02-02"); conn .setOptions(new SQLOptions().setAutoGeneratedKeys(true)) .updateWithParams(sql, params, onSuccess(result -> { assertUpdate(result, 1); int id = result.getKeys().getInteger(0); conn.queryWithParams("SElECT DOB FROM insert_table WHERE id=?", new JsonArray().add(id), onSuccess(resultSet -> { assertNotNull(resultSet); assertEquals(1, resultSet.getResults().size()); System.out.println(resultSet.getResults().get(0).getValue(0)); testComplete(); })); })); await(); }
.setOptions(new SQLOptions().setAutoGeneratedKeys(true)) .updateWithParams(sql, params, onSuccess((UpdateResult result) -> { assertUpdate(result, 1);
@Test public void testSelectTx() { String sql = "INSERT INTO insert_table VALUES (?, ?, ?, ?);"; JsonArray params = new JsonArray().addNull().add("smith").add("john").add("2003-03-03"); client.getConnection(onSuccess(conn -> { assertNotNull(conn); conn.setAutoCommit(false, onSuccess(v -> { conn .setOptions(new SQLOptions().setAutoGeneratedKeys(true)) .updateWithParams(sql, params, onSuccess((UpdateResult updateResult) -> { assertUpdate(updateResult, 1); int id = updateResult.getKeys().getInteger(0); // Explicit typing of resultset is not really necessary but without it IntelliJ reports // syntax error :( conn.queryWithParams("SELECT LNAME FROM insert_table WHERE id = ?", new JsonArray().add(id), onSuccess((ResultSet resultSet) -> { assertFalse(resultSet.getResults().isEmpty()); assertEquals("smith", resultSet.getResults().get(0).getString(0)); testComplete(); })); })); })); })); await(); }