Last active
June 28, 2017 01:44
-
-
Save hiro-hori/7ef51eb094425967c6e15c9d6992d7f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test008 | |
import org.scalatest._ | |
import scalikejdbc._ | |
import scalikejdbc.scalatest.AutoRollback | |
import skinny.dbmigration.DBSeeds | |
import skinny.orm._ | |
trait Connection { | |
Class.forName("org.h2.Driver") | |
ConnectionPool.add('test008, "jdbc:h2:mem:test008;MODE=PostgreSQL", "sa", "sa") | |
} | |
trait CreateTables extends DBSeeds { self: Connection => | |
override val dbSeedsAutoSession = NamedAutoSession('test008) | |
addSeedSQL(sql""" | |
create table grand_child ( | |
id serial primary key, | |
name varchar(100) not null | |
)""") | |
addSeedSQL(sql""" | |
create table child ( | |
parent_id int not null, | |
grand_child_id int not null references grand_child(id) | |
)""") | |
addSeedSQL(sql""" | |
create table parent ( | |
id serial primary key | |
)""") | |
runIfFailed(sql"select count(1) from parent") | |
} | |
class Spec extends fixture.FunSpec with Matchers with Connection with CreateTables with AutoRollback { | |
override def db(): DB = NamedDB('test008).toDB() | |
case class GrandChild(name: String) | |
object GrandChild extends SkinnyCRUDMapper[GrandChild] { | |
override val connectionPoolName = 'test008 | |
override def defaultAlias = createAlias("g") | |
override def extract(rs: WrappedResultSet, rn: ResultName[GrandChild]) = autoConstruct(rs, rn) | |
} | |
case class Child(parentId: Int, grandChild: Option[GrandChild] = None) | |
object Child extends SkinnyNoIdCRUDMapper[Child] { | |
override val connectionPoolName = 'test008 | |
override def defaultAlias = createAlias("c") | |
override def extract(rs: WrappedResultSet, rn: ResultName[Child]) = autoConstruct(rs, rn, "grandChild") | |
belongsToWithFk[GrandChild]( | |
right = GrandChild, | |
fk = "grandChildId", | |
merge = (child, grandChild) => child.copy(grandChild = grandChild) | |
).byDefault | |
} | |
case class Parent(id: Int, children: Seq[Child] = Nil) | |
object Parent extends SkinnyCRUDMapper[Parent] { | |
override val connectionPoolName = 'test008 | |
override def defaultAlias = createAlias("p") | |
override def extract(rs: WrappedResultSet, rn: ResultName[Parent]) = autoConstruct(rs, rn, "children") | |
lazy val childRef = hasManyWithFk[Child]( | |
many = (Child, Child.defaultAlias), | |
fk = "parentId", | |
on = (parent, child) => sqls.eq(parent.id, child.parentId), | |
merge = (parent, children) => parent.copy(children = children) | |
).includes[Child]( | |
merge = (parents, children) => parents.map { parent => | |
parent.copy(children = children.filter(_.parentId == parent.id)) | |
} | |
) | |
} | |
describe("triple join with .includes(ref)") { | |
it("should work?") { implicit session => | |
val grandChildId = GrandChild.createWithAttributes( | |
'name -> "grandChildName1" | |
) | |
val parentId = Parent.createWithAttributes() | |
Child.createWithAttributes( | |
'parentId -> parentId, | |
'grandChildId -> grandChildId | |
) | |
println(GrandChild.findAll()) | |
println(Child.findAll()) | |
println(Parent.includes(Parent.childRef).findAll()) | |
// println(Parent.findAll()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment