在Querulous中更新ResultSet?

时间:2011-09-09 13:45:30

标签: scala twitter squeryl querulous

如何在Querulous中更新ResultSet?例如,我有一个没有加密密码的遗留应用程序。我想使用一些bcrypt java库返回并加密所有这些密码。

这是我用ScalaQuery提出的解决方案,一次加密一个月的数据,因为有很多记录(虽然我不确定这很重要,所以如果你看到有什么问题,请随时评论我的实施)。

我已经知道它在squeryl中会是什么样子,所以我很想知道它在Querulous中会是什么样子:

package chubaka

import org.scalaquery.session._
import org.scalaquery.session.Database.threadLocalSession
import org.joda.time.DateMidnight
import java.sql.Date

import org.scalaquery.ql._
import org.scalaquery.ql.TypeMapper._

import org.scalaquery.ql.extended.OracleDriver.Implicit._
import org.scalaquery.ql.extended.{ExtendedTable => Table}

import org.scala_tools.time.Imports._
import org.mindrot.BCrypt

object MembershipUpdate {
  def main(args: Array[String]) {
    case class MembersElement(id: String, password: String, dateCreated: String)

    val Members = new Table[(String, Option[String], Option[Date])]("MEMBERS") {
      def id = column[String]("ID", O.PrimaryKey) // This is the primary key column
      def password = column[Option[String]]("PASSWORD")
      def dateCreated = column[Option[Date]]("DATECREATED")
      // Every table needs a * projection with the same type as the table's type parameter
      def * = id ~ password ~ dateCreated
    }

    // Connect to the database and execute the following block within a session
    Database.forURL("jdbc:oracle:thin:scott/tiger@//test.chubaka:1521/legacydb", driver = "oracle.jdbc.OracleDriver") withSession {
      // The session is never named explicitly. It is bound to the current
      // thread as the threadLocalSession that we imported

      def bcryptPassword(password: String) = BCrypt.hashpw(password, BCrypt.gensalt())
      def dtToSqlDT(dt: DateMidnight) = new Date(dt.toDate.getTime)

      var month = new DateMidnight(new DateMidnight().withDayOfMonth(1) + 1.months)

      while (month > new DateTime(2006, 1, 1, 0, 0, 0, 0)) {
        val prevMonth = month - 1.month

        val q = Members.where{ p => 
          (p.password isNotNull) && (p.dateCreated >= dtToSqlDT(prevMonth)) && (p.dateCreated < dtToSqlDT(month))
        }

        q.map(_.password.get).mutate(m => m.row = bcryptPassword(m.row))

        month = prevMonth
      }
    }
  }
}

0 个答案:

没有答案