Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use Map[String,String] to hold url options in constructors

  • Loading branch information...
commit 4839dc01dafcff53d145c9e04c4d47b3aa9e4388 1 parent af23ca0
@freels freels authored
View
6 config/test.conf
@@ -1,5 +1,9 @@
db {
+ hostname = "localhost"
username = "root"
password = ""
- url_options = "useUnicode=true&characterEncoding=UTF-8"
+ url_options = {
+ useUnicode = "true"
+ characterEncoding = "UTF-8"
+ }
}
View
4 src/main/scala/com/twitter/querulous/database/ApachePoolingDatabase.scala
@@ -13,7 +13,7 @@ class ApachePoolingDatabaseFactory(
checkConnectionHealthOnReservation: Boolean,
evictConnectionIfIdleFor: Duration) extends DatabaseFactory {
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = {
val pool = new ApachePoolingDatabase(
dbhosts,
dbname,
@@ -35,7 +35,7 @@ class ApachePoolingDatabase(
dbname: String,
username: String,
password: String,
- urlOptions: String,
+ urlOptions: Map[String, String],
minOpenConnections: Int,
maxOpenConnections: Int,
checkConnectionHealthWhenIdleFor: Duration,
View
2  src/main/scala/com/twitter/querulous/database/AutoDisablingDatabase.scala
@@ -6,7 +6,7 @@ import java.sql.{Connection, SQLException, SQLIntegrityConstraintViolationExcept
class AutoDisablingDatabaseFactory(databaseFactory: DatabaseFactory, disableErrorCount: Int, disableDuration: Duration) extends DatabaseFactory {
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = {
new AutoDisablingDatabase(
databaseFactory(dbhosts, dbname, username, password, urlOptions),
dbhosts.first,
View
6 src/main/scala/com/twitter/querulous/database/Database.scala
@@ -34,7 +34,7 @@ object DatabaseFactory {
}
trait DatabaseFactory {
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String): Database
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]): Database
def apply(dbhosts: List[String], dbname: String, username: String, password: String): Database =
apply(dbhosts, dbname, username, password, null)
@@ -57,9 +57,9 @@ trait Database {
}
}
- protected def url(dbhosts: List[String], dbname: String, urlOptions: String) = {
+ protected def url(dbhosts: List[String], dbname: String, urlOptions: Map[String, String]) = {
val dbnameSegment = if (dbname == null) "" else ("/" + dbname)
- val urlOptsSegment = if (urlOptions == null) "" else ("?" + urlOptions)
+ val urlOptsSegment = if (urlOptions == null) "" else ("?" + urlOptions.keys.map( k => k + "=" + urlOptions(k) ).mkString("&"))
"jdbc:mysql://" + dbhosts.mkString(",") + dbnameSegment + urlOptsSegment
}
}
View
2  src/main/scala/com/twitter/querulous/database/MemoizingDatabase.scala
@@ -5,7 +5,7 @@ import scala.collection.mutable
class MemoizingDatabaseFactory(databaseFactory: DatabaseFactory) extends DatabaseFactory {
private val databases = new mutable.HashMap[String, Database] with mutable.SynchronizedMap[String, Database]
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = synchronized {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = synchronized {
databases.getOrElseUpdate(
dbhosts.first + "/" + dbname,
databaseFactory(dbhosts, dbname, username, password, urlOptions))
View
4 src/main/scala/com/twitter/querulous/database/SingleConnectionDatabase.scala
@@ -5,12 +5,12 @@ import java.sql.{SQLException, Connection}
class SingleConnectionDatabaseFactory extends DatabaseFactory {
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = {
new SingleConnectionDatabase(dbhosts, dbname, username, password, urlOptions)
}
}
-class SingleConnectionDatabase(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String)
+class SingleConnectionDatabase(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String])
extends Database {
Class.forName("com.mysql.jdbc.Driver")
private val connectionFactory = new DriverManagerConnectionFactory(url(dbhosts, dbname, urlOptions), username, password)
View
2  src/main/scala/com/twitter/querulous/database/StatsCollectingDatabase.scala
@@ -6,7 +6,7 @@ class StatsCollectingDatabaseFactory(
databaseFactory: DatabaseFactory,
stats: StatsCollector) extends DatabaseFactory {
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = {
new StatsCollectingDatabase(databaseFactory(dbhosts, dbname, username, password, urlOptions), stats)
}
}
View
2  src/main/scala/com/twitter/querulous/database/TimingOutDatabase.scala
@@ -9,7 +9,7 @@ import net.lag.logging.Logger
class SqlDatabaseTimeoutException(msg: String) extends SQLException(msg)
class TimingOutDatabaseFactory(databaseFactory: DatabaseFactory, poolSize: Int, queueSize: Int, openTimeout: Duration, initialTimeout: Duration, maxConnections: Int) extends DatabaseFactory {
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = {
val dbLabel = if (dbname != null) dbname else "(null)"
new TimingOutDatabase(databaseFactory(dbhosts, dbname, username, password, urlOptions), dbhosts, dbLabel, poolSize, queueSize, openTimeout, initialTimeout, maxConnections)
View
2  src/main/scala/com/twitter/querulous/evaluator/AutoDisablingQueryEvaluator.scala
@@ -18,7 +18,7 @@ class AutoDisablingQueryEvaluatorFactory(
disableDuration)
}
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = {
chainEvaluator(queryEvaluatorFactory(dbhosts, dbname, username, password, urlOptions))
}
}
View
13 src/main/scala/com/twitter/querulous/evaluator/QueryEvaluator.scala
@@ -32,15 +32,15 @@ object QueryEvaluator extends QueryEvaluatorFactory {
new StandardQueryEvaluatorFactory(databaseFactory, queryFactory)
}
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = {
createEvaluatorFactory()(dbhosts, dbname, username, password, urlOptions)
}
}
trait QueryEvaluatorFactory {
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String): QueryEvaluator
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]): QueryEvaluator
- def apply(dbhost: String, dbname: String, username: String, password: String, urlOptions: String): QueryEvaluator = {
+ def apply(dbhost: String, dbname: String, username: String, password: String, urlOptions: Map[String, String]): QueryEvaluator = {
apply(List(dbhost), dbname, username, password, urlOptions)
}
@@ -63,10 +63,11 @@ trait QueryEvaluatorFactory {
def apply(config: ConfigMap): QueryEvaluator = {
apply(
config.getList("hostname").toList,
- config("database"),
+ config.getString("database").getOrElse(null),
config("username"),
- config("password"),
- config.getString("url_options").getOrElse(null)
+ config.getString("password").getOrElse(null),
+ // this is so lame, why do I have to cast this back?
+ config.getConfigMap("url_options").map(_.asMap.asInstanceOf[Map[String, String]]).getOrElse(null)
)
}
}
View
2  src/main/scala/com/twitter/querulous/evaluator/StandardQueryEvaluator.scala
@@ -10,7 +10,7 @@ class StandardQueryEvaluatorFactory(
databaseFactory: DatabaseFactory,
queryFactory: QueryFactory) extends QueryEvaluatorFactory {
- def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: String) = {
+ def apply(dbhosts: List[String], dbname: String, username: String, password: String, urlOptions: Map[String, String]) = {
val database = databaseFactory(dbhosts, dbname, username, password, urlOptions)
new StandardQueryEvaluator(database, queryFactory)
}
View
9 src/test/scala/com/twitter/querulous/integration/QuerySpec.scala
@@ -14,12 +14,9 @@ class QuerySpec extends Specification {
import TestEvaluator._
val config = Configgy.config.configMap("db")
- val username = config("username")
- val password = config("password")
- val urlOptions = config("url_options")
"Query" should {
- val queryEvaluator = testEvaluatorFactory("localhost", null, username, password, urlOptions)
+ val queryEvaluator = testEvaluatorFactory(config)
"with too many arguments" >> {
queryEvaluator.select("SELECT 1 FROM DUAL WHERE 1 IN (?)", 1, 2, 3) { r => 1 } must throwA[TooManyQueryParametersException]
@@ -34,10 +31,10 @@ class QuerySpec extends Specification {
}
"be backwards compatible" >> {
- val noOpts = testEvaluatorFactory("localhost", null, username, password)
+ val noOpts = testEvaluatorFactory("localhost", null, config("username"), config("password"))
noOpts.select("SELECT 1 FROM DUAL WHERE 1 IN (?)", List(1, 2, 3))(_.getInt(1)).toList mustEqual List(1)
- val noDBNameOrOpts = testEvaluatorFactory("localhost", username, password)
+ val noDBNameOrOpts = testEvaluatorFactory("localhost", config("username"), config("password"))
noDBNameOrOpts.select("SELECT 1 FROM DUAL WHERE 1 IN (?)", List(1, 2, 3))(_.getInt(1)).toList mustEqual List(1)
}
}
View
2  src/test/scala/com/twitter/querulous/unit/QueryEvaluatorSpec.scala
@@ -21,7 +21,7 @@ class QueryEvaluatorSpec extends Specification with JMocker with ClassMocker {
val config = Configgy.config.configMap("db")
val username = config("username")
val password = config("password")
- val urlOptions = config("url_options")
+ val urlOptions = config("url_options").asInstanceOf[Map[String, String]]
"QueryEvaluator" should {
val queryEvaluator = testEvaluatorFactory("localhost", "db_test", username, password, urlOptions)
Please sign in to comment.
Something went wrong with that request. Please try again.