Managing a Class Hierarchy with SQL in Matisse



From this document you will learn the answers to the following questions:

What is required to combine two SELECT statements from the table for Customer _ objtyp and Corporation _ objtyp?

How many people are in the class Customer?

What is too rigid to be extended?

Similar documents
Object Oriented Databases. OOAD Fall 2012 Arjun Gopalakrishna Bhavya Udayashankar

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Using Temporary Tables to Improve Performance for SQL Data Services

Oracle Database: SQL and PL/SQL Fundamentals

OBJECT ORIENTED EXTENSIONS TO SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Introduction to Triggers using SQL

IT2305 Database Systems I (Compulsory)

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: SQL and PL/SQL Fundamentals

Java Application Developer Certificate Program Competencies

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

1 File Processing Systems

Getting Started with Matisse

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Java (12 Weeks) Introduction to Java Programming Language

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

Oracle SQL. Course Summary. Duration. Objectives

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

CACHÉ: FLEXIBLE, HIGH-PERFORMANCE PERSISTENCE FOR JAVA APPLICATIONS

T-SQL STANDARD ELEMENTS

IT2304: Database Systems 1 (DBS 1)

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Part VI. Object-relational Data Models

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Oracle Data Integrator: Administration and Development

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Database Programming with PL/SQL: Learning Objectives

Relational Databases

SQL Server 2008 Core Skills. Gary Young 2011

A Rational Software Whitepaper

Oracle Database 12c: Introduction to SQL Ed 1.1

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Oracle Database 10g: Introduction to SQL

An Oracle White Paper June Migrating Applications and Databases with Oracle Database 12c

Data Modeling Basics

A Brief Introduction to MySQL

CS352 Lecture - Object-Based Databases

Data Modeling. Database Systems: The Complete Book Ch ,

Schema Evolution in SQL-99 and Commercial (Object-)Relational DBMS

DBMS / Business Intelligence, SQL Server

Instant SQL Programming

Overview RDBMS-ORDBMS- OODBMS

SQL NULL s, Constraints, Triggers

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

Database Modelling in UML

The Entity-Relationship Model

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Package sjdbc. R topics documented: February 20, 2015

CSCE 156H/RAIK 184H Assignment 4 - Project Phase III Database Design

DATABASE SYSTEM CONCEPTS AND ARCHITECTURE CHAPTER 2

Chapter 9: Object-Based Databases

The Sleepy Cat Database and Semantic Data Models in Java

Mimer SQL. Programmer s Manual. Version 8.2 Copyright 2000 Mimer Information Technology AB

Handling Unstructured Data Type in DB2 and Oracle

Oracle to MySQL Migration

Introduction to Microsoft Jet SQL

Automatic generation of fully-executable code from the Domain tier of UML diagrams

Chapter 2 Database System Concepts and Architecture

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Performance Evaluation of Java Object Relational Mapping Tools

The first time through running an Ad Hoc query or Stored Procedure, SQL Server will go through each of the following steps.

FileMaker 12. ODBC and JDBC Guide

The Design of a Robust Persistence Layer For Relational Databases

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

Matisse Installation Guide for MS Windows. 10th Edition

Data warehousing with PostgreSQL

Data Mining Extensions (DMX) Reference

Oracle Database: SQL and PL/SQL Fundamentals NEW

OBJECTS AND DATABASES. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 21

Object Oriented Database Management System for Decision Support System.

SAS BI Dashboard 4.3. User's Guide. SAS Documentation

A basic create statement for a simple student table would look like the following.

FileMaker 14. ODBC and JDBC Guide

How To Design Software

Database Query 1: SQL Basics

Cache Database: Introduction to a New Generation Database

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

12 File and Database Concepts 13 File and Database Concepts A many-to-many relationship means that one record in a particular record type can be relat

Databases and BigData

1.204 Lecture 3. SQL: Basics, Joins SQL

Coveo Platform 7.0. Microsoft Dynamics CRM Connector Guide

