Skip to content

Commit 0408c7e

Browse files
committed
feat: implemented jdbc driver methods
1 parent 0c8861a commit 0408c7e

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/main/scala/dev/mongocamp/driver/mongodb/jdbc/MongoJdbcDriver.scala

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
package dev.mongocamp.driver.mongodb.jdbc
22

3-
import com.mongodb.MongoCredential.createCredential
43
import com.vdurmont.semver4j.Semver
54
import dev.mongocamp.driver.mongodb.BuildInfo
6-
import org.mongodb.scala.MongoClient.DEFAULT_CODEC_REGISTRY
7-
import org.mongodb.scala.{ConnectionString, MongoClient, MongoClientSettings, MongoCredential}
5+
import dev.mongocamp.driver.mongodb.database.{ DatabaseProvider, MongoConfig }
6+
import org.mongodb.scala.{ ConnectionString, ServerAddress }
87

9-
import java.sql.{Connection, DriverPropertyInfo}
8+
import java.sql.{ Connection, DriverPropertyInfo }
109
import java.util.Properties
1110
import java.util.logging.Logger
11+
import scala.jdk.CollectionConverters.CollectionHasAsScala
1212

1313
class MongoJdbcDriver extends java.sql.Driver {
1414

1515
private lazy val semVer = new Semver(BuildInfo.version)
1616

17-
18-
/**
19-
* Connect to the database using a URL like :
20-
* jdbc:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
21-
* The URL excepting the jdbc: prefix is passed as it is to the MongoDb native Java driver.
22-
*/
17+
/** Connect to the database using a URL like : jdbc:mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] The
18+
* URL excepting the jdbc: prefix is passed as it is to the MongoDb native Java driver.
19+
*/
2320
override def connect(url: String, info: Properties): Connection = {
2421
if (url == null || !acceptsURL(url)) {
2522
return null
2623
}
2724

2825
val connectionUrl = url.replaceFirst("^jdbc:", "")
29-
val username = info.getProperty("user")
30-
val password = info.getProperty("password")
31-
32-
val builder = MongoClientSettings
33-
.builder()
34-
.applyConnectionString(new ConnectionString(connectionUrl))
35-
.codecRegistry(DEFAULT_CODEC_REGISTRY)
36-
37-
if (!username.equalsIgnoreCase("") && !password.equalsIgnoreCase("")) {
38-
val credential: MongoCredential = createCredential(username, "admin", password.toCharArray)
39-
builder.credential(credential).build()
40-
}
41-
42-
val client: MongoClient = MongoClient(builder.build())
43-
new MongoJdbcConnection(client)
26+
val username = Option(info.getProperty("user")).filter(_.trim.nonEmpty)
27+
val password = Option(info.getProperty("password")).filter(_.trim.nonEmpty)
28+
29+
val string = new ConnectionString(connectionUrl)
30+
val provider = DatabaseProvider(
31+
MongoConfig(
32+
string.getDatabase,
33+
MongoConfig.DefaultHost,
34+
MongoConfig.DefaultPort,
35+
string.getApplicationName,
36+
username,
37+
password,
38+
string.getDatabase,
39+
serverAddressList = string.getHosts.asScala.toList.map(h => new ServerAddress(h))
40+
)
41+
)
42+
new MongoJdbcConnection(provider)
4443
}
4544

4645
override def acceptsURL(url: String): Boolean = {

0 commit comments

Comments
 (0)