Scala classes are mapped to tables via instances of
that are grouped in a org.squeryl.Schema singleton.
Columns and group of columns can have attributes declared via the
on/declare syntax. Inside the example schema defined above, we could
influence the DDL generation with the following declarations :
Of course, not all combinations of column attributes make sense, the
valid combinations are :
Schema.create will connect to the database and create all tables,
constraints, indexes, etc. While this is usefull for development phases,
when a system has been in production long enough it is often more
convenient to generate the schema, and evolve it manually.
Use Schema.printDdl to print your schema :
Squeryl applies the principle of Convention over Configuration :
The class to table and field to column correspondence is determined
by name equivalence. It is possible to override a field’s column name
with the org.squeryl.annotations.Column annotations and the class’s
table name table with the
method. as illustrated in the previous example.
The Column annotation can also be used to redefine default length
for String/varchar columns (and also for other types although it
should rarely be necessary).
The default (and strongly recommended) way of mapping nullable columns
to fields is with the Option type. If you use Squeryl to create
(or generate) your schema, all fields have a not null constraint,
and Option fields are nullable.
Enumerations are persisted by ‘int’ columns
You will have to decide if your table objects will be mapped with
primitive (Int. Long, Date, String etc.) or custom types. It’s a
question of tradeoffs :
The main motivations for using primitive types are for performance and
If a query returns N rows of objects with M fields primitive types
will cause the the garbage collector to handle of N objects, while
query using custom types will cause the creation of N * M objects.
To use primitive types, simply import org.squeryl.PrimitiveTypeMode._
in the scope where database objects and queries are defined :
that’s all there is to it.
When using org.squeryl.PrimitiveTypeMode, the compiler will treat an
expression like the
one in the next example as a Boolean. The .~ function is needed to tell
the compiler that the
left side is a node of TypedExpressionNode[Int] which will cause the
whole expression to be a
LogicalBoolean which is what the where clause takes :
It is also needed in the following case :
This is required when using PrimitiveType mode. With custom types
there is no ambiguity, since custom types are not (AnyVal) numerics.
One motivation for using custom wrapper types is to allow fields
to carry meta data along with validation, as in the next example.
Custom field types must inherit one of the subtypes of CustomType in the
org.squeryl.customtypes, and import the
into the scope where statements are defined.