Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support MySQL with MariaDB engine gluu #18 #712

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,4 @@ PyCharm
# Mac
.DS_STORE
tmp
/.metadata/
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
Expand Down Expand Up @@ -52,7 +55,12 @@
*/
public class SqlConnectionProvider {

private static final Logger LOG = LoggerFactory.getLogger(SqlConnectionProvider.class);
private static final String JSON_TYPE_NAME = "json";

private static final Logger LOG = LoggerFactory.getLogger(SqlConnectionProvider.class);

private static final String QUERY_ENGINE_TYPE =
"SELECT TABLE_NAME, ENGINE FROM information_schema.tables WHERE table_schema = ?";

private static final String DRIVER_PROPERTIES_PREFIX = "connection.driver-property";

Expand All @@ -78,6 +86,7 @@ public class SqlConnectionProvider {
private SQLQueryFactory sqlQueryFactory;

private Map<String, Map<String, String>> tableColumnsMap;
private Map<String, String> tableEnginesMap = new HashMap<>();

protected SqlConnectionProvider() {
}
Expand Down Expand Up @@ -184,27 +193,48 @@ protected void init() throws Exception {
DatabaseMetaData databaseMetaData = con.getMetaData();
this.dbType = databaseMetaData.getDatabaseProductName().toLowerCase();
LOG.debug("Database product name: '{}'", dbType);
loadTableMetaData(databaseMetaData);
loadTableMetaData(databaseMetaData, con);
} catch (Exception ex) {
throw new ConnectionException("Failed to detect database product name", ex);
throw new ConnectionException("Failed to detect database product name and load metadata", ex);
}

this.creationResultCode = ResultCode.SUCCESS_INT_VALUE;
}

private void loadTableMetaData(DatabaseMetaData databaseMetaData) throws SQLException {
LOG.info("Scanning DB metadata...");

private void loadTableMetaData(DatabaseMetaData databaseMetaData, Connection con) throws SQLException {
long takes = System.currentTimeMillis();
ResultSet tableResultSet = databaseMetaData.getTables(null, schemaName, null, new String[]{"TABLE"});

LOG.info("Detecting engine types...");
PreparedStatement preparedStatement = con.prepareStatement(QUERY_ENGINE_TYPE);
preparedStatement.setString(1, schemaName);

ResultSet tableEnginesResultSet = preparedStatement.executeQuery();
while (tableEnginesResultSet.next()) {
String tableName = tableEnginesResultSet.getString("TABLE_NAME");
String engineName = tableEnginesResultSet.getString("ENGINE");

tableEnginesMap.put(tableName, engineName);
}

LOG.info("Scanning DB metadata...");
ResultSet tableResultSet = databaseMetaData.getTables(null, schemaName, null, new String[]{"TABLE"});
while (tableResultSet.next()) {
String tableName = tableResultSet.getString("TABLE_NAME");
Map<String, String> tableColumns = new HashMap<>();

String engineType = tableEnginesMap.get(tableName);

LOG.debug("Found table: '{}'.", tableName);
ResultSet columnResultSet = databaseMetaData.getColumns(null, schemaName, tableName, null);
while (columnResultSet.next()) {
tableColumns.put(columnResultSet.getString("COLUMN_NAME").toLowerCase(), columnResultSet.getString("TYPE_NAME").toLowerCase());
String columnName = columnResultSet.getString("COLUMN_NAME").toLowerCase();
String columTypeName = columnResultSet.getString("TYPE_NAME").toLowerCase();

String remark = columnResultSet.getString("REMARKS");
if ("mariadb".equalsIgnoreCase(engineType) && "longtext".equalsIgnoreCase(columTypeName) && "json".equalsIgnoreCase(remark)) {
columTypeName = JSON_TYPE_NAME;
}
tableColumns.put(columnName, columTypeName);
}

tableColumnsMap.put(tableName, tableColumns);
Expand Down Expand Up @@ -374,6 +404,12 @@ public TableMapping getTableMappingByKey(String key, String objectClass) {
return tableMapping;
}

public String getEngineType(String objectClass) {
String tableName = objectClass;

return tableEnginesMap.get(tableName);
}

public Connection getConnection() {
try {
return this.poolingDataSource.getConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private boolean addEntryImpl(TableMapping tableMapping, String key, Collection<A

for (AttributeData attribute : attributes) {
String attributeType = columTypes.get(attribute.getName().toLowerCase());
boolean multiValued = (attributeType != null) && "json".equals(attributeType);
boolean multiValued = (attributeType != null) && isJsonColumn(tableMapping.getTableName(), attributeType);

sqlInsertQuery.columns(Expressions.stringPath(attribute.getName()));
if (multiValued || Boolean.TRUE.equals(attribute.getMultiValued())) {
Expand Down Expand Up @@ -222,7 +222,7 @@ private boolean updateEntryImpl(TableMapping tableMapping, String key, List<Attr
Path path = Expressions.stringPath(attribute.getName());

String attributeType = columTypes.get(attribute.getName().toLowerCase());
boolean multiValued = (attributeType != null) && "json".equals(attributeType);
boolean multiValued = (attributeType != null) && isJsonColumn(tableMapping.getTableName(), attributeType);

AttributeModificationType type = attributeMod.getModificationType();
if ((AttributeModificationType.ADD == type) || (AttributeModificationType.FORCE_UPDATE == type)) {
Expand Down Expand Up @@ -362,7 +362,7 @@ private List<AttributeData> lookupImpl(TableMapping tableMapping, String key, St
.where(whereExp).limit(1);

try (ResultSet resultSet = sqlSelectQuery.getResults();) {
List<AttributeData> result = getAttributeDataList(resultSet, true);
List<AttributeData> result = getAttributeDataList(tableMapping, resultSet, true);
if (result != null) {
return result;
}
Expand Down Expand Up @@ -441,7 +441,7 @@ private <O> PagedResult<EntryData> searchImpl(TableMapping tableMapping, String
LOG.debug("Executing query: '" + queryStr + "'");

try (ResultSet resultSet = query.getResults()) {
lastResult = getEntryDataList(resultSet);
lastResult = getEntryDataList(tableMapping, resultSet);
}

lastCountRows = lastResult.size();
Expand Down Expand Up @@ -484,7 +484,7 @@ private <O> PagedResult<EntryData> searchImpl(TableMapping tableMapping, String
LOG.debug("Execution query: '" + queryStr + "'");

try (ResultSet resultSet = query.getResults()) {
lastResult = getEntryDataList(resultSet);
lastResult = getEntryDataList(tableMapping, resultSet);
searchResultList.addAll(lastResult);
}
} catch (QueryException ex) {
Expand Down Expand Up @@ -551,7 +551,7 @@ public String[] createStoragePassword(String[] passwords) {
return results;
}

private List<AttributeData> getAttributeDataList(ResultSet resultSet, boolean skipDn) throws EntryConvertationException {
private List<AttributeData> getAttributeDataList(TableMapping tableMapping, ResultSet resultSet, boolean skipDn) throws EntryConvertationException {
try {
if ((resultSet == null)) {
return null;
Expand Down Expand Up @@ -593,7 +593,7 @@ private List<AttributeData> getAttributeDataList(ResultSet resultSet, boolean sk
continue;
}
} else {
if ("json".equals(columnTypeName)) {
if (isJsonColumn(tableMapping.getTableName(), columnTypeName)) {
attributeValueObjects = convertDbJsonToValue(attributeObject.toString());
multiValued = Boolean.TRUE;
} else if (attributeObject instanceof Integer) {
Expand Down Expand Up @@ -641,12 +641,12 @@ private List<AttributeData> getAttributeDataList(ResultSet resultSet, boolean sk
}
}

private List<EntryData> getEntryDataList(ResultSet resultSet) throws EntryConvertationException, SQLException {
private List<EntryData> getEntryDataList(TableMapping tableMapping, ResultSet resultSet) throws EntryConvertationException, SQLException {
List<EntryData> entryDataList = new LinkedList<>();

List<AttributeData> attributeDataList = null;
while (!resultSet.isLast()) {
attributeDataList = getAttributeDataList(resultSet, false);
attributeDataList = getAttributeDataList(tableMapping, resultSet, false);
if (attributeDataList == null) {
break;
}
Expand Down Expand Up @@ -869,4 +869,18 @@ private Object[] convertDbJsonToValue(String jsonValue) {
}
}

private boolean isJsonColumn(String tableName, String columnTypeName) {
if (columnTypeName == null) {
return false;
}

// String engineType = connectionProvider.getEngineType(tableName);
// if ((engineType != null) && engineType.equalsIgnoreCase("mariadb")) {
// return "longtext".equals(columnTypeName);
// }

return "json".equals(columnTypeName);

}

}