Skip to content

Commit 6a01bdc

Browse files
committed
chore: code cleanup
1 parent 09292b0 commit 6a01bdc

File tree

6 files changed

+37
-85
lines changed

6 files changed

+37
-85
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fun shouldReturnTupleWithLimitNumber() {
2626
)
2727
val querydslRsql = QuerydslRsql.Builder<Car>(entityManager)
2828
.select(select)
29-
.from(clazz)
29+
// .from(clazz)
3030
.where("id=notnull='' and (name=like='%a%' or name=con='Béla2,Béla11')")
3131
.sort("id.desc")
3232
.limit(15L, 15)

src/main/kotlin/team/yi/rsql/querydsl/QuerydslRsql.kt

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -208,39 +208,17 @@ class QuerydslRsql<E> private constructor(builder: Builder<E>) {
208208
this.orderSpecifiers = builder.orderSpecifiers
209209
}
210210

211-
fun select(select: String?): BuildBuilder<E> {
212-
this.selectString = select
211+
fun select(select: String?): BuildBuilder<E> = BuildBuilder(this).apply { this.selectString = select }
212+
fun select(vararg expression: Expression<*>?): BuildBuilder<E> = BuildBuilder(this).apply { this.selectExpressions = expression.filterNotNull().distinct() }
213213

214-
return BuildBuilder(this)
215-
}
216-
217-
fun select(vararg expression: Expression<*>?): BuildBuilder<E> {
218-
this.selectExpressions = expression.filterNotNull().distinct()
219-
220-
return BuildBuilder(this)
221-
}
222-
223-
fun from(entityName: String?): BuildBuilder<E> {
224-
this.entityName = entityName
225-
226-
return BuildBuilder(this)
227-
}
214+
fun from(entityName: String?): BuildBuilder<E> = BuildBuilder(this).apply { this.entityName = entityName }
215+
fun from(entityClass: Class<E>?): BuildBuilder<E> = BuildBuilder(this).apply { this.entityClass = entityClass }
228216

229-
fun from(entityClass: Class<E>?): BuildBuilder<E> {
230-
this.entityClass = entityClass
231-
232-
return BuildBuilder(this)
233-
}
234-
235-
fun where(where: String?): BuildBuilder<E> {
236-
this.where = where
237-
238-
return BuildBuilder(this)
239-
}
217+
fun where(where: String?): BuildBuilder<E> = BuildBuilder(this).apply { this.where = where }
240218

