Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public class DBDictionary
public int maxEmbeddedBlobSize = -1;
public int maxEmbeddedClobSize = -1;
public int inClauseLimit = -1;

/**
* Attention, while this is named datePrecision it actually only get used for Timestamp handling!
* @see StateManagerImpl#roundTimestamp(Timestamp, int)
Expand Down Expand Up @@ -502,10 +502,10 @@ public enum DateMillisecondBehaviors { DROP, ROUND, RETAIN }
private String conversionKey = null;

public boolean supportsUuidType = false;

public boolean supportsUnsizedCharOnCast = true;
public String integerCastTypeName = integerTypeName;

public String integerCastTypeName = integerTypeName;

// Naming utility and naming rules
private DBIdentifierUtil namingUtil = null;
Expand Down Expand Up @@ -638,6 +638,10 @@ protected void configureNamingRules() {

DBIdentifierRule columnNamingRule = new ColumnIdentifierRule(invalidColumnWordSet);
namingRules.put(columnNamingRule.getName(), columnNamingRule);
// restoring default "empty" cfg
IdentifierRule rule = Normalizer.getNamingConfiguration().getDefaultIdentifierRule();
rule.setDelimitReservedWords(false);
rule.setReservedWords(Set.of());
}

//////////////////////
Expand Down Expand Up @@ -2063,7 +2067,7 @@ public String getTypeName(Column col) {
return appendSize(col, autoAssignTypeName);

if (col.getJavaType() == JavaTypes.UUID_OBJ) {
if (supportsUuidType)
if (supportsUuidType)
return appendSize(col, uuidTypeName);
else {
return appendSize(col, getTypeName(Types.VARCHAR));
Expand Down Expand Up @@ -3270,7 +3274,7 @@ public void substring(SQLBuffer buf, FilterValue str, FilterValue start,
}
buf.append(")");
}

public void replace(SQLBuffer buf, FilterValue from, FilterValue subs, FilterValue replacement) {
buf.append(replaceFunctionName).append("(");
from.appendTo(buf);
Expand All @@ -3280,7 +3284,7 @@ public void replace(SQLBuffer buf, FilterValue from, FilterValue subs, FilterVal
replacement.appendTo(buf);
buf.append(")");
}

public void left(SQLBuffer buf, FilterValue str, FilterValue length) {
buf.append(leftFunctionName).append("(");
str.appendTo(buf);
Expand Down Expand Up @@ -5298,7 +5302,9 @@ public void endConfiguration() {
selectWordSet.addAll(Arrays.asList(StringUtil.split(selectWords.toUpperCase(Locale.ENGLISH), ",", 0)));

if (invalidColumnWordSet.isEmpty()) {
invalidColumnWordSet.addAll(loadFromResource("sql-invalid-column-names.rsrc"));
Collection<String> invalidColumns = loadFromResource("sql-invalid-column-names.rsrc");
invalidColumnWordSet.addAll(invalidColumns);
namingRules.get(DBIdentifierType.COLUMN.name()).setReservedWords(invalidColumns);
}

// initialize the error codes
Expand Down Expand Up @@ -5578,11 +5584,10 @@ public void closeDataSource(DataSource dataSource) {
}

/**
* Used by some mappings to represent data that has already been
* serialized so that we don't have to serialize multiple times.
*/
public record SerializedData(byte[] bytes) {

* Used by some mappings to represent data that has already been
* serialized so that we don't have to serialize multiple times.
*/
public record SerializedData(byte[] bytes) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Set;

import org.apache.openjpa.jdbc.identifier.DBIdentifier;
import org.apache.openjpa.jdbc.identifier.Normalizer;
import org.apache.openjpa.jdbc.identifier.DBIdentifier.DBIdentifierType;
import org.apache.openjpa.jdbc.kernel.JDBCFetchConfiguration;
import org.apache.openjpa.jdbc.kernel.JDBCStore;
Expand All @@ -41,6 +42,7 @@
import org.apache.openjpa.jdbc.schema.Index;
import org.apache.openjpa.jdbc.schema.PrimaryKey;
import org.apache.openjpa.jdbc.schema.Table;
import org.apache.openjpa.lib.identifier.IdentifierRule;
import org.apache.openjpa.lib.util.StringUtil;
import org.apache.openjpa.util.ExceptionInfo;
import org.apache.openjpa.util.StoreException;
Expand Down Expand Up @@ -129,7 +131,7 @@ public MySQLDictionary() {
"AUTO_INCREMENT", "BINARY", "BLOB", "CHANGE", "ENUM", "INFILE",
"INT1", "INT2", "INT4", "FLOAT1", "FLOAT2", "FLOAT4", "LOAD",
"MEDIUMINT", "OUTFILE", "REPLACE", "STARTING", "TEXT", "UNSIGNED",
"ZEROFILL", "INDEX",
"ZEROFILL", "INDEX", "LIBRARY"
}));

