use of org.jooq.Table in project jOOQ by jOOQ.
the class DefaultParseContext method parseUpdate.
private final Query parseUpdate(WithImpl with, boolean parseResultQuery) {
parseKeyword("UPDATE", "UPD");
Field<Long> limit = null;
// T-SQL style TOP .. START AT
if (parseKeywordIf("TOP")) {
limit = (Field) parseField();
// [#8623] TODO Support this
// percent = parseKeywordIf("PERCENT") && requireProEdition();
}
Table<?> table = parseTable(() -> peekKeyword(KEYWORDS_IN_UPDATE_FROM));
scope(table);
UpdateSetFirstStep<?> s1 = (with == null ? dsl.update(table) : with.update(table));
List<Table<?>> from = parseKeywordIf("FROM") ? parseList(',', t -> parseTable(() -> peekKeyword(KEYWORDS_IN_UPDATE_FROM))) : null;
parseKeyword("SET");
UpdateFromStep<?> s2;
if (peek('(')) {
Row row = parseRow();
parse('=');
// TODO Can we extract a public API for this?
if (peekSelectOrWith(true))
((UpdateImpl<?>) s1).getDelegate().addValues0(row, parseWithOrSelect(row.size()));
else
((UpdateImpl<?>) s1).getDelegate().addValues0(row, parseRow(row.size()));
s2 = (UpdateFromStep<?>) s1;
} else {
Map<Field<?>, Object> map = parseSetClauseList();
s2 = s1.set(map);
}
UpdateWhereStep<?> s3 = from != null ? s2.from(from) : parseKeywordIf("FROM") ? s2.from(parseList(',', t -> parseTable(() -> peekKeyword(KEYWORDS_IN_UPDATE_FROM)))) : s2;
UpdateOrderByStep<?> s4 = parseKeywordIf("ALL") ? s3 : parseKeywordIf("WHERE") ? s3.where(parseCondition()) : s3;
UpdateLimitStep<?> s5 = parseKeywordIf("ORDER BY") ? s4.orderBy(parseList(',', c -> c.parseSortField())) : s4;
UpdateReturningStep<?> s6 = (limit != null || parseKeywordIf("LIMIT")) ? s5.limit(limit != null ? limit : (Field) parseField()) : s5;
return (parseResultQuery ? parseKeyword("RETURNING") : parseKeywordIf("RETURNING")) ? s6.returning(parseSelectList()) : s6;
}
use of org.jooq.Table in project jOOQ by jOOQ.
the class DDL method queries.
final Queries queries(Meta meta) {
List<Query> queries = new ArrayList<>();
List<Schema> schemas = sortIf(meta.getSchemas(), !configuration.respectSchemaOrder());
for (Schema schema : schemas) if (configuration.flags().contains(SCHEMA) && !schema.getUnqualifiedName().empty())
if (configuration.createSchemaIfNotExists())
queries.add(ctx.createSchemaIfNotExists(schema.getUnqualifiedName()));
else
queries.add(ctx.createSchema(schema.getUnqualifiedName()));
if (configuration.flags().contains(TABLE)) {
for (Schema schema : schemas) {
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder())) {
List<Constraint> constraints = new ArrayList<>();
constraints.addAll(primaryKeys(table));
constraints.addAll(uniqueKeys(table));
constraints.addAll(checks(table));
queries.addAll(createTableOrView(table, constraints));
}
}
} else {
for (Schema schema : schemas) {
if (configuration.flags().contains(PRIMARY_KEY))
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder())) for (Constraint constraint : sortIf(primaryKeys(table), !configuration.respectConstraintOrder())) queries.add(ctx.alterTable(table).add(constraint));
if (configuration.flags().contains(UNIQUE))
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder())) for (Constraint constraint : sortIf(uniqueKeys(table), !configuration.respectConstraintOrder())) queries.add(ctx.alterTable(table).add(constraint));
if (configuration.flags().contains(CHECK))
for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder())) for (Constraint constraint : sortIf(checks(table), !configuration.respectConstraintOrder())) queries.add(ctx.alterTable(table).add(constraint));
}
}
if (configuration.flags().contains(FOREIGN_KEY))
for (Schema schema : schemas) for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder())) for (Constraint constraint : foreignKeys(table)) queries.add(ctx.alterTable(table).add(constraint));
if (configuration.flags().contains(DOMAIN))
for (Schema schema : schemas) for (Domain<?> domain : sortIf(schema.getDomains(), !configuration.respectDomainOrder())) queries.add(createDomain(domain));
if (configuration.flags().contains(SEQUENCE))
for (Schema schema : schemas) for (Sequence<?> sequence : sortIf(schema.getSequences(), !configuration.respectSequenceOrder())) queries.add(createSequence(sequence));
if (configuration.flags().contains(COMMENT))
for (Schema schema : schemas) for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder())) queries.addAll(commentOn(table));
if (configuration.flags().contains(INDEX))
for (Schema schema : schemas) for (Table<?> table : sortIf(schema.getTables(), !configuration.respectTableOrder())) queries.addAll(createIndex(table));
return ctx.queries(queries);
}
use of org.jooq.Table in project jOOQ by jOOQ.
the class SelectQueryImpl method distinctOnEmulation.
@SuppressWarnings({ "rawtypes", "unchecked" })
private final Select<?> distinctOnEmulation() {
// [#3564] TODO: Extract and merge this with getSelectResolveSomeAsterisks0()
List<Field<?>> partitionBy = new ArrayList<>(distinctOn.size());
for (SelectFieldOrAsterisk s : distinctOn) if (s instanceof Field)
partitionBy.add((Field<?>) s);
Field<Integer> rn = rowNumber().over(partitionBy(partitionBy).orderBy(orderBy)).as("rn");
SelectQueryImpl<R> copy = copy(x -> {
});
copy.distinctOn = null;
copy.select.add(rn);
copy.orderBy.clear();
copy.limit.clear();
SelectLimitStep<?> s1 = DSL.select(new QualifiedSelectFieldList(table(name("t")), select)).from(copy.asTable("t")).where(rn.eq(one())).orderBy(map(orderBy, o -> unqualified(o)));
if (limit.limit != null) {
SelectLimitPercentStep<?> s2 = s1.limit(limit.limit);
SelectWithTiesStep<?> s3 = limit.percent ? s2.percent() : s2;
SelectOffsetStep<?> s4 = limit.withTies ? s3.withTies() : s3;
return limit.offset != null ? s4.offset(limit.offset) : s4;
} else
return limit.offset != null ? s1.offset(limit.offset) : s1;
}
use of org.jooq.Table in project jOOQ by jOOQ.
the class JavaGenerator method generateSchema.
protected void generateSchema(SchemaDefinition schema, JavaWriter out) {
final String catalogId = out.ref(getStrategy().getFullJavaIdentifier(schema.getCatalog()), 2);
final String schemaName = schema.getQualifiedOutputName();
final String schemaId = getStrategy().getJavaIdentifier(schema);
final String className = getStrategy().getJavaClassName(schema);
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(schema, Mode.DEFAULT));
printPackage(out, schema);
if (scala) {
out.println("object %s {", className);
out.tab(1).javadoc("The reference instance of <code>%s</code>", schemaName);
out.tab(1).println("val %s = new %s", schemaId, className);
out.println("}");
out.println();
}
generateSchemaClassJavadoc(schema, out);
printClassAnnotations(out, schema);
if (scala) {
out.println("class %s extends %s(\"%s\", %s)[[before= with ][separator= with ][%s]] {", className, SchemaImpl.class, schema.getOutputName(), catalogId, interfaces);
} else {
out.println("public class %s extends %s[[before= implements ][%s]] {", className, SchemaImpl.class, interfaces);
out.printSerial();
out.tab(1).javadoc("The reference instance of <code>%s</code>", schemaName);
out.tab(1).println("public static final %s %s = new %s();", className, schemaId, className);
if (generateGlobalTableReferences()) {
for (TableDefinition table : schema.getTables()) {
final String tableClassName = out.ref(getStrategy().getFullJavaClassName(table));
final String tableId = getStrategy().getJavaIdentifier(table);
final String tableFullId = getStrategy().getFullJavaIdentifier(table);
final String tableComment = !StringUtils.isBlank(table.getComment()) ? escapeEntities(table.getComment()) : "The table <code>" + table.getQualifiedOutputName() + "</code>.";
out.tab(1).javadoc(tableComment);
if (scala)
out.tab(1).println("val %s = %s", tableId, tableFullId);
else
out.tab(1).println("public final %s %s = %s;", tableClassName, tableId, tableFullId);
// globalObjectReferences
if (table.isTableValuedFunction())
printTableValuedFunction(out, table, getStrategy().getJavaIdentifier(table));
}
}
out.tab(1).javadoc(NO_FURTHER_INSTANCES_ALLOWED);
out.tab(1).println("private %s() {", className);
out.tab(2).println("super(\"%s\", null);", schema.getOutputName());
out.tab(1).println("}");
}
out.println();
if (scala) {
out.tab(1).println("override def getCatalog : %s = %s", Catalog.class, catalogId);
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s getCatalog() {", Catalog.class);
out.tab(2).println("return %s;", catalogId);
out.tab(1).println("}");
}
// [#2255] Avoid referencing sequence literals, if they're not generated
if (generateGlobalSequenceReferences())
printReferences(out, database.getSequences(schema), Sequence.class, true);
if (generateGlobalTableReferences())
printReferences(out, database.getTables(schema), Table.class, true);
if (generateGlobalUDTReferences())
printReferences(out, database.getUDTs(schema), UDT.class, true);
generateSchemaClassFooter(schema, out);
out.println("}");
}
use of org.jooq.Table in project jOOQ by jOOQ.
the class ParserImpl method parseQueryPrimary.
private static final SelectQueryImpl<Record> parseQueryPrimary(ParserContext ctx) {
if (parseIf(ctx, '(')) {
SelectQueryImpl<Record> result = parseSelect(ctx);
parse(ctx, ')');
return result;
}
parseKeyword(ctx, "SELECT");
boolean distinct = parseKeywordIf(ctx, "DISTINCT") || parseKeywordIf(ctx, "UNIQUE");
Long limit = null;
Long offset = null;
if (!distinct)
parseKeywordIf(ctx, "ALL");
if (parseKeywordIf(ctx, "TOP")) {
limit = parseUnsignedInteger(ctx);
if (parseKeywordIf(ctx, "START AT"))
offset = parseUnsignedInteger(ctx);
}
List<Field<?>> select = parseSelectList(ctx);
List<Table<?>> from = null;
Condition startWith = null;
Condition connectBy = null;
boolean connectByNoCycle = false;
Condition where = null;
List<GroupField> groupBy = null;
Condition having = null;
if (parseKeywordIf(ctx, "FROM"))
from = parseTables(ctx);
// TODO is there a better way?
if (from != null && from.size() == 1 && from.get(0).getName().equalsIgnoreCase("dual"))
from = null;
if (parseKeywordIf(ctx, "START WITH")) {
startWith = parseCondition(ctx);
parseKeyword(ctx, "CONNECT BY");
connectByNoCycle = parseKeywordIf(ctx, "NOCYCLE");
connectBy = parseCondition(ctx);
} else if (parseKeywordIf(ctx, "CONNECT BY")) {
connectByNoCycle = parseKeywordIf(ctx, "NOCYCLE");
connectBy = parseCondition(ctx);
if (parseKeywordIf(ctx, "START WITH"))
startWith = parseCondition(ctx);
}
if (parseKeywordIf(ctx, "WHERE"))
where = parseCondition(ctx);
if (parseKeywordIf(ctx, "GROUP BY")) {
if (parseIf(ctx, '(')) {
parse(ctx, ')');
groupBy = emptyList();
} else if (parseKeywordIf(ctx, "ROLLUP")) {
parse(ctx, '(');
groupBy = singletonList(rollup(parseFields(ctx).toArray(EMPTY_FIELD)));
parse(ctx, ')');
} else if (parseKeywordIf(ctx, "CUBE")) {
parse(ctx, '(');
groupBy = singletonList(cube(parseFields(ctx).toArray(EMPTY_FIELD)));
parse(ctx, ')');
} else if (parseKeywordIf(ctx, "GROUPING SETS")) {
List<List<Field<?>>> fieldSets = new ArrayList<List<Field<?>>>();
parse(ctx, '(');
do {
parse(ctx, '(');
if (parseIf(ctx, ')')) {
fieldSets.add(Collections.<Field<?>>emptyList());
} else {
fieldSets.add(parseFields(ctx));
parse(ctx, ')');
}
} while (parseIf(ctx, ','));
parse(ctx, ')');
groupBy = singletonList(groupingSets(fieldSets.toArray((Collection[]) EMPTY_COLLECTION)));
} else {
groupBy = (List) parseFields(ctx);
if (parseKeywordIf(ctx, "WITH ROLLUP"))
groupBy = singletonList(rollup(groupBy.toArray(EMPTY_FIELD)));
}
}
if (parseKeywordIf(ctx, "HAVING"))
having = parseCondition(ctx);
// TODO support WINDOW
SelectQueryImpl<Record> result = (SelectQueryImpl<Record>) ctx.dsl.selectQuery();
if (distinct)
result.setDistinct(distinct);
if (select.size() > 0)
result.addSelect(select);
if (from != null)
result.addFrom(from);
if (connectBy != null)
if (connectByNoCycle)
result.addConnectByNoCycle(connectBy);
else
result.addConnectBy(connectBy);
if (startWith != null)
result.setConnectByStartWith(startWith);
if (where != null)
result.addConditions(where);
if (groupBy != null)
result.addGroupBy(groupBy);
if (having != null)
result.addHaving(having);
if (limit != null)
if (offset != null)
result.addLimit((int) (long) offset, (int) (long) limit);
else
result.addLimit((int) (long) limit);
return result;
}
Aggregations