package io.vertx.jdbcclient.impl.actions;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.jdbc.impl.actions.AbstractJDBCAction;
import io.vertx.ext.jdbc.impl.actions.JDBCStatementHelper;
import io.vertx.ext.sql.SQLOptions;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCPrepareStatementAction extends AbstractJDBCAction<io.vertx.sqlclient.impl.PreparedStatement> {
private final String sql;
public JDBCPrepareStatementAction(JDBCStatementHelper helper, SQLOptions options, String sql) {
super(helper, options);
this.sql = sql;
}
@Override
public io.vertx.sqlclient.impl.PreparedStatement execute(Connection conn) throws SQLException {
boolean autoGeneratedKeys = options == null || options.isAutoGeneratedKeys();
boolean autoGeneratedIndexes = options != null && options.getAutoGeneratedKeysIndexes() != null;
final java.sql.PreparedStatement ps;
if (autoGeneratedKeys && !autoGeneratedIndexes) {
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
} else if (autoGeneratedIndexes) {
JsonArray indexes = options.getAutoGeneratedKeysIndexes();
try {
if (indexes.getValue(0) instanceof Number) {
int[] keys = new int[indexes.size()];
for (int i = 0; i < keys.length; i++) {
keys[i] = indexes.getInteger(i);
}
ps = conn.prepareStatement(sql, keys);
} else if (indexes.getValue(0) instanceof String) {
String[] keys = new String[indexes.size()];
for (int i = 0; i < keys.length; i++) {
keys[i] = indexes.getString(i);
}
ps = conn.prepareStatement(sql, keys);
} else {
throw new SQLException("Invalid type of index, only [int, String] allowed");
}
} catch (RuntimeException e) {
throw new SQLException(e);
}
} else {
ps = conn.prepareStatement(sql);
}
return new JDBCPreparedStatement(sql, ps);
}
}