Previous Next Table of Contents

6. SQL generation

Every time the user presses a button like query or update, the program will generate a SQL statement to perform the appropriate operation on the database. The following paragraphs describe how the statement is generated.

6.1 Query

The Query button generate a SELECT statement. The list of columns comes from the columns entry in the input array (all the columns by default).

The WHERE clause is built from all the fields that hold data (including the hidden ones if there are any).

For non character columns, the comparison operator used is =. For character columns, it is LIKE.

Example: for a screen with custid, custname, custfirstname, custsomenumber fields, where data was entered in custname (xxx) and custsomenumber (yyy), the statement would be:


SELECT custid,custname,custfirstname,custsomenumber from customers
WHERE custname LIKE 'xxx' AND custsomenumber = yyy
 

If a numeric field begins with '<' or '>', whatever is entered in the field will be used as a condition in the WHERE clause, and AND'ed with the rest. (Ex: you could enter ">10" or "<>1234" , or ">10 and custsomenumber<20).

6.2 Add

The Add button generates an INSERT statement. All fields which hold data are used for the values, the others are not listed. Char fields are suitably quoted. With the same example as above, the SQL statement would be:


INSERT INTO customers(custname,custsomenumber) VALUES('xxx',yyy) 
 

There is no explicitely provided way to insert a NULL value (and certainly none for a char field).

If the table's primary key is a serial or auto_increment field, and the corresponding field is set as "noentry", the value for the field is reset to "" before inserting to let auto_increment do its job.

There seems to be no way to retrieve the auto_increment attribute from a mysql client program, so that, when using MySQL, we make the assumption that if an integer field is a primary key, it also has the auto_increment attribute. If the field is also set as noentry, it will be reset before inserting.

6.3 Update

The Update button generates an UPDATE statement. There are two issues: the WHERE clause and the values.

The WHERE clause is built from the columns that were designated in the updateindex list (if no such list was explicitely indicated, sqlscreens tries to use the primary index columns for the table. If there is no primary index, no updateindex list is built, and no Update button is created, neither can you run an update by typing Esc u).

The values in the WHERE clause are taken from those that were saved when the last Query (or Next, Rewind, Reset) was performed, which means that it is possible to update the columns in the primary index. If you try an update without having performed some query before, you'll get strange error messages about missing array entries.

The values for the update are taken from the screen fields (including the possible hidden ones). All fields whose value is different from the saved value are used. If no value changed, no update is performed (and an error dialog is shown).

As opposed to what happens for SELECT and INSERT, even the fields with no data are used. For char fields, the columns are set to '', for other types, they are set to NULL. This is somewhat arbitrary, but we like it like this.

6.4 Delete

The Delete button generates a DELETE statement. The WHERE clause is built like the SELECTs, except that no LIKE operators are used.

If some columns have NULL or zero-length string values, they will not be used in the WHERE clause. This means that more rows than expected could sometimes be affected by the statement.

For this reason, the program will create a dialog screen and ask for confirmation if more than one row would be affected by a DELETE statement.


Previous Next Table of Contents