241219
@Suppress("unused", "MemberVisibilityCanBePrivate")
242220
class BuildBuilder<E>(builder: Builder<E>) : Builder<E>(builder) {
243-
fun globalPredicate(globalPredicate: BooleanExpression?): BuildBuilder<E> = this.also { super.globalPredicate = globalPredicate }
221+
fun globalPredicate(globalPredicate: BooleanExpression?): BuildBuilder<E> = this.apply { super.globalPredicate = globalPredicate }
244222

245223
@Throws(RsqlException::class)
246224
fun build(): QuerydslRsql<E> {
@@ -252,41 +230,37 @@ class QuerydslRsql<E> private constructor(builder: Builder<E>) {
252230
}
253231

254232
fun offset(offset: Int?): BuildBuilder<E> = offset(offset?.toLong())
255-
fun offset(offset: Long?): BuildBuilder<E> = this.also { super.offset = offset }
233+
fun offset(offset: Long?): BuildBuilder<E> = this.apply { super.offset = offset }
256234

257235
fun limit(limit: Int?): BuildBuilder<E> = limit(limit?.toLong())
258-
fun limit(limit: Long?): BuildBuilder<E> = this.also { super.limit = limit }
236+
fun limit(limit: Long?): BuildBuilder<E> = this.apply { super.limit = limit }
259237

260238
fun limit(offset: Int?, limit: Int?): BuildBuilder<E> = limit(offset?.toLong(), limit?.toLong())
261-
fun limit(offset: Long?, limit: Long?): BuildBuilder<E> = this.also {
239+
fun limit(offset: Long?, limit: Long?): BuildBuilder<E> = this.apply {
262240
super.offset = offset
263241
super.limit = limit
264242
}
265243

266244
fun page(pageNumber: Int, pageSize: Int): BuildBuilder<E> = page(pageNumber.toLong(), pageSize.toLong())
267245
fun page(pageNumber: Int, pageSize: Long): BuildBuilder<E> = page(pageNumber.toLong(), pageSize)
268246

269-
fun page(pageNumber: Long, pageSize: Long): BuildBuilder<E> = this.also {
247+
fun page(pageNumber: Long, pageSize: Long): BuildBuilder<E> = this.apply {
270248
super.limit = pageSize
271249
super.offset = pageNumber * pageSize
272250
}
273251

274-
fun sort(sort: String?): BuildBuilder<E> = this.also { super.sort = sort }
275-
fun sort(vararg expression: OrderSpecifier<*>?): BuildBuilder<E> = this.also { super.orderSpecifiers = expression.filterNotNull().distinct() }
276-
fun sort(orderSpecifiers: List<OrderSpecifier<*>>?): BuildBuilder<E> = this.also { super.orderSpecifiers = orderSpecifiers }
252+
fun sort(sort: String?): BuildBuilder<E> = this.apply { super.sort = sort }
253+
fun sort(vararg expression: OrderSpecifier<*>?): BuildBuilder<E> = this.apply { super.orderSpecifiers = expression.filterNotNull().distinct() }
254+
fun sort(orderSpecifiers: List<OrderSpecifier<*>>?): BuildBuilder<E> = this.apply { super.orderSpecifiers = orderSpecifiers }
277255

278-
fun operator(vararg operator: RsqlOperator): BuildBuilder<E> = this.also { super.rsqlConfig.operators = operator.toList() }
279-
fun operators(operators: List<RsqlOperator>): BuildBuilder<E> = this.also { super.rsqlConfig.operators = operators.toList() }
256+
fun operator(vararg operator: RsqlOperator): BuildBuilder<E> = this.apply { super.rsqlConfig.operators = operator.toList() }
257+
fun operators(operators: List<RsqlOperator>): BuildBuilder<E> = this.apply { super.rsqlConfig.operators = operators.toList() }
280258

281-
fun fieldTypeHandler(vararg typeHandler: Class<FieldTypeHandler<E>>): BuildBuilder<E> {
282-
return this.also { super.rsqlConfig.addFieldTypeHandler(*typeHandler) }
283-
}
259+
fun fieldTypeHandler(vararg typeHandler: Class<FieldTypeHandler<E>>): BuildBuilder<E> = this.apply { super.rsqlConfig.addFieldTypeHandler(*typeHandler) }
284260

285-
fun sortFieldTypeHandler(vararg typeHandler: Class<SortFieldTypeHandler<E>>): BuildBuilder<E> {
286-
return this.also { super.rsqlConfig.addSortFieldTypeHandler(*typeHandler) }
287-
}
261+
fun sortFieldTypeHandler(vararg typeHandler: Class<SortFieldTypeHandler<E>>): BuildBuilder<E> = this.apply { super.rsqlConfig.addSortFieldTypeHandler(*typeHandler) }
288262

289-
fun dateFormat(dateFormat: String): BuildBuilder<E> = this.also { super.rsqlConfig.dateFormat = dateFormat }
263+
fun dateFormat(dateFormat: String): BuildBuilder<E> = this.apply { super.rsqlConfig.dateFormat = dateFormat }
290264
}
291265
}
292266
}

src/main/kotlin/team/yi/rsql/querydsl/RsqlConfig.kt

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,40 +99,30 @@ class RsqlConfig<E> private constructor(builder: Builder<E>) {
9999
internal val nodeInterceptors = mutableListOf<RsqlNodeInterceptor>()
100100
internal var dateFormat: String? = null
101101

102-
fun operator(vararg operator: RsqlOperator): Builder<E> = this.also { this.operators = operator.toList() }
103-
fun fieldTypeHandler(vararg typeHandler: Class<out FieldTypeHandler<*>>): Builder<E> = this.also { this.fieldTypeHandlers = typeHandler.toList() }
104-
fun sortFieldTypeHandler(vararg typeHandler: Class<out SortFieldTypeHandler<*>>): Builder<E> = this.also { this.sortFieldTypeHandlers = typeHandler.toList() }
102+
fun operator(vararg operator: RsqlOperator): Builder<E> = this.apply { this.operators = operator.toList() }
103+
104+
fun fieldTypeHandler(vararg typeHandler: Class<out FieldTypeHandler<*>>): Builder<E> = this.apply { this.fieldTypeHandlers = typeHandler.toList() }
105+
106+
fun sortFieldTypeHandler(vararg typeHandler: Class<out SortFieldTypeHandler<*>>): Builder<E> = this.apply { this.sortFieldTypeHandlers = typeHandler.toList() }
105107

106108
@Suppress("UNCHECKED_CAST")
107-
fun javaFieldTypeHandler(vararg typeHandler: Class<*>): Builder<E> = this.also {
109+
fun javaFieldTypeHandler(vararg typeHandler: Class<*>): Builder<E> = this.apply {
108110
this.fieldTypeHandlers = typeHandler.mapNotNull { it as? Class<out FieldTypeHandler<*>> }
109111
}
110112

111-
@Suppress("UNCHECKED_CAST", "unused")
112-
fun javaSortFieldTypeHandler(vararg typeHandler: Class<*>): Builder<E> = this.also {
113+
@Suppress("UNCHECKED_CAST")
114+
fun javaSortFieldTypeHandler(vararg typeHandler: Class<*>): Builder<E> = this.apply {
113115
this.sortFieldTypeHandlers = typeHandler.mapNotNull { it as? Class<out SortFieldTypeHandler<*>> }
114116
}
115117

116-
fun nodeInterceptors(nodeInterceptors: List<RsqlNodeInterceptor>?): Builder<E> {
117-
nodeInterceptors?.let { this.nodeInterceptors.addAll(nodeInterceptors) }
118-
119-
return this
120-
}
118+
fun nodeInterceptors(nodeInterceptors: List<RsqlNodeInterceptor>?): Builder<E> = this.apply { nodeInterceptors?.let { this.nodeInterceptors.addAll(nodeInterceptors) } }
121119

122-
fun nodeInterceptor(block: () -> RsqlNodeInterceptor?): Builder<E> {
123-
block()?.let { this.nodeInterceptor(it) }
124-
125-
return this
126-
}
120+
fun nodeInterceptor(block: () -> RsqlNodeInterceptor?): Builder<E> = this.apply { block()?.let { this.nodeInterceptor(it) } }
127121

128122
@Suppress("MemberVisibilityCanBePrivate")
129-
fun nodeInterceptor(nodeInterceptor: RsqlNodeInterceptor?): Builder<E> {
130-
nodeInterceptor?.let { this.nodeInterceptors.add(nodeInterceptor) }
131-
132-
return this
133-
}
123+
fun nodeInterceptor(nodeInterceptor: RsqlNodeInterceptor?): Builder<E> = this.apply { nodeInterceptor?.let { this.nodeInterceptors.add(nodeInterceptor) } }
134124

135-
fun dateFormat(dateFormat: String?): Builder<E> = this.also { this.dateFormat = dateFormat }
125+
fun dateFormat(dateFormat: String?): Builder<E> = this.apply { this.dateFormat = dateFormat }
136126

137127
@Throws(RsqlException::class)
138128
fun build(): RsqlConfig<E> {

src/main/kotlin/team/yi/rsql/querydsl/exception/BuildException.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/kotlin/team/yi/rsql/querydsl/exception/EntityNotFoundException.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/kotlin/team/yi/rsql/querydsl/util/RsqlUtil.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import javax.persistence.EntityManager
1212

1313
@Suppress("MemberVisibilityCanBePrivate")
1414
object RsqlUtil {
15+
@JvmStatic
1516
fun getClassForEntityString(entityName: String, entityManager: EntityManager): Class<*>? {
1617
return entityManager.metamodel.entities
1718
.firstOrNull { it.name == entityName }
1819
?.bindableJavaType
1920
}
2021

22+
@JvmStatic
2123
fun getOperators(customOperators: List<RsqlOperator>?): Set<ComparisonOperator> {
2224
val operators = Operator.lookup.keys
2325
.asSequence()
@@ -35,6 +37,7 @@ object RsqlUtil {
3537
return operators
3638
}
3739

40+
@JvmStatic
3841
fun validateOperators(operators: List<RsqlOperator>) {
3942
operators.forEach { operator ->
4043
operator.symbols.forEach {
@@ -54,6 +57,7 @@ object RsqlUtil {
5457
return if (selectFields.isEmpty()) emptyList() else selectFields.map { pathBuilder[it] }
5558
}
5659

60+
@JvmStatic
5761
fun parseSelectExpression(selectString: String?): List<String> {
5862
if (selectString.isNullOrBlank()) return emptyList()
5963

@@ -62,6 +66,7 @@ object RsqlUtil {
6266
return str.split(',').distinct()
6367
}
6468

69+
@JvmStatic
6570
fun parseFieldSelector(rootClass: Class<*>, fieldSelector: String?): List<FieldMetadata> {
6671
var field = fieldSelector
6772

@@ -89,6 +94,7 @@ object RsqlUtil {
8994
return fieldMetadataList
9095
}
9196

97+
@JvmStatic
9298
fun parseSortExpression(sort: String?): Map<String, Order> {
9399
val result: MutableMap<String, Order> = HashMap()
94100
val sortParams = parseSelectExpression(sort)

0 commit comments

Comments
 (0)