use of org.jooq.Configuration in project jOOQ by jOOQ.
the class JavaGenerator method generateDao.
protected void generateDao(TableDefinition table, JavaWriter out) {
UniqueKeyDefinition key = table.getPrimaryKey();
if (key == null) {
log.info("Skipping DAO generation", out.file().getName());
return;
}
final String className = getStrategy().getJavaClassName(table, Mode.DAO);
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(table, Mode.DAO));
final String tableRecord = out.ref(getStrategy().getFullJavaClassName(table, Mode.RECORD));
final String daoImpl = out.ref(DAOImpl.class);
final String tableIdentifier = out.ref(getStrategy().getFullJavaIdentifier(table), 2);
String tType = (scala ? "Unit" : "Void");
String pType = out.ref(getStrategy().getFullJavaClassName(table, Mode.POJO));
List<ColumnDefinition> keyColumns = key.getKeyColumns();
if (keyColumns.size() == 1) {
tType = getJavaType(keyColumns.get(0).getType());
} else if (keyColumns.size() <= Constants.MAX_ROW_DEGREE) {
String generics = "";
String separator = "";
for (ColumnDefinition column : keyColumns) {
generics += separator + out.ref(getJavaType(column.getType()));
separator = ", ";
}
if (scala)
tType = Record.class.getName() + keyColumns.size() + "[" + generics + "]";
else
tType = Record.class.getName() + keyColumns.size() + "<" + generics + ">";
} else {
tType = Record.class.getName();
}
tType = out.ref(tType);
printPackage(out, table, Mode.DAO);
generateDaoClassJavadoc(table, out);
printClassAnnotations(out, table.getSchema());
if (generateSpringAnnotations())
out.println("@%s", out.ref("org.springframework.stereotype.Repository"));
if (scala)
out.println("class %s(configuration : %s) extends %s[%s, %s, %s](%s, classOf[%s], configuration)[[before= with ][separator= with ][%s]] {", className, Configuration.class, daoImpl, tableRecord, pType, tType, tableIdentifier, pType, interfaces);
else
out.println("public class %s extends %s<%s, %s, %s>[[before= implements ][%s]] {", className, daoImpl, tableRecord, pType, tType, interfaces);
// Default constructor
// -------------------
out.tab(1).javadoc("Create a new %s without any configuration", className);
if (scala) {
out.tab(1).println("def this() = {");
out.tab(2).println("this(null)");
out.tab(1).println("}");
} else {
out.tab(1).println("public %s() {", className);
out.tab(2).println("super(%s, %s.class);", tableIdentifier, pType);
out.tab(1).println("}");
}
if (scala) {
} else {
out.tab(1).javadoc("Create a new %s with an attached configuration", className);
if (generateSpringAnnotations())
out.tab(1).println("@%s", out.ref("org.springframework.beans.factory.annotation.Autowired"));
out.tab(1).println("public %s(%s configuration) {", className, Configuration.class);
out.tab(2).println("super(%s, %s.class, configuration);", tableIdentifier, pType);
out.tab(1).println("}");
}
// -------------------------------
if (scala) {
out.println();
out.tab(1).println("override protected def getId(o : %s) : %s = {", pType, tType);
} else {
out.tab(1).overrideInherit();
out.tab(1).println("protected %s getId(%s object) {", tType, pType);
}
if (keyColumns.size() == 1) {
if (scala)
out.tab(2).println("o.%s", getStrategy().getJavaGetterName(keyColumns.get(0), Mode.POJO));
else
out.tab(2).println("return object.%s();", getStrategy().getJavaGetterName(keyColumns.get(0), Mode.POJO));
} else // [#2574] This should be replaced by a call to a method on the target table's Key type
{
String params = "";
String separator = "";
for (ColumnDefinition column : keyColumns) {
if (scala)
params += separator + "o." + getStrategy().getJavaGetterName(column, Mode.POJO);
else
params += separator + "object." + getStrategy().getJavaGetterName(column, Mode.POJO) + "()";
separator = ", ";
}
if (scala)
out.tab(2).println("compositeKeyRecord(%s)", params);
else
out.tab(2).println("return compositeKeyRecord(%s);", params);
}
out.tab(1).println("}");
for (ColumnDefinition column : table.getColumns()) {
final String colName = column.getOutputName();
final String colClass = getStrategy().getJavaClassName(column);
final String colType = out.ref(getJavaType(column.getType()));
final String colIdentifier = out.ref(getStrategy().getFullJavaIdentifier(column), colRefSegments(column));
// fetchBy[Column]([T]...)
// -----------------------
out.tab(1).javadoc("Fetch records that have <code>%s IN (values)</code>", colName);
if (scala) {
out.tab(1).println("def fetchBy%s(values : %s*) : %s[%s] = {", colClass, colType, List.class, pType);
out.tab(2).println("fetch(%s, values:_*)", colIdentifier);
out.tab(1).println("}");
} else {
out.tab(1).println("public %s<%s> fetchBy%s(%s... values) {", List.class, pType, colClass, colType);
out.tab(2).println("return fetch(%s, values);", colIdentifier);
out.tab(1).println("}");
}
// -----------------------
ukLoop: for (UniqueKeyDefinition uk : column.getUniqueKeys()) {
// If column is part of a single-column unique key...
if (uk.getKeyColumns().size() == 1 && uk.getKeyColumns().get(0).equals(column)) {
out.tab(1).javadoc("Fetch a unique record that has <code>%s = value</code>", colName);
if (scala) {
out.tab(1).println("def fetchOneBy%s(value : %s) : %s = {", colClass, colType, pType);
out.tab(2).println("fetchOne(%s, value)", colIdentifier);
out.tab(1).println("}");
} else {
out.tab(1).println("public %s fetchOneBy%s(%s value) {", pType, colClass, colType);
out.tab(2).println("return fetchOne(%s, value);", colIdentifier);
out.tab(1).println("}");
}
break ukLoop;
}
}
}
generateDaoClassFooter(table, out);
out.println("}");
}
use of org.jooq.Configuration in project jOOQ by jOOQ.
the class JavaGenerator method printConvenienceMethodTableValuedFunction.
protected void printConvenienceMethodTableValuedFunction(JavaWriter out, TableDefinition function, String javaMethodName) {
// [#281] - Java can't handle more than 255 method parameters
if (function.getParameters().size() > 254) {
log.warn("Too many parameters", "Function " + function + " has more than 254 in parameters. Skipping generation of convenience method.");
return;
}
final String recordClassName = out.ref(getStrategy().getFullJavaClassName(function, Mode.RECORD));
// [#3456] Local variables should not collide with actual function arguments
final String configurationArgument = disambiguateJavaMemberName(function.getParameters(), "configuration");
out.tab(1).javadoc("Call <code>%s</code>.", function.getQualifiedOutputName());
if (scala)
out.tab(1).print("def %s(%s : %s", javaMethodName, configurationArgument, Configuration.class);
else
out.tab(1).print("public static %s<%s> %s(%s %s", Result.class, recordClassName, javaMethodName, Configuration.class, configurationArgument);
if (!function.getParameters().isEmpty())
out.print(", ");
printParameterDeclarations(out, function, false);
if (scala) {
out.println(") : %s[%s] = {", Result.class, recordClassName);
out.tab(2).print("%s.using(%s).selectFrom(%s.call(", DSL.class, configurationArgument, // [#5765] To prevent name clashes, this identifier is not imported
getStrategy().getFullJavaIdentifier(function));
} else {
out.println(") {");
out.tab(2).print("return %s.using(%s).selectFrom(%s.call(", DSL.class, configurationArgument, // [#5765] To prevent name clashes, this identifier is not imported
getStrategy().getFullJavaIdentifier(function));
}
String separator = "";
for (ParameterDefinition parameter : function.getParameters()) {
out.print(separator);
out.print("%s", getStrategy().getJavaMemberName(parameter));
separator = ", ";
}
out.print(")).fetch()");
if (scala)
out.println();
else
out.println(";");
out.tab(1).println("}");
}
use of org.jooq.Configuration in project jOOQ by jOOQ.
the class DefaultBinding method set.
@Override
public void set(BindingSetSQLOutputContext<U> ctx) throws SQLException {
Configuration configuration = ctx.configuration();
T value = converter.to(ctx.value());
if (value == null) {
ctx.output().writeObject(null);
} else if (type == Blob.class) {
ctx.output().writeBlob((Blob) value);
} else if (type == Boolean.class) {
ctx.output().writeBoolean((Boolean) value);
} else if (type == BigInteger.class) {
ctx.output().writeBigDecimal(new BigDecimal((BigInteger) value));
} else if (type == BigDecimal.class) {
ctx.output().writeBigDecimal((BigDecimal) value);
} else if (type == Byte.class) {
ctx.output().writeByte((Byte) value);
} else if (type == byte[].class) {
// Use reflection to avoid dependency on OJDBC
if (isLob) {
Blob blob = null;
try {
blob = on("oracle.sql.BLOB").call("createTemporary", on(ctx.output()).call("getSTRUCT").call("getJavaSqlConnection").get(), false, on("oracle.sql.BLOB").get("DURATION_SESSION")).get();
blob.setBytes(1, (byte[]) value);
ctx.output().writeBlob(blob);
} finally {
DefaultExecuteContext.register(blob);
}
} else {
ctx.output().writeBytes((byte[]) value);
}
} else if (type == Clob.class) {
ctx.output().writeClob((Clob) value);
} else if (type == Date.class) {
Date date = (Date) value;
ctx.output().writeDate(date);
} else if (type == Double.class) {
ctx.output().writeDouble((Double) value);
} else if (type == Float.class) {
ctx.output().writeFloat((Float) value);
} else if (type == Integer.class) {
ctx.output().writeInt((Integer) value);
} else if (type == Long.class) {
ctx.output().writeLong((Long) value);
} else if (type == Short.class) {
ctx.output().writeShort((Short) value);
} else if (type == String.class) {
// Use reflection to avoid dependency on OJDBC
if (isLob) {
Clob clob = null;
try {
clob = on("oracle.sql.CLOB").call("createTemporary", on(ctx.output()).call("getSTRUCT").call("getJavaSqlConnection").get(), false, on("oracle.sql.CLOB").get("DURATION_SESSION")).get();
clob.setString(1, (String) value);
ctx.output().writeClob(clob);
} finally {
DefaultExecuteContext.register(clob);
}
} else {
ctx.output().writeString((String) value);
}
} else if (type == Time.class) {
ctx.output().writeTime((Time) value);
} else if (type == Timestamp.class) {
ctx.output().writeTimestamp((Timestamp) value);
} else if (type == YearToMonth.class) {
ctx.output().writeString(value.toString());
} else if (type == DayToSecond.class) {
ctx.output().writeString(value.toString());
} else // }
if (UNumber.class.isAssignableFrom(type)) {
ctx.output().writeString(value.toString());
} else if (type == UUID.class) {
ctx.output().writeString(value.toString());
} else if (EnumType.class.isAssignableFrom(type)) {
ctx.output().writeString(((EnumType) value).getLiteral());
} else if (UDTRecord.class.isAssignableFrom(type)) {
ctx.output().writeObject((UDTRecord<?>) value);
} else {
throw new UnsupportedOperationException("Type " + type + " is not supported");
}
}
use of org.jooq.Configuration in project keywhiz by square.
the class ClientDAO method sawClient.
public void sawClient(Client client) {
Instant now = Instant.now();
Instant lastSeen = Optional.ofNullable(client.getLastSeen()).map(ls -> Instant.ofEpochSecond(ls.toEpochSecond())).orElse(null);
// this way we can have less granularity on lastSeen and save db writes
if (lastSeen == null || now.isAfter(lastSeen.plus(lastSeenThreshold, SECONDS))) {
dslContext.transaction(configuration -> {
Param<Long> val = DSL.val(now.getEpochSecond(), CLIENTS.LASTSEEN);
DSL.using(configuration).update(CLIENTS).set(CLIENTS.LASTSEEN, DSL.when(CLIENTS.LASTSEEN.isNull(), val).otherwise(DSL.greatest(CLIENTS.LASTSEEN, val))).where(CLIENTS.ID.eq(client.getId())).execute();
});
}
}
use of org.jooq.Configuration in project jOOQ by jOOQ.
the class JavaGenerator method generateDao.
protected void generateDao(TableDefinition table, JavaWriter out) {
UniqueKeyDefinition key = table.getPrimaryKey();
if (key == null) {
log.info("Skipping DAO generation", out.file().getName());
return;
}
final String className = getStrategy().getJavaClassName(table, Mode.DAO);
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(table, Mode.DAO));
final String tableRecord = out.ref(getStrategy().getFullJavaClassName(table, Mode.RECORD));
final String daoImpl = out.ref(DAOImpl.class);
final String tableIdentifier = out.ref(getStrategy().getFullJavaIdentifier(table), 2);
String tType = (scala || kotlin ? "Unit" : "Void");
String pType = out.ref(getStrategy().getFullJavaClassName(table, Mode.POJO));
List<ColumnDefinition> keyColumns = key.getKeyColumns();
if (keyColumns.size() == 1) {
tType = getJavaType(keyColumns.get(0).getType(resolver(out)), out, Mode.POJO);
} else if (keyColumns.size() <= Constants.MAX_ROW_DEGREE) {
StringBuilder generics = new StringBuilder();
forEach(keyColumns, "", ", ", (column, separator) -> {
generics.append(out.ref(getJavaType(column.getType(resolver(out)), out)));
if (kotlin)
generics.append("?");
generics.append(separator);
});
if (scala)
tType = Record.class.getName() + keyColumns.size() + "[" + generics + "]";
else
tType = Record.class.getName() + keyColumns.size() + "<" + generics + ">";
} else {
tType = Record.class.getName();
}
tType = out.ref(tType);
printPackage(out, table, Mode.DAO);
generateDaoClassJavadoc(table, out);
printClassAnnotations(out, table, Mode.DAO);
if (generateSpringAnnotations())
out.println("@%s", out.ref("org.springframework.stereotype.Repository"));
if (scala)
out.println("%sclass %s(configuration: %s) extends %s[%s, %s, %s](%s, classOf[%s], configuration)[[before= with ][separator= with ][%s]] {", visibility(), className, Configuration.class, daoImpl, tableRecord, pType, tType, tableIdentifier, pType, interfaces);
else if (kotlin)
out.println("%sopen class %s(configuration: %s?) : %s<%s, %s, %s>(%s, %s::class.java, configuration)[[before=, ][%s]] {", visibility(), className, Configuration.class, daoImpl, tableRecord, pType, tType, tableIdentifier, pType, interfaces);
else
out.println("%sclass %s extends %s<%s, %s, %s>[[before= implements ][%s]] {", visibility(), className, daoImpl, tableRecord, pType, tType, interfaces);
// Default constructor
// -------------------
out.javadoc("Create a new %s without any configuration", className);
if (scala) {
out.println("%sdef this() = this(null)", visibility());
} else if (kotlin) {
out.println("%sconstructor(): this(null)", visibility());
} else {
out.println("%s%s() {", visibility(), className);
out.println("super(%s, %s.class);", tableIdentifier, pType);
out.println("}");
}
if (!scala && !kotlin) {
out.javadoc("Create a new %s with an attached configuration", className);
printDaoConstructorAnnotations(table, out);
out.println("%s%s(%s configuration) {", visibility(), className, Configuration.class);
out.println("super(%s, %s.class, configuration);", tableIdentifier, pType);
out.println("}");
}
// -------------------------------
if (scala) {
out.println();
out.print("%soverride def getId(o: %s): %s = ", visibilityPublic(), pType, tType);
} else if (kotlin) {
out.println();
out.print("%soverride fun getId(o: %s): %s? = ", visibilityPublic(), pType, tType);
} else {
out.overrideInherit();
printNonnullAnnotation(out);
out.println("%s%s getId(%s object) {", visibilityPublic(), tType, pType);
}
if (keyColumns.size() == 1) {
if (scala)
out.println("o.%s", getStrategy().getJavaGetterName(keyColumns.get(0), Mode.POJO));
else if (kotlin)
out.println("o.%s", getStrategy().getJavaMemberName(keyColumns.get(0), Mode.POJO));
else
out.println("return object.%s();", generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(keyColumns.get(0), Mode.POJO) : getStrategy().getJavaGetterName(keyColumns.get(0), Mode.POJO));
} else // [#2574] This should be replaced by a call to a method on the target table's Key type
{
StringBuilder params = new StringBuilder();
forEach(keyColumns, "", ", ", (column, separator) -> {
if (scala)
params.append("o.").append(getStrategy().getJavaGetterName(column, Mode.POJO));
else if (kotlin)
params.append("o.").append(getStrategy().getJavaMemberName(column, Mode.POJO));
else
params.append("object.").append(generatePojosAsJavaRecordClasses() ? getStrategy().getJavaMemberName(column, Mode.POJO) : getStrategy().getJavaGetterName(column, Mode.POJO)).append("()");
params.append(separator);
});
if (scala || kotlin)
out.println("compositeKeyRecord(%s)", params.toString());
else
out.println("return compositeKeyRecord(%s);", params.toString());
}
if (scala || kotlin) {
} else
out.println("}");
List<Definition> embeddablesAndUnreplacedColumns = embeddablesAndUnreplacedColumns(table);
for (Definition column : embeddablesAndUnreplacedColumns) {
final String colName = column.getOutputName();
final String colClass = getStrategy().getJavaClassName(column);
final String colMemberUC = StringUtils.toUC(getStrategy().getJavaMemberName(column), getStrategy().getTargetLocale());
final String colTypeFull = getJavaType(column, out, Mode.POJO);
final String colTypeRecord = out.ref(getJavaType(column, out, Mode.RECORD));
final String colType = out.ref(colTypeFull);
final String colIdentifier = out.ref(getStrategy().getFullJavaIdentifier(column), colRefSegments(column));
// -----------------------
if (!printDeprecationIfUnknownType(out, colTypeFull))
out.javadoc("Fetch records that have <code>%s BETWEEN lowerInclusive AND upperInclusive</code>", colName);
if (scala) {
if (column instanceof EmbeddableDefinition)
out.println("%sdef fetchRangeOf%s(lowerInclusive: %s, upperInclusive: %s): %s[%s] = fetchRange(%s, new %s(lowerInclusive), new %s(upperInclusive))", visibility(), colMemberUC, colType, colType, List.class, pType, colIdentifier, colTypeRecord, colTypeRecord);
else
out.println("%sdef fetchRangeOf%s(lowerInclusive: %s, upperInclusive: %s): %s[%s] = fetchRange(%s, lowerInclusive, upperInclusive)", visibility(), colClass, colType, colType, List.class, pType, colIdentifier);
} else if (kotlin) {
if (column instanceof EmbeddableDefinition)
out.println("%sfun fetchRangeOf%s(lowerInclusive: %s?, upperInclusive: %s?): %s<%s> = fetchRange(%s, if (lowerInclusive != null) %s(lowerInclusive) else null, if (upperInclusive != null) %s(upperInclusive) else null)", visibility(), colMemberUC, colType, colType, out.ref(KLIST), pType, colIdentifier, colTypeRecord, colTypeRecord);
else
out.println("%sfun fetchRangeOf%s(lowerInclusive: %s?, upperInclusive: %s?): %s<%s> = fetchRange(%s, lowerInclusive, upperInclusive)", visibility(), colClass, colType, colType, out.ref(KLIST), pType, colIdentifier);
} else {
printNonnullAnnotation(out);
if (column instanceof EmbeddableDefinition) {
out.println("%s%s<%s> fetchRangeOf%s(%s lowerInclusive, %s upperInclusive) {", visibility(), List.class, pType, colMemberUC, colType, colType);
out.println("return fetchRange(%s, new %s(lowerInclusive), new %s(upperInclusive));", colIdentifier, colTypeRecord, colTypeRecord);
} else {
out.println("%s%s<%s> fetchRangeOf%s(%s lowerInclusive, %s upperInclusive) {", visibility(), List.class, pType, colClass, colType, colType);
out.println("return fetchRange(%s, lowerInclusive, upperInclusive);", colIdentifier);
}
out.println("}");
}
// -----------------------
if (!printDeprecationIfUnknownType(out, colTypeFull))
out.javadoc("Fetch records that have <code>%s IN (values)</code>", colName);
if (scala) {
if (column instanceof EmbeddableDefinition)
out.println("%sdef fetchBy%s(values: %s*): %s[%s] = fetch(%s, values.map(v => new %s(v)).toArray:_*)", visibility(), colMemberUC, colType, List.class, pType, colIdentifier, colTypeRecord);
else
out.println("%sdef fetchBy%s(values: %s*): %s[%s] = fetch(%s, values:_*)", visibility(), colClass, colType, List.class, pType, colIdentifier);
} else if (kotlin) {
String toTypedArray = PRIMITIVE_WRAPPERS.contains(colTypeFull) ? ".toTypedArray()" : "";
if (column instanceof EmbeddableDefinition)
out.println("%sfun fetchBy%s(vararg values: %s): %s<%s> = fetch(%s, values.map { %s(it) })", visibility(), colMemberUC, colType, out.ref(KLIST), pType, colIdentifier, colTypeRecord);
else
out.println("%sfun fetchBy%s(vararg values: %s): %s<%s> = fetch(%s, *values%s)", visibility(), colClass, colType, out.ref(KLIST), pType, colIdentifier, toTypedArray);
} else {
printNonnullAnnotation(out);
if (column instanceof EmbeddableDefinition) {
out.println("%s%s<%s> fetchBy%s(%s... values) {", visibility(), List.class, pType, colMemberUC, colType);
out.println("%s[] records = new %s[values.length];", colTypeRecord, colTypeRecord);
out.println();
out.println("for (int i = 0; i < values.length; i++)");
out.tab(1).println("records[i] = new %s(values[i]);", colTypeRecord);
out.println();
out.println("return fetch(%s, records);", colIdentifier);
} else {
out.println("%s%s<%s> fetchBy%s(%s... values) {", visibility(), List.class, pType, colClass, colType);
out.println("return fetch(%s, values);", colIdentifier);
}
out.println("}");
}
// -----------------------
ukLoop: if (column instanceof ColumnDefinition) {
for (UniqueKeyDefinition uk : ((ColumnDefinition) column).getKeys()) {
// If column is part of a single-column unique key...
if (uk.getKeyColumns().size() == 1 && uk.getKeyColumns().get(0).equals(column)) {
if (!printDeprecationIfUnknownType(out, colTypeFull))
out.javadoc("Fetch a unique record that has <code>%s = value</code>", colName);
if (scala) {
out.println("%sdef fetchOneBy%s(value: %s): %s = fetchOne(%s, value)", visibility(), colClass, colType, pType, colIdentifier);
} else if (kotlin) {
out.println("%sfun fetchOneBy%s(value: %s): %s? = fetchOne(%s, value)", visibility(), colClass, colType, pType, colIdentifier);
} else {
printNullableAnnotation(out);
out.println("%s%s fetchOneBy%s(%s value) {", visibility(), pType, colClass, colType);
out.println("return fetchOne(%s, value);", colIdentifier);
out.println("}");
if (!printDeprecationIfUnknownType(out, colTypeFull))
out.javadoc("Fetch a unique record that has <code>%s = value</code>", colName);
printNonnullAnnotation(out);
out.println("%s%s<%s> fetchOptionalBy%s(%s value) {", visibility(), Optional.class, pType, colClass, colType);
out.println("return fetchOptional(%s, value);", colIdentifier);
out.println("}");
}
break ukLoop;
}
}
}
}
generateDaoClassFooter(table, out);
out.println("}");
}
Aggregations