Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Order by distance function for JetBrains Exposed.
import org.jetbrains.exposed.sql.*
fun Query.orderByDistance(
columnX: Column<Double>,
columnY: Column<Double>,
compareX: Double,
compareY: Double,
order: SortOrder = SortOrder.ASC
): Query = this.orderBy(object : Expression<Int>() {
override fun toSQL(queryBuilder: QueryBuilder): String = buildString {
// Distance function
append("ST_DISTANCE(") // st_distance start
// Stored point
append("POINT(") // point start
append(columnX.toSQL(queryBuilder)) // point arg1
append(",") // point divide
append(columnY.toSQL(queryBuilder)) // point arg2
append(")") // point end
append(",") // st_distance divide
// Compare point
append("POINT(") // point start
append(compareX) // point arg1
append(",") // point divide
append(compareY) // point arg2
append(")") // point end
append(")") // st_distance end
}
} to order)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.