def songs = from(MusicDb.songs)(s => where(s.artistId === id) select(s)) def fixArtistName = update(songs)(s => where(s.title === "Prodigy") set( s.title := "The Prodigy", ) )
class User(var id:Long, var username:String) extends KeyedEntity[Long] object MySchema extends Schema{ val userTable = table[User] }
val userTable = table[User]("USER_TABLE")
class User(var id:Long, @Column("USER_NAME") var username:String) extends KeyedEntity[Long]
class DateTrunc(span: String, e: DateExpression[Timestamp], m: OutMapper[Timestamp]) extends FunctionNode[Timestamp]( "date_trunc", Some(m), Seq(new TokenExpressionNode("'" + span + "'"), e) ) with DateExpression[Timestamp] def dateTrunc(span: String, e: DateExpression[Timestamp])(implicit m: OutMapper[Timestamp]) = new DateTrunc(span, e, m)
"org.squeryl" %% "squeryl" % "0.9.5-7", "org.postgresql" % "postgresql" % "9.3-1101-jdbc41"
db.default.driver = org.postgresql.Driver db.default.url = "postgres://postgres:password@localhost/database" db.default.logStatements = true evolutionplugin = disabled
import org.squeryl.adapters.PostgreSqlAdapter import org.squeryl.{Session, SessionFactory} import play.api.db.DB import play.api.mvc.WithFilters import play.api.{Application, GlobalSettings} object Global extends GlobalSettings { override def onStart(app: Application) { SessionFactory.concreteFactory = Some(() => Session.create(DB.getConnection()(app), new PostgreSqlAdapter)) } }
"com.google.code.gson" % "gson" % "2.2.4"
package models import com.google.gson.Gson import org.joda.time.DateTime import org.squeryl.KeyedEntity import play.api.libs.json.JsValue trait EntityBase[K] extends KeyedEntity[K] { def table = findTablesFor(this).head def json(implicit gson: Gson): JsValue = play.api.libs.json.Json.parse(gson.toJson(this)) def isNew: Boolean def save(): this.type = transaction { if (isNew) table.insert(this) else table.update(this) this } } trait EntityC[K] extends EntityBase[K] { var created: TimeStamp = null override def save(): this.type = { if (isNew) created = DateTime.now() super.save() } } trait EntityCUD[K] extends EntityC[K] { var updated: TimeStamp = null var deleted = false override def save(): this.type = { updated = DateTime.now() super.save() } def delete(): this.type = { deleted = true save() } } class Entity extends EntityCUD[Long] { var id = 0L override def isNew = id == 0L }
import java.sql.Timestamp import com.google.gson.Gson import org.joda.time.DateTime import org.squeryl.customtypes._ import org.squeryl.{Schema, Table} import play.api.libs.json.{JsObject, JsValue, Json} import scala.language.implicitConversions package object models extends Schema with CustomTypesMode { val logins = table[Login] def getTable[E <: Entity]()(implicit manifestT: Manifest[E]): Table[E] = tables.find(_.posoMetaData.clasz == manifestT.runtimeClass).get.asInstanceOf[Table[E]] def getTable(name: String): Table[_ <: Entity] = tables.find(_.posoMetaData.clasz.getSimpleName.toLowerCase == name) .get.asInstanceOf[Table[_ <: Entity]] def get[T <: Entity](id: Long)(implicit manifestT: Manifest[T]): Option[T] = getTable[T]().lookup(id).map(e => { if (e.deleted) None else Some(e) }).getOrElse(None) def get(table: String, id: Long): Option[Entity] = getTable(table).lookup(id).map(e => { if (e.deleted) None else Some(e) }).getOrElse(None) def getAll(table: String): Seq[Entity] = from(getTable(table))(e => select(e)).toSeq def save(table: String, json: String)(implicit gson: Gson) = gson.fromJson( json, getTable(table).posoMetaData.clasz ).save() def delete(table: String, id: Long) = get(table, id).map(_.delete()) class TimeStamp(t: Timestamp) extends TimestampField(t) implicit def jodaToTimeStamp(dateTime: DateTime): TimeStamp = new TimeStamp(new Timestamp(dateTime.getMillis)) implicit def timeStampToJoda(timeStamp: TimeStamp): DateTime = new DateTime(timeStamp.value.getTime) class Json(s: String) extends StringField(s) implicit def stringToJson(s: String): Json = new Json(s) implicit def jsonToString(json: Json): String = json.value implicit def jsValueToJson(jsValue: JsValue): Json = new Json(jsValue.toString()) implicit def jsonToJsObject(json: Json): JsObject = Json.parse(json.value).asInstanceOf[JsObject] class ForeignKey[E <: Entity](l: Long) extends LongField(l) { private var _entity = Option.empty[E] def entity(implicit manifestT: Manifest[E]): E = _entity.getOrElse({ val res = get[E](value).get _entity = Some(res) res }) def entity_=(value: E) { _entity = Some(value) } } implicit def entityToForeignKey[E <: Entity](entity: E): ForeignKey[E] = { val fk = new ForeignKey[E](entity.id) fk.entity = entity fk } implicit def foreignKeyToEntity[T <: Entity](fk: ForeignKey[T])(implicit manifestT: Manifest[T]): T = fk.entity implicit def longToForeignKey[T <: Entity](l: Long)(implicit manifestT: Manifest[T]) = new ForeignKey[T](l) }
package models class Login extends Entity { var login = "" var password = "" var parent: ForeignKey[Login] = null }
package controllers import models._ import play.api.mvc._ import utils.Jsons import scala.concurrent.Future import scala.language.implicitConversions trait BaseController extends Controller { implicit val gson = new Gson object DbAction extends ActionBuilder[Request] { override def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]): Future[Result] = transaction { block(request) } } }
package controllers import play.api.libs.json.Json import play.api.mvc.Action object Api extends BaseController { def get(cls: String, id: Long) = DbAction { Ok(models.get(cls, id).map(_.json).getOrElse(Json.obj())) } def save(cls: String) = DbAction{ request => Ok(models.save(cls, request.form.getOrElse("data", "{}")).json) } def delete(cls: String, id: Long) = DbAction { Ok(models.delete(cls, id).map(_.json).getOrElse(Json.obj())) } }
# Api GET /api/:cls/:id controllers.Api.get(cls:String,id:Long) POST /api/save/:cls controllers.Api.save(cls:String) POST /api/delete/:cls/:id controllers.Api.delete(cls:String,id:Long)
Source: https://habr.com/ru/post/230353/
All Articles