3.3. Queries

Besides relations, a transformation can also use an OCL query as a utility expression. In the following example, the PrimitiveTypeToSqlType query is used in the PrimitiveAttribute2Column relation to calculate the SQL type of a column based on the type of its attribute.

transformation umlToRdbms(uml:SimpleUML, rdbms:SimpleRDBMS)
{
    /* All same as before */
    
    relation PrimitiveAttribute2Column
    {
        attributeName, primitiveTypeName, className, sqltype: String;

        checkonly domain uml c:UMLClass {
            attributes = a:UMLAttribute {
                name = attributeName,
                type = p:UMLPrimitiveType {
                    name = primitiveTypeName
                }
             }
        };
        enforce domain rdbms t:Table {
            columns = cl:Column {
                name = columnName,
                type = sqlType
            }
        };
        primitive domain prefix:String;
        
        where {
           columnName = if (prefix = '') then attributeName 
                            else prefix + attributeName 
                        endif;
           sqltype = PrimitiveTypeToSqlType(primitiveTypeName);
        }
    }

    query PrimitiveTypeToSqlType(typename : String) : String {
        if (typename = 'INTEGER') then 'NUMBER'
            else if (typename = 'BOOLEAN') then 'BOOLEAN'
                else 'VARCHAR'
            endif
        endif
    }
}