Wiki source code of Squeryl _Entity.eotemplate

Last modified by Ravi Mendis on 2011/05/18 04:49

Show last authors
1 This set of templates will generate a Squeryl schema from an EO model. Still basic/proof-of-concept templates. Of course, you may customize them further.
2
3 {{note title="Limitations"}}
4 * Single-package/single-model schemas only
5 * No support for object inheritance
6 {{/note}}
7
8 {{code}}
9
10 // DO NOT EDIT. Make changes to ${entity.classNameWithOptionalPackage}.scala instead.
11 #if ($entity.superclassPackageName)
12 package $entity.superclassPackageName
13
14 #end
15 import org.squeryl.annotations._
16 import org.squeryl.KeyedEntity
17 import org.squeryl.PrimitiveTypeMode._
18 #if ($entity.primaryKeyAttributes.size() == 2)
19 import org.squeryl.dsl.CompositeKey2
20 #end
21 #if ($entity.model.sortedEntities.first().equals($entity))
22 import org.squeryl.Schema
23
24 /**
25 * DB schema for $entity.superclassPackageName - ideally should be in a separate file "package.scala"
26 */
27 protected object `package` extends Schema {
28 #foreach ($_entity in $entity.model.sortedEntities)
29 val ${_entity.name}s = table[$_entity.classNameWithoutPackage]("$_entity.externalName")
30 #end
31
32 // relations
33 #foreach ($_entity in $entity.model.sortedEntities)
34 #foreach ($relationship in $_entity.sortedToManyRelationships)
35 #if (!$relationship.definition)
36 val ${relationship.entity.name}To${relationship.destination.name}s = oneToManyRelation(${relationship.destination.name}s, ${relationship.entity.name}s).
37 via((x, y) => x.$relationship.firstJoin.destinationAttribute.name === y.$relationship.firstJoin.sourceAttribute.name)
38
39 #end
40 #end
41 #end
42 }
43 #end
44
45 import java.util.Date
46 #foreach ($relationship in $entity.sortedClassToOneRelationships)
47 #if ($relationship.actualDestination.packageName != $entity.packageName)
48 import $relationship.actualDestination.classNameWithDefault
49 #end
50 #end
51 #foreach ($relationship in $entity.sortedClassToManyRelationships)
52 #if ($relationship.actualDestination.packageName != $entity.packageName)
53 import $relationship.actualDestination.classNameWithDefault
54 #end
55 #end
56
57 /**
58 * $entity.classNameWithoutPackage
59 */
60 protected abstract class _$entity.classNameWithoutPackage (
61 ##
62 ##java -> Scala types go here
63 ##
64 #set ($hasOptionalAttribute = 0)
65 #foreach ($attribute in $entity.sortedAttributes)
66 #if ($attribute.javaClassName.equals("Integer")) #set( $scalaClassName = "Long" )
67 #elseif ($attribute.javaClassName.equals("String")) #set( $scalaClassName = "String" )
68 #elseif ($attribute.javaClassName.contains("BigDecimal")) #set( $scalaClassName = "BigDecimal" )
69 #elseif ($attribute.javaClassName.equals("NSTimestamp")) #set( $scalaClassName = "Date" )
70 #elseif ($attribute.javaClassName.equals("NSData")) #set( $scalaClassName = "Array[Byte]" ) #end
71 #if (!$attribute.columnName.equalsIgnoreCase($attribute.name)) @Column("$attribute.columnName")#end ${attribute.name}:#if ($attribute.allowsNull) Option[$scalaClassName]#else $scalaClassName#end#if (!$attribute.equals($entity.sortedAttributes.last())),
72 #else
73 #if ($attribute.allowsNull)#set ($hasOptionalAttribute = 1)#end
74 #end
75 #end
76
77 ) extends#if ($entity.primaryKeyAttributes.size() == 1) KeyedEntity[#if ($entity.singlePrimaryKeyAttribute.javaClassName.equals("String"))String#elseif ($entity.singlePrimaryKeyAttribute.javaClassName.equals("Integer"))Long#end]#else KeyedEntity[CompositeKey2[Long,Long]]#end {
78
79 // primaryKey
80 #if ($entity.primaryKeyAttributes.size() == 1)
81 def id = $entity.singlePrimaryKeyAttribute.name
82 #elseif ($entity.primaryKeyAttributes.size() == 2)
83 #set( $pkIndex = 0 ) def id = compositeKey(#foreach ($attribute in $entity.primaryKeyAttributes)$attribute.name#if ($pkIndex == 0), #end#set( $pkIndex = $pkIndex+1 )#end)
84 #end
85
86 #if ($hasOptionalAttribute == 1)
87 // zero-arg constructor necessary when there are optional fields
88 def this() = this(#foreach ($attribute in $entity.sortedAttributes)#if ($attribute.javaClassName.equals("Integer")) #set( $zeroValue = "0L" )
89 #elseif ($attribute.javaClassName.equals("String")) #set( $zeroValue = '""' )
90 #elseif ($attribute.javaClassName.contains("BigDecimal")) #set( $zeroValue = "0F" )
91 #elseif ($attribute.javaClassName.equals("NSTimestamp")) #set( $zeroValue = "new Date()" )
92 #elseif ($attribute.javaClassName.equals("NSData")) #set( $zeroValue = "new Array[Byte](0)" )#end#if ($attribute.allowsNull)Some($zeroValue)#else$zeroValue#end#if (!$attribute.equals($entity.sortedAttributes.last())), #end#end)
93 #end
94
95 #if (!$entity.abstractEntity)
96 #if ($entity.sortedToOneRelationships.size() > 0)
97 // toOne Relationships
98 #foreach ($relationship in $entity.sortedToOneRelationships)
99 #if (!$relationship.propagatesPrimaryKey && !$relationship.inverseRelationship.propagatesPrimaryKey)
100 lazy val to$relationship.destination.name = ${relationship.destination.name}To${relationship.entity.name}s.left(this.asInstanceOf[$relationship.entity.name])
101 def $relationship.name = to${relationship.destination.name}.#if ($relationship.mandatory)head
102 #elseif (!$relationship.mandatory)headOption
103 #end
104 #end
105 #end
106 #end
107 #end
108
109 #if ($entity.sortedClassToManyRelationships.size() > 0)
110 // toMany relationships
111 #foreach ($relationship in $entity.sortedClassToManyRelationships)
112 #if (!$relationship.definition)
113 lazy val $relationship.name = ${relationship.entity.name}To${relationship.destination.name}s.right(this.asInstanceOf[$relationship.entity.name])
114
115 #end
116 #end
117 #end
118 }
119
120
121 {{/code}}