|
| 1 | +package io.elastic.jdbc; |
| 2 | + |
| 3 | +import io.elastic.api.DynamicMetadataProvider; |
| 4 | +import io.elastic.api.SelectModelProvider; |
| 5 | +import java.sql.Connection; |
| 6 | +import java.sql.DatabaseMetaData; |
| 7 | +import java.sql.ResultSet; |
| 8 | +import java.sql.SQLException; |
| 9 | +import java.sql.Types; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import javax.json.Json; |
| 14 | +import javax.json.JsonObject; |
| 15 | +import javax.json.JsonObjectBuilder; |
| 16 | +import javax.json.JsonValue; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +public class PrimaryColumnNamesProvider implements DynamicMetadataProvider, SelectModelProvider { |
| 21 | + |
| 22 | + private static final Logger logger = LoggerFactory.getLogger(PrimaryColumnNamesProvider.class); |
| 23 | + |
| 24 | + public JsonObject getSelectModel(JsonObject configuration) { |
| 25 | + JsonObject result = Json.createObjectBuilder().build(); |
| 26 | + JsonObject properties = getPrimaryColumns(configuration); |
| 27 | + for (Map.Entry<String, JsonValue> entry : properties.entrySet()) { |
| 28 | + JsonValue field = entry.getValue(); |
| 29 | + result = Json.createObjectBuilder().add(entry.getKey(), field.toString()).build(); |
| 30 | + } |
| 31 | + return result; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns Columns list as metadata |
| 36 | + */ |
| 37 | + |
| 38 | + public JsonObject getMetaModel(JsonObject configuration) { |
| 39 | + JsonObject result = Json.createObjectBuilder().build(); |
| 40 | + JsonObject inMetadata = Json.createObjectBuilder().build(); |
| 41 | + JsonObject properties = getPrimaryColumns(configuration); |
| 42 | + inMetadata = Json.createObjectBuilder().add("type", "object") |
| 43 | + .add("properties", properties).build(); |
| 44 | + result = Json.createObjectBuilder().add("out", inMetadata) |
| 45 | + .add("in", inMetadata).build(); |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + public JsonObject getPrimaryColumns(JsonObject configuration) { |
| 50 | + if (configuration.getString("tableName") == null || configuration.getString("tableName") |
| 51 | + .isEmpty()) { |
| 52 | + throw new RuntimeException("Table name is required"); |
| 53 | + } |
| 54 | + String tableName = configuration.getString("tableName"); |
| 55 | + JsonObject properties = Json.createObjectBuilder().build(); |
| 56 | + Connection connection = null; |
| 57 | + ResultSet rs = null; |
| 58 | + String schemaName = null; |
| 59 | + Boolean isEmpty = true; |
| 60 | + Boolean isOracle = (configuration.getString("dbEngine").equals("oracle")) ? true : false; |
| 61 | + Boolean isMssql = (configuration.getString("dbEngine").equals("mssql")) ? true : false; |
| 62 | + List<String> primaryKeys = new ArrayList(); |
| 63 | + try { |
| 64 | + connection = Utils.getConnection(configuration); |
| 65 | + DatabaseMetaData dbMetaData = connection.getMetaData(); |
| 66 | + if (tableName.contains(".")) { |
| 67 | + schemaName = |
| 68 | + (isOracle) ? tableName.split("\\.")[0].toUpperCase() : tableName.split("\\.")[0]; |
| 69 | + tableName = |
| 70 | + (isOracle) ? tableName.split("\\.")[1].toUpperCase() : tableName.split("\\.")[1]; |
| 71 | + } |
| 72 | + rs = dbMetaData |
| 73 | + .getPrimaryKeys(null, ((isOracle && !schemaName.isEmpty()) ? schemaName : null), |
| 74 | + tableName); |
| 75 | + while (rs.next()) { |
| 76 | + primaryKeys.add(rs.getString("COLUMN_NAME")); |
| 77 | + logger.info("Primary Key: {}", rs.getString("COLUMN_NAME")); |
| 78 | + } |
| 79 | + rs = dbMetaData |
| 80 | + .getColumns(null, ((isOracle && !schemaName.isEmpty()) ? schemaName : null), tableName, |
| 81 | + "%"); |
| 82 | + while (rs.next()) { |
| 83 | + if (primaryKeys.contains(rs.getString("COLUMN_NAME"))) { |
| 84 | + JsonObjectBuilder field = Json.createObjectBuilder(); |
| 85 | + String name = rs.getString("COLUMN_NAME"); |
| 86 | + Boolean isRequired = false; |
| 87 | + if (isMssql) { |
| 88 | + String isAutoincrement = |
| 89 | + (rs.getString("IS_AUTOINCREMENT") != null) ? rs.getString("IS_AUTOINCREMENT") : ""; |
| 90 | + Integer isNullable = (rs.getObject("NULLABLE") != null) ? rs.getInt("NULLABLE") : 1; |
| 91 | + isRequired = isNullable == 0 && !isAutoincrement.equals("YES"); |
| 92 | + } else { |
| 93 | + isRequired = true; |
| 94 | + } |
| 95 | + field.add("required", isRequired) |
| 96 | + .add("title", name) |
| 97 | + .add("type", convertType(rs.getInt("DATA_TYPE"))); |
| 98 | + properties = Json.createObjectBuilder().add(name, field.build()).build(); |
| 99 | + isEmpty = false; |
| 100 | + } |
| 101 | + } |
| 102 | + if (isEmpty) { |
| 103 | + logger.info("Empty PK list - no primary keys"); |
| 104 | + throw new IllegalStateException("No Primary Keys"); |
| 105 | + } |
| 106 | + |
| 107 | + } catch (SQLException e) { |
| 108 | + throw new RuntimeException(e); |
| 109 | + } finally { |
| 110 | + if (rs != null) { |
| 111 | + try { |
| 112 | + rs.close(); |
| 113 | + } catch (SQLException e) { |
| 114 | + logger.error("Failed to close result set", e.toString()); |
| 115 | + } |
| 116 | + } |
| 117 | + if (connection != null) { |
| 118 | + try { |
| 119 | + connection.close(); |
| 120 | + } catch (SQLException e) { |
| 121 | + logger.error("Failed to close connection", e.toString()); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return properties; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Converts JDBC column type name to js type according to http://db.apache.org/ojb/docu/guides/jdbc-types.html |
| 130 | + * |
| 131 | + * @param sqlType JDBC column type |
| 132 | + * @url http://db.apache.org/ojb/docu/guides/jdbc-types.html |
| 133 | + */ |
| 134 | + private String convertType(Integer sqlType) { |
| 135 | + if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL || sqlType == Types.TINYINT |
| 136 | + || sqlType == Types.SMALLINT || sqlType == Types.INTEGER || sqlType == Types.BIGINT |
| 137 | + || sqlType == Types.REAL || sqlType == Types.FLOAT || sqlType == Types.DOUBLE) { |
| 138 | + return "number"; |
| 139 | + } |
| 140 | + if (sqlType == Types.BIT || sqlType == Types.BOOLEAN) { |
| 141 | + return "boolean"; |
| 142 | + } |
| 143 | + return "string"; |
| 144 | + } |
| 145 | +} |
0 commit comments