org.mybatis.generator.internal.ObjectFactory Java Examples

The following examples show how to use org.mybatis.generator.internal.ObjectFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractDAOElementGenerator.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
public DAOMethodNameCalculator getDAOMethodNameCalculator() {
    if (dAOMethodNameCalculator == null) {
        String type = context.getJavaClientGeneratorConfiguration()
                .getProperty(PropertyRegistry.DAO_METHOD_NAME_CALCULATOR);
        if (stringHasValue(type)) {
            if ("extended".equalsIgnoreCase(type)) { //$NON-NLS-1$
                type = ExtendedDAOMethodNameCalculator.class.getName();
            } else if ("default".equalsIgnoreCase(type)) { //$NON-NLS-1$
                type = DefaultDAOMethodNameCalculator.class.getName();
            }
        } else {
            type = DefaultDAOMethodNameCalculator.class.getName();
        }

        try {
            dAOMethodNameCalculator = (DAOMethodNameCalculator) ObjectFactory
                    .createInternalObject(type);
        } catch (Exception e) {
            dAOMethodNameCalculator = new DefaultDAOMethodNameCalculator();
            warnings.add(getString(
                    "Warning.17", type, e.getMessage())); //$NON-NLS-1$
        }
    }

    return dAOMethodNameCalculator;
}
 
Example #2
Source File: RootClassInfo.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
private RootClassInfo(String className, List<String> warnings) {
    super();
    this.className = className;
    this.warnings = warnings;

    if (className == null) {
        return;
    }

    try {
        Class<?> clazz = ObjectFactory.externalClassForName(className);
        BeanInfo bi = Introspector.getBeanInfo(clazz);
        propertyDescriptors = bi.getPropertyDescriptors();
    } catch (Exception e) {
        propertyDescriptors = null;
        warnings.add(getString("Warning.20", className)); //$NON-NLS-1$
    }
}
 
Example #3
Source File: SqlScriptRunner.java    From mybatis-generator-plus with Apache License 2.0 6 votes vote down vote up
private BufferedReader getScriptReader() throws MojoExecutionException, IOException {
    BufferedReader answer;
    
    if (sourceFile.startsWith("classpath:")) {
        String resource = sourceFile.substring("classpath:".length());
        URL url = ObjectFactory.getResource(resource);
        InputStream is = url.openStream();
        if (is == null) {
            throw new MojoExecutionException("SQL script file does not exist: " + resource);
        }
        answer = new BufferedReader(new InputStreamReader(is));
    } else {
        File file = new File(sourceFile);
        if (!file.exists()) {
            throw new MojoExecutionException("SQL script file does not exist");
        }
        answer = new BufferedReader(new FileReader(file));
    }
    
    return answer;
}
 
Example #4
Source File: RootClassInfo.java    From mapper-generator-javafx with Apache License 2.0 6 votes vote down vote up
private RootClassInfo(String className, List<String> warnings) {
    super();
    this.className = className;
    this.warnings = warnings;

    if (className == null) {
        return;
    }

    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(className);
    String nameWithoutGenerics = fqjt.getFullyQualifiedNameWithoutTypeParameters();
    if (!nameWithoutGenerics.equals(className)) {
        genericMode = true;
    }

    try {
        Class<?> clazz = ObjectFactory.externalClassForName(nameWithoutGenerics);
        BeanInfo bi = Introspector.getBeanInfo(clazz);
        propertyDescriptors = bi.getPropertyDescriptors();
    } catch (Exception e) {
        propertyDescriptors = null;
        warnings.add(getString("Warning.20", className));
    }
}
 
