Sie sind auf Seite 1von 4

Rexx Scripting for DB2 http://www.ibmdatabasemag.com/showArticle.jhtml?

articleID=161601982 By Howard Fosdick This powerful open-source language lets you take full command of DB2 Universal Database. Scripting languages hold a big advantage over traditional programming languages (such as C+ +, Java, and COBOL): They make developers more productive. Each line of code in a scripting language typically does more than a line of code in a traditional programming language. Rexx is a good example of a powerful scripting language. It glues existing code components together and leverages them for high-level programming. Rexx scripts can issue operating system commands, run existing programs, execute functions from external function libraries, access shared libraries, leverage objects, manipulate widgets, and so on. Rexx is high level, interpreted, and readily extensible, and it offers integrated debugging. With a powerful database system like DB2 Universal Database (UDB), you can satisfy many user requirements without programming by using advanced query, report, and data analysis tools. But there are situations in which programming is a must. For example, you may need to customize or automate operations or database administration. Or, you may need to script utilities or configuration. In these situations, Rexx scripting yields a whole new level of productivity. But before you can harness this productivity, you must understand the different ways you can script Rexx with DB2 databases. I'll describe the two primary methods for leveraging Rexx for DB2. About Rexx Rexx distinguishes itself from many other scripting languages in that it combines ease of use with power. The language has minimal syntax, is free-format and case-insensitive, and is easy to learn and use (rather like Basic or PHP). With Rexx, you don't have to define variables before use, assign variables "data types," or predefine the sizes of string variables or arrays. This frees developers from handling housekeeping details, increasing productivity. An easy language also means that scripts contain fewer errors and are more reliable. In IT, where more than half of all programmers perform maintenance, readable, high-level scripting saves money. Although easy to code, Rexx doesn't lack power. The predominant mainframe scripting language for two decades, Rexx today is an open-source language, freely available for just about every operating system. Free Rexx runs under all the operating systems on which DB2 UDB runs (including all versions of Linux, Unix, and Windows) and on all IBM's mainframe and midrange operating systems. IBM bundles Rexx with its own operating systems. Developers working with other operating systems can use one of eight free Rexx interpreters. All conform to the Rexx language standards, and each is optimized or extended for a different platform or environment. Two interpreters are object oriented (and upwardly compatible with standard Rexx, so they can run procedural Rexx scripts without change). I'll discuss a few specific Rexx interpreters and how they play in the DB2 world later in this article. IBM Rexx-to-DB2 Interfaces There are two main approaches to scripting Rexx with DB2 UDB. One uses IBM's Rexx-to-DB2 interfaces; the other uses Rexx/SQL, an open-source scripting interface.

IBM's three Rexx interfaces ship as bundled components with DB2 UDB for Linux, Windows, and AIX (see Table 1). The three interfaces can be used together in Rexx scripts in any combination. Simply issue a few statements to establish access, and all their capabilities are yours.

