|
1. Defining the Object Model
1.2 Specifying a Parameterised Class
A parameterised class defines not only the class of object held, but also additional information that may be required in relation to the class. A parameterised class is actually an instance of (a subclass of) SSWDBClassWithInfo. However you do not usually need to create this directly. Instead, classes that support parameterisation define helper methods that create the relevant SSWDBClassWithInfo instance.
Currently, classes supporting parameterisation are ScaledDecimal and String.
ScaledDecimal
Within Dolphin, an instance of ScaledDecimal has a scale - this defines the number of digits after the decimal point (see the comment to ScaledDecimal>>scale for further details). In ReStore, when defining an instance variable as a ScaledDecimal, as a minimum you must give the scale of that ScaledDecimal. This is done by specifying a Parameterised Class through the ScaledDecimal class method withScale:, e.g.define: #totalPrice as: (ScaledDecimal withScale: 2)
Most relational databases support a type similar to ScaledDecimal, but in addition to scale there is usually also precision - the total number of digits that may be held, including the scale. If you specify just a scale (as in the above example) a default precision of 15 will be used. Alternatively, you may specify the precision yourself:
define: #totalPrice as: (ScaledDecimal withPrecision: 8 scale: 2)
String
In Smalltalk, an instance of String can be any size, with (to all intents) no upper bound. Within relational databases, however, there are usually three different types of Strings:
- Fixed sized String, with some upper limit on the number of characters
- usually referred to as a CHAR- Variable sized String, with some upper limit on the number of characters
- this is usually referred to as a VARCHAR- An unbound, variable sized String
- names vary; LONGTEXT, TEXT, MEMO, CLOB etc. etc.Type 1 is usually the most efficient for the database to store but is obviously the most restrictive. Type 3 is the most flexible and most closely resembles a Smalltalk String. However, there are sometimes limitations on the use of this type - some databases only allow one String of this type per table; there may also be performance or memory overheads when using this type.
To help mitigate this, ReStore allows you to parameterise a String to enable the best choice of database String type to be made. For Strings of a known, fixed number of characters (e.g. a postal or zip code, or a product code), you can specify a CHAR-type String using the String class method fixedSize:
define: #productCode as: (String fixedSize: 8)
For a variable sized String with a definable maximum number of characters (VARCHAR), the class method maxSize: can be used:
define: #surname as: (String maxSize: 100)
Finally, if you just specify String (i.e. unparameterised) then a default value will be used as the maximum size of that String. This value will vary from database to database, but the net effect is usually to cause a LONGTEXT-type String to be used. A LONGTEXT-type String will also be used if the number of characters you specify as the argument to fixedSize: or maxSize: is greater than that allowed by your database.
For more details on the default size of unparameterised Strings, and how to tune this for a particular database, see Customising ReStore for Individual Databases.