Example #5
Source File: AbstractDAOElementGenerator.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
public DAOMethodNameCalculator getDAOMethodNameCalculator() {
    if (dAOMethodNameCalculator == null) {
        String type = context.getJavaClientGeneratorConfiguration()
                .getProperty(PropertyRegistry.DAO_METHOD_NAME_CALCULATOR);
        if (stringHasValue(type)) {
            if ("extended".equalsIgnoreCase(type)) { //$NON-NLS-1$
                type = ExtendedDAOMethodNameCalculator.class.getName();
            } else if ("default".equalsIgnoreCase(type)) { //$NON-NLS-1$
                type = DefaultDAOMethodNameCalculator.class.getName();
            }
        } else {
            type = DefaultDAOMethodNameCalculator.class.getName();
        }

        try {
            dAOMethodNameCalculator = (DAOMethodNameCalculator) ObjectFactory
                    .createInternalObject(type);
        } catch (Exception e) {
            dAOMethodNameCalculator = new DefaultDAOMethodNameCalculator();
            warnings.add(getString(
                    "Warning.17", type, e.getMessage())); //$NON-NLS-1$
        }
    }

    return dAOMethodNameCalculator;
}
 
Example #6
Source File: RootClassInfo.java    From mybatis-generator-core-fix with Apache License 2.0 6 votes vote down vote up
private RootClassInfo(String className, List<String> warnings) {
    super();
    this.className = className;
    this.warnings = warnings;

    if (className == null) {
        return;
    }
    
    FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(className);
    String nameWithoutGenerics = fqjt.getFullyQualifiedNameWithoutTypeParameters();
    if (!nameWithoutGenerics.equals(className)) {
        genericMode = true;
    }

    try {
        Class<?> clazz = ObjectFactory.externalClassForName(nameWithoutGenerics);
        BeanInfo bi = Introspector.getBeanInfo(clazz);
        propertyDescriptors = bi.getPropertyDescriptors();
    } catch (Exception e) {
        propertyDescriptors = null;
        warnings.add(getString("Warning.20", className)); //$NON-NLS-1$
    }
}
 
Example #7
Source File: Context.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the xml formatter.
 *
 * @return the xml formatter
 */
public XmlFormatter getXmlFormatter() {
    if (xmlFormatter == null) {
        xmlFormatter = ObjectFactory.createXmlFormatter(this);
    }

    return xmlFormatter;
}
 
Example #8
Source File: Context.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
public void generateFiles(ProgressCallback callback, List<GeneratedJavaFile> generatedJavaFiles, List<GeneratedXmlFile> generatedXmlFiles, List<String> warnings) throws InterruptedException {

		pluginAggregator = new PluginAggregator();
		for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
			Plugin plugin = ObjectFactory.createPlugin(this, pluginConfiguration);
			if (plugin.validate(warnings)) {
				pluginAggregator.addPlugin(plugin);
			}
			else {
				warnings.add(getString("Warning.24", //$NON-NLS-1$
						pluginConfiguration.getConfigurationType(), id));
			}
		}

		if (introspectedTables != null) {
			for (IntrospectedTable introspectedTable : introspectedTables) {
				callback.checkCancel();

				introspectedTable.initialize();
				introspectedTable.calculateGenerators(warnings, callback);
				generatedJavaFiles.addAll(introspectedTable.getGeneratedJavaFiles());
				generatedXmlFiles.addAll(introspectedTable.getGeneratedXmlFiles());

				generatedJavaFiles.addAll(pluginAggregator.contextGenerateAdditionalJavaFiles(introspectedTable));
				generatedXmlFiles.addAll(pluginAggregator.contextGenerateAdditionalXmlFiles(introspectedTable));
			}
		}

		generatedJavaFiles.addAll(pluginAggregator.contextGenerateAdditionalJavaFiles());
		generatedXmlFiles.addAll(pluginAggregator.contextGenerateAdditionalXmlFiles());


		//每个表自定义包路径
		setJavaFilesCustomTargetPackage(generatedJavaFiles);
		setXmlFilesCustomTargetPackage(generatedJavaFiles);
	}
 
Example #9
Source File: Context.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
public XmlFormatter getXmlFormatter() {
	if (xmlFormatter == null) {
		xmlFormatter = ObjectFactory.createXmlFormatter(this);
	}

	return xmlFormatter;
}
 
