Skip to content

Instantly share code, notes, and snippets.

@ostronom
Created February 20, 2012 17:00
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ostronom/1870114 to your computer and use it in GitHub Desktop.
Save ostronom/1870114 to your computer and use it in GitHub Desktop.
Play 2.0 & squeryl simple integration
package controllers
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.{Session}
import play.api._
import play.api.mvc._
import models._
object CountryController extends Controller {
def index(offset: Int) = Action {
inTransaction {
val countriesPage = from(CoreSchema.countries)(country =>
where(country.enabled === true)
select(country)
orderBy(country.id asc)
).page(25*offset, 25).toSeq
Ok(views.html.countries(countriesPage))
}
}
def show(slug: String) = Action {
inTransaction {
//Session.currentSession.setLogger(println)
from(CoreSchema.countries)(country =>
where(country.enabled === true and country.slug === slug)
select(country)
).headOption match {
case Some(item) => Ok(views.html.country(item))
case None => NotFound(views.html.notfound())
}
}
}
}
import play.api._
import play.api.db._
import org.squeryl.{Session, SessionFactory}
import org.squeryl.adapters.PostgreSqlAdapter
object Global extends GlobalSettings {
override def onStart(app: Application) {
SessionFactory.concreteFactory = Some( () => connection )
def connection() = {
Session.create(DB.getConnection()(app), new PostgreSqlAdapter)
}
}
}
package models
//import scala.util.DynamicVariable
import play.api.db._
import play.api.Play.current
import org.squeryl.KeyedEntity
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.dsl.{OneToMany}
import org.squeryl.Schema
import org.squeryl.annotations.Column
import org.squeryl.{Session, SessionFactory}
import java.sql.Timestamp
import System._
import java.lang.{Integer}
class BaseEntity extends KeyedEntity[Long] {
val id: Long = 0
}
trait SlugField {
val slug: String = ""
}
trait CreationTimeMonitoring {
val created_at: Timestamp = new Timestamp(System.currentTimeMillis)
}
class User(var username: String,
var password: String,
var email: String,
var verification_code: String,
var is_verified: Boolean) extends BaseEntity {
def this() = this("", "", "", "", false)
}
class Country(var enabled: Boolean,
var area: Integer,
var engname: String,
var rusname: String,
var iso: String) extends BaseEntity with SlugField {
def this() = this(false, 0, "", "", "")
lazy val cities: OneToMany[City] = CoreSchema.countryToCities.left(this)
def citiesByPopulation = {
from(CoreSchema.cities)(city =>
where(city.country_id === id)
select(city)
orderBy(city.pop desc)
)
}
def wikifiedName = {
((engname toLowerCase) split " " map (_ capitalize)).mkString("_")
}
}
class City(var engname: String,
var rusname: String,
var lat: Double,
var lon: Double,
var pop: Long,
var country_id: Long) extends BaseEntity with SlugField{
def this() = this("", "", 0, 0, 0, 0)
}
class Post(var title: String,
var content: String,
var author_id: Long,
var country_id: Long) extends BaseEntity with SlugField with CreationTimeMonitoring {
def this() = this("", "", 0, 0)
}
object CoreSchema extends Schema {
val users = table[User]("user")
val countries = table[Country]("country")
val cities = table[City]("city")
val posts = table[Post]("post")
val countryToPosts =
oneToManyRelation(countries, posts).
via((country,post) => country.id === post.country_id)
val countryToCities =
oneToManyRelation(countries, cities).
via((country,city) => country.id === city.country_id)
val userToPosts =
oneToManyRelation(users, posts).
via((user,post) => user.id === post.author_id)
on(users)(ent => declare(
ent.id is(autoIncremented),
ent.email is(unique, dbType("varchar(255)"))
))
on(countries)(ent => declare(
ent.id is(autoIncremented),
ent.iso is(unique, dbType("varchar(2)")),
ent.slug is(unique)
))
on(cities)(ent => declare(
ent.id is(autoIncremented),
ent.slug is(unique)
))
on(posts)(ent => declare(
ent.id is(autoIncremented),
ent.slug is(unique)
))
}
@sebnozzi
Copy link

Thank for this! Pretty much appreciated.
Is there any advantage of having your entity classes be "normal" classes over being "case classes"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment