如何使用自动生成的表/行定义填充数据库?

时间:2014-08-13 00:20:49

标签: scala slick

我针对数据库(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))

1 个答案:

答案 0 :(得分:0)

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

  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定义的表格,我也很想知道它们:)

相关问题