Skip to content

Commit

Permalink
sample codes for #743
Browse files Browse the repository at this point in the history
  • Loading branch information
tminglei committed Dec 24, 2024
1 parent 804a70d commit 9059a73
Showing 1 changed file with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ object PgCompositeSupportSuite {
import ScalaVersionShim.maybeTypeConverters._
def ts(str: String) = LocalDateTime.parse(str.replace(' ', 'T'))

object WeekDays extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}

case class Composite1(
id: Long,
txt: String,
date: LocalDateTime,
weekDay: WeekDays.WeekDay,
tsRange: Option[Range[LocalDateTime]]
) extends Struct

Expand Down Expand Up @@ -65,6 +71,14 @@ object PgCompositeSupportSuite {
///
trait API extends JdbcAPI with ArrayImplicits {
registerTypeConverters()
//-- for scala 2
register((s:String) => WeekDays.withName(s))
register((v:WeekDays.WeekDay) => v.toString)
//-- for scala 3
// import com.github.tminglei.slickpg.utils.RegisteredTypeConverter
// implicit val StringToWeekDay: RegisteredTypeConverter[String, WeekDays.WeekDay] = RegisteredTypeConverter((s:String) => WeekDays.withName(s))
// implicit val WeekDayToString: RegisteredTypeConverter[WeekDays.WeekDay, String] = RegisteredTypeConverter((v:WeekDays.WeekDay) => v.toString)
//--//

implicit val composite1TypeMapper: GenericJdbcType[Composite1] = createCompositeJdbcType[Composite1]("composite1")
implicit val composite2TypeMapper: GenericJdbcType[Composite2] = createCompositeJdbcType[Composite2]("composite2")
Expand Down Expand Up @@ -176,13 +190,13 @@ class PgCompositeSupportSuite extends AnyFunSuite with PostgresContainer {

//-------------------------------------------------------------------

val rec1 = TestBean(333, List(Composite2(201, Composite1(101, "(test1'", ts("2001-01-03T13:21:00"),
val rec1 = TestBean(333, List(Composite2(201, Composite1(101, "(test1'", ts("2001-01-03T13:21:00"), WeekDays.Fri,
Some(Range(ts("2010-01-01T14:30:00"), ts("2010-01-03T15:30:00")))), true, Map("t" -> "haha", "t2" -> "133"))))
val rec2 = TestBean(335, List(Composite2(202, Composite1(102, "test2\\", ts("2012-05-08T11:31:06"),
val rec2 = TestBean(335, List(Composite2(202, Composite1(102, "test2\\", ts("2012-05-08T11:31:06"), WeekDays.Mon,
Some(Range(ts("2011-01-01T14:30:00"), ts("2011-11-01T15:30:00")))), false, Map("t't" -> "1,363"))))
val rec3 = TestBean(337, List(Composite2(203, Composite1(103, "getRate(\"x\") + 5;", ts("2015-03-08T17:17:03"),
val rec3 = TestBean(337, List(Composite2(203, Composite1(103, "getRate(\"x\") + 5;", ts("2015-03-08T17:17:03"), WeekDays.Sat,
None), false, Map("t,t" -> "getRate(\"x\") + 5;"))))
val rec4 = TestBean(339, List(Composite2(204, Composite1(104, "x=1&y=2&[INSERT_DEVICE_ID_HERE]&z=3", ts("2001-01-03T13:21:00"),
val rec4 = TestBean(339, List(Composite2(204, Composite1(104, "x=1&y=2&[INSERT_DEVICE_ID_HERE]&z=3", ts("2001-01-03T13:21:00"), WeekDays.Sun,
Some(Range(ts("2010-01-01T14:30:00"), ts("2010-01-03T15:30:00")))), true, Map("t" -> "haha", "t2" -> "133"))))

val rec11 = TestBean1(111, List(Composite3(Some("(test1'"))))
Expand All @@ -191,19 +205,19 @@ class PgCompositeSupportSuite extends AnyFunSuite with PostgresContainer {
val rec14 = TestBean1(114, List(Composite3(Some("Word1 (Word2)"))))
val rec15 = TestBean1(115, List(Composite3(Some(""))))

val rec21 = TestBean2(211, Composite1(201, "test3", ts("2015-01-03T13:21:00"),
val rec21 = TestBean2(211, Composite1(201, "test3", ts("2015-01-03T13:21:00"), WeekDays.Thu,
Some(Range(ts("2015-01-01T14:30:00"), ts("2016-01-03T15:30:00")))), C2(List(C1("1s"))))
val rec22 = TestBean2(212, Composite1(202, "", ts("2015-01-03T13:21:00"),
val rec22 = TestBean2(212, Composite1(202, "", ts("2015-01-03T13:21:00"), WeekDays.Tue,
Some(Range(ts("2015-01-01T14:30:00"), ts("2016-01-03T15:30:00")))), C2(List(C1("test1"))))

val rec31 = TestBean3(1, None)
val rec32 = TestBean3(2, Some(Composite4(1, "x1", Nil, Some(List.empty), "get(\"x1\").ok", "(4).ok")))
val rec32_al = TestBean3(2, Some(Composite4(1, "x1", Nil, None, "get(\"x1\").ok", "(4).ok")))
val rec33 = TestBean3(3, Some(Composite4(2, "x2", List("xxx(yyy)zz,z", "u(vv)(w)x(y)", "x=1&y=2&[INSERT_DEVICE_ID_HERE]&z=3",
")read_world_example", // parses a null element into list `" null, ")read_world_example"`
"(", ")", // parsed as one element `"(,)"`
"{\"", "}", // parsed as one element `""{",}"`
"\\", "\"", // both are parsed as `\"`
// ")read_world_example", // parses a null element into list `" null, ")read_world_example"`
// "(", ")", // parsed as one element `"(,)"`
// "{\"", "}", // parsed as one element `""{",}"`
// "\\", "\"", // both are parsed as `\"`

// "();", // the following completely break parsing
// "(real_world_example",
Expand All @@ -214,7 +228,8 @@ class PgCompositeSupportSuite extends AnyFunSuite with PostgresContainer {
test("Composite type Lifted support") {
Await.result(db.run(
DBIO.seq(
sqlu"create type composite1 as (id int8, txt text, date timestamp, ts_range tsrange)",
sqlu"create type weekday as enum ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')",
sqlu"create type composite1 as (id int8, txt text, date timestamp, week_day weekday, ts_range tsrange)",
sqlu"create type composite2 as (id int8, comp1 composite1, confirm boolean, map hstore)",
sqlu"create type composite3 as (txt text, id int4, code int4, bool boolean)",
sqlu"create type composite4 as (id int4, name text, variable text[], optVars text[], script1 text, script2 text)",
Expand Down Expand Up @@ -284,7 +299,8 @@ class PgCompositeSupportSuite extends AnyFunSuite with PostgresContainer {
sqlu"drop type composite4",
sqlu"drop type composite3",
sqlu"drop type composite2",
sqlu"drop type composite1"
sqlu"drop type composite1",
sqlu"drop type weekday"
)
).transactionally
), Duration.Inf)
Expand All @@ -297,7 +313,8 @@ class PgCompositeSupportSuite extends AnyFunSuite with PostgresContainer {
implicit val getTestBean1Result: GetResult[TestBean1] = GetResult(r => TestBean1(r.nextLong(), r.nextArray[Composite3]().toList))

Await.result(db.run(DBIO.seq(
sqlu"create type composite1 as (id int8, txt text, date timestamp, ts_range tsrange)",
sqlu"create type weekday as enum ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')",
sqlu"create type composite1 as (id int8, txt text, date timestamp, week_day weekday, ts_range tsrange)",
sqlu"create type composite2 as (id int8, comp1 composite1, confirm boolean, map hstore)",
sqlu"create type composite3 as (txt text, id int4, code int4, bool boolean)",
sqlu"create table CompositeTest (id BIGINT NOT NULL, comps composite2[] DEFAULT '{}' NOT NULL)",
Expand All @@ -316,7 +333,8 @@ class PgCompositeSupportSuite extends AnyFunSuite with PostgresContainer {
sqlu"drop table CompositeTest",
sqlu"drop type composite3",
sqlu"drop type composite2",
sqlu"drop type composite1"
sqlu"drop type composite1",
sqlu"drop type weekday"
).transactionally), Duration.Inf)
}
}

0 comments on commit 9059a73

Please sign in to comment.