Squeryl = SQL-like DSL in Scala
Advantages of Squeryl over EOF:
- Concurrent
- Spawns multiple database connections
- Issues database transactions concurrently
- Scala Actor compatible
- Immutable object model/graph
- Explicit transaction control
- Type Safety
- Better suited for database/business "logic".
E.g: Exploits the compiler and IDE to catch exceptions at compile time rather than at run-time.
- Better suited for database/business "logic".
- Uses Scala Collections
Migrating EOF -> Squeryl
In contrast to EOF Squeryl maintains its ORM information programmatically - in the classes itself and collectively in a schema. In keeping with the strongly-typed philosophy of Scala, Squeryl has no dynamic component like EOF (i.e an EO model file).
EOF has the ability to generate classes in Java (and in Objective-C prior to WebObjects 4.5) because enforcing type has become customary in enterprise environments. We may exploit this feature of EOF to generate a Squeryl schema from an EO model.
Preparing your EO model
- Make sure all EO entities have a class name (including abstract many-To-many "join" tables). FYI: There can be no support for entities classified as
EOGenericRecord
. - Mark the abstract many-To-Many join entities as
Abstract
. - (Temporary) Ensure all the model entities are in the same package. i.e the package is exclusive to the model.
Generating the Squeryl Schema
- Create a .eogen file for your EO model as normal. Only set the
File Names
extension to "scala". - Use the Squeryl EO Templates:
Note
Any custom business "logic" will have to be manually re-written in Scala
Differences Between a Squeryl Schema and EO Model/Classes
- Optional attributes (i.e those that
allowsNull
) are typed asOption[WO:T]
- To one relationships that are not mandatory (i.e optional relationships) are also typed as
Option[WO:T]
- To many relationships are represented as a Squeryl iterable (collection class) as opposed to a
NSArray
.
Use of Scala Collections
1. Filtering
Instead of using EOQualifiers to filter EOs dynamically, you can apply the type safe filter in Scala:
def activeFiles = files.filter(_.active == true)
2. Iteration
Functional language iteration that's become increasingly popular can be used:
activeFiles.foreach(f => { ... })
3. For-Comprehensions
Here's just an example use of sequence comprehension:
def activeFiles = for (file <- files if file.active == true) yield file