com.crobox.clickhouse.dsl.misc.QueryImprovements.scala Maven / Gradle / Ivy
package com.crobox.clickhouse.dsl.misc
import com.crobox.clickhouse.dsl.Query
import com.crobox.clickhouse.dsl.execution.{QueryExecutor, QueryResult}
import com.crobox.clickhouse.dsl.language.{ClickhouseTokenizerModule, TokenizeContext}
import com.typesafe.scalalogging.LazyLogging
import spray.json.JsonReader
import scala.concurrent.{ExecutionContext, Future}
object QueryImprovements extends LazyLogging {
val tokenizer: ClickhouseTokenizerModule = new ClickhouseTokenizerModule {}
implicit class QueryImpr(query: Query) {
def execute[V: JsonReader](
implicit executionContext: ExecutionContext,
queryExecutor: QueryExecutor
): Future[QueryResult[V]] =
queryExecutor.execute(query)
def executeWithLogging[V: JsonReader](debug: Boolean)(
implicit executionContext: ExecutionContext,
queryExecutor: QueryExecutor
): Future[QueryResult[V]] = {
if (debug)
logger.info(
s"SQL: ${tokenizer.toSql(query.internalQuery)(TokenizeContext(queryExecutor.serverVersion))}"
)
queryExecutor.execute(query)
}
def executeWithLogging[V: JsonReader](traceId: String)(
implicit executionContext: ExecutionContext,
queryExecutor: QueryExecutor
): Future[QueryResult[V]] = {
logger.info(
s"[$traceId] ${tokenizer.toSql(query.internalQuery)(TokenizeContext(queryExecutor.serverVersion))}"
)
queryExecutor.execute(query)
}
def executeWithLogging[V: JsonReader](traceId: Option[String])(
implicit executionContext: ExecutionContext,
queryExecutor: QueryExecutor
): Future[QueryResult[V]] = {
traceId.foreach(
id =>
logger.info(
s"[$id] ${tokenizer.toSql(query.internalQuery)(TokenizeContext(queryExecutor.serverVersion))}"
)
)
queryExecutor.execute(query)
}
def executeWithLogging[V: JsonReader](
implicit executionContext: ExecutionContext,
queryExecutor: QueryExecutor
): Future[QueryResult[V]] = {
logger.info(
s"SQL: ${tokenizer.toSql(query.internalQuery)(TokenizeContext(queryExecutor.serverVersion))}"
)
queryExecutor.execute(query)
}
}
}