【问题标题】:Play2's anorm can't work on postgresqlPlay2 的异常不能在 postgresql 上工作
【发布时间】:2012-03-04 01:27:14
【问题描述】:

我发现play2的异常行解析器依赖于jdbc驱动返回的元数据。

所以在play提供的内置示例“zentasks”中,我可以找到这样的代码:

object Project {
  val simple = {
    get[Pk[Long]]("project.id") ~
    get[String]("project.folder") ~
    get[String]("project.name") map {
      case id~folder~name => Project(id, folder, name)
    }
  }
}

请注意所有字段都有project. 前缀。

它在 h2 数据库上运行良好,但在 postgresql 上不行。如果我使用portgresql,我应该写成:

object Project {
  val simple = {
    get[Pk[Long]]("id") ~
    get[String]("folder") ~
    get[String]("name") map {
      case id~folder~name => Project(id, folder, name)
    }
  }
}

我问过this in play's google group,Guillaume Bort 说:

是的,如果您使用的是 postgres,这可能是原因。 PostgreSQL jdbc 驱动程序损坏并且不返回表名。

如果postgresql的jdbc驱动真的有这个问题,我想anorm会有问题: 如果两个表有相同名称的字段,而我用join 查询它们,则异常不会得到正确的值,因为它无法找出哪个名称属于哪个表。

所以我写了一个测试。

1。在 postgresql 上创建表

create table a (
    id      text not null primary key,
    name    text not null
);

create table b (
    id      text not null primary key,
    name    text not null,
    a_id    text,
    foreign key(a_id) references a(id) on delete cascade
);

2。创建异常模型

case class A(id: Pk[String] = NotAssigned, name: String)
case class B(id: Pk[String] = NotAssigned, name: String, aId: String)

object A {
  val simple = {
    get[Pk[String]]("id") ~
      get[String]("name") map {
        case id ~ name =>
          A(id, name)
      }
  }

  def create(a: A): A = {
    DB.withConnection { implicit connection =>
      val id = newId()
      SQL("""
          insert into a (id, name)
          values (
            {id}, {name}
          )
          """).on('id -> id, 'name -> a.name).executeUpdate()
      a.copy(id = Id(id))
    }
  }

  def findAll(): Seq[(A, B)] = {
    DB.withConnection { implicit conn =>
      SQL("""
          select a.*, b.* from a as a left join b as b on a.id=b.a_id
          """).as(A.simple ~ B.simple map {
        case a ~ b => a -> b
      } *)
    }
  }
}

object B {
  val simple = {
    get[Pk[String]]("id") ~
      get[String]("name") ~
      get[String]("a_id") map {
        case id ~ name ~ aId =>
          B(id, name, aId)
      }
  }

  def create(b: B): B = {
    DB.withConnection { implicit conneciton =>
      val id = UUID.randomUUID().toString
      SQL("""
          insert into b (id, name, a_id) 
          values (
          {id}, {name}, {aId}
          )
          """).on('id -> id, 'name -> b.name, 'aId -> b.aId).executeUpdate()
      b.copy(id = Id(id))
    }
  }
}

3。 scalatest 测试用例

class ABTest extends DbSuite {

  "AB" should "get one-to-many" in {
    running(fakeApp) {
      val a = A.create(A(name = "AAA"))
      val b1 = B.create(B(name = "BBB1", aId = a.id.get))
      val b2 = B.create(B(name = "BBB2", aId = a.id.get))

      val ab = A.findAll()
      ab foreach {
        case (a, b) => {
          println("a: " + a)
          println("b: " + b)
        }
      }
    }
  }
}

4。输出

a: A(dbc52793-0f6f-4910-a954-940e508aab26,BBB1)
b: B(dbc52793-0f6f-4910-a954-940e508aab26,BBB1,4a66ebe7-536e-4bd5-b1bd-08f022650f1f)
a: A(d1bc8520-b4d1-40f1-af92-52b3bfe50e9f,BBB2)
b: B(d1bc8520-b4d1-40f1-af92-52b3bfe50e9f,BBB2,4a66ebe7-536e-4bd5-b1bd-08f022650f1f)

您可以看到“a”的名称为“BBB1/BBB2”,但不是“AAA”。

我尝试将带有前缀的解析器重新定义为:

 val simple = {
    get[Pk[String]]("a.id") ~
      get[String]("a.name") map {
        case id ~ name =>
          A(id, name)
      }
  }

但是会报找不到指定字段的错误。

这是异常的大问题吗?还是我错过了什么?

【问题讨论】:

  • 我有一个解决方法:修改postgresql的“org.postgresql.jdbc2.AbstractJdbc2ResultSetMetaData#g​​etTableName”的来源,让它返回“getBaseTableName(column)”
  • 如果您认为“postgresql jdbc 驱动程序已损坏”,为什么不提交错误报告 -> jdbc.postgresql.org/lists.html
  • 我在邮件列表中登顶了一位,但没有人回复我。

标签: postgresql scala playframework playframework-2.0 anorm


【解决方案1】:

最新的play2(RC3)通过检查元对象的类名解决了这个问题:

// HACK FOR POSTGRES
if (meta.getClass.getName.startsWith("org.postgresql.")) {
  meta.asInstanceOf[{ def getBaseTableName(i: Int): String }].getBaseTableName(i)
} else {
  meta.getTableName(i)
}

但是如果你想和p6spy一起使用要小心,它不起作用,因为meta的类名是“com.p6spy....”,而不是“org.postgresql....” .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2019-01-14
    相关资源
    最近更新 更多