Example #10
Source File: Context.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
public JavaFormatter getJavaFormatter() {
	if (javaFormatter == null) {
		javaFormatter = ObjectFactory.createJavaFormatter(this);
	}

	return javaFormatter;
}
 
Example #11
Source File: Context.java    From mybatis-generator-plus with Apache License 2.0 5 votes vote down vote up
public CommentGenerator getCommentGenerator() {
	if (commentGenerator == null) {
		commentGenerator = ObjectFactory.createCommentGenerator(this);
	}

	return commentGenerator;
}
 
Example #12
Source File: Context.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the java formatter.
 *
 * @return the java formatter
 */
public JavaFormatter getJavaFormatter() {
    if (javaFormatter == null) {
        javaFormatter = ObjectFactory.createJavaFormatter(this);
    }

    return javaFormatter;
}
 
Example #13
Source File: Context.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
public CommentGenerator getCommentGenerator() {
    if (commentGenerator == null) {
        commentGenerator = ObjectFactory.createCommentGenerator(this);
    }

    return commentGenerator;
}
 
Example #14
Source File: Context.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
public JavaFormatter getJavaFormatter() {
    if (javaFormatter == null) {
        javaFormatter = ObjectFactory.createJavaFormatter(this);
    }

    return javaFormatter;
}
 
Example #15
Source File: Context.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
public XmlFormatter getXmlFormatter() {
    if (xmlFormatter == null) {
        xmlFormatter = ObjectFactory.createXmlFormatter(this);
    }

    return xmlFormatter;
}
 
Example #16
Source File: Context.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
private Connection getConnection() throws SQLException {
    ConnectionFactory connectionFactory;
    if (jdbcConnectionConfiguration != null) {
        connectionFactory = new JDBCConnectionFactory(jdbcConnectionConfiguration);
    } else {
        connectionFactory = ObjectFactory.createConnectionFactory(this);
    }

    return connectionFactory.getConnection();
}
 
Example #17
Source File: Context.java    From mybatis-generator-core-fix with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the comment generator.
 *
 * @return the comment generator
 */
public CommentGenerator getCommentGenerator() {
    if (commentGenerator == null) {
        commentGenerator = ObjectFactory.createCommentGenerator(this);
    }

    return commentGenerator;
}
 
Example #18
Source File: Context.java    From mapper-generator-javafx with Apache License 2.0 5 votes vote down vote up
public void generateFiles(ProgressCallback callback,
        List<GeneratedJavaFile> generatedJavaFiles,
        List<GeneratedXmlFile> generatedXmlFiles, List<String> warnings)
        throws InterruptedException {

    pluginAggregator = new PluginAggregator();
    for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
        Plugin plugin = ObjectFactory.createPlugin(this, pluginConfiguration);
        if (plugin.validate(warnings)) {
            pluginAggregator.addPlugin(plugin);
        } else {
            warnings.add(getString("Warning.24",
                    pluginConfiguration.getConfigurationType(), id));
        }
    }

    if (introspectedTables != null) {
        for (IntrospectedTable introspectedTable : introspectedTables) {
            callback.checkCancel();

            introspectedTable.initialize();
            introspectedTable.calculateGenerators(warnings, callback);
            generatedJavaFiles.addAll(introspectedTable.getGeneratedJavaFiles());
            generatedXmlFiles.addAll(introspectedTable.getGeneratedXmlFiles());

            generatedJavaFiles.addAll(pluginAggregator.contextGenerateAdditionalJavaFiles(introspectedTable));
            generatedXmlFiles.addAll(pluginAggregator.contextGenerateAdditionalXmlFiles(introspectedTable));
        }
    }

    generatedJavaFiles.addAll(pluginAggregator.contextGenerateAdditionalJavaFiles());
    generatedXmlFiles.addAll(pluginAggregator.contextGenerateAdditionalXmlFiles());
}
 
