Skip to content

Commit c6fe36b

Browse files
committed
feat(jdbc): start implementing jdbc driver for mongodb
1 parent 9e73ce1 commit c6fe36b

File tree

9 files changed

+970
-164
lines changed

9 files changed

+970
-164
lines changed

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5"
9797

9898
libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0"
9999

100+
libraryDependencies += ("com.vdurmont" % "semver4j" % "3.1.0")
101+
100102
buildInfoPackage := "dev.mongocamp.driver.mongodb"
101103

102104
buildInfoOptions += BuildInfoOption.BuildTime

scalastyle-config.xml

Lines changed: 0 additions & 117 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dev.mongocamp.driver.mongodb.jdbc.MongoJdbcDriver

src/main/scala/dev/mongocamp/driver/mongodb/database/MongoConfig.scala

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,29 @@ package dev.mongocamp.driver.mongodb.database
33
import java.util.concurrent.TimeUnit
44
import com.mongodb.MongoCompressor
55
import com.mongodb.MongoCredential.createCredential
6-
import com.mongodb.event.{ CommandListener, ConnectionPoolListener }
6+
import com.mongodb.event.{CommandListener, ConnectionPoolListener}
77
import dev.mongocamp.driver.mongodb.database.MongoConfig._
8-
import com.typesafe.config.{ Config, ConfigFactory }
8+
import com.typesafe.config.{Config, ConfigFactory}
99
import org.mongodb.scala.connection._
10-
import org.mongodb.scala.{ MongoClientSettings, MongoCredential, ServerAddress }
10+
import org.mongodb.scala.{MongoClientSettings, MongoCredential, ServerAddress}
1111

1212
import scala.jdk.CollectionConverters._
1313
import scala.collection.mutable.ArrayBuffer
1414

