Skip to content

Commit

Permalink
build: 2.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Jul 8, 2022
1 parent 4c697a8 commit ba65922
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 27 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
run: |
brew info postgresql
brew services start postgresql -v
sleep 10s
sleep 3s
tail -n 100 /usr/local/var/log/postgres.log
createuser -U runner -s postgres
createdb mirai
Expand All @@ -72,7 +72,7 @@ jobs:
brew install mysql
brew info mysql
brew services restart mysql -v
sleep 10s
sleep 3s
mysqladmin -uroot create mirai
mysqladmin -uroot password root
Expand Down Expand Up @@ -100,6 +100,9 @@ jobs:
- name: H2Test
run: ./gradlew test --tests "xyz.cssxsh.mirai.hibernate.entry.H2Test" --scan --info

- name: MysqlTest
run: ./gradlew test --tests "xyz.cssxsh.mirai.hibernate.entry.MysqlTest" --scan --info

- name: PostgreSqlTest
run: ./gradlew test --tests "xyz.cssxsh.mirai.hibernate.entry.PostgreSqlTest" --scan --info

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[Mirai Console](https://github.com/mamoe/mirai-console) 的前置插件,用于 Hibernate ORM 框架的初始化

[![maven-central](https://img.shields.io/maven-central/v/xyz.cssxsh.mirai/mirai-hibernate-plugin)](https://search.maven.org/artifact/xyz.cssxsh.mirai/mirai-hibernate-plugin)
[![Database Test](https://github.com/cssxsh/mirai-hibernate-plugin/actions/workflows/test.yml/badge.svg)](https://github.com/cssxsh/mirai-hibernate-plugin/actions/workflows/test.yml)

插件自带聊天记录器 [MiraiHibernateRecorder](src/main/kotlin/xyz/cssxsh/mirai/hibernate/MiraiHibernateLoader.kt),
会记录 `群聊/私聊` 的内容到数据库方便其他插件使用,默认是 `h2database` 数据库(since `2.2.0+`)
Expand All @@ -15,10 +16,10 @@

本插件打包了以下版本的数据库驱动和连接池

* `mysql:mysql-connector-java:8.0.29`
* `org.xerial:sqlite-jdbc:3.36.0.3`
* `org.postgresql:postgresql:42.4.0`
* `com.h2database:h2:2.1.214`
* `mysql:mysql-connector-java:8.0.29` - [mysql](example/mysql.hibernate.properties)
* `org.xerial:sqlite-jdbc:3.36.0.3` - [sqlite](example/sqlite.hibernate.properties)
* `org.postgresql:postgresql:42.4.0` - [postgresql](example/postgresql.hibernate.properties)
* `com.h2database:h2:2.1.214` - [h2](example/h2.hibernate.properties)
* `com.zaxxer:HikariCP:5.0.1`

需要其他数据库驱动或连接池支持,请添加 `plugin-shared-libraries` 依赖,有两种方法
Expand All @@ -35,6 +36,11 @@ repositories {
dependencies {
compileOnly("xyz.cssxsh.mirai:mirai-hibernate-plugin:${version}")
}
// hibernate 6 和 HikariCP 5 需要 jdk11
mirai {
jvmTarget = JavaVersion.VERSION_11
}
```

## 在 Mirai Core Jvm 项目中引用
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "xyz.cssxsh.mirai"
version = "2.3.2"
version = "2.3.3"

mavenCentralPublish {
useCentralS01()
Expand Down
2 changes: 0 additions & 2 deletions example/mysql.hibernate.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@ hibernate.dialect=org.hibernate.dialect.MariaDBDialect
hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider
hibernate.connection.isolation=1
hibernate.hbm2ddl.auto=update
hibernate-connection-autocommit=true
hibernate.connection.show_sql=false
hibernate.autoReconnect=true
2 changes: 0 additions & 2 deletions example/postgresql.hibernate.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider
hibernate.connection.isolation=1
hibernate.hbm2ddl.auto=update
hibernate-connection-autocommit=true
hibernate.connection.show_sql=false
hibernate.autoReconnect=true
11 changes: 6 additions & 5 deletions src/main/kotlin/xyz/cssxsh/hibernate/Criteria.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ public fun CriteriaBuilder.rand(): Expression<Double> = function("rand", Double:
* @param model 模,应为正整数
* @return 随机生成 [0, model) 范围的数
* @since 2.3.3
* @see Configuration.addRandFunction
*/
public fun CriteriaBuilder.dice(model: Expression<Long>): Expression<Long> = function("dice", Long::class.java, model)

/**
* Sqlite / PostgreSQL 添加 rand 函数别名
*
* 通过 `hibernate.connection.url` 判断数据库类型,然后添加对应的函数别名
*
* @since 2.3.3
* @see CriteriaBuilder.rand
*/
public fun Configuration.addRandFunction() {
Expand All @@ -39,7 +40,7 @@ public fun Configuration.addRandFunction() {
val url = getProperty("hibernate.connection.url") ?: throw IllegalStateException("url is empty")
when {
url.startsWith("jdbc:sqlite") -> {
addSqlFunction("rand", MacroSQLFunction("rand", StandardBasicTypes.DOUBLE) { _, _ ->
addSqlFunction("rand", MacroSQLFunction(StandardBasicTypes.DOUBLE) { _, _ ->
appendSql("((RANDOM() + 9223372036854775808) / 2.0 / 9223372036854775808)")
})
}
Expand All @@ -65,23 +66,23 @@ public fun Configuration.addDiceFunction() {
val url = getProperty("hibernate.connection.url") ?: throw IllegalStateException("url is empty")
when {
url.startsWith("jdbc:sqlite") -> {
addSqlFunction("dice", MacroSQLFunction("dice", StandardBasicTypes.LONG) { args, translator ->
addSqlFunction("dice", MacroSQLFunction(StandardBasicTypes.LONG) { args, translator ->
val (model) = args
appendSql("ABS(RANDOM() % ")
translator.render(model, SqlAstNodeRenderingMode.DEFAULT)
appendSql(")")
})
}
url.startsWith("jdbc:postgresql") -> {
addSqlFunction("dice", MacroSQLFunction("dice", StandardBasicTypes.LONG) { args, translator ->
addSqlFunction("dice", MacroSQLFunction(StandardBasicTypes.LONG) { args, translator ->
val (model) = args
appendSql("FLOOR(")
translator.render(model, SqlAstNodeRenderingMode.DEFAULT)
appendSql(" * RANDOM())")
})
}
else -> {
addSqlFunction("dice", MacroSQLFunction("dice", StandardBasicTypes.LONG) { args, translator ->
addSqlFunction("dice", MacroSQLFunction(StandardBasicTypes.LONG) { args, translator ->
val (model) = args
appendSql("FLOOR(")
translator.render(model, SqlAstNodeRenderingMode.DEFAULT)
Expand Down
15 changes: 11 additions & 4 deletions src/main/kotlin/xyz/cssxsh/hibernate/MacroSQLFunction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ import org.hibernate.sql.ast.tree.predicate.Predicate
import org.hibernate.sql.ast.tree.select.SortSpecification
import org.hibernate.type.*

/**
* 宏函数,通过构造宏来实现自定义的函数方法.
* @param type 返回值类型,无返回值可以填 null,
* @param macro 表达式构造 lambda
* @since 2.3.3
* @see addRandFunction
* @see addDiceFunction
*/
public class MacroSQLFunction(
name: String,
type: BasicTypeReference<*>? = null,
private val expression: SqlAppender.(List<SqlAstNode>, SqlAstTranslator<*>) -> Unit
) : StandardSQLFunction(name, false, type) {
private val macro: SqlAppender.(List<SqlAstNode>, SqlAstTranslator<*>) -> Unit
) : StandardSQLFunction("macro", false, type) {

override fun render(
sqlAppender: SqlAppender,
sqlAstArguments: List<SqlAstNode>,
translator: SqlAstTranslator<*>
) {
expression.invoke(sqlAppender, sqlAstArguments, translator)
macro.invoke(sqlAppender, sqlAstArguments, translator)
}

override fun render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,25 @@ public class MiraiHibernateConfiguration(private val loader: MiraiHibernateLoade
private fun load() {
if (loader.autoScan) scan()
loader.configuration.apply { if (exists().not()) writeText(loader.default) }.inputStream().use(properties::load)
// 设置 rand 别名
addRandFunction()
// 设置 dice 宏
addDiceFunction()
val url = getProperty("hibernate.connection.url").orEmpty()
when {
url.startsWith("jdbc:h2") -> {
//
}
url.startsWith("jdbc:sqlite") -> {
// SQLite 是单文件数据库,最好只有一个连接
setProperty("hibernate.hikari.minimumIdle", "${1}")
setProperty("hibernate.hikari.maximumPoolSize", "${1}")
// 设置 rand 别名
addRandFunction()
setProperty("hibernate.hikari.minimumIdle", "1")
setProperty("hibernate.hikari.maximumPoolSize", "1")
}
url.startsWith("jdbc:mysql") -> {
//
}
url.startsWith("jdbc:postgresql") -> {
// 设置 rand 别名
addRandFunction()
//
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public object MiraiHibernatePlugin : KotlinPlugin(
JvmPluginDescription(
id = "xyz.cssxsh.mirai.plugin.mirai-hibernate-plugin",
name = "mirai-hibernate-plugin",
version = "2.3.2",
version = "2.3.3",
) {
author("cssxsh")

Expand Down

0 comments on commit ba65922

Please sign in to comment.