mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-19 21:00:53 +08:00
#633136 : fixed path quoting issues
This commit is contained in:
parent
686755fc5e
commit
0fc3ffefec
@ -5,19 +5,27 @@
|
||||
*/
|
||||
package com.mysema.query.types.path;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import com.mysema.query.types.Expr;
|
||||
import com.mysema.query.types.TemplateFactory;
|
||||
import com.mysema.query.types.custom.CSimple;
|
||||
|
||||
/**
|
||||
* NullExpr defines a general null expression
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public class NullExpr<T> extends PSimple<T>{
|
||||
public class NullExpr<T> extends CSimple<T>{
|
||||
|
||||
public static final NullExpr<Object> DEFAULT = new NullExpr<Object>(Object.class);
|
||||
|
||||
private static final long serialVersionUID = -5311968198973316411L;
|
||||
|
||||
public NullExpr(Class<? extends T> type) {
|
||||
super(type, "null");
|
||||
super(type,TemplateFactory.DEFAULT.create("null"), Collections.<Expr<?>>emptyList());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ import com.mysema.query.types.Path;
|
||||
*/
|
||||
public class Configuration {
|
||||
|
||||
public static final Configuration DEFAULT = new Configuration(new SQLTemplates());
|
||||
public static final Configuration DEFAULT = new Configuration(new SQLTemplates("\"",false));
|
||||
|
||||
private final JDBCTypeMapping jdbcTypeMapping = new JDBCTypeMapping();
|
||||
|
||||
|
||||
@ -88,12 +88,12 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
|
||||
private void appendAsColumnName(Path<?> path){
|
||||
String column = path.getMetadata().getExpression().toString();
|
||||
append(templates.quoteColumnName(column));
|
||||
append(templates.quoteIdentifier(column));
|
||||
}
|
||||
|
||||
private void appendAsTableName(Path<?> path){
|
||||
String table = path.getAnnotatedElement().getAnnotation(Table.class).value();
|
||||
append(templates.quoteTableName(table));
|
||||
append(templates.quoteIdentifier(table));
|
||||
}
|
||||
|
||||
public List<Object> getConstants(){
|
||||
@ -480,14 +480,21 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
if (dml){
|
||||
if (path.equals(entity)){
|
||||
appendAsTableName(path);
|
||||
return null;
|
||||
}else if (entity.equals(path.getMetadata().getParent()) && skipParent){
|
||||
appendAsColumnName(path);
|
||||
}else{
|
||||
super.visit(path, context);
|
||||
return null;
|
||||
}
|
||||
}else{
|
||||
super.visit(path, context);
|
||||
}
|
||||
if (path.getMetadata().getPathType() == PathType.DELEGATE){
|
||||
handle(path.getMetadata().getExpression());
|
||||
return null;
|
||||
|
||||
}else if (path.getMetadata().getParent() != null){
|
||||
visit(path.getMetadata().getParent(), context);
|
||||
append(".");
|
||||
}
|
||||
append(templates.quoteIdentifier(path.getMetadata().getExpression().toString()));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@ -6,17 +6,11 @@
|
||||
package com.mysema.query.sql;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mysema.commons.lang.Assert;
|
||||
import com.mysema.query.JoinType;
|
||||
import com.mysema.query.QueryException;
|
||||
@ -41,7 +35,7 @@ public class SQLTemplates extends Templates {
|
||||
|
||||
public static final Operator<Object> UNION = new OperatorImpl<Object>("UNION");
|
||||
|
||||
public static final SQLTemplates DEFAULT = new SQLTemplates();
|
||||
public static final SQLTemplates DEFAULT = new SQLTemplates("\"",false);
|
||||
|
||||
private static final Pattern IDENTIFIER_CHARS = Pattern.compile("[a-zA-Z0-9_\\-]+");
|
||||
|
||||
@ -118,10 +112,6 @@ public class SQLTemplates extends Templates {
|
||||
private String values = "\nvalues ";
|
||||
|
||||
private String where = "\nwhere ";
|
||||
|
||||
protected SQLTemplates() {
|
||||
this("\"", false);
|
||||
}
|
||||
|
||||
protected SQLTemplates(String quoteStr, boolean useQuotes) {
|
||||
this.quoteStr = Assert.notNull(quoteStr, "quoteStr");
|
||||
@ -161,11 +151,6 @@ public class SQLTemplates extends Templates {
|
||||
add(Ops.SUBSTR_1ARG, "substr({0},{1}+1)");
|
||||
add(Ops.SUBSTR_2ARGS, "substr({0},{1}+1,{2})");
|
||||
|
||||
if (quoteStr != null){
|
||||
add(PathType.PROPERTY, "{0}." + quoteStr + "{1s}" + quoteStr);
|
||||
add(PathType.VARIABLE, quoteStr + "{0s}" + quoteStr);
|
||||
}
|
||||
|
||||
add(CAST, "cast({0} as {1s})");
|
||||
add(UNION, "{0}\nunion\n{1}");
|
||||
|
||||
@ -361,16 +346,8 @@ public class SQLTemplates extends Templates {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public final String quoteColumnName(String column){
|
||||
return quoteIdentifier(column);
|
||||
}
|
||||
|
||||
public final String quoteTableName(String table){
|
||||
return quoteIdentifier(table);
|
||||
}
|
||||
|
||||
protected String quoteIdentifier(String identifier){
|
||||
public String quoteIdentifier(String identifier){
|
||||
if (useQuotes || requiresQuotes(identifier)){
|
||||
return quoteStr + identifier + quoteStr;
|
||||
}else{
|
||||
|
||||
@ -52,7 +52,7 @@ public class CreateTableClause {
|
||||
public CreateTableClause(Connection conn, SQLTemplates templates, String table) {
|
||||
this.connection = conn;
|
||||
this.templates = templates;
|
||||
this.table = templates.quoteTableName(table);
|
||||
this.table = templates.quoteIdentifier(table);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,7 +65,7 @@ public class CreateTableClause {
|
||||
public CreateTableClause column(String name, Class<?> type) {
|
||||
Assert.notNull(name,"name");
|
||||
Assert.notNull(type,"type");
|
||||
columns.add(new ColumnData(templates.quoteColumnName(name), templates.getTypeForClass(type)));
|
||||
columns.add(new ColumnData(templates.quoteIdentifier(name), templates.getTypeForClass(type)));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -115,7 +115,7 @@ public class CreateTableClause {
|
||||
Assert.notNull(name,"name");
|
||||
Assert.notEmpty(columns,"columns");
|
||||
for (int i = 0; i < columns.length; i++){
|
||||
columns[i] = templates.quoteColumnName(columns[i]);
|
||||
columns[i] = templates.quoteIdentifier(columns[i]);
|
||||
}
|
||||
primaryKey = new PrimaryKeyData(name, columns);
|
||||
return this;
|
||||
|
||||
@ -28,7 +28,7 @@ public class DropTableClause {
|
||||
|
||||
public DropTableClause(Connection conn, SQLTemplates templates, String table) {
|
||||
this.connection = conn;
|
||||
this.table = templates.quoteTableName(table);
|
||||
this.table = templates.quoteIdentifier(table);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
|
||||
|
||||
@ -40,8 +40,8 @@ public class ForeignKeyBuilder {
|
||||
ForeignKeyData foreignKey = new ForeignKeyData(name, table);
|
||||
for (int i = 0; i < parentColumns.length; i++){
|
||||
foreignKey.add(
|
||||
templates.quoteColumnName(foreignColumns[i]),
|
||||
templates.quoteColumnName(parentColumns[i]));
|
||||
templates.quoteIdentifier(foreignColumns[i]),
|
||||
templates.quoteIdentifier(parentColumns[i]));
|
||||
}
|
||||
foreignKeys.add(foreignKey);
|
||||
return clause;
|
||||
|
||||
@ -606,7 +606,7 @@ public abstract class SelectBaseTest extends AbstractBaseTest{
|
||||
|
||||
@Test
|
||||
public void aliasQuotes() {
|
||||
expectedQuery = "select firstname as \"First Name\" from EMPLOYEE2 e";
|
||||
expectedQuery = "select e.FIRSTNAME as \"First Name\" from EMPLOYEE2 e";
|
||||
query().from(employee).list(employee.firstname.as("First Name"));
|
||||
}
|
||||
|
||||
|
||||
@ -37,7 +37,7 @@ public class SelectMySQLTest extends SelectBaseTest {
|
||||
@Test
|
||||
@Override
|
||||
public void aliasQuotes() {
|
||||
expectedQuery = "select firstname as `First Name` from EMPLOYEE2 e";
|
||||
expectedQuery = "select e.FIRSTNAME as `First Name` from EMPLOYEE2 e";
|
||||
query().from(employee).list(employee.firstname.as("First Name"));
|
||||
}
|
||||
|
||||
|
||||
@ -8,6 +8,8 @@ package com.mysema.query.sql;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.types.Template;
|
||||
@ -26,4 +28,12 @@ public class SQLTemplatesTest {
|
||||
serializer.handle(CSimple.create(Object.class, template, ENumberConst.create(5)));
|
||||
assertEquals("fetch first 5 rows only", serializer.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void quote(){
|
||||
Pattern pattern = Pattern.compile("[a-zA-Z0-9_\\-]+");
|
||||
assertTrue(pattern.matcher("a1").matches());
|
||||
assertTrue(pattern.matcher("a").matches());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ public class TemplatesTest {
|
||||
new MySQLTemplates();
|
||||
new OracleTemplates();
|
||||
new PostgresTemplates();
|
||||
new SQLTemplates();
|
||||
new SQLTemplates("\"",false);
|
||||
new SQLServerTemplates();
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user