1.Foreword
Many database applications are written in object-oriented programming languages (java, c++), but they need to store data in and fetch data from databases. Because of the type differences between the native type system of object-oriented programming languages and the relational model supported by the database, conversions between the two models are required when fetching or storing data. Extending the database-supported type system alone is not enough to solve this problem completely, because a different language than the programming language must be used to express database access, which makes the programmer's job harder.
In practice, this problem can be solved by integrating object-oriented features with the database system, that is, building an object-relational database system and adding object-oriented features to the relational database system.
AntDB provides a variety of object-oriented features, one of which is custom types, whose usage is described below.
2.Syntax
Figure 1: syntax structure
3.Detailed Description of Functions
You can register a new data type in the database using the CREATE TYPE statement. However, when defining a data type, it is important to note:
- The creator of a datatype is the owner of the datatype.
- The schema can be specified at creation time by prefixing the type name with the schema name, otherwise it will be created in the default schema.
- The name of the new type must not be the same as the existing type name, domain name, or table name in the schema it is in.
As can be seen from the above syntax, a total of five types can be created, namely, combination type, enumeration type, range type, base type, and shell type. The following section focuses on the first four types. Shell type is mainly used as forward references for subsequent definitions when declaring range type and base type, and play the role of type placeholders.
Combination Type
The first form in the syntax description is created as a combination type. A combination type is represented by a list of attribute names and data types. If the data type of the attribute is sortable, you can also specify a sorting rule (collation) for the attribute. A combination type is essentially the same as a table row type, but if you just want to define a type, you can use CREATE TYPE instead of creating a table.
To create a combination type, you must have the USAGE permission on all its attribute types.
Table 1: Syntax Options of Combination Type
Example: *
Figure 2: create a combination type
3.2 Enumeration Type
The second form in the syntax description is created as a enumeration type. An enumeration type is represented by a list of tags with quoted labels, each of which must be no longer than NAMEDATALEN bytes (64 bytes, determined at compile time). It is possible to create enumeration type with zero labels, but such type cannot be used to hold specific values until the labels are added to the enumeration type.
Table 2: Syntax Options of Enumeration Type
Example:
Figure 3 :create a enumeration type
3.3 Range Type
The third form in the syntax description is created as a range type. The range type expresses the range of values of an element type (subtype of range).
Table 3: Syntax Options of Range Type
Example:
Figure 4: create a range type
3.4 Base Type
The forth form in the syntax description is created as a base type. To create a new basic type, you must have superuser privileges (the reason for this restriction is that incorrect type definitions can cause database crashes). The order of the arguments in the above syntax definitions is arbitrary and non-fixed, and many arguments are optional. Usually the function specified in the argument must be defined in C or a lower-level language.
Table 4: Syntax Options of Base Type
Example:
Figure 5 :create a base type
4.Array Type
When a custom type is created, the system automatically creates an associated array type for it. The array type name is prefixed by an underscore to the name of its element type. If the name length exceeds the NAMEDATALEN byte limit it is truncated (if the generated name conflicts with the name of an existing type, the process is repeated until a non-conflicting name is found). This implicitly created array type is variable-length and uses the built-in input and output functions (array_in and array_out). The system also uses this array type when building ARRAY[] on top of user-defined types. This array type changes as the owner or schema of its element type changes, and is deleted when its element type is deleted.
At this point, the reader may have questions, since the system has automatically created the correct array type, why there is still ELEMENT option. This is because ELEMENT works when you create a fixed-length type that happens to be an array of a fixed type and you want to be able to index the data items by subscript. For example, the point type is represented by two floating-point numbers that can be accessed by point[0] and point[1]. This approach is limited to the fact that internally it is an explicit series of fixed-length fields. For historical reasons (which are now unmodifiable), fixed-length arrays start with a subscript of 0, rather than 1 as variable-length arrays do.
5.Compatibility
The above syntax for creating combination types conforms to the SQL standard, the other forms are extended syntax.
6.Conclusion
Custom types provide the most basic object-oriented capabilities. Based on similar ideas, database systems can implement more object-oriented features. More object-oriented features of AntDB will be introduced in the future, so stay tuned.