use of org.jooq.Record 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.Record in project jOOQ by jOOQ.
the class JavaGenerator method generateRecord0.
private final void generateRecord0(Definition tableOrUdt, JavaWriter out) {
final UniqueKeyDefinition key = (tableOrUdt instanceof TableDefinition) ? ((TableDefinition) tableOrUdt).getPrimaryKey() : null;
final String className = getStrategy().getJavaClassName(tableOrUdt, Mode.RECORD);
final String tableIdentifier = out.ref(getStrategy().getFullJavaIdentifier(tableOrUdt), 2);
final List<String> interfaces = out.ref(getStrategy().getJavaClassImplements(tableOrUdt, Mode.RECORD));
final List<? extends TypedElementDefinition<?>> columns = getTypedElements(tableOrUdt);
printPackage(out, tableOrUdt, Mode.RECORD);
if (tableOrUdt instanceof TableDefinition)
generateRecordClassJavadoc((TableDefinition) tableOrUdt, out);
else
generateUDTRecordClassJavadoc((UDTDefinition) tableOrUdt, out);
printClassAnnotations(out, tableOrUdt.getSchema());
if (tableOrUdt instanceof TableDefinition)
printTableJPAAnnotation(out, (TableDefinition) tableOrUdt);
Class<?> baseClass;
if (tableOrUdt instanceof UDTDefinition)
baseClass = UDTRecordImpl.class;
else if (generateRelations() && key != null)
baseClass = UpdatableRecordImpl.class;
else
baseClass = TableRecordImpl.class;
int degree = columns.size();
String rowType = null;
String rowTypeRecord = null;
// [#3130] Invalid UDTs may have a degree of 0
if (degree > 0 && degree <= Constants.MAX_ROW_DEGREE) {
rowType = refRowType(out, columns);
if (scala)
rowTypeRecord = out.ref(Record.class.getName() + degree) + "[" + rowType + "]";
else
rowTypeRecord = out.ref(Record.class.getName() + degree) + "<" + rowType + ">";
interfaces.add(rowTypeRecord);
}
if (generateInterfaces()) {
interfaces.add(out.ref(getStrategy().getFullJavaClassName(tableOrUdt, Mode.INTERFACE)));
}
if (scala)
out.println("class %s extends %s[%s](%s)[[before= with ][separator= with ][%s]] {", className, baseClass, className, tableIdentifier, interfaces);
else
out.println("public class %s extends %s<%s>[[before= implements ][%s]] {", className, baseClass, className, interfaces);
out.printSerial();
for (int i = 0; i < degree; i++) {
TypedElementDefinition<?> column = columns.get(i);
if (tableOrUdt instanceof TableDefinition) {
generateRecordSetter(column, i, out);
generateRecordGetter(column, i, out);
} else {
generateUDTRecordSetter(column, i, out);
generateUDTRecordGetter(column, i, out);
}
}
if (generateRelations() && key != null) {
int keyDegree = key.getKeyColumns().size();
if (keyDegree <= Constants.MAX_ROW_DEGREE) {
String keyType = refRowType(out, key.getKeyColumns());
out.tab(1).header("Primary key information");
if (scala) {
out.tab(1).println("override def key : %s[%s] = {", out.ref(Record.class.getName() + keyDegree), keyType);
out.tab(2).println("return super.key.asInstanceOf[ %s[%s] ]", out.ref(Record.class.getName() + keyDegree), keyType);
out.tab(1).println("}");
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s<%s> key() {", out.ref(Record.class.getName() + keyDegree), keyType);
out.tab(2).println("return (%s) super.key();", out.ref(Record.class.getName() + keyDegree));
out.tab(1).println("}");
}
}
}
if (tableOrUdt instanceof UDTDefinition) {
// [#799] Oracle UDT's can have member procedures
for (RoutineDefinition routine : ((UDTDefinition) tableOrUdt).getRoutines()) {
// Instance methods ship with a SELF parameter at the first position
// [#1584] Static methods don't have that
boolean instance = routine.getInParameters().size() > 0 && routine.getInParameters().get(0).getInputName().toUpperCase().equals("SELF");
try {
if (!routine.isSQLUsable()) {
// Instance execute() convenience method
printConvenienceMethodProcedure(out, routine, instance);
} else {
// Instance execute() convenience method
if (!routine.isAggregate()) {
printConvenienceMethodFunction(out, routine, instance);
}
}
} catch (Exception e) {
log.error("Error while generating routine " + routine, e);
}
}
}
// [#3130] Invalid UDTs may have a degree of 0
if (degree > 0 && degree <= Constants.MAX_ROW_DEGREE) {
out.tab(1).header("Record%s type implementation", degree);
// fieldsRow()
if (scala) {
out.println();
out.tab(1).println("override def fieldsRow : %s[%s] = {", out.ref(Row.class.getName() + degree), rowType);
out.tab(2).println("super.fieldsRow.asInstanceOf[ %s[%s] ]", out.ref(Row.class.getName() + degree), rowType);
out.tab(1).println("}");
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s<%s> fieldsRow() {", out.ref(Row.class.getName() + degree), rowType);
out.tab(2).println("return (%s) super.fieldsRow();", out.ref(Row.class.getName() + degree));
out.tab(1).println("}");
}
// valuesRow()
if (scala) {
out.println();
out.tab(1).println("override def valuesRow : %s[%s] = {", out.ref(Row.class.getName() + degree), rowType);
out.tab(2).println("super.valuesRow.asInstanceOf[ %s[%s] ]", out.ref(Row.class.getName() + degree), rowType);
out.tab(1).println("}");
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s<%s> valuesRow() {", out.ref(Row.class.getName() + degree), rowType);
out.tab(2).println("return (%s) super.valuesRow();", out.ref(Row.class.getName() + degree));
out.tab(1).println("}");
}
// field[N]()
for (int i = 1; i <= degree; i++) {
TypedElementDefinition<?> column = columns.get(i - 1);
final String colType = out.ref(getJavaType(column.getType()));
final String colIdentifier = out.ref(getStrategy().getFullJavaIdentifier(column), colRefSegments(column));
if (scala) {
out.tab(1).println("override def field%s : %s[%s] = %s", i, Field.class, colType, colIdentifier);
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s<%s> field%s() {", Field.class, colType, i);
out.tab(2).println("return %s;", colIdentifier);
out.tab(1).println("}");
}
}
// value[N]()
for (int i = 1; i <= degree; i++) {
TypedElementDefinition<?> column = columns.get(i - 1);
final String colType = out.ref(getJavaType(column.getType()));
final String colGetter = getStrategy().getJavaGetterName(column, Mode.RECORD);
if (scala) {
out.tab(1).println("override def value%s : %s = %s", i, colType, colGetter);
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s value%s() {", colType, i);
out.tab(2).println("return %s();", colGetter);
out.tab(1).println("}");
}
}
// value[N](T[N])
for (int i = 1; i <= degree; i++) {
TypedElementDefinition<?> column = columns.get(i - 1);
final String colType = out.ref(getJavaType(column.getType()));
final String colSetter = getStrategy().getJavaSetterName(column, Mode.RECORD);
if (scala) {
out.println();
out.tab(1).println("override def value%s(value : %s) : %s = {", i, colType, className);
out.tab(2).println("%s(value)", colSetter);
out.tab(2).println("this");
out.tab(1).println("}");
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s value%s(%s value) {", className, i, varargsIfArray(colType));
out.tab(2).println("%s(value);", colSetter);
out.tab(2).println("return this;");
out.tab(1).println("}");
}
}
List<String> arguments = new ArrayList<String>();
List<String> calls = new ArrayList<String>();
for (int i = 1; i <= degree; i++) {
TypedElementDefinition<?> column = columns.get(i - 1);
final String colType = out.ref(getJavaType(column.getType()));
if (scala) {
arguments.add("value" + i + " : " + colType);
calls.add("this.value" + i + "(value" + i + ")");
} else {
arguments.add(colType + " value" + i);
calls.add("value" + i + "(value" + i + ");");
}
}
if (scala) {
out.println();
out.tab(1).println("override def values([[%s]]) : %s = {", arguments, className);
for (String call : calls) out.tab(2).println(call);
out.tab(2).println("this");
out.tab(1).println("}");
} else {
out.tab(1).overrideInherit();
out.tab(1).println("public %s values([[%s]]) {", className, arguments);
for (String call : calls) out.tab(2).println(call);
out.tab(2).println("return this;");
out.tab(1).println("}");
}
}
if (generateInterfaces()) {
printFromAndInto(out, tableOrUdt);
}
if (scala) {
} else {
out.tab(1).header("Constructors");
out.tab(1).javadoc("Create a detached %s", className);
out.tab(1).println("public %s() {", className);
out.tab(2).println("super(%s);", tableIdentifier);
out.tab(1).println("}");
}
// [#3176] Avoid generating constructors for tables with more than 255 columns (Java's method argument limit)
if (degree > 0 && degree < 256) {
List<String> arguments = new ArrayList<String>();
for (int i = 0; i < degree; i++) {
final TypedElementDefinition<?> column = columns.get(i);
final String columnMember = getStrategy().getJavaMemberName(column, Mode.DEFAULT);
final String type = out.ref(getJavaType(column.getType()));
if (scala)
arguments.add(columnMember + " : " + type);
else
arguments.add(type + " " + columnMember);
}
out.tab(1).javadoc("Create a detached, initialised %s", className);
if (scala) {
out.tab(1).println("def this([[%s]]) = {", arguments);
out.tab(2).println("this()", tableIdentifier);
} else {
out.tab(1).println("public %s([[%s]]) {", className, arguments);
out.tab(2).println("super(%s);", tableIdentifier);
}
out.println();
for (int i = 0; i < degree; i++) {
final TypedElementDefinition<?> column = columns.get(i);
final String columnMember = getStrategy().getJavaMemberName(column, Mode.DEFAULT);
if (scala)
out.tab(2).println("set(%s, %s)", i, columnMember);
else
out.tab(2).println("set(%s, %s);", i, columnMember);
}
out.tab(1).println("}");
}
if (tableOrUdt instanceof TableDefinition)
generateRecordClassFooter((TableDefinition) tableOrUdt, out);
else
generateUDTRecordClassFooter((UDTDefinition) tableOrUdt, out);
out.println("}");
}
use of org.jooq.Record in project jOOQ by jOOQ.
the class CUBRIDDatabase method getEnums0.
@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
List<EnumDefinition> result = new ArrayList<EnumDefinition>();
for (TableDefinition tableDefinition : getTables(getSchemata().get(0))) {
for (Record record : create().fetch("SHOW COLUMNS FROM {0} WHERE TYPE LIKE 'ENUM(%)'", field(name(tableDefinition.getInputName())))) {
String table = tableDefinition.getInputName();
String column = record.get("Field", String.class);
String columnType = record.get("Type", String.class);
String name = table + "_" + column;
ColumnDefinition columnDefinition = tableDefinition.getColumn(column);
// are explicitly forced to another type
if (getConfiguredForcedType(columnDefinition) == null) {
DefaultEnumDefinition definition = new DefaultEnumDefinition(getSchemata().get(0), name, "");
for (String string : columnType.replaceAll("ENUM\\(|\\)", "").split(",")) {
definition.addLiteral(string.trim().replaceAll("'", ""));
}
result.add(definition);
}
}
}
return result;
}
use of org.jooq.Record in project jOOQ by jOOQ.
the class CUBRIDDatabase method loadUniqueKeys.
@Override
protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
for (Record record : fetchKeys(DB_INDEX.IS_PRIMARY_KEY.isTrue())) {
String key = record.get("constraint_name", String.class);
String tableName = record.get(DB_CLASS.CLASS_NAME);
String columnName = record.get(DB_INDEX_KEY.KEY_ATTR_NAME);
TableDefinition table = getTable(getSchemata().get(0), tableName);
if (table != null) {
relations.addPrimaryKey(key, table.getColumn(columnName));
}
}
}
use of org.jooq.Record in project jOOQ by jOOQ.
the class CUBRIDDatabase method getSequences0.
@Override
protected List<SequenceDefinition> getSequences0() throws SQLException {
List<SequenceDefinition> result = new ArrayList<SequenceDefinition>();
for (Record record : create().select(DB_SERIAL.NAME, DB_SERIAL.MAX_VAL).from(DB_SERIAL).fetch()) {
BigInteger value = defaultIfNull(record.get(DB_SERIAL.MAX_VAL, BigInteger.class), BigInteger.valueOf(Long.MAX_VALUE));
DataTypeDefinition type = getDataTypeForMAX_VAL(getSchemata().get(0), value);
result.add(new DefaultSequenceDefinition(getSchemata().get(0), record.get(DB_SERIAL.NAME), type));
}
return result;
}
Aggregations