Example #19
Source File: DatabaseIntrospector.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
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<IntrospectedTable>();

    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, context);

        IntrospectedTable introspectedTable = ObjectFactory
                .createIntrospectedTable(tc, table, context);

        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            introspectedTable.addColumn(introspectedColumn);
        }

        calculatePrimaryKey(table, introspectedTable);

        answer.add(introspectedTable);
    }

    return answer;
}
 
Example #20
Source File: Context.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
/**
 * This method does a simple validate, it makes sure that all required fields have been filled in. It does not do
 * any more complex operations such as validating that database tables exist or validating that named columns exist
 *
 * @param errors
 *            the errors
 */
public void validate(List<String> errors) {
    if (!stringHasValue(id)) {
        errors.add(getString("ValidationError.16"));
    }

    if (jdbcConnectionConfiguration == null && connectionFactoryConfiguration == null) {
        // must specify one
        errors.add(getString("ValidationError.10", id));
    } else if (jdbcConnectionConfiguration != null && connectionFactoryConfiguration != null) {
        // must not specify both
        errors.add(getString("ValidationError.10", id));
    } else if (jdbcConnectionConfiguration != null) {
        jdbcConnectionConfiguration.validate(errors);
    } else {
        connectionFactoryConfiguration.validate(errors);
    }

    if (javaModelGeneratorConfiguration == null) {
        errors.add(getString("ValidationError.8", id));
    } else {
        javaModelGeneratorConfiguration.validate(errors, id);
    }

    if (javaClientGeneratorConfiguration != null) {
        javaClientGeneratorConfiguration.validate(errors, id);
    }

    IntrospectedTable it = null;
    try {
        it = ObjectFactory.createIntrospectedTableForValidation(this);
    } catch (Exception e) {
        errors.add(getString("ValidationError.25", id));
    }

    if (it != null && it.requiresXMLGenerator()) {
        if (sqlMapGeneratorConfiguration == null) {
            errors.add(getString("ValidationError.9", id));
        } else {
            sqlMapGeneratorConfiguration.validate(errors, id);
        }
    }

    if (tableConfigurations.isEmpty()) {
        errors.add(getString("ValidationError.3", id));
    } else {
        for (int i = 0; i < tableConfigurations.size(); i++) {
            TableConfiguration tc = tableConfigurations.get(i);

            tc.validate(errors, i);
        }
    }

    for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
        pluginConfiguration.validate(errors, id);
    }
}
 
Example #21
Source File: Context.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
/**
 * Introspect tables based on the configuration specified in the
 * constructor. This method is long running.
 * 
 * @param callback
 *            a progress callback if progress information is desired, or
 *            <code>null</code>
 * @param warnings
 *            any warning generated from this method will be added to the
 *            List. Warnings are always Strings.
 * @param fullyQualifiedTableNames
 *            a set of table names to generate. The elements of the set must
 *            be Strings that exactly match what's specified in the
 *            configuration. For example, if table name = "foo" and schema =
 *            "bar", then the fully qualified table name is "foo.bar". If
 *            the Set is null or empty, then all tables in the configuration
 *            will be used for code generation.
 * 
 * @throws SQLException
 *             if some error arises while introspecting the specified
 *             database tables.
 * @throws InterruptedException
 *             if the progress callback reports a cancel
 */
