» By Joren on Saturday 29 November 2008

Uit de lijst van postcodes van alle Belgische steden heb ik een SQL-bestand samengesteld. De gegevens bevatten de postcode zelf, de naam van de stad, de naam van de stad in hoofdletters en een veld “structure” waaruit de gemeente-deelgemeente relatie gehaald kan worden als er op gesorteerd wordt. Dit zijn bijvoorbeeld de deelgemeentes van Chimay.
6460 CHIMAY
6460 Bailièvre
6460 Robechies
6460 Saint-Remy (Ht.)
6460 Salles
6460 Villers-la-Tour
6461 Virelles
6462 Vaulx-lez-Chimay
6463 Lompret
6464 Baileux
6464 Bourlers
6464 Forges
6464 l'Escaillère
6464 Rièzes
Het sorteren kan in PostgreSQL met deze SQL instructie: order by translate(structure, ' ', 'z')
. Het SQL-script zelf is een lijst van INSERT INTO
SQL-Statements.
insert into cities(zipcode,name,up,structure) VALUES ('1790','Affligem','AFFLIGEM','1790 AFFLIGEM');
insert into cities(zipcode,name,up,structure) VALUES ('9051','Afsnee','AFSNEE','9051 Afsnee');
insert into cities(zipcode,name,up,structure) VALUES ('5544','Agimont','AGIMONT','5544 Agimont');
...
Dit is het SQL-bestand met een lijst van alle Belgische postcodes en steden. Hopelijk is hier iemand ooit iets mee.
Code and 0110.be
» By Joren on Monday 10 November 2008

While working at the Vooruit Arts Centre I got the assignment to create a tool to query an Oracle database with ticketing data. There were a few requirements for the Query Tool, in the current version all of these are met:
- First of all it had to be easy to execute complex queries, no knowledge of SQL should be required for the end user.
- The results of the queries should be exportable to an easy to analyse format. * Editing queries or adding new ones should be doable, by someone knowledgeable with SQL.
- The Query Tool should be easy to configure and database independent.
- Should work on Windows, Mac OS X and Linux.
By publishing the Query Tool on my website I hope that the fruits of my labour can be enjoyed by a wider audience. To see it in action you can give it a spin. A recent version, version 6, of the JRE is needed.
How Do I Use The Query Tool?
The program supports two ways to query a database:
- A number of predefined queries can be selected and executed. Depending on the selected query, zero or more parameters are required. E.g. the screen shot below depicts a query with one parameter: a product category. When the query is executed all the products in the selected category are fetched.
- The tab “SQL” can be used to execute arbitrary SQL-instructions.
The two buttons below are self explanatory. When the button “CVS Export” is hit a CVS file is created in a configured directory.
Depending on the complexity of a query it can take a long time before results are returned. Because the application is multithreaded the user interface remains responsive and the query can be stopped at any time.
The contents of the tab “log” gives you an idea what the application does. When something goes awry while executing a query a message appears in this tab.
The tab “Config” can be used to set configuration parameters. The tab “Help” contains… helpful information.
How Do I Add My Own Queries?
The list of predefined queries is constructed by iterating over SQL-files in a configured directory. Adding additional queries to the program is easy, just add an extra SQL-file to the directory. An SQL-file should have the following format, otherwise it is ignored:
TITEL
----
DESCRIPTION
----
SQL-INSTRUCTION with zero or more !{PARAMETERS}!
In the screen shot above this query is visible:
Select products in category
----
Select all the products in a category.
----
SELECT * FROM
products WHERE categoryid = !{category}!
To make the queries dynamic the Query Tool supports different kinds of parameters. A parameter has this form: !{type name}!
, the name is optional. If there is a name specified it is used as a label in the interface, otherwise type
is used. There are three types of parameters:
- Parameters that define a type. For each type a corresponding user interface is rendered. E.g. for the type string a text field is rendered. The supported types are:
!{string}!
!{boolean}!
!{double}!
!{date}!
!{integer}!
- Parameters for raw SQL. A textfield is rendered, the contents is directly injected in the SQL-query. It has this format:
!{sql}!
- Parameters for lists. In the example above a list parameter is used. These lists are fetched from the database. E.g. a list of categories. The SQL-instruction and name of the list parameters can be configured.
If you want to use your own database you need to configure the database connection string. The program uses JDBC to connect to the database. It uses metadata provided by the JDBC layer. If your database has a JDBC driver with support for metadata the Query Tool will work correctly. The JDBC driver must be included in the classpath.
Credits
The Query Tool uses the famfamfam mini icons.
For demoing purposes the executable contains a lightweight hsql database. The data in the database is a modified version of the Microsoft Northwind database. The northwind hsql database is created with this SQL-script.
Downloads
Code, Vooruit, Portfolio, and 0110.be
vooruit_logo.png, ui.png, QueryTool.jar, QueryTool.source.zip, northwind.hsql.zip, and northwind.script.txt