use of org.jooq.Field in project jOOQ by jOOQ.
the class DefaultDSLContext method fetchLazy.
@Override
public Cursor<Record> fetchLazy(ResultSet rs, DataType<?>... types) {
try {
Field<?>[] fields = new Field[types.length];
ResultSetMetaData meta = rs.getMetaData();
int columns = meta.getColumnCount();
for (int i = 0; i < types.length && i < columns; i++) {
fields[i] = field(meta.getColumnLabel(i + 1), types[i]);
}
return fetchLazy(rs, fields);
} catch (SQLException e) {
throw new DataAccessException("Error while accessing ResultSet meta data", e);
}
}
use of org.jooq.Field in project jOOQ by jOOQ.
the class Fields method field.
@Override
@SuppressWarnings("unchecked")
public final <T> Field<T> field(Field<T> field) {
if (field == null)
return null;
// [#4540] Try finding a match by identity
for (Field<?> f : fields) if (f == field)
return (Field<T>) f;
// [#1802] Try finding an exact match (e.g. exact matching qualified name)
for (Field<?> f : fields) if (f.equals(field))
return (Field<T>) f;
// [#4283] table / column matches are better than only column matches
Field<?> columnMatch = null;
Field<?> columnMatch2 = null;
String tableName = tableName(field);
String fieldName = field.getName();
for (Field<?> f : fields) {
String fName = f.getName();
if (tableName != null) {
String tName = tableName(f);
if (tName != null && tableName.equals(tName) && fName.equals(fieldName))
return (Field<T>) f;
}
// In case no exact match was found, return the first field with matching name
if (fName.equals(fieldName)) {
if (columnMatch == null)
columnMatch = f;
else
// [#4476] [#4477] This might be unintentional from a user
// perspective, e.g. when ambiguous ID columns are present.
// [#5578] Finish the loop, though, as we might have an exact match
// despite some ambiguity
columnMatch2 = f;
}
}
if (columnMatch2 != null)
if (log.isInfoEnabled())
log.info("Ambiguous match found for " + fieldName + ". Both " + columnMatch + " and " + columnMatch2 + " match.", new SQLWarning());
return (Field<T>) columnMatch;
}
use of org.jooq.Field in project jOOQ by jOOQ.
the class Function method toSQLArguments1.
final void toSQLArguments1(Context<?> ctx, QueryPartList<QueryPart> args) {
if (distinct) {
ctx.keyword("distinct");
// [#2883] PostgreSQL can use the DISTINCT keyword with formal row value expressions.
if (ctx.family() == POSTGRES && args.size() > 1) {
ctx.sql('(');
} else {
ctx.sql(' ');
}
}
if (!args.isEmpty()) {
if (filter == null || HSQLDB == ctx.family() || POSTGRES_9_4.precedes(ctx.dialect())) {
ctx.visit(args);
} else {
QueryPartList<Field<?>> expressions = new QueryPartList<Field<?>>();
for (QueryPart argument : args) expressions.add(DSL.when(filter, argument == ASTERISK ? one() : argument));
ctx.visit(expressions);
}
}
if (distinct)
if (ctx.family() == POSTGRES && args.size() > 1)
ctx.sql(')');
if (ignoreNulls) {
ctx.sql(' ').keyword("ignore nulls");
} else if (respectNulls) {
ctx.sql(' ').keyword("respect nulls");
}
}
use of org.jooq.Field in project jOOQ by jOOQ.
the class JoinTable method onKey.
@SuppressWarnings({ "unchecked", "rawtypes" })
private final JoinTable onKey(ForeignKey<?, ?> key, Table<?> fk, Table<?> pk) {
JoinTable result = this;
TableField<?, ?>[] references = key.getFieldsArray();
TableField<?, ?>[] referenced = key.getKey().getFieldsArray();
for (int i = 0; i < references.length; i++) {
Field f1 = fk.field(references[i]);
Field f2 = pk.field(referenced[i]);
// [#2870] TODO: If lhs or rhs are aliased tables, extract the appropriate fields from them
result.and(f1.equal(f2));
}
return result;
}
use of org.jooq.Field in project jOOQ by jOOQ.
the class ConstraintImpl method accept.
@Override
public final void accept(Context<?> ctx) {
if (ctx.data(DATA_CONSTRAINT_REFERENCE) != null) {
if (name == null)
throw new DataAccessException("Cannot ALTER or DROP CONSTRAINT without name");
ctx.visit(name);
} else {
boolean qualify = ctx.qualify();
if (name != null)
ctx.keyword("constraint").sql(' ').visit(name).formatIndentStart().formatSeparator();
if (unique != null) {
ctx.keyword("unique").sql(" (").qualify(false).visit(new QueryPartList<Field<?>>(unique)).qualify(qualify).sql(')');
} else if (primaryKey != null) {
ctx.keyword("primary key").sql(" (").qualify(false).visit(new QueryPartList<Field<?>>(primaryKey)).qualify(qualify).sql(')');
} else if (foreignKey != null) {
ctx.keyword("foreign key").sql(" (").qualify(false).visit(new QueryPartList<Field<?>>(foreignKey)).qualify(qualify).sql(')').formatSeparator().keyword("references").sql(' ').visit(referencesTable).sql(" (").qualify(false).visit(new QueryPartList<Field<?>>(references)).qualify(qualify).sql(')');
if (onDelete != null)
ctx.sql(' ').keyword("on delete").sql(' ').keyword(onDelete.sql);
if (onUpdate != null)
ctx.sql(' ').keyword("on update").sql(' ').keyword(onUpdate.sql);
} else if (check != null) {
ctx.keyword("check").sql(" (").qualify(false).visit(check).qualify(qualify).sql(')');
}
ctx.formatIndentEnd();
}
}
Aggregations