public void introspectTables(ProgressCallback callback, List<String> warnings, Set<String> fullyQualifiedTableNames) throws SQLException, InterruptedException {

	introspectedTables = new ArrayList<IntrospectedTable>();
	JavaTypeResolver javaTypeResolver = ObjectFactory.createJavaTypeResolver(this, warnings);

	Connection connection = null;

	try {
		callback.startTask(getString("Progress.0")); //$NON-NLS-1$
		connection = getConnection();

		DatabaseIntrospector databaseIntrospector = new DatabaseIntrospector(this, connection.getMetaData(), javaTypeResolver, warnings);

		for (TableConfiguration tc : tableConfigurations) {
			String tableName = composeFullyQualifiedTableName(tc.getCatalog(), tc.getSchema(), tc.getTableName(), '.');

			if (fullyQualifiedTableNames != null && fullyQualifiedTableNames.size() > 0) {
				if (!fullyQualifiedTableNames.contains(tableName)) {
					continue;
				}
			}

			if (!tc.areAnyStatementsEnabled()) {
				warnings.add(getString("Warning.0", tableName)); //$NON-NLS-1$
				continue;
			}

			callback.startTask(getString("Progress.1", tableName)); //$NON-NLS-1$
			List<IntrospectedTable> tables = databaseIntrospector.introspectTables(tc);

			if (tables != null) {
				introspectedTables.addAll(tables);
			}

			callback.checkCancel();
		}
	}
	finally {
		closeConnection(connection);
	}
}
 
Example #22
Source File: Context.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
/**
 * Introspect tables based on the configuration specified in the
 * constructor. This method is long running.
 * 
 * @param callback
 *            a progress callback if progress information is desired, or
 *            <code>null</code>
 * @param warnings
 *            any warning generated from this method will be added to the
 *            List. Warnings are always Strings.
 * @param fullyQualifiedTableNames
 *            a set of table names to generate. The elements of the set must
 *            be Strings that exactly match what's specified in the
 *            configuration. For example, if table name = "foo" and schema =
 *            "bar", then the fully qualified table name is "foo.bar". If
 *            the Set is null or empty, then all tables in the configuration
 *            will be used for code generation.
 * 
 * @throws SQLException
 *             if some error arises while introspecting the specified
 *             database tables.
 * @throws InterruptedException
 *             if the progress callback reports a cancel
 */
public void introspectTables(ProgressCallback callback,
        List<String> warnings, Set<String> fullyQualifiedTableNames)
        throws SQLException, InterruptedException {

    introspectedTables = new ArrayList<>();
    JavaTypeResolver javaTypeResolver = ObjectFactory.createJavaTypeResolver(this, warnings);

    Connection connection = null;

    try {
        callback.startTask(getString("Progress.0"));
        connection = getConnection();

        DatabaseIntrospector databaseIntrospector = new DatabaseIntrospector(
                this, connection.getMetaData(), javaTypeResolver, warnings);

        for (TableConfiguration tc : tableConfigurations) {
            String tableName = composeFullyQualifiedTableName(tc.getCatalog(), tc.getSchema(), tc.getTableName(), '.');

            if (fullyQualifiedTableNames != null
                    && !fullyQualifiedTableNames.isEmpty()
                    && !fullyQualifiedTableNames.contains(tableName)) {
                continue;
            }

            if (!tc.areAnyStatementsEnabled()) {
                warnings.add(getString("Warning.0", tableName));
                continue;
            }

            callback.startTask(getString("Progress.1", tableName));
            List<IntrospectedTable> tables = databaseIntrospector.introspectTables(tc);

            if (tables != null) {
                introspectedTables.addAll(tables);
            }

            callback.checkCancel();
        }
    } finally {
        closeConnection(connection);
    }
}
 
Example #23
Source File: Context.java    From mybatis-generator-plus with Apache License 2.0 4 votes vote down vote up
/**
 * This method does a simple validate, it makes sure that all required
 * fields have been filled in. It does not do any more complex operations
 * such as validating that database tables exist or validating that named
 * columns exist
 */
