Skip to content

Commit

Permalink
Merge pull request #1562 from lift/lift_30-with-master-and-1560
Browse files Browse the repository at this point in the history
Merge with master + deprecated mongo/ConnectionIdentifier code removal.
  • Loading branch information
Shadowfiend committed May 18, 2014
2 parents 33c6b6f + 72bd272 commit ad41071
Show file tree
Hide file tree
Showing 26 changed files with 277 additions and 133 deletions.
@@ -0,0 +1,38 @@
/*
* Copyright 2014 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.liftweb
package util

import Helpers._
import common._

trait ConnectionIdentifier {
def jndiName: String

override def toString() = "ConnectionIdentifier(" + jndiName + ")"

override def hashCode() = jndiName.hashCode()

override def equals(other: Any): Boolean = other match {
case ci: ConnectionIdentifier => ci.jndiName == this.jndiName
case _ => false
}
}

case object DefaultConnectionIdentifier extends ConnectionIdentifier {
val jndiName = "lift"
}
Expand Up @@ -89,10 +89,14 @@ object TimeHelpersSpec extends Specification with ScalaCheck with TimeAmountsGen
3.seconds - 4.seconds must_== (-1).seconds
}
"have a later method returning a date relative to now plus the time span" in {
3.seconds.later.getTime must beCloseTo(new Date().getTime + 3.seconds.millis, 500L)
val expectedTime = new Date().getTime + 3.seconds.millis

3.seconds.later.getTime must beCloseTo(expectedTime, 700L)
}
"have an ago method returning a date relative to now minus the time span" in {
3.seconds.ago.getTime must beCloseTo(new Date().getTime - 3.seconds.millis, 500L)
val expectedTime = new Date().getTime - 3.seconds.millis

3.seconds.ago.getTime must beCloseTo(expectedTime, 500L)
}
"have a toString method returning the relevant number of weeks, days, hours, minutes, seconds, millis" in {
val conversionIsOk = forAll(timeAmounts)((t: TimeAmounts) => { val (timeSpanToString, timeSpanAmounts) = t
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2011 WorldWide Conferencing, LLC
* Copyright 2006-2014 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package db

import java.sql.Connection
import net.liftweb.common._
import net.liftweb.util.ConnectionIdentifier

/**
* Vend JDBC connections
Expand Down
20 changes: 1 addition & 19 deletions persistence/db/src/main/scala/net/liftweb/db/DB.scala
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2011 WorldWide Conferencing, LLC
* Copyright 2006-2014 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1093,24 +1093,6 @@ object SuperConnection {
implicit def superToConn(in: SuperConnection): Connection = in.connection
}

trait ConnectionIdentifier {
def jndiName: String

override def toString() = "ConnectionIdentifier(" + jndiName + ")"

override def hashCode() = jndiName.hashCode()

override def equals(other: Any): Boolean = other match {
case ci: ConnectionIdentifier => ci.jndiName == this.jndiName
case _ => false
}
}

case object DefaultConnectionIdentifier extends ConnectionIdentifier {
var jndiName = "lift"
}


/**
* The standard DB vendor.
* @param driverName the name of the database driver
Expand Down
3 changes: 2 additions & 1 deletion persistence/db/src/test/scala/net/liftweb/db/DBSpec.scala
@@ -1,5 +1,5 @@
/*
* Copyright 2011 WorldWide Conferencing, LLC
* Copyright 2011-2014 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@ import org.mockito.Matchers._

import net.liftweb.common._
import net.liftweb.db._
import net.liftweb.util.DefaultConnectionIdentifier
import net.liftweb.util.ControlHelpers._

import java.sql._
Expand Down
Expand Up @@ -1056,7 +1056,6 @@ trait MetaMapper[A<:Mapper[A]] extends BaseMetaMapper with Mapper[A] {
}
}


/**
* A set of CssSels that can be used to bind this MetaMapper's fields.
*
Expand Down
Expand Up @@ -17,13 +17,13 @@
package net.liftweb
package object mapper {
type SuperConnection = db.SuperConnection
type ConnectionIdentifier = db.ConnectionIdentifier
type ConnectionIdentifier = util.ConnectionIdentifier
type DriverType = db.DriverType
type ConnectionManager = db.ConnectionManager
type DBLogEntry = db.DBLogEntry
type StandardDBVendor = db.StandardDBVendor

def DBLogEntry = db.DBLogEntry
def DefaultConnectionIdentifier = db.DefaultConnectionIdentifier
def DefaultConnectionIdentifier = util.DefaultConnectionIdentifier
def DriverType = db.DriverType
}
Expand Up @@ -53,6 +53,17 @@ trait MongoMetaRecord[BaseRecord <: MongoRecord[BaseRecord]]
case x => x
}

/*
* Use the collection associated with this Meta.
*/
def useColl[T](f: DBCollection => T): T =
MongoDB.useCollection(connectionIdentifier, collectionName)(f)

