Skip to content

Commit

Permalink
✨ Supported for redis database modification and add jooq page util (#63)
Browse files Browse the repository at this point in the history
* ✨ Supported for redis database modification

* ✨ Add jooq page util

* Format code

* Optimize code and update gradle

* Optimize code and update gradle

* Fixed bug

* Reformat code
  • Loading branch information
GuoDuanLZ authored Aug 27, 2020
1 parent 8a3ed62 commit f6da309
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.bybutter.sisyphus.protobuf
import com.bybutter.sisyphus.protobuf.primitives.FieldDescriptorProto
import com.bybutter.sisyphus.security.base64Decode
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KProperty
import kotlin.reflect.full.companionObjectInstance
import kotlin.reflect.full.isSubclassOf
Expand Down Expand Up @@ -121,8 +120,17 @@ class MessagePatcher : PatcherNode {
}

fun applyTo(message: MutableMessage<*, *>) {
val function = MutableMessage<*, *>::mergeWith as KFunction<Unit>
function.call(message, asMessage(message.type()))
for ((field, value) in nodes) {
if (message.getProperty(field) == null) {
continue
}

if (value is MessagePatcher && message.has(field)) {
value.applyTo(message[field])
} else {
message[field] = value.asField(message.fieldDescriptor(field), message.getProperty(field)!!)
}
}
}

@OptIn(InternalProtoApi::class)
Expand All @@ -138,12 +146,7 @@ class MessagePatcher : PatcherNode {
@OptIn(InternalProtoApi::class)
fun asMessage(type: String): Message<*, *> {
return ProtoTypes.ensureSupportByProtoName(type).newMutable().apply {
for ((field, value) in nodes) {
if (this.getProperty(field) == null) {
continue
}
this[field] = value.asField(this.fieldDescriptor(field), this.getProperty(field)!!)
}
applyTo(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.bybutter.sisyphus.middleware.jdbc.utils

import com.bybutter.sisyphus.api.paging.OffsetPaging
import com.bybutter.sisyphus.api.paging.invoke
import com.bybutter.sisyphus.api.paging.nextPage
import org.jooq.Record
import org.jooq.SelectConditionStep

data class Page<T>(var data: List<T>, var nextToken: String?, var total: Int? = null)

inline fun <reified T : Record> SelectConditionStep<T>.withPaging(pageToken: String, pageSize: Int, needTotal: Boolean = true): Page<T> {
val offset = OffsetPaging(pageToken)?.offset
val size = if (pageSize in 1..30) {
pageSize
} else {
30
}
val count = if (needTotal) this.count() else null
val data = this.offset(offset).limit(size).fetchInto(T::class.java)
val next = OffsetPaging(pageToken).nextPage(data.size, size)
return Page(data, next, count)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ open class DefaultRedisClientFactory : RedisClientFactory {

protected open fun createRedisClient(host: String, port: Int, property: RedisProperty): RedisClient {
return clients.getOrPut("$host:$port") {
val database = property.database ?: 0
RedisClient.create(RedisURI.Builder.redis(host, port).withPassword(property.password).withDatabase(database).build())
RedisClient.create(RedisURI.Builder.redis(host, port).withPassword(property.password).withDatabase(property.database ?: 0).build())
}
}
}

0 comments on commit f6da309

Please sign in to comment.