mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Merge pull request #1783 from querydsl/i1780
Improve with serialization
This commit is contained in:
commit
7bf13ccc93
@ -50,6 +50,8 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
|
||||
private final List<Object> constants = new ArrayList<Object>();
|
||||
|
||||
private final Set<Path<?>> withAliases = Sets.newHashSet();
|
||||
|
||||
private final boolean dml;
|
||||
|
||||
protected Stage stage = Stage.SELECT;
|
||||
@ -190,17 +192,23 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
if (je.getTarget() instanceof RelationalPath && templates.isSupportsAlias()) {
|
||||
final RelationalPath<?> pe = (RelationalPath<?>) je.getTarget();
|
||||
if (pe.getMetadata().getParent() == null) {
|
||||
SchemaAndTable schemaAndTable = getSchemaAndTable(pe);
|
||||
boolean precededByDot;
|
||||
if (templates.isPrintSchema()) {
|
||||
appendSchemaName(schemaAndTable.getSchema());
|
||||
append(".");
|
||||
precededByDot = true;
|
||||
if (withAliases.contains(pe)) {
|
||||
String name = pe.getMetadata().getName();
|
||||
appendTableName(pe.getMetadata().getName(), false);
|
||||
append(templates.getTableAlias());
|
||||
} else {
|
||||
precededByDot = false;
|
||||
SchemaAndTable schemaAndTable = getSchemaAndTable(pe);
|
||||
boolean precededByDot;
|
||||
if (templates.isPrintSchema()) {
|
||||
appendSchemaName(schemaAndTable.getSchema());
|
||||
append(".");
|
||||
precededByDot = true;
|
||||
} else {
|
||||
precededByDot = false;
|
||||
}
|
||||
appendTableName(schemaAndTable.getTable(), precededByDot);
|
||||
append(templates.getTableAlias());
|
||||
}
|
||||
appendTableName(schemaAndTable.getTable(), precededByDot);
|
||||
append(templates.getTableAlias());
|
||||
}
|
||||
}
|
||||
inJoin = true;
|
||||
@ -968,6 +976,14 @@ public class SQLSerializer extends SerializerBase<SQLSerializer> {
|
||||
} else {
|
||||
super.visitOperation(type, operator, args);
|
||||
}
|
||||
|
||||
if (operator == SQLOps.WITH_ALIAS || operator == SQLOps.WITH_COLUMNS) {
|
||||
if (args.get(0) instanceof Path) {
|
||||
withAliases.add((Path<?>) args.get(0));
|
||||
} else {
|
||||
withAliases.add((Path<?>) ((Operation<?>) args.get(0)).getArg(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setUseLiterals(boolean useLiterals) {
|
||||
|
||||
@ -1864,8 +1864,8 @@ public class SelectBase extends AbstractBaseTest {
|
||||
@Test
|
||||
@IncludeIn({HSQLDB, ORACLE, POSTGRESQL})
|
||||
public void with() {
|
||||
assertEquals(100, query().with(employee2, query().from(employee)
|
||||
.where(employee.firstname.eq("Tom"))
|
||||
assertEquals(10, query().with(employee2, query().from(employee)
|
||||
.where(employee.firstname.eq("Jim"))
|
||||
.select(Wildcard.all))
|
||||
.from(employee, employee2)
|
||||
.select(employee.id, employee2.id).fetch().size());
|
||||
@ -1875,11 +1875,11 @@ public class SelectBase extends AbstractBaseTest {
|
||||
@IncludeIn({HSQLDB, ORACLE, POSTGRESQL})
|
||||
public void with2() {
|
||||
QEmployee employee3 = new QEmployee("e3");
|
||||
assertEquals(1000, query().with(employee2, query().from(employee)
|
||||
.where(employee.firstname.eq("Tom"))
|
||||
assertEquals(100, query().with(employee2, query().from(employee)
|
||||
.where(employee.firstname.eq("Jim"))
|
||||
.select(Wildcard.all))
|
||||
.with(employee2, query().from(employee)
|
||||
.where(employee.firstname.eq("Tom"))
|
||||
.where(employee.firstname.eq("Jim"))
|
||||
.select(Wildcard.all))
|
||||
.from(employee, employee2, employee3)
|
||||
.select(employee.id, employee2.id, employee3.id).fetch().size());
|
||||
@ -1888,9 +1888,9 @@ public class SelectBase extends AbstractBaseTest {
|
||||
@Test
|
||||
@IncludeIn({HSQLDB, ORACLE, POSTGRESQL})
|
||||
public void with3() {
|
||||
assertEquals(100, query().with(employee2, employee2.all()).as(
|
||||
assertEquals(10, query().with(employee2, employee2.all()).as(
|
||||
query().from(employee)
|
||||
.where(employee.firstname.eq("Tom"))
|
||||
.where(employee.firstname.eq("Jim"))
|
||||
.select(Wildcard.all))
|
||||
.from(employee, employee2)
|
||||
.select(employee.id, employee2.id).fetch().size());
|
||||
@ -1899,12 +1899,12 @@ public class SelectBase extends AbstractBaseTest {
|
||||
@Test
|
||||
@IncludeIn({HSQLDB, ORACLE, POSTGRESQL})
|
||||
public void with_limit() {
|
||||
assertEquals(50, query().with(employee2, employee2.all()).as(
|
||||
assertEquals(5, query().with(employee2, employee2.all()).as(
|
||||
query().from(employee)
|
||||
.where(employee.firstname.eq("Tom"))
|
||||
.where(employee.firstname.eq("Jim"))
|
||||
.select(Wildcard.all))
|
||||
.from(employee, employee2)
|
||||
.limit(50)
|
||||
.limit(5)
|
||||
.orderBy(employee.id.asc(), employee2.id.asc())
|
||||
.select(employee.id, employee2.id).fetch().size());
|
||||
}
|
||||
@ -1912,13 +1912,13 @@ public class SelectBase extends AbstractBaseTest {
|
||||
@Test
|
||||
@IncludeIn({HSQLDB, ORACLE, POSTGRESQL})
|
||||
public void with_limitOffset() {
|
||||
assertEquals(50, query().with(employee2, employee2.all()).as(
|
||||
assertEquals(5, query().with(employee2, employee2.all()).as(
|
||||
query().from(employee)
|
||||
.where(employee.firstname.eq("Tom"))
|
||||
.where(employee.firstname.eq("Jim"))
|
||||
.select(Wildcard.all))
|
||||
.from(employee, employee2)
|
||||
.limit(50)
|
||||
.offset(10)
|
||||
.limit(10)
|
||||
.offset(5)
|
||||
.orderBy(employee.id.asc(), employee2.id.asc())
|
||||
.select(employee.id, employee2.id).fetch().size());
|
||||
}
|
||||
@ -1926,8 +1926,8 @@ public class SelectBase extends AbstractBaseTest {
|
||||
@Test
|
||||
@IncludeIn({ORACLE, POSTGRESQL})
|
||||
public void with_recursive() {
|
||||
assertEquals(100, query().withRecursive(employee2, query().from(employee)
|
||||
.where(employee.firstname.eq("Tom"))
|
||||
assertEquals(10, query().withRecursive(employee2, query().from(employee)
|
||||
.where(employee.firstname.eq("Jim"))
|
||||
.select(Wildcard.all))
|
||||
.from(employee, employee2)
|
||||
.select(employee.id, employee2.id).fetch().size());
|
||||
@ -1937,9 +1937,9 @@ public class SelectBase extends AbstractBaseTest {
|
||||
@Test
|
||||
@IncludeIn({ORACLE, POSTGRESQL})
|
||||
public void with_recursive2() {
|
||||
assertEquals(100, query().withRecursive(employee2, employee2.all()).as(
|
||||
assertEquals(10, query().withRecursive(employee2, employee2.all()).as(
|
||||
query().from(employee)
|
||||
.where(employee.firstname.eq("Tom"))
|
||||
.where(employee.firstname.eq("Jim"))
|
||||
.select(Wildcard.all))
|
||||
.from(employee, employee2)
|
||||
.select(employee.id, employee2.id).fetch().size());
|
||||
|
||||
@ -205,8 +205,22 @@ public class SerializationTest {
|
||||
select(survey2.id, survey2.name).from(survey2));
|
||||
|
||||
assertEquals("with SURVEY (ID, NAME) as (select survey2.ID, survey2.NAME\n" +
|
||||
"from SURVEY survey2)\n\n" +
|
||||
"from dual", q.toString());
|
||||
"from SURVEY survey2)\n\n" +
|
||||
"from dual", q.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void with_complex() {
|
||||
QSurvey s = new QSurvey("s");
|
||||
SQLQuery<?> q = new SQLQuery<Void>();
|
||||
q.with(s, s.id, s.name).as(
|
||||
select(survey.id, survey.name).from(survey))
|
||||
.select(s.id, s.name, survey.id, survey.name).from(s, survey);
|
||||
|
||||
assertEquals("with s (ID, NAME) as (select SURVEY.ID, SURVEY.NAME\n" +
|
||||
"from SURVEY SURVEY)\n" +
|
||||
"select s.ID, s.NAME, SURVEY.ID, SURVEY.NAME\n" +
|
||||
"from s s, SURVEY SURVEY", q.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
Reference in New Issue
Block a user