use of org.jooq.Field in project jOOQ by jOOQ.
the class ParserImpl method parseField.
private static final Field<?> parseField(ParserContext ctx, Type type) {
if (B.is(type)) {
Field<?> r = parseFieldAnd(ctx);
Condition c = null;
while (parseKeywordIf(ctx, "OR")) c = ((c == null) ? condition((Field) r) : c).or((Field) parseFieldAnd(ctx));
return c == null ? r : field(c);
} else {
return parseFieldConcat(ctx, type);
}
}
use of org.jooq.Field in project jOOQ by jOOQ.
the class ParserImpl method parseAlterTable.
private static final DDLQuery parseAlterTable(ParserContext ctx) {
boolean ifExists = parseKeywordIf(ctx, "IF EXISTS");
Table<?> tableName = parseTableName(ctx);
parseWhitespaceIf(ctx);
AlterTableStep s1 = ifExists ? ctx.dsl.alterTableIfExists(tableName) : ctx.dsl.alterTable(tableName);
switch(ctx.character()) {
case 'a':
case 'A':
if (parseKeywordIf(ctx, "ADD")) {
ConstraintTypeStep constraint = null;
if (parseKeywordIf(ctx, "CONSTRAINT"))
constraint = constraint(parseIdentifier(ctx));
if (parseKeywordIf(ctx, "PRIMARY KEY")) {
parse(ctx, '(');
Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
parse(ctx, ')');
return constraint == null ? s1.add(primaryKey(fieldNames)) : s1.add(constraint.primaryKey(fieldNames));
} else if (parseKeywordIf(ctx, "UNIQUE")) {
parse(ctx, '(');
Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
parse(ctx, ')');
return constraint == null ? s1.add(unique(fieldNames)) : s1.add(constraint.unique(fieldNames));
} else if (parseKeywordIf(ctx, "FOREIGN KEY")) {
parse(ctx, '(');
Field<?>[] referencing = parseFieldNames(ctx).toArray(EMPTY_FIELD);
parse(ctx, ')');
parseKeyword(ctx, "REFERENCES");
Table<?> referencedTable = parseTableName(ctx);
parse(ctx, '(');
Field<?>[] referencedFields = parseFieldNames(ctx).toArray(EMPTY_FIELD);
parse(ctx, ')');
if (referencing.length != referencedFields.length)
throw ctx.exception();
return constraint == null ? s1.add(foreignKey(referencing).references(referencedTable, referencedFields)) : s1.add(constraint.foreignKey(referencing).references(referencedTable, referencedFields));
} else if (parseKeywordIf(ctx, "CHECK")) {
parse(ctx, '(');
Condition condition = parseCondition(ctx);
parse(ctx, ')');
return constraint == null ? s1.add(check(condition)) : s1.add(constraint.check(condition));
} else if (constraint != null) {
throw ctx.unexpectedToken();
} else {
parseKeywordIf(ctx, "COLUMN");
// The below code is taken from CREATE TABLE, with minor modifications as
// https://github.com/jOOQ/jOOQ/issues/5317 has not yet been implemented
// Once implemented, we might be able to factor out the common logic into
// a new parseXXX() method.
String fieldName = parseIdentifier(ctx);
DataType type = parseDataType(ctx);
boolean nullable = false;
boolean defaultValue = false;
boolean unique = false;
for (; ; ) {
if (!nullable) {
if (parseKeywordIf(ctx, "NULL")) {
type = type.nullable(true);
nullable = true;
continue;
} else if (parseKeywordIf(ctx, "NOT NULL")) {
type = type.nullable(false);
nullable = true;
continue;
}
}
if (!defaultValue) {
if (parseKeywordIf(ctx, "DEFAULT")) {
type = type.defaultValue(parseField(ctx));
defaultValue = true;
continue;
}
}
if (!unique) {
if (parseKeywordIf(ctx, "PRIMARY KEY")) {
throw ctx.unexpectedToken();
} else if (parseKeywordIf(ctx, "UNIQUE")) {
throw ctx.unexpectedToken();
}
}
if (parseKeywordIf(ctx, "CHECK")) {
throw ctx.unexpectedToken();
}
break;
}
return s1.add(field(name(fieldName), type), type);
}
}
break;
case 'd':
case 'D':
if (parseKeywordIf(ctx, "DROP")) {
if (parseKeywordIf(ctx, "COLUMN")) {
Field<?> field = parseFieldName(ctx);
boolean cascade = parseKeywordIf(ctx, "CASCADE");
boolean restrict = !cascade && parseKeywordIf(ctx, "RESTRICT");
AlterTableDropStep s2 = s1.dropColumn(field);
AlterTableFinalStep s3 = cascade ? s2.cascade() : restrict ? s2.restrict() : s2;
return s3;
} else if (parseKeywordIf(ctx, "CONSTRAINT")) {
String constraint = parseIdentifier(ctx);
return s1.dropConstraint(constraint);
}
}
break;
case 'r':
case 'R':
if (parseKeywordIf(ctx, "RENAME")) {
if (parseKeywordIf(ctx, "TO")) {
String newName = parseIdentifier(ctx);
return s1.renameTo(newName);
} else if (parseKeywordIf(ctx, "COLUMN")) {
String oldName = parseIdentifier(ctx);
parseKeyword(ctx, "TO");
String newName = parseIdentifier(ctx);
return s1.renameColumn(oldName).to(newName);
} else if (parseKeywordIf(ctx, "CONSTRAINT")) {
String oldName = parseIdentifier(ctx);
parseKeyword(ctx, "TO");
String newName = parseIdentifier(ctx);
return s1.renameConstraint(oldName).to(newName);
}
}
break;
}
throw ctx.unexpectedToken();
}
use of org.jooq.Field in project jOOQ by jOOQ.
the class TableAlias method init.
/**
* Register fields for this table alias
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private final Fields<R> init(String[] fieldAliases) {
List<Field<?>> result = new ArrayList<Field<?>>();
Row row = this.alias.wrapped().fieldsRow();
int size = row.size();
for (int i = 0; i < size; i++) {
Field<?> field = row.field(i);
String name = field.getName();
if (fieldAliases != null && fieldAliases.length > i) {
name = fieldAliases[i];
}
result.add(new TableFieldImpl(name, field.getDataType(), this, field.getComment(), field.getBinding()));
}
return new Fields<R>(result);
}
use of org.jooq.Field in project jOOQ by jOOQ.
the class MergeImpl method getStandardMerge.
// -------------------------------------------------------------------------
// QueryPart API
// -------------------------------------------------------------------------
/**
* Return a standard MERGE statement emulating the H2-specific syntax
*/
private final QueryPart getStandardMerge() {
// The SRC for the USING() clause:
// ------------------------------
Table<?> src;
if (upsertSelect != null) {
List<Field<?>> v = new ArrayList<Field<?>>();
Row row = upsertSelect.fieldsRow();
for (int i = 0; i < row.size(); i++) {
v.add(row.field(i).as("s" + (i + 1)));
}
// [#579] TODO: Currently, this syntax may require aliasing
// on the call-site
src = DSL.select(v).from(upsertSelect).asTable("src");
} else {
List<Field<?>> v = new ArrayList<Field<?>>();
for (int i = 0; i < getUpsertValues().size(); i++) {
v.add(getUpsertValues().get(i).as("s" + (i + 1)));
}
src = DSL.select(v).asTable("src");
}
// The condition for the ON clause:
// --------------------------------
Set<Field<?>> onFields = new HashSet<Field<?>>();
Condition condition = null;
if (getUpsertKeys().isEmpty()) {
UniqueKey<?> key = table.getPrimaryKey();
if (key != null) {
onFields.addAll(key.getFields());
for (int i = 0; i < key.getFields().size(); i++) {
Condition rhs = key.getFields().get(i).equal((Field) src.field(i));
if (condition == null) {
condition = rhs;
} else {
condition = condition.and(rhs);
}
}
} else // This should probably execute an INSERT statement
{
throw new IllegalStateException("Cannot omit KEY() clause on a non-Updatable Table");
}
} else {
for (int i = 0; i < getUpsertKeys().size(); i++) {
int matchIndex = getUpsertFields().indexOf(getUpsertKeys().get(i));
if (matchIndex == -1) {
throw new IllegalStateException("Fields in KEY() clause must be part of the fields specified in MERGE INTO table (...)");
}
onFields.addAll(getUpsertKeys());
Condition rhs = getUpsertKeys().get(i).equal((Field) src.field(matchIndex));
if (condition == null) {
condition = rhs;
} else {
condition = condition.and(rhs);
}
}
}
// INSERT and UPDATE clauses
// -------------------------
Map<Field<?>, Field<?>> update = new LinkedHashMap<Field<?>, Field<?>>();
Map<Field<?>, Field<?>> insert = new LinkedHashMap<Field<?>, Field<?>>();
for (int i = 0; i < src.fieldsRow().size(); i++) {
// Oracle does not allow to update fields from the ON clause
if (!onFields.contains(getUpsertFields().get(i))) {
update.put(getUpsertFields().get(i), src.field(i));
}
insert.put(getUpsertFields().get(i), src.field(i));
}
return DSL.mergeInto(table).using(src).on(condition).whenMatchedThenUpdate().set(update).whenNotMatchedThenInsert().set(insert);
}
use of org.jooq.Field in project jOOQ by jOOQ.
the class ParserImpl method parseUpdate.
private static final Update<?> parseUpdate(ParserContext ctx) {
parseKeyword(ctx, "UPDATE");
Table<?> tableName = parseTableName(ctx);
parseKeyword(ctx, "SET");
// TODO Row value expression updates
Map<Field<?>, Object> map = parseSetClauseList(ctx);
// TODO support FROM
Condition condition = parseKeywordIf(ctx, "WHERE") ? parseCondition(ctx) : null;
// TODO support RETURNING
return condition == null ? ctx.dsl.update(tableName).set(map) : ctx.dsl.update(tableName).set(map).where(condition);
}
Aggregations