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

Provide text search for PostgreSQL #486

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 17 additions & 54 deletions ktorm-core/src/main/kotlin/org/ktorm/expression/SqlFormatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,7 @@ public abstract class SqlFormatter(
if (!expr.declaredName.isNullOrBlank()) {
write("${expr.declaredName.quoted} ")
} else {
if (expr.expression.removeBrackets) {
visit(expr.expression)
} else {
write("(")
visit(expr.expression)
removeLastBlank()
write(") ")
}
writeSqlExpression(expr.expression)
}

return expr
Expand Down Expand Up @@ -475,52 +468,20 @@ public abstract class SqlFormatter(

override fun <T : Any> visitUnary(expr: UnaryExpression<T>): UnaryExpression<T> {
if (expr.type == UnaryExpressionType.IS_NULL || expr.type == UnaryExpressionType.IS_NOT_NULL) {
if (expr.operand.removeBrackets) {
visit(expr.operand)
} else {
write("(")
visit(expr.operand)
removeLastBlank()
write(") ")
}

writeSqlExpression(expr.operand)
writeKeyword("${expr.type} ")
} else {
writeKeyword("${expr.type} ")

if (expr.operand.removeBrackets) {
visit(expr.operand)
} else {
write("(")
visit(expr.operand)
removeLastBlank()
write(") ")
}
writeSqlExpression(expr.operand)
}

return expr
}

override fun <T : Any> visitBinary(expr: BinaryExpression<T>): BinaryExpression<T> {
if (expr.left.removeBrackets) {
visit(expr.left)
} else {
write("(")
visit(expr.left)
removeLastBlank()
write(") ")
}

writeSqlExpression(expr.left)
writeKeyword("${expr.type} ")

if (expr.right.removeBrackets) {
visit(expr.right)
} else {
write("(")
visit(expr.right)
removeLastBlank()
write(") ")
}
writeSqlExpression(expr.right)

return expr
}
Expand All @@ -533,16 +494,7 @@ public abstract class SqlFormatter(

override fun <T : Any> visitCasting(expr: CastingExpression<T>): CastingExpression<T> {
writeKeyword("cast(")

if (expr.expression.removeBrackets) {
visit(expr.expression)
} else {
write("(")
visit(expr.expression)
removeLastBlank()
write(") ")
}

writeSqlExpression(expr.expression)
writeKeyword("as ${expr.sqlType.typeName}) ")
return expr
}
Expand Down Expand Up @@ -711,4 +663,15 @@ public abstract class SqlFormatter(
override fun visitUnknown(expr: SqlExpression): SqlExpression {
throw DialectFeatureNotSupportedException("Unsupported expression type: ${expr.javaClass}")
}

protected fun writeSqlExpression(expr: SqlExpression) {
if (expr.removeBrackets) {
visit(expr)
} else {
write("(")
visit(expr)
removeLastBlank()
write(") ")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2018-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ktorm.support.postgresql

import java.sql.PreparedStatement
import java.sql.ResultSet

/**
* Custom PGObject class that can be used instead of the actual implementation. The actual implementation is not a
* JDK 9 module, thus we are not able to require it in module-info.java.
*
* The implementation accesses the postgresql API by reflection.
*/
@JvmInline
internal value class PgObject private constructor(internal val pgObject: Any) {

internal val type: String
get() = getTypeMethod.invoke(pgObject) as String

internal val value: String?
get() = getValueMethod.invoke(pgObject) as String?

internal companion object {
private val pgObjectClass = Class.forName("org.postgresql.util.PGobject")
private val pgObjectConstructor = pgObjectClass.getDeclaredConstructor()
private val setTypeMethod = pgObjectClass.getMethod("setType", String::class.java)
private val setValueMethod = pgObjectClass.getMethod("setValue", String::class.java)
private val getTypeMethod = pgObjectClass.getMethod("getType")
private val getValueMethod = pgObjectClass.getMethod("getValue")

internal fun fromPGobject(obj: Any): PgObject = PgObject(pgObjectClass.cast(obj))

internal operator fun invoke(type: String, value: String?): PgObject {
val pgObject = pgObjectConstructor.newInstance()
setTypeMethod.invoke(pgObject, type)
setValueMethod.invoke(pgObject, value)
return PgObject(pgObject)
}
}
}

internal fun PreparedStatement.setPgObject(parameterIndex: Int, ktormPgObject: PgObject) {
this.setObject(parameterIndex, ktormPgObject.pgObject)
}

internal fun ResultSet.getPgObject(columnIndex: Int): PgObject? {
val obj = this.getObject(columnIndex)
if (obj == null) {
return null
} else {
return PgObject.fromPGobject(obj)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public interface PostgreSqlExpressionVisitor : SqlExpressionVisitor {
is HStoreExpression -> visitHStore(expr)
is CubeExpression -> visitCube(expr)
is DefaultValueExpression -> visitDefaultValue(expr)
is TextSearchExpression -> visitTextSearch(expr)
else -> super.visitScalar(expr)
}

Expand Down Expand Up @@ -182,4 +183,20 @@ public interface PostgreSqlExpressionVisitor : SqlExpressionVisitor {
public fun <T : Any> visitDefaultValue(expr: DefaultValueExpression<T>): DefaultValueExpression<T> {
return expr
}

/**
* Function that visits a [TextSearchExpression].
*/
public fun <T : Any> visitTextSearch(expr: TextSearchExpression<T>): TextSearchExpression<T> {
val left = expr.left?.let {
visitScalar(it)
}
val right = visitScalar(expr.right)

if (left === expr.left && right === expr.right) {
return expr
} else {
return expr.copy(left = left, right = right)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,73 +173,35 @@ public open class PostgreSqlFormatter(
}

override fun visitILike(expr: ILikeExpression): ILikeExpression {
if (expr.left.removeBrackets) {
visit(expr.left)
} else {
write("(")
visit(expr.left)
removeLastBlank()
write(") ")
}

writeSqlExpression(expr.left)
writeKeyword("ilike ")

if (expr.right.removeBrackets) {
visit(expr.right)
} else {
write("(")
visit(expr.right)
removeLastBlank()
write(") ")
}
writeSqlExpression(expr.right)

return expr
}

override fun <T : Any> visitHStore(expr: HStoreExpression<T>): HStoreExpression<T> {
if (expr.left.removeBrackets) {
visit(expr.left)
} else {
write("(")
visit(expr.left)
removeLastBlank()
write(") ")
}

writeSqlExpression(expr.left)
writeKeyword("${expr.type} ")

if (expr.right.removeBrackets) {
visit(expr.right)
} else {
write("(")
visit(expr.right)
removeLastBlank()
write(") ")
}
writeSqlExpression(expr.right)

return expr
}

override fun <T : Any> visitCube(expr: CubeExpression<T>): CubeExpression<T> {
if (expr.left.removeBrackets) {
visit(expr.left)
} else {
write("(")
visit(expr.left)
removeLastBlank()
write(") ")
}

writeSqlExpression(expr.left)
writeKeyword("${expr.type} ")
writeSqlExpression(expr.right)

if (expr.right.removeBrackets) {
visit(expr.right)
} else {
write("(")
visit(expr.right)
removeLastBlank()
write(") ")
return expr
}

public override fun <T : Any> visitTextSearch(expr: TextSearchExpression<T>): TextSearchExpression<T> {
if (expr.left != null) {
writeSqlExpression(expr.left)
}
writeKeyword("${expr.type} ")
writeSqlExpression(expr.right)

return expr
}
Expand Down
Loading