This section describes the object-oriented interface to SQL databases using specialized CLOS classes. These classes have standard-db-object as one of their superclasses and have a common metaclass which provides the specialized behavior for mapping subclasses of standard-db-object onto records in the database. A class of this kind is created using
It is not possible to inherit from a class that was defined by def-view-class. All of the slots need to be in the same class (and hence also in the same SQL table).
The OODDL lets you define a mapping between the relational and object-oriented worlds to be defined. Through the mapping a CLOS object can effectively denote a collection of records in a database view, and can contain pointers to other view-based CLOS objects. The CLOS object makes explicit an object implicitly described by the flat relational values.
The mapping is defined using the macro def-view-class. This extends the syntax of defclass to allow special base slots to be mapped onto the attributes of database views (presently single tables). When you submit a select query that names a View Class (that is, a class defined by def-view-class), then the corresponding database view is queried, and the slots in the resulting instances are filled with attribute values from the database.
All the special slots are distinguished by a modified set of class and slot options. The special slots and their options are described in more detail under def-view-class in the LispWorks Reference Manual .
Note: def-view-class defines a Lisp view of an underlying database table. It is a similar concept to that of SQL VIEWs, but does not interact with them.
The following example shows a View Class corresponding to the traditional employees table, with the employee's department given by a join with the departments table. See def-view-class for a description of the slot options.
The def-view-class macro allows elements or lists of elements to follow
:foreign-key. The elements can be symbols,
nil, strings, integers or floats.
This syntax means that an object from the join class is only included in the join slot if the values from home-key are
equal to the values in foreign-key, in order. These values are calculated as follows:
Note that some database vendors may have short maximum identifier lengths. The CLOS interface uses constructed alias names for tables in its SQL queries, and long table names or long class names may cause the constructed aliases to exceed the maximum identifier length for a particular vendor.
The OODML is designed to be powerful and expressive, while remaining familiar to users of the FDML. To achieve this aim, some of the functions and macros in the SQL interface have been overloaded -- particularly the select function and the iteration constructs.
The function select is common across the both the functional and object-oriented SQL interfaces. If its first argument, selections, refers to a View Class by supplying its symbolic name then the select operation becomes object-oriented and it returns a list of instances instead of a list of attributes.
A subsequent equivalent select call will return the same (
eql) instances. The
:refresh argument can be used to ensure that existing instances get updated with any changed data. If such an update requires action by your application, then add methods on the generic function
In a View Class select call, the symbol
slot-value is a valid SQL operator for use within the
To find the View Classes for a particular database, use the function list-classes.
To delete records corresponding to instances of the View Class, use the generic function
The object-oriented SQL interface has the same three iteration constructs as the functional interface (see Iteration): a
do-loop, a mapping function, and an extension to the Common Lisp loop macro. However, in this case, the iteration focus is not a tuple of attributes (that is, a record), but a tuple of instances. For example:
LispWorks User Guide and Reference Manual - 13 Feb 2015