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

Add a jOOQ-meta-kotlin module for kotlin extensions to the jOOQ-meta module #15400

Closed
lukaseder opened this issue Jul 13, 2023 · 2 comments
Closed

Comments

@lukaseder
Copy link
Member

jOOQ 3.19 will ship with a new official jooq-codegen-gradle plugin:

To make configuring jOOQ from Groovy / Kotlin simpler, new modules could be added containing extension functions that DSL-ify the org.jooq.meta.jaxb.Configuration class, etc. For example:

fun configuration(block: Configuration.() -> Unit): Configuration {
    val c = Configuration()
    block(c)
    return c
}

fun Configuration.generator(block: Generator.() -> Unit) {
    if (generator == null)
        generator = Generator()

    block(generator)
}

fun Generator.database(block: Database.() -> Unit) {
    if (database == null)
        database = Database()

    block(database)
}

fun Generator.target(block: Target.() -> Unit) {
    if (target == null)
        target = Target()

    block(target)
}

fun Database.properties(block: MutableList<Property>.() -> Unit) {
    block(properties)
}

fun MutableList<Property>.property(block: Property.() -> Unit) {
    val p = Property()
    block(p)
    add(p)
}

println(configuration {
    generator {
        database {
            properties {
                property {
                    key = "sql"
                    value = """
                    create schema s;
                    create table s.t (i int primary key)
                    """
                }
            }
        }
        target {
            packageName = "com.example"
        }
    }
})

This produces a valid XML configuration as follows:

<configuration>
	<generator>
		<name>org.jooq.codegen.DefaultGenerator</name>
		<database>
			<properties>
				<property>
					<key>sql</key>
					<value>
                    			create schema s;
                    			create table s.t (i int primary key)
                    			</value>
				</property>
			</properties>
		</database>
		<target>
			<packageName>com.example</packageName>
		</target>
	</generator>
</configuration>

Seems like a super low hanging fruit! I'll check if the gradle/groovy integration can profit of a similar thing. The existing third party plugin https://github.com/etiennestuder/gradle-jooq-plugin does some reflection tricks, but it would be better if we could generate actual code to help the IDE with auto completion, etc.

@lukaseder lukaseder added this to the Version 3.19.0 milestone Jul 13, 2023
@lukaseder lukaseder added this to To do in 3.14 Better Kotlin and Scala Support via automation Jul 13, 2023
@lukaseder lukaseder changed the title Add a jooq-meta-kotlin Add a jooq-meta-kotlin module for kotlin extensions to the jOOQ-meta module Jul 13, 2023
@lukaseder lukaseder changed the title Add a jooq-meta-kotlin module for kotlin extensions to the jOOQ-meta module Add a jOOQ-meta-kotlin module for kotlin extensions to the jOOQ-meta module Jul 13, 2023
@lukaseder
Copy link
Member Author

This test passes!

assertEquals(
    Configuration()
        .withJdbc(Jdbc().withUrl("x"))
        .withLogging(Logging.DEBUG)
        .withGenerator(Generator()
            .withDatabase(Database()
                .withProperties(
                    Property().withKey("k").withValue("v"),
                    Property().withKey("x").withValue("y"))
                .withSyntheticObjects(SyntheticObjectsType()
                    .withPrimaryKeys(SyntheticPrimaryKeyType()
                        .withKey("k")
                        .withFields("a", "b", "c")))
                .withSchemata(SchemaMappingType()
                    .withInputSchema("input")
                    .withOutputSchema("output")
                ))
            .withGenerate(Generate()
                .withUdts(true))
        ),
    configuration {
        jdbc { url = "x" }
        logging = Logging.DEBUG
        generator {
            database {
                properties {
                    property {
                        key = "k"
                        value = "v"
                    }
                    property {
                        key = "x"
                        value = "y"
                    }
                }
                syntheticObjects {
                    primaryKeys {
                        primaryKey {
                            key = "k"
                            fields = listOf("a", "b", "c")
                        }
                    }
                }
                schemata {
                    schema {
                        inputSchema = "input"
                        outputSchema = "output"
                    }
                }
            }
            generate {
                isUdts = true
            }
        }
    }
)

Very cool!

3.14 Better Kotlin and Scala Support automation moved this from To do to Done Jul 13, 2023
@lukaseder
Copy link
Member Author

Depending on whether #12985 will be backported, this will be backported as well.

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

No branches or pull requests

1 participant