Skip to content
Permalink
Browse files

Use application.conf for Slick settings.

- The system properties scala.slick.ansiDump and scala.slick.dumpPaths
  become config keys slick.ansiDump and slick.dumpPaths.

- slick.ansiDump is enabled by default in testkit/test.

- Drivers get their own Config object. This can be used in the future to
  enable driver-specific features that require a certain database
  version or configuration.
  • Loading branch information
szeiger committed Aug 6, 2014
1 parent f0bc906 commit 369ad732467a547db79ed3ca7237636bd915fecc
@@ -239,7 +239,7 @@ object SlickBuild extends Build {
testGrouping in DocTest <<= definedTests in DocTest map partitionTests,
parallelExecution in Test := false,
fork in run := true,
javaOptions in run += "-Dscala.slick.ansiDump=true",
javaOptions in run += "-Dslick.ansiDump=true",
//javaOptions in run += "-verbose:gc",
compile in Test ~= { a =>
// Delete classes in "compile" packages after compiling. (Currently only scala.slick.test.compile.NestedShapeTest)
@@ -0,0 +1 @@
slick.ansiDump = true
@@ -0,0 +1,8 @@
# These settings are read by scala.slick.util.SlickConfig
slick {
# Use ANSI color sequences in tree dumps
ansiDump = false

# Dump individual Select and Ref nodes instead of a single Path
dumpPaths = false
}
@@ -2,11 +2,10 @@ package scala.slick.ast

import scala.language.existentials
import scala.slick.SlickException
import scala.slick.util.{Logging, Dumpable, DumpInfo}
import scala.slick.util.{Logging, Dumpable, DumpInfo, GlobalConfig}
import TypeUtil.typeToTypeUtil
import Util._
import scala.reflect.ClassTag
import scala.sys.BooleanProp

/**
* A node in the query AST.
@@ -114,7 +113,7 @@ trait Node extends Dumpable {
val tpe = nodePeekType
val ch = this match {
// Omit path details unless dumpPaths is set
case Path(l @ (_ :: _ :: _)) if !Node.dumpPaths => Vector.empty
case Path(l @ (_ :: _ :: _)) if !GlobalConfig.dumpPaths => Vector.empty
case _ => nodeChildNames.zip(nodeChildren).toVector
}
DumpInfo(objName, mainInfo, if(tpe != UnassignedType) ": " + tpe.toString else "", ch)
@@ -139,8 +138,6 @@ trait SimplyTypedNode extends Node {
object Node extends Logging {
private def logType(n: Node): Unit =
logger.debug("Assigned type "+n.nodePeekType+" to node "+n)

private val dumpPaths: Boolean = BooleanProp.valueIsTrue("scala.slick.dumpPaths")
}

trait TypedNode extends Node with Typed {
@@ -1,10 +1,12 @@
package scala.slick.profile

import scala.language.{higherKinds, implicitConversions}
import scala.language.{higherKinds, implicitConversions, existentials}
import scala.slick.compiler.QueryCompiler
import scala.slick.backend.DatabaseComponent
import scala.slick.ast._
import scala.slick.lifted._
import com.typesafe.config.{ConfigFactory, Config}
import scala.slick.util.GlobalConfig

/**
* The basic functionality that has to be implemented by all drivers.
@@ -92,6 +94,29 @@ trait BasicDriver extends BasicProfile {
cl.substring(19, cl.length-1)
else super.toString
}

/** The configuration for this driver, loaded via [[loadDriverConfig]]. */
final lazy val driverConfig: Config = loadDriverConfig

/** Load the configuration for this driver. This can be overridden in user-defined driver
* subclasses to load different configurations.
*
* The default implementation does a breadth-first search in the supertype hierarchy of the
* runtime class until it finds a class or trait matching "scala.slick.driver.XXXDriver"
* where XXX is an arbitrary name, and then returns the path "slick.driver.XXX" from the
* application config, if it exists, otherwise an empty Config object. */
protected[this] def loadDriverConfig: Config = {
def findConfigName(classes: Vector[Class[_]]): Option[String] =
classes.iterator.map { cl =>
val n = cl.getName
if(n.startsWith("scala.slick.driver.") && n.endsWith("Driver")) Some(n.substring(19, n.length-6))
else None
}.find(_.isDefined).getOrElse {
val parents = classes.flatMap { cl => Option(cl.getSuperclass) ++: cl.getInterfaces.toVector }
if(parents.isEmpty) None else findConfigName(parents)
}
GlobalConfig.driverConfig(findConfigName(Vector(getClass)).get)
}
}

trait BasicInvokerComponent { driver: BasicDriver =>
@@ -0,0 +1,21 @@
package scala.slick.util

import com.typesafe.config.{Config, ConfigFactory}

/** Singleton object with Slick's configuration, loaded from the application config.
* This includes configuration for the global driver objects and settings for debug logging. */
object GlobalConfig {
private[this] val config = ConfigFactory.load()

/** Dump individual `Select` and `Ref` nodes instead of a single `Path` */
val dumpPaths = config.getBoolean("slick.dumpPaths")

/** Use ANSI color sequences in tree dumps */
val ansiDump = config.getBoolean("slick.ansiDump")

/** Get a `Config` object for a Slick driver */
def driverConfig(name: String): Config = {
val path = "slick.driver." + name
if(config.hasPath(path)) config.getConfig(path) else ConfigFactory.empty()
}
}
@@ -1,13 +1,11 @@
package scala.slick.util

import java.io.{OutputStreamWriter, StringWriter, PrintWriter}
import scala.sys.BooleanProp

/** Create a readable printout of a tree. */
object TreeDump {
private[util] val ansiDump = BooleanProp.valueIsTrue("scala.slick.ansiDump")
private[util] val (normal, green, yellow, blue, cyan) = {
if(ansiDump) ("\u001B[0m", "\u001B[32m", "\u001B[33m", "\u001B[34m", "\u001B[36m")
if(GlobalConfig.ansiDump) ("\u001B[0m", "\u001B[32m", "\u001B[33m", "\u001B[34m", "\u001B[36m")
else ("", "", "", "", "")
}

0 comments on commit 369ad73

Please sign in to comment.
You can’t perform that action at this time.