Skip to content

Commit

Permalink
Fix generate entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Enaium committed Jan 22, 2025
1 parent c539b78 commit 281dcfb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

## Feature

- Generate code for database table, column and association.
- Generate code for table, column and association from database or ddl.
- Incremental compile for dto language (apt/ksp).
- Add implementation (spring-boot-start/sql/sql-kotlin) for dependencies.
- Add annotationProcessor/ksp for dependencies.
Expand Down Expand Up @@ -98,6 +98,8 @@ jimmer {
![Static Badge](https://img.shields.io/badge/-MariaDB-gray?style=flat-square&logo=mariadb&logoColor=white)
![Static Badge](https://img.shields.io/badge/-MySQL-gray?style=flat-square&logo=mysql&logoColor=white)

### From Database

```kotlin
import cn.enaium.jimmer.gradle.extension.Association
import cn.enaium.jimmer.gradle.extension.Driver
Expand All @@ -109,7 +111,7 @@ plugins {

dependencies {
//...
implementation("org.postgresql:postgresql:42.6.0")//require jdbc driver
runtimeOnly("org.postgresql:postgresql:42.6.0")//require jdbc driver
}

jimmer {
Expand All @@ -123,7 +125,6 @@ jimmer {
url.set("jdbc:postgresql://localhost:5432/postgres")
username.set("postgres")
password.set("postgres")
// ddl.set(file("src/main/resources/schema.sql"))// if you set ddl, driver, url, username, password can be ignored
// catalog.set("postgres")
// schemaPattern.set("public")
// tableNamePattern.set("t_%")
Expand All @@ -143,6 +144,22 @@ jimmer {
}
```

### From DDL

```kotlin
jimmer {
generator {
target {
srcDir.set("src/main/kotlin")
packageName.set("cn.enaium")
}
jdbc {
ddl.set(file("src/main/resources/schema.sql"))
}
}
}
```

### Association(Only Gradle Project Plugin)

You must add the suffix '_id'(`primaryKey.set("id")`) to the column name if the column is a fake foreign key, otherwise
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
version=0.0.26
version=0.0.27
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ enum class Driver(val className: String, val module: String) {
HYPERSONIC("org.hsqldb.jdbc.JDBCDriver", "hsqldb"),
ORACLE("oracle.jdbc.driver.OracleDriver", "ojdbc"),
DB2("com.ibm.db2.jcc.DB2Driver", "jcc"),
SQL_SERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver", "mssql-jdbc"),
SQLSERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver", "mssql-jdbc"),
}
35 changes: 20 additions & 15 deletions src/main/kotlin/cn/enaium/jimmer/gradle/task/GenerateEntityTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,28 @@ open class GenerateEntityTask : DefaultTask() {
javaTypeMappings.putAll(generator.table.typeMappings.get())
}

val driver =
generator.jdbc.driver.takeIf { it.isPresent }?.get() ?: throw RuntimeException("Driver not configured")
configurations.named("runtimeClasspath").get()
.find { file -> file.name.startsWith(driver.module) }
?.also {
DriverManager.registerDriver(
DiverWrapper(
Class.forName(
driver.className, true,
URLClassLoader(arrayOf(it.toURI().toURL()), this.javaClass.classLoader)
).getConstructor()
.newInstance() as Driver
generator.jdbc.driver.takeIf { it.isPresent }?.also driver@{ driver ->
configurations.named("runtimeClasspath").get()
.find { file -> file.name.startsWith(driver.get().module) }
?.also {
generator.jdbc.ddl.isPresent && return@also
DriverManager.registerDriver(
DiverWrapper(
Class.forName(
driver.get().className, true,
URLClassLoader(arrayOf(it.toURI().toURL()), this.javaClass.classLoader)
).getConstructor()
.newInstance() as Driver
)
)
)
} ?: run {
} ?: run {
if (!generator.jdbc.ddl.isPresent) {
throw RuntimeException("Failed to find driver module")
}
}
} ?: run {
if (!generator.jdbc.ddl.isPresent) {
throw RuntimeException("Failed to find driver module")
throw RuntimeException("Driver not configured")
}
}
if (extension.language.get() == Language.KOTLIN) {
Expand Down

0 comments on commit 281dcfb

Please sign in to comment.