Object-Oriented Design of Database Stored Procedures (PDF)
Total Page:16
File Type:pdf, Size:1020Kb
Object-Oriented Design of Database Stored Procedures Michael Blaha, [email protected] Bill Huth, [email protected] Peter Cheung, [email protected] Abstract base I/O. One nice feature of SPs is their support for op- tional input parameters. An SP may have optional inputs Object-oriented (OO) software engineering techniques are if: (1) the definition lists all outputs before inputs and (2) often used with programming languages, but they also ap- optional inputs have default values. ply to relational databases. OO techniques are not only helpful for determining database structure, but also for de- 3. Financial Application Example signing stored procedure code. In fact, we were surprised by the magnitude of the stored procedure benefits. OO The examples in this article are based on an application for techniques boost development productivity as well as the managing syndicated loans. The loans can be huge, involv- quality, clarity, and maintainability of the resulting code. ing billions of dollars, and can arise from lending to gov- ernments and multinational corporations. Participation in a 1. Introduction loan must be spread across multiple lenders because the risk is too large for a single lender. Object-oriented (OO) techniques are versatile. They are not Figure 1 shows an excerpt of a UML class model for only helpful for developing programs and structuring data- the application. An Asset is something of value and can be bases, but they are also effective for designing stored proce- a Currency (such as US Dollars, Euros, and Yen), a Loan- dures. A stored procedure is programming code that runs in Instrument, or an OtherAsset (such as bonds and stock). An the address space of a database management system Asset can involve various CounterPartyRoles. Methods (DBMS). All the major DBMS products (such as SQL Serv- (SPs) link an Asset to a CounterPartyRole (bindCPR), un- er, Oracle, MySQL, DB2) now support stored procedures. link an Asset from a CounterPartyRole (unbindCPR), and When we build database applications, we start by pre- retrieve the collection of CounterPartyRoles for an Asset paring a UML class model and then create a corresponding (getCPRs). database structure [2]. One of the subsequent steps is to im- A CounterParty is some organization that is relevant plement methods, that is the behavior for classes. We have for the purpose of tracking loans. (The term CounterParty found stored procedures to be helpful for implementing is banking jargon.) CounterPartyRoleType specifies the methods. ways that CounterParties can be involved in loans; exam- 2. The SQL Server SP Language ples include borrower, lender, guarantor, and agent. A CounterPartyRole combines a CounterParty with a Coun- We have been using SQL Server in our recent applications terPartyRoleType. The software not only tracks who is in- and emphasize it in this article. However, we believe that volved with Assets, but also tracks how they are involved. many of the techniques we discuss here would apply to oth- Methods can create a new object (new), delete an object (de- er DBMSs. lete), modify data (update), and retrieve data (getWithID). SQL Server has three kinds of stored procedures: ex- A LoanInstrument can be a FacilityAgreement, tended stored procedures (XPs), common-language-run- Tranche, or TrancheItem. A FacilityAgreement is an over- time stored procedures (CLRPs, new to SQL Server 2005 all financial amount that is available to a borrower. A [1]), and interpreted stored procedures (SPs). Developers Tranche is a loan that is made under the auspices of a Fa- write XPs and CLRPs in a programming language (such as cilityAgreement. Tranches are important because they pro- C or C++), compile them, and then link them into the data- vide the means for managing Drawdowns (borrowed base. In contrast, SPs are written with Microsoft’s Trans- funds). A TrancheItem is the portion of a Tranche covered act-SQL language and are interpreted with linking an im- by a particular lender. Thus a Tranche splits into plicit part of the language. TrancheItems, one for each lender. For convenience, we have favored SPs in our applica- Via inheritance from Asset, the objects in the different tions. Of the three approaches, SPs are the easiest to write subclasses can have CounterPartyRoles. Thus TrancheIt- and the interpreter overhead is small compared to data- em inherits CounterPartyRoles. The model does not en- Page 1 Object-Oriented Design of Database Stored Procedures Asset * * CounterPartyRoleType CounterPartyRole bindCPR 1 * * getCPRs name delete unbindCPR delete getWithID new new update update * 1 Currency LoanInstrument OtherAsset name name CounterParty deleteCommitment name getWithID deleteCommitments baseCurrency 1 getCommitments Commitment delete sumCommitments beginDate getWithID new 1 endDate * * commitmentAmount update 1 lender FacilityAgreement Tranche TrancheItem * name name DrawdownItem beginCommitmentDate beginDate addCommitment drawdownItemAmount endCommitmentDate endDate delete manualOrComputed getAll addCommitment addCommitment getDrawdownItems * delete delete getLender getAll getAll 1 1 * 1 * getWithID getTranches getDrawdowns new Drawdown getWithID getWithID update getWithName getWithName drawdownAmount updateCommitment new new drawDate update update maturityDate updateCommitment updateCommitment apportion 1 bindCPR delete * getCPRs * getDrawdownItems getWithID new unbindCPR update Figure 1 Excerpt of UML class model with public methods force some business constraints: (1) each TrancheItem has them. In contrast, methods to add and update Commitments exactly one lender and (2) of the many TrancheItems for a have constraints that vary, so the subclasses (Facil- Tranche, exactly one TrancheItem pertains to each lender. ityAgreement, Tranche, and TrancheItem) individually Since an Asset can be bound to any number of CounterPar- specify them. Similarly, the new, delete, update, and tyRoles, the model cannot enforce these constraints. How- getWithID methods also vary by subclass. By definition, ever the database structure can store the data and SPs can each TrancheItem has a single lender that is assigned when then enforce the constraints. a new TrancheItem is created. The getLender method re- A LoanInstrument may have any number of Commit- trieves a TrancheItem’s lender. ments that are summed to limit the amount available for The FacilityAgreement Commitments limit Draw- Drawdowns. Each Commitment has begin and end dates; downs for a borrower. The Tranche Commitment limits thus the total amount available varies over time. Drawdowns for a loan and the TrancheItem Commitments The methods to delete, get, and sum Commitments do limit exposure for a lender to a loan. Ultimately, the sum of not vary across subclasses, so LoanInstrument specifies the Commitments for a Tranche cannot exceed the sum of Page 2 Michael Blaha, Bill Huth, Peter Cheung Object-Oriented Design of Database Stored Procedures the Commitments for its TrancheItems. (The total funds are important in that they improve understandability, doc- from lenders must be able to cover the withdrawals of a umentation, and productivity. borrower.) This is another constraint that SPs must check. A Drawdown is a removal of funds within the scope of 5. Public vs. Private a Tranche. A Tranche can involve multiple Drawdowns A public SP is available to other applications and the user that occur on various dates—the drawDate must lie within interface. A private SP is only for the use of other SPs in the begin and end dates for the Tranche. There are addition- the same application. In the syndicated loan application al constraints. The total Drawdowns cannot exceed the about 90% of the SPs were public and 10% were private. Commitment for its Tranche. Furthermore, the total Draw- Figure 1 shows only public methods. downs cannot exceed the total Commitment for its Facil- SQL Server does not distinguish between public and ityAgreement (traverse from Drawdown to Tranche and private. So we established a convention to indicate the dif- from the Tranche to FacilityAgreement). ference. A prefix of usp (user stored procedure) denotes a A DrawdownItem is the portion of a Drawdown for a public SP; a prefix of isp (internal stored procedure) de- particular lender. Consequently, a Drawdown may be asso- notes a private SP. Developers must be disciplined in ad- ciated with any relevant CounterPartyRoles, except for hering to this convention—isp SPs are solely for internal lenders. The apportioning of a Drawdown to Drawdown- use. Items is normally consistent with the splitting of its Similarly, we distinguished between private views Tranche into TrancheItems (manualOrComputed = “com- (prefix of iv) and public views (prefix of uv). puted”), but sometimes a DrawdownItem is manually ad- Private SPs are helpful in a number of situations. For justed (manualOrComputed = “manual”). example, superclass SPs that are overridden by subclasses The methods can link a Drawdown to a CounterParty- should be declared as private. Similarly, computational ar- Role (bindCPR), unlink a Drawdown from a CounterPar- tifacts are not part of the published interface and should tyRole (unbindCPR), and retrieve the collection of Coun- also be declared as private. terPartyRoles for a Drawdown (getCPRs). There is a meth- od to apportion a Drawdown to DrawdownItems consistent We found that it was more difficult to determine the with the splitting of its Tranche into TrancheItems. The ap- class owner for private methods than for public meth- portion method