Вопрос

I encouter one problem using jOOQ. Can I include dynamic column to mapper/table ?

SQL

CREATE TABLE "PartnerCategory"
    (
      "ID" uuid NOT NULL,
      "Name" character varying(200),
      "Description" character varying,
      "ParentId" uuid,
      "Visible" boolean,
      CONSTRAINT "PartnerCategory_PK" PRIMARY KEY ("ID")
    )
    WITH (
      OIDS=FALSE
    );
    ALTER TABLE "PartnerCategory"
      OWNER TO postgres;

and

val SQL_CTE =
    """
      WITH recursive cte1 as (
      select pc."ID", pc."Name", pc."Description", pc."ParentId", pc."Visible", CAST(pc."Name" as text) as Path
        from "PartnerCategory" pc where pc."ParentId" IS NULL
        union
      select pc."ID", pc."Name", pc."Description", pc."ParentId", pc."Visible", ct.Path || ' | ' || CAST(pc."Name" as text) as Path
        from "PartnerCategory" pc
        join cte1 ct on pc."ParentId" = ct."ID"  )
    select ct."ID", ct."Name", ct."Description", ct."ParentId", ct."Visible", ct.Path from cte1 ct
    """.stripMargin

Code

In query above we have all columns from PartnerCategory and one column which is dynamic Path. Previously to get this all working I had manaully iterate over result:

val x: Seq[PartnerCategory] = for (i <- abcd.asScala)
    yield PartnerCategory(
        UUID.fromString(i.getValue(0).toString),
        i.getValue(1).toString,
        i.getValue(2).toString,
        i.getValue(3) match {
          case null => None
          case f: UUID => Some(UUID.fromString(f.toString))
        },
        false,
        i.getValue(5).toString)

I have custom mapper for this table:

class PartnerCategoryMapper extends RecordMapper[PartnercategoryRecord, PartnerCategory] {
  def map(r: PartnercategoryRecord): PartnerCategory = {
    val pc = PartnerCategory(r.getId, r.getName, r.getDescription, Some(r.getParentid), r.getVisible, "")
    pc.IsNew = false
    pc
  }
}

My model looks like:

case class PartnerCategory(var ID: UUID,var Name: String, var Description: String, var ParentId: Option[UUID], var Visible: Boolean, Path: String)

I tried to write better code and I found that this works almost perfect. I just loose information about dynamic column Path:

val F = model.jooq.tables.Partnercategory.PARTNERCATEGORY
val mapper = new PartnerCategoryMapper()
val record = new PartnercategoryRecord()
ctx.fetch(SQL_CTE).into(F).map(mapper).asScala

Question

How can I do fetch into another mapper with 1 extra column, for example like this:

class PartnerCategoryMapperDynamic extends RecordMapper[PartnercategoryRecordWithPath, PartnerCategory] {
  def map(r: PartnercategoryRecordWithPath): PartnerCategory = {
    val pc = PartnerCategory(r.getId, r.getName, r.getDescription, Some(r.getParentid), r.getVisible, r.getPath)
    pc.IsNew = false
    pc
  }
}
Это было полезно?

Решение

If you're willing to let go of some of the typesafety, you could write:

class PartnerCategoryMapperDynamic 
extends RecordMapper[Record, PartnerCategory] {
  def map(r: Record): PartnerCategory = {
    val pc = PartnerCategory(
      r.getValue(PARTNER_CATEGORY.ID), 
      r.getValue(PARTNER_CATEGORY.NAME), 
      r.getValue(PARTNER_CATEGORY.DESCRIPTION), 
      Some(r.getValue(PARTNER_CATEGORY.PARENTID)), 
      r.getValue(PARTNER_CATEGORY.VISIBLE),
      if (r.field(PARTNER_CATEGORY_WITH_PATH.PATH) != null)
        r.getValue(PARTNER_CATEGORY_WITH_PATH.PATH)
      else
        ""
    )
    pc.IsNew = false
    pc
  }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top