如何在 Querulous 中更新 ResultSet?例如,我有一个未加密密码的遗留应用程序。我想返回并使用一些 bcrypt java 库对所有这些密码进行 bcrypt。
这是我使用 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
}
}
}
}