There is no strictly defined SQL grammar. On the contrary, there are probably as many SQL dialects as databases of independent vendors. However, some ODBC SQL grammar should be supported by the ODBC driver for every DBMS. Taking into account the extended ODBC SQL grammar, DALE offers four special classes: SQL_SELECT, SQL_INSERT, SQL_UPDATE and SQL_DELETE.
In contrast of the QUERY, these classes allow to construct a query by filling up the contents of the predefined set of sections which depends on a type of the SQL statement. Usage of these classes adds two new facilities. First, you can fill up query's sections in any order, obtain and modify the contents of any section and so on. Second, an additional checking of section's contents is carried out, and besides, on the client side.
The following SQL SELECT statement:
SELECT ProdName, UnitPrice / 100, Customer.Country FROM Product, Orders, Customer WHERE Product.ProdId = Orders.ProdId AND Orders.CustId = Customer.CustId AND Customer.Country = `Canada' OR Customer.Country = `Russia' ORDER BY ProdName
can be coded like this:
q: SQL_SELECT ... !!q.select_ ("ProdName, UnitPrice / 100, Customer.Country") q.from_("Product, Orders, Customer") q.where ("Product.ProdId = Orders.ProdId") q.and_where ("Orders.CustId = Customer.CustId") q.and_where ("Customer.Country = `Canada'") q.or_where ("Customer.Country = `Russia'") q.order_by ("ProdName")
The following SQL INSERT statement:
INSERT INTO Product (ProdName, UnitPrice) VALUES (`Red Hot Chili Pepper', 101.0)
can be coded like this:
q: SQL_INSERT ... !!q.insert_into ("Product") q.columns ("ProdName, UnitPrice") q.values ("Red Hot Chili Pepper", 101.0)
and executed on some database:
q.execute_on (database)
The following SQL UPDATE statement:
UPDATE Product SET ProdName = `Finnish salami', UnitPrice = 2.5 WHERE ProdName = `Russian kolbasa'
can be coded like this:
q: SQL_UPDATE ... !!q.update ("Product") q.set (<<"ProdName", "UnitPrice">>, <<"Finnish salami", 2.5>>) q.where ("ProdName = `Russian kolbasa'")
and executed on some database:
q.execute_on (database)
The following SQL DELETE statement:
DELETE FROM Product WHERE ProdName LIKE `Spice %'
can be coded like this:
q: SQL_DELETE ... !!q.compose ("Product", "ProdName LIKE `Spice %'")
and executed on some database:
q.execute_on (database)
This is another kind of a query which is intended to deal mainly with stored procedures. It is clear, that you can act only if stored procedures are supported in the corresponding data source.
In contrast of an arbitrary query, to execute a stored procedure you have to open it at first via the open feature. The first argument is the database to attach to; the second one denotes procedure's name. At that, if there's no such procedure in the database an exception will be thrown - one may deal only with existing procedures.
Stored procedures sometimes return a value. DB_PROCEDURE class allows to obtain it with the help of the result_value attribute. Note, that this attribute is not Void when and only when result_available is TRUE.
Stored procedure is an object of database which has a name, owner and qualifier. Besides, a procedure can have some textual description in database. If you interesting in this information you can use the name, owner, qualifier and remarks attributes respectively.
Often, if stored procedures are supported in some data source, there is a method to enumerate their parameters, find out their types, etc. The DB_PROCEDURE class doesn't provide any additional means, just obtains the parameters catalog for the given procedure from the database instead of parsing the query's body.
For instance, you want to execute the get_phone_number stored procedure which accepts a string as person's name ("Name"), finds that person in the PEOPLE table and returns his (her) phone number. The following code makes that:
proc : DB_PROCEDURE phone: STRING ... !!proc.open (database, "get_phone_number") proc.parameters.put ("Kate Margaritova", "Name") proc.execute if proc.result_available then phone := proc.result_value.to_string end
See also the PROCEDURES and FIELD classes for more information.