AugustNagro / magnum

A 'new look' for database access in Scala
Apache License 2.0
153 stars 10 forks source link

Readme FAQ #26

Closed AugustNagro closed 3 months ago

AugustNagro commented 3 months ago

Closes #24

alwins0n commented 3 months ago

After re-reading it on the projects main page, I wanted to add one minor correction: Database DO support nested/embedded structures such as postgres composite type, so it's wrong to state that they don't or only support flat structures.

e.g. https://www.postgresql.org/docs/current/sql-createtype.html

AugustNagro commented 3 months ago

That's a good point.. I will re-word.

For Postgres, it appears that the JDBC driver does not support user-defined types https://github.com/pgjdbc/pgjdbc/issues/641

However, I WOULD support this feature, user-defined type support, for databases that support them, IF the official JDBC driver also gains support.

As it stands, there is a work-around one could use for UDTs in Postgres + Magnum. For the embedded class, you can define a custom DBCodec that grabs the column as a Pgobject , then does some custom deserialization (have not tested):

  given EmbeddedAddressCodec: DbCodec[Address] with
    def queryRepr: String = "?"
    val cols: IArray[Int] = IArray(Types.JAVA_OBJECT)
    def readSingle(resultSet: ResultSet, pos: Int): Address =
      val obj = resultSet.getObject(pos, classOf[PGobject])
      customDeser(obj.getValue)
    def writeSingle(entity: Address, ps: PreparedStatement, pos: Int): Unit =
      val res = customSer(entity)
      ps.setObject(pos, res)

You could then even make it generic.

AugustNagro commented 3 months ago

You could then even make it generic.

Maybe by defining

trait UdtDbCodec[A] extends DbCodec[A]

alwins0n commented 3 months ago

Thanks for the hint. I will fiddle a little with your library. Maybe I can extend the functionality to my requirements. After all I should always be able to supply a custom DbCodec right?