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

fix: support boolean jdbc data type #6953

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.jans.as.server.dev;

import java.util.List;
import java.util.Properties;

import io.jans.as.server.model.ldap.TokenEntity;
import io.jans.orm.search.filter.Filter;
import io.jans.orm.sql.impl.SqlEntryManager;
import io.jans.orm.sql.impl.SqlEntryManagerFactory;

public final class TokenTest {


public static SqlEntryManager createSqlEntryManager() {
SqlEntryManagerFactory sqlEntryManagerFactory = new SqlEntryManagerFactory();
sqlEntryManagerFactory.create();
Properties connectionProperties = new Properties();

connectionProperties.put("sql#db.schema.name", "public");
connectionProperties.put("sql#connection.uri", "jdbc:postgresql://localhost:16432/jansdb");

connectionProperties.put("sql#auth.userName", "jans");
connectionProperties.put("sql#auth.userPassword", "ObIwZ94SPhO8");

SqlEntryManager sqlEntryManager = sqlEntryManagerFactory.createEntryManager(connectionProperties);
System.out.println("Created SqlEntryManager: " + sqlEntryManager);

return sqlEntryManager;
}

public static void main1(SqlEntryManager sqlEntryManager, String[] args) throws Exception {
String baseDn = "ou=tokens,o=jans";
Filter typeFilter = Filter.createEqualityFilter("tknTyp", "access_token");
Filter userFilter = Filter.createEqualityFilter("usrId", "admin");
Filter delFilter = Filter.createEqualityFilter("del", true);

Filter filter = Filter.createANDFilter(typeFilter, userFilter, delFilter);
List<TokenEntity> tokens = sqlEntryManager.findEntries(baseDn, TokenEntity.class, filter);

System.out.println("tokens: " + tokens.size());
}

public static void main2(SqlEntryManager sqlEntryManager, String[] args) throws Exception {
String baseDn = "ou=tokens,o=jans";

TokenEntity tokenEntity = new TokenEntity();
tokenEntity.setDn(baseDn);
tokenEntity.setUserId("admin");
tokenEntity.setTokenType("access_token");

List<TokenEntity> tokens = sqlEntryManager.findEntries(tokenEntity);

System.out.println("tokens: " + tokens.size());
}

public static void main(String[] args) throws Exception {
SqlEntryManager sqlEntryManager = createSqlEntryManager();

main1(sqlEntryManager, args);
main2(sqlEntryManager, args);

sqlEntryManager.destroy();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

package io.jans.orm.sql.impl;

import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -16,6 +15,7 @@
import java.util.Map;
import java.util.function.Function;

import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -42,7 +42,6 @@
import io.jans.orm.sql.operation.SupportedDbType;
import io.jans.orm.util.ArrayHelper;
import io.jans.orm.util.StringHelper;
import org.apache.commons.text.StringEscapeUtils;

/**
* Filter to SQL query convert
Expand Down Expand Up @@ -497,15 +496,25 @@ private Object prepareTypedExpressionValue(TableMapping tableMapping, Filter fil
try {
if (attributeType != null) {
String columnType = attributeType.getType();
java.sql.JDBCType jdbcType = java.sql.JDBCType.valueOf(StringHelper.toUpperCase(columnType));

java.sql.JDBCType jdbcType;
// Fix for PostgreSQL
if (StringHelper.equalsIgnoreCase(columnType, "bool")) {
jdbcType = java.sql.JDBCType.BOOLEAN;
} else {
jdbcType = java.sql.JDBCType.valueOf(StringHelper.toUpperCase(columnType));
}

if ((jdbcType == java.sql.JDBCType.SMALLINT) || (jdbcType == java.sql.JDBCType.BOOLEAN)) {
if (StringHelper.equalsIgnoreCase((String) assertionValue, "true") || StringHelper.equalsIgnoreCase((String) assertionValue, "1")) {
assertionValue = 1;
}
if (jdbcType == java.sql.JDBCType.SMALLINT) {
boolean res = StringHelper.equalsIgnoreCase((String) assertionValue, "true") || StringHelper.equalsIgnoreCase((String) assertionValue, "1");
assertionValue = res ? 1 : 0;
} else if (jdbcType == java.sql.JDBCType.BOOLEAN) {
boolean res = StringHelper.equalsIgnoreCase((String) assertionValue, "true") || StringHelper.equalsIgnoreCase((String) assertionValue, "1");
assertionValue = res;
}
}
} catch (java.lang.IllegalArgumentException ex) {
LOG.error("Failed to determine JDBC type '{}' ", attributeType.getType(), ex);
// Do nothing. Type is not defined in enum
}
}
Expand Down