/*
* Use the db associated with this Meta.
*/
def useDb[T](f: DB => T): T = MongoDB.use(connectionIdentifier)(f)

/**
* Delete the instance from backing store
*/
Expand Down
Expand Up @@ -81,7 +81,7 @@ class DBRefField[OwnerType <: BsonRecord[OwnerType], RefType <: MongoRecord[RefT
// assume string is json
def setFromString(in: String): Box[DBRef] = {
val dbo = JSON.parse(in).asInstanceOf[BasicDBObject]
MongoDB.use(ref.meta.mongoIdentifier) ( db => {
MongoDB.use(ref.meta.connectionIdentifier) ( db => {
val id = dbo.get("$id").toString
ObjectId.isValid(id) match {
case true => Full(set(new DBRef(db, dbo.get("$ref").toString, new ObjectId(id))))
Expand Down
Expand Up @@ -25,7 +25,7 @@ import common._
import json._
import json.ext.{EnumSerializer, JsonBoxSerializer}
import http.SHtml
import util.FieldError
import util.{FieldError, Helpers}

import java.math.MathContext
import scala.xml.Text
Expand Down Expand Up @@ -421,3 +421,11 @@ class JObjectFieldTestRecord private () extends MongoRecord[JObjectFieldTestReco
object JObjectFieldTestRecord extends JObjectFieldTestRecord with MongoMetaRecord[JObjectFieldTestRecord] {
override def formats = allFormats
}

class CustomFieldName private () extends MongoRecord[CustomFieldName] with ObjectIdPk[CustomFieldName] {
def meta = CustomFieldName

object customField extends StringField(this, 256)
}

object CustomFieldName extends CustomFieldName with MongoMetaRecord[CustomFieldName]
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 WorldWide Conferencing, LLC
* Copyright 2010-2014 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,7 +31,9 @@ import http.{S, LiftSession}
import http.js.JsExp
import json._
import JsonDSL._
import util.Helpers.snakify

import net.liftweb.record.RecordRules
import net.liftweb.record.field.Countries

import com.mongodb._
Expand Down Expand Up @@ -918,6 +920,15 @@ class MongoRecordSpec extends Specification with MongoTestKit {
rec.dirty_? must_== false
}
}

"support custom field name" in {
RecordRules.fieldName.doWith((_, name) => snakify(name)) {
val rec = CustomFieldName.createRecord
rec.customField.name must_== "custom_field"
rec.save()

CustomFieldName.find(rec.id.get) must_== Full(rec)
}
}
}
}

Expand Up @@ -18,6 +18,8 @@ package net.liftweb
package mongodb
package record

import util.{ConnectionIdentifier, DefaultConnectionIdentifier}

import org.specs2.mutable.Specification
import org.specs2.specification.BeforeAfterExample

Expand All @@ -35,7 +37,7 @@ trait MongoTestKit extends Specification with BeforeAfterExample {
def mongo = new MongoClient("127.0.0.1", 27017)

// If you need more than one db, override this
def dbs: List[(MongoIdentifier, String)] = List((DefaultMongoIdentifier, dbName))
def dbs: List[(ConnectionIdentifier, String)] = List((DefaultConnectionIdentifier, dbName))

def debug = false

Expand Down

0 comments on commit ad41071

Please sign in to comment.