19.7 Example: PL/SQL-to-Server IntegrationTo provide an(other) demonstration of how collections can ease the burden of transferring data between server and PL/SQL application program, let's look at a new example. The main entity in this example is the "apartment complex." We use a nested table of objects in order to hold the list of apartments for each apartment complex. Each apartment is described by the following attributes: CREATE TYPE Apartment_t AS OBJECT ( unit_no NUMBER, square_feet NUMBER, bedrooms NUMBER, bathrooms NUMBER, rent_in_dollars NUMBER ); And we can now define the nested table type which will hold a list of these apartment objects: CREATE TYPE Apartment_tab_t AS TABLE OF Apartment_t; Using this type as the type of a column, here is the definition of our database table: CREATE TABLE apartment_complexes (name VARCHAR2(75), landlord_name VARCHAR2(45), apartments Apartment_tab_t) NESTED TABLE apartments STORE AS apartments_store_tab; If you're curious, the INSERT statements to populate such a table look like the following (note the use of nested constructors to create the collection of objects):
Now, at last, we can show off some wonderful features of storing collections in the database. Imagine that we are the new managers of the River Oaks Four apartments (hardly large enough to qualify as a complex) and we want to demolish any unit that rents for less than $500, and raise the rent on everything else by 15%.
To me, one of the most significant aspects of this example is the single-statement fetch (and store). This PL/SQL fragment emulates the creating of a "client-side cache" of data, which is an essential concept in many object-oriented and client-server architectures. Using this kind of approach with collections can reduce network traffic and improve the quality of your code.
Copyright (c) 2000 O'Reilly & Associates. All rights reserved. |
|