Skip to content

Commit

Permalink
feat: compatibility for scala 2.12
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadStingray committed Oct 17, 2024
1 parent 2ecc276 commit 8a39da8
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 39 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ developers := List(

licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html"))

crossScalaVersions := Seq("2.13.13", "2.12.17")
crossScalaVersions := Seq("2.13.15", "2.12.20")

scalaVersion := crossScalaVersions.value.head
scalaVersion := crossScalaVersions.value.last

scalacOptions += "-deprecation"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.mongocamp.driver.mongodb.schema

import io.circe.Decoder.Result
import io.circe.{Decoder, Encoder, HCursor, Json}
import jdk.internal.reflect.Reflection
import org.bson.types.ObjectId
import org.joda.time.DateTime
import org.mongodb.scala.Document

import java.util.Date

trait CirceProductSchema {

def productElementNames(internalProduct: Product): Iterator[String] = {
(internalProduct.getClass.getDeclaredFields ++ internalProduct.getClass.getFields).map(_.getName).iterator
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.mongocamp.driver.mongodb.schema

import io.circe.Decoder.Result
import io.circe.{ Decoder, Encoder, HCursor, Json }
import org.bson.types.ObjectId
import org.joda.time.DateTime
import org.mongodb.scala.Document

import java.util.Date

trait CirceProductSchema {

def productElementNames(internalProduct: Product): Iterator[String] = {
internalProduct.productElementNames
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package dev.mongocamp.driver.mongodb.jdbc

import com.vdurmont.semver4j.Semver
import dev.mongocamp.driver.mongodb.BuildInfo
import dev.mongocamp.driver.mongodb.database.{ DatabaseProvider, MongoConfig }
import org.mongodb.scala.{ ConnectionString, ServerAddress }
import dev.mongocamp.driver.mongodb.database.{DatabaseProvider, MongoConfig}
import org.mongodb.scala.{ConnectionString, ServerAddress}

import java.sql.{ Connection, DriverPropertyInfo }
import java.sql.{Connection, DriverPropertyInfo}
import java.util.Properties
import java.util.logging.Logger
import scala.jdk.CollectionConverters.CollectionHasAsScala
import scala.jdk.CollectionConverters._

class MongoJdbcDriver extends java.sql.Driver {
private val propertyInfoHelper = new MongodbJdbcDriverPropertyInfoHelper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package dev.mongocamp.driver.mongodb.jdbc.resultSet

import dev.mongocamp.driver.mongodb.MongoDAO
import dev.mongocamp.driver.mongodb.bson.BsonConverter
import org.mongodb.scala.bson.{BsonArray, BsonBoolean, BsonDateTime, BsonInt32, BsonInt64, BsonNull, BsonNumber, BsonObjectId, BsonString}
import org.mongodb.scala.bson.{ BsonArray, BsonBoolean, BsonDateTime, BsonInt32, BsonInt64, BsonNull, BsonNumber, BsonObjectId, BsonString }
import org.mongodb.scala.bson.collection.immutable.Document

import java.io.{InputStream, Reader}
import java.net.{URI, URL}
import java.{sql, util}
import java.sql.{Blob, Clob, Date, NClob, Ref, ResultSet, ResultSetMetaData, RowId, SQLException, SQLWarning, SQLXML, Statement, Time, Timestamp}
import java.io.{ InputStream, Reader }
import java.net.{ URI, URL }
import java.{ sql, util }
import java.sql.{ Blob, Clob, Date, NClob, Ref, ResultSet, ResultSetMetaData, RowId, SQLException, SQLWarning, SQLXML, Statement, Time, Timestamp }
import java.util.Calendar
import dev.mongocamp.driver.mongodb._
import dev.mongocamp.driver.mongodb.jdbc.MongoJdbcCloseable

import java.nio.charset.StandardCharsets
import scala.util.Try

class MongoDbResultSet(collectionDao: MongoDAO[Document], data: List[Document], queryTimeOut: Int) extends ResultSet with MongoJdbcCloseable {
private var currentRow: Document = _
Expand Down Expand Up @@ -75,9 +76,9 @@ class MongoDbResultSet(collectionDao: MongoDAO[Document], data: List[Document],
checkClosed()
val value = currentRow.getValue(metaData.getColumnName(columnIndex))
value match {
case b : BsonInt32 => b.longValue()
case b : BsonInt64 => b.longValue()
case _ => Option(value).flatMap(_.toString.toLongOption).getOrElse(0)
case b: BsonInt32 => b.longValue()
case b: BsonInt64 => b.longValue()
case _ => Option(value).flatMap(v => Try(v.toString.toLong).toOption).getOrElse(0)
}
}

Expand Down Expand Up @@ -632,7 +633,7 @@ class MongoDbResultSet(collectionDao: MongoDAO[Document], data: List[Document],

override def getRef(columnLabel: String): Ref = sqlFeatureNotSupported()

override def updateRef(columnIndex: Int, x: Ref): Unit = sqlFeatureNotSupported()
override def updateRef(columnIndex: Int, x: Ref): Unit = sqlFeatureNotSupported()

override def updateRef(columnLabel: String, x: Ref): Unit = sqlFeatureNotSupported()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ case class MongoPreparedStatement(connection: MongoJdbcConnection) extends Calla
val collectionName = Option(queryHolder.getCollection).map(c => connection.getDatabaseProvider.dao(c))
if (!sql.toLowerCase().contains("_id")){
response = response.map(doc => {
val newDoc = Document(doc - "_id")
val newDoc = doc - "_id"
newDoc
})
}
Expand Down Expand Up @@ -507,7 +507,7 @@ case class MongoPreparedStatement(connection: MongoJdbcConnection) extends Calla
checkClosed()
false
}

// todo
override def unwrap[T](iface: Class[T]): T = null.asInstanceOf[T]

override def isWrapperFor(iface: Class[_]): Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.mongodb.scala.Document

import java.util.Date

trait CirceSchema {
trait CirceSchema extends CirceProductSchema {

implicit val DateFormat: Encoder[Date] with Decoder[Date] = new Encoder[Date] with Decoder[Date] {
override def apply(a: Date): Json = Encoder.encodeString.apply(a.toInstant.toString)
Expand Down Expand Up @@ -136,11 +136,11 @@ trait CirceSchema {
.toList: _*
)
case product: Product =>
val productElementNames = product.productElementNames.toList
val fieldMap = productElementNames
val productElementKeys = productElementNames(product).toList
val fieldMap = productElementKeys
.map(
key => {
val index = productElementNames.indexOf(key)
val index = productElementKeys.indexOf(key)
(key, product.productElement(index))
}
)
Expand All @@ -159,4 +159,7 @@ trait CirceSchema {
}
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ class SchemaExplorer {

private def schemaAggregation(deepth: Int, sampleSize: Option[Int]): List[PipelineStage] = {
val buffer = ArrayBuffer[PipelineStage]()
buffer.addAll(
sampleSize.map(size => PipelineStage("sample", Map("size" -> size)))
)
buffer ++= sampleSize.map(size => PipelineStage("sample", Map("size" -> size)))

buffer.addOne(PipelineStage("project", Map("_" -> processObject(deepth, 0, "$$ROOT", List()), "_id" -> 0)))
buffer += PipelineStage("project", Map("_" -> processObject(deepth, 0, "$$ROOT", List()), "_id" -> 0))

(0 to deepth).foreach(_ => {
buffer.addOne(PipelineStage("unwind", Map("path" -> "$_", "preserveNullAndEmptyArrays" -> true)))
buffer.addOne(PipelineStage("replaceRoot", Map("newRoot" -> Map("$cond" -> List(Map("$eq" -> List("$_", null)), "$$ROOT", "$_")))))
buffer += PipelineStage("unwind", Map("path" -> "$_", "preserveNullAndEmptyArrays" -> true))
buffer += PipelineStage("replaceRoot", Map("newRoot" -> Map("$cond" -> List(Map("$eq" -> List("$_", null)), "$$ROOT", "$_"))))
})

buffer.addAll(
buffer ++=
List(
PipelineStage("project", Map("_" -> 0)),
PipelineStage("project", Map("l" -> "$$REMOVE", "n" -> 1, "t" -> 1, "v" -> "$$REMOVE")),
Expand All @@ -49,7 +47,6 @@ class SchemaExplorer {
PipelineStage("group", Map("T" -> Map("$push" -> "$$ROOT"), "_id" -> Map("n" -> "$n"), "c" -> Map("$sum" -> "$c"))),
PipelineStage("project", Map("T" -> 1, "_id" -> 0, "c" -> 1, "n" -> "$_id.n")),
PipelineStage("sort", Map("n" -> 1))
)
)
buffer.toList
}
Expand Down Expand Up @@ -79,7 +76,7 @@ class SchemaExplorer {
.foreach(string => {
var fieldName = string
if (fieldName.startsWith(NameSeparator)) {
responseArray.addOne(NameSeparator)
responseArray += NameSeparator
fieldName = fieldName.substring(1)
}
val hasEndingSeperator: Boolean = if (fieldName.endsWith(NameSeparator)) {
Expand All @@ -89,9 +86,9 @@ class SchemaExplorer {
else {
false
}
responseArray.addOne(fieldName)
responseArray += fieldName
if (hasEndingSeperator) {
responseArray.addOne(NameSeparator)
responseArray += NameSeparator
}
})

Expand Down Expand Up @@ -204,7 +201,7 @@ class SchemaExplorer {
fieldsToJsonSchemaDefinition(map, fieldObjectName, field.subFields.toList)
}
if (field.percentageOfParent == 1.0) {
requiredFields.addOne(field.name)
requiredFields += field.name
}
if (field.fieldTypes.size == 1) {
val t = field.fieldTypes.head
Expand Down Expand Up @@ -346,7 +343,8 @@ class SchemaExplorer {
private def convertToBsonPipeline(pipeline: List[PipelineStage]): Seq[Bson] = {
val response: Seq[Bson] = pipeline.map(element => {
val stage = if (element.stage.startsWith("$")) element.stage else "$" + element.stage
Map(stage -> element.value)
val bson: Bson = Map(stage -> element.value)
bson
})
response
}
Expand Down Expand Up @@ -402,7 +400,7 @@ class SchemaExplorer {

val newField = SchemaAnalysisField(name.replace(ArrayItemMark, ArrayElementText), fullName, types, fieldCount, percentage, ArrayBuffer())

parent.subFields.addOne(newField)
parent.subFields.+=(newField)
fieldsMap.put(s"$parentName$NameSeparator$name".replace("ROOT.", ""), newField)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import java.util.concurrent.TimeUnit
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.jdk.CollectionConverters._
import scala.util.Try

class MongoSqlQueryHolder {
private val aggregatePipeline: ArrayBuffer[Document] = ArrayBuffer()
Expand Down Expand Up @@ -236,7 +237,7 @@ class MongoSqlQueryHolder {
}
case e: net.sf.jsqlparser.schema.Column =>
val name = e.getColumnName
name.toIntOption.getOrElse(name.toBooleanOption.getOrElse(name))
Try(name.toInt).toOption.getOrElse(Try(name.toBoolean).toOption.getOrElse(name))
case _ =>
throw new IllegalArgumentException("not supported value type")
}
Expand Down Expand Up @@ -320,7 +321,7 @@ class MongoSqlQueryHolder {
private def convertSelectStatement(select: Select): Unit = {
select.getSelectBody match {
case plainSelect: PlainSelect =>
val selectItems = Option(plainSelect.getSelectItems).map(_.asScala).getOrElse(List.empty)
val selectItems = Option(plainSelect.getSelectItems).map(_.asScala).getOrElse(List.empty)
val maybeDistinct = Option(plainSelect.getDistinct)

selectItems.foreach(sI => {
Expand Down Expand Up @@ -357,7 +358,7 @@ class MongoSqlQueryHolder {
aggregatePipeline += Map("$group" -> groupMap)
})
if (maybeGroupByElement.isEmpty && keepOneDocument) {
val group = mutable.Map[String, Any]()
val group = mutable.Map[String, Any]()
val idGroupMap = mutable.Map()
selectItems.foreach { case se: SelectItem[Expression] =>
val expressionName = se.getExpression.toString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class SchemaSpec extends Specification with Before {
schemaJson.contains("\"title\":\"Friends\"") must beTrue
schemaJson.contains("\"People\":") must beTrue
schemaJson.contains("\"title\":\"People\"") must beTrue
schemaJson.contains("\"_id\":{\"pattern\":\"^([a-fA-F0-9]{2})+$\",\"type\":\"string\"}") must beTrue
val idPattern1 = schemaJson.contains("\"_id\":{\"pattern\":\"^([a-fA-F0-9]{2})+$\",\"type\":\"string\"}")
val idPattern2 = schemaJson.contains("\"_id\":{\"type\":\"string\",\"pattern\":\"^([a-fA-F0-9]{2})+$\"}")
(idPattern1 || idPattern2) must beTrue
schemaJson.contains("\"isActive\":{\"type\":\"boolean\"}") must beTrue
}

Expand Down

0 comments on commit 8a39da8

Please sign in to comment.