A Customized Database Query Tool

This customized query tool makes query creation more accessible for users.


May 01, 2004
URL:http://www.drdobbs.com/database/a-customized-database-query-tool/184405650

May04: A Customized Database Query Tool

Achieving ease-of-use without sacrificing power

Bill is End-User Computing Manager at Marvin F. Poer & Company. He can be contacted at [email protected].


Most organizations use generic query tools to give end users a means of developing simple queries to access complex databases. In this article, I present a custom query tool that makes query creation more accessible for users. The query tool I present is a Microsoft Access database (available electronically; see "Resource Center," page 5) that queries an AS400 database and returns the results in an Excel spreadsheet. However, the design is generally applicable for other systems as well.

In general, query tools:

The hard part in implementing this is in hiding database complexities and SQL syntax without losing much of the expressive power of SQL.

Most end-user query tools are designed to create almost any possible SQL statement. On the other hand, you probably don't want to give users the power to create cross-product joins of your client and address files with 10 zillion records in the query. Consequently, when evaluating query tools to make things easier for users, ask yourself:

Selecting Tables

Users need to select the tables that contain the data fields they need, then join those tables together into a single query. In my system, users can use only tables and table joins that have been defined in the dictionary; see Table 1. Tables are joined in the sequence they are added to the query with no nesting. Both inner and outer joins are supported. Outer join means that the query should include results from the main table, even if it can't find a matching record in the joined table. I used a "Select If No Match?" flag on table joins to indicate outer joins, but you may have better terminology.

To build queries, users first add a table to start the query definition, then add available linked tables one at a time. If there is more than one valid link between the two tables, then a pop-up window asks which link the user wants to pick. Restrictions to this process include:

Selecting Fields

After the tables have been added to a query, users need to select the fields to include in the query. Since users can only pick fields defined in the dictionary, there is no support for user-defined calculated fields. The dictionary can contain calculated fields, although this is limited to calculations based on fields in a single table.

The system supports sorting and grouping in queries by having users specify optional "sort sequence" and "group and totals" attributes for each field. For total queries, the system uses the groups and totals flag to put the various selection criteria into the appropriate "Having" or "Where" clause. There is also a "hide field" attribute in case a field is needed for selection, sorting, or grouping but shouldn't appear in the output.

Entering the Selection Criteria

Instead of typing in expressions like "Field= Value," users select a test from a list of available options. There is a default set of tests depending on the field type, and you can specify the tests available for specific fields. If you don't specify the value in the dictionary, users are prompted to choose a value when they build the query. For coded fields, the dictionary can include a separate test for each valid code with an associated description so that users don't have to know the codes.

The tool supports several types of tests: Equal, Not Equal, Greater Than, Greater Than or Equal, Less Than, Less Than or Equal, Starts With, Contains, Ends With, and Is Null. The system automatically combines a list of individual tests against a single field into more complex field test expressions using "And," "Or," and "In (x, y, z)" where appropriate.

To date, I haven't developed a good structured model to handle complicated "And" and "Or" clauses with nested parentheses so, by default, query tests for different fields are combined with an "And." If the default wasn't what they wanted, I numbered the clauses to let users enter expressions like: "1 and (2 or 5) and (3 or 4)." Hopefully, you can devise a better solution.

Returning Data to Users

Instead of formatting reports, my tool simply puts results into an Excel spreadsheet for users to manipulate. The system does some formatting, but doesn't attempt to create a polished presentation spreadsheet.

To help with the formatting of the spreadsheet data, each field is assigned a logical data type, such as: Text, Numeric, Upper Case, yyyyddd, Quantity, Dollar, Phone, and Zip Code. The yyyyddd data type lets the system automatically convert Julian dates into regular date fields in the spreadsheet. The dictionary includes a format table that lets you customize the default formatting rules for particular fields.

User Interface

The tool uses a simple UI based on choosing items from lists:

The general pattern is to have a list of valid choices on the left and selected choices on the right with buttons to make changes to the selected choices. For example, the pop-up window for adding field tests has "available field tests" and "selected field tests" list boxes, and the buttons: Add Test, Add Not Test, Delete Test, Start Over, Change Test Value, Toggle Not, Move Test Up, Move Test Down, Save New Test, and Cancel New Test.

Implementation Details

I implemented the system in Microsoft Access with most of the processing logic contained in the following set of Visual Basic classes.

The methods in these classes can be categorized as follows:

The design and programming of most of the methods is not complicated. The logic for generating the SQL statement gets tricky, but the work is divided up by having an AS400SQL method for each class. The most complicated method to program was QueryTest.AS400SQL, which scans through all tests for a single field and turns them into a single clause to be included in the Where statement. That method has about 200 lines of Visual Basic code.

Maintaining the Dictionary

The query system also needs to include the ability to update the data dictionary. Because dictionary maintenance needs to be done by the technical staff, you don't have to worry as much about user friendliness here. The dictionary UI has the following components:

If you have a sizable database, creating the dictionary will take a fair amount of effort and you should provide any automated support that you can. For example, my tool includes logic to read the AS400 catalog and create an initial set of Field entries for tables.

Conclusion

While database concepts can be arcane to many nontechnical people, the system I present here has successfully increased the number of users who create queries. That said, I'm still probably the biggest user of the application, using it frequently to respond to ad hoc requests from end users for AS400 data. In practice, the limitations on the queries that can be created hasn't been much of a problem, and it is easier to use this tool than the generic query tool provided by IBM for the AS400. Also, it saves times when responding to user requests since the data is automatically loaded into an Excel spreadsheet that can be sent to users.

DDJ

May04: A Customized Database Query Tool

Figure 1: Query definition screen.

May04: A Customized Database Query Tool

Table 1: Tables in the dictionary.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.