#57 : worked on DSL

This commit is contained in:
Timo Westkämper 2011-11-28 22:29:43 +02:00
parent e9fb521566
commit d73281d48a
15 changed files with 176 additions and 136 deletions

BIN
querydsl-scala/.cache Normal file

Binary file not shown.

View File

@ -203,8 +203,10 @@
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
</classpathContainers>
<excludes>
<exclude>org.scala-lang:scala-library</exclude>
<exclude>org.scala-lang:scala-compiler</exclude>
<exclude>org.scala-lang:scala-library</exclude>
<!--
<exclude>org.scala-lang:scala-compiler</exclude>
-->
</excludes>
<sourceIncludes>
<sourceInclude>**/*.scala</sourceInclude>

View File

@ -14,6 +14,10 @@ import scala.reflect.BeanProperty
import scala.collection.JavaConversions._
import scala.collection.mutable.Set
/**
* @author tiwe
*
*/
class ScalaBeanSerializer @Inject() (typeMappings: TypeMappings) extends BeanSerializer(typeMappings) {
var javadocSuffix = " is a Querydsl bean type"
@ -40,6 +44,10 @@ class ScalaBeanSerializer @Inject() (typeMappings: TypeMappings) extends BeanSer
}
/**
* @author tiwe
*
*/
class CaseClassSerializer @Inject() (typeMappings: TypeMappings) extends BeanSerializer(typeMappings) {
def javaBeanSupport = false
@ -51,8 +59,11 @@ class CaseClassSerializer @Inject() (typeMappings: TypeMappings) extends BeanSer
}
}
/**
* @author tiwe
*
*/
abstract class BeanSerializer(typeMappings: TypeMappings) extends Serializer {
var createCompanionObject = true

View File

@ -68,8 +68,10 @@ class ScalaEntitySerializer @Inject()(val typeMappings: TypeMappings) extends Se
}
def writeAdditionalConstructors(modelName: String, writer: ScalaWriter) = {
writer.line("def this(variable: String) = this(classOf[",modelName,"], forVariable(variable))\n")
writer.line("def this(parent: Path[_], variable: String) = this(classOf[",modelName,"], forProperty(parent, variable))\n")
writer.line("def this(variable: String) = this(classOf[",
modelName,"], forVariable(variable))\n")
writer.line("def this(parent: Path[_], variable: String) = this(classOf[",
modelName,"], forProperty(parent, variable))\n")
}
def writeAdditionalFields(model: EntityType, writer: ScalaWriter) = {

View File

@ -28,7 +28,7 @@ object Constants {
}
trait SimpleExpression[T] extends Expression[T]{
trait SimpleExpression[T] extends Expression[T] {
def as(right: Path[T]): SimpleExpression[T] = simple(getType, ALIAS.asInstanceOf[Operator[T]], this, right)

View File

@ -9,6 +9,12 @@ import TypeDefs._
object GroupBy extends GroupBy
/**
* Group by result transforming functionality
*
* @author tiwe
*
*/
trait GroupBy {
def groupBy[K](key: Ex[K]) = new GroupByBuilder[K](key)
@ -60,7 +66,7 @@ class GSet[T](e: Ex[T]) extends AbstractGroupExpression[T, Set[T]](classOf[Set[T
}
}
class GMap[K,V](tu2ex: Tu2Ex[K,V,_,_]) extends AbstractGroupExpression[(K,V), Map[K,V]](classOf[Map[K,V]], tu2ex) {
class GMap[K,V](tu2ex: Tu2Ex[K,V]) extends AbstractGroupExpression[(K,V), Map[K,V]](classOf[Map[K,V]], tu2ex) {
def createGroupCollector() = {
new GroupCollector[(K,V), Map[K,V]]() {

View File

@ -17,6 +17,11 @@ trait Helpers extends Projections with GroupBy {
implicit def toRichProjectable(p: Projectable) = new RichProjectable(p)
}
/**
* @author tiwe
*
* @param <T>
*/
class RichSimpleProjectable[T](private val p: SimpleProjectable[T]) {
def select: List[T] = p.list.toList
@ -29,6 +34,10 @@ class RichSimpleProjectable[T](private val p: SimpleProjectable[T]) {
}
/**
* @author tiwe
*
*/
class RichProjectable(private val p: Projectable) {
def select[T](e: Ex[T]): List[T] = p.list(e).toList

View File

@ -7,54 +7,53 @@ import TypeDefs._
object Projections extends Projections
/**
* @author tiwe
*
*/
trait Projections {
// TODO : simplify this
implicit def tuple2Expr[T1,T2,E1 <: Ex[T1],E2 <: Ex[T2]](t: (Ex[T1] with E1,Ex[T2] with E2)) = {
new Tuple2Expression[T1,T2,E1,E2](t._1, t._2)
implicit def tuple2Expr[T1,T2](t: (_ <: Ex[T1], _ <: Ex[T2])) = new Tu2Ex[T1,T2](t._1, t._2)
implicit def tuple3Expr[T1,T2,T3](t: (_ <: Ex[T1], _ <: Ex[T2], _ <: Ex[T3])) = {
new Tu3Ex[T1,T2,T3](t._1, t._2, t._3)
}
implicit def tuple3Expr[T1,T2,T3,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3]](t: (Ex[T1] with E1,Ex[T2] with E2,Ex[T3] with E3)) = {
new Tuple3Expression[T1,T2,T3,E1,E2,E3](t._1, t._2, t._3)
}
implicit def tuple4Expr[T1,T2,T3,T4,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3],E4 <: Ex[T4]](t: (Ex[T1] with E1,Ex[T2] with E2,Ex[T3] with E3,Ex[T4] with E4)) = {
new Tuple4Expression[T1,T2,T3,T4,E1,E2,E3,E4](t._1, t._2, t._3, t._4)
implicit def tuple4Expr[T1,T2,T3,T4](t: (_ <: Ex[T1], _ <: Ex[T2], _ <: Ex[T3], _ <: Ex[T4])) = {
new Tu4Ex[T1,T2,T3,T4](t._1, t._2, t._3, t._4)
}
implicit def tuple5Expr[T1,T2,T3,T4,T5,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3],E4 <: Ex[T4],E5 <: Ex[T5]](t: (E1 with Ex[T1],E2 with Ex[T2],E3 with Ex[T3],E4 with Ex[T4],E5 with Ex[T5])) = {
new Tuple5Expression[T1,T2,T3,T4,T5,E1,E2,E3,E4,E5](t._1, t._2, t._3, t._4, t._5)
implicit def tuple5Expr[T1,T2,T3,T4,T5](t: (_ <: Ex[T1], _ <: Ex[T2], _ <: Ex[T3], _ <: Ex[T4], _<: Ex[T5])) = {
new Tu5Ex[T1,T2,T3,T4,T5](t._1, t._2, t._3, t._4, t._5)
}
}
class Tuple2Expression[T1,T2,E1 <: Ex[T1], E2 <: Ex[T2]](val _1: E1, val _2: E2)
extends FactoryExpressionBase[(T1,T2)](classOf[Tuple2[T1,T2]], _1, _2) {
class Tuple2Expression[T1,T2](args: Ex[_]*)
extends FactoryExpressionBase[(T1,T2)](classOf[Tuple2[T1,T2]], args:_*) {
def newInstance(args: AnyRef*): (T1,T2) = {
(args(0).asInstanceOf[T1], args(1).asInstanceOf[T2])
}
}
class Tuple3Expression[T1,T2,T3,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3]](val _1: E1, val _2: E2, val _3: E3)
extends FactoryExpressionBase[Tuple3[T1,T2,T3]](classOf[Tuple3[T1,T2,T3]], _1, _2, _3) {
class Tuple3Expression[T1,T2,T3](args: Ex[_]*)
extends FactoryExpressionBase[Tuple3[T1,T2,T3]](classOf[Tuple3[T1,T2,T3]], args:_*) {
def newInstance(args: AnyRef*): (T1,T2,T3) = {
(args(0).asInstanceOf[T1], args(1).asInstanceOf[T2], args(2).asInstanceOf[T3])
}
}
class Tuple4Expression[T1,T2,T3,T4,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3],E4 <: Ex[T4]](val _1: E1, val _2: E2, val _3: E3, val _4: E4)
extends FactoryExpressionBase[Tuple4[T1,T2,T3,T4]](classOf[Tuple4[T1,T2,T3,T4]], _1, _2, _3, _4) {
class Tuple4Expression[T1,T2,T3,T4](args: Ex[_]*)
extends FactoryExpressionBase[Tuple4[T1,T2,T3,T4]](classOf[Tuple4[T1,T2,T3,T4]], args:_*) {
def newInstance(args: AnyRef*): (T1,T2,T3,T4) = {
(args(0).asInstanceOf[T1], args(1).asInstanceOf[T2], args(2).asInstanceOf[T3], args(3).asInstanceOf[T4])
}
}
class Tuple5Expression[T1,T2,T3,T4,T5,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3],E4 <: Ex[T4],E5 <: Ex[T5]](val _1: E1, val _2: E2, val _3: E3, val _4: E4, val _5: E5)
extends FactoryExpressionBase[Tuple5[T1,T2,T3,T4,T5]](classOf[Tuple5[T1,T2,T3,T4,T5]], _1, _2, _3, _4, _5) {
class Tuple5Expression[T1,T2,T3,T4,T5](args: Ex[_]*)
extends FactoryExpressionBase[Tuple5[T1,T2,T3,T4,T5]](classOf[Tuple5[T1,T2,T3,T4,T5]], args:_*) {
def newInstance(args: AnyRef*): (T1,T2,T3,T4,T5) = {
(args(0).asInstanceOf[T1], args(1).asInstanceOf[T2], args(2).asInstanceOf[T3], args(3).asInstanceOf[T4], args(4).asInstanceOf[T5])
@ -67,4 +66,6 @@ abstract class FactoryExpressionBase[T](cl: Class[T], args: Ex[_]*) extends Expr
def getArgs(): JavaList[Ex[_]] = Arrays.asList(args:_*)
def apply(i: Int): Ex[_] = args(i)
}

View File

@ -7,49 +7,65 @@ import TypeDefs._
import com.mysema.codegen.model.TypeCategory
// TODO : factory object for template creation
/**
* Factory for templates
*
* @author tiwe
*
*/
object Templates {
class SimpleTemplate[T](t: Class[_ <: T], template: Template, args: java.util.List[Ex[_]])
extends TemplateExpressionImpl[T](t, template, args) with SimpleExpression[T] {
def simple[T](t: Class[_ <: T], tpl: Template, args: Ex[_]*): SimpleExpression[T] = new SimpleTemplate[T](t, tpl, args: _*)
def comparable[T <: Comparable[_]](t: Class[_ <: T], tpl: Template, args: Ex[_]*): ComparableExpression[T] = new ComparableTemplate[T](t, tpl, args: _*)
def date[T <: Comparable[_]](t: Class[_ <: T], tpl: Template, args: Ex[_]*): DateExpression[T] = new DateTemplate[T](t, tpl, args: _*)
def dateTime[T <: Comparable[_]](t: Class[_ <: T], tpl: Template, args: Ex[_]*): DateTimeExpression[T] = new DateTimeTemplate[T](t, tpl, args: _*)
def time[T <: Comparable[_]](t: Class[_ <: T], tpl: Template, args: Ex[_]*): TimeExpression[T] = new TimeTemplate[T](t, tpl, args: _*)
def number[T <: Number with Comparable[T]](t: Class[_ <: T], tpl: Template, args: Ex[_]*): NumberExpression[T] = new NumberTemplate[T](t, tpl, args: _*)
def boolean(tpl: Template, args: Ex[_]*): BooleanExpression = new BooleanTemplate(tpl, args: _*)
def string(tpl: Template, args: Ex[_]*): StringExpression = new StringTemplate(tpl, args: _*)
def enum[T <: Enum[T]](t: Class[T], tpl: Template, args: Ex[_]*): EnumExpression[T] = new EnumTemplate[T](t, tpl, args: _*)
}
class ComparableTemplate[T <: Comparable[_]](t: Class[_ <: T], template: Template, args: java.util.List[Ex[_]])
extends TemplateExpressionImpl[T](t, template, args) with ComparableExpression[T] {
class SimpleTemplate[T](t: Class[_ <: T], template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[T](t, template, args:_*) with SimpleExpression[T]
}
class NumberTemplate[T <: Number with Comparable[T]](t: Class[_ <: T], template: Template, args: java.util.List[Ex[_]])
extends TemplateExpressionImpl[T](t, template, args) with NumberExpression[T] {
class ComparableTemplate[T <: Comparable[_]](t: Class[_ <: T], template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[T](t, template, args:_*) with ComparableExpression[T]
}
class BooleanTemplate(template: Template, args: java.util.List[Expression[_]])
extends TemplateExpressionImpl[java.lang.Boolean](classOf[java.lang.Boolean], template, args) with BooleanExpression {
class NumberTemplate[T <: Number with Comparable[T]](t: Class[_ <: T], template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[T](t, template, args:_*) with NumberExpression[T]
}
class StringTemplate(template: Template, args: java.util.List[Ex[_]])
extends TemplateExpressionImpl[String](classOf[String], template, args) with StringExpression {
class BooleanTemplate(template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[java.lang.Boolean](classOf[java.lang.Boolean], template, args:_*) with BooleanExpression
}
class StringTemplate(template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[String](classOf[String], template, args:_*) with StringExpression
class DateTemplate[T <: Comparable[_]](t: Class[_ <: T], template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[T](t, template, args:_*) with DateExpression[T]
class DateTemplate[T <: Comparable[_]](t: Class[_ <: T], template: Template, args: java.util.List[Ex[_]])
extends TemplateExpressionImpl[T](t, template, args) with DateExpression[T] {
}
class DateTimeTemplate[T <: Comparable[_]](t: Class[_ <: T], template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[T](t, template, args:_*) with DateTimeExpression[T]
class DateTimeTemplate[T <: Comparable[_]](t: Class[_ <: T], template: Template, args: java.util.List[Ex[_]])
extends TemplateExpressionImpl[T](t, template, args) with DateTimeExpression[T] {
}
class TimeTemplate[T <: Comparable[_]](t: Class[_ <: T], template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[T](t, template, args:_*) with TimeExpression[T]
class TimeTemplate[T <: Comparable[_]](t: Class[_ <: T], template: Template)
extends TemplateExpressionImpl[T](t, template) with TimeExpression[T] {
}
class EnumTemplate[T <: Enum[T]](t: Class[_ <: T], template: Template)
extends TemplateExpressionImpl[T](t, template) with EnumExpression[T] {
}
class EnumTemplate[T <: Enum[T]](t: Class[_ <: T], template: Template, args: Ex[_]*)
extends TemplateExpressionImpl[T](t, template, args:_*) with EnumExpression[T]

View File

@ -6,13 +6,13 @@ object TypeDefs {
type Ex[T] = Expression[T]
type Tu2Ex[T1,T2,E1 <: Ex[T1],E2 <: Ex[T2]] = Tuple2Expression[T1,T2,E1,E2]
type Tu2Ex[T1,T2] = Tuple2Expression[T1,T2]
type Tu3Ex[T1,T2,T3,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3]] = Tuple3Expression[T1,T2,T3,E1,E2,E3]
type Tu3Ex[T1,T2,T3] = Tuple3Expression[T1,T2,T3]
type Tu4Ex[T1,T2,T3,T4,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3],E4 <: Ex[T4]] = Tuple4Expression[T1,T2,T3,T4,E1,E2,E3,E4]
type Tu4Ex[T1,T2,T3,T4] = Tuple4Expression[T1,T2,T3,T4]
type Tu5Ex[T1,T2,T3,T4,T5,E1 <: Ex[T1],E2 <: Ex[T2],E3 <: Ex[T3],E4 <: Ex[T4], E5 <: Ex[T5]] = Tuple5Expression[T1,T2,T3,T4,T5,E1,E2,E3,E4,E5]
type Tu5Ex[T1,T2,T3,T4,T5] = Tuple5Expression[T1,T2,T3,T4,T5]
type JavaList[T] = java.util.List[T]

View File

@ -16,16 +16,18 @@ import com.mysema.query.scala.TypeDefs._
class RelationalPathImpl[T](md: PathMetadata[_], schema: String, table: String)(implicit val mf: Manifest[T])
extends BeanPath[T](mf.erasure.asInstanceOf[Class[T]], md) with RelationalPath[T] {
type JList[X] = java.util.List[X]
private var primaryKey: PrimaryKey[T] = _
@BeanProperty
val columns: java.util.List[Path[_]] = new ArrayList[Path[_]]
val columns: JList[Path[_]] = new ArrayList[Path[_]]
@BeanProperty
val foreignKeys: java.util.List[ForeignKey[_]] = new ArrayList[ForeignKey[_]]
val foreignKeys: JList[ForeignKey[_]] = new ArrayList[ForeignKey[_]]
@BeanProperty
val inverseForeignKeys: java.util.List[ForeignKey[_]] = new ArrayList[ForeignKey[_]]
val inverseForeignKeys: JList[ForeignKey[_]] = new ArrayList[ForeignKey[_]]
def this(variable: String, schema: String, table: String)(implicit mf: Manifest[T]) = this(forVariable(variable), schema, table)(mf)

View File

@ -11,79 +11,86 @@ import com.mysema.query.scala.TypeDefs._
import com.mysema.query.scala.RichProjectable
import com.mysema.query.scala.Projections._
object RichSimpleQuery {
class RichSimpleQuery[PT, P <: RelationalPath[PT], T, E <: Ex[T]](path: P, expr: E, qry: SQLQuery)
extends RichProjectable(qry) {
type RP[T] = RelationalPath[T]
// TODO : use closures instead of ExprToTarget
implicit def pathToTuple2[T,E <: RP[T],T2,E2 <: RP[T2]] = {
new ExprToTarget[T,E,T2,E2,(T,T2),Tu2Ex[T,T2,E,E2]]() {
def toTarget(e: E, rp: E2) = new Tu2Ex[T,T2,E,E2](e,rp)
}}
implicit def tuple2ToTuple3[T1,E1 <: Ex[T1],T2,E2 <: Ex[T2],T3,E3 <: RP[T3]] = {
new ExprToTarget[(T1,T2),Tu2Ex[T1,T2,E1,E2],T3,E3,(T1,T2,T3),Tu3Ex[T1,T2,T3,E1,E2,E3]]() {
def toTarget(e: Tu2Ex[T1,T2,E1,E2], rp: E3) = new Tu3Ex[T1,T2,T3,E1,E2,E3](e._1, e._2, rp)
}}
}
// TODO : define both callback (RelationalPath[_]) and projection expression (Ex[_])
class RichSimpleQuery[T, E <: Ex[T]](expr: E, qry: SQLQuery) extends RichProjectable(qry) {
type FEx[X] = P => Ex[X]
def query = this
// TODO : simplify these
def join[T2, R2 <: RelationalPath[T2], T3, E3 <: Ex[T3]](f : E => ForeignKey[T2], rp: R2)
(implicit e2t: ExprToTarget[T,E,T2,R2,T3,E3]): RichSimpleQuery[T3,E3] = join(f(expr), rp)
(implicit e2t: ExprToTarget[T,E,T2,R2,T3,E3]): RichSimpleQuery[PT,P,T3,E3] = join(f(expr), rp)
def join[T2, R2 <: RelationalPath[T2], T3, E3 <: Ex[T3]](fk: ForeignKey[T2], rp: R2)
(implicit e2t: ExprToTarget[T,E,T2,R2,T3,E3]): RichSimpleQuery[T3,E3] = {
new RichSimpleQuery[T3,E3](e2t.toTarget(expr, rp), qry.innerJoin(fk, rp))
(implicit e2t: ExprToTarget[T,E,T2,R2,T3,E3]): RichSimpleQuery[PT,P,T3,E3] = {
new RichSimpleQuery[PT,P,T3,E3](path, e2t.toTarget(expr, rp), qry.innerJoin(fk, rp))
}
def limit(l: Long) = { qry.limit(l); this }
def offset(l: Long) = { qry.offset(l); this }
def orderBy(f: E => OrderSpecifier[_]) = { qry.orderBy(f(expr)); this }
def orderBy(f: P => OrderSpecifier[_]) = { qry.orderBy(f(path)); this }
def orderBy(o: OrderSpecifier[_]*) = { qry.orderBy(o:_*); this }
def where(f: E => Predicate) = { qry.where(f(expr)); this }
def where(f: P => Predicate) = { qry.where(f(path)); this }
def where(predicates: Predicate*) = { qry.where(predicates:_*); this }
// projection
def all: List[T] = select
def count = qry.count()
def all: List[T] = select
def select: List[T] = select(expr)
def select[T](f: E => Ex[T]): List[T] = select(f(expr))
def select[T](f: FEx[T]): List[T] = select(f(path))
def select[T,U](f1: E => Ex[T], f2: E => Ex[U]): List[(T,U)] = select(f1(expr), f2(expr))
def select[T,U](f1: FEx[T], f2: FEx[U]): List[(T,U)] = select(f1(path), f2(path))
def select[T,U,V](f1: E => Ex[T], f2: E => Ex[U], f3:E => Ex[V]): List[(T,U,V)] = {
select(f1(expr), f2(expr), f3(expr))
def select[T,U,V](f1: FEx[T], f2: FEx[U], f3: FEx[V]): List[(T,U,V)] = {
select(f1(path), f2(path), f3(path))
}
def select[T,U,V,W](f1: E => Ex[T], f2: E => Ex[U], f3: E => Ex[V], f4: E => Ex[W]): List[(T,U,V,W)] = {
select(f1(expr), f2(expr), f3(expr), f4(expr))
def select[T,U,V,W](f1: FEx[T], f2: FEx[U], f3: FEx[V], f4: FEx[W])
: List[(T,U,V,W)] = {
select(f1(path), f2(path), f3(path), f4(path))
}
def select[T,U,V,W,X](f1: E => Ex[T], f2: E => Ex[U], f3: E => Ex[V], f4: E => Ex[W], f5: E => Ex[X]): List[(T,U,V,W,X)] = {
select(f1(expr), f2(expr), f3(expr), f4(expr), f5(expr))
def select[T,U,V,W,X](f1: FEx[T], f2: FEx[U], f3: FEx[V], f4: FEx[W], f5: FEx[X])
: List[(T,U,V,W,X)] = {
select(f1(path), f2(path), f3(path), f4(path), f5(path))
}
def single: Option[T] = single(expr)
def single[T](f: E => Ex[T]): Option[T] = single(f(expr))
def single[T](f: FEx[T]): Option[T] = single(f(path))
def unique: Option[T] = unique(expr)
def unique[T](f: E => Ex[T]): Option[T] = unique(f(expr))
def unique[T](f: FEx[T]): Option[T] = unique(f(path))
}
object ExprToTarget {
type RP[T] = RelationalPath[T]
// TODO : use closures instead of ExprToTarget
implicit def pathToTuple2[T,E <: RP[T],T2,E2 <: RP[T2]] = {
new ExprToTarget[T,E,T2,E2,(T,T2),Tu2Ex[T,T2]]() {
def toTarget(e: E, rp: E2) = new Tu2Ex[T,T2](e,rp)
}}
implicit def tuple2ToTuple3[T1,E1 <: Ex[T1],T2,E2 <: Ex[T2],T3,E3 <: RP[T3]] = {
new ExprToTarget[(T1,T2),Tu2Ex[T1,T2],T3,E3,(T1,T2,T3),Tu3Ex[T1,T2,T3]]() {
def toTarget(e: Tu2Ex[T1,T2], rp: E3) = new Tu3Ex[T1,T2,T3](e(0), e(1), rp)
}}
}

View File

@ -13,7 +13,7 @@ trait SQLHelpers {
def templates: SQLTemplates
implicit def toRichSimpleQuery[T, R <: RelationalPath[T]](p: RelationalPath[T] with R) = {
new RichSimpleQuery[T, R](p, new SQLQueryImpl(connection, templates).from(p) )
new RichSimpleQuery[T, R, T, R](p, p, new SQLQueryImpl(connection, templates).from(p) )
}
}

View File

@ -72,46 +72,32 @@ class ScalaMetaDataSerializer @Inject() (typeMappings: TypeMappings, val namingS
}
def serializePrimaryKeys(model: EntityType, writer: CodeWriter, primaryKeys: Collection[PrimaryKeyData]) {
primaryKeys foreach { primaryKey =>
val fieldName = namingStrategy.getPropertyNameForPrimaryKey(primaryKey.getName(), model)
val value = new StringBuilder("createPrimaryKey(")
value.append(primaryKey.getColumns().map({ column =>
escape(namingStrategy.getPropertyName(column, model))
}).mkString(", "))
value.append(")")
writer.publicFinal(new ClassType(classOf[PrimaryKey[_]], model), fieldName, value.toString)
primaryKeys.foreach { pk =>
val fieldName = namingStrategy.getPropertyNameForPrimaryKey(pk.getName(), model)
val value = pk.getColumns.map(c => escape(namingStrategy.getPropertyName(c, model)))
.mkString("createPrimaryKey(", ", ", ")")
writer.publicFinal(new ClassType(classOf[PrimaryKey[_]], model), fieldName, value)
}
}
def serializeForeignKeys(model: EntityType, writer: CodeWriter, foreignKeys: Collection[_ <: KeyData], inverse: Boolean) {
foreignKeys foreach { foreignKey =>
var fieldName: String = null
if (inverse) {
fieldName = namingStrategy.getPropertyNameForInverseForeignKey(foreignKey.getName, model)
foreignKeys.foreach { fk =>
val fieldName = if (inverse) {
namingStrategy.getPropertyNameForInverseForeignKey(fk.getName, model)
} else {
fieldName = namingStrategy.getPropertyNameForForeignKey(foreignKey.getName, model)
namingStrategy.getPropertyNameForForeignKey(fk.getName, model)
}
val value = new StringBuilder(if (inverse) "createInvForeignKey(" else "createForeignKey(")
if (foreignKey.getForeignColumns.size == 1) {
value.append(namingStrategy.getPropertyName(foreignKey.getForeignColumns.get(0), model))
value.append(", \"" + foreignKey.getParentColumns().get(0) + "\"")
if (fk.getForeignColumns.size == 1) {
value.append(namingStrategy.getPropertyName(fk.getForeignColumns.get(0), model))
value.append(", \"" + fk.getParentColumns().get(0) + "\"")
} else {
val local = new StringBuilder()
val foreign = new StringBuilder()
var i = 0
while (i < foreignKey.getForeignColumns().size()) {
if (i > 0) {
local.append(", ")
foreign.append(", ")
}
local.append(escape(namingStrategy.getPropertyName(foreignKey.getForeignColumns().get(0), model)))
foreign.append("\"" + foreignKey.getParentColumns.get(0) + "\"")
i += 1
}
val local = fk.getForeignColumns.map(c => escape(namingStrategy.getPropertyName(c, model))).mkString(", ")
val foreign = fk.getParentColumns.map(c => "\"" + c + "\"").mkString(", ")
value.append("Arrays.asList(" + local + "), Arrays.asList(" + foreign + ")")
}
value.append(")")
val t = new ClassType(classOf[ForeignKey[_]], foreignKey.getType)
val t = new ClassType(classOf[ForeignKey[_]], fk.getType)
writer.publicFinal(t, fieldName, value.toString())
}
}

View File

@ -9,8 +9,6 @@ import test._
class QueriesTest extends SQLHelpers {
import RichSimpleQuery._
val templates = new H2Templates()
def connection: Connection = null
@ -46,7 +44,7 @@ class QueriesTest extends SQLHelpers {
val sup2 = Employee as "sup2"
assertEquals(
"from EMPLOYEE employee\ninner join EMPLOYEE sup\non employee.SUPERIOR_ID = sup.ID\ninner join EMPLOYEE sup2\non sup.SUPERIOR_ID = sup2.ID",
Employee.join(_.superiorFk, sup).join(_._2.superiorFk, sup2).toString)
Employee.join(_.superiorFk, sup).join(sup.superiorFk, sup2).toString)
}
@Test
@ -55,7 +53,7 @@ class QueriesTest extends SQLHelpers {
val sup2 = Employee as "sup2"
assertEquals(
"from EMPLOYEE employee\ninner join EMPLOYEE sup\non employee.SUPERIOR_ID = sup.ID\nwhere employee.ID = ?",
Employee.join(_.superiorFk, sup).where( _._1.id eq 1).toString)
Employee.join(_.superiorFk, sup).where( _.id eq 1).toString)
}
@Test
@ -64,7 +62,7 @@ class QueriesTest extends SQLHelpers {
val sup2 = Employee as "sup2"
assertEquals(
"from EMPLOYEE employee\ninner join EMPLOYEE sup\non employee.SUPERIOR_ID = sup.ID\nwhere employee.ID = sup.ID",
Employee.join(_.superiorFk, sup).where(e => e._1.id eq e._2.id).toString)
Employee.join(_.superiorFk, sup).where(_.id eq sup.id).toString)
}
}