AD-HOC QUERY BUILDER International Institute of Information Technology Bangalore Submitted By: Bratati Mohapatra (MT2009089) Rashmi R Rao (MT2009116) Niranjani S (MT2009124) Guided By: Prof Chandrashekar Ramanathan 1
Contents i. Acknowledgement 1. Introduction 2. Tools /Software used 3. N-tier architecture 4. Modules in the program 5. Screenshots 6. Individual Contribution 7. Scope for future work 8. Conclusion 9. References 2
Acknowledgment We would like to thank Prof Chandrashekar Ramanathan for his guidance and useful insights during the project development. We would also like to thank our alma mater IIITB and also IBM for offering this course and providing us opportunity to work on this project. 3
1. Introduction 1.1 Project Objective This project is aimed at develop a product, Ad-hoc query builder which is user friendly tool to build and execute SQL queries on Oracle database. All the inputs are taken from user through a GUI and query is constructed on the fly. Reports are generated and displayed to the user. 1.2 Ad-hoc Query Ad hoc querying is a term in information science. Many application software systems have an underlying database which can be accessed by only a limited number of queries and reports. Typically these are available via some sort of menu, and will have been carefully designed, pre-programmed and optimized for performance by expert programmers. By contrast, "ad hoc" reporting systems allow the users themselves to create specific, customized queries. Typically this would be via a user-friendly GUI-based system without the need for the in-depth knowledge of SQL, or database schema that a programmer would have. Because such reporting has the potential to severely degrade the performance of a live system. Ad hoc querying/reporting is a business intelligence subtopic, along with OLAP, data warehousing, data mining and other tools. [1]. 2. Tools/Software used The ad-hoc query builder is designed for oracle database. Hence the back-end is Oracle 10g. The front-end is built using JSP and JavaScript and the interaction with the back end is through Servlet and POJO (Plain Old Java Objects). The IDE used to build this software is Net Beans 6.8 with Glass-Fish server. The operating system using which this product is developed is Ubuntu 10.04. 3. Architectural and component-level design A description of the program architecture is presented. [2] 3.1 Program Structure 3.1.1 Architecture diagram A 4 tier architecture is used Logical building blocks of the application 4
Fig 1: 4 tier architecture diagram A pictorial representation of the architecture used is presented. Fig 2: Architecture 5
3.1.2 Why n-tier architecture was chosen? Each layer can be developed independently of the other provided that it adheres to the standards and communicates with the other layers as per the specifications. Each layer can potentially treat the other layers as black-box. In other words, each layer does not care how the other layer processes the data as long as it sends the right data in a correct format. The n-tier Applications provide specific advantages that are vital to the business continuity of the enterprise. Typical features of a real life n-tier includes the following: 1) Security: Application has appropriate authentication, logging and monitoring mechanisms. 2) Availability and Scalability: Application should be reliable and should have sufficient fail-over mechanisms (redundancy) such as fail-over clusters. 3) Manageability: Application should be designed to have the capability to deploy, monitor and troubleshoot. Methodology to handle errors, log errors and provide useful information for problem resolution. 4) Easy Maintenance: This is generally achieved by adopting coding standards, deployment standards, modular application design, data abstraction and application frameworks. 5) Data Abstraction: Easily make changes to the functionality, business rules with the least amount of impact to the entire applications These features facilitate a successful n-tier application that intends to provide a good solution. 4-tier architecture is used because it best suits the project. Why not event driven, front-end back-end or monolithic architectures? Event driven architecture are best-suited for embedded systems while the other architectures are suited for stand-alone applications. 3.2 Description of the layers 3.2.1 Layer n Process Specification Client layer: The client layer consists of the Web browser which supports all the features of a typical day-to-day browser. It takes care of the interaction between the user and the application through web pages and HTTP. The server used in this case is Glass Fish server which comes integrated with NetBeans 6.8. Presentation layer: This layer deals with the general layout of the web page as seen by the user. It consists of form-based components like list boxes, text area, buttons and other form-based components of HTML. Business logic layer: 6
This layer takes care of all the business functions and information exchange between the database and presentation layer. It consists of logic to establish connection with the Oracle 10G database and run SQL query or stored procedure. Database layer: The database used here is Oracle 10G. It takes care of the persistence storage of objects. Further this work uses a stored procedure which takes care of all the logic to construct a SQL query given the proper inputs from the GUI. 3.2.2 Layer n interface description Client layer (Browser): All the input and output interfaces of this layer are designed in a way, a web browser can understand. The protocol used is HTTP. A web browser enables the user to interact in a GUI-based environment rather than a character-based environment. Presentation layer: The input and output to this layer is received and sent from/to the client layer. Whatever the user enters in the web page, say a form filled and button clicked triggers a piece of code to be executed, that interacts with the database. Typical code consists of JSP and Servlet. The presentation layer may also send inputs received from the client as parameters to a piece of code in the business logic layer. Business logic layer: The input and output to this layer is tied to the Presentation layer. This layer gets input from the presentation layer as parameters and executes code written as, say, a Java Bean. A Java Bean is a POJO (Plain Old Java Object) which can be called from the Presentation layer and returns a value say an Array List which can be used in the Presentation layer. This layer also takes care of the establishment of connections with the Oracle database and executing an SQL query or Oracle Stored Procedure Database layer: The database used is Oracle 10G which contains the entities and the attributes. It also contains the Oracle stored procedure which consists of the main logic of constructing the query. 1. Designing the GUI: 4. Modules in the program This module involves the use of JSP, JavaScript and Cascaded style sheets. The program consists of two jsp files index.jsp and query.jsp which consists of various components like list box, text Area and buttons. The list boxes have to be designed in a way that when one item is selected and a button is 7
pressed, the selected item should go to another list box. The page must be designed such that different forms are submitted using the post method when different buttons are clicked. The buttons are connected to respective JavaScript functions by the attribute onclick. They do the required functions on click of these buttons. 2. Coding of Servlets which are called from the GUI on submitting the form: Servlets are invoked when a form is submitted using the post method. The Servlet reads the values entered in the various fields and invokes the corresponding Plain Old Java Object by passing these fields as parameters. This Java Object may in turn execute an SQL query or Called Procedure which returns the ArrayList of results and redirects the session to another jsp file. In the case of executing the final query string, the Servlet returns the value as an ArrayList of HashMaps as discussed in the next point and the table is displayed by traversing the ArrayList where each element is a HashMap of columns. 3. Coding of JavaBeans which in turn call Plain Old Java Objects that return values to the GUI: In this work, there are Java codes in the jsp file which invoke a JavaBeans that may return values to the GUI and the code is embedded within <% %>. While executing queries with output as single column the Java Object returns the result as ArrayList of Strings. In the case of executing the final query, the output may contain many columns that are returned as ResultSets. This ResultSet is converted to an ArrayList of HashMaps where each element of the ArrayList is a single row of the output and the HashMap is a <key, value> pair where the attribute (column) name is the key and the value is the content of that attribute name corresponding to that row. 4. Coding of the stored procedure which constructs the query in Oracle 10g : Stored procedures in oracle are written in PL/SQL. These procedures take input from plain old java object which is in turn called by servlet. The stored procedures are the ones which generate required SQL query string. In procedures, logic is written to separate and store input into list. First all the required tables are fetched and stored in a variable. To fetch all tables used, attributes selected and condition given is used (i.e) all tables to which these attributes and condition fields belong to are fetched. Using topological sort a path to connect tables is established where primary key, foreign key relationship tables are joined if a path exists between them. This is stored in a string variable. All the generated variables are used to generate the query in proper SQL format. If any variable could not be fetched or if path does is not present, then an error status message is generated. If no errors are present then the generated SQL query is returned, else error message is returned. 8
5. Screenshots 1. The attributes to be displayed to the user is given in a list and user selects the required attributes. The screen for selecting attributes: 2. After selecting required attributes: 9
3. After selecting required attributes, save button is clicked to save those attributes and next button to take to next page. In the next page other inputs as to how to order the output values, join condition of tables and the condition on which to fetch the attributes is present. The screen of next page 4. After selecting required inputs, the screen would be: 10
5. After selecting all required inputs, save button has to be clicked to generate the SQL query. The screenshot of SQL generated query is: 6. Now the ad hoc query is generated. The query has to be run on Oracle to get the required output. On click of Run button, the query is run and output is generated. The output is displayed in the next page. The screenshot of the output is: 11
6. Individual Contribution Rashmi: Module 1 Designing the GUI Niranjani:Module 2 Coding of Servlets which are called from the GUI on submitting the form: Bratati: Module 3 Coding of JavaBeans which in turn call Plain Old Java Objects that return values to the GUI. Prof.Chandrashekhar Ramanathan:Module 4 Coding of the stored procedure which constructs the query in Oracle 10G : 7. Future scope of work The current running version of the product does not support a complete abstraction of the database entities. In the running product we still have to supply properly structured SQL code for the where clause which requires the entity names present in the database.the future work which can be done to enhance this project are: 1.One more abstraction level can be added which will construct structured SQL where-clauses from userfriendly where-clauses. 2. The product can be developed for mysql and other existing databases and can be customised for the clients based on their respective business needs. 3. In addition to this the product can be enabled to support construction of more complex queries. 4. Lastly the product should include of saving the document on the hard disk and also export them to other document formats like.doc or pdf. 8. Conclusion Ad Hoc Query tool is an user friendly tool which can be used to build SQL queries and run them. This project has given us exposure to oracle procedures, and web service applications. We learnt how 4 tier architecture implementation abstracts the layers from each others. Overall this project has been an eye opener towards Product Development. 9. References [1] http://en.wikipedia.org/wiki/ad_hoc_query [2] Sofware Design Specification submitted as part of Project Family-A in the course work Data Modeling by Ramachandra Kousik A.S, Amit Jain, Avinash Mehta, Bodhisatta Barman Roy and Niranjani S. 12