SQL SELECT Query: Intermediate

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Choosing a Data Model for Your Database

Oracle8/ SQLJ Programming

Transcription:

Managing a Class Hierarchy with SQL in Matisse A Technical Brief Matisse Software Inc. 2003 MATISSE SOFTWARE INC., ALL RIGHTS RESERVED. Matisse is a registered trademark of Matisse software Inc. Other product or company names mentioned herein may be the trademarks of their respective owners.

Introduction The SQL standard, which has been constrained for the past 30 years to query and manipulate relational tables, has now been morphed by Matisse Software into a powerful language to operate on a hierarchy of classes. This technical brief explores the management of a hierarchy of classes with standard SQL-99, and shows the benefits of using SQL on an object database schema in terms of simplicity, extensibility and performance. Object developers will discover that it is now possible to develop component-based business logic with stored methods. While relational database developers will discover that classes, attributes, stored methods and objects can easily be mapped into familiar relational concepts respectively tables, columns, stored procedures and rows. Defining a Class Hierarchy In response to the market demand, relational vendors have extended their relational model to support the definition of class hierarchy. The objectrelational model was born. However database developers are not taking advantage of these object features, simply because it is (1) complex to design, (2) the class hierarchy is too rigid to be extended and (3) the runtime performance degrades substantially compared to plain relational modeling. Consequently, what database developers really need is a true object model accessible via SQL. For example, consider the case of a simple class hierarchy that models clients in a stock management system as described by the UML diagram in Figure 1. Ideally, you would like to define categories of clients that share properties from the parent Customer class while maintaining properties of their own. This category tree lends itself naturally into a hierarchy of classes. Customer accountno : Integer name : String phone : String Corporation primarycontact : String contactphonelist : List(String) accountrep : String Figure 1: The class Customer and its subclass Corporation in UML Matisse Native Object Model matches naturally with the UML class hierarchy, resulting in a two classes database schema as shown in figure 2. CREATE CLASS Customer ( accountno INTEGER NOT NULL, name STRING, phone STRING CREATE CLASS Corporation UNDER Customer ( primarycontact STRING, contactphonelist LIST(STRING), accountrep STRING Figure 2: The class Customer and its subclass Corporation in DDL with Matisse By comparison, with the object-relational model, you need to define types and tables (and possibly views) to map into a class hierarchy, as shown in figure 3. This gives you the illusion to deal with an object model while it all ends up into relational tables. The underlying model of object-relational databases remains the same: rows and columns. These mappings between types and tables or tables and views are usually complex to define, to maintain and error prone, so database developers are rarely using these features 2

CREATE TYPE Customer_objtyp AS OBJECT ( accountno NUMBER, name VARCHAR(50), phone VARCHAR(20) ) NOT FINAL; CREATE TYPE contactphones_vartyp AS VARRAY(10) OF VARCHAR(20 CREATE TYPE Corporation_objtyp UNDER Customer_objtyp ( primarycontact VARCHAR(50 contactphones contactphones_vartyp, accountrep VARCHAR(50 ) NOT FINAL; CREATE TABLE Customer_objtab OF Customer_objtyp (accountno PRIMARY KEY) OBJECT IDENTIFIER IS PRIMARY KEY; CREATE TABLE Corporation_objtab OF Corporation_objtyp (accountno PRIMARY KEY) OBJECT IDENTIFIER IS PRIMARY KEY; Figure 3: The type Customer and its subtype Corporation in DDL with an Object-Relational DBMS The definition of both Customer_objtab and Corporation_objtab tables, in figure 3, prevents the application from accessing the corporate customers when selecting data from the Customer_objtab table, which is expected when programming with objects. Accessing Objects in the Hierarchy A key benefit of modeling a class hierarchy is to filter data by class and sub-classes within the hierarchy. Another important advantage is the generic access to objects, which lets you extend the class hierarchy without modifying the access methods already in place. However with objectrelational technology, accessing objects in a class hierarchy is quite challenging, which often leads developers to flatten the class hierarchy into a single table. In Figure 4, Matisse SELECT statement returns all the instances of the class Customer and its subclass Corporation in a table format. The columns of the resulting table are the attributes of the Customer class. > SELECT * FROM Customer; accountno name phone --------- ------------ ------------ 33450001 John Doe 415-596-1042 <- Customer 33450008 Mary Smith 650-222-2222 <- Customer 102000448 Matisse Inc 650-548-2581 <- Corporation Figure 4: Selecting all customer types with Matisse Matisse also supports the syntax to select Customer objects while excluding objects of its sub-classes by using the keyword ONLY to filter objects as shown in figure 5. > SELECT * FROM ONLY Customer; accountno name phone --------- ------------ ------------ 33450001 John Doe 415-596-1042 <- Customer 33450008 Mary Smith 650-222-2222 <- Customer Figure 5: Selecting customers without corporate customers with Matisse On the other hand, when you are using the object-relational model defined in figure 3, retrieving all customers requires to combine the result of two SELECT statements from the tables for Customer_objtyp and Corporation_objtyp (figure 6). > SELECT accountno, name, phone FROM Customer_objtab UNION SELECT accountno, name, phone FROM Corporation_objtab; Figure 6: Selecting two types of customers with an Object-Relational DBMS In order to avoid using UNION to select both kinds of customers, you need to create a single table for both Customer_objtyp and 3

Corporation_objtyp, where you insert both types of objects into the table. The problem with this approach is the complexity for accessing sub-table columns. Figure 7 illustrates the typecasting mandatory to filter objects of type Corporation and to access the primarycontact column, which is defined in Corporation_objtyp, and not in Customer_objtyp. > SELECT TREAT(VALUE(c) AS Corporation_objtyp).primaryContact FROM Customer_objtab c WHERE VALUE(c) IS OF (Corporation_objtyp Figure 7: Selecting a column from the Corporation class with an Object- Relational DBMS In figure 8, Matisse demonstrates the simplicity of selecting an attribute from a sub-class. > SELECT primarycontact FROM Corporation; Figure 8: Selecting an attribute from Corporation with Matisse Today, The vast majority of applications and services are developed using an object programming language such as Java, C# or C++. Ideally you would expect to retrieve objects directly from the database, avoiding the O-R mapping layer. To avoid O-R mapping, Matisse provides the REF() function, when used in SELECT statement, returns objects rather than atomic values as shown in the following Java example: String query = SELECT REF(c) FROM Customer c ORDER BY c.accountno ; stmt = conn.createstatement( resultset = stmt.executequery(query while (resultset.next()) { // Objects of the right class // (Customer or Corporate) // are actually returned Customer c = (Customer)resultSet.getObject(1 } resultset.close( stmt.close( Figure 9: A Java program with a SELECT statement returning Java objects Programming with Stored Methods Up to this point, we have discussed how class and inheritance can be integrated into SQL, now let s discuss how to implement active business components that rely on unhampered object programming. Persistent Stored Module (PSM) is the component of the SQL-99 standard that provides syntactic and semantic constructs for the specification of stored methods. In addition to the obvious advantages of stored procedures such as runtime performance or reusability, you also benefit from the object concepts of polymorphism, encapsulation to achieve full reusability and extensibility of your business components. Consider now the example of a financial institution, which wants to implement diverse fee management policies per account type as described in the account hierarchy in figure 10. MarketAccount transferto() Account accountid : String balance : Numeric transfermoney() transferto() Figure 10: UML diagram for Account classes SavingAccount transferto() The transfermoney() method is a static method, equivalent to a procedure in the relational world, that transfers an amount of money from a source account to a destination one. The transferto() method is defined on each type of account and it implements the business logic, shown on figure 11, which is associated with each account type. 4

CREATE STATIC METHOD transfermoney (origaccntid STRING, destaccntid STRING, amount NUMERIC(19, 2)) RETURNS NUMERIC(19,2) FOR Account DECLARE origaccnt, destaccnt Account; -- Account objects SELECT REF(c) INTO origaccnt FROM Account WHERE accountid = origaccntid; SELECT REF(c) INTO destaccnt FROM Account WHERE accountid = destaccntid; RETURN origaccnt.transferto (destaccnt, amount -- returns a processing fee CREATE METHOD transferto (destacct Account, amount NUMERIC(19, 2)) RETURNS NUMERIC(19,2) FOR Account SELF.subtractBalance(amount IF balance < 0 THEN... -- raising exception; END IF; destacct.addbalance(amount IF amount > 100.00 THEN RETURN 1.20; -- a processing fee ELSE RETURN 2.50; -- a processing fee END IF; CREATE METHOD transferto (destacct Account, amount NUMERIC(19, 2)) RETURNS NUMERIC(19,2) FOR SavingAccount RETURN 1.50; -- a fixed processing fee CREATE METHOD transferto (destacct Account, amount NUMERIC(19, 2)) RETURNS NUMERIC(19,2) FOR MarketAccount RETURN amount * 0.0001; -- a proportional processing fee Figure 11: Stored methods for account management with Matisse 5

Methods can also be used within regular SQL statements. The SELECT statement, in figure 12, has a where-clause which filters customers by area code using the isinareacode() method. CREATE METHOD isinareacode(in areacode STRING) RETURNS BOOLEAN FOR Customer RETURN SUBSTR(telephone, 1, 3) = areacode ; > SELECT * FROM Customer c WHERE c.isinareacode( 415 ) = TRUE; Figure 12: A SELECT statement using a SQL method and its definition with Matisse Chances are that you would override the method isinareacode() for Corporation, which searches in contactphonelist as well. In this case, the object-relational approach shown in figure 13 generates more complex code that is costly to extend and maintain. Extending the class hierarchy, by adding a subclass of Customer, which has a different way of managing contact phone numbers, requires to update all SELECT statements that filter by area code. > SELECT name FROM Customer c WHERE (SUBSTR(c.telephone, 1, 3) = 415 ) OR (VALUE(c) IS OF (Corporation_objtyp) AND (SUBSTR(c.telephone, 1, 3) = 415 Corporation_objtyp).contactPhone1,1,3)= 415 Corporation_objtyp).contactPhone2,1,3)= 415 Corporation_objtyp).contactPhone3,1,3)= 415 Corporation_objtyp).contactPhone4,1,3)= 415 Corporation_objtyp).contactPhone5,1,3)= 415 ) Figure 13: A relational approach instead of using isinareacode() method Extending the class hierarchy with Matisse consists in defining an overriding method isinareacode() for the new type, and all the existing logic in place still works without any change. Joining Classes Database applications usually require extensive use of multi-table joins. But joins are computationally intensive, and each join is computed at runtime to link information on-the-fly, thereby substancially impacting performance. To illustrate multi-tables joins, consider the case of a stock management system, where the relevant classes are shown in the UML diagram in the appendix along with its equivalent definition by Matisse Data Definition Language. > SELECT c.name, o.orderno, l.lineitemno FROM Customer c, Order o, LineItem l WHERE c.orders = o.oid -- line 5 AND o.lineitems = l.oid -- line 6 AND o.orderno = 3001; name orderno lineitemno ---------- --------- ------------- John Doe 3001 21001 John Doe 3001 21002 Figure 14: Joining classes with Matisse To retrieve the customer and line item data for a specific purchase order, you must join three classes, as illustrated by figure 14. In Matisse SQL syntax, the OID property, which represents the object identifier, is implicitly defined for all classes, and plays the role of a primary key. The orders property at line 5 is defined in class Customer as a reference to Order objects, and it acts as the foreign key to the class Order. 6

