3

我正在为一个网络应用程序使用电梯(这太棒了!)并且很困惑使用什么 - 光滑或映射器。我以前使用过带有应用程序的映射器。它运作良好,但我发现了一个问题。假设我想基于多个过滤器查询数据库,这些过滤器可能存在也可能不存在,但我想不出办法。例如。

class Gig extends LongKeyedMapper[Gig]{
  def primaryKeyField = id
  def getSingleton = Gig

  object id extends MappedLongIndex(this) {
    override def dbColumnName = "id"
    override def dbIndexed_? = true
  }

  object slug extends MappedString(this, 250) {
    override def dbColumnName = "slug"
    override def dbNotNull_? = true
  }

  object status extends MappedEnum(this, GigStatus) {
    override def dbColumnName = "status"
    override def dbIndexed_? = true
    override def targetSQLType(field: String) = Types.TINYINT
  }

  object title extends MappedString(this, 150) {
    override def dbColumnName = "title"
    override def uniqueFieldId = Full("gig_title")
    override def dbNotNull_? = true
    override def validations = valMinLen(5, "Please add a proper title.") _ :: valMaxLen(150, "Title can not exceed 150 characters") _ ::  super.validations
  }

  object description extends MappedText(this) with StringValidators{
    override def dbColumnName = "description"
    override def uniqueFieldId = Full("gig_desc")
    override def dbNotNull_? = true

    protected def valueTypeToBoxString(in: String): Box[String] = {
      Full(this.is)
    }

    protected def boxStrToValType(in: Box[String]): String = {
      in openOr null
    }

    def maxLen = 200
    override def validations = valMinLen(30, "Please add a proper description.") _ :: Nil
  }

  object locked extends MappedBoolean(this) {
    override def dbColumnName = "locked"
  }

  object amount extends MappedDouble(this) {
    override def dbColumnName = "amount"
    override def uniqueFieldId = Full("gig_amount")
    override def dbNotNull_? = true
  }

  object category extends MappedLongForeignKey(this, Category) {
    override def dbColumnName = "category"
    override def uniqueFieldId = Full("gig_category")
    override def dbNotNull_? = true

    override def validations = categoryExists _ :: super.validations

    def categoryExists(id: Long): List[FieldError] = {
      Category.findByKey(id) match {
        case Full(cat) => Nil
        case _ => List(FieldError(this, "Invalid category selected"))
      }
    }
  }

  object createdOn extends MappedDateTime(this) {
    override def dbColumnName = "created_on"
  }

  object updatedOn extends MappedDateTime(this) {
    override def dbColumnName = "updated_on"
  }

  object createdBy extends MappedLongForeignKey(this, User) {
    override def dbColumnName = "created_by"
  }

  object updatedBy extends MappedLongForeignKey(this, User) {
    override def dbColumnName = "updated_by"
  }

  def canApply: Boolean = {
    User.currentUser.map(user => user.provider_? && !user.id.is.equals(createdBy.is)) openOr false
  }

  def myUrl = "/gigs/"+id.is+"/"+slug.is

  def editUrl = "/gigs/"+id.is+"/edit"

  def unlockUrl = "/payment?id="+this.id.is+"&type=gig&action=unlock"

  def hasAnyBidsByUser(user: User): Boolean = {
    Bid.get(this, user).size match {
      case 0 => false
      case _  => true
    }
  }

  def locked_? = this.locked.is

  def bidsCount = Bid.getBidsCount(this)

  def isOwner(user: User): Boolean = user.id.is.equals(this.createdBy.is)
}

假设我想查询数量 = 10 且标题 = 'Sample' 的演出。其中任何一个都可能存在或都不存在。当我早些时候在java中工作时使用hibernate时,我曾经根据条件添加到条件 if amount!=null 将金额添加到条件,如果 title!=null 将标题添加到条件。我在映射器中使用本机 sql 做到了这一点,findAllByInsecureSql我不知道这是否是最好的方法。

目前我正在尝试用电梯滑滑。我找不到任何有关如何使其工作的资源。有人可以指出我应该使用什么的正确方向。Mapper 或 Slick 以及任何将 slick 与 Lift 集成的好资源。

4

0 回答 0