public void validate(List<String> errors) {
	if (!stringHasValue(id)) {
		errors.add(getString("ValidationError.16")); //$NON-NLS-1$
	}

	if (jdbcConnectionConfiguration == null) {
		errors.add(getString("ValidationError.10", id)); //$NON-NLS-1$
	}
	else {
		jdbcConnectionConfiguration.validate(errors);
	}

	if (javaModelGeneratorConfiguration == null) {
		errors.add(getString("ValidationError.8", id)); //$NON-NLS-1$
	}
	else {
		javaModelGeneratorConfiguration.validate(errors, id);
	}

	if (javaClientGeneratorConfiguration != null) {
		javaClientGeneratorConfiguration.validate(errors, id);
	}

	IntrospectedTable it = null;
	try {
		it = ObjectFactory.createIntrospectedTableForValidation(this);
	}
	catch (Exception e) {
		errors.add(getString("ValidationError.25", id)); //$NON-NLS-1$
	}

	if (it != null && it.requiresXMLGenerator()) {
		if (sqlMapGeneratorConfiguration == null) {
			errors.add(getString("ValidationError.9", id)); //$NON-NLS-1$
		}
		else {
			sqlMapGeneratorConfiguration.validate(errors, id);
		}
	}

	if (tableConfigurations.size() == 0) {
		errors.add(getString("ValidationError.3", id)); //$NON-NLS-1$
	}
	else {
		for (int i = 0; i < tableConfigurations.size(); i++) {
			TableConfiguration tc = tableConfigurations.get(i);

			tc.validate(errors, i);
		}
	}

	for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
		pluginConfiguration.validate(errors, id);
	}
}
 
Example #24
Source File: LombokPlugin.java    From mybatis-generator-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * 添加注解
 * @param topLevelClass
 * @param introspectedTable
 * @param modelType
 */
private void addAnnotations(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, EnumModelType modelType) {
    for (String annotation : this.annotations) {
        // @Data
        if (annotation.startsWith("@Data")) {
            this.addAnnotation(topLevelClass, annotation);
            if (topLevelClass.getSuperClass() != null) {
                this.addAnnotation(topLevelClass, "@EqualsAndHashCode(callSuper = true)");
                this.addAnnotation(topLevelClass, "@ToString(callSuper = true)");
            }
        } else if (annotation.startsWith("@Builder")) {
            // TODO 配合IncrementsPlugin,以后删除
            boolean checkIncrementsPlugin = true;
            if (!this.suportSuperBuilderForIdea) {
                switch (modelType) {
                    case MODEL_PRIMARY_KEY:
                        checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelPrimaryKeyBuilderClassGenerated(topLevelClass, introspectedTable);
                        break;
                    case MODEL_BASE_RECORD:
                        checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelBaseRecordBuilderClassGenerated(topLevelClass, introspectedTable);
                        break;
                    case MODEL_RECORD_WITH_BLOBS:
                        checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, introspectedTable);
                        break;
                }
            }

            if (checkIncrementsPlugin) {
                // 有子类或者父类
                int count = 0;
                if (introspectedTable.getRules().generatePrimaryKeyClass()) {
                    count++;
                }
                if (introspectedTable.getRules().generateBaseRecordClass()) {
                    count++;
                }
                if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
                    count++;
                }

                if (topLevelClass.getSuperClass() != null || count >= 2) {
                    if (this.suportSuperBuilderForIdea) {
                        // TODO 兼容老版本
                        PluginConfiguration configuration = new PluginConfiguration();
                        configuration.setConfigurationType(ModelBuilderPlugin.class.getTypeName());
                        ModelBuilderPlugin modelBuilderPlugin = (ModelBuilderPlugin) ObjectFactory.createPlugin(this.context, configuration);
                        switch (modelType) {
                            case MODEL_PRIMARY_KEY:
                                modelBuilderPlugin.modelPrimaryKeyBuilderClassGenerated(topLevelClass, introspectedTable);
                                break;
                            case MODEL_BASE_RECORD:
                                modelBuilderPlugin.modelBaseRecordBuilderClassGenerated(topLevelClass, introspectedTable);
                                break;
                            case MODEL_RECORD_WITH_BLOBS:
                                modelBuilderPlugin.modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, introspectedTable);
                                break;
                        }
                    } else {
                        this.addAnnotation(topLevelClass, "@SuperBuilder");
                    }
                } else {
                    this.addAnnotation(topLevelClass, annotation);
                }
            }
        } else {
            this.addAnnotation(topLevelClass, annotation);
        }
    }
}
 
