INFORMIX DataBlade Developers Kit Tutorial

List of Exercises

Tutorial Exercise 3

Creating Distinct Data Types and Casts

This exercise demonstrates how to create distinct data types and the casts that convert them.

The Mercury DataBlade module contains distinct data types to represent Fahrenheit and Celsius temperatures and casts to convert between the two.

You create the distinct data type hierarchy, beginning with the temperature distinct data type, which is based on the built-in double-precision data type.

Then you create the CTemp and FTemp types that inherit from temperature, as shown in the following diagram.

The Mercury DataBlade module ensures that users cannot enter temperatures below absolute zero in the CTemp and FTemp types. If a user attempts to insert an incorrect value, the INSERT statement fails and returns an error. The DataBlade module uses user-defined support routines and casts to check the input value.

Using Casts

The Mercury DataBlade module uses casts in two ways:

  • To convert temperatures between the Celsius and Fahrenheit.
  • To enable users to enter numbers as integer and decimal data types directly into CTemp and FTemp columns without having to use an explicit cast in their SQL statements.

The temperature data type is implemented to reduce the number of casts you need to define. If you defined casts from integer and decimal for both CTemp and FTemp, you would create a total of six casts. Instead, you add integer and decimal casts only to the temperature type; the CTemp and FTemp types inherit the casts. This approach reduces the number of casts you have to define from six to four.

DataBlade Module Objects

The Mercury DataBlade module creates the following user-defined objects.

Distinct data types

temperature

CTemp

FTemp

 

Routines

Assign() function for the CTemp data type

Assign() function for the FTemp data type

These functions are called whenever the database server assigns a value to a CTemp or FTemp column. They check that the value is greater than absolute zero before assigning it.

Casts (with support routines)

integer to temperature

decimal to temperature

 

CTemp to FTemp

FTemp to CTemp

These casts convert input data from integer and decimal types to the temperature type.

 

These casts convert values between the CTemp and FTemp types

Error messages

One error message for values less than absolute zero

 

Tutorial Steps

This exercise consists of 16 steps. It takes approximately one hour to complete.


1

Create a new project.

Start BladeSmith from the Informix program group and choose Project New.

Enter the following information into the New Project wizard.

Project Property Mercury Value

DataBlade module name

Mercury

Object prefix

MRC

Description locale

en_us.1252

Server compatibilty

9.13 or 9.14

Project version number (Major.Minor)

1.0

Project description

This DataBlade module supports data types and casts for Celsius and Fahrenheit temperatures.

Vendor unique ID (optional)

Your company's name

Company information

Your company's name, copyright, and contact information

Click Finish.

Choose Project Save; create a new directory called Mercury in a convenient place on your hard drive and save your project in this directory as a file called Mercury.ibs.


Copyright © 1998, Informix Software, Inc. All rights reserved.