Tony B
Tony B

Reputation: 949

jooq specify database runtime

I have the exact same database definition for multiple databases ( and database servers ). How do I tell Jooq to use the same database as the "Connection" I created to connect to the DB?

Example ( for MySQL ):

  1. jdbc:mysql://localhost:3306/tsm - my development DB ( tsm ), used to generate code
  2. jdbc:mysql://RemoteAmazonDBHost:3306/customer1 - one of my customers
  3. jdbc:mysql://RemoteAmazonDBHost:3306/customer2 - Another customer

All 3 databases have the same definition, the same tables, indexes, etc. The TSM one is the standard our application uses.

Maybe I should be using DSL.using( Connection, Setting ) instead of DSL.using(Connection)? Is that what the manual is implying here?

If I only have one "Input" schema, do I have to specify it? In other words, can I do something like this:

        Settings settings = new Settings()
                .withRenderMapping(new RenderMapping()
                .withSchemata(
                    new MappedSchema().withOutput(
                            databaseInfo.getProperties().getProperty("database.db"))));

Or do I have to do something like this:

        Settings settings = new Settings()
                .withRenderMapping(new RenderMapping()
                .withSchemata(
                    new MappedSchema().withInput("TSM")
                                      .withOutput(databaseInfo.getProperties().getProperty("database.db"))));

Upvotes: 2

Views: 527

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221076

I'm assuming you're using code generation. In that case, it might be the easiest to not generate the schema at all but use <outputSchemaToDefault> in the code generation configuration, e.g.

<configuration>
  <generator>
    <database>
      <inputSchema>your_codegen_input_schema_here</inputSchema>
      <outputSchemaToDefault>true</outputSchemaToDefault>
    </database>
  </generator>
</configuration>

See the manual for details: https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-database/codegen-database-catalog-and-schema-mapping/

If you want to keep your generated code with schema qualification and map things at runtime, then your second attempt seems correct. Pass this to your Configuration (i.e. the DSL.using() call):

Settings settings = new Settings()
    .withRenderMapping(new RenderMapping()
        .withSchemata(new MappedSchema()
            .withInput("TSM")
            .withOutput(databaseInfo.getProperties().getProperty("database.db"))));

More details can be found here: https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-render-mapping

Upvotes: 2

Related Questions