Example #25
Source File: DatabaseIntrospector.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
/**
 * Calculate introspected tables.
 *
 * @param tc
 *            the tc
 * @param columns
 *            the columns
 * @return the list
 */
private List<IntrospectedTable> calculateIntrospectedTables(
        TableConfiguration tc,
        Map<ActualTableName, List<IntrospectedColumn>> columns) throws SQLException {
    boolean delimitIdentifiers = tc.isDelimitIdentifiers()
            || stringContainsSpace(tc.getCatalog())
            || stringContainsSpace(tc.getSchema())
            || stringContainsSpace(tc.getTableName());

    List<IntrospectedTable> answer = new ArrayList<IntrospectedTable>();

    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, context);

        //设置数据库表的备注信息
        //start
        Statement stmt = this.databaseMetaData.getConnection().createStatement();
        ResultSet rs = stmt.executeQuery(new StringBuilder().append("SHOW TABLE STATUS LIKE '").append(atn.getTableName()).append("'").toString());
        while (rs.next())
            table.setRemark(rs.getString("COMMENT"));

        closeResultSet(rs);
        stmt.close();
        //end

        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;
}
 
Example #26
Source File: Context.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
/**
 * Generate files.
 *
 * @param callback
 *            the callback
 * @param generatedJavaFiles
 *            the generated java files
 * @param generatedXmlFiles
 *            the generated xml files
 * @param warnings
 *            the warnings
 * @throws InterruptedException
 *             the interrupted exception
 */
public void generateFiles(ProgressCallback callback,
        List<GeneratedJavaFile> generatedJavaFiles,
        List<GeneratedXmlFile> generatedXmlFiles, List<String> warnings)
        throws InterruptedException {

    pluginAggregator = new PluginAggregator();
    for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
        Plugin plugin = ObjectFactory.createPlugin(this,
                pluginConfiguration);
        if (plugin.validate(warnings)) {
            pluginAggregator.addPlugin(plugin);
        } else {
            warnings.add(getString("Warning.24", //$NON-NLS-1$
                    pluginConfiguration.getConfigurationType(), id));
        }
    }

    if (introspectedTables != null) {
        for (IntrospectedTable introspectedTable : introspectedTables) {
            callback.checkCancel();

            introspectedTable.initialize();
            introspectedTable.calculateGenerators(warnings, callback);
            generatedJavaFiles.addAll(introspectedTable
                    .getGeneratedJavaFiles());
            generatedXmlFiles.addAll(introspectedTable
                    .getGeneratedXmlFiles());

            generatedJavaFiles.addAll(pluginAggregator
                    .contextGenerateAdditionalJavaFiles(introspectedTable));
            generatedXmlFiles.addAll(pluginAggregator
                    .contextGenerateAdditionalXmlFiles(introspectedTable));
        }
    }

    generatedJavaFiles.addAll(pluginAggregator
            .contextGenerateAdditionalJavaFiles());
    generatedXmlFiles.addAll(pluginAggregator
            .contextGenerateAdditionalXmlFiles());
}
 
Example #27
Source File: Context.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
/**
 * Introspect tables based on the configuration specified in the
 * constructor. This method is long running.
 * 
 * @param callback
 *            a progress callback if progress information is desired, or
 *            <code>null</code>
 * @param warnings
 *            any warning generated from this method will be added to the
 *            List. Warnings are always Strings.
 * @param fullyQualifiedTableNames
 *            a set of table names to generate. The elements of the set must
 *            be Strings that exactly match what's specified in the
 *            configuration. For example, if table name = "foo" and schema =
 *            "bar", then the fully qualified table name is "foo.bar". If
 *            the Set is null or empty, then all tables in the configuration
 *            will be used for code generation.
 * 
 * @throws SQLException
 *             if some error arises while introspecting the specified
 *             database tables.
 * @throws InterruptedException
 *             if the progress callback reports a cancel
 */
