2014-02-15 41 views
0

我在玩2.1.2ORDER BY子句是打破ANORM選擇查詢中播放2.1.2

def list(startDate:String, endDate:String, page:Option[Int], pageSize:Option[Int]) = { 
    DB.withConnection { implicit connection => 
     val c = SQL(""" 
       select 
       employeeId, 
       CertType, 
       ct.name, 
       applicable, 
       due, 
       year(due) as yeardue, month(due) as monthdue, day(due) as daydue, 
       year(completed),  month(completed),  day(completed), 
       year(last),  month(last),  day(last), 
       status 
       from cert 
       join certtype ct on CertType=ct.code 
       where due<{endDate} and due>{startDate} and CertType not in (500,600) 
       order by due 
      """ 
     ) 
      .on('index -> page, 'pageSize -> pageSize , 'startDate -> startDate, 'endDate->endDate)().collect{ 
      case Row( 
        employeeId:Long, 
        certType:Int, 
        name:String, 
        applicable:Int, 
        Some(due:java.sql.Date), 
        Some(yeardue:Long),  Some(monthdue:Long),  Some(daydue:Long), 
        Some(yearcompleted:Long), Some(monthcompleted:Long), Some(daycompleted:Long), 
        Some(yearlast:Long), Some(monthlast:Long),  Some(daylast:Long), 
        status:Long) => 
        Reminder(
         employeeId, 
         certType, 
         name, 
         if (applicable>0) true else false, 
         makeDate(yeardue, monthdue, daydue), 
         makeDate(yearcompleted, monthcompleted, daycompleted), 
         makeDate(yearlast, monthlast, daylast), 
         status); 
     } 
     c.toList 
    } 
} 

這個作品的唯一方法是將下面的代碼,如果我「借命令」條款刪除。
排序方式只能使用像ct.name這樣的字符串字段。 否則,列表爲空。

我沒有這個錯誤信息,只是一個空白列表。

+0

代碼其實訂購通常是首選,從效率的角度在DB排序。此外,按SQL順序的AFAIK也適用於日期。可能是Anorm的問題?你可以用Slick來代替嗎?或者只是不要在數據庫中進行排序。此外,'startDate和endDate'之間的交易也稍微有效一些。 – Ashalynd

+0

嗯......不知道在代碼中訂購更有效率,但感謝在日期範圍內使用'between'的提示。 – Joel

回答

0

好的,現在這個工程,我不知道爲什麼。

這裏的工作版本:

def list(startDate:String, endDate:String, page:Option[Int], pageSize:Option[Int]) = { 
    DB.withConnection { implicit connection => 
     val c = SQL(""" 
         select 
          employeeId, 
          CertType, 
          ct.name, 
          applicable, 
          due, 
          completed, 
          last, 
          status 
         from cert 
         join certtype ct on CertType=ct.code 
         where due between {startDate} and {endDate} and CertType not in (500,600) 
         order by due 
        """ 
      ) 
      .on('index -> page, 'pageSize -> pageSize , 'startDate -> startDate, 'endDate->endDate)().collect{ 
       case Row( 
          employeeId:Long, 
          certType:Int, 
          name:String, 
          applicable:Int, 
          Some(due:java.sql.Date),  // optional to handle potential null value 
          Some(completed:java.sql.Date), // optional to handle potential null value 
          last:java.sql.Date,    // this is probably required to NOT be optional since it is a PK in the schema (?) 
          status:Long) => 
        Reminder(
          employeeId, 
          certType, 
          name, 
          if (applicable>0) true else false, 
          new DateTime(due.  getTime()).toString("yyyy-MM-dd"), 
          new DateTime(completed. getTime()).toString("yyyy-MM-dd"), 
          new DateTime(last.  getTime()).toString("yyyy-MM-dd"), 
          status); 
     } 
     c.toList 
    } 
    }