Jooq tables at runtime

108 views
Skip to first unread message

deepankar gupta

unread,
Oct 12, 2023, 2:02:36 AM10/12/23
to jOOQ User Group
can jooq create classes from database-schema file at runtime??

Lukas Eder

unread,
Oct 12, 2023, 2:41:32 AM10/12/23
to jooq...@googlegroups.com
Hi Deepankar,

Thank you for your message.

What are you trying to do?

Best Regards,
Lukas

On Thu, Oct 12, 2023 at 8:02 AM deepankar gupta <deepankar...@gmail.com> wrote:
can jooq create classes from database-schema file at runtime??

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/8419fe0a-469f-4be7-8124-167584db1f05n%40googlegroups.com.

deepankar gupta

unread,
Oct 12, 2023, 3:01:52 AM10/12/23
to jOOQ User Group
Hi Lucas,

Currently, I am using database-schema.xml file and jooq-codegen-maven plugin to generate jooq tables at build time. But Now I want to create jooq tables at runtime. So I was going through some articles where we can generate jooq tables at runtime using groovy or kotlin script. Is this a correct way or any other alternative.
// Create a Groovy script that generates jOOQ classes def script = """ import org.jooq.codegen.GenerationTool import org.jooq.meta.jaxb.* import org.jooq.meta.extensions.* import org.jooq.meta.* import java.io.* def config = new Configuration() config.withJdbc(new Jdbc() .withDriver("com.mysql.cj.jdbc.Driver") .withUrl("jdbc:mysql://localhost:3306/mydatabase") .withUser("myuser") .withPassword("mypassword")) config.withGenerator(new Generator() .withDatabase(new Database() .withName("org.jooq.meta.mysql.MySQLDatabase") .withIncludes(".*") .withExcludes("") .withInputSchema("public")) .withGenerate(new Generate() .withPojos(true) .withDaos(true)) .withTarget(new Target() .withPackageName("com.example.generated") .withDirectory("/path/to/generated/classes"))) def tool = new GenerationTool() tool.run(config, new PrintWriter(System.out)) """ // Evaluate the Groovy script to generate jOOQ classes GroovyShell shell = new GroovyShell(); shell.evaluate(script); // Use the generated jOOQ classes in your application DSLContext context = DSL.using(dataSource, SQLDialect.MYSQL); MyTableRecord record = context.selectFrom(MY_TABLE).where(MY_TABLE.ID.eq(1)).fetchOne();

Lukas Eder

unread,
Oct 12, 2023, 3:06:20 AM10/12/23
to jooq...@googlegroups.com
Hi Deepankar,

Why do you want to generate classes at runtime?

deepankar gupta

unread,
Oct 12, 2023, 3:16:47 AM10/12/23
to jOOQ User Group
Hi Lucas,
Basically, I have different clients which are using my library to generate sql. Currently, we have to 2 seperate repos one for code generation and another one is using database-schema file generated by first library to generate jooq tables at build time. And right now its a very tedious process to connect their database first and run code generation tool to generate database-schema.xml file and create a pr in second repo with that database schema file and again start the application. So if there is a way at runtime only we connect to their database and generate jooq tables on the fly and create sql query. 

Regards
Deepankar Gupta

Lukas Eder

unread,
Oct 12, 2023, 7:38:49 AM10/12/23
to jooq...@googlegroups.com
I'm not sure if I understand your use-case. Nor the desire to generate classes at runtime. I don't see how doing things at runtime would make anything easier at all.

You don't have to use generated code if you don't know your client's schema at build time... You can discover the schema at runtime using the org.jooq.Meta utility (available from DSLContext::meta). But I'm not sure if that's any help at all.

Reply all
Reply to author
Forward
0 new messages