Search in sources :

Example 1 with IntrospectedTable

use of org.mybatis.generator.api.IntrospectedTable in project generator by mybatis.

the class ObjectFactory method createIntrospectedTable.

/**
     * Creates a new Object object.
     *
     * @param tableConfiguration
     *            the table configuration
     * @param table
     *            the table
     * @param context
     *            the context
     * @return the introspected table
     */
public static IntrospectedTable createIntrospectedTable(TableConfiguration tableConfiguration, FullyQualifiedTable table, Context context) {
    IntrospectedTable answer = createIntrospectedTableForValidation(context);
    answer.setFullyQualifiedTable(table);
    answer.setTableConfiguration(tableConfiguration);
    return answer;
}
Also used : IntrospectedTable(org.mybatis.generator.api.IntrospectedTable)

Example 2 with IntrospectedTable

use of org.mybatis.generator.api.IntrospectedTable in project generator by mybatis.

the class Context method introspectTables.

/**
     * 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 {
        //$NON-NLS-1$
        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.size() > 0 && !fullyQualifiedTableNames.contains(tableName)) {
                continue;
            }
            if (!tc.areAnyStatementsEnabled()) {
                //$NON-NLS-1$
                warnings.add(getString("Warning.0", tableName));
                continue;
            }
            //$NON-NLS-1$
            callback.startTask(getString("Progress.1", tableName));
            List<IntrospectedTable> tables = databaseIntrospector.introspectTables(tc);
            if (tables != null) {
                introspectedTables.addAll(tables);
            }
            callback.checkCancel();
        }
    } finally {
        closeConnection(connection);
    }
}
Also used : JavaTypeResolver(org.mybatis.generator.api.JavaTypeResolver) DatabaseIntrospector(org.mybatis.generator.internal.db.DatabaseIntrospector) Connection(java.sql.Connection) IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString)

Example 3 with IntrospectedTable

use of org.mybatis.generator.api.IntrospectedTable in project generator by mybatis.

the class Context method generateFiles.

/**
     * 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(//$NON-NLS-1$
            "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());
}
Also used : PluginAggregator(org.mybatis.generator.internal.PluginAggregator) IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) Plugin(org.mybatis.generator.api.Plugin)

Example 4 with IntrospectedTable

use of org.mybatis.generator.api.IntrospectedTable in project generator by mybatis.

the class ObjectFactory method createIntrospectedTableForValidation.

/**
     * This method creates an introspected table implementation that is only usable for validation (i.e. for a context
     * to determine if the target is ibatis2 or mybatis3).
     * 
     *
     * @param context
     *            the context
     * @return the introspected table
     */
public static IntrospectedTable createIntrospectedTableForValidation(Context context) {
    String type = context.getTargetRuntime();
    if (!stringHasValue(type)) {
        type = IntrospectedTableMyBatis3Impl.class.getName();
    } else if ("Ibatis2Java2".equalsIgnoreCase(type)) {
        //$NON-NLS-1$
        type = IntrospectedTableIbatis2Java2Impl.class.getName();
    } else if ("Ibatis2Java5".equalsIgnoreCase(type)) {
        //$NON-NLS-1$
        type = IntrospectedTableIbatis2Java5Impl.class.getName();
    } else if ("Ibatis3".equalsIgnoreCase(type)) {
        //$NON-NLS-1$
        type = IntrospectedTableMyBatis3Impl.class.getName();
    } else if ("MyBatis3".equalsIgnoreCase(type)) {
        //$NON-NLS-1$
        type = IntrospectedTableMyBatis3Impl.class.getName();
    } else if ("MyBatis3Simple".equalsIgnoreCase(type)) {
        //$NON-NLS-1$
        type = IntrospectedTableMyBatis3SimpleImpl.class.getName();
    }
    IntrospectedTable answer = (IntrospectedTable) createInternalObject(type);
    answer.setContext(context);
    return answer;
}
Also used : IntrospectedTableIbatis2Java5Impl(org.mybatis.generator.codegen.ibatis2.IntrospectedTableIbatis2Java5Impl) IntrospectedTableMyBatis3Impl(org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) IntrospectedTable(org.mybatis.generator.api.IntrospectedTable)

Example 5 with IntrospectedTable

use of org.mybatis.generator.api.IntrospectedTable in project generator by mybatis.

the class DatabaseIntrospector method introspectTables.

/**
     * Returns a List of IntrospectedTable elements that matches the specified table configuration.
     *
     * @param tc
     *            the tc
     * @return a list of introspected tables
     * @throws SQLException
     *             the SQL exception
     */
public List<IntrospectedTable> introspectTables(TableConfiguration tc) throws SQLException {
    // get the raw columns from the DB
    Map<ActualTableName, List<IntrospectedColumn>> columns = getColumns(tc);
    if (columns.isEmpty()) {
        warnings.add(getString(//$NON-NLS-1$
        "Warning.19", //$NON-NLS-1$
        tc.getCatalog(), tc.getSchema(), tc.getTableName()));
        return null;
    }
    removeIgnoredColumns(tc, columns);
    calculateExtraColumnInformation(tc, columns);
    applyColumnOverrides(tc, columns);
    calculateIdentityColumns(tc, columns);
    List<IntrospectedTable> introspectedTables = calculateIntrospectedTables(tc, columns);
    // now introspectedTables has all the columns from all the
    // tables in the configuration. Do some validation...
    Iterator<IntrospectedTable> iter = introspectedTables.iterator();
    while (iter.hasNext()) {
        IntrospectedTable introspectedTable = iter.next();
        if (!introspectedTable.hasAnyColumns()) {
            // add warning that the table has no columns, remove from the
            // list
            String warning = getString("Warning.1", //$NON-NLS-1$
            introspectedTable.getFullyQualifiedTable().toString());
            warnings.add(warning);
            iter.remove();
        } else if (!introspectedTable.hasPrimaryKeyColumns() && !introspectedTable.hasBaseColumns()) {
            // add warning that the table has only BLOB columns, remove from
            // the list
            String warning = getString("Warning.18", //$NON-NLS-1$ 
            introspectedTable.getFullyQualifiedTable().toString());
            warnings.add(warning);
            iter.remove();
        } else {
            // now make sure that all columns called out in the
            // configuration
            // actually exist
            reportIntrospectionWarnings(introspectedTable, tc, introspectedTable.getFullyQualifiedTable());
        }
    }
    return introspectedTables;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) JavaBeansUtil.getCamelCaseString(org.mybatis.generator.internal.util.JavaBeansUtil.getCamelCaseString)

Aggregations

IntrospectedTable (org.mybatis.generator.api.IntrospectedTable)7 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)1 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)1 JavaTypeResolver (org.mybatis.generator.api.JavaTypeResolver)1 Plugin (org.mybatis.generator.api.Plugin)1 IntrospectedTableIbatis2Java5Impl (org.mybatis.generator.codegen.ibatis2.IntrospectedTableIbatis2Java5Impl)1 IntrospectedTableMyBatis3Impl (org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl)1 PluginAggregator (org.mybatis.generator.internal.PluginAggregator)1 DatabaseIntrospector (org.mybatis.generator.internal.db.DatabaseIntrospector)1 JavaBeansUtil.getCamelCaseString (org.mybatis.generator.internal.util.JavaBeansUtil.getCamelCaseString)1