Skip to content

Commit

Permalink
fix #122 - provide configuration option to disable the query probing
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil committed Jan 25, 2016
1 parent d029bef commit 130919d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -840,6 +840,7 @@ object db extends JdbcSource[MySQLDialect, SnakeCase]

application.properties
```
db.queryProbing=true
db.dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
db.dataSource.url=jdbc:mysql://host/database
db.dataSource.user=root
Expand Down Expand Up @@ -870,6 +871,7 @@ object db extends JdbcSource[PostgresDialect, SnakeCase]

application.properties
```
db.queryProbing=true
db.dataSourceClassName=org.postgresql.ds.PGSimpleDataSource
db.dataSource.user=root
db.dataSource.password=root
Expand Down Expand Up @@ -901,6 +903,7 @@ object db extends MysqlAsyncSource[SnakeCase]

application.properties
```
db.queryProbing=true
db.host=host
db.port=3306
db.user=root
Expand Down Expand Up @@ -931,6 +934,7 @@ object db extends PostgresAsyncSource[SnakeCase]

application.properties
```
db.queryProbing=true
db.host=host
db.port=5432
db.user=root
Expand Down Expand Up @@ -961,6 +965,7 @@ object db extends FinagleMysqlSource[SnakeCase]

application.properties
```
db.queryProbing=true
db.dest=localhost:3306
db.user=root
db.password=root
Expand Down Expand Up @@ -1001,6 +1006,7 @@ The configurations are set using runtime reflection on the [`Cluster.builder`](h

application.properties
```
db.queryProbing=true
db.keyspace=quill_test
db.preparedStatementCacheSize=1000
db.session.contactPoint=127.0.0.1
Expand Down
Expand Up @@ -12,6 +12,7 @@ import scala.concurrent.duration._
import scala.reflect.api.Trees
import io.getquill.util.Cache
import java.io.Closeable
import io.getquill.util.Config

trait ResolveSourceMacro {
val c: Context
Expand All @@ -28,15 +29,28 @@ trait ResolveSourceMacro {

private def resolve[T <: Source[_, _]](tpe: Type)(implicit t: ClassTag[T]): Option[Source[_, _]] = {
val sourceName = tpe.typeSymbol.name.decodedName.toString
resolve(sourceName, baseClasses[T](tpe)) match {
case (None, errors) =>
c.warning(NoPosition, s"Can't load the source '$sourceName' at compile time. The sql probing is disabled for the source. Trace: \n${errors.mkString("\n")}")
isEnabled(sourceName) match {
case true =>
resolve(sourceName, baseClasses[T](tpe)) match {
case (None, errors) =>
c.warning(NoPosition, s"Can't load the source '$sourceName' at compile time. The sql probing is disabled for the source. Trace: \n${errors.mkString("\n")}")
None
case (some, errors) =>
some
}
case false =>
None
case (some, errors) =>
some
}
}

private def isEnabled(sourceName: String) = {
val config = Config(sourceName)
if (config.hasPath("queryProbing"))
config.getBoolean("queryProbing")
else
true
}

private def baseClasses[T](tpe: Type)(implicit ct: ClassTag[T]): List[Class[Any]] =
tpe.baseClasses.map(_.asClass.fullName)
.flatMap(name => List(loadClass(name), loadClass(name + "$")).flatten)
Expand Down
9 changes: 2 additions & 7 deletions quill-core/src/main/scala/io/getquill/source/Source.scala
Expand Up @@ -4,6 +4,7 @@ import scala.reflect.ClassTag
import scala.util.DynamicVariable
import com.typesafe.config.ConfigFactory
import java.io.Closeable
import io.getquill.util.Config

abstract class Source[R: ClassTag, S: ClassTag] extends Closeable {

Expand All @@ -27,13 +28,7 @@ abstract class Source[R: ClassTag, S: ClassTag] extends Closeable {
getClass.getSimpleName.replaceAllLiterally("$", "")
}

protected def config = {
val factory = ConfigFactory.load(getClass.getClassLoader)
if (factory.hasPath(configPrefix))
factory.getConfig(configPrefix)
else
ConfigFactory.empty
}
protected def config = Config(configPrefix)
}

object Source {
Expand Down
14 changes: 14 additions & 0 deletions quill-core/src/main/scala/io/getquill/util/Config.scala
@@ -0,0 +1,14 @@
package io.getquill.util

import com.typesafe.config.ConfigFactory

object Config {

def apply(configPrefix: String) = {
val factory = ConfigFactory.load(getClass.getClassLoader)
if (factory.hasPath(configPrefix))
factory.getConfig(configPrefix)
else
ConfigFactory.empty
}
}
Expand Up @@ -10,6 +10,13 @@ class ResolveSourceMacroSpec extends Spec {
"buggySource.run(qr1.delete)" must compile
}

"doesn't warn if query probing is disabled and the source can't be resolved at compile time" in {
System.setProperty("disabledSource.queryProbing", "false")
object disabledSource extends MirrorSourceTemplate
disabledSource.run(qr1.delete)
()
}

"warns if the probe fails" in {
case class Fail()
"io.getquill.source.mirror.mirrorSource.run(query[Fail].delete)" must compile
Expand Down

0 comments on commit 130919d

Please sign in to comment.