Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to obtain the unique constraint fields of a table in extension plugin #1214

Closed
jobmission opened this issue Jul 16, 2024 · 2 comments
Closed
Labels

Comments

@jobmission
Copy link

Hello, I would like to write an extension plugin that automatically generates a method for querying or updating based on the unique constraint of a table. However, how can I obtain the unique constraint fields of a table in the plugin?

@jeffgbutler
Copy link
Member

Any plugin method has access to the plugin context. From the context you can create a database connection and retrieve information you need. In this case, you should call the getCrossReference method in database metadata and interpret the results as you wish. For example something like this:

    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        try (Connection connection = context.getConnection()) {
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            ResultSet resultSet = databaseMetaData.getCrossReference(...)
            // process the resultSet to find the unique constraints, then generate methods as needed
        } catch (SQLException e) {
            throw new RuntimeException("SqlException in my plugin", e);
        }

        return true;
    }

If you need to do this in more than one plugin method, you could do the instrospection before the individual methods are called like this:

    @Override
    public void initialized(IntrospectedTable introspectedTable) {
        try (Connection connection = context.getConnection()) {
            DatabaseMetaData databaseMetaData = connection.getMetaData();
            ResultSet resultSet = databaseMetaData.getCrossReference(...)
            // process the resultSet to find the unique constraints, then save that information for later use
        } catch (SQLException e) {
            throw new RuntimeException("SqlException in my plugin", e);
        }
    }

    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        // use the saved data to generate methods as needed

        return true;
    }

@jobmission
Copy link
Author

Thanks a lot.Got it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants