【问题标题】:How do i populate a database with table/row definitions that were autogenerated?如何使用自动生成的表/行定义填充数据库?
【发布时间】:2014-08-16 05:01:52
【问题描述】:

我已经针对数据库 (sqlserver db) 运行了自动生成器,它生成了许多表定义。

我正在寻找使用这些定义来填充新数据库 - 我以为我会使用 Tables.ddl.create,但我看到编译错误 (value create is not a member of app.db.Tables.profile.DDL)

另外,一旦创建了一个表,填充它的规范模式是什么? (自动生成的代码似乎不包含 * 投影 - 这是故意的吗?)


示例自动生成的表定义:

 /** Entity class storing rows of table SampleTable
   *  @param sampletableid Database column SampleTableId DBType(uniqueidentifier), Length(36,false)*/
  case class SampleTableRow(sampletableid: Option[String])

  /** GetResult implicit for fetching SampleTableRow objects using plain SQL queries */
  implicit def GetResultSampleTableRow(implicit e0: GR[Option[String]], e1: GR[Int], e2: GR[Option[java.sql.Clob]], e3: GR[java.sql.Timestamp]): GR[SampleTableRow] = GR{
    prs => import prs._
   SampleTableRow.tupled((<<?[String]))
  }

  /** Table description of table SampleTable. Objects of this class serve as prototypes for rows in queries. */
  class SampleTable(_tableTag: Tag) extends Table[SampleTableRow](_tableTag, Some("dbo"), "SampleTable") {
    def * = (sampletableid) <> (SampleTableRow.tupled, SampleTableRow.unapply)
    /** Maps whole row to an option. Useful for outer joins. */
    def ? = (sampletableid).shaped.<>({r=>import r._; _3.map(_=>SampleTableRow.tupled((_1)))}, (_:Any) =>  throw new Exception("Inserting into ? projection not supported."))    
    /** Database column SampleTableId DBType(uniqueidentifier), Length(36,false) */
    val sampletableid: Column[Option[String]] = column[Option[String]]("SampleTableId", O.Length(36,varying=false))
  }

  /** Collection-like TableQuery object for table SampleTable */
  lazy val SampleTable = new TableQuery(tag => new SampleTable(tag))

【问题讨论】:

  • 我从未将模式生成器与 SQL 数据库(仅 PostgreSQL 和 MySQL)一起使用,但据我所知,投影方法应该是自动生成的。
  • 绝对不存在,对于我的 20 个左右的表定义中的任何一个
  • 您能在问题中添加一个表定义吗?
  • 只需使用将转换为查询的巧妙方法(插入、删除、过滤等)。或者你可以编写一个通用的表对象,在其中添加所有必要的方法并让其他表从它继承。

标签: scala slick


【解决方案1】:

您可以使用 Slick 执行的通用 Table 对象的示例(假设您有 database:Database 在范围内):

  abstract class genericTable[T <: Table[A] , A] {
    val table: TableQuery[T]

    /**
     *  regenerate the table based on its definition in code
     */
    def create = database.withSession { implicit session =>table.ddl.create }

    /**
     *  drop the table from schema
     */
    def drop = database.withSession { implicit session =>table.ddl.drop }

    /**
     *  insert an entry of the given type
     */
    def insert(entry: A): A = database.withSession { implicit session =>
      table += entry
    }

    /**
     *  insert several entries of the given type
     */
    def insertAll(entries: List[A]) = database.withSession { implicit session =>
       table.insertAll(entries:_*) }

    /*
     *  return all elements from the given table
     */
    def all: List[A] = database.withSession { implicit session =>
      table.list.map(_.asInstanceOf[A])
    }
  }

然后您可以将您的具体查询对象定义为:

object sampleTable extends genericTable(SampleTable, SampleTableRow) {
    val table = TableQuery[SampleTable]
}

并获取您在基类中定义的所有方法。这意味着,您可以从其他地方拨打以下电话:

val allSampleRecords = sampleTable.all
sampleTable.insertAll(List(SampleTableRow(None), SampleTableRow(Some("anEntry"))))

因为方法 all 和 insertAll 已经定义。

如果有一些更通用的方法可以将数据序列化到使用 Slick 定义的表中,我也很想知道它们:)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2022-12-21
  • 2011-08-28
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 2020-01-25
  • 2017-06-18
  • 1970-01-01
相关资源
最近更新 更多