Skip to content

Commit

Permalink
exclude CompositeSupport for scala 3
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsimpson committed Nov 28, 2023
1 parent f14654d commit 6fd4690
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 17 deletions.
7 changes: 4 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
val scala212 = "2.12.18"
val scala213 = "2.13.12"
val scala3 = "3.3.1"

lazy val commonSettings = Seq(
organizationName := "slick-pg",
Expand All @@ -8,7 +9,7 @@ lazy val commonSettings = Seq(
version := "0.22.0-M4",

scalaVersion := scala213,
crossScalaVersions := Seq(scala212, scala213),
crossScalaVersions := Seq(scala212, scala213, scala3),
scalacOptions ++= Seq("-deprecation", "-feature",
"-language:implicitConversions",
"-language:reflectiveCalls",
Expand Down Expand Up @@ -64,7 +65,7 @@ lazy val commonSettings = Seq(
def mainDependencies(scalaVersion: String) = {
Seq (
"org.scala-lang.modules" %% "scala-parser-combinators" % "2.3.0",
"org.scala-lang" % "scala-reflect" % scalaVersion,
"dev.zio" %% "izumi-reflect" % "2.3.8",
"com.typesafe.slick" %% "slick" % "3.5.0-M4",
"org.postgresql" % "postgresql" % "42.6.0",
"org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0",
Expand Down Expand Up @@ -145,7 +146,7 @@ lazy val slickPgPlayJson = (project in file("./addons/play-json"))
name := "slick-pg_play-json",
description := "Slick extensions for PostgreSQL - play-json module",
libraryDependencies := mainDependencies(scalaVersion.value) ++
Seq("com.typesafe.play" %% "play-json" % "2.9.4")
Seq("org.playframework" %% "play-json" % "3.0.1")
)
.dependsOn (slickPgCore % "test->test;compile->compile")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package com.github.tminglei.slickpg
package composite
package com.github.tminglei.slickpg.composite

import slick.jdbc.{JdbcTypesComponent, PostgresProfile}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.tminglei.slickpg.composite

import izumi.reflect.macrortti.LightTypeTag
import izumi.reflect.Tag
import slick.jdbc.{JdbcTypesComponent, PostgresProfile}
import scala.annotation.static
import scala.collection.concurrent.TrieMap
import scala.reflect.{classTag, ClassTag}

trait Struct extends AnyRef

trait PgCompositeExtensions extends JdbcTypesComponent { driver: PostgresProfile =>
//TODO not implemented by now
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Supported Composite type Oper/Functions
---------------------------------------

_ps: only type mapper supported currently_
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.tminglei.slickpg
package utils

import izumi.reflect.{Tag => TTag}
import izumi.reflect.macrortti.LightTypeTag
import slick.util.Logging
import java.sql.{Date, Time, Timestamp}
import java.time.{LocalDate, LocalDateTime, LocalTime}
import java.util.UUID

object TypeConverters extends Logging {
case class ConvConfig(from: LightTypeTag, to: LightTypeTag, var conv: (_ => _))

private var convConfigList = List[ConvConfig]()

// register basic converters
register((v: String) => v.toInt)
register((v: String) => v.toLong)
register((v: String) => v.toShort)
register((v: String) => v.toFloat)
register((v: String) => v.toDouble)
register((v: String) => pgBoolAdjust(v).toBoolean)
register((v: String) => v.toByte)
register((v: String) => UUID.fromString(v))
// register date/time converters
register((v: String) => Date.valueOf(v))
register((v: String) => Time.valueOf(v))
register((v: String) => Timestamp.valueOf(v))
register((v: Date) => v.toString)
register((v: Time) => v.toString)
register((v: Timestamp) => v.toString)
register((v: String) => LocalDate.parse(v))
register((v: String) => LocalTime.parse(v))
register((v: String) => LocalDateTime.parse(v.replace(' ', 'T')))
register((v: LocalDate) => v.toString)
register((v: LocalTime) => v.toString)
register((v: LocalDateTime) => v.toString)

def register[FROM, TO](convert: (FROM => TO))(implicit from: TTag[FROM], to: TTag[TO]) = {
logger.info(s"register converter for ${from.tag.repr} => ${to.tag.repr}")
find(from.tag, to.tag).map(_.conv = convert).orElse({
convConfigList :+= ConvConfig(from.tag, to.tag, convert)
None
})
}

def converter[FROM,TO](implicit from: TTag[FROM], to: TTag[TO]): (FROM => TO) = {
find(from.tag, to.tag).map(_.conv.asInstanceOf[(FROM => TO)])
.getOrElse(throw new IllegalArgumentException(s"Converter NOT FOUND for ${from.tag} => ${to.tag}"))
}

///
private def pgBoolAdjust(s: String): String =
Option(s).map(_.toLowerCase) match {
case Some("t") => "true"
case Some("f") => "false"
case _ => s
}

def find(from: LightTypeTag, to: LightTypeTag): Option[ConvConfig] = {
logger.debug(s"get converter for ${from.repr} => ${to.repr}")
convConfigList.find(e => (e.from =:= from && e.to =:= to)).orElse({
if (from <:< to) {
Some(ConvConfig(from, to, (v: Any) => v))
} else None
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@ package com.github.tminglei.slickpg.utils

import slick.util.Logging

import izumi.reflect.{Tag => TTag}
import scala.reflect.ClassTag
import slick.jdbc.{GetResult, PositionedResult, SetParameter, PositionedParameters}

import scala.reflect.runtime.{universe => u}

object PlainSQLUtils extends Logging {
import SimpleArrayUtils._
private[slickpg] var nextArrayConverters = Map.empty[String, PositionedResult => Option[Seq[_]]]

/** used to support 'nextArray[T]/nextArrayOption[T]' in PgArraySupport */
def addNextArrayConverter[T](conv: PositionedResult => Option[Seq[T]])(implicit ttag: u.TypeTag[T]) = {
logger.info(s"\u001B[36m >>> adding next array converter for ${u.typeOf[T]} \u001B[0m")
nextArrayConverters.synchronized {
val convKey = u.typeOf[T].toString
def addNextArrayConverter[T](conv: PositionedResult => Option[Seq[T]])(implicit ttag: TTag[T]) = {
logger.info(s"\u001B[36m >>> adding next array converter for ${ttag.tag.repr} \u001B[0m")
val convKey = ttag.tag.repr
val existed = nextArrayConverters.get(convKey)
if (existed.isDefined) logger.warn(
s"\u001B[31m >>> DUPLICATED next array converter for ${u.typeOf[T]}!!! \u001B[36m If it's expected, pls ignore it.\u001B[0m"
s"\u001B[31m >>> DUPLICATED next array converter for ${ttag.tag.repr}!!! \u001B[36m If it's expected, pls ignore it.\u001B[0m"
)
nextArrayConverters += (convKey -> conv)
}
}

///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.tminglei.slickpg

import izumi.reflect.macrortti.LightTypeTag
import izumi.reflect.{Tag => TTag}

import scala.reflect.{ClassTag, classTag}
import composite.Struct
import slick.jdbc.{PositionedResult, PostgresProfile}
import slick.jdbc.SetParameter

import scala.deriving.*
import scala.compiletime.{error, erasedValue, summonInline}

trait PgCompositeSupport extends utils.PgCommonJdbcTypes with array.PgArrayJdbcTypes { driver: PostgresProfile =>

protected lazy val emptyMembersAsNull = true

//---
def createCompositeJdbcType[T <: Struct](sqlTypeName: String, cl: ClassLoader = getClass.getClassLoader)(implicit ev: TTag[T], tag: ClassTag[T]): GenericJdbcType[T] =
throw new UnsupportedOperationException("Composite support is unimplemented for scala 3")

def createCompositeArrayJdbcType[T <: Struct](sqlTypeName: String, cl: ClassLoader = getClass.getClassLoader)(implicit ev: TTag[T], tag: ClassTag[T]): AdvancedArrayJdbcType[T] =
throw new UnsupportedOperationException("Composite support is unimplemented for scala 3")

/// Plain SQL support
def nextComposite[T <: Struct](r: PositionedResult, cl: ClassLoader = getClass.getClassLoader)(implicit ev: TTag[T], tag: ClassTag[T]) =
throw new UnsupportedOperationException("Composite support is unimplemented for scala 3")
def nextCompositeArray[T <: Struct](r: PositionedResult, cl: ClassLoader = getClass.getClassLoader)(implicit ev: TTag[T], tag: ClassTag[T]) =
throw new UnsupportedOperationException("Composite support is unimplemented for scala 3")

def createCompositeSetParameter[T <: Struct](sqlTypeName: String, cl: ClassLoader = getClass.getClassLoader)(implicit ev: TTag[T], tag: ClassTag[T]) =
throw new UnsupportedOperationException("Composite support is unimplemented for scala 3")

def createCompositeOptionSetParameter[T <: Struct](sqlTypeName: String, cl: ClassLoader = getClass.getClassLoader)(implicit ev: TTag[T], tag: ClassTag[T]) =
throw new UnsupportedOperationException("Composite support is unimplemented for scala 3")
def createCompositeArraySetParameter[T <: Struct](sqlTypeName: String, cl: ClassLoader = getClass.getClassLoader)(implicit ev: TTag[T], tag: ClassTag[T]) = {
throw new UnsupportedOperationException("Composite support is unimplemented for scala 3")
}
def createCompositeOptionArraySetParameter[T <: Struct](sqlTypeName: String, cl: ClassLoader = getClass.getClassLoader)(implicit ev: TTag[T], tag: ClassTag[T]) =
throw new UnsupportedOperationException("Composite support is unimplemented for scala 3")
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.tminglei.slickpg

import izumi.reflect.{Tag => TTag}
import java.util.UUID
import java.sql.{Date, Time, Timestamp}
import slick.jdbc.{GetResult, JdbcType, PositionedResult, PostgresProfile, SetParameter}
import scala.reflect.runtime.{universe => u}
import scala.reflect.classTag

trait PgArraySupport extends array.PgArrayExtensions with array.PgArrayJdbcTypes { driver: PostgresProfile =>
Expand Down Expand Up @@ -63,9 +63,9 @@ trait PgArraySupport extends array.PgArrayExtensions with array.PgArrayJdbcTypes
}

implicit class PgArrayPositionedResult(r: PositionedResult) {
def nextArray[T]()(implicit tpe: u.TypeTag[T]): Seq[T] = nextArrayOption[T]().getOrElse(Nil)
def nextArrayOption[T]()(implicit ttag: u.TypeTag[T]): Option[Seq[T]] = {
nextArrayConverters.get(u.typeOf[T].toString).map(_.apply(r))
def nextArray[T]()(implicit tpe: TTag[T]): Seq[T] = nextArrayOption[T]().getOrElse(Nil)
def nextArrayOption[T]()(implicit ttag: TTag[T]): Option[Seq[T]] = {
nextArrayConverters.get(ttag.tag.repr).map(_.apply(r))
.getOrElse(simpleNextArray[T](r)).asInstanceOf[Option[Seq[T]]]
}
}
Expand Down

0 comments on commit 6fd4690

Please sign in to comment.