Consider the implications of the uses the table describes. Rexx scripts can issue any SQL statement: DML, DCL, or DDL. These statements can be dynamically prepared and can include cursor processing and parameter markers. The Execute Immediate command provides onestep execution for SQL statements. Rexx scripts can issue any of the 70-plus DB2 system commands. These commands are directed to the operating system's command line. Rexx scripts can also issue any of the more than 100 DB2 commands through their direct interface to the DB2 Command Line Processor (CLP). Scripts receive return codes and can manage their CLP interaction programmatically. Through the SQLDBS interface, Rexx scripts can issue any of the 100-plus administrative functions packaged in DB2's Administrative API. IBM's Rexx interface is the only scripting interface that can directly invoke the Administrative APIs. Rexx's scripting interface gives you a higher-level way to manage DB2 UDB than coding in traditional languages. Together, the three Rexx-to-DB2 interfaces give Rexx scripts unparalleled ability to direct, manage, and control DB2 UDB. They enable Rexx scripts to control virtually all aspects of DB2 UDB's configuration, operation, and management. The IBM Rexx interfaces require a specific Rexx interpreter: IBM Object Rexx. Object Rexx is an object-oriented superset of standard, procedural Rexx that runs standard Rexx scripts without any changes. In December 2004, IBM donated Object Rexx to the open-source community and handed management of the open-source project to the Rexx Language Association. The association promises continued compatibility with all features of IBM Object Rexx. The open-source Object Rexx is now called Open Object Rexx, or ooRexx. On the Windows platform, ooRexx is highly integrated. Its extensions support Windows Script Host (WSH), Object Linking and Embedding (OLE), and ActiveX. Through WSH, ooRexx can be an ActiveX script engine embedded within HTML and XML. It accesses ActiveX controls that support automation interfaces for the Windows Active Directory Service Interfaces (ADSI) and Windows Management Instrumentation (WMI) interfaces supported by DB2 UDB v.8. WSH provides a vehicle to integrate ooRexx scripts with those written in VBScript, JScript, and PerlScript. Developers have several alternatives when scripting for DB2 UDB under Windows: Write ooRexx scripts that run at the Windows command line or within the WSH, or simply double-click on scripts in the GUI. IBM's Rexx-to-DB2 interfaces offer many advantages, but technology always presents tradeoffs. These proprietary interfaces work only with a single Rexx interpreter under Linux and Windows (AIX supports other Rexx interpreters). Also, IBM has "stabilized" the Rexx-to-DB2 interfaces, meaning that no future enhancements are planned, which could become an issue as new features are added to DB2 UDB but not to the Rexx interfaces. Today, for example, the interfaces don't handle SQL object identifiers (such as table names) longer than 18 characters.

Open-Source Rexx An alternative to IBM's Rexx-to-DB2 interfaces is the open-source Rexx/SQL. Rexx/SQL meets all database standards, including the X/Open Call Level Interface (CLI) and the open database connectivity (ODBC) API. Rexx/SQL lets you write portable Rexx database scripts that issue DML, DCL, and DDL SQL statements. The Rexx/SQL product supports all typical database features, including transaction control with COMMITs and ROLLBACKs; concurrent database connections, cursors, and autocommit; dynamic statement preparation and parameter markers; SQL control structures such as the SQL Communications Area and SQL error messages; and retrieval and setting of database interface behaviors. Among Rexx/SQL's special features is the ability to issue any SQL statement through a single function call, including SELECT statements that return multiple rows. The product returns multiple-row result sets as arrays that you can easily manipulate from within the script. Of course, Rexx/SQL also supports standard cursor-based processing of multiple row results sets. As Table 2 shows, Rexx/SQL supports the command set expected in a standard database interface.

A portable open-source product that runs on many major operating systems, Rexx/SQL offers a very big advantage: The scripts you develop with it are database-independent. You typically can redirect scripts from one database product to another with only minor changes. Say you're converting from Oracle to DB2 UDB. You can re-target Rexx/SQL scripts to DB2 UDB with very few code changes other than altering any Oracle SQL that doesn't comply with SQL standards.

The generic Rexx/SQL interface also renders your skills transferable. Say you normally program for DB2 UDB, but are asked to write an application for the SQL Server-based package some department purchased. Rexx with the Rexx/SQL interface gives you a quick way to do this without learning SQL Server. The trade-off with Rexx/SQL is that it's a generic database interface for SQL only. It doesn't support unique DB2 UDB features such as a native DB2 command interface or the DB2 Administrative APIs. Scripts that use Rexx/SQL can still issue DB2 system commands to the operating system prompt and control the DB2 CLP externally. The Power Couple When database programming is required, scripting languages offer high productivity and quick program development. Rexx is one of the easiest, most powerful scripting languages to learn and use. You can leverage its power with one of two Rexx-to-DB2 interfaces to rapidly develop DB2 UDB scripts. IBM's interfaces offer unequalled DB2 UDB control and programmability, and the open-source Rexx/SQL interface yields database independence, code portability, and transferable skills.

Howard Fosdick is an independent DBA consultant who has worked with most major scripting languages. He is a founder of the International DB2 Users Group.

Resources "More Scripting With DB2 UDB for Windows," "Quick DB2 Scripting: Learn Rexx for Quick Access to DB2 UDB Data," Rexx Programmer's Reference by Howard Fosdick (Wrox, 2005) Return to Article

Das könnte Ihnen auch gefallen