in exposed-core/src/main/kotlin/org/jetbrains/exposed/v1/core/AbstractQuery.kt [206:281]
open fun prepareSQL(builder: QueryBuilder): String {
require(set.fields.isNotEmpty()) { "Can't prepare SELECT statement without columns or expressions to retrieve" }
builder {
comments[CommentPosition.FRONT]?.let { comment ->
append("/*$comment*/ ")
}
append("SELECT ")
comments[CommentPosition.AFTER_SELECT]?.let { comment ->
append("/*$comment*/ ")
}
if (count) {
append("COUNT(*)")
} else {
if (distinct) {
append("DISTINCT ")
}
distinctOn
?.takeIf { it.isNotEmpty() }
?.let { columns ->
columns.appendTo(prefix = "DISTINCT ON (", postfix = ") ") { append(it) }
}
set.realFields.appendTo { +it }
}
@OptIn(InternalApi::class)
if (set.source != Table.Dual || currentDialect.supportsDualTableConcept) {
append(" FROM ")
set.source.describe(currentTransaction(), this)
}
where?.let {
append(" WHERE ")
+it
}
if (!count) {
if (groupedByColumns.isNotEmpty()) {
append(" GROUP BY ")
groupedByColumns.appendTo {
+((it as? IExpressionAlias<*>)?.aliasOnlyExpression() ?: it)
}
}
having?.let {
append(" HAVING ")
append(it)
}
if (orderByExpressions.isNotEmpty()) {
append(" ORDER BY ")
orderByExpressions.appendTo { (expression, sortOrder) ->
currentDialect.dataTypeProvider.precessOrderByClause(this, expression, sortOrder)
}
}
if (limit != null || offset > 0) {
append(" ")
append(currentDialect.functionProvider.queryLimitAndOffset(limit, offset, orderByExpressions.isNotEmpty()))
}
}
if (isForUpdate()) {
forUpdate?.apply {
append(" $querySuffix")
}
}
comments[CommentPosition.BACK]?.let { comment ->
append(" /*$comment*/")
}
}
return builder.toString()
}