rzo1 commented on code in PR #144: URL: https://github.com/apache/openjpa/pull/144#discussion_r3557725124
########## openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestJDBCSchemaManager.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.openjpa.jdbc.meta; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.UUID; + +import org.apache.openjpa.jdbc.conf.JDBCConfiguration; +import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.jdbc.sql.OracleDictionary; +import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; +import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; +import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; +import org.junit.Test; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityManagerFactory; +import jakarta.persistence.SchemaValidationException; + +/** + * Test that a {@link MappingTool#ACTION_REFRESH} uses the right + * types for new columns and takes any mapping in DBDictionary into account. + */ +public class TestJDBCSchemaManager extends AbstractPersistenceTestCase { + /** + * First we create a schema mapping with boolean representation as CHAR(1). + * Then we create an entry. + * After that we create a diff from the entity to the current DB. + * This should result in an empty diff. + */ + @Test + public void testSchemaReset() throws Exception { + EntityManagerFactory emf = createEMF(UUIDEntity.class, DROP_TABLES); + EntityManager em = emf.createEntityManager(); + + assertNotNull(em); + + em.getTransaction().begin(); + UUIDEntity ue1 = new UUIDEntity(); + ue1.setValue("Something"); + em.persist(ue1); + + em.getTransaction().commit(); + UUID id = ue1.getId(); + closeEM(em); + + EntityManager em2 = emf.createEntityManager(); + assertNotNull(em2); + + UUIDEntity ue2 = em2.find(UUIDEntity.class, id); + assertNotNull(ue2); + assertNotEquals(ue1, ue2); + + closeEM(em2); + + closeEMF(emf); + } + + @Test + public void testBuildSchema() throws IOException, SQLException { + EntityManagerFactory emf = createEMF(UUIDEntity.class, EntityBoolChar.class, DROP_TABLES); + emf.createEntityManager(); + + emf.getSchemaManager().create(false); + emf.getSchemaManager().drop(false); + } + + @Test + public void testTruncate() { + EntityManagerFactory emf = createEMF(UUIDEntity.class, EntityBoolChar.class, DROP_TABLES); + EntityManager em = emf.createEntityManager(); + + em.getTransaction().begin(); + UUIDEntity ue1 = new UUIDEntity(); + ue1.setValue("Something"); + em.persist(ue1); + + em.getTransaction().commit(); + UUIDEntity ue2 = em.find(UUIDEntity.class, ue1.getId()); + assertNotNull(ue2); + + closeEM(em); + + emf.getSchemaManager().truncate(); + + em = emf.createEntityManager(); + assertNull(em.find(UUIDEntity.class, ue1.getId())); + closeEM(em); + } + + @Test + public void testValidate() { + OpenJPAEntityManagerFactorySPI emf = createEMF(UUIDEntity.class, EntityBoolChar.class, DROP_TABLES);; + EntityManager em = emf.createEntityManager(); + + em.getTransaction().begin(); + UUIDEntity ue1 = new UUIDEntity(); + ue1.setValue("Something"); + em.persist(ue1); + em.getTransaction().commit(); + + Connection conn = (Connection) ((OpenJPAEntityManagerSPI) em.getDelegate()).getConnection(); + + JDBCConfiguration conf = ((JDBCConfiguration) emf.getConfiguration()); + DBDictionary dict = conf.getDBDictionaryInstance(); + + try (Statement stmt = conn.createStatement()) { + stmt.executeUpdate("ALTER TABLE UUIDEntity DROP COLUMN value_"); + stmt.executeUpdate("ALTER TABLE UUIDEntity ADD " + (dict instanceof OracleDictionary ? "" : "COLUMN") + " value_ BOOLEAN DEFAULT FALSE"); Review Comment: Investigated this one - it's reproducible without OpenJPA and it's not a driver/binding issue (`sendTimeAsDatetime=false` is already set on the connection URL and doesn't help). The `{t '01:32:21'}` in those queries is the JDBC/ODBC escape literal coming straight from the JPQL. OpenJPA inlines it verbatim into the SQL (`Lit.appendTo`), and SQL Server types the ODBC `{t}` escape as legacy `datetime`, not `time`: ```sql SELECT SQL_VARIANT_PROPERTY({t '01:32:21'}, 'BaseType') -- returns: datetime ``` Since SQL Server has **no implicit conversion between `time` and `datetime`**, comparing it against our `EXTRACT(TIME ...)` translation (`CAST(... AS TIME)`) raises error 402: ```sql SELECT 1 WHERE CAST({ts '2005-03-21 01:32:21'} AS TIME) = {t '01:32:21'} -- Msg 402: The data types time and datetime are incompatible in the equal to operator. ``` Only `{t}` is affected — `{d}` and `{ts}` also map to `datetime`, but `date`/`datetime` comparisons have an implicit conversion, so they work. **Fix (verified against SQL Server 2022):** rewrite the time escape to a typed literal on MSSQL: `{t '01:32:21'}` → `CAST('01:32:21' AS TIME)`. Both failing queries then behave exactly as the tests expect: ```sql WHERE CAST({ts '2005-03-21 01:32:21'} AS TIME) = CAST('01:32:21' AS TIME) -- matches WHERE CAST(CONVERT(TIME, GETDATE()) AS TIME) = CAST('01:32:20' AS TIME) -- 0 rows ``` The only place these escapes enter the SQL is `Lit.appendTo` in `openjpa-jdbc`, so the fix might be something like that in the dictionary: ```java // Lit.java — delegate instead of raw passthrough } else if (parseType == Literal.TYPE_DATE || parseType == Literal.TYPE_TIME || parseType == Literal.TYPE_TIMESTAMP) { lstate.sqlValue = new Raw(ctx.store.getDBDictionar .toJDBCEscapedDateTimeLiteral(_val.toString(), parseType)); } ``` ```java // DBDictionary — default passthrough public String toJDBCEscapedDateTimeLiteral(String escape, int parseType) { return escape; } ``` ```java // SQLServerDictionary — unwrap only the time escape @Override public String toJDBCEscapedDateTimeLiteral(String escape, int parseType) { // SQL Server types the ODBC {t '...'} escape as Dss), // which has no implicit conversion to TIME -> err if (parseType == Literal.TYPE_TIME) { int start = escape.indexOf('\''); int end = escape.lastIndexOf('\''); if (start >= 0 && end > start) { return "CAST(" + escape.substring(start, end + 1) + " AS TIME)"; } } return escape; } ``` -- 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]
