This is an automated email from the ASF dual-hosted git repository. bossenti pushed a commit to branch STREAMPIPES-320 in repository https://gitbox.apache.org/repos/asf/incubator-streampipes-extensions.git
commit a5451c82bc8c6365d41268118b6f8e350889118f Author: bossenti <[email protected]> AuthorDate: Tue Apr 6 15:44:49 2021 +0200 [STREAMPIPES-320] create util class for statements --- .../jvm/jdbcclient/utils/StatementUtils.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/streampipes-sinks-databases-jvm/src/main/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/utils/StatementUtils.java b/streampipes-sinks-databases-jvm/src/main/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/utils/StatementUtils.java new file mode 100644 index 0000000..16eb56e --- /dev/null +++ b/streampipes-sinks-databases-jvm/src/main/java/org/apache/streampipes/sinks/databases/jvm/jdbcclient/utils/StatementUtils.java @@ -0,0 +1,50 @@ +package org.apache.streampipes.sinks.databases.jvm.jdbcclient.utils; + +import org.apache.streampipes.commons.exceptions.SpRuntimeException; + +import org.apache.streampipes.sinks.databases.jvm.jdbcclient.model.DbDataTypeFactory; +import org.apache.streampipes.sinks.databases.jvm.jdbcclient.model.ParameterInformation; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +public class StatementUtils { + + /** + * Sets the value in the prepardStatement {@code ps} + * + * @param p The needed info about the parameter (index and type) + * @param value The value of the object, which should be filled in the + * @param ps The prepared statement, which will be filled + * @throws SpRuntimeException When the data type in {@code p} is unknown + * @throws SQLException When the setters of the statement throw an + * exception (e.g. {@code setInt()}) + */ + public static void setValue(ParameterInformation p, Object value, PreparedStatement ps) + throws SQLException, SpRuntimeException { + switch (DbDataTypeFactory.getDataType(p.getDataType())) { + case Integer: + ps.setInt(p.getIndex(), (Integer) value); + break; + case Long: + ps.setLong(p.getIndex(), (Long) value); + break; + case Float: + ps.setFloat(p.getIndex(), (Float) value); + break; + case Double: + ps.setDouble(p.getIndex(), (Double) value); + break; + case Boolean: + ps.setBoolean(p.getIndex(), (Boolean) value); + break; + case String: + ps.setString(p.getIndex(), value.toString()); + break; + case Number: + case Sequence: + default: + throw new SpRuntimeException("Unknown SQL datatype"); + } + } +}