The query statement shown in figure 15 can be rewritten using a navigational expression through object references as follows: > SELECT o.orderedby.name, -- line 1 o.orderno, o.lineitems.lineitemno -- line 3 FROM Order o WHERE o.orderno = 3001; name orderno lineitemno ---------- --------- ------------- John Doe 3001 21001 John Doe 3001 21002 Figure 15: Navigation through references with Matisse Interestingly, you may have noticed that both queries, in figure 14 and 15, return the same results. This demonstrates that join conditions are expressions of relationships between objects. The expression in line 4 in the Join statement (figure 14) is specifying the navigation path through the reference lineitems between class Order and class LineItem, as expressed in line 3 of the query statement figure 15. In fact, Matisse transforms a SELECT statement with join conditions into navigational expressions using relationships defined on classes. For both statements, the selection operation (i.e., where clause) filters objects based upon attribute values, and the projection (i.e., select-list) is generated by navigation through inter-objects references. Data Reporting Since Matisse supports the SQL standard as demonstrated here, as well as ODBC and JDBC, it is easy to connect Matisse to any reporting tool that relies on ODBC or JDBC for easy and quick business data presentation. A class hierarchy often models a hierarchy of types upon which summary reports can be based. In figure 16, The GROUP BY statement retrieves the average of each customer's total purchase amount by client type. The SUM(c.orders.totalPurchase) operation returns each customer's total purchase amount, since SUM() aggregates data from all the orders of each customer. > SELECT c.class_name AS Client Type, AVG(SUM(c.orders.totalPurchase)) AS Avg Odr FROM Customer c GROUP BY c.class_name; Client Type Avg Odr ------------------ ------------- Customer 124.55 Corporation 2268.23 Figure 16: Summary Report using grouping by class In summary, the query execution does not need costly join calculations, which are usually computationally intensive operations for relational products. Matisse eliminates the need for join operations by taking advantage of the explicit references defined between objects. Therefore, for join intensive applications, Matisse easily outperforms relational products by 100x while consuming much less computer resources. 7

