Squeryl _Entity.eotemplate

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

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.

Warning

Limitations

  • Single-package/single-model schemas only
  • No support for object inheritance

// DO NOT EDIT.  Make changes to ${entity.classNameWithOptionalPackage}.scala instead.
#if ($entity.superclassPackageName)
package $entity.superclassPackageName

#end
import org.squeryl.annotations._
import org.squeryl.KeyedEntity
import org.squeryl.PrimitiveTypeMode._
#if ($entity.primaryKeyAttributes.size() == 2)
import org.squeryl.dsl.CompositeKey2
#end
#if ($entity.model.sortedEntities.first().equals($entity))
import org.squeryl.Schema

/**
* DB schema for $entity.superclassPackageName - ideally should be in a separate file "package.scala"
*/
protected object `package` extends Schema {
#foreach ($_entity in $entity.model.sortedEntities)
    val ${_entity.name}s = table[$_entity.classNameWithoutPackage]("$_entity.externalName")
#end

// relations
#foreach ($_entity in $entity.model.sortedEntities)
#foreach ($relationship in $_entity.sortedToManyRelationships)
#if (!$relationship.definition)
val ${relationship.entity.name}To${relationship.destination.name}s = oneToManyRelation(${relationship.destination.name}s, ${relationship.entity.name}s).
via((x, y) => x.$relationship.firstJoin.destinationAttribute.name === y.$relationship.firstJoin.sourceAttribute.name)

#end
#end
#end
}
#end

import java.util.Date
#foreach ($relationship in $entity.sortedClassToOneRelationships)
#if ($relationship.actualDestination.packageName != $entity.packageName)
import $relationship.actualDestination.classNameWithDefault
#end
#end
#foreach ($relationship in $entity.sortedClassToManyRelationships)
#if ($relationship.actualDestination.packageName != $entity.packageName)
import $relationship.actualDestination.classNameWithDefault
#end
#end

/**
* $entity.classNameWithoutPackage
*/
protected abstract class _$entity.classNameWithoutPackage (
##
##java -> Scala types go here
##
#set ($hasOptionalAttribute = 0)
#foreach ($attribute in $entity.sortedAttributes)
#if ($attribute.javaClassName.equals("Integer")) #set( $scalaClassName = "Long" )
#elseif ($attribute.javaClassName.equals("String")) #set( $scalaClassName = "String" )
#elseif ($attribute.javaClassName.contains("BigDecimal")) #set( $scalaClassName = "BigDecimal" )
#elseif ($attribute.javaClassName.equals("NSTimestamp")) #set( $scalaClassName = "Date" )
#elseif ($attribute.javaClassName.equals("NSData")) #set( $scalaClassName = "Array[Byte]" ) #end
#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())),
#else
#if ($attribute.allowsNull)#set ($hasOptionalAttribute = 1)#end
#end
#end

) 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 {

// primaryKey
#if ($entity.primaryKeyAttributes.size() == 1)
def id = $entity.singlePrimaryKeyAttribute.name
#elseif ($entity.primaryKeyAttributes.size() == 2)
#set( $pkIndex = 0 ) def id = compositeKey(#foreach ($attribute in $entity.primaryKeyAttributes)$attribute.name#if ($pkIndex == 0), #end#set( $pkIndex = $pkIndex+1 )#end)
#end

#if ($hasOptionalAttribute == 1)
// zero-arg constructor necessary when there are optional fields
def this() = this(#foreach ($attribute in $entity.sortedAttributes)#if ($attribute.javaClassName.equals("Integer")) #set( $zeroValue = "0L" )
#elseif ($attribute.javaClassName.equals("String")) #set( $zeroValue = '""' )
#elseif ($attribute.javaClassName.contains("BigDecimal")) #set( $zeroValue = "0F" )
#elseif ($attribute.javaClassName.equals("NSTimestamp")) #set( $zeroValue = "new Date()" )
#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)
#end

#if (!$entity.abstractEntity)
#if ($entity.sortedToOneRelationships.size() > 0)
// toOne Relationships
#foreach ($relationship in $entity.sortedToOneRelationships)
#if (!$relationship.propagatesPrimaryKey && !$relationship.inverseRelationship.propagatesPrimaryKey)
lazy val to$relationship.destination.name = ${relationship.destination.name}To${relationship.entity.name}s.left(this.asInstanceOf[$relationship.entity.name])
def $relationship.name = to${relationship.destination.name}.#if ($relationship.mandatory)head 
#elseif (!$relationship.mandatory)headOption
#end
#end
#end
#end
#end

#if ($entity.sortedClassToManyRelationships.size() > 0)
// toMany relationships
#foreach ($relationship in $entity.sortedClassToManyRelationships)
#if (!$relationship.definition)
lazy val $relationship.name = ${relationship.entity.name}To${relationship.destination.name}s.right(this.asInstanceOf[$relationship.entity.name])

#end
#end
#end
}