// reservedWordSet subset that CANNOT be used as valid column names
Expand Down Expand Up @@ -271,6 +273,14 @@ private static int[] getMajorMinorVersions(String versionStr)
return new int[]{maj, min};
}

@Override
protected void configureNamingRules() {
super.configureNamingRules();
IdentifierRule rule = Normalizer.getNamingConfiguration().getDefaultIdentifierRule();
rule.setDelimitReservedWords(true);
rule.setReservedWords(reservedWordSet);
}

@Override
public String[] getCreateTableSQL(Table table) {
String[] sql = super.getCreateTableSQL(table);
Expand Down Expand Up @@ -477,8 +487,8 @@ protected int matchErrorState(Map<Integer,Set<String>> errorStates, SQLException
if (state == ExceptionInfo.GENERAL && ex.getErrorCode() == 0 && ex.getSQLState() == null) {
// look at the nested MySQL exception for more details
SQLException sqle = ex.getNextException();
if (sqle != null
&& (sqle.toString().startsWith("com.mysql.jdbc.exceptions.MySQLTimeoutException") ||
if (sqle != null
&& (sqle.toString().startsWith("com.mysql.jdbc.exceptions.MySQLTimeoutException") ||
sqle.toString().startsWith("com.mysql.cj.jdbc.exceptions.MySQLTimeoutException"))) {
if (conf != null && conf.getLockTimeout() != -1) {
state = StoreException.LOCK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package org.apache.openjpa.jdbc.meta;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
Expand All @@ -39,20 +41,20 @@ public class TestSchemaTool {
@Parameters(name = "{index}: {0} -> {1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{"", 0},
{" ", 0},
{"org/apache/openjpa/jdbc/meta/testScript1", 0},
{"org/apache/openjpa/jdbc/meta/testScript2", 1},
{"org/apache/openjpa/jdbc/meta/testScript3", 1},
{"org/apache/openjpa/jdbc/meta/testScript4", 0},
{"org/apache/openjpa/jdbc/meta/testScriptMulti1", 1},
{"org/apache/openjpa/jdbc/meta/testScriptMulti2", 0},
{"", List.of()},
{" ", List.of()},
{"org/apache/openjpa/jdbc/meta/testScript1", List.of()},
{"org/apache/openjpa/jdbc/meta/testScript2", List.of("SELECT * FROM Customers WHERE Country = 'Germany'")},
{"org/apache/openjpa/jdbc/meta/testScript3", List.of("SELECT * FROM Customers")},
{"org/apache/openjpa/jdbc/meta/testScript4", List.of()},
{"org/apache/openjpa/jdbc/meta/testScriptMulti1", List.of("SELECT * FROM MyTable")},
{"org/apache/openjpa/jdbc/meta/testScriptMulti2", List.of()},
});
}
@Parameter(0)
public String sqlScript;
@Parameter(1)
public Integer resultingLines;
public List<String> expected;

@Test
public void testExecuteScript() throws Exception {
Expand All @@ -75,6 +77,12 @@ protected boolean executeSQL(String[] sql) {
};
tool.setScriptToExecute(sqlScript);
tool.run();
assertEquals(resultingLines.intValue(), sqlToRun.size());
if (expected.size() != sqlToRun.size()) {
fail("Expected list wasn't found: \r\n expected" + expected
+ "\r\n actual: \r\n" + sqlToRun);
}
for (int i = 0; i < expected.size(); ++i) {
assertEquals(expected.get(i), sqlToRun.get(i));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
*/
package org.apache.openjpa.lib.identifier;

import java.util.Collection;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.openjpa.lib.util.StringUtil;

Expand Down Expand Up @@ -122,8 +125,10 @@ public boolean isOnlyLettersDigitsUnderscores() {
return _onlyLettersDigitsUnderscores;
}

public void setReservedWords(Set<String> reservedWords) {
_reservedWords = reservedWords;
public void setReservedWords(Collection<String> reservedWords) {
_reservedWords = reservedWords.stream()
.map(w -> w.toUpperCase(Locale.ENGLISH))
.collect(Collectors.toSet());
}

public Set<String> getReservedWords() {
Expand Down Expand Up @@ -202,6 +207,6 @@ public boolean requiresDelimiters(String identifier) {
}

public boolean isReservedWord(String identifier) {
return _reservedWords.contains(identifier);
return _reservedWords.contains(identifier.toUpperCase(Locale.ENGLISH));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import static org.junit.Assert.assertTrue;

public class TestSnakeCaseDDL {

private static final Logger logger = Logger.getLogger(TestSnakeCaseDDL.class.getCanonicalName());

@Test
Expand Down Expand Up @@ -82,8 +82,9 @@ public void ddlInSnakeCase() throws SQLException {
.getTables(null, null, "%", null)) {
while (tables.next()) {
final String table = tables.getString(3);
if (table.toUpperCase(Locale.ROOT).startsWith("SNAKE")) {
createdTables.put(table.toUpperCase(Locale.ROOT), table);
final String upper = table.toUpperCase(Locale.ENGLISH);
if (upper.startsWith("SNAKE")) {
createdTables.put(upper, table);
}
}
}
Expand Down Expand Up @@ -132,7 +133,7 @@ public void ddlInSnakeCase() throws SQLException {
em.close();
}
}
final String tableName = createdTables.get("SnakeCaseDDLMy1Entity".toUpperCase(Locale.ROOT));
final String tableName = createdTables.get("SnakeCaseDDLMy1Entity".toUpperCase(Locale.ENGLISH));
try (final Connection connection = ds.getConnection();
final Statement statement = connection.createStatement();
final ResultSet rs = statement.executeQuery("select foo_bar, this_field from \"" + tableName + "\"")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* @version $Rev$ $Date$
*/
public abstract class AbstractCriteriaTestCase extends TestCase {

private static final Logger logger = Logger.getLogger(AbstractCriteriaTestCase.class.getCanonicalName());

protected abstract SQLAuditor getAuditor();
Expand Down Expand Up @@ -156,6 +156,13 @@ void assertEquivalence(QueryDecorator decorator, CriteriaQuery<?> c, String jpql
executeAndCompareSQL(jpql, cQ, jQ, expectedSQL);
}

protected boolean same(String expected, String sql) {
return sql.equalsIgnoreCase(expected) ||
sql.replace(dict.getLeadingDelimiter(), "")
.replace(dict.getTrailingDelimiter(), "")
.equalsIgnoreCase(expected);
}

/**
* Execute the two given queries. The first query originated from a JPQL string must be well-formed. The second
* query originated from a Criteria is being tested.
Expand Down Expand Up @@ -194,19 +201,20 @@ void executeAndCompareSQL(String jpql, Query cQ, Query jQ, String expectedSQL) {
return;

for (int i = 0; i < jSQL.size(); i++) {
if (!jSQL.get(i).equalsIgnoreCase(cSQL.get(i))) {
boolean eq = same(cSQL.get(i), jSQL.get(i));
if (!eq) {
printSQL("Target SQL for JPQL", jSQL);
printSQL("Target SQL for CriteriaQuery", cSQL);
assertTrue(i + "-th SQL for JPQL and CriteriaQuery for " + jpql + " is different\r\n" +
"JPQL = [" + jSQL.get(i) + "]\r\n" +
"CSQL = [" + cSQL.get(i) + "]\r\n",
jSQL.get(i).equalsIgnoreCase(cSQL.get(i)));
}
assertTrue(i + "-th SQL for JPQL and CriteriaQuery for " + jpql + " is different\r\n" +
"JPQL = [" + jSQL.get(i) + "]\r\n" +
"CSQL = [" + cSQL.get(i) + "]\r\n",
eq);
}

if (expectedSQL != null) {
assertTrue("SQL for JPQL " + jpql + " is different than expecetd " + expectedSQL,
jSQL.get(0).equalsIgnoreCase(expectedSQL));
same(expectedSQL, jSQL.get(0)));

}
}
Expand All @@ -227,12 +235,12 @@ void executeAndCompareSQL(String jpql, String expectedSQL) {
return;

for (int i = 0; i < jSQL.size(); i++) {
if (!jSQL.get(i).equalsIgnoreCase(expectedSQL)) {
boolean eq = same(expectedSQL, jSQL.get(i));
if (!eq) {
printSQL("SQL for JPQL", jSQL.get(i));
printSQL("Expected SQL", expectedSQL);
assertTrue(i + "-th SQL for JPQL: " + jSQL.get(i) + " are different than Expected SQL " + expectedSQL,
expectedSQL.equalsIgnoreCase(jSQL.get(i)));
}
assertTrue(i + "-th SQL for JPQL: " + jSQL.get(i) + " are different than Expected SQL " + expectedSQL, eq);
}
}

Expand All @@ -246,18 +254,20 @@ void executeAndCompareSQL(Query jQ, String expectedSQL) {
fail(w.toString());
}

if (!(dict instanceof DerbyDictionary || dict instanceof MySQLDictionary || dict instanceof MariaDBDictionary))
if (!(dict instanceof DerbyDictionary || dict instanceof MySQLDictionary || dict instanceof MariaDBDictionary)) {
return;
}

String jSql = jSQL.get(0).trim();
if (jSql.indexOf("optimize for 1 row") != -1)
if (jSql.indexOf("optimize for 1 row") != -1) {
jSql = jSql.substring(0, jSql.indexOf("optimize for 1 row")).trim();
}

if (!jSql.equalsIgnoreCase(expectedSQL)) {
boolean eq = same(expectedSQL, jSql);
if (!eq) {
printSQL("SQL for JPQL", jSql);
assertTrue("SQL for JPQL " + jSql + " is different than expecetd " + expectedSQL,
expectedSQL.equalsIgnoreCase(jSql));
}
assertTrue("SQL for JPQL " + jSql + " is different than expecetd " + expectedSQL, eq);
}

void executeExpectFail(CriteriaQuery<?> c, String jpql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,26 @@

import jakarta.persistence.Persistence;

import java.util.Locale;

import org.apache.openjpa.jdbc.identifier.Normalizer;
import org.apache.openjpa.jdbc.meta.MappingRepository;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.lib.identifier.IdentifierConfiguration;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;

/**
* Testcase that verifies the names for Foreign Key columns is as expected.
*/
public class TestFKColumnNames extends AbstractPersistenceTestCase {

private String getName(Column col) {
IdentifierConfiguration cfg = Normalizer.getNamingConfiguration();
return col.getIdentifier().getName()
.replace(cfg.getLeadingDelimiter(), "")
.replace(cfg.getTrailingDelimiter(), "")
.toUpperCase(Locale.ENGLISH);
}

/**
* <P>
Expand All @@ -48,11 +59,11 @@ public void testSQLKeywords() {
(MappingRepository) emf.getConfiguration()
.getMetaDataRepositoryInstance();

assertEquals("SELECT_ID", repos.getMapping(FKColumnNamesInner1Entity.class, null, true)
.getFieldMapping("select").getColumns()[0].getName());
assertEquals("SELECT_ID", getName(repos.getMapping(FKColumnNamesInner1Entity.class, null, true)
.getFieldMapping("select").getColumns()[0]));

assertEquals("FROM_ID", repos.getMapping(FKColumnNamesInner2Entity.class, null, true)
.getFieldMapping("from").getColumns()[0].getName());
assertEquals("FROM_ID", getName(repos.getMapping(FKColumnNamesInner2Entity.class, null, true)
.getFieldMapping("from").getColumns()[0]));
closeEMF(emf);
}

Expand Down
Loading
Loading