Conclusion In this brief, we have introduced new ways of managing a class hierarchy with SQL. While in Matisse, SQL queries are relational in their syntax, they also take advantage of the object paradigm by supporting inheritance, polymorphism, and true navigation. Furthermore, query processing takes place on the server to enforce security and achieve best performance. Matisse native object model combined with SQL support demonstrates immediate benefits over relational or object-relational technology: - Simplicity and extensibility when defining a class hierarchy - Natural integration with object-oriented languages - Better performance when joining classes - True object-oriented programming using SQL methods These benefits enable you to implement applications and services that require high performance on a level of data complexity that goes beyond the modeling capabilities of legacy relational databases. Matisse Software Inc. www.matisse.com 433 Airport Blvd, suite 421 Burlingame, CA 94010 650-548-2581 Download a developer s version of Matisse 6.0 at www.matisse.com 2003 Matisse Software Inc. All rights reserved. 8

Appendix The following figures represent the UML diagram and class definition for the stock management system, which illustrates multi-tables joins in Matisse. Customer accountno : Integer name : String phone : String +orderedby +orders Order orderno : Integer 0..1 0..* +containedin 0..n Corporation primarycontact : String contactphonelist : List(String) accountrep : String +lineitems 0..n LineItem lineitemno : Integer Figure 17: UML diagram for Customer, Order, and LineItem CREATE CLASS Customer ( AccountNo INTEGER NOT NULL, name STRING, phone STRING, orders REFERENCES (Order) INVERSE Order.orderedBy CREATE CLASS Order ( orderno INTEGER NOT NULL, orderedby REFERENCES (Customer) CARDINALITY (0, 1) INVERSE Customer.orders, lineitems REFERENCES (LineItem) INVERSE LineItem.containedIn CREATE CLASS LineItem ( lineitemno INTEGER NOT NULL, containedin REFERENCES (Order) INVERSE Order.lineItems Figure 18: Class with References in DDL with Matisse 9