1515
case class MongoConfig(
16-
database: String,
17-
host: String = DefaultHost,
18-
port: Int = DefaultPort,
19-
applicationName: String = DefaultApplicationName,
20-
userName: Option[String] = None,
21-
password: Option[String] = None,
22-
authDatabase: String = DefaultAuthenticationDatabaseName,
23-
poolOptions: MongoPoolOptions = MongoPoolOptions(),
24-
compressors: List[String] = List(),
25-
connectionPoolListener: List[ConnectionPoolListener] = List(),
26-
commandListener: List[CommandListener] = List(),
27-
customClientSettings: Option[MongoClientSettings] = None
28-
) {
16+
database: String,
17+
host: String = DefaultHost,
18+
port: Int = DefaultPort,
19+
applicationName: String = DefaultApplicationName,
20+
userName: Option[String] = None,
21+
password: Option[String] = None,
22+
authDatabase: String = DefaultAuthenticationDatabaseName,
23+
poolOptions: MongoPoolOptions = MongoPoolOptions(),
24+
compressors: List[String] = List(),
25+
connectionPoolListener: List[ConnectionPoolListener] = List(),
26+
commandListener: List[CommandListener] = List(),
27+
customClientSettings: Option[MongoClientSettings] = None
28+
) {
2929

3030
val clientSettings: MongoClientSettings = {
3131
if (customClientSettings.isDefined) {
@@ -81,71 +81,88 @@ case class MongoConfig(
8181
trait ConfigHelper {
8282
val conf: Config = ConfigFactory.load()
8383

84-
def stringConfig(configPath: String, key: String, default: String = ""): Option[String] =
84+
def stringConfig(configPath: String, key: String, default: String = ""): Option[String] = {
8585
if (conf.hasPath("%s.%s".format(configPath, key))) {
8686
val str = conf.getString("%s.%s".format(configPath, key))
87-
if (str.nonEmpty)
87+
if (str.nonEmpty) {
8888
Some(str)
89-
else
89+
}
90+
else {
9091
None
92+
}
9193
}
92-
else if (default.nonEmpty)
94+
else if (default.nonEmpty) {
9395
Some(default)
94-
else
96+
}
97+
else {
9598
None
99+
}
100+
}
96101

97-
def intConfig(configPath: String, key: String, default: Int = 0): Int =
98-
if (conf.hasPath("%s.%s".format(configPath, key)))
102+
def intConfig(configPath: String, key: String, default: Int = 0): Int = {
103+
if (conf.hasPath("%s.%s".format(configPath, key))) {
99104
conf.getInt("%s.%s".format(configPath, key))
100-
else
105+
}
106+
else {
101107
default
108+
}
109+
}
102110

103-
def booleanConfig(configPath: String, key: String, default: Boolean = false): Boolean =
104-
if (conf.hasPath("%s.%s".format(configPath, key)))
111+
def booleanConfig(configPath: String, key: String, default: Boolean = false): Boolean = {
112+
if (conf.hasPath("%s.%s".format(configPath, key))) {
105113
conf.getBoolean("%s.%s".format(configPath, key))
106-
else
114+
}
115+
else {
107116
default
117+
}
118+
}
108119
}
109120

110121
object MongoConfig extends ConfigHelper {
111-
val DefaultHost = "127.0.0.1"
112-
val DefaultPort = 27017
122+
val DefaultHost = "127.0.0.1"
123+
val DefaultPort = 27017
113124
val DefaultAuthenticationDatabaseName = "admin"
114-
val DefaultApplicationName = "mongocampdb-app"
125+
val DefaultApplicationName = "mongocampdb-app"
115126

116-
val DefaultPoolMaxConnectionIdleTime = 60
117-
val DefaultPoolMaxSize = 50
118-
val DefaultPoolMinSize = 0
119-
val DefaultPoolMaxWaitQueueSize = 500
127+
val DefaultPoolMaxConnectionIdleTime = 60
128+
val DefaultPoolMaxSize = 50
129+
val DefaultPoolMinSize = 0
130+
val DefaultPoolMaxWaitQueueSize = 500
120131
val DefaultPoolMaintenanceInitialDelay = 0
121132

122133
val ComressionSnappy = "snappy"
123-
val ComressionZlib = "zlib"
124-
val ComressionZstd = "zstd"
134+
val ComressionZlib = "zlib"
135+
val ComressionZstd = "zstd"
125136

126137
val DefaultConfigPathPrefix = "mongodb"
127138

128139
def fromPath(configPath: String = DefaultConfigPathPrefix): MongoConfig = {
129140

130-
def poolOptionsConfig(key: String, default: Int): Int =
131-
if (conf.hasPath("%s.pool.%s".format(configPath, key)))
141+
def poolOptionsConfig(key: String, default: Int): Int = {
142+
if (conf.hasPath("%s.pool.%s".format(configPath, key))) {
132143
conf.getInt("%s.pool.%s".format(configPath, key))
133-
else
144+
}
145+
else {
134146
default
147+
}
148+
}
135149

136150
val port: Int = intConfig(configPath, "port", DefaultPort)
137151

138-
val compressors: List[String] =
139-
if (conf.hasPath("%s.compressors".format(configPath)))
152+
val compressors: List[String] = {
153+
if (conf.hasPath("%s.compressors".format(configPath))) {
140154
conf.getStringList("%s.compressors".format(configPath)).asScala.toList
141-
else
155+
}
156+
else {
142157
List()
158+
}
159+
}
143160

144-
val host = stringConfig(configPath, "host", DefaultHost).get
145-
val database = stringConfig(configPath, "database").get
146-
val userName = stringConfig(configPath, "userName")
147-
val password = stringConfig(configPath, "password")
148-
val authDatabase = stringConfig(configPath, "authDatabase", DefaultAuthenticationDatabaseName).get
161+
val host = stringConfig(configPath, "host", DefaultHost).get
162+
val database = stringConfig(configPath, "database").get
163+
val userName = stringConfig(configPath, "userName")
164+
val password = stringConfig(configPath, "password")
165+
val authDatabase = stringConfig(configPath, "authDatabase", DefaultAuthenticationDatabaseName).get
149166
val applicationName = stringConfig(configPath, "applicationName", DefaultApplicationName).get
150167

151168
val poolOptions = MongoPoolOptions(

0 commit comments

Comments
 (0)