Copilot commented on code in PR #11066:
URL: https://github.com/apache/gravitino/pull/11066#discussion_r3246917431


##########
plugins/idp-basic/src/test/java/org/apache/gravitino/idp/basic/storage/relational/mapper/AbstractIdpUserMetaStorageTest.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.idp.basic.storage.relational.mapper;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.UUID;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.config.ConfigConstants;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.integration.test.container.ContainerSuite;
+import org.apache.gravitino.integration.test.container.MySQLContainer;
+import org.apache.gravitino.integration.test.container.PostgreSQLContainer;
+import org.apache.gravitino.integration.test.util.TestDatabaseName;
+import org.apache.gravitino.storage.relational.JDBCBackend;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.AfterEach;
+
+abstract class AbstractIdpUserMetaStorageTest {
+  private static final String H2_BACKEND = "h2";
+  private static final String MYSQL_BACKEND = "mysql";
+  private static final String POSTGRESQL_BACKEND = "postgresql";
+  private static final TestDatabaseName MYSQL_TEST_DATABASE = 
TestDatabaseName.MYSQL_JDBC_BACKEND;
+  private static final TestDatabaseName POSTGRESQL_TEST_DATABASE = 
TestDatabaseName.PG_JDBC_BACKEND;
+
+  protected JDBCBackend backend;
+  protected SqlSession sharedSession;
+  protected IdpUserMetaMapper idpUserMetaMapper;
+
+  private Config config;
+  private Path h2Path;
+
+  static Stream<String> storageProvider() {
+    return Stream.of(H2_BACKEND, MYSQL_BACKEND, POSTGRESQL_BACKEND);
+  }
+
+  @AfterEach
+  void closeSuite() throws IOException {
+    closeSession();
+    if (backend != null) {
+      backend.close();
+      backend = null;
+    }
+
+    SqlSessionFactoryHelper.getInstance().close();
+    ContainerSuite.getInstance().close();

Review Comment:
   Calling `ContainerSuite.getInstance().close()` in `@AfterEach` tears down 
the shared MySQL/PostgreSQL containers after every single parameterized test 
invocation. Since the test class iterates over three backends and has many test 
methods, this forces a full container restart on every iteration that uses 
MySQL/PostgreSQL, dramatically increasing test runtime and undermining the 
singleton design of `ContainerSuite`. Consider moving container teardown to 
`@AfterAll` (or omitting it entirely so the suite is reused across the JVM), 
and only doing per-test cleanup of the per-test schema/database and 
`SqlSessionFactoryHelper`.



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/basic/storage/relational/mapper/provider/postgresql/IdpUserMetaPostgreSQLProvider.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.postgresql;
+
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.IdpUserMetaMapper;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.base.IdpUserMetaBaseSQLProvider;
+import org.apache.ibatis.annotations.Param;
+
+public class IdpUserMetaPostgreSQLProvider extends IdpUserMetaBaseSQLProvider {
+
+  @Override
+  protected String currentTimeMillisExpression() {
+    return "CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000 AS BIGINT)";
+  }
+
+  @Override
+  public String deleteIdpUserMetasByLegacyTimeline(
+      @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) 
{
+    return "DELETE FROM "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " WHERE user_id IN (SELECT user_id FROM "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT 
#{limit})";

Review Comment:
   The inner `SELECT ... LIMIT` has no `ORDER BY`, so PostgreSQL may pick an 
arbitrary set of rows up to `limit`. More importantly, the inner subquery 
filters by `deleted_at > 0 AND deleted_at < #{legacyTimeline}` but the outer 
`DELETE` only matches by `user_id`, meaning if any row for that `user_id` 
exists with `deleted_at = 0` or `deleted_at >= legacyTimeline` (e.g. a 
re-created active user) it would also be deleted. Consider repeating the 
`deleted_at` predicate in the outer `WHERE` (e.g. `DELETE ... WHERE deleted_at 
> 0 AND deleted_at < #{legacyTimeline} AND ctid IN (SELECT ctid FROM ... LIMIT 
#{limit})`) to ensure only legacy rows are removed.
   



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/basic/storage/relational/mapper/IdpUserMetaSQLProviderFactory.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.idp.basic.storage.relational.mapper;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.List;
+import java.util.Map;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.base.IdpUserMetaBaseSQLProvider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.h2.IdpUserMetaH2Provider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.mysql.IdpUserMetaMySQLProvider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.postgresql.IdpUserMetaPostgreSQLProvider;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.annotations.Param;
+
+public class IdpUserMetaSQLProviderFactory {
+  private static final IdpUserMetaBaseSQLProvider IDP_USER_META_H2_PROVIDER =
+      new IdpUserMetaH2Provider();
+  private static final IdpUserMetaBaseSQLProvider IDP_USER_META_MYSQL_PROVIDER 
=
+      new IdpUserMetaMySQLProvider();
+  private static final IdpUserMetaBaseSQLProvider 
IDP_USER_META_POSTGRESQL_PROVIDER =
+      new IdpUserMetaPostgreSQLProvider();
+
+  private static final Map<JDBCBackendType, IdpUserMetaBaseSQLProvider>
+      IDP_USER_META_SQL_PROVIDER_MAP =
+          ImmutableMap.of(
+              JDBCBackendType.MYSQL, IDP_USER_META_MYSQL_PROVIDER,
+              JDBCBackendType.H2, IDP_USER_META_H2_PROVIDER,
+              JDBCBackendType.POSTGRESQL, IDP_USER_META_POSTGRESQL_PROVIDER);
+
+  static IdpUserMetaBaseSQLProvider getProvider(
+      String databaseId, Map<JDBCBackendType, IdpUserMetaBaseSQLProvider> 
providerMap) {

Review Comment:
   `getProvider` takes a `providerMap` parameter but in every call site only 
`IDP_USER_META_SQL_PROVIDER_MAP` is passed. Parameterizing it adds no 
flexibility (the method is package-private and not used elsewhere) and 
complicates the API. Consider removing the parameter and referencing the 
constant directly inside the method.



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/basic/storage/relational/mapper/IdpUserMetaSQLProviderFactory.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.idp.basic.storage.relational.mapper;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.List;
+import java.util.Map;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.base.IdpUserMetaBaseSQLProvider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.h2.IdpUserMetaH2Provider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.mysql.IdpUserMetaMySQLProvider;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.provider.postgresql.IdpUserMetaPostgreSQLProvider;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.annotations.Param;
+
+public class IdpUserMetaSQLProviderFactory {
+  private static final IdpUserMetaBaseSQLProvider IDP_USER_META_H2_PROVIDER =
+      new IdpUserMetaH2Provider();
+  private static final IdpUserMetaBaseSQLProvider IDP_USER_META_MYSQL_PROVIDER 
=
+      new IdpUserMetaMySQLProvider();
+  private static final IdpUserMetaBaseSQLProvider 
IDP_USER_META_POSTGRESQL_PROVIDER =
+      new IdpUserMetaPostgreSQLProvider();
+
+  private static final Map<JDBCBackendType, IdpUserMetaBaseSQLProvider>
+      IDP_USER_META_SQL_PROVIDER_MAP =
+          ImmutableMap.of(
+              JDBCBackendType.MYSQL, IDP_USER_META_MYSQL_PROVIDER,
+              JDBCBackendType.H2, IDP_USER_META_H2_PROVIDER,
+              JDBCBackendType.POSTGRESQL, IDP_USER_META_POSTGRESQL_PROVIDER);
+
+  static IdpUserMetaBaseSQLProvider getProvider(
+      String databaseId, Map<JDBCBackendType, IdpUserMetaBaseSQLProvider> 
providerMap) {
+    if (databaseId == null) {
+      throw new IllegalStateException(
+          "MyBatis databaseId is not configured for IdP user SQL providers.");
+    }
+
+    try {
+      JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId);
+      IdpUserMetaBaseSQLProvider provider = providerMap.get(jdbcBackendType);
+      if (provider != null) {
+        return provider;
+      }
+
+      throw new IllegalStateException(
+          String.format(
+              "No IdP user SQL provider registered for backend %s (databaseId: 
%s)",
+              jdbcBackendType, databaseId));
+    } catch (IllegalArgumentException e) {
+      throw new IllegalStateException(
+          String.format(
+              "Unsupported IdP user SQL provider databaseId: %s, supported 
backends: %s",
+              databaseId, providerMap.keySet()),
+          e);
+    }
+  }
+
+  public static IdpUserMetaBaseSQLProvider h2Provider() {
+    return IDP_USER_META_H2_PROVIDER;
+  }
+
+  public static IdpUserMetaBaseSQLProvider mysqlProvider() {
+    return IDP_USER_META_MYSQL_PROVIDER;
+  }
+
+  public static IdpUserMetaBaseSQLProvider postgresqlProvider() {
+    return IDP_USER_META_POSTGRESQL_PROVIDER;
+  }
+
+  public static String selectIdpUser(@Param("username") String username) {
+    return getProvider(currentDatabaseId(), 
IDP_USER_META_SQL_PROVIDER_MAP).selectIdpUser(username);
+  }
+
+  public static String selectIdpUsers(@Param("usernames") List<String> 
usernames) {
+    return getProvider(currentDatabaseId(), IDP_USER_META_SQL_PROVIDER_MAP)
+        .selectIdpUsers(usernames);
+  }
+
+  public static String insertIdpUser(@Param("userMeta") IdpUserPO userPO) {

Review Comment:
   The `@Param` annotations on these static SQL-provider factory methods have 
no effect — MyBatis only honors `@Param` on the mapper interface methods (which 
are already annotated in `IdpUserMetaMapper`). Keeping them here is misleading 
and adds an unnecessary `org.apache.ibatis.annotations.Param` import. Consider 
removing the `@Param` annotations from all factory methods.



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/basic/storage/relational/mapper/provider/base/IdpUserMetaBaseSQLProvider.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.idp.basic.storage.relational.mapper.provider.base;
+
+import java.util.List;
+import 
org.apache.gravitino.idp.basic.storage.relational.mapper.IdpUserMetaMapper;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.ibatis.annotations.Param;
+
+public abstract class IdpUserMetaBaseSQLProvider {
+  public String selectIdpUser(@Param("username") String username) {
+    return "SELECT user_id as userId, user_name as userName, password_hash as 
passwordHash,"
+        + " current_version as currentVersion,"
+        + " last_version as lastVersion, deleted_at as deletedAt"
+        + " FROM "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " WHERE user_name = #{username} AND deleted_at = 0";
+  }
+
+  public String selectIdpUsers(@Param("usernames") List<String> usernames) {
+    if (usernames == null || usernames.isEmpty()) {
+      return "SELECT user_id as userId, user_name as userName, password_hash 
as passwordHash,"
+          + " current_version as currentVersion,"
+          + " last_version as lastVersion, deleted_at as deletedAt"
+          + " FROM "
+          + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+          + " WHERE deleted_at = 0 AND 1 = 0";

Review Comment:
   When `usernames` is null/empty the generated SQL still executes a query 
(`WHERE deleted_at = 0 AND 1 = 0`) against the database round-trip just to 
return zero rows. Since the mapper caller can easily short-circuit this in Java 
(return `Collections.emptyList()` when input is null/empty), consider handling 
the empty case in the caller (or in `IdpUserMetaMapper`'s default method) to 
avoid an unnecessary DB round-trip. If this approach is intentional for 
simplicity, that's acceptable, but worth a comment explaining the rationale.
   



##########
plugins/idp-basic/src/test/java/org/apache/gravitino/idp/basic/storage/relational/mapper/AbstractIdpUserMetaStorageTest.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.idp.basic.storage.relational.mapper;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.UUID;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.config.ConfigConstants;
+import org.apache.gravitino.idp.basic.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.integration.test.container.ContainerSuite;
+import org.apache.gravitino.integration.test.container.MySQLContainer;
+import org.apache.gravitino.integration.test.container.PostgreSQLContainer;
+import org.apache.gravitino.integration.test.util.TestDatabaseName;
+import org.apache.gravitino.storage.relational.JDBCBackend;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.session.SqlSession;
+import org.junit.jupiter.api.AfterEach;
+
+abstract class AbstractIdpUserMetaStorageTest {
+  private static final String H2_BACKEND = "h2";
+  private static final String MYSQL_BACKEND = "mysql";
+  private static final String POSTGRESQL_BACKEND = "postgresql";
+  private static final TestDatabaseName MYSQL_TEST_DATABASE = 
TestDatabaseName.MYSQL_JDBC_BACKEND;
+  private static final TestDatabaseName POSTGRESQL_TEST_DATABASE = 
TestDatabaseName.PG_JDBC_BACKEND;
+
+  protected JDBCBackend backend;
+  protected SqlSession sharedSession;
+  protected IdpUserMetaMapper idpUserMetaMapper;
+
+  private Config config;
+  private Path h2Path;
+
+  static Stream<String> storageProvider() {
+    return Stream.of(H2_BACKEND, MYSQL_BACKEND, POSTGRESQL_BACKEND);
+  }
+
+  @AfterEach
+  void closeSuite() throws IOException {
+    closeSession();
+    if (backend != null) {
+      backend.close();
+      backend = null;
+    }
+
+    SqlSessionFactoryHelper.getInstance().close();
+    ContainerSuite.getInstance().close();
+
+    if (h2Path != null && Files.exists(h2Path)) {
+      deleteDirectory(h2Path);
+      h2Path = null;
+    }
+  }
+
+  protected void init(String type) throws IOException {
+    config = createBackendConfig(type);
+    backend = new JDBCBackend();
+    backend.initialize(config);
+    sharedSession = 
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+    idpUserMetaMapper = sharedSession.getMapper(IdpUserMetaMapper.class);
+  }
+
+  protected void restartBackend() throws IOException {
+    closeSession();
+    backend.close();
+    backend = new JDBCBackend();
+    backend.initialize(config);
+    sharedSession = 
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+    idpUserMetaMapper = sharedSession.getMapper(IdpUserMetaMapper.class);
+  }
+
+  protected IdpUserPO insertUser(
+      long userId,
+      String userName,
+      String passwordHash,
+      long currentVersion,
+      long lastVersion,
+      long deletedAt) {
+    IdpUserPO userPO =
+        IdpUserPO.builder()
+            .withUserId(userId)
+            .withUserName(userName)
+            .withPasswordHash(passwordHash)
+            .withCurrentVersion(currentVersion)
+            .withLastVersion(lastVersion)
+            .withDeletedAt(deletedAt)
+            .build();
+    idpUserMetaMapper.insertIdpUser(userPO);
+    return userPO;
+  }
+
+  protected long queryLongValue(String table, String column, String idColumn, 
long idValue) {
+    try (SqlSession sqlSession =
+            
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+        Connection connection = sqlSession.getConnection();
+        PreparedStatement statement =
+            connection.prepareStatement(
+                "SELECT " + column + " FROM " + table + " WHERE " + idColumn + 
" = ?")) {
+      statement.setLong(1, idValue);
+      try (ResultSet resultSet = statement.executeQuery()) {
+        assertTrue(resultSet.next());
+        return resultSet.getLong(1);
+      }
+    } catch (SQLException e) {
+      throw new RuntimeException("Query " + column + " from " + table + " 
failed", e);
+    }
+  }
+
+  protected int countRows(String table, String idColumn, long idValue) {
+    try (SqlSession sqlSession =
+            
SqlSessionFactoryHelper.getInstance().getSqlSessionFactory().openSession(true);
+        Connection connection = sqlSession.getConnection();
+        PreparedStatement statement =
+            connection.prepareStatement(
+                "SELECT COUNT(1) FROM " + table + " WHERE " + idColumn + " = 
?")) {
+      statement.setLong(1, idValue);
+      try (ResultSet resultSet = statement.executeQuery()) {
+        assertTrue(resultSet.next());
+        return resultSet.getInt(1);
+      }
+    } catch (SQLException e) {
+      throw new RuntimeException("Count rows from " + table + " failed", e);
+    }
+  }
+
+  protected void closeSession() {
+    if (sharedSession != null) {
+      sharedSession.close();
+      sharedSession = null;
+    }
+  }
+
+  private Config createBackendConfig(String type) throws IOException {
+    Config backendConfig = new Config(false) {};
+    backendConfig.set(Configs.ENTITY_STORE, Configs.RELATIONAL_ENTITY_STORE);
+    backendConfig.set(Configs.ENTITY_RELATIONAL_STORE, type);
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_MAX_CONNECTIONS, 
20);
+    
backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_WAIT_MILLISECONDS, 
1000L);
+
+    switch (type) {
+      case MYSQL_BACKEND:
+        initializeMySQLBackend(backendConfig);
+        break;
+      case POSTGRESQL_BACKEND:
+        initializePostgreSQLBackend(backendConfig);
+        break;
+      case H2_BACKEND:
+        initializeH2Backend(backendConfig);
+        break;
+      default:
+        throw new IllegalArgumentException("Unsupported backend type: " + 
type);
+    }
+
+    return backendConfig;
+  }
+
+  private void initializeMySQLBackend(Config backendConfig) throws IOException 
{
+    ContainerSuite containerSuite = ContainerSuite.getInstance();
+    containerSuite.startMySQLContainer(MYSQL_TEST_DATABASE);
+    MySQLContainer mySQLContainer = containerSuite.getMySQLContainer();
+    String jdbcUrl = mySQLContainer.getJdbcUrl(MYSQL_TEST_DATABASE);
+
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL, jdbcUrl);
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_USER, 
mySQLContainer.getUsername());
+    backendConfig.set(
+        Configs.ENTITY_RELATIONAL_JDBC_BACKEND_PASSWORD, 
mySQLContainer.getPassword());
+    backendConfig.set(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_DRIVER, 
"com.mysql.cj.jdbc.Driver");
+
+    try (Connection connection =
+            DriverManager.getConnection(
+                StringUtils.substringBeforeLast(jdbcUrl, "/"),
+                mySQLContainer.getUsername(),
+                mySQLContainer.getPassword());
+        Statement statement = connection.createStatement()) {
+      statement.execute("DROP DATABASE IF EXISTS " + MYSQL_TEST_DATABASE);
+      statement.execute("CREATE DATABASE " + MYSQL_TEST_DATABASE);
+      statement.execute("USE " + MYSQL_TEST_DATABASE);
+      executeSqlStatements(statement, loadSchemaStatements(MYSQL_BACKEND));

Review Comment:
   The database/schema names are interpolated directly into DDL statements 
(also in the PostgreSQL path with the random `schemaName`). Although the values 
currently come from trusted sources, this pattern is risky if a future change 
passes user-controlled input. Consider using a strict identifier validator 
(e.g. enforce `[A-Za-z0-9_]+`) before concatenation to make the intent explicit 
and prevent accidental SQL injection.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to