use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class KotlinDataClassGenerator method getKotlinFiles.
@Override
public List<KotlinFile> getKotlinFiles() {
FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
// $NON-NLS-1$
progressCallback.startTask(getString("Progress.8", table.toString()));
CommentGenerator commentGenerator = context.getCommentGenerator();
FullyQualifiedKotlinType type = new FullyQualifiedKotlinType(introspectedTable.getKotlinRecordType());
KotlinFile kf = new KotlinFile(type.getShortNameWithoutTypeArguments());
kf.setPackage(type.getPackageName());
KotlinType dataClass = KotlinType.newClass(type.getShortNameWithoutTypeArguments()).withModifier(KotlinModifier.DATA).build();
kf.addNamedItem(dataClass);
commentGenerator.addFileComment(kf);
commentGenerator.addModelClassComment(dataClass, introspectedTable);
List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();
for (IntrospectedColumn introspectedColumn : introspectedColumns) {
FullyQualifiedKotlinType kotlinType = JavaToKotlinTypeConverter.convert(introspectedColumn.getFullyQualifiedJavaType());
KotlinProperty kp = KotlinProperty.newVar(introspectedColumn.getJavaProperty()).withDataType(// $NON-NLS-1$
kotlinType.getShortNameWithTypeArguments() + "?").withInitializationString(// $NON-NLS-1$
"null").build();
dataClass.addConstructorProperty(kp);
kf.addImports(kotlinType.getImportList());
}
if (context.getPlugins().kotlinDataClassGenerated(kf, dataClass, introspectedTable)) {
return listOf(kf);
} else {
return Collections.emptyList();
}
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class FragmentGenerator method getAnnotatedConstructorArgs.
public MethodParts getAnnotatedConstructorArgs() {
MethodParts.Builder builder = new MethodParts.Builder();
// $NON-NLS-1$
builder.withImport(new FullyQualifiedJavaType("org.apache.ibatis.type.JdbcType"));
// $NON-NLS-1$
builder.withImport(new FullyQualifiedJavaType("org.apache.ibatis.annotations.ConstructorArgs"));
// $NON-NLS-1$
builder.withImport(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Arg"));
// $NON-NLS-1$
builder.withAnnotation("@ConstructorArgs({");
StringBuilder sb = new StringBuilder();
Set<FullyQualifiedJavaType> imports = new HashSet<>();
Iterator<IntrospectedColumn> iterPk = introspectedTable.getPrimaryKeyColumns().iterator();
Iterator<IntrospectedColumn> iterNonPk = introspectedTable.getNonPrimaryKeyColumns().iterator();
while (iterPk.hasNext()) {
IntrospectedColumn introspectedColumn = iterPk.next();
sb.setLength(0);
javaIndent(sb, 1);
sb.append(getArgAnnotation(imports, introspectedColumn, true));
if (iterPk.hasNext() || iterNonPk.hasNext()) {
sb.append(',');
}
builder.withAnnotation(sb.toString());
}
while (iterNonPk.hasNext()) {
IntrospectedColumn introspectedColumn = iterNonPk.next();
sb.setLength(0);
javaIndent(sb, 1);
sb.append(getArgAnnotation(imports, introspectedColumn, false));
if (iterNonPk.hasNext()) {
sb.append(',');
}
builder.withAnnotation(sb.toString());
}
// $NON-NLS-1$
builder.withAnnotation("})").withImports(imports);
return builder.build();
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class FragmentGenerator method getSetLines.
private List<String> getSetLines(List<IntrospectedColumn> columnList, String firstLinePrefix, String subsequentLinePrefix, boolean terminate, String fragment) {
List<String> lines = new ArrayList<>();
List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(columnList);
Iterator<IntrospectedColumn> iter = columns.iterator();
boolean first = true;
while (iter.hasNext()) {
IntrospectedColumn column = iter.next();
String fieldName = AbstractMethodGenerator.calculateFieldName(tableFieldName, column);
String methodName = JavaBeansUtil.getGetterMethodName(column.getJavaProperty(), column.getFullyQualifiedJavaType());
String start;
if (first) {
start = firstLinePrefix;
first = false;
} else {
start = subsequentLinePrefix;
}
String line = start + // $NON-NLS-1$
".set(" + fieldName + // $NON-NLS-1$
")." + fragment + // $NON-NLS-1$
"(row::" + methodName + // $NON-NLS-1$
")";
if (terminate && !iter.hasNext()) {
// $NON-NLS-1$
line += ";";
}
lines.add(line);
}
return lines;
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class InsertMultipleMethodGenerator method generateMethodAndImports.
@Override
public MethodAndImports generateMethodAndImports() {
if (!Utils.generateMultipleRowInsert(introspectedTable)) {
return null;
}
Set<FullyQualifiedJavaType> imports = new HashSet<>();
// $NON-NLS-1$
imports.add(new FullyQualifiedJavaType("org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils"));
imports.add(recordType);
// $NON-NLS-1$
Method method = new Method("insertMultiple");
method.setDefault(true);
context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, imports);
method.setReturnType(FullyQualifiedJavaType.getIntInstance());
// $NON-NLS-1$
FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType("java.util.Collection");
parameterType.addTypeArgument(recordType);
imports.add(parameterType);
// $NON-NLS-1$
method.addParameter(new Parameter(parameterType, "records"));
String methodName;
if (Utils.canRetrieveMultiRowGeneratedKeys(introspectedTable)) {
methodName = "MyBatis3Utils.insertMultipleWithGeneratedKeys";
} else {
methodName = "MyBatis3Utils.insertMultiple";
}
method.addBodyLine(// $NON-NLS-1$ //$NON-NLS-2$
"return " + methodName + "(this::insertMultiple, records, " + // $NON-NLS-1$
tableFieldName + // $NON-NLS-1$
", c ->");
List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
boolean first = true;
for (IntrospectedColumn column : columns) {
String fieldName = calculateFieldName(column);
if (first) {
method.addBodyLine(// $NON-NLS-1$
" c.map(" + fieldName + ").toProperty(\"" + // $NON-NLS-1$
column.getJavaProperty() + // $NON-NLS-1$
"\")");
first = false;
} else {
method.addBodyLine(// $NON-NLS-1$
" .map(" + fieldName + ").toProperty(\"" + // $NON-NLS-1$
column.getJavaProperty() + // $NON-NLS-1$
"\")");
}
}
// $NON-NLS-1$
method.addBodyLine(");");
return MethodAndImports.withMethod(method).withImports(imports).build();
}
use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.
the class DatabaseIntrospector method calculateIntrospectedTables.
private List<IntrospectedTable> calculateIntrospectedTables(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
boolean delimitIdentifiers = tc.isDelimitIdentifiers() || stringContainsSpace(tc.getCatalog()) || stringContainsSpace(tc.getSchema()) || stringContainsSpace(tc.getTableName());
List<IntrospectedTable> answer = new ArrayList<>();
for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
ActualTableName atn = entry.getKey();
// we only use the returned catalog and schema if something was
// actually
// specified on the table configuration. If something was returned
// from the DB for these fields, but nothing was specified on the
// table
// configuration, then some sort of DB default is being returned
// and we don't want that in our SQL
FullyQualifiedTable table = new FullyQualifiedTable(stringHasValue(tc.getCatalog()) ? atn.getCatalog() : null, stringHasValue(tc.getSchema()) ? atn.getSchema() : null, atn.getTableName(), tc.getDomainObjectName(), tc.getAlias(), isTrue(tc.getProperty(PropertyRegistry.TABLE_IGNORE_QUALIFIERS_AT_RUNTIME)), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_CATALOG), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_SCHEMA), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_TABLE_NAME), delimitIdentifiers, tc.getDomainObjectRenamingRule(), context);
IntrospectedTable introspectedTable = ObjectFactory.createIntrospectedTable(tc, table, context);
for (IntrospectedColumn introspectedColumn : entry.getValue()) {
introspectedTable.addColumn(introspectedColumn);
}
calculatePrimaryKey(table, introspectedTable);
enhanceIntrospectedTable(introspectedTable);
answer.add(introspectedTable);
}
return answer;
}
Aggregations