mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
Merge pull request #862 from querydsl/i861
Add escaping of pk and fk property names
This commit is contained in:
commit
bc0c26ddf8
@ -15,6 +15,7 @@ package com.mysema.query.sql.codegen;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.mysema.query.codegen.EntityType;
|
||||
|
||||
/**
|
||||
@ -76,7 +77,7 @@ public class DefaultNamingStrategy extends AbstractNamingStrategy {
|
||||
if (fkName.toLowerCase().startsWith("fk_")) {
|
||||
fkName = fkName.substring(3) + "_" + fkName.substring(0,2);
|
||||
}
|
||||
return getPropertyName(fkName, entityType);
|
||||
return escape(entityType, getPropertyName(fkName, entityType));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -90,7 +91,13 @@ public class DefaultNamingStrategy extends AbstractNamingStrategy {
|
||||
if (pkName.toLowerCase().startsWith("pk_")) {
|
||||
pkName = pkName.substring(3) + "_" + pkName.substring(0,2);
|
||||
}
|
||||
return getPropertyName(pkName, entityType);
|
||||
String propertyName = getPropertyName(pkName, entityType);
|
||||
for (String candidate : ImmutableList.of(propertyName, propertyName + "Pk")) {
|
||||
if (!entityType.getEscapedPropertyNames().contains(candidate)) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return escape(entityType, propertyName);
|
||||
}
|
||||
|
||||
protected String normalizePropertyName(String name) {
|
||||
|
||||
@ -13,13 +13,12 @@
|
||||
*/
|
||||
package com.mysema.query.sql.codegen;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.codegen.model.Types;
|
||||
import com.mysema.query.codegen.EntityType;
|
||||
import com.mysema.query.codegen.Property;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DefaultNamingStrategyTest {
|
||||
|
||||
@ -86,13 +85,18 @@ public class DefaultNamingStrategyTest {
|
||||
assertEquals("refFooBar_", namingStrategy.getPropertyNameForForeignKey("REF_FOO_BAR_", entityModel));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void GetPropertyNameForPrimaryKey() {
|
||||
assertEquals("superiorPk", namingStrategy.getPropertyNameForPrimaryKey("pk_superior", entityModel));
|
||||
assertEquals("superiorPk", namingStrategy.getPropertyNameForPrimaryKey("PK_SUPERIOR", entityModel));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void GetPropertyNameForPrimaryKey_Clash() {
|
||||
entityModel.addProperty(new Property(entityModel, "id", Types.STRING));
|
||||
assertEquals("idPk", namingStrategy.getPropertyNameForPrimaryKey("id", entityModel));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void GetDefaultVariableName() {
|
||||
assertEquals("object", namingStrategy.getDefaultVariableName(entityModel));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user