Skip to main content

Properties Based on Data Types

Properties Based on Data Types

When you define a property and you specify its type as a data type class, you have special options for defining and working with that property, as described in this section.

Data Type Classes

Data type classes enable you to enforce sets of rules about the values of properties.

InterSystems IRIS provides data type classes which include %Library.StringOpens in a new tab, %Library.IntegerOpens in a new tab, %Library.NumericOpens in a new tab, %Library.DateOpens in a new tab, and many others. Because the names of classes of the %Library package can be abbreviated, you can abbreviate many of these; for example, %DateOpens in a new tab is an abbreviation for %Library.DateOpens in a new tab.

Each data type class has the following features:

  • It specifies values for compiler keywords. For a property, a compiler keyword can do things like the following:

    • Make the property required

    • Specify an initial value for the property

    • Control how the property is projected to SQL, ODBC, and Java clients

  • It specifies values for parameters that affect the details such as the following:

    • Maximum and minimum allowed logical value for the data type

    • Maximum and minimum number of characters the string can contain

    • Number of digits following the decimal point

    • Whether to truncate the string if it exceeds the maximum number of characters

    • Display format

    • How to escape any special XML or HTML characters

    • Enumerated lists of logical values and display values to use in any user interface

    • Pattern that the string must match (automatically uses the InterSystems IRIS pattern-matching operator)

    • Whether to respect or ignore the UTC time zone when importing or exporting to XML

  • It provides a set of methods to translate literal data among the stored (on disk), logical (in memory), and display formats.

You can add your own data type classes. For example, the following shows a custom subclass of %Library.StringOpens in a new tab:

Class MyApp.MyType Extends %Library.String
{

/// The maximum number of characters the string can contain.
Parameter MAXLEN As INTEGER = 2000;

}

Overriding Parameters of Data Type Classes

When you define a property and you specify its type as a data type class, you can override any parameters defined by the data type class.

For example, the %IntegerOpens in a new tab data type class defines the class parameter (MAXVAL) but provides no value for this parameter. You can override this in a property definition as follows:

Property MyInteger As %Integer(MAXVAL=10);

For this property, the maximum allowed value is 10.

(Internally, this works because the validation methods for the data type classes are method generators; the parameter value you provide is used when the compiler generates code for your class.

Similarly, every property of type %StringOpens in a new tab has a collation type, which determines how values are ordered (such as whether capitalization has effects or not). The default collation type is SQLUPPER.

For another example, the data type classes define the DISPLAYLIST and VALUELIST parameters, which you can use to specify choices to display in a user interface and their corresponding internal values:

Property Gender As %String(DISPLAYLIST = ",Female,Male", VALUELIST = ",F,M");

Using Other Property Methods

Properties have a number of methods associated with them automatically. These methods are generated by the data type classes and can be accessed from ObjectScript.

For example, if we define a class Person with three properties:

Class MyApp.Person Extends %Persistent
{
Property Name As %String;
Property Age As %Integer;
Property DOB As %Date;
}

The name of each generated method is the property name concatenated with the name of the method from the inherited class. You can access these generated methods from ObjectScript, as in the example below. You can access the same information from Python by calling the associated method directly from the inherited class. For example, some of the methods associated with the %DateOpens in a new tab class and therefore the DOB property are:

 Set x = person.DOBIsValid(person.DOB)
 Write person.DOBLogicalToDisplay(person.DOB)
x = iris.cls("%Date").IsValid(person.DOB)
print(iris.cls("%Date").LogicalToDisplay(person.DOB))

where IsValid is a method of the property class and LogicalToDisplay is a method of the %DateOpens in a new tab data type class.

FeedbackOpens in a new tab