public void introspectTables(ProgressCallback callback,
        List<String> warnings, Set<String> fullyQualifiedTableNames)
        throws SQLException, InterruptedException {

    introspectedTables = new ArrayList<IntrospectedTable>();
    JavaTypeResolver javaTypeResolver = ObjectFactory
            .createJavaTypeResolver(this, warnings);

    Connection connection = null;

    try {
        callback.startTask(getString("Progress.0")); //$NON-NLS-1$
        connection = getConnection();

        DatabaseIntrospector databaseIntrospector = new DatabaseIntrospector(
                this, connection.getMetaData(), javaTypeResolver, warnings);

        for (TableConfiguration tc : tableConfigurations) {
            String tableName = composeFullyQualifiedTableName(tc.getCatalog(), tc
                            .getSchema(), tc.getTableName(), '.');

            if (fullyQualifiedTableNames != null
                    && fullyQualifiedTableNames.size() > 0) {
                if (!fullyQualifiedTableNames.contains(tableName)) {
                    continue;
                }
            }

            if (!tc.areAnyStatementsEnabled()) {
                warnings.add(getString("Warning.0", tableName)); //$NON-NLS-1$
                continue;
            }

            callback.startTask(getString("Progress.1", tableName)); //$NON-NLS-1$
            List<IntrospectedTable> tables = databaseIntrospector
                    .introspectTables(tc);

            if (tables != null) {
                introspectedTables.addAll(tables);
            }

            callback.checkCancel();
        }
    } finally {
        closeConnection(connection);
    }
}
 
Example #28
Source File: DatabaseIntrospector.java    From mapper-generator-javafx with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example #29
Source File: Context.java    From mybatis-generator-core-fix with Apache License 2.0 4 votes vote down vote up
/**
 * This method does a simple validate, it makes sure that all required fields have been filled in. It does not do
 * any more complex operations such as validating that database tables exist or validating that named columns exist
 *
 * @param errors
 *            the errors
 */
public void validate(List<String> errors) {
    if (!stringHasValue(id)) {
        errors.add(getString("ValidationError.16")); //$NON-NLS-1$
    }

    if (jdbcConnectionConfiguration == null) {
        errors.add(getString("ValidationError.10", id)); //$NON-NLS-1$
    } else {
        jdbcConnectionConfiguration.validate(errors);
    }

    if (javaModelGeneratorConfiguration == null) {
        errors.add(getString("ValidationError.8", id)); //$NON-NLS-1$
    } else {
        javaModelGeneratorConfiguration.validate(errors, id);
    }

    if (javaClientGeneratorConfiguration != null) {
        javaClientGeneratorConfiguration.validate(errors, id);
    }

    IntrospectedTable it = null;
    try {
        it = ObjectFactory.createIntrospectedTableForValidation(this);
    } catch (Exception e) {
        errors.add(getString("ValidationError.25", id)); //$NON-NLS-1$
    }
    
    if (it != null && it.requiresXMLGenerator()) {
        if (sqlMapGeneratorConfiguration == null) {
            errors.add(getString("ValidationError.9", id)); //$NON-NLS-1$
        } else {
            sqlMapGeneratorConfiguration.validate(errors, id);
        }
    }

    if (tableConfigurations.size() == 0) {
        errors.add(getString("ValidationError.3", id)); //$NON-NLS-1$
    } else {
        for (int i = 0; i < tableConfigurations.size(); i++) {
            TableConfiguration tc = tableConfigurations.get(i);

            tc.validate(errors, i);
        }
    }

    for (PluginConfiguration pluginConfiguration : pluginConfigurations) {
        pluginConfiguration